Implement most missing wasm-c-api APIs (#303) (#676)

Remove unnecessary functions and implement more APIs:
- wasm_##name##same
- wasm##name##as_ref
- wasm_ref_as##name##
- wasm_ref_delete
- wasm_module_validate
- wasm_table_get/set/size
- wasm_memory_size
- wasm_config_new
- wasm_foreign_new

And add more wasm-c-api samples, update the related documen, add more CI rules.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-07-28 21:53:37 +08:00
committed by GitHub
parent 4193949ef5
commit edb184f709
24 changed files with 1872 additions and 450 deletions

View File

@ -2822,9 +2822,12 @@ aot_table_grow(AOTModuleInstance *module_inst, uint32 tbl_idx,
return orig_tbl_sz;
}
if (tbl_inst->cur_size > UINT32_MAX - inc_entries) {
return (uint32)-1;
}
entry_count = tbl_inst->cur_size + inc_entries;
/* prevent from integer overflow */
if (entry_count < tbl_inst->cur_size || entry_count > tbl_inst->max_size) {
if (entry_count > tbl_inst->max_size) {
return (uint32)-1;
}

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@
#ifndef _WASM_C_API_INTERNAL_H
#define _WASM_C_API_INTERNAL_H
#include "wasm_c_api.h"
#include "../include/wasm_c_api.h"
#include "wasm_runtime_common.h"
#ifndef own
@ -19,9 +19,9 @@
/* caller needs to take care resource for the vector itself */
#define DEFAULT_VECTOR_INIT_LENGTH (64)
WASM_DECLARE_VEC(store, *)
WASM_DECLARE_VEC(module, *)
WASM_DECLARE_VEC(instance, *)
WASM_DECLARE_VEC(module, *)
WASM_DECLARE_VEC(store, *)
/* Runtime Environment */
struct wasm_engine_t {
@ -32,6 +32,7 @@ struct wasm_engine_t {
struct wasm_store_t {
wasm_module_vec_t *modules;
wasm_instance_vec_t *instances;
Vector *foreigns;
};
/* Type Representations */
@ -82,8 +83,25 @@ struct wasm_exporttype_t {
};
/* Runtime Objects */
enum wasm_reference_kind {
WASM_REF_foreign,
WASM_REF_func,
WASM_REF_global,
WASM_REF_memory,
WASM_REF_table,
};
struct wasm_host_info {
void *info;
void (*finalizer)(void *);
};
struct wasm_ref_t {
uint32 obj;
wasm_store_t *store;
enum wasm_reference_kind kind;
struct wasm_host_info host_info;
uint32 ref_idx_rt;
WASMModuleInstanceCommon *inst_comm_rt;
};
struct wasm_trap_t {
@ -91,11 +109,22 @@ struct wasm_trap_t {
Vector *frames;
};
struct wasm_foreign_t {
wasm_store_t *store;
enum wasm_reference_kind kind;
struct wasm_host_info host_info;
int32 ref_cnt;
uint32 foreign_idx_rt;
WASMModuleInstanceCommon *inst_comm_rt;
};
struct wasm_func_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_functype_t *type;
bool with_env;
@ -117,10 +146,12 @@ struct wasm_func_t {
};
struct wasm_global_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_globaltype_t *type;
wasm_val_t *init;
/*
@ -132,10 +163,12 @@ struct wasm_global_t {
};
struct wasm_memory_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_memorytype_t *type;
/*
* an index in both memory runtime instance lists
@ -146,10 +179,12 @@ struct wasm_memory_t {
};
struct wasm_table_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
uint16 kind;
struct wasm_host_info host_info;
wasm_tabletype_t *type;
/*
* an index in both table runtime instance lists
@ -160,16 +195,49 @@ struct wasm_table_t {
};
struct wasm_extern_t {
wasm_store_t *store;
wasm_name_t *module_name;
wasm_name_t *name;
wasm_externkind_t kind;
uint8 data[1];
uint8 data[4];
};
struct wasm_instance_t {
wasm_store_t *store;
wasm_extern_vec_t *imports;
wasm_extern_vec_t *exports;
struct wasm_host_info host_info;
WASMModuleInstanceCommon *inst_comm_rt;
};
wasm_ref_t *
wasm_ref_new_internal(wasm_store_t *store,
enum wasm_reference_kind kind,
uint32 obj_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_foreign_t *
wasm_foreign_new_internal(wasm_store_t *store,
uint32 foreign_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_func_t *
wasm_func_new_internal(wasm_store_t *store,
uint16 func_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_global_t *
wasm_global_new_internal(wasm_store_t *store,
uint16 global_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_memory_t *
wasm_memory_new_internal(wasm_store_t *store,
uint16 memory_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
wasm_table_t *
wasm_table_new_internal(wasm_store_t *store,
uint16 table_idx_rt,
WASMModuleInstanceCommon *inst_comm_rt);
#endif /* _WASM_C_API_INTERNAL_H */

View File

@ -3977,57 +3977,6 @@ wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
return false;
}
uint8 *
wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
WASMModuleInstance *module_inst =
(WASMModuleInstance *)module_inst_comm;
WASMMemoryInstance *memory_inst =
module_inst->memories[memory_inst_idx];
return memory_inst->memory_data;
}
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst_comm->module_type == Wasm_Module_AoT) {
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
AOTMemoryInstance *memory_inst =
((AOTMemoryInstance**)module_inst->memories.ptr)[memory_inst_idx];
return memory_inst->memory_data.ptr;
}
#endif
return NULL;
}
uint32
wasm_runtime_get_memory_data_size(
const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
WASMModuleInstance *module_inst =
(WASMModuleInstance *)module_inst_comm;
WASMMemoryInstance *memory_inst =
module_inst->memories[memory_inst_idx];
return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
}
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst_comm->module_type == Wasm_Module_AoT) {
AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
AOTMemoryInstance *memory_inst =
((AOTMemoryInstance**)module_inst->memories.ptr)[memory_inst_idx];
return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
}
#endif
return 0;
}
static inline bool
argv_to_params(wasm_val_t *out_params,
const uint32 *argv,
@ -4058,6 +4007,23 @@ argv_to_params(wasm_val_t *out_params,
u32[0] = *argv++;
u32[1] = *argv++;
break;
#if WASM_ENABLE_REF_TYPES != 0
case VALUE_TYPE_EXTERNREF:
param->kind = WASM_ANYREF;
if (NULL_REF == *argv) {
param->of.ref = NULL;
}
else {
if (!wasm_externref_ref2obj(*argv,
(void **)&param->of.ref)) {
return false;
}
}
argv++;
break;
#endif
default:
return false;
}
@ -4067,7 +4033,8 @@ argv_to_params(wasm_val_t *out_params,
}
static inline bool
results_to_argv(uint32 *out_argv,
results_to_argv(WASMModuleInstanceCommon *module_inst,
uint32 *out_argv,
const wasm_val_t *results,
WASMType *func_type)
{
@ -4087,6 +4054,15 @@ results_to_argv(uint32 *out_argv,
*argv++ = u32[0];
*argv++ = u32[1];
break;
#if WASM_ENABLE_REF_TYPES != 0
case VALUE_TYPE_EXTERNREF:
if (!wasm_externref_obj2ref(module_inst, result->of.ref,
argv)) {
return false;
}
argv++;
break;
#endif
default:
return false;
}
@ -4134,7 +4110,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
char trap_message[128] = { 0 };
bh_memcpy_s(
trap_message, 127, trap->message->data,
(trap->message->size < 127 ? trap->message->size : 127));
(trap->message->size < 127 ? (uint32)trap->message->size : 127));
wasm_runtime_set_exception(module_inst, trap_message);
}
else {
@ -4152,7 +4128,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
goto fail;
}
if (!results_to_argv(argv, results, func_type)) {
if (!results_to_argv(module_inst, argv, results, func_type)) {
wasm_runtime_set_exception(module_inst, "unsupported result type");
goto fail;
}

View File

@ -796,14 +796,6 @@ wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
uint32 *out_min_size,
uint32 *out_max_size);
uint8 *
wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx);
uint32
wasm_runtime_get_memory_data_size(const WASMModuleInstanceCommon *module_inst_comm,
uint32 memory_inst_idx);
bool
wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
void *func_ptr, WASMType *func_type,

View File

@ -25,6 +25,7 @@ get_tbl_inst_offset(const AOTCompContext *comp_ctx,
while (i < tbl_idx && i < comp_ctx->comp_data->import_table_count) {
offset += offsetof(AOTTableInstance, data);
/* avoid loading from current AOTTableInstance */
offset += sizeof(uint32) * aot_get_imp_tbl_data_slots(imp_tbls + i);
++i;
}
@ -37,6 +38,7 @@ get_tbl_inst_offset(const AOTCompContext *comp_ctx,
i -= comp_ctx->comp_data->import_table_count;
while (i < tbl_idx && i < comp_ctx->comp_data->table_count) {
offset += offsetof(AOTTableInstance, data);
/* avoid loading from current AOTTableInstance */
offset += sizeof(uint32) * aot_get_tbl_data_slots(tbls + i);
++i;
}

View File

@ -573,6 +573,12 @@ WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
own wasm_trap_t**
);
// please refer to wasm_runtime_instantiate(...) in core/iwasm/include/wasm_export.h
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args(
wasm_store_t*, const wasm_module_t*, const wasm_extern_t *const imports[],
own wasm_trap_t**, const uint32_t stack_size, const uint32_t heap_size
);
WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);
@ -764,6 +770,7 @@ static inline void* wasm_val_ptr(const wasm_val_t* val) {
#define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .of = {.ref = r}}
#define WASM_INIT_VAL {.kind = WASM_ANYREF, .of = {.ref = NULL}}
#define KILOBYTE(n) ((n) * 1024)
///////////////////////////////////////////////////////////////////////////////

View File

@ -7881,7 +7881,7 @@ fail_data_cnt_sec_require:
#if WASM_ENABLE_REF_TYPES != 0
case WASM_OP_TABLE_INIT:
{
uint8 seg_ref_type, tbl_ref_type;
uint8 seg_ref_type = 0, tbl_ref_type = 0;
if (!wasm_get_ref_types_flag()) {
goto unsupported_opcode;

View File

@ -476,9 +476,11 @@ tables_instantiate(const WASMModule *module,
/* instantiate tables from import section */
import = module->import_tables;
for (i = 0; i < module->import_table_count; i++, import++) {
uint32 max_size_fixed = 0;
#if WASM_ENABLE_MULTI_MODULE != 0
WASMTableInstance *table_inst_linked = NULL;
WASMModuleInstance *module_inst_linked = NULL;
if (import->u.table.import_module) {
if (!(module_inst_linked =
get_sub_module_inst(module_inst, import->u.table.import_module))) {
@ -499,12 +501,14 @@ tables_instantiate(const WASMModule *module,
else
#endif
{
/* in order to save memory, alloc resource as few as possible */
max_size_fixed = import->u.table.possible_grow
? import->u.table.max_size
: import->u.table.init_size;
/* it is a built-in table, every module has its own */
total_size = offsetof(WASMTableInstance, base_addr);
total_size +=
import->u.table.possible_grow
? sizeof(uint32) * (uint64)import->u.table.max_size
: sizeof(uint32) * (uint64)import->u.table.init_size;
total_size += (uint64)max_size_fixed * sizeof(uint32);
}
if (!(table = tables[table_index++] = runtime_malloc
@ -515,6 +519,7 @@ tables_instantiate(const WASMModule *module,
/* Set all elements to -1 to mark them as uninitialized elements */
memset(table, -1, (uint32)total_size);
#if WASM_ENABLE_MULTI_MODULE != 0
table->table_inst_linked = table_inst_linked;
if (table_inst_linked != NULL) {
@ -527,21 +532,26 @@ tables_instantiate(const WASMModule *module,
{
table->elem_type = import->u.table.elem_type;
table->cur_size = import->u.table.init_size;
table->max_size = import->u.table.max_size;
table->max_size = max_size_fixed;
}
}
/* instantiate tables from table section */
for (i = 0; i < module->table_count; i++) {
uint32 max_size_fixed = 0;
total_size = offsetof(WASMTableInstance, base_addr);
#if WASM_ENABLE_MULTI_MODULE != 0
/* in case, a module which imports this table will grow it */
total_size += sizeof(uint32) * (uint64)module->tables[i].max_size;
max_size_fixed = module->tables[i].max_size;
#else
total_size += module->tables[i].possible_grow
? sizeof(uint32) * (uint64)module->tables[i].max_size
: sizeof(uint32) * (uint64)module->tables[i].init_size;
max_size_fixed =
module->tables[i].possible_grow
? module->tables[i].max_size
: module->tables[i].init_size;
#endif
total_size += sizeof(uint32) * (uint64)max_size_fixed;
if (!(table = tables[table_index++] = runtime_malloc
(total_size, error_buf, error_buf_size))) {
tables_deinstantiate(tables, table_count);
@ -552,7 +562,7 @@ tables_instantiate(const WASMModule *module,
memset(table, -1, (uint32)total_size);
table->elem_type = module->tables[i].elem_type;
table->cur_size = module->tables[i].init_size;
table->max_size = module->tables[i].max_size;
table->max_size = max_size_fixed;
#if WASM_ENABLE_MULTI_MODULE != 0
table->table_inst_linked = NULL;
#endif
@ -2150,10 +2160,12 @@ wasm_enlarge_table(WASMModuleInstance *module_inst,
return false;
}
if (inc_entries > UINT32_MAX - table_inst->cur_size) {
return false;
}
entry_count = table_inst->cur_size + inc_entries;
/* prevent from integer overflow */
if (entry_count < table_inst->cur_size
|| entry_count > table_inst->max_size) {
if (entry_count > table_inst->max_size) {
return false;
}

View File

@ -456,4 +456,3 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env);
#endif
#endif /* end of _WASM_RUNTIME_H */