Skip to content

Commit 86d0d27

Browse files
committed
core: make sure close callbacks don't access the JS context
1 parent a87f0df commit 86d0d27

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

src/fswatch.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
#include "utils.h"
2727

2828
typedef struct {
29-
uv_fs_event_t handle;
3029
JSContext *ctx;
30+
JSRuntime *rt;
31+
uv_fs_event_t handle;
3132
JSValue callback;
3233
int closed;
3334
int finalized;
@@ -44,7 +45,7 @@ static void uv__fsevent_close_cb(uv_handle_t *handle) {
4445
if (fw) {
4546
fw->closed = 1;
4647
if (fw->finalized)
47-
js_free(fw->ctx, fw);
48+
js_free_rt(fw->rt, fw);
4849
}
4950
}
5051

@@ -193,6 +194,7 @@ static JSValue tjs_fs_watch(JSContext *ctx, JSValueConst this_val, int argc, JSV
193194
JS_FreeCString(ctx, path);
194195

195196
fw->ctx = ctx;
197+
fw->rt = JS_GetRuntime(ctx);
196198
fw->handle.data = fw;
197199
fw->callback = JS_DupValue(ctx, argv[1]);
198200

src/process.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static JSClassID tjs_process_class_id;
3333

3434
typedef struct {
3535
JSContext *ctx;
36+
JSRuntime *rt;
3637
bool closed;
3738
bool finalized;
3839
uv_process_t process;
@@ -50,7 +51,7 @@ static void uv__close_cb(uv_handle_t *handle) {
5051
CHECK_NOT_NULL(p);
5152
p->closed = true;
5253
if (p->finalized)
53-
js_free(p->ctx, p);
54+
js_free_rt(p->rt, p);
5455
}
5556

5657
static void maybe_close(TJSProcess *p) {
@@ -185,6 +186,7 @@ static JSValue tjs_spawn(JSContext *ctx, JSValueConst this_val, int argc, JSValu
185186
}
186187

187188
p->ctx = ctx;
189+
p->rt = JS_GetRuntime(ctx);
188190
p->process.data = p;
189191

190192
TJS_ClearPromise(ctx, &p->status.result);

src/signals.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
typedef struct {
3030
JSContext *ctx;
31+
JSRuntime *rt;
3132
int closed;
3233
int finalized;
3334
uv_signal_t handle;
@@ -43,7 +44,7 @@ static void uv__signal_close_cb(uv_handle_t *handle) {
4344
if (sh) {
4445
sh->closed = 1;
4546
if (sh->finalized)
46-
js_free(sh->ctx, sh);
47+
js_free_rt(sh->rt, sh);
4748
}
4849
}
4950

@@ -118,6 +119,7 @@ static JSValue tjs_signal(JSContext *ctx, JSValueConst this_val, int argc, JSVal
118119
uv_unref((uv_handle_t *) &sh->handle);
119120

120121
sh->ctx = ctx;
122+
sh->rt = JS_GetRuntime(ctx);
121123
sh->sig_num = sig_num;
122124
sh->handle.data = sh;
123125
sh->func = JS_DupValue(ctx, func);

src/udp.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
typedef struct {
3232
JSContext *ctx;
33+
JSRuntime *rt;
3334
int closed;
3435
int finalized;
3536
uv_udp_t udp;
@@ -56,7 +57,7 @@ static void uv__udp_close_cb(uv_handle_t *handle) {
5657
CHECK_NOT_NULL(u);
5758
u->closed = 1;
5859
if (u->finalized)
59-
js_free(u->ctx, u);
60+
js_free_rt(u->rt, u);
6061
}
6162

6263
static void maybe_close(TJSUdp *u) {
@@ -296,6 +297,7 @@ static JSValue tjs_new_udp(JSContext *ctx, int af) {
296297
}
297298

298299
u->ctx = ctx;
300+
u->rt = JS_GetRuntime(ctx);
299301
u->closed = 0;
300302
u->finalized = 0;
301303

src/worker.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* QuickJS libuv bindings
2+
* txiki.js
33
*
44
* Copyright (c) 2019-present Saúl Ibarra Corretgé <s@saghul.net>
55
*
@@ -50,6 +50,7 @@ typedef struct {
5050

5151
typedef struct {
5252
JSContext *ctx;
53+
JSRuntime *rt;
5354
union {
5455
uv_handle_t handle;
5556
uv_stream_t stream;
@@ -131,7 +132,7 @@ static void worker_entry(void *arg) {
131132
static void uv__close_cb(uv_handle_t *handle) {
132133
TJSWorker *w = handle->data;
133134
CHECK_NOT_NULL(w);
134-
js_free(w->ctx, w);
135+
js_free_rt(w->rt, w);
135136
}
136137

137138
static void tjs_worker_finalizer(JSRuntime *rt, JSValue val) {
@@ -234,6 +235,7 @@ static JSValue tjs_new_worker(JSContext *ctx, uv_os_sock_t channel_fd, bool is_m
234235
}
235236

236237
w->ctx = ctx;
238+
w->rt = JS_GetRuntime(ctx);
237239
w->is_main = is_main;
238240
w->h.handle.data = w;
239241

0 commit comments

Comments
 (0)