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
This commit is contained in:
Rekai Nyangadzayi Musuka 2023-12-15 02:47:01 -06:00
parent 5947747533
commit 479319e7ca
3 changed files with 17 additions and 9 deletions

View File

@ -3,6 +3,7 @@ const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Emulator = @import("lib.zig").Emulator; const Emulator = @import("lib.zig").Emulator;
const State = @import("State.zig"); const State = @import("State.zig");
const Server = @import("Server.zig");
const target = @import("Server.zig").target; const target = @import("Server.zig").target;
const memory_map = @import("Server.zig").memory_map; 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]) { switch (self.contents[0]) {
// Required // Required
'?' => return .{ .static = "T05" }, // FIXME: which errno? '?' => return .{ .static = "T05" }, // FIXME: which errno?
@ -61,7 +62,7 @@ pub fn parse(self: *Self, allocator: Allocator, emu: *Emulator) !String {
var i: u32 = 0; var i: u32 = 0;
while (i < r.len + 1) : (i += 1) { while (i < r.len + 1) : (i += 1) {
var reg: u32 = if (i < r.len) r[i] else cpsr; 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 // 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 // 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' // TODO: Figure out the difference between 'M' and 'X'
'D' => { 'D' => {
log.info("Disconnecting...", .{}); log.info("Disconnecting...", .{});
state.should_quit = true;
return .{ .static = "OK" }; return .{ .static = "OK" };
}, },
'H' => return .{ .static = "" }, 'H' => return .{ .static = "" },

View File

@ -57,20 +57,23 @@ pub const memory_map: []const u8 =
// FIXME: Shouldn't this be a Packet Struct? // FIXME: Shouldn't this be a Packet Struct?
pkt_cache: ?[]const u8 = null, pkt_cache: ?[]const u8 = null,
server: Server, socket: Server,
state: State = .{},
emu: Emulator, emu: Emulator,
pub const State = struct { should_quit: bool = false };
pub fn init(emulator: Emulator) !Self { pub fn init(emulator: Emulator) !Self {
var server = std.net.StreamServer.init(.{}); var server = std.net.StreamServer.init(.{});
try server.listen(std.net.Address.initIp4([_]u8{0} ** 4, port)); 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 { pub fn deinit(self: *Self, allocator: Allocator) void {
self.reset(allocator); self.reset(allocator);
self.server.deinit(); self.socket.deinit();
self.* = undefined; self.* = undefined;
} }
@ -86,11 +89,13 @@ const Action = union(enum) {
pub fn run(self: *Self, allocator: Allocator, quit: *Atomic(bool)) !void { pub fn run(self: *Self, allocator: Allocator, quit: *Atomic(bool)) !void {
var buf: [Packet.max_len]u8 = undefined; 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}); log.info("client connected from {}", .{client.address});
while (true) { 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 (len == 0) break;
if (quit.load(.Monotonic)) 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; var packet = Packet.from(allocator, input) catch return .nack;
defer packet.deinit(allocator); 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); defer string.deinit(allocator);
const reply = string.inner(); const reply = string.inner();

View File

@ -60,7 +60,7 @@ test Server {
var server = try Server.init(iface); var server = try Server.init(iface);
defer server.deinit(allocator); 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(); defer t.join();
var should_quit = std.atomic.Atomic(bool).init(false); var should_quit = std.atomic.Atomic(bool).init(false);