WebAssembly Stack / Stack Initialization and Memory Pointer

I am currently looking at WebAssembly compiled through LLVM, but I have not yet been able to understand the stack / stack pointer and how it relates to the general memory structure.

I found out what I need to use s2wasmwith --allocate-stack Nto run my program, and I decided that this is basically an addition (data (i32.const 4) "8\00\00\00")(with N = 8) to my generated wast, with the binary part obviously a pointer to a memory offset, and the constant i32 is its offset in linear memory.

However, I do not quite understand why the value of the pointer is equal 56(again with N = 8) and how this value relates to the exact area of ​​the stack in memory, which in my case currently looks like this:

0-3: zero 4-7: 56 7-35: other data sections 36-55: zeroes 56-59: zero

I know that I'm probably more likely to be a candidate for "just use emscripten", but I would also like to understand this.

  • Is the stack pointer always stored at offset 4 in linear memory?
  • How is its initial value calculated? (aligned to the next offset% 16 == 0 + N after the data?)
  • What is stored earlier, and what after the offset indicates?
0
source share
1 answer

I touched on this in another matter . From the C ++ stack, there are actually 3 places where values ​​can end:

  • On the execution stack (each operation code pushes and returns values, so add2 pops up and then presses 1).
  • How local.
  • In Memory.

, 1. 2. , 3. , WebAssembly, , ABI . Emscripten , 4, , . 4, ABI, .

: , , malloc , . .

/ ( , , , ). WebAssembly , ( ). " " , WebAssembly.Memory, , . , , WebAssembly, WebAssembly.Memory.

, , 1. 2. , ++ WebAssembly, ++ .

+4

Source: https://habr.com/ru/post/1686079/


All Articles