bytecodealliance / wasm-micro-runtime

WebAssembly Micro Runtime (WAMR)
Apache License 2.0
4.98k stars 628 forks source link

AOT code ends up in non-executable memory by `try_merge_data_and_text()` #3825

Open sasq64 opened 1 month ago

sasq64 commented 1 month ago

Subject of the issue

Running AOT code on MacOS; works fine without --xip, but with the flag I crash on access error when trying to call native code.

Test case

Your environment

Build a wasm-file and compile to aot with wamrc --target aarch64v8 --xip

Expected behavior

Should not crash

Actual behavior

Crashes with access error in non executable memory

Extra Info

Changing aot_loader.c:2163 from

sections = loader_mmap((uint32)total_size, false, NULL, 0); to sections = loader_mmap((uint32)total_size, true, NULL, 0);

fixes the issue for me.

wenyongh commented 1 month ago

Hi, try_merge_data_and_text is called only when the aot module is not in indirect mode (module->is_indirect_mode is false): https://github.com/bytecodealliance/wasm-micro-runtime/blob/deacb7a8d8ca96b9dff797907b7e3b2ced4887df/core/iwasm/aot/aot_loader.c#L3757-L3758

But module->is_indirect_mode is set to true when the input is an XIP file: https://github.com/bytecodealliance/wasm-micro-runtime/blob/deacb7a8d8ca96b9dff797907b7e3b2ced4887df/core/iwasm/aot/aot_loader.c#L4012-L4015

It is a little strange that it isn't set, could you help check whether create_sections and resolve_execute_mode are called?

BTW, in iwasm's main function, we mmap the AOT file previously for XIP mode before loading it, and aot text won't be mmapped again during loading: https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/product-mini/platforms/posix/main.c#L891-L902