wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

Fixed the link failure when the sequence of native imports and binary imports are different.

Open JakeOrange opened this issue 1 year ago • 10 comments

When the sequence of native imports and binary imports are different, it can't find right native method. Then will have a error tips "out of bounds memory access".

JakeOrange avatar Jan 10 '24 06:01 JakeOrange

The point is func_idx_rt can't be operated from host side, which means if developers create a wasm_func_t with wasm_func_new and func_idx_rt will be -1 and they can't assign the right value. Plus, developers can't predicate positions of imports.

for the problem in title, you might want to see https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/samples/wasm-c-api-imports

lum1n0us avatar Jan 11 '24 08:01 lum1n0us

The point is func_idx_rt can't be operated from host side, which means if developers create a wasm_func_t with wasm_func_new and func_idx_rt will be -1 and they can't assign the right value. Plus, developers can't predicate positions of imports.

for the problem in title, you might want to see https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/samples/wasm-c-api-imports

I understand your replay. The cause is that there is a wrong bind in fill in module_inst->e->c_api_func_imports of wasm_instance_new_with_args. func_ptr_linked and env_arg are binded wrongly.

JakeOrange avatar Jan 11 '24 12:01 JakeOrange

i don't understand what this PR is trying to solve. maybe you can add a small sample to samples/ demonstrate the issue.

yamt avatar Jan 15 '24 05:01 yamt

IIUC, @JakeOrange is facing a similar problem in samples/wasi-c-api-imports.

lum1n0us avatar Jan 15 '24 06:01 lum1n0us

In the case " WebAssembly.instantiate(fetch(wasmBinaryFile), info)", info is a two-dimensional array. Before calling the "wasm_instance_new_with_arg", the imported elements were exported and ware stored as a map. The cause is that there is a wrong bind in fill in module_inst->e->c_api_func_imports of wasm_instance_new_with_args. func_ptr_linked and env_arg are binded wrongly. I had change the map to vector for storing the impoted elements. It ensure the corrent order and bind rightly. The result has become right. Thank you!

JakeOrange avatar Jan 15 '24 06:01 JakeOrange

Again, we thought the big problem in this line is

CApiFuncImport *linkFunc = func_import + func_host->func_idx_rt;

a host function should not operate, or predicate, the value of func_idx_rt. Maybe in one case, with a fixed .wasm, it will work. But in a generic way, we still suggest follow samples/wasm-c-api-imports.

lum1n0us avatar Jan 15 '24 07:01 lum1n0us

Again, we thought the big problem in this line is

CApiFuncImport *linkFunc = func_import + func_host->func_idx_rt;

a host function should not operate, or predicate, the value of func_idx_rt. Maybe in one case, with a fixed .wasm, it will work. But in a generic way, we still suggest follow samples/wasm-c-api-imports.

I had implemented it according to sample. If func_idx_rt is -1, do_link will fail. I can't understand which scenario will happen?

JakeOrange avatar Jan 15 '24 09:01 JakeOrange

In the case " WebAssembly.instantiate(fetch(wasmBinaryFile), info)", info is a two-dimensional array. Before calling the "wasm_instance_new_with_arg", the imported elements were exported and ware stored as a map. The cause is that there is a wrong bind in fill in module_inst->e->c_api_func_imports of wasm_instance_new_with_args. func_ptr_linked and env_arg are binded wrongly. I had change the map to vector for storing the impoted elements. It ensure the corrent order and bind rightly. The result has become right. Thank you!

is there WebAssembly.instantiate implementation backed by WAMR?

yamt avatar Jan 15 '24 11:01 yamt

@JakeOrange

If func_idx_rt is -1, do_link will fail

Usually, a developer creates a host function with wasm_func_new(), and then pass it to wasm_instance_new() as a part of wasm_extern_vec_t *imports(need to make sure the right order of host functions in imports).

wasm_func_new() will assign -1 to func_idx_rt because in that phase, there is no such knowledge about a. will the host function be used to match an import function. b. which import function will be satisfied by this host function.

Later, in wasm_instance_new(), if the function type fits, runtime will assign the right value to func_idx_rt. So don't be worried about func_idx_rt from the host side.

@yamt

is there WebAssembly.instantiate implementation backed by WAMR

Nay. I guess it is a JS API.

lum1n0us avatar Jan 15 '24 12:01 lum1n0us

So don't be worried about func_idx_rt from the host side. Yes. It is my answer on the 3th floor.

JakeOrange avatar Jan 16 '24 03:01 JakeOrange