Skip to content

Fix leak when interpreter exits due to exception #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
12 changes: 10 additions & 2 deletions gen/function_source.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)

int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
rt = JS_NewRuntime();
js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(rt);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_function_source, qjsc_function_source_size, 0);
js_std_loop(ctx);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeRuntime(rt);
return 0;
return r;
}
12 changes: 10 additions & 2 deletions gen/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)

int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
rt = JS_NewRuntime();
js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(rt);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_hello, qjsc_hello_size, 0);
js_std_loop(ctx);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeRuntime(rt);
return 0;
return r;
}
12 changes: 10 additions & 2 deletions gen/hello_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)

int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
rt = JS_NewRuntime();
js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(rt);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_hello_module, qjsc_hello_module_size, 0);
js_std_loop(ctx);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeRuntime(rt);
return 0;
return r;
}
12 changes: 10 additions & 2 deletions gen/test_fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)

int main(int argc, char **argv)
{
int r;
JSValue ret;
JSRuntime *rt;
JSContext *ctx;
r = 0;
rt = JS_NewRuntime();
js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(rt);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
ctx = JS_NewCustomContext(rt);
js_std_add_helpers(ctx, argc, argv);
js_std_eval_binary(ctx, qjsc_test_fib, qjsc_test_fib_size, 0);
js_std_loop(ctx);
ret = js_std_loop(ctx);
if (JS_IsException(ret)) {
js_std_dump_error1(ctx, ret);
r = 1;
}
JS_FreeValue(ctx, ret);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeRuntime(rt);
return 0;
return r;
}
2 changes: 2 additions & 0 deletions qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <time.h>

#include "cutils.h"
#include "quickjs.h"
#include "quickjs-libc.h"

#ifdef QJS_USE_MIMALLOC
Expand Down Expand Up @@ -556,6 +557,7 @@ int main(int argc, char **argv)
ret = js_std_loop(ctx);
if (!JS_IsUndefined(ret)) {
js_std_dump_error1(ctx, ret);
JS_FreeValue(ctx, ret);
goto fail;
}
}
Expand Down
14 changes: 11 additions & 3 deletions qjsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,19 +314,27 @@ static void compile_file(JSContext *ctx, FILE *fo,
static const char main_c_template1[] =
"int main(int argc, char **argv)\n"
"{\n"
" int r;\n"
" JSValue ret;\n"
" JSRuntime *rt;\n"
" JSContext *ctx;\n"
" r = 0;\n"
" rt = JS_NewRuntime();\n"
" js_std_set_worker_new_context_func(JS_NewCustomContext);\n"
" js_std_init_handlers(rt);\n"
;

static const char main_c_template2[] =
" js_std_loop(ctx);\n"
" ret = js_std_loop(ctx);\n"
" if (JS_IsException(ret)) {\n"
" js_std_dump_error1(ctx, ret);\n"
" r = 1;\n"
" }\n"
" JS_FreeValue(ctx, ret);\n"
" JS_FreeContext(ctx);\n"
" js_std_free_handlers(rt);\n"
" JS_FreeRuntime(rt);\n"
" return 0;\n"
" return r;\n"
"}\n";

#define PROG_NAME "qjsc"
Expand Down Expand Up @@ -375,7 +383,7 @@ int main(int argc, char **argv)
stack_size = 0;
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));


/* add system modules */
namelist_add(&cmodule_list, "qjs:std", "std", 0);
namelist_add(&cmodule_list, "qjs:os", "os", 0);
Expand Down
7 changes: 5 additions & 2 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3499,7 +3499,7 @@ static void *worker_func(void *opaque)
js_std_dump_error(ctx);
JS_FreeValue(ctx, val);

js_std_loop(ctx);
JS_FreeValue(ctx, js_std_loop(ctx));

JS_FreeContext(ctx);
js_std_free_handlers(rt);
Expand Down Expand Up @@ -4137,6 +4137,7 @@ JSValue js_std_loop(JSContext *ctx)
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = js_get_thread_state(rt);
JSContext *ctx1;
JSValue ret;
int err;

for(;;) {
Expand All @@ -4156,7 +4157,9 @@ JSValue js_std_loop(JSContext *ctx)
break;
}
done:
return ts->exc;
ret = ts->exc;
ts->exc = JS_UNDEFINED;
return ret;
}

/* Wait for a promise and execute pending jobs while waiting for
Expand Down
8 changes: 7 additions & 1 deletion run-test262.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef pthread_t js_thread_t;

#include "cutils.h"
#include "list.h"
#include "quickjs.h"
#include "quickjs-c-atomics.h"
#include "quickjs-libc.h"

Expand Down Expand Up @@ -1554,7 +1555,12 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
}

if (local) {
js_std_loop(ctx);
JSValue val = js_std_loop(ctx);
if (JS_IsException(val)) {
js_std_dump_error1(ctx, val);
ret = -1;
}
JS_FreeValue(ctx, val);
}

JS_FreeCString(ctx, error_name);
Expand Down
Loading