Skip to content

Commit 1bc61ac

Browse files
committed
Fix leak when interpreter exits due to exception
Fixes: #720
1 parent 9631492 commit 1bc61ac

File tree

8 files changed

+65
-14
lines changed

8 files changed

+65
-14
lines changed

gen/function_source.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
5757

5858
int main(int argc, char **argv)
5959
{
60+
int r;
61+
JSValue ret;
6062
JSRuntime *rt;
6163
JSContext *ctx;
64+
r = 0;
6265
rt = JS_NewRuntime();
6366
js_std_set_worker_new_context_func(JS_NewCustomContext);
6467
js_std_init_handlers(rt);
6568
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
6669
ctx = JS_NewCustomContext(rt);
6770
js_std_add_helpers(ctx, argc, argv);
6871
js_std_eval_binary(ctx, qjsc_function_source, qjsc_function_source_size, 0);
69-
js_std_loop(ctx);
72+
ret = js_std_loop(ctx);
73+
if (JS_IsException(ret)) {
74+
js_std_dump_error1(ctx, ret);
75+
r = 1;
76+
}
77+
JS_FreeValue(ctx, ret);
7078
JS_FreeContext(ctx);
7179
js_std_free_handlers(rt);
7280
JS_FreeRuntime(rt);
73-
return 0;
81+
return r;
7482
}

gen/hello.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
3030

3131
int main(int argc, char **argv)
3232
{
33+
int r;
34+
JSValue ret;
3335
JSRuntime *rt;
3436
JSContext *ctx;
37+
r = 0;
3538
rt = JS_NewRuntime();
3639
js_std_set_worker_new_context_func(JS_NewCustomContext);
3740
js_std_init_handlers(rt);
3841
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
3942
ctx = JS_NewCustomContext(rt);
4043
js_std_add_helpers(ctx, argc, argv);
4144
js_std_eval_binary(ctx, qjsc_hello, qjsc_hello_size, 0);
42-
js_std_loop(ctx);
45+
ret = js_std_loop(ctx);
46+
if (JS_IsException(ret)) {
47+
js_std_dump_error1(ctx, ret);
48+
r = 1;
49+
}
50+
JS_FreeValue(ctx, ret);
4351
JS_FreeContext(ctx);
4452
js_std_free_handlers(rt);
4553
JS_FreeRuntime(rt);
46-
return 0;
54+
return r;
4755
}

gen/hello_module.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
8282

8383
int main(int argc, char **argv)
8484
{
85+
int r;
86+
JSValue ret;
8587
JSRuntime *rt;
8688
JSContext *ctx;
89+
r = 0;
8790
rt = JS_NewRuntime();
8891
js_std_set_worker_new_context_func(JS_NewCustomContext);
8992
js_std_init_handlers(rt);
9093
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
9194
ctx = JS_NewCustomContext(rt);
9295
js_std_add_helpers(ctx, argc, argv);
9396
js_std_eval_binary(ctx, qjsc_hello_module, qjsc_hello_module_size, 0);
94-
js_std_loop(ctx);
97+
ret = js_std_loop(ctx);
98+
if (JS_IsException(ret)) {
99+
js_std_dump_error1(ctx, ret);
100+
r = 1;
101+
}
102+
JS_FreeValue(ctx, ret);
95103
JS_FreeContext(ctx);
96104
js_std_free_handlers(rt);
97105
JS_FreeRuntime(rt);
98-
return 0;
106+
return r;
99107
}

gen/test_fib.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,26 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
5858

5959
int main(int argc, char **argv)
6060
{
61+
int r;
62+
JSValue ret;
6163
JSRuntime *rt;
6264
JSContext *ctx;
65+
r = 0;
6366
rt = JS_NewRuntime();
6467
js_std_set_worker_new_context_func(JS_NewCustomContext);
6568
js_std_init_handlers(rt);
6669
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
6770
ctx = JS_NewCustomContext(rt);
6871
js_std_add_helpers(ctx, argc, argv);
6972
js_std_eval_binary(ctx, qjsc_test_fib, qjsc_test_fib_size, 0);
70-
js_std_loop(ctx);
73+
ret = js_std_loop(ctx);
74+
if (JS_IsException(ret)) {
75+
js_std_dump_error1(ctx, ret);
76+
r = 1;
77+
}
78+
JS_FreeValue(ctx, ret);
7179
JS_FreeContext(ctx);
7280
js_std_free_handlers(rt);
7381
JS_FreeRuntime(rt);
74-
return 0;
82+
return r;
7583
}

qjs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <time.h>
3737

3838
#include "cutils.h"
39+
#include "quickjs.h"
3940
#include "quickjs-libc.h"
4041

4142
#ifdef QJS_USE_MIMALLOC
@@ -556,6 +557,7 @@ int main(int argc, char **argv)
556557
ret = js_std_loop(ctx);
557558
if (!JS_IsUndefined(ret)) {
558559
js_std_dump_error1(ctx, ret);
560+
JS_FreeValue(ctx, ret);
559561
goto fail;
560562
}
561563
}

qjsc.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,27 @@ static void compile_file(JSContext *ctx, FILE *fo,
314314
static const char main_c_template1[] =
315315
"int main(int argc, char **argv)\n"
316316
"{\n"
317+
" int r;\n"
318+
" JSValue ret;\n"
317319
" JSRuntime *rt;\n"
318320
" JSContext *ctx;\n"
321+
" r = 0;\n"
319322
" rt = JS_NewRuntime();\n"
320323
" js_std_set_worker_new_context_func(JS_NewCustomContext);\n"
321324
" js_std_init_handlers(rt);\n"
322325
;
323326

324327
static const char main_c_template2[] =
325-
" js_std_loop(ctx);\n"
328+
" ret = js_std_loop(ctx);\n"
329+
" if (JS_IsException(ret)) {\n"
330+
" js_std_dump_error1(ctx, ret);\n"
331+
" r = 1;\n"
332+
" }\n"
333+
" JS_FreeValue(ctx, ret);\n"
326334
" JS_FreeContext(ctx);\n"
327335
" js_std_free_handlers(rt);\n"
328336
" JS_FreeRuntime(rt);\n"
329-
" return 0;\n"
337+
" return r;\n"
330338
"}\n";
331339

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

378-
386+
379387
/* add system modules */
380388
namelist_add(&cmodule_list, "qjs:std", "std", 0);
381389
namelist_add(&cmodule_list, "qjs:os", "os", 0);

quickjs-libc.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,7 +3499,7 @@ static void *worker_func(void *opaque)
34993499
js_std_dump_error(ctx);
35003500
JS_FreeValue(ctx, val);
35013501

3502-
js_std_loop(ctx);
3502+
JS_FreeValue(ctx, js_std_loop(ctx));
35033503

35043504
JS_FreeContext(ctx);
35053505
js_std_free_handlers(rt);
@@ -4137,6 +4137,7 @@ JSValue js_std_loop(JSContext *ctx)
41374137
JSRuntime *rt = JS_GetRuntime(ctx);
41384138
JSThreadState *ts = js_get_thread_state(rt);
41394139
JSContext *ctx1;
4140+
JSValue ret;
41404141
int err;
41414142

41424143
for(;;) {
@@ -4156,7 +4157,9 @@ JSValue js_std_loop(JSContext *ctx)
41564157
break;
41574158
}
41584159
done:
4159-
return ts->exc;
4160+
ret = ts->exc;
4161+
ts->exc = JS_UNDEFINED;
4162+
return ret;
41604163
}
41614164

41624165
/* Wait for a promise and execute pending jobs while waiting for

run-test262.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef pthread_t js_thread_t;
4545

4646
#include "cutils.h"
4747
#include "list.h"
48+
#include "quickjs.h"
4849
#include "quickjs-c-atomics.h"
4950
#include "quickjs-libc.h"
5051

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

15561557
if (local) {
1557-
js_std_loop(ctx);
1558+
JSValue val = js_std_loop(ctx);
1559+
if (JS_IsException(val)) {
1560+
js_std_dump_error1(ctx, val);
1561+
ret = -1;
1562+
}
1563+
JS_FreeValue(ctx, val);
15581564
}
15591565

15601566
JS_FreeCString(ctx, error_name);

0 commit comments

Comments
 (0)