From 6df55c2d86b6a2feaf7443c884fc563652159ae1 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 8 Apr 2022 02:34:08 -0300 Subject: [PATCH] feat: implement GamePak out-of-bounds reads --- src/bus/GamePak.zig | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/bus/GamePak.zig b/src/bus/GamePak.zig index 9ee9ac7..a87e678 100644 --- a/src/bus/GamePak.zig +++ b/src/bus/GamePak.zig @@ -3,6 +3,9 @@ const std = @import("std"); const Backup = @import("backup.zig").Backup; const Allocator = std.mem.Allocator; const log = std.log.scoped(.GamePak); + +const intToBytes = @import("../util.zig").intToBytes; + const Self = @This(); title: [12]u8, @@ -14,11 +17,21 @@ pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Sel const file = try std.fs.cwd().openFile(rom_path, .{}); defer file.close(); - const len = try file.getEndPos(); - const buf = try file.readToEndAlloc(alloc, len); - const title = parseTitle(buf); + const file_buf = try file.readToEndAlloc(alloc, try file.getEndPos()); + defer alloc.free(file_buf); - const kind = Backup.guessKind(buf) orelse .Sram; + const title = parseTitle(file_buf); + const kind = Backup.guessKind(file_buf) orelse .Sram; + + const buf = try alloc.alloc(u8, 0x200_0000); // 32MiB + + // GamePak addressable space has known values if there's no cartridge inserted + var i: usize = 0; + while (i < buf.len) : (i += @sizeOf(u16)) { + std.mem.copy(u8, buf[i..][0..2], &intToBytes(u16, @truncate(u16, i / 2))); + } + + std.mem.copy(u8, buf[0..file_buf.len], file_buf[0..file_buf.len]); // Copy over ROM const pak = Self{ .buf = buf,