I was playing around with Cranelift's JIT module and I started playing around with memory. I tried just storing a byte at the memory address 0x0
and it crashed the program. I assume I overwrote something important? So, my question is, how can I find a safe place to store items in memory?
I'm not sure if WASM treats 0x0
specially, but LLVM, C, Rust all special case this address to never be valid, and cranelift might be relying on this. Does writing to other addresses work?
The address 0x0 is not mapped on pretty much every OS.
If you use cranelift_jit, you don't get a big linear memory like wasm. You will read and write to the actual address space of the process you are running in when you use the load
and store
instructions. When compiling wasm, the heap_load
and heap_store
instructions are used which lower to an instruction sequence that interprets the given address as an offset into the given heap and checks that you don't go out of bounds.
@Zack
If you want to store data, you will either have to store it on the stack (stack_load
/stack_store
) or put it in a global variable (use module.define_data()
to define it and then the global_value
instruction to get the address of the global which you can then pass to load
and store
).
Last updated: Dec 23 2024 at 12:05 UTC