Skip to content

Commit

Permalink
core: make sure close callbacks don't access the JS context
Browse files Browse the repository at this point in the history
  • Loading branch information
saghul committed Feb 29, 2024
1 parent a87f0df commit 86d0d27
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/fswatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
#include "utils.h"

typedef struct {
uv_fs_event_t handle;
JSContext *ctx;
JSRuntime *rt;
uv_fs_event_t handle;
JSValue callback;
int closed;
int finalized;
Expand All @@ -44,7 +45,7 @@ static void uv__fsevent_close_cb(uv_handle_t *handle) {
if (fw) {
fw->closed = 1;
if (fw->finalized)
js_free(fw->ctx, fw);
js_free_rt(fw->rt, fw);
}
}

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

fw->ctx = ctx;
fw->rt = JS_GetRuntime(ctx);
fw->handle.data = fw;
fw->callback = JS_DupValue(ctx, argv[1]);

Expand Down
4 changes: 3 additions & 1 deletion src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static JSClassID tjs_process_class_id;

typedef struct {
JSContext *ctx;
JSRuntime *rt;
bool closed;
bool finalized;
uv_process_t process;
Expand All @@ -50,7 +51,7 @@ static void uv__close_cb(uv_handle_t *handle) {
CHECK_NOT_NULL(p);
p->closed = true;
if (p->finalized)
js_free(p->ctx, p);
js_free_rt(p->rt, p);
}

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

p->ctx = ctx;
p->rt = JS_GetRuntime(ctx);
p->process.data = p;

TJS_ClearPromise(ctx, &p->status.result);
Expand Down
4 changes: 3 additions & 1 deletion src/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

typedef struct {
JSContext *ctx;
JSRuntime *rt;
int closed;
int finalized;
uv_signal_t handle;
Expand All @@ -43,7 +44,7 @@ static void uv__signal_close_cb(uv_handle_t *handle) {
if (sh) {
sh->closed = 1;
if (sh->finalized)
js_free(sh->ctx, sh);
js_free_rt(sh->rt, sh);
}
}

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

sh->ctx = ctx;
sh->rt = JS_GetRuntime(ctx);
sh->sig_num = sig_num;
sh->handle.data = sh;
sh->func = JS_DupValue(ctx, func);
Expand Down
4 changes: 3 additions & 1 deletion src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

typedef struct {
JSContext *ctx;
JSRuntime *rt;
int closed;
int finalized;
uv_udp_t udp;
Expand All @@ -56,7 +57,7 @@ static void uv__udp_close_cb(uv_handle_t *handle) {
CHECK_NOT_NULL(u);
u->closed = 1;
if (u->finalized)
js_free(u->ctx, u);
js_free_rt(u->rt, u);
}

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

u->ctx = ctx;
u->rt = JS_GetRuntime(ctx);
u->closed = 0;
u->finalized = 0;

Expand Down
6 changes: 4 additions & 2 deletions src/worker.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* QuickJS libuv bindings
* txiki.js
*
* Copyright (c) 2019-present Saúl Ibarra Corretgé <[email protected]>
*
Expand Down Expand Up @@ -50,6 +50,7 @@ typedef struct {

typedef struct {
JSContext *ctx;
JSRuntime *rt;
union {
uv_handle_t handle;
uv_stream_t stream;
Expand Down Expand Up @@ -131,7 +132,7 @@ static void worker_entry(void *arg) {
static void uv__close_cb(uv_handle_t *handle) {
TJSWorker *w = handle->data;
CHECK_NOT_NULL(w);
js_free(w->ctx, w);
js_free_rt(w->rt, w);
}

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

w->ctx = ctx;
w->rt = JS_GetRuntime(ctx);
w->is_main = is_main;
w->h.handle.data = w;

Expand Down

0 comments on commit 86d0d27

Please sign in to comment.