Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing the stack/heap from Javascript #1215

Closed
void4 opened this issue Jun 12, 2018 · 5 comments
Closed

Accessing the stack/heap from Javascript #1215

void4 opened this issue Jun 12, 2018 · 5 comments

Comments

@void4
Copy link

void4 commented Jun 12, 2018

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).

@binji
Copy link
Member

binji commented Jun 12, 2018

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.

@void4
Copy link
Author

void4 commented Jun 12, 2018

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.

@binji
Copy link
Member

binji commented Jun 12, 2018

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.

@void4
Copy link
Author

void4 commented Jun 12, 2018

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!

@void4 void4 closed this as completed Jun 16, 2018
@xosg
Copy link

xosg commented Dec 24, 2019

WebAssembly.Memory is so called heap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants