Skip to content

Conversation

@lum1n0us
Copy link
Contributor

@lum1n0us lum1n0us commented Nov 8, 2024

Based on #3893

@lum1n0us lum1n0us force-pushed the feat/inst_linking_table branch from 0e0841f to 39c2cf1 Compare November 10, 2024 14:07
@lum1n0us lum1n0us marked this pull request as ready for review November 11, 2024 07:02
@lum1n0us lum1n0us force-pushed the feat/inst_linking_table branch 2 times, most recently from be499a7 to f5b94f5 Compare November 20, 2024 05:55
@lum1n0us lum1n0us force-pushed the feat/inst_linking_table branch from f5b94f5 to d22f1ea Compare November 21, 2024 07:01
@lum1n0us lum1n0us force-pushed the feat/inst_linking_table branch from d22f1ea to 4877adf Compare November 21, 2024 07:07
memory_deinstantiate(memory);

#if WASM_ENABLE_MULTI_MODULE == 0
uint16 rc = BH_ATOMIC_16_FETCH_OR(memory->ref_count, 0);
Copy link
Collaborator

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);
Copy link
Collaborator

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);
Copy link
Collaborator

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;
Copy link
Collaborator

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?

Copy link
Contributor Author

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 * 3

Copy link
Collaborator

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);
Copy link
Collaborator

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);
Copy link
Collaborator

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?

Copy link
Collaborator

@wenyongh wenyongh left a 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks.

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
Copy link
Collaborator

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
        {
            ...
        }

if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) {
read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
}
#else
Copy link
Collaborator

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
        {
            ...
        }


/* TBC: sync up with AOT_CURRENT_VERSION in config.h */
/* v3 doesn't emit names */
if (module->package_version >= 4) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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

read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable);
}
#endif
{
Copy link
Collaborator

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

Copy link
Contributor Author

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);
        }

Copy link
Collaborator

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
        {
            ...
        }

Copy link
Contributor Author

@lum1n0us lum1n0us Dec 4, 2024

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
{
Copy link
Collaborator

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

Copy link
Contributor Author

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);
        }

Copy link
Collaborator

@wenyongh wenyongh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lum1n0us lum1n0us merged commit ee2d60b into bytecodealliance:dev/instantiate_linking Dec 6, 2024
383 checks passed
@lum1n0us lum1n0us deleted the feat/inst_linking_table branch December 6, 2024 02:13
lum1n0us added a commit to lum1n0us/wasm-micro-runtime that referenced this pull request Feb 19, 2025
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants