From 789897b51a301bb55af96031063e279b828b7a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Tanr=C4=B1kulu?= Date: Wed, 11 Feb 2026 14:22:05 -0300 Subject: [PATCH] fix infra: saturating memory tracker, check build imports, buffer growth test --- build.zig | 4 ++++ src/core/tracking_allocator.zig | 23 +++++++++++++++++------ tests/core/test_buffer.zig | 6 +++--- tests/integration/test_error_handling.zig | 4 ++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/build.zig b/build.zig index 3ae0c02..0322a00 100644 --- a/build.zig +++ b/build.zig @@ -1963,6 +1963,10 @@ pub fn build(b: *std.Build) void { check_module.addImport("network", network_module); check_module.addImport("protocol", protocol_module); check_module.addImport("files", files_module); + check_module.addImport("noise", noise_module); + check_module.addImport("p2p", p2p_module); + check_module.addImport("mwp", mwp_module); + check_module.addImport("mwp_lib", mwp_lib_module); const check_exe = b.addExecutable(.{ .name = "p2p-check", diff --git a/src/core/tracking_allocator.zig b/src/core/tracking_allocator.zig index 9a25f96..7f39f98 100644 --- a/src/core/tracking_allocator.zig +++ b/src/core/tracking_allocator.zig @@ -39,6 +39,17 @@ pub const TrackingAllocator = struct { const Self = @This(); + /// Atomically subtract from allocated_bytes, saturating at 0 to prevent underflow. + /// Uses fetchSub (single atomic op) in the common path for maximum throughput. + /// Only corrects on underflow, which is rare (forced shutdown freeing untracked memory). + fn saturatingSub(self: *Self, amount: u64) void { + const prev = self.allocated_bytes.fetchSub(amount, .release); + if (prev < amount) { + // Underflowed — correct back to 0 + _ = self.allocated_bytes.fetchAdd(amount - prev, .release); + } + } + /// Initialize a tracking allocator with a backing allocator and memory limit pub fn init(backing_allocator: Allocator, max_bytes: u64) Self { return .{ @@ -99,7 +110,7 @@ pub const TrackingAllocator = struct { const result = self.backing_allocator.rawRemap(buf, buf_align, new_len, ret_addr); if (result == null) { // Failed - release reserved memory - _ = self.allocated_bytes.fetchSub(delta, .release); + self.saturatingSub(delta); return null; } @@ -121,7 +132,7 @@ pub const TrackingAllocator = struct { const result = self.backing_allocator.rawRemap(buf, buf_align, new_len, ret_addr); if (result != null) { const delta = old_size - new_len; - _ = self.allocated_bytes.fetchSub(delta, .release); + self.saturatingSub(delta); } return result; } else { @@ -217,7 +228,7 @@ pub const TrackingAllocator = struct { if (result == null) { // Allocation failed - release the reserved memory - _ = self.allocated_bytes.fetchSub(size, .release); + self.saturatingSub(size); std.log.debug("CRIT-8: Backing allocator failed for {d} bytes", .{size}); return null; } @@ -258,7 +269,7 @@ pub const TrackingAllocator = struct { // Try the actual resize if (!self.backing_allocator.rawResize(buf, buf_align, new_len, ret_addr)) { // Failed - release reserved memory - _ = self.allocated_bytes.fetchSub(delta, .release); + self.saturatingSub(delta); return false; } @@ -280,7 +291,7 @@ pub const TrackingAllocator = struct { const delta = old_size - new_len; if (self.backing_allocator.rawResize(buf, buf_align, new_len, ret_addr)) { - _ = self.allocated_bytes.fetchSub(delta, .release); + self.saturatingSub(delta); return true; } return false; @@ -298,7 +309,7 @@ pub const TrackingAllocator = struct { self.backing_allocator.rawFree(buf, buf_align, ret_addr); // Release tracked memory - _ = self.allocated_bytes.fetchSub(size, .release); + self.saturatingSub(size); _ = self.free_count.fetchAdd(1, .monotonic); } }; diff --git a/tests/core/test_buffer.zig b/tests/core/test_buffer.zig index 56c128b..83c7a9c 100644 --- a/tests/core/test_buffer.zig +++ b/tests/core/test_buffer.zig @@ -399,9 +399,9 @@ test "buffer capacity management" { // Ensure capacity should work for current capacity try buffer.ensureCapacity(50); // Less than current capacity - try buffer.ensureCapacity(150); // More than current capacity - // Note: ensureCapacity for larger size is not implemented yet - // This would require buffer growth implementation + try buffer.ensureCapacity(150); // More than current capacity — triggers realloc growth + // Verify buffer actually grew + try testing.expect(buffer.base.capacity >= 150); } test "buffer type identification" { diff --git a/tests/integration/test_error_handling.zig b/tests/integration/test_error_handling.zig index ef8893a..97b906e 100644 --- a/tests/integration/test_error_handling.zig +++ b/tests/integration/test_error_handling.zig @@ -99,8 +99,8 @@ test "replay protection cleanup handles allocation failures" { const peer_id = [_]u8{1} ** 32; // Add some old messages that should be cleaned up - try protector.checkMessage(1, old_time, 1000, 0, peer_id); - try protector.checkMessage(2, old_time, 1001, 0, peer_id); + try protector.checkMessage(1, old_time, 1000, 0, peer_id, 0); + try protector.checkMessage(2, old_time, 1001, 0, peer_id, 0); // Force cleanup - should handle any allocation failures gracefully protector.forceCleanup();