Stream: general

Topic: memory management in cranelift jit


view this post on Zulip Zack (Aug 04 2021 at 03:13):

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?

view this post on Zulip Mario Carneiro (Aug 04 2021 at 04:58):

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?

view this post on Zulip bjorn3 (Aug 04 2021 at 14:31):

The address 0x0 is not mapped on pretty much every OS.

view this post on Zulip bjorn3 (Aug 04 2021 at 14:34):

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.

view this post on Zulip bjorn3 (Aug 04 2021 at 14:34):

@Zack

view this post on Zulip bjorn3 (Aug 04 2021 at 14:35):

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: Nov 22 2024 at 16:03 UTC