@@ -471,7 +471,7 @@ var private_remote_inspector_process: PrivateRemoteInspectorProcessState = .{};
471471var private_inspector_sessions_lock: std.atomic.Mutex = .unlocked;
472472var private_inspector_sessions: std.ArrayListUnmanaged(*CInspectorSession) = .empty;
473473var 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) = .{} ;
475475var next_private_script_execution_context_id: std.atomic.Value(u32) = .init(1);
476476const 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
568569fn 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+
809832const 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
1746317488fn 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+
2308523192test "private serialized owner capabilities reject collisions and publish atomically" {
2308623193 const Providers = struct {
2308723194 var token_calls: usize = 0;
0 commit comments