Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/jsc/bindings/BunClientData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,14 @@ void JSVMClientData::create(VM* vm, void* bunVM, WorkerMessagingProxy* worker)
})),
JSC::ConstraintVolatility::GreyedByExecution));

// The common string cache: slots filled by the JS thread, read here with the world stopped (as above).
// The per-VM caches: slots filled by the JS thread, read here with the world stopped (as above). A realm that fills a slot can die first, so no global marks them.
vm->heap.addMarkingConstraint(makeUnique<JSC::SimpleMarkingConstraint>(
"Bcs"_s, "Bun CommonStrings"_s,
MAKE_MARKING_CONSTRAINT_EXECUTOR_PAIR(([clientData](auto& visitor) {
JSC::SetRootMarkReasonScope rootScope(visitor, JSC::RootMarkReason::StrongHandles);
clientData->commonStrings.visit(visitor);
clientData->schemaStrings.visit(visitor);
clientData->httpHeaderIdentifiers().visit(visitor);
})),
JSC::ConstraintVolatility::GreyedByExecution));

Expand Down
6 changes: 1 addition & 5 deletions src/jsc/bindings/BunClientData.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,7 @@ class JSVMClientData : public JSC::VM::ClientData {

ExtendedDOMClientIsoSubspaces& clientSubspaces() { return *m_clientSubspaces.get(); }

// Constructed eagerly so the concurrent GC marker
// (Zig::GlobalObject::visitChildrenImpl) never races the mutator on a
// lazy std::optional::emplace(). The ctor only calls
// LazyProperty::initLater ~90 times (stores a tagged function pointer),
// so there is no startup cost worth deferring.
// Constructed eagerly: the constructor only stores about 100 tagged function pointers. The "Bcs" marking constraint marks its cells.
WebCore::HTTPHeaderIdentifiers& httpHeaderIdentifiers() { return m_httpHeaderIdentifiers; }

// Public so Bun::commonStrings(vm) below is a static_cast and a member load.
Expand Down
10 changes: 0 additions & 10 deletions src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3713,16 +3713,6 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor)
FOR_EACH_GLOBALOBJECT_GC_MEMBER(VISIT_GLOBALOBJECT_GC_MEMBER)
#undef VISIT_GLOBALOBJECT_GC_MEMBER

// This runs on a concurrent GC helper thread. Fetch the VM through the
// visitor (its collector's heap, guaranteed alive for the duration of
// marking) rather than thisObject->vm() which
// dereferences JSGlobalObject::m_vm and can read stale bytes if the cell
// was picked up via conservative scan mid-recycle (see the
// visitGlobalObjectMember(unique_ptr) guard above for the same window).
// A stale m_vm surfaces as a SEGV in TypeCastTraits<JSVMClientData>::isType
// when downcast<> calls the virtual isWebCoreJSClientData() on garbage.
WebCore::clientData(visitor.collector().heap().vm())->httpHeaderIdentifiers().template visit<Visitor>(visitor);

thisObject->visitAdditionalChildrenInGCThread<Visitor>(visitor);
}

Expand Down
44 changes: 44 additions & 0 deletions test/js/web/fetch/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,50 @@ describe("Headers", () => {
]);
});

// The strings of known names are cached for the whole VM. A realm can read a name first and be
// collected before an older realm reads it. The cache must still hold the name.
test.skipIf(typeof ShadowRealm === "undefined")(
"a known name that a collected ShadowRealm read first is still that name",
async () => {
// prettier-ignore
const names = [
"accept-ranges", "content-disposition", "content-location", "if-range", "last-event-id", "referrer-policy",
"timing-allow-origin", "x-frame-options",
];
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
/* js */ `
const { edenGC } = require("bun:jsc");
const names = ${JSON.stringify(names)};
// Each realm is the first to read one name, and nothing holds the realm afterwards.
// Only an eden collection shows the fault: a full one visits every global object,
// and a global object used to mark the cache.
for (const name of names) {
new ShadowRealm().evaluate("[...new Headers({ " + JSON.stringify(name) + ": '1' })].length");
Comment thread
robobun marked this conversation as resolved.
Resolved
edenGC();
}
// Strings that take the cells that the collections freed.
const junk = [];
for (let i = 0; i < 20000; i++) junk.push("junk" + i);
const read = [...new Headers(names.map(name => [name, "1"]))].map(([name]) => name);
console.log(JSON.stringify(read));
`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ stdout, stderr, exitCode }).toEqual({
stdout: JSON.stringify(names) + "\n",
stderr: "",
exitCode: 0,
});
},
);

describe("while the headers change", () => {
const start = () =>
new Headers([
Expand Down
Loading