From 2f4fd2388478b369236646aff9cf0411b1360730 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 12 Feb 2018 17:36:29 -0500 Subject: [PATCH] Add per fiber stack limit. --- src/core/fiber.c | 1 + src/core/vm.c | 5 ++--- src/include/dst/dstconfig.h | 2 +- src/include/dst/dsttypes.h | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/core/fiber.c b/src/core/fiber.c index afaafe78..096fbe6f 100644 --- a/src/core/fiber.c +++ b/src/core/fiber.c @@ -37,6 +37,7 @@ DstFiber *dst_fiber(int32_t capacity) { fiber->data = NULL; } fiber->parent = NULL; + fiber->maxstack = DST_STACK_MAX; return dst_fiber_reset(fiber); } diff --git a/src/core/vm.c b/src/core/vm.c index 22eeb140..9e182125 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -572,11 +572,10 @@ static void *op_lookup[255] = { VM_OP(DOP_CALL) { Dst callee = stack[oparg(2, 0xFFFF)]; -#ifdef DST_STACK_MAX - if (dst_vm_fiber->stacktop > DST_STACK_MAX) { + if (dst_vm_fiber->maxstack && + dst_vm_fiber->stacktop > dst_vm_fiber->maxstack) { vm_throw("stack overflow"); } -#endif if (dst_checktype(callee, DST_FUNCTION)) { func = dst_unwrap_function(callee); dst_stack_frame(stack)->pc = pc; diff --git a/src/include/dst/dstconfig.h b/src/include/dst/dstconfig.h index 759d4838..167f3356 100644 --- a/src/include/dst/dstconfig.h +++ b/src/include/dst/dstconfig.h @@ -123,7 +123,7 @@ extern "C" { /* Define max stack size for stacks before raising a stack overflow error. * If this is not defined, fiber stacks can grow without limit (until memory * runs out) */ -#define DST_STACK_MAX 4096 +#define DST_STACK_MAX 8192 /* Use nanboxed values - uses 8 bytes per value instead of 12 or 16. */ #define DST_NANBOX diff --git a/src/include/dst/dsttypes.h b/src/include/dst/dsttypes.h index f2308716..2b668039 100644 --- a/src/include/dst/dsttypes.h +++ b/src/include/dst/dsttypes.h @@ -314,6 +314,7 @@ struct DstFiber { int32_t stackstart; /* Beginning of next args */ int32_t stacktop; /* Top of stack. Where values are pushed and popped from. */ int32_t capacity; + int32_t maxstack; /* Arbitrary defined limit for stack overflow */ enum { DST_FIBER_PENDING, DST_FIBER_NEW,