Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
29 changes: 28 additions & 1 deletion src/allocators/AllocationScope.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ const AllocationScope = @This();

pub const enabled = bun.Environment.enableAllocScopes;

pub const checkIsPoisoned = if (bun.Environment.enableAsan)
struct {
extern "c" fn __asan_address_is_poisoned(addr: *const anyopaque) c_int;
pub fn checkIsPoisoned(addr: *const anyopaque) bool {
const result = __asan_address_is_poisoned(addr) == 1;
debug("checkIsPoisoned(0x{x}) = {any}", .{ addr, result });
return result;
}
}.checkIsPoisoned
else
struct {
pub fn checkIsPoisoned(_: *const anyopaque) bool {
return true;
}
Comment on lines +17 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Return false from the non-ASAN fallback.

The ASAN branch returns true only for poisoned addresses. The fallback currently returns true for every address, so direct callers in non-ASAN builds classify every pointer as poisoned. deinitImpl guards its current call with bun.Environment.enableAsan, but the public helper still has incorrect semantics.

Proposed fix
     struct {
         pub fn checkIsPoisoned(_: *const anyopaque) bool {
-            return true;
+            return false;
         }
     }.checkIsPoisoned;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
struct {
pub fn checkIsPoisoned(_: *const anyopaque) bool {
return true;
}
struct {
pub fn checkIsPoisoned(_: *const anyopaque) bool {
return false;
}
}.checkIsPoisoned;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/allocators/AllocationScope.zig` around lines 17 - 20, Update the non-ASAN
fallback implementation of checkIsPoisoned in AllocationScope to return false
unconditionally, preserving the ASAN branch’s poisoned-address semantics and
ensuring direct callers do not classify every pointer as poisoned.

}.checkIsPoisoned;

parent: Allocator,
state: if (enabled) struct {
mutex: bun.Mutex,
Expand Down Expand Up @@ -52,6 +68,10 @@ pub fn init(parent: Allocator) AllocationScope {
}

pub fn deinit(scope: *AllocationScope) void {
return deinitImpl(scope, false);
}

pub fn deinitImpl(scope: *AllocationScope, check_poisoned: bool) void {
if (enabled) {
scope.state.mutex.lock();
defer scope.state.allocations.deinit(scope.parent);
Expand All @@ -64,7 +84,13 @@ pub fn deinit(scope: *AllocationScope) void {
var it = scope.state.allocations.iterator();
var n: usize = 0;
while (it.next()) |entry| {
Output.prettyErrorln("- {any}, len {d}, at:", .{ entry.key_ptr.*, entry.value_ptr.len });
// It's possible it got freed but we accidentally used the actual
// underlying allocator and NOT this allocation scope.
if (check_poisoned and bun.Environment.enableAsan and checkIsPoisoned(entry.key_ptr.*)) {
Output.prettyErrorln("- {any}, len {d}, (poisoned; did you free this pointer but forget to use the allocation scope to do so?) at:", .{ entry.key_ptr.*, entry.value_ptr.len });
} else {
Output.prettyErrorln("- {any}, len {d}, at:", .{ entry.key_ptr.*, entry.value_ptr.len });
}
bun.crash_handler.dumpStackTrace(entry.value_ptr.allocated_at.trace(), trace_limits);

switch (entry.value_ptr.extra) {
Expand Down Expand Up @@ -251,6 +277,7 @@ pub inline fn downcast(a: Allocator) ?*AllocationScope {
null;
}

const debug = bun.Output.scoped(.AllocationScope, false);
const std = @import("std");
const Allocator = std.mem.Allocator;
const bun = @import("bun");
Expand Down
6 changes: 6 additions & 0 deletions src/shell/AllocScope.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub fn endScope(this: *AllocScope) void {
}
}

pub fn endScopeWithPoisonCheck(this: *AllocScope) void {
if (comptime bun.Environment.enableAllocScopes) {
this.__scope.deinitImpl(true);
}
}

pub fn leakSlice(this: *AllocScope, memory: anytype) void {
if (comptime bun.Environment.enableAllocScopes) {
_ = @typeInfo(@TypeOf(memory)).pointer;
Expand Down
1 change: 1 addition & 0 deletions src/shell/Builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ pub fn init(
cmd.exec.bltn.impl = .{
.rm = Rm{
.opts = .{},
.alloc_scope = shell.AllocScope.beginScope(bun.default_allocator),
},
};
},
Expand Down
Loading