From a5eab8264cdf25f830a11a77f92960a34bb35e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 5 Apr 2024 12:05:54 +0200 Subject: [PATCH] `memcpy()` thread source to avoid race condition --- src/runtime.c | 3 +++ src/thread.c | 33 +++++++++++++++++++++++++-------- src/types.h | 2 ++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/runtime.c b/src/runtime.c index ceb8caf..09a3e59 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -1118,6 +1118,9 @@ bare_runtime_run (bare_runtime_t *runtime, const char *filename, bare_source_t s err = js_create_external_arraybuffer(env, source.buffer.base, source.buffer.len, NULL, NULL, &args[1]); assert(err == 0); break; + + case bare_source_arraybuffer: + args[1] = source.arraybuffer; } } diff --git a/src/thread.c b/src/thread.c index f1a8624..6bf8c80 100644 --- a/src/thread.c +++ b/src/thread.c @@ -14,10 +14,10 @@ #include "types.h" static void -bare_thread_entry (void *data) { +bare_thread_entry (void *opaque) { int err; - bare_thread_t *thread = (bare_thread_t *) data; + bare_thread_t *thread = (bare_thread_t *) opaque; bare_runtime_t *runtime = thread->runtime; @@ -38,19 +38,36 @@ bare_thread_entry (void *data) { err = js_open_handle_scope(env, &scope); assert(err == 0); - bare_source_t thread_source = thread->source; + bare_source_t source; - js_value_t *thread_data; + switch (thread->source.type) { + case bare_source_none: + case bare_source_arraybuffer: + source.type = bare_source_none; + break; + + case bare_source_buffer: + source.type = bare_source_arraybuffer; + + void *data; + err = js_create_arraybuffer(env, thread->source.buffer.len, &data, &source.arraybuffer); + assert(err == 0); + + memcpy(data, thread->source.buffer.base, thread->source.buffer.len); + break; + } + + js_value_t *data; switch (thread->data.type) { case bare_data_none: default: - err = js_get_null(runtime->env, &thread_data); + err = js_get_null(runtime->env, &data); assert(err == 0); break; case bare_data_sharedarraybuffer: { - err = js_create_sharedarraybuffer_with_backing_store(env, thread->data.backing_store, NULL, NULL, &thread_data); + err = js_create_sharedarraybuffer_with_backing_store(env, thread->data.backing_store, NULL, NULL, &data); assert(err == 0); err = js_release_arraybuffer_backing_store(env, thread->data.backing_store); @@ -71,14 +88,14 @@ bare_thread_entry (void *data) { err = js_get_global(env, &global); assert(err == 0); - js_call_function(env, global, fn, 1, (js_value_t *[]){thread_data}, NULL); + js_call_function(env, global, fn, 1, (js_value_t *[]){data}, NULL); err = js_close_handle_scope(env, scope); assert(err == 0); uv_sem_post(&thread->lock); - bare_runtime_run(runtime, thread->filename, thread_source); + bare_runtime_run(runtime, thread->filename, thread->source); free(thread->filename); diff --git a/src/types.h b/src/types.h index 2449af4..0e420cb 100644 --- a/src/types.h +++ b/src/types.h @@ -57,10 +57,12 @@ struct bare_source_s { enum { bare_source_none, bare_source_buffer, + bare_source_arraybuffer, } type; union { uv_buf_t buffer; + js_value_t *arraybuffer; }; };