create Mutual Witness Protocol specs and implementation. #1
@@ -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",
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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" {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user