简体   繁体   English

WebAssembly 中的堆栈空间(和 WASI)

[英]Stack space in WebAssembly (& WASI)

I'm writing small programs in WebAssembly text format ( .wat ).我正在以 WebAssembly 文本格式 ( .wat ) 编写小程序。 I've looked at Wasm code compiled from C and it looks like the common practice is to maintain a global stack pointer, and when we enter a function, to create a stack frame by subtracting the stack pointer by some multiple of 4 (eg 16):我看过从 C 编译的 Wasm 代码,看起来常见的做法是维护一个全局堆栈指针,当我们进入一个函数时,通过将堆栈指针减去 4 的倍数(例如 16 ):

    (global.set $g0
      (local.tee $l1
        (i32.sub
          (global.get $g0)
          (i32.const 16))))

;; --snip--

  (global $g0 (mut i32) (i32.const 67088))

I need to implement a very basic malloc to do dynamic memory management.我需要实现一个非常基本的malloc来进行动态内存管理。 In this implementation, I think I need to grow the memory with memory.grow if I run out of memory.在这个实现中,我认为如果内存memory.grow ,我需要使用memory.grow增加内存。

My question:我的问题:

What should I do with the stack space when I grow the memory?当我增加内存时,堆栈空间应该怎么办? AFAIK if I don't do anything the memory looks like this: AFAIK 如果我什么都不做,内存看起来像这样:

+-----------------------+  high memory
| Newly acquired memory |
|                       |
+-----------------------+
| Stack space           |
+-----------------------+
| Heap (managed with    |
|       malloc)         |
+-----------------------+  low memory

wasm-ld (the llvm linker for WebAssembly) lays out memory by default with the stack first (at a lower address). wasm-ld(用于 WebAssembly 的 llvm 链接器)默认以堆栈优先(在较低地址处)来布置内存。 This allows for sbrk (or indeed your custom malloc) to use memory.grow to extent the heap region.这允许 sbrk(或者实际上是您的自定义 malloc)使用 memory.grow 来扩展堆区域。 The location of the start of the heap can be found by taking the address of the linker-created symbols __heap_base .可以通过获取链接器创建的符号__heap_base的地址来找到堆开始的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM