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

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

Open sasq64 opened this issue 1 year ago • 1 comments

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

  • MacOS Sonoma 14.2
  • WAMR 2.1.2

Steps to reproduce

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.

sasq64 avatar Sep 30 '24 08:09 sasq64

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

wenyongh avatar Oct 08 '24 07:10 wenyongh