Skip to content

Commit

Permalink
Fix crash in bake on load (oven-sh#16021)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored and probably-neb committed Jan 7, 2025
1 parent 47f4151 commit 20ee0de
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
23 changes: 18 additions & 5 deletions src/bake/BakeGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
extern "C" BunString BakeProdResolve(JSC::JSGlobalObject*, BunString a, BunString b);

namespace Bake {
using namespace JSC;

JSC::JSInternalPromise*
bakeModuleLoaderImportModule(JSC::JSGlobalObject* global,
Expand Down Expand Up @@ -151,13 +152,14 @@ const JSC::GlobalObjectMethodTable GlobalObject::s_globalObjectMethodTable = {
INHERIT_HOOK_METHOD(deriveShadowRealmGlobalObject),
INHERIT_HOOK_METHOD(codeForEval),
INHERIT_HOOK_METHOD(canCompileStrings),
INHERIT_HOOK_METHOD(trustedScriptStructure),
};

GlobalObject* GlobalObject::create(JSC::VM& vm, JSC::Structure* structure,
const JSC::GlobalObjectMethodTable* methodTable)
{
GlobalObject* ptr = new (NotNull, JSC::allocateCell<GlobalObject>(vm))
GlobalObject(vm, structure, methodTable);
Bake::GlobalObject* ptr = new (NotNull, JSC::allocateCell<Bake::GlobalObject>(vm))
Bake::GlobalObject(vm, structure, methodTable);
ptr->finishCreation(vm);
return ptr;
}
Expand All @@ -168,6 +170,13 @@ void GlobalObject::finishCreation(JSC::VM& vm)
ASSERT(inherits(info()));
}

JSC::Structure* GlobalObject::createStructure(JSC::VM& vm)
{
auto* structure = JSC::Structure::create(vm, nullptr, jsNull(), JSC::TypeInfo(JSC::GlobalObjectType, StructureFlags & ~IsImmutablePrototypeExoticObject), info());
structure->setTransitionWatchpointIsLikelyToBeFired(true);
return structure;
}

struct BunVirtualMachine;
extern "C" BunVirtualMachine* Bun__getVM();

Expand All @@ -181,9 +190,9 @@ extern "C" GlobalObject* BakeCreateProdGlobal(void* console)
BunVirtualMachine* bunVM = Bun__getVM();
WebCore::JSVMClientData::create(&vm, bunVM);

JSC::Structure* structure = GlobalObject::createStructure(vm);
GlobalObject* global = GlobalObject::create(
vm, structure, &GlobalObject::s_globalObjectMethodTable);
JSC::Structure* structure = Bake::GlobalObject::createStructure(vm);
Bake::GlobalObject* global = Bake::GlobalObject::create(
vm, structure, &Bake::GlobalObject::s_globalObjectMethodTable);
if (!global)
BUN_PANIC("Failed to create BakeGlobalObject");

Expand All @@ -193,6 +202,7 @@ extern "C" GlobalObject* BakeCreateProdGlobal(void* console)

global->setConsole(console);
global->setStackTraceLimit(10); // Node.js defaults to 10
global->isThreadLocalDefaultGlobalObject = true;

// TODO: it segfaults! process.nextTick is scoped out for now i guess!
// vm.setOnComputeErrorInfo(computeErrorInfoWrapper);
Expand All @@ -214,4 +224,7 @@ extern "C" void BakeGlobalObject__attachPerThreadData(GlobalObject* global, Prod
global->m_perThreadData = perThreadData;
}

const JSC::ClassInfo Bake::GlobalObject::s_info = { "GlobalObject"_s, &Base::s_info, nullptr, nullptr,
CREATE_METHOD_TABLE(Bake::GlobalObject) };

}; // namespace Bake
13 changes: 9 additions & 4 deletions src/bake/BakeGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class GlobalObject : public Zig::GlobalObject {
public:
using Base = Zig::GlobalObject;

ProductionPerThread* m_perThreadData;
ProductionPerThread* m_perThreadData = nullptr;
DECLARE_INFO;

template<typename, JSC::SubspaceAccess mode> static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
{
Expand All @@ -22,16 +23,20 @@ class GlobalObject : public Zig::GlobalObject {
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForBakeGlobalScope = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForBakeGlobalScope.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForBakeGlobalScope = std::forward<decltype(space)>(space); },
[](auto& server) -> JSC::HeapCellType& { return server.m_heapCellTypeForJSWorkerGlobalScope; });
[](auto& server) -> JSC::HeapCellType& { return server.m_heapCellTypeForBakeGlobalObject; });
}

static const JSC::GlobalObjectMethodTable s_globalObjectMethodTable;
static GlobalObject* create(JSC::VM& vm, JSC::Structure* structure, const JSC::GlobalObjectMethodTable* methodTable);

static JSC::Structure* createStructure(JSC::VM& vm);

void finishCreation(JSC::VM& vm);

GlobalObject(JSC::VM& vm, JSC::Structure* structure, const JSC::GlobalObjectMethodTable* methodTable)
: Zig::GlobalObject(vm, structure, methodTable) { }
GlobalObject(JSC::VM& vm, JSC::Structure* structure, const JSC::GlobalObjectMethodTable* methodTable)
: Zig::GlobalObject(vm, structure, methodTable)
{
}
};

}; // namespace Kit
3 changes: 2 additions & 1 deletion src/bun.js/bindings/BunClientData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "JSDOMWrapper.h"
#include <JavaScriptCore/DeferredWorkTimer.h>
#include "NodeVM.h"

#include "../../bake/BakeGlobalObject.h"
namespace WebCore {
using namespace JSC;

Expand All @@ -32,6 +32,7 @@ RefPtr<JSC::SourceProvider> createBuiltinsSourceProvider();
JSHeapData::JSHeapData(Heap& heap)
: m_heapCellTypeForJSWorkerGlobalScope(JSC::IsoHeapCellType::Args<Zig::GlobalObject>())
, m_heapCellTypeForNodeVMGlobalObject(JSC::IsoHeapCellType::Args<Bun::NodeVMGlobalObject>())
, m_heapCellTypeForBakeGlobalObject(JSC::IsoHeapCellType::Args<Bake::GlobalObject>())
, m_domBuiltinConstructorSpace ISO_SUBSPACE_INIT(heap, heap.cellHeapCellType, JSDOMBuiltinConstructorBase)
, m_domConstructorSpace ISO_SUBSPACE_INIT(heap, heap.cellHeapCellType, JSDOMConstructorBase)
, m_domNamespaceObjectSpace ISO_SUBSPACE_INIT(heap, heap.cellHeapCellType, JSDOMObject)
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/BunClientData.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class JSHeapData {

JSC::IsoHeapCellType m_heapCellTypeForJSWorkerGlobalScope;
JSC::IsoHeapCellType m_heapCellTypeForNodeVMGlobalObject;
JSC::IsoHeapCellType m_heapCellTypeForBakeGlobalObject;

private:
Lock m_lock;
Expand Down

0 comments on commit 20ee0de

Please sign in to comment.