duktape icon indicating copy to clipboard operation
duktape copied to clipboard

Question about DUK_USE_CALLSTACK_LIMIT and DUK_USE_VALSTACK_LIMIT

Open wdoreen opened this issue 5 years ago • 1 comments

I saw this example which limits the memory by re-defining the allocation functions. https://github.com/svaarala/duktape/blob/master/examples/sandbox/sandbox.c Are DUK_USE_CALLSTACK_LIMIT and DUK_USE_VALSTACK_LIMIT overlapping what the example is doing as call stack and value stack are also limited accordingly when the methods in the example is used? To limit the memory used by ECMAScripts in my C/C++ project, are DUK_USE_CALLSTACK_LIMIT and DUK_USE_VALSTACK_LIMIT sufficient?

wdoreen avatar Jul 01 '19 19:07 wdoreen

Call stack and value stack are in principle entirely separate. It is possible to use a huge amount of value stack while doing no function calls at all (just call duk_require_stack(ctx, 1000000000)). It's possible to do a lot of function calls but consume very small amount of value stack per call.

Neither limit is enough to bound memory usage. For example, a single ECMAScript object takes one slot in a value stack (it's just a reference) but may have millions of properties. Similarly, a 1GB long string value takes only one value stack lot but consumes 1GB of memory.

The best way to limit memory usage is to use a custom allocator which puts a bound on how much memory it allows Duktape to allocate. Refusing allocations causes Duktape to try to free unused stuff, and if even that fails, a RangeError will be thrown.

svaarala avatar Jul 01 '19 19:07 svaarala