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:
- Is there currently any
deno_core or rusty_v8 feature which could allow limiting the Large Object Space size for a Web Worker?
- 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.
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:
create_params.heap_limitsandjs_runtime.add_near_heap_limit_callback.WEB_WORKER_HEAP_LIMIT_BYTESenv variable): https://gist.github.com/josephrocca/cf66802299505c51933413521a16a8b5Deno.memoryUsage().externalwill not exceed a threshold. LuckilyDeno.memoryUsage().externaldoes 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:
deno_coreorrusty_v8feature which could allow limiting the Large Object Space size for a Web Worker?workerOptions.deno.memoryLimit.heapandworkerOptions.deno.memoryLimit.external, so that a custom build wouldn't be required?Related:
ResourceLimitsfornode:worker_threads#26156setIntervalneeds to be implemented manually IIUC.[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.