Skip to content

A way to limit non-heap allocations (i.e. V8 "Large Object Storage" / Deno.memoryUsage().external) in Web Workers for untrusted code #26202

@josephrocca

Description

@josephrocca

Context: I have a use case where I need to run some untrusted1 code in a Worker without it being able to crash the whole Deno process.

To briefly summarize:

  • Limiting heap memory: This is possible via a custom build using create_params.heap_limits and js_runtime.add_near_heap_limit_callback.
  • Limiting non-heap memory: If I understand correctly, V8 allocates large ArrayBuffers and typed arrays to a non-heap memory area - the "Large Object Space". I'm not sure if/how this can be limited in a robust way.
    • My current workaround: Add some code in the Worker that runs during init, which replaces ArrayBuffer and all typed arrays with copies that intercept and check that the bytes being allocated + Deno.memoryUsage().external will not exceed a threshold. Luckily Deno.memoryUsage().external does seem to be specific to the Web Worker that it's called from. But in general I'm not sure how full-proof this strategy is.

So I'm wondering:

  1. Is there currently any deno_core or rusty_v8 feature which could allow limiting the Large Object Space size for a Web Worker?
  2. Is there any chance of something like workerOptions.deno.memoryLimit.heap and workerOptions.deno.memoryLimit.external, so that a custom build wouldn't be required?

Related:


[1] By "untrusted" I don't mean that I need to worry about Spectre-type security issues. I just need to prevent it from crashing the process due to memory usage issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    runtimeRelates to code in the runtime cratesuggestionsuggestions for new features (yet to be agreed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions