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 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 = "" },

View File

@ -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();

View File

@ -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);