-
Notifications
You must be signed in to change notification settings - Fork 742
[instantiation linking] Create and import WASMTableInstance #3898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[instantiation linking] Create and import WASMTableInstance #3898
Conversation
0e0841f to
39c2cf1
Compare
be499a7 to
f5b94f5
Compare
f5b94f5 to
d22f1ea
Compare
d22f1ea to
4877adf
Compare
…deinstantiation logic
… table instantiation logic and reducing unnecessary assertions
…in AOT loader and runtime
…execution environment
core/iwasm/aot/aot_runtime.c
Outdated
| memory_deinstantiate(memory); | ||
|
|
||
| #if WASM_ENABLE_MULTI_MODULE == 0 | ||
| uint16 rc = BH_ATOMIC_16_FETCH_OR(memory->ref_count, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the ref_count is only available for shared memory, and normally we should access with with g_shared_memory_lock acquired, refer to:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/common/wasm_shared_memory.c#L85-L91
so here per my understanding, the code had better be like:
uint16 rc = 0;
#if WASM_ENABLE_SHARED_MEMORY != 0
if (memory->is_shared_memory) {
#if BH_ATOMIC_16_IS_ATOMIC == 0
os_mutex_lock(&g_shared_memory_lock);
#endif
rc = BH_ATOMIC_16_FETCH_OR(memory->ref_count, 0);
#if BH_ATOMIC_16_IS_ATOMIC == 0
os_mutex_unlock(&g_shared_memory_lock);
#endif
}
#endif
if (rc == 0) {
wasm_runtime_free(memory);
}Or how about adding an API like shared_memory_get_ref_count in wasm_shared_memory.c, and then here:
uint16 rc = 0;
#if WASM_ENABLE_SHARED_MEMORY != 0
if (memory->is_shared_memory)
rc = shared_memory_get_ref_count(memory);
#endif
...| table = | ||
| (AOTTableInstance *)((uint8 *)table | ||
| + offsetof(AOTTableInstance, elems) | ||
| + sizeof(table_elem_type_t) * table->max_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
offsetof(AOTTableInstance, elems) + sizeof(table_elem_type_t) * n are used in many places, maybe we can define it as a macro or function?
|
|
||
| AOTImportTable *import_table = comp_data->import_tables; | ||
| for (uint32 i = 0; i < comp_data->import_table_count; i++, import_table++) { | ||
| offset = align_uint(offset, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had better align_uint(offset, 4)
| * + u32 init_size + u32 max_size | ||
| * + u32 elem_ref_type.heap_type | ||
| */ | ||
| size += sizeof(uint32) * 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems should be size += sizeof(uint32) * 2 since the elem_ref_type.heap_type is optional and is calculated below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// aot_emit_import_table_info()
EMIT_U8(import_table->table_type.elem_type);
EMIT_U8(import_table->table_type.flags);
EMIT_U8(import_table->table_type.possible_grow);
#if WASM_ENABLE_GC != 0
if (comp_ctx->enable_gc && import_table->table_type.elem_ref_type) {
EMIT_U8(
import_table->table_type.elem_ref_type->ref_ht_common.nullable);
}
else
#endif
{
/* emit one placeholder to keep the same size */
EMIT_U8(0);
}
// --------------------- U32
EMIT_U32(import_table->table_type.init_size);
EMIT_U32(import_table->table_type.max_size);
// --------------------- U32 * 3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, thanks.
| memory_deinstantiate(memory); | ||
| #else | ||
| memory_deinstantiate(memory); | ||
| uint16 rc = BH_ATOMIC_16_FETCH_OR(memory->ref_count, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as aot_runtime.c, suggest to add API shared_memory_get_ref_count, and here:
uint16 rc = 0;
#if WASM_ENABLE_SHARED_MEMORY != 0
if (memory->is_shared_memory)
rc = shared_memory_get_ref_count(memory);
#endif
...| } | ||
|
|
||
| table_elem_type_t *table_elems = | ||
| wasm_locate_table_elems(module, table, i); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as aot_runtime.c, should we check whether table_elems is NULL, and free it only when it is not NULL?
… in AOT compilation
…mory support in AOT runtime and interpreter
564d11c to
d8721a6
Compare
wenyongh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with minor comments.
| * + u32 init_size + u32 max_size | ||
| * + u32 elem_ref_type.heap_type | ||
| */ | ||
| size += sizeof(uint32) * 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, thanks.
core/iwasm/aot/aot_loader.c
Outdated
| if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) { | ||
| read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable); | ||
| } | ||
| #else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had better add an else branch when WASM_ENABLE_GC is enabled, how about:
#if WASM_ENABLE_GC != 0
if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) {
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
}
else
#endif
{
...
}
core/iwasm/aot/aot_loader.c
Outdated
| if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) { | ||
| read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable); | ||
| } | ||
| #else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above, how about:
#if WASM_ENABLE_GC != 0
if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) {
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
}
else
#endif
{
...
}
core/iwasm/aot/aot_loader.c
Outdated
|
|
||
| /* TBC: sync up with AOT_CURRENT_VERSION in config.h */ | ||
| /* v3 doesn't emit names */ | ||
| if (module->package_version >= 4) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems that the import table in the older aot file isn't used at all or developer doesn't generate the import table? Maybe we can remove the line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not certain. I'll add a TODO note here, maintain the 'if' control, and run a full compatible test later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original aot table instantiation has bug when there are import tables: module->tables[i].table_type.elem_type and module->tables[i].table_type.elem_ref_type are used to set the table instance's fields, and i will be out of range: there are only module->table_count tables, while i can be module->import_table_count to module->import_table_count + module->table_count - 1. So accessing module->tables[i].table_type.elem_ref_type will access illegal memory. I guess that import table was not used, no one reported the error.
I uploaded a PR to fix it: #3946
… version compatibility in AOT loader
| read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable); | ||
| } | ||
| #endif | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should add else before #endif
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sync up with aot_emit_table_info()
#if WASM_ENABLE_GC != 0
if (comp_ctx->enable_gc
&& comp_data->import_tables[i].table_type.elem_ref_type) {
EMIT_U8(comp_data->import_tables[i]
.table_type.elem_ref_type->ref_ht_common.nullable);
}
else
#endif
{
/* emit one placeholder to keep the same size */
EMIT_U8(0);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so you will add else before #endif?
#if WASM_ENABLE_GC != 0
if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) {
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
}
else
#endif
{
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😨 oops
forgot sync up. Here is another one for main branch. #3940. please take a look
| read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable); | ||
| } | ||
| #endif | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should add else before #endif
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sync up with aot_emit_table_info()
#if WASM_ENABLE_GC != 0
if (comp_ctx->enable_gc
&& comp_data->tables[i].table_type.elem_ref_type) {
EMIT_U8(comp_data->tables[i]
.table_type.elem_ref_type->ref_ht_common.nullable);
}
else
#endif
{
/* emit one placeholder to keep the same size */
EMIT_U8(0);
}
wenyongh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ee2d60b
into
bytecodealliance:dev/instantiate_linking
…alliance#3898) - Implement import table and table creation - Enhance memory de-instantiation checks and adjust stack size for wasm execution environment - Improve shared memory reference handling and enhance table de-instantiation checks
Based on #3893