From 479319e7cad78c3fd38b6865c56e9fe9e78d495c Mon Sep 17 00:00:00 2001 From: paoda Date: Fri, 15 Dec 2023 02:47:01 -0600 Subject: [PATCH] fix: general bug fixes 1) prevent integer overflow if the PC is less than 0x0000_0004 2) gracefully exit when gdb gracefully exits 3) rename server to socket in Server.zig --- src/Packet.zig | 7 +++++-- src/Server.zig | 17 +++++++++++------ src/test.zig | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Packet.zig b/src/Packet.zig index e2b77c9..cc322ef 100644 --- a/src/Packet.zig +++ b/src/Packet.zig @@ -3,6 +3,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const Emulator = @import("lib.zig").Emulator; const State = @import("State.zig"); +const Server = @import("Server.zig"); const target = @import("Server.zig").target; const memory_map = @import("Server.zig").memory_map; @@ -46,7 +47,7 @@ const String = union(enum) { } }; -pub fn parse(self: *Self, allocator: Allocator, emu: *Emulator) !String { +pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emulator) !String { switch (self.contents[0]) { // Required '?' => return .{ .static = "T05" }, // FIXME: which errno? @@ -61,7 +62,7 @@ pub fn parse(self: *Self, allocator: Allocator, emu: *Emulator) !String { var i: u32 = 0; while (i < r.len + 1) : (i += 1) { var reg: u32 = if (i < r.len) r[i] else cpsr; - if (i == 15) reg -= if (cpsr >> 5 & 1 == 1) 4 else 8; // PC is ahead + if (i == 15) reg -|= if (cpsr >> 5 & 1 == 1) 4 else 8; // PC is ahead // writes the formatted integer to the buffer, returns a slice to the buffer but we ignore that // GDB also expects the bytes to be in the opposite order for whatever reason @@ -191,6 +192,8 @@ pub fn parse(self: *Self, allocator: Allocator, emu: *Emulator) !String { // TODO: Figure out the difference between 'M' and 'X' 'D' => { log.info("Disconnecting...", .{}); + + state.should_quit = true; return .{ .static = "OK" }; }, 'H' => return .{ .static = "" }, diff --git a/src/Server.zig b/src/Server.zig index 5fc470f..f3197fc 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -57,20 +57,23 @@ pub const memory_map: []const u8 = // FIXME: Shouldn't this be a Packet Struct? pkt_cache: ?[]const u8 = null, -server: Server, +socket: Server, +state: State = .{}, emu: Emulator, +pub const State = struct { should_quit: bool = false }; + pub fn init(emulator: Emulator) !Self { var server = std.net.StreamServer.init(.{}); try server.listen(std.net.Address.initIp4([_]u8{0} ** 4, port)); - return .{ .emu = emulator, .server = server }; + return .{ .emu = emulator, .socket = server }; } pub fn deinit(self: *Self, allocator: Allocator) void { self.reset(allocator); - self.server.deinit(); + self.socket.deinit(); self.* = undefined; } @@ -86,11 +89,13 @@ const Action = union(enum) { pub fn run(self: *Self, allocator: Allocator, quit: *Atomic(bool)) !void { var buf: [Packet.max_len]u8 = undefined; - var client = try self.server.accept(); + var client = try self.socket.accept(); log.info("client connected from {}", .{client.address}); while (true) { - const len = try client.stream.readAll(&buf); + if (self.state.should_quit) break; + + const len = try client.stream.read(&buf); if (len == 0) break; if (quit.load(.Monotonic)) break; @@ -127,7 +132,7 @@ fn handlePacket(self: *Self, allocator: Allocator, input: []const u8) !Action { var packet = Packet.from(allocator, input) catch return .nack; defer packet.deinit(allocator); - var string = packet.parse(allocator, &self.emu) catch return .nack; + var string = packet.parse(allocator, &self.state, &self.emu) catch return .nack; defer string.deinit(allocator); const reply = string.inner(); diff --git a/src/test.zig b/src/test.zig index db43492..4039238 100644 --- a/src/test.zig +++ b/src/test.zig @@ -60,7 +60,7 @@ test Server { var server = try Server.init(iface); defer server.deinit(allocator); - const t = try std.Thread.spawn(.{}, clientFn, .{server.server.listen_address}); + const t = try std.Thread.spawn(.{}, clientFn, .{server.socket.listen_address}); defer t.join(); var should_quit = std.atomic.Atomic(bool).init(false);