Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,37 @@ static void new_errors(void)
JS_FreeRuntime(rt);
}

static void backtrace_oom_current_exception(void)
{
static const char setup_code[] =
"globalThis.f = function() { missing; };\n"
"Object.defineProperty(f, 'name', { value: 'x'.repeat(2 * 1024 * 1024) });";
JSMemoryUsage stats;
JSValue ret, exception;
JSRuntime *rt;
JSContext *ctx;

rt = new_runtime();
ctx = JS_NewContext(rt);

ret = eval(ctx, setup_code);
assert(!JS_IsException(ret));
JS_FreeValue(ctx, ret);

JS_ComputeMemoryUsage(rt, &stats);
JS_SetMemoryLimit(rt, (size_t)stats.malloc_size + 128 * 1024);

ret = eval(ctx, "f()");
assert(JS_IsException(ret));
assert(JS_HasException(ctx));
exception = JS_GetException(ctx);
JS_FreeValue(ctx, exception);
JS_SetMemoryLimit(rt, 0);

JS_FreeContext(ctx);
JS_FreeRuntime(rt);
}

static int gop_get_own_property(JSContext *ctx, JSPropertyDescriptor *desc,
JSValueConst obj, JSAtom prop)
{
Expand Down Expand Up @@ -1042,6 +1073,7 @@ int main(void)
promise_hook();
dump_memory_usage();
new_errors();
backtrace_oom_current_exception();
global_object_prototype();
slice_string_tocstring();
immutable_array_buffer();
Expand Down
10 changes: 6 additions & 4 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -7743,7 +7743,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,
int line_num, int col_num, int backtrace_flags)
{
JSStackFrame *sf, *sf_start;
JSValue stack, prepare, saved_exception;
JSValue stack, prepare, saved_exception, error_obj;
DynBuf dbuf;
const char *func_name_str;
const char *str1;
Expand All @@ -7760,6 +7760,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,
if (rt->in_build_stack_trace)
return;
rt->in_build_stack_trace = true;
error_obj = JS_DupValue(ctx, error_val);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use js_dup.


// Save exception because conversion to double may fail.
saved_exception = JS_GetException(ctx);
Expand Down Expand Up @@ -7905,7 +7906,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,
JS_FreeValue(ctx, csd[k].func_name);
}
JSValueConst args[] = {
error_val,
error_obj,
stack,
};
JSValue stack2 = JS_Call(ctx, prepare, ctx->error_ctor, countof(args), args);
Expand All @@ -7926,13 +7927,14 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,

if (JS_IsUndefined(ctx->error_back_trace))
ctx->error_back_trace = js_dup(stack);
if (has_filter_func || can_add_backtrace(error_val)) {
JS_DefinePropertyValue(ctx, error_val, JS_ATOM_stack, stack,
if (has_filter_func || can_add_backtrace(error_obj)) {
JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, stack,
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
} else {
JS_FreeValue(ctx, stack);
}

JS_FreeValue(ctx, error_obj);
rt->in_build_stack_trace = false;
}

Expand Down
Loading