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

add allocation metering to XS #2972

Closed
warner opened this issue Apr 27, 2021 · 2 comments · Fixed by #2988
Closed

add allocation metering to XS #2972

warner opened this issue Apr 27, 2021 · 2 comments · Fixed by #2988
Assignees
Labels
enhancement New feature or request xsnap the XS execution tool

Comments

@warner
Copy link
Member

warner commented Apr 27, 2021

What is the Problem Being Solved?

To switch to XS, we need to protect dynamic vats against code that allocates so much memory that the process crashes (because the host computer cannot supply enough virtual memory space, due to a ulimit setting or limited RAM+swap space), or the host computer goes into a swap thrash (preventing progress of other vats). We want the xsnap worker to signal a metering fault before it starts to cause problems.

The Moddable folks tell us that xsnap's fxAbort(XS_NOT_ENOUGH_MEMORY_EXIT) will be called if malloc ever fails, and at that point the engine state is probably toast (we really need to call c_exit(status) to terminate the process, because returning from fxAbort() is going to lead to null pointer dereferences and such). If fxAbort() is called with different status codes (e.g. XS_TOO_MUCH_COMPUTATION_EXIT), then it may be safe to return from fxAbort(), and/or call fxExitToHost() to tell the engine to unwind the JS stack and return from the top-level engine invocation (preserving the ability to make more invocations in the future).

It sounds like fxExitToHost() does a longjmp. So the way to instrument memory allocation will be:

  • change the allocator we give to XS to increment a big counter with the requested size
  • if this counter overflows the allocation limit, call fxExitToHost(), which will not return (from the point of view of the caller: it longjmps away)
  • XS doesn't currently free() the memory until the entire engine terminates, so we never decrement the counter
    • which is good, because we don't know the size of that memory chunk, so wouldn't know how much to decrement it by

The allocator is controlled by xsnap.h when it does #define mxUseDefaultChunkAllocation 1 and mxUseDefaultSlotAllocation. When set, these tell https://github.com/Moddable-OpenSource/moddable/blob/4ae91266ad4a9a4370e335d3904834ccccc3c5dc/xs/sources/xsPlatforms.c#L116-L143 to use c_malloc()/c_free(). I'm guessing we want to delete those #defines and instead implement our own fxAllocateChunks, fxFreeChunks (which can be a no-op), fxAllocateSlots, and fxFreeSlots.

I think that will be complete, at least as far as the heap is concerned. We'll need to investigate the stack as well, but I'm pretty sure that's already covered (our fxAbort handles an XS_STACK_OVERFLOW_EXIT status value).

@warner warner added enhancement New feature or request xsnap the XS execution tool labels Apr 27, 2021
@warner warner assigned warner and michaelfig and unassigned michaelfig Apr 27, 2021
@warner
Copy link
Member Author

warner commented Apr 27, 2021

I'll try my hand at this and will drag in @dckc and @michaelfig when I inevitably get stuck.

@dckc
Copy link
Member

dckc commented Apr 27, 2021

@warner here's a preview in case you're interested; I think it came out OK:

  • cbe63dc feat(xsnap): specify exit codes for meter exhaustion etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request xsnap the XS execution tool
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants