Design: Accessing the stack/heap from Javascript

Created on 12 Jun 2018  路  5Comments  路  Source: WebAssembly/design

Is it possible to access the heap of the calling WASM instance within an imported Javascript function?

Related, is it possible to resume an instance after an imported Javascript function threw an exception? Specifically, right after point where it stopped, including the current heap data, call stack etc.

I'm trying to find out whether it is possible to make WASM processes orthogonally persistent, and that not only between calls, but within calls (for example when they run out of resources, see https://github.com/ewasm/wasm-metering).

Most helpful comment

A WebAssembly module doesn't have a heap, it just has a memory which is a flat vector of bytes. When a language like C++ is compiled to WebAssembly, the heap is stored in the memory, but so are other data.

In general, the WebAssembly call stack (which includes the stack of called functions and their locals) is not stored in memory, and is not accessible. But when a language like C++ is compiled to WebAssembly, some elements of the C++ stack may be stored in memory. Generally these are the "address-taken" C++ variables and large arrays.

All 5 comments

Is it possible to access the heap of the calling WASM instance within an imported Javascript function?

Yes, you can access the linear memory if you export it from the module. It will be a WebAssembly.Memory object, and its buffer property is an ArrayBuffer with the contents of linear memory.

Related, is it possible to resume an instance after an imported Javascript function threw an exception?

No, that's not currently possible. The call stack is discarded when an exception is thrown. To do this, you'd have to instrument your module to allow for this behavior. It's likely that some of the extensions discussed in the exception-handling proposal would help here as well.

Is the heap contained in the memory buffer? I haven't seen it mentioned in the documentation.
It also seems impossible to access the call stack within the imported javascript function without instrumenting the WASM module.

A WebAssembly module doesn't have a heap, it just has a memory which is a flat vector of bytes. When a language like C++ is compiled to WebAssembly, the heap is stored in the memory, but so are other data.

In general, the WebAssembly call stack (which includes the stack of called functions and their locals) is not stored in memory, and is not accessible. But when a language like C++ is compiled to WebAssembly, some elements of the C++ stack may be stored in memory. Generally these are the "address-taken" C++ variables and large arrays.

Ah, I see, so that's what the __heap_base is for!

Instance {
  exports: 
   { memory: Memory {},
     __heap_base: 66560,
     __data_end: 1024,
     main: [Function: 2],
     fac: [Function: 3] }
}

Thank you very much!

WebAssembly.Memory is so called heap

Was this page helpful?
0 / 5 - 0 ratings

Related issues

badumt55 picture badumt55  路  8Comments

Artur-A picture Artur-A  路  3Comments

arunetm picture arunetm  路  7Comments

nikhedonia picture nikhedonia  路  7Comments

mfateev picture mfateev  路  5Comments