Skip to content

Commit

Permalink
Stack overflow detection wrapping for more entrypoints
Browse files Browse the repository at this point in the history
I don't think this is exhaustive. This also adds an assert to detect when
js_check_stack_overflow is called outside a guard.

I'm trying to be conservative and wrap the entrypoints explicitly, but maybe it
would make sense to wrap JS_CallInternal instead?
  • Loading branch information
mikezackles committed Mar 10, 2021
1 parent c920bcb commit c8ea4c4
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,8 @@ static inline uint8_t *js_get_stack_pointer(void)
static inline BOOL js_check_stack_overflow(JSRuntime *rt, size_t alloca_size)
{
size_t size;

assert(stack_top != NULL);
size = stack_top - js_get_stack_pointer();
return unlikely((size + alloca_size) > rt->stack_size);
}
Expand Down Expand Up @@ -18618,16 +18620,33 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
JSValue JS_Call(JSContext *ctx, JSValueConst func_obj, JSValueConst this_obj,
int argc, JSValueConst *argv)
{
return JS_CallInternal(ctx, func_obj, this_obj, JS_UNDEFINED,
argc, (JSValue *)argv, JS_CALL_FLAG_COPY_ARGV);
JSValue res;

if (stack_top == NULL) {
stack_top = js_get_stack_pointer();
}
res = JS_CallInternal(ctx, func_obj, this_obj, JS_UNDEFINED,
argc, (JSValue *)argv, JS_CALL_FLAG_COPY_ARGV);
if (stack_top == js_get_stack_pointer()) {
stack_top = NULL;
}
return res;
}

static JSValue JS_CallFree(JSContext *ctx, JSValue func_obj, JSValueConst this_obj,
int argc, JSValueConst *argv)
{
JSValue res = JS_CallInternal(ctx, func_obj, this_obj, JS_UNDEFINED,
JSValue res;

if (stack_top == NULL) {
stack_top = js_get_stack_pointer();
}
res = JS_CallInternal(ctx, func_obj, this_obj, JS_UNDEFINED,
argc, (JSValue *)argv, JS_CALL_FLAG_COPY_ARGV);
JS_FreeValue(ctx, func_obj);
if (stack_top == js_get_stack_pointer()) {
stack_top = NULL;
}
return res;
}

Expand Down

0 comments on commit c8ea4c4

Please sign in to comment.