镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 58697c0

Browse files
committed
fix(private-abi): seed script context registry (#867)
Replace predictable process-default hashing for Home-controlled execution-context IDs with a lazy, independently seeded exact identity map. Preserve public 32-bit IDs while making first registration and explicit assignment failure-atomic. Allocate isolation-transfer storage before publishing the replacement mapping, add deterministic collision/entropy/OOM coverage, and record the hardening in the hash-guarded pinned ABI contract and rendered documentation. Fixes #867
1 parent c6642bb commit 58697c0

5 files changed

Lines changed: 139 additions & 29 deletions

File tree

‎docs/.data/bunpress-output-v1.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
},
1111
{
1212
"path": "abi/README/index.html",
13-
"bytes": 324366,
14-
"sha256": "642dfcfc8a51b4ac29e06451f92f696917020c2bcf70365fdb90d21fd379fcc4"
13+
"bytes": 324518,
14+
"sha256": "980167ab0b048cdaa055458f285891446de26bb203b868c9eccaa1e887dc0d70"
1515
},
1616
{
1717
"path": "advanced/debugging/index.html",
@@ -274,5 +274,5 @@
274274
"sha256": "51a1b101a171cb21eb88f49b2b3a8c2bd032da92bcec26589423924de7f8785a"
275275
}
276276
],
277-
"tree_sha256": "0dbebbfbb68cd41b80a1aa0f07b64c73312321228ae55f5cd1bf9a3790beadc6"
277+
"tree_sha256": "b72a0caf6f7fcbafec47640f76b3a9d59512774a4980a8a1a51f871c60777c27"
278278
}

‎docs/abi/README.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,13 @@ bit-identical across sibling realms while isolating independent VMs.
531531

532532
The script-execution-context identifier boundary lazily assigns a stable,
533533
nonzero 32-bit process identifier to each global context. The process registry
534-
keeps parallel creation unique and resolves only live contexts; explicit Home
535-
worker removal and every natural teardown retire the mapping before context
536-
storage is freed. Sibling realms remain isolated, while zero, unknown, and
537-
already-retired IDs are inert. The pinned teardown/UAF-prevention contract is
538-
in [`home-script-execution-context-7ed99c02.json`](home-script-execution-context-7ed99c02.json).
534+
uses an independent lazy secure hash seed with exact integer equality; its first
535+
mapping and context publish together only after successful allocation. Parallel
536+
creation stays unique and resolves only live contexts; explicit Home worker
537+
removal and every natural teardown retire the mapping before context storage is
538+
freed. Sibling realms remain isolated, while zero, unknown, and already-retired
539+
IDs are inert. The pinned teardown/UAF-prevention contract is in
540+
[`home-script-execution-context-7ed99c02.json`](home-script-execution-context-7ed99c02.json).
539541

540542
The two retired module-registry snapshot exports exactly preserve pinned JSC's
541543
documented no-caller shims: get returns null and reset returns false without

‎docs/abi/home-script-execution-context-7ed99c02.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"Removal is context-isolated and idempotent for zero, unknown, and already-retired identifiers.",
2222
"Every C-API context destruction path unregisters its identifier before freeing context-owned state.",
2323
"The registry and identifier allocator are serialized across host threads.",
24+
"Live identifier lookup uses independently seeded exact-integer hashing with failure-atomic first publication.",
2425
"The exact Home-profile library compiles, links, and runs a consumer covering explicit removal, natural teardown, sibling isolation, and parallel lifetimes."
2526
]
2627
}

‎src/c_api.zig‎

Lines changed: 127 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ var private_remote_inspector_process: PrivateRemoteInspectorProcessState = .{};
471471
var private_inspector_sessions_lock: std.atomic.Mutex = .unlocked;
472472
var private_inspector_sessions: std.ArrayListUnmanaged(*CInspectorSession) = .empty;
473473
var private_script_execution_contexts_lock: std.atomic.Mutex = .unlocked;
474-
var private_script_execution_contexts: std.AutoHashMapUnmanaged(u32, *Context) = .empty;
474+
var private_script_execution_contexts: CPrivateIdentityMapUnmanaged(*Context) = .{};
475475
var next_private_script_execution_context_id: std.atomic.Value(u32) = .init(1);
476476
const PrivateGlobalLifecycle = struct {
477477
context: *Context,
@@ -561,8 +561,9 @@ fn unregisterPrivateScriptExecutionContext(context: *Context) void {
561561
if (identifier == 0) return;
562562
lockPrivateScriptExecutionContexts();
563563
defer private_script_execution_contexts_lock.unlock();
564-
if (private_script_execution_contexts.get(identifier) == context)
565-
_ = private_script_execution_contexts.remove(identifier);
564+
const key: usize = identifier;
565+
if (private_script_execution_contexts.get(key) == context)
566+
_ = private_script_execution_contexts.remove(key);
566567
}
567568

568569
fn privateScriptExecutionContextIdentifier(context: *Context) u32 {
@@ -574,7 +575,13 @@ fn privateScriptExecutionContextIdentifier(context: *Context) u32 {
574575
if (winner != 0) return winner;
575576
const current = next_private_script_execution_context_id.load(.monotonic);
576577
if (current == 0 or current == std.math.maxInt(u32)) return 0;
577-
private_script_execution_contexts.putNoClobber(gpa, current, context) catch return 0;
578+
if (!(installPrivateScriptExecutionContextUsing(
579+
&private_script_execution_contexts,
580+
gpa,
581+
current,
582+
context,
583+
newCPrivateIdentityHashContext,
584+
) catch return 0)) return 0;
578585
next_private_script_execution_context_id.store(current + 1, .monotonic);
579586
context.c_api_script_execution_context_unregister = unregisterPrivateScriptExecutionContext;
580587
context.c_api_script_execution_context_id.store(current, .release);
@@ -585,9 +592,14 @@ fn privateAssignScriptExecutionContextIdentifier(context: *Context, identifier:
585592
if (identifier == 0) return false;
586593
lockPrivateScriptExecutionContexts();
587594
defer private_script_execution_contexts_lock.unlock();
588-
if (context.c_api_script_execution_context_id.load(.acquire) != 0 or
589-
private_script_execution_contexts.contains(identifier)) return false;
590-
private_script_execution_contexts.putNoClobber(gpa, identifier, context) catch return false;
595+
if (context.c_api_script_execution_context_id.load(.acquire) != 0) return false;
596+
if (!(installPrivateScriptExecutionContextUsing(
597+
&private_script_execution_contexts,
598+
gpa,
599+
identifier,
600+
context,
601+
newCPrivateIdentityHashContext,
602+
) catch return false)) return false;
591603
const next = next_private_script_execution_context_id.load(.monotonic);
592604
if (next <= identifier and identifier != std.math.maxInt(u32))
593605
next_private_script_execution_context_id.store(identifier + 1, .monotonic);
@@ -602,19 +614,15 @@ fn privateTransferScriptExecutionContextIdentifier(old: *Context, new: *Context)
602614
lockPrivateScriptExecutionContexts();
603615
defer private_script_execution_contexts_lock.unlock();
604616
if (new.c_api_script_execution_context_id.load(.acquire) != 0) return false;
605-
if (private_script_execution_contexts.get(inherited)) |owner|
617+
const inherited_key: usize = inherited;
618+
if (private_script_execution_contexts.get(inherited_key)) |owner|
606619
if (owner != old) return false;
607-
private_script_execution_contexts.ensureUnusedCapacity(gpa, 1) catch return false;
608-
if (private_script_execution_contexts.get(inherited) == old)
609-
_ = private_script_execution_contexts.remove(inherited);
610-
private_script_execution_contexts.putAssumeCapacity(inherited, new);
611620

612621
const replacement = next_private_script_execution_context_id.load(.monotonic);
613-
if (replacement == 0 or replacement == std.math.maxInt(u32)) {
614-
_ = private_script_execution_contexts.remove(inherited);
615-
private_script_execution_contexts.putAssumeCapacity(inherited, old);
616-
return false;
617-
}
622+
if (replacement == 0 or replacement == std.math.maxInt(u32)) return false;
623+
const mapping = private_script_execution_contexts.getOrPutSecure(gpa, inherited_key) catch return false;
624+
if (mapping.found_existing) std.debug.assert(mapping.value_ptr.* == old);
625+
mapping.value_ptr.* = new;
618626
next_private_script_execution_context_id.store(replacement + 1, .monotonic);
619627
old.c_api_script_execution_context_id.store(replacement, .release);
620628
new.c_api_script_execution_context_unregister = unregisterPrivateScriptExecutionContext;
@@ -806,6 +814,21 @@ fn registerPrivateSerializedScriptUsing(
806814
}
807815
}
808816

817+
fn installPrivateScriptExecutionContextUsing(
818+
registry: *CPrivateIdentityMapUnmanaged(*Context),
819+
allocator: std.mem.Allocator,
820+
identifier: u32,
821+
context: *Context,
822+
context_provider: CPrivateIdentityContextProvider,
823+
) std.mem.Allocator.Error!bool {
824+
if (identifier == 0) return false;
825+
const key: usize = identifier;
826+
const result = try registry.getOrPutSecureUsing(allocator, key, context_provider);
827+
if (result.found_existing) return false;
828+
result.value_ptr.* = context;
829+
return true;
830+
}
831+
809832
const ReachabilityVisitor = struct {
810833
allocator: std.mem.Allocator,
811834
identity_context_provider: CPrivateIdentityContextProvider = newCPrivateIdentityHashContext,
@@ -17447,7 +17470,8 @@ export fn ScriptExecutionContextIdentifier__getGlobalObject(identifier: u32) cal
1744717470
if (identifier == 0) return null;
1744817471
lockPrivateScriptExecutionContexts();
1744917472
defer private_script_execution_contexts_lock.unlock();
17450-
return @ptrCast(private_script_execution_contexts.get(identifier) orelse return null);
17473+
const key: usize = identifier;
17474+
return @ptrCast(private_script_execution_contexts.get(key) orelse return null);
1745117475
}
1745217476

1745317477
/// Home removes a worker from the process registry before freeing its event
@@ -17457,7 +17481,8 @@ export fn Bun__ScriptExecutionContext__removeFromContextsMapByIdentifier(identif
1745717481
if (identifier == 0) return;
1745817482
lockPrivateScriptExecutionContexts();
1745917483
defer private_script_execution_contexts_lock.unlock();
17460-
_ = private_script_execution_contexts.remove(identifier);
17484+
const key: usize = identifier;
17485+
_ = private_script_execution_contexts.remove(key);
1746117486
}
1746217487

1746317488
fn inspectorState(c: *Context) ?*CInspectorState {
@@ -23014,7 +23039,7 @@ test "private heap snapshot string interning is secure and failure atomic" {
2301423039
try std.testing.expectEqual(@as(u64, 0x4845_4150_5354_0001), strings.map.context.?.seed);
2301523040
}
2301623041

23017-
test "private heap snapshot identity maps are keyed exact and failure atomic" {
23042+
test "private ABI identity maps are keyed exact and failure atomic" {
2301823043
const FixedContext = struct {
2301923044
fn first() std.mem.Allocator.Error!CPrivateIdentityHashContext {
2302023045
return .{ .seed = 0x4845_4150_4944_0001 };
@@ -23082,6 +23107,88 @@ test "private heap snapshot identity maps are keyed exact and failure atomic" {
2308223107
try std.testing.expectEqual(@as(usize, 0), identities.count());
2308323108
}
2308423109

23110+
test "private script execution context registry is secure and failure atomic" {
23111+
const Providers = struct {
23112+
fn fixed() std.mem.Allocator.Error!CPrivateIdentityHashContext {
23113+
return .{ .seed = 0x5343_5249_5054_0001 };
23114+
}
23115+
23116+
fn unavailable() std.mem.Allocator.Error!CPrivateIdentityHashContext {
23117+
return error.OutOfMemory;
23118+
}
23119+
};
23120+
const first: *Context = @ptrFromInt(0x1000);
23121+
const second: *Context = @ptrFromInt(0x2000);
23122+
23123+
var unavailable: CPrivateIdentityMapUnmanaged(*Context) = .{};
23124+
defer unavailable.deinit(std.testing.allocator);
23125+
try std.testing.expect(!try installPrivateScriptExecutionContextUsing(
23126+
&unavailable,
23127+
std.testing.allocator,
23128+
0,
23129+
first,
23130+
Providers.unavailable,
23131+
));
23132+
try std.testing.expectError(
23133+
error.OutOfMemory,
23134+
installPrivateScriptExecutionContextUsing(
23135+
&unavailable,
23136+
std.testing.allocator,
23137+
7,
23138+
first,
23139+
Providers.unavailable,
23140+
),
23141+
);
23142+
try std.testing.expect(unavailable.context == null);
23143+
try std.testing.expectEqual(@as(usize, 0), unavailable.count());
23144+
try std.testing.expectEqual(@as(usize, 0), unavailable.capacity());
23145+
23146+
var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
23147+
var allocation_failure: CPrivateIdentityMapUnmanaged(*Context) = .{};
23148+
defer allocation_failure.deinit(std.testing.allocator);
23149+
try std.testing.expectError(
23150+
error.OutOfMemory,
23151+
installPrivateScriptExecutionContextUsing(
23152+
&allocation_failure,
23153+
failing.allocator(),
23154+
11,
23155+
first,
23156+
Providers.fixed,
23157+
),
23158+
);
23159+
try std.testing.expect(allocation_failure.context == null);
23160+
try std.testing.expectEqual(@as(usize, 0), allocation_failure.count());
23161+
try std.testing.expectEqual(@as(usize, 0), allocation_failure.capacity());
23162+
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
23163+
23164+
try std.testing.expect(try installPrivateScriptExecutionContextUsing(
23165+
&allocation_failure,
23166+
std.testing.allocator,
23167+
11,
23168+
first,
23169+
Providers.fixed,
23170+
));
23171+
try std.testing.expect(!try installPrivateScriptExecutionContextUsing(
23172+
&allocation_failure,
23173+
std.testing.allocator,
23174+
11,
23175+
second,
23176+
Providers.unavailable,
23177+
));
23178+
try std.testing.expectEqual(first, allocation_failure.get(11).?);
23179+
try std.testing.expect(try installPrivateScriptExecutionContextUsing(
23180+
&allocation_failure,
23181+
std.testing.allocator,
23182+
12,
23183+
second,
23184+
Providers.unavailable,
23185+
));
23186+
try std.testing.expectEqual(second, allocation_failure.get(12).?);
23187+
try std.testing.expect(allocation_failure.remove(11));
23188+
try std.testing.expect(!allocation_failure.remove(11));
23189+
try std.testing.expectEqual(second, allocation_failure.get(12).?);
23190+
}
23191+
2308523192
test "private serialized owner capabilities reject collisions and publish atomically" {
2308623193
const Providers = struct {
2308723194
var token_calls: usize = 0;

‎tools/private-abi.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const PLATFORM: Record<string, string[]> = {
3131
};
3232
const CONTRACT_DIGESTS: Record<string, string> = {
3333
"docs/abi/home-script-execution-context-7ed99c02.json":
34-
"7cd3df82c4b5d655c2f78985aabce587dcd00675f2e68b294dd4418aac3863d1",
34+
"96ee63996ebfcbab873bc9dd920d04e7d6a5202c28698665bebede5c4c421019",
3535
"docs/abi/cpu-profile-sampling-404.json":
3636
"fa30c5e8b8f72b6396be992d6543882e735ca8a84a9981220f3273e378b1cda4",
3737
"docs/abi/readable-stream-consumption-405.json":

0 commit comments

Comments
 (0)