danos/test/system/services/shared-memory-server/shared-memory-server.zig

46 lines
1.7 KiB
Zig

//! test/system/services/shared-memory-server — the receiving half of the shared-memory test (docs/display-v2.md V2).
//! It binds `/protocol/test/shared-memory`; when `shared-memory-client` calls it carrying a
//! shared-memory capability, it `shared_memory_map`s that capability and checks the client's pattern
//! is visible through the mapping — proving the two processes share the same physical pages
//! (not a copy). On success it prints `shared-memory: shared 4096 bytes ok`, the test's marker.
const ipc = @import("ipc");
const service = @import("service");
const memory = @import("memory");
const logging = @import("logging");
const pattern_len = 4096;
/// The pattern the client writes — must match shared-memory-client.zig.
fn expected(i: usize) u8 {
return @truncate(i *% 7 +% 3);
}
fn onMessage(message: []const u8, reply: []u8, sender: u32, arrived: *ipc.Arrival) usize {
_ = message;
_ = reply;
_ = sender;
// Peeked, not claimed: the mapping survives the handle, so the turn closes it.
const cap = arrived.peek() orelse {
_ = logging.write("shared-memory: shared FAILED (no capability)\n");
return 0;
};
const ptr = memory.sharedMap(cap) orelse {
_ = logging.write("shared-memory: shared FAILED (map)\n");
return 0;
};
var i: usize = 0;
while (i < pattern_len) : (i += 1) {
if (ptr[i] != expected(i)) {
_ = logging.write("shared-memory: shared FAILED (mismatch)\n");
return 0;
}
}
_ = logging.write("shared-memory: shared 4096 bytes ok\n");
return 0; // empty reply — the client only needs the round trip to unblock
}
pub fn main() void {
service.run(64, .{ .service = "test/shared-memory", .on_message = onMessage });
}