From 7498299cbff7b09556394c166015d002f57c8377 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Fri, 31 Jan 2025 15:16:55 +0100 Subject: [PATCH] Fix componentize::GetMemBuffer This patch fixes an issue where if the return value of `sbrk(0)` ever changed after initially being set, we'd encounter a panic: the value is stored in a `PersistentRootedObject`, `AB`, which the code tries to initialize multiple times. Persistent roots can't only be initialized ones, after which they have to be `set`. --- embedding/embedding.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/embedding/embedding.cpp b/embedding/embedding.cpp index f1a7566b..c87075b6 100644 --- a/embedding/embedding.cpp +++ b/embedding/embedding.cpp @@ -490,7 +490,7 @@ bool ReportAndClearException(JSContext *cx) { return true; } -void *LAST_SBRK; +void *LAST_SBRK = nullptr; JS::PersistentRootedObject AB; static bool GetMemBuffer(JSContext *cx, unsigned argc, JS::Value *vp) { if (sbrk(0) != LAST_SBRK) { @@ -502,7 +502,7 @@ static bool GetMemBuffer(JSContext *cx, unsigned argc, JS::Value *vp) { #endif JS::RootedObject mem_buffer(cx, JS::NewArrayBufferWithUserOwnedContents( cx, (size_t)LAST_SBRK, base)); - AB.init(cx, mem_buffer); + AB.set(mem_buffer); } JS::CallArgs args = JS::CallArgsFromVp(argc, vp); args.rval().setObject(*AB); @@ -512,6 +512,7 @@ static bool GetMemBuffer(JSContext *cx, unsigned argc, JS::Value *vp) { bool install(api::Engine *engine) { Runtime.engine = engine; Runtime.cx = engine->cx(); + AB.init(engine->cx()); char env_name[100];