create Mutual Witness Protocol specs and implementation. #1

Merged
mdt merged 26 commits from mwp into main 2026-02-11 17:38:08 +00:00
62 changed files with 10825 additions and 220 deletions
Showing only changes of commit 600fdd4245 - Show all commits

View File

@@ -432,10 +432,12 @@ pub const Connection = struct {
// This was the last reference // This was the last reference
return true; return true;
} }
// Safety check: underflow would indicate a bug // Safety check: underflow is an invariant violation (double-free)
if (old_count == 0) { if (old_count == 0) {
log.err("Connection reference count underflow! Double-free detected.", .{}); // fetchSub already wrapped the counter — this is unrecoverable corruption.
return Error.InternalError; // Panic immediately so the bug is caught in development rather than
// silently propagated through the call chain.
@panic("Connection reference count underflow: double-free detected");
} }
return false; return false;
} }

View File

@@ -156,13 +156,13 @@ pub const Socket = struct {
} }
const domain: u32 = switch (self.config.protocol) { const domain: u32 = switch (self.config.protocol) {
.tcp, .udp => 2, // AF_INET .tcp, .udp => posix.AF.INET,
.unix_stream, .unix_dgram => 1, // AF_UNIX .unix_stream, .unix_dgram => posix.AF.UNIX,
}; };
const sock_type: u32 = switch (self.config.protocol) { const sock_type: u32 = switch (self.config.protocol) {
.tcp, .unix_stream => 1, // SOCK_STREAM .tcp, .unix_stream => posix.SOCK.STREAM,
.udp, .unix_dgram => 2, // SOCK_DGRAM .udp, .unix_dgram => posix.SOCK.DGRAM,
}; };
self.fd = posix.socket(domain, sock_type, 0) catch |err| { self.fd = posix.socket(domain, sock_type, 0) catch |err| {
@@ -192,9 +192,10 @@ pub const Socket = struct {
log.err("CRITICAL: Failed to set non-blocking mode: {}", .{err}); log.err("CRITICAL: Failed to set non-blocking mode: {}", .{err});
self.stats.recordError(); self.stats.recordError();
// Close the socket since it's in an invalid state // Close the socket since it's in an invalid state
// Set state BEFORE closing FD to prevent another thread from using a closed FD
self.state = .closed;
posix.close(self.fd); posix.close(self.fd);
self.fd = -1; self.fd = -1;
self.state = .closed;
return err; return err;
}; };
} }