Skip to content

Commit 3746534

Browse files
authored
Add table type API support (#3515)
Add `wasm_runtime_get_export_table_inst` and `wasm_table_get_func_inst`, and related wasm_table_type_get_xxx APIs.
1 parent 72f74b7 commit 3746534

15 files changed

Lines changed: 590 additions & 245 deletions

File tree

core/iwasm/aot/aot_loader.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,24 +1330,25 @@ load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
13301330

13311331
/* keep sync with aot_emit_table_info() aot_emit_aot_file */
13321332
for (i = 0; i < module->import_table_count; i++, import_table++) {
1333-
read_uint8(buf, buf_end, import_table->elem_type);
1334-
read_uint8(buf, buf_end, import_table->table_flags);
1335-
read_uint8(buf, buf_end, import_table->possible_grow);
1333+
read_uint8(buf, buf_end, import_table->table_type.elem_type);
1334+
read_uint8(buf, buf_end, import_table->table_type.flags);
1335+
read_uint8(buf, buf_end, import_table->table_type.possible_grow);
13361336
#if WASM_ENABLE_GC != 0
1337-
if (wasm_is_type_multi_byte_type(import_table->elem_type)) {
1337+
if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) {
13381338
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
13391339
}
13401340
#endif
1341-
read_uint32(buf, buf_end, import_table->table_init_size);
1342-
read_uint32(buf, buf_end, import_table->table_max_size);
1341+
read_uint32(buf, buf_end, import_table->table_type.init_size);
1342+
read_uint32(buf, buf_end, import_table->table_type.max_size);
13431343
#if WASM_ENABLE_GC != 0
1344-
if (wasm_is_type_multi_byte_type(import_table->elem_type)) {
1344+
if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) {
13451345
read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
13461346

1347-
ref_type.ref_type = import_table->elem_type;
1347+
ref_type.ref_type = import_table->table_type.elem_type;
13481348
/* TODO: check ref_type */
1349-
if (!(import_table->elem_ref_type = wasm_reftype_set_insert(
1350-
module->ref_type_set, &ref_type))) {
1349+
if (!(import_table->table_type.elem_ref_type =
1350+
wasm_reftype_set_insert(module->ref_type_set,
1351+
&ref_type))) {
13511352
set_error_buf(error_buf, error_buf_size,
13521353
"insert ref type to hash set failed");
13531354
return false;
@@ -1383,23 +1384,23 @@ load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
13831384

13841385
/* Create each table data segment */
13851386
for (i = 0; i < module->table_count; i++, table++) {
1386-
read_uint8(buf, buf_end, table->elem_type);
1387-
read_uint8(buf, buf_end, table->table_flags);
1388-
read_uint8(buf, buf_end, table->possible_grow);
1387+
read_uint8(buf, buf_end, table->table_type.elem_type);
1388+
read_uint8(buf, buf_end, table->table_type.flags);
1389+
read_uint8(buf, buf_end, table->table_type.possible_grow);
13891390
#if WASM_ENABLE_GC != 0
1390-
if (wasm_is_type_multi_byte_type(table->elem_type)) {
1391+
if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) {
13911392
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
13921393
}
13931394
#endif
1394-
read_uint32(buf, buf_end, table->table_init_size);
1395-
read_uint32(buf, buf_end, table->table_max_size);
1395+
read_uint32(buf, buf_end, table->table_type.init_size);
1396+
read_uint32(buf, buf_end, table->table_type.max_size);
13961397
#if WASM_ENABLE_GC != 0
1397-
if (wasm_is_type_multi_byte_type(table->elem_type)) {
1398+
if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) {
13981399
read_uint32(buf, buf_end, ref_type.ref_ht_common.heap_type);
13991400

1400-
ref_type.ref_type = table->elem_type;
1401+
ref_type.ref_type = table->table_type.elem_type;
14011402
/* TODO: check ref_type */
1402-
if (!(table->elem_ref_type = wasm_reftype_set_insert(
1403+
if (!(table->table_type.elem_ref_type = wasm_reftype_set_insert(
14031404
module->ref_type_set, &ref_type))) {
14041405
set_error_buf(error_buf, error_buf_size,
14051406
"insert ref type to hash set failed");

core/iwasm/aot/aot_runtime.c

Lines changed: 109 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -618,23 +618,23 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
618618
for (i = 0; i != module_inst->table_count; ++i) {
619619
if (i < module->import_table_count) {
620620
AOTImportTable *import_table = module->import_tables + i;
621-
tbl_inst->cur_size = import_table->table_init_size;
621+
tbl_inst->cur_size = import_table->table_type.init_size;
622622
tbl_inst->max_size =
623623
aot_get_imp_tbl_data_slots(import_table, false);
624+
tbl_inst->elem_type = module->tables[i].table_type.elem_type;
624625
#if WASM_ENABLE_GC != 0
625-
tbl_inst->elem_type = module->tables[i].elem_type;
626626
tbl_inst->elem_ref_type.elem_ref_type =
627-
module->tables[i].elem_ref_type;
627+
module->tables[i].table_type.elem_ref_type;
628628
#endif
629629
}
630630
else {
631631
AOTTable *table = module->tables + (i - module->import_table_count);
632-
tbl_inst->cur_size = table->table_init_size;
632+
tbl_inst->cur_size = table->table_type.init_size;
633633
tbl_inst->max_size = aot_get_tbl_data_slots(table, false);
634+
tbl_inst->elem_type = module->tables[i].table_type.elem_type;
634635
#if WASM_ENABLE_GC != 0
635-
tbl_inst->elem_type = module->tables[i].elem_type;
636636
tbl_inst->elem_ref_type.elem_ref_type =
637-
module->tables[i].elem_ref_type;
637+
module->tables[i].table_type.elem_ref_type;
638638
#endif
639639
}
640640

@@ -1183,6 +1183,75 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
11831183
return true;
11841184
}
11851185

1186+
AOTFunctionInstance *
1187+
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
1188+
{
1189+
AOTModule *module = (AOTModule *)module_inst->module;
1190+
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
1191+
AOTFunctionInstance *export_funcs =
1192+
(AOTFunctionInstance *)module_inst->export_functions;
1193+
uint32 i;
1194+
1195+
/* export functions are pre-instantiated */
1196+
for (i = 0; i < module_inst->export_func_count; i++) {
1197+
if (export_funcs[i].func_index == func_idx)
1198+
return &export_funcs[i];
1199+
}
1200+
1201+
exception_lock(module_inst);
1202+
1203+
/* allocate functions array if needed */
1204+
if (!extra->functions) {
1205+
uint64 func_count =
1206+
((uint64)module->import_func_count + module->func_count);
1207+
uint64 total_size = func_count * (uint64)sizeof(AOTFunctionInstance *);
1208+
1209+
if ((func_count == 0)
1210+
|| !(extra->functions = runtime_malloc(total_size, NULL, 0))) {
1211+
exception_unlock(module_inst);
1212+
return NULL;
1213+
}
1214+
1215+
extra->function_count = func_count;
1216+
}
1217+
1218+
/* instantiate function if needed */
1219+
bh_assert(func_idx < extra->function_count);
1220+
if (!extra->functions[func_idx]) {
1221+
AOTFunctionInstance *function = (AOTFunctionInstance *)runtime_malloc(
1222+
sizeof(AOTFunctionInstance), NULL, 0);
1223+
if (!function) {
1224+
exception_unlock(module_inst);
1225+
return NULL;
1226+
}
1227+
1228+
if (func_idx < module->import_func_count) {
1229+
/* instantiate function from import section */
1230+
function->is_import_func = true;
1231+
function->func_name = module->import_funcs[func_idx].func_name;
1232+
function->func_index = func_idx;
1233+
function->u.func_import = &module->import_funcs[func_idx];
1234+
}
1235+
else {
1236+
/* instantiate non-import function */
1237+
uint32 ftype_index =
1238+
module->func_type_indexes[func_idx - module->import_func_count];
1239+
function->is_import_func = false;
1240+
function->func_index = func_idx;
1241+
function->u.func.func_type =
1242+
(AOTFuncType *)module->types[ftype_index];
1243+
function->u.func.func_ptr =
1244+
module->func_ptrs[func_idx - module->import_func_count];
1245+
}
1246+
1247+
extra->functions[func_idx] = function;
1248+
}
1249+
1250+
exception_unlock(module_inst);
1251+
1252+
return extra->functions[func_idx];
1253+
}
1254+
11861255
static bool
11871256
init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module,
11881257
char *error_buf, uint32 error_buf_size)
@@ -1490,6 +1559,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
14901559
#if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0
14911560
WASMModuleInstanceExtraCommon *common;
14921561
#endif
1562+
AOTModuleInstanceExtra *extra = NULL;
14931563
const uint32 module_inst_struct_size =
14941564
offsetof(AOTModuleInstance, global_table_data.bytes);
14951565
const uint64 module_inst_mem_inst_size =
@@ -1543,14 +1613,13 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
15431613
module_inst->module = (void *)module;
15441614
module_inst->e =
15451615
(WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
1616+
extra = (AOTModuleInstanceExtra *)module_inst->e;
15461617

15471618
#if WASM_ENABLE_GC != 0
15481619
/* Initialize gc heap first since it may be used when initializing
15491620
globals and others */
15501621
if (!is_sub_inst) {
15511622
uint32 gc_heap_size = wasm_runtime_get_gc_heap_size_default();
1552-
AOTModuleInstanceExtra *extra =
1553-
(AOTModuleInstanceExtra *)module_inst->e;
15541623

15551624
if (gc_heap_size < GC_HEAP_SIZE_MIN)
15561625
gc_heap_size = GC_HEAP_SIZE_MIN;
@@ -1570,8 +1639,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
15701639
#endif
15711640

15721641
#if WASM_ENABLE_MULTI_MODULE != 0
1573-
((AOTModuleInstanceExtra *)module_inst->e)->sub_module_inst_list =
1574-
&((AOTModuleInstanceExtra *)module_inst->e)->sub_module_inst_list_head;
1642+
extra->sub_module_inst_list = &extra->sub_module_inst_list_head;
15751643
ret = wasm_runtime_sub_module_instantiate(
15761644
(WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
15771645
stack_size, heap_size, max_memory_pages, error_buf, error_buf_size);
@@ -1587,7 +1655,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
15871655
goto fail;
15881656

15891657
#if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0
1590-
common = &((AOTModuleInstanceExtra *)module_inst->e)->common;
1658+
common = &extra->common;
15911659
#endif
15921660
#if WASM_ENABLE_BULK_MEMORY != 0
15931661
if (module->mem_init_data_count > 0) {
@@ -1682,7 +1750,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
16821750
#endif
16831751
module_inst->default_wasm_stack_size = stack_size;
16841752

1685-
((AOTModuleInstanceExtra *)module_inst->e)->stack_sizes =
1753+
extra->stack_sizes =
16861754
aot_get_data_section_addr(module, AOT_STACK_SIZES_SECTION_NAME, NULL);
16871755

16881756
#if WASM_ENABLE_PERF_PROFILING != 0
@@ -1885,8 +1953,8 @@ destroy_c_api_frames(Vector *frames)
18851953
void
18861954
aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
18871955
{
1888-
WASMModuleInstanceExtraCommon *common =
1889-
&((AOTModuleInstanceExtra *)module_inst->e)->common;
1956+
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
1957+
WASMModuleInstanceExtraCommon *common = &extra->common;
18901958
if (module_inst->exec_env_singleton) {
18911959
/* wasm_exec_env_destroy will call
18921960
wasm_cluster_wait_for_all_except_self to wait for other
@@ -1923,6 +1991,16 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
19231991
if (module_inst->export_functions)
19241992
wasm_runtime_free(module_inst->export_functions);
19251993

1994+
if (extra->functions) {
1995+
uint32 func_idx;
1996+
for (func_idx = 0; func_idx < extra->function_count; ++func_idx) {
1997+
if (extra->functions[func_idx]) {
1998+
wasm_runtime_free(extra->functions[func_idx]);
1999+
}
2000+
}
2001+
wasm_runtime_free(extra->functions);
2002+
}
2003+
19262004
if (module_inst->func_ptrs)
19272005
wasm_runtime_free(module_inst->func_ptrs);
19282006

@@ -1934,12 +2012,10 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
19342012

19352013
#if WASM_ENABLE_GC != 0
19362014
if (!is_sub_inst) {
1937-
AOTModuleInstanceExtra *extra =
1938-
(AOTModuleInstanceExtra *)module_inst->e;
1939-
if (extra->common.gc_heap_handle)
1940-
mem_allocator_destroy(extra->common.gc_heap_handle);
1941-
if (extra->common.gc_heap_pool)
1942-
wasm_runtime_free(extra->common.gc_heap_pool);
2015+
if (common->gc_heap_handle)
2016+
mem_allocator_destroy(common->gc_heap_handle);
2017+
if (common->gc_heap_pool)
2018+
wasm_runtime_free(common->gc_heap_pool);
19432019
}
19442020
#endif
19452021

@@ -2021,7 +2097,9 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
20212097
void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv,
20222098
uint32 *argv_ret) =
20232099
func_type->quick_aot_entry;
2100+
exec_env->attachment = attachment;
20242101
invoke_native(func_ptr, exec_env, argv, argv_ret);
2102+
exec_env->attachment = NULL;
20252103
ret = !aot_copy_exception(module_inst, NULL);
20262104
}
20272105
else
@@ -2099,6 +2177,7 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
20992177
unsigned argc, uint32 argv[])
21002178
{
21012179
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
2180+
AOTModule *module = (AOTModule *)module_inst->module;
21022181
AOTFuncType *func_type = function->is_import_func
21032182
? function->u.func_import->func_type
21042183
: function->u.func.func_type;
@@ -2108,6 +2187,7 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
21082187
void *func_ptr = function->is_import_func
21092188
? function->u.func_import->func_ptr_linked
21102189
: function->u.func.func_ptr;
2190+
void *attachment = NULL;
21112191
#if WASM_ENABLE_MULTI_MODULE != 0
21122192
bh_list *sub_module_list_node = NULL;
21132193
const char *sub_inst_name = NULL;
@@ -2167,6 +2247,10 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
21672247
hw bound check is enabled */
21682248
#endif
21692249

2250+
if (function->func_index < module->import_func_count) {
2251+
attachment = function->u.func_import->attachment;
2252+
}
2253+
21702254
/* Set exec env, so it can be later retrieved from instance */
21712255
module_inst->cur_exec_env = exec_env;
21722256

@@ -2217,7 +2301,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
22172301
#endif
22182302

22192303
ret = invoke_native_internal(exec_env, function->u.func.func_ptr,
2220-
func_type, NULL, NULL, argv1, argc, argv);
2304+
func_type, NULL, attachment, argv1, argc,
2305+
argv);
22212306

22222307
if (!ret) {
22232308
#ifdef AOT_STACK_FRAME_DEBUG
@@ -2286,8 +2371,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
22862371
}
22872372
#endif
22882373

2289-
ret = invoke_native_internal(exec_env, func_ptr, func_type, NULL, NULL,
2290-
argv, argc, argv);
2374+
ret = invoke_native_internal(exec_env, func_ptr, func_type, NULL,
2375+
attachment, argv, argc, argv);
22912376

22922377
if (aot_copy_exception(module_inst, NULL)) {
22932378
#ifdef AOT_STACK_FRAME_DEBUG
@@ -2888,8 +2973,8 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
28882973
/* Call native function */
28892974
import_func = aot_module->import_funcs + func_idx;
28902975
signature = import_func->signature;
2976+
attachment = import_func->attachment;
28912977
if (import_func->call_conv_raw) {
2892-
attachment = import_func->attachment;
28932978
ret = wasm_runtime_invoke_native_raw(exec_env, func_ptr, func_type,
28942979
signature, attachment, argv,
28952980
argc, argv);

core/iwasm/aot/aot_runtime.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ typedef struct AOTFunctionInstance {
104104
typedef struct AOTModuleInstanceExtra {
105105
DefPointer(const uint32 *, stack_sizes);
106106
WASMModuleInstanceExtraCommon common;
107+
AOTFunctionInstance **functions;
108+
uint32 function_count;
107109
#if WASM_ENABLE_MULTI_MODULE != 0
108110
bh_list sub_module_inst_list_head;
109111
bh_list *sub_module_inst_list;
@@ -507,6 +509,17 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
507509
AOTFunctionInstance *
508510
aot_lookup_function(const AOTModuleInstance *module_inst, const char *name);
509511

512+
/**
513+
* Get a function in the AOT module instance.
514+
*
515+
* @param module_inst the module instance
516+
* @param func_idx the index of the function
517+
*
518+
* @return the function instance found
519+
*/
520+
AOTFunctionInstance *
521+
aot_get_function_instance(AOTModuleInstance *module_inst, uint32_t func_idx);
522+
510523
/**
511524
* Call the given AOT function of a AOT module instance with
512525
* arguments.

0 commit comments

Comments
 (0)