From e8a94fb77e1f4e1cdd49f918c1104aff7bad75c6 Mon Sep 17 00:00:00 2001 From: endercodezz Date: Fri, 6 Nov 2026 16:46:32 +0200 Subject: [PATCH] fix(tools): keep BinDiff from panicking on a full Git Bash pipe Zig 0.17.1 file writers panic when an asynchronous pipe fills up, which took about 200 changed ELF sections. The report is now built in memory and written once, through WriteFile on Windows. Exit codes are unchanged. Verified: 3,000 changed sections through a Git Bash pipe give the same bytes as the output to a file; the new overlapped pipe test fails with exit 3 before the fix; tools/bindiff/check.py 15/15 CLI tests. --- docs/CURRENT_STATE.md | 7 ++++++ tools/bindiff/src/main.zig | 31 +++++++++++++++++++++----- tools/bindiff/test_cli.py | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/docs/CURRENT_STATE.md b/docs/CURRENT_STATE.md index c6aa3d93..213de66b 100644 --- a/docs/CURRENT_STATE.md +++ b/docs/CURRENT_STATE.md @@ -12,6 +12,13 @@ This is the current source of truth, newest entries first. Evidence concerns ULU - [UNVERIFIED] Hardware with a slow card: no periodic freezes and speed near 97-99 % in the same scene. Needs the reporter's confirmation on the next release. A host crash now leaves only the start report unless `report_live.txt` is present. - `profile_seconds.csv` (opt-in) still writes every second on the game's thread, so it can disturb a slow card itself. +## BinDiff no longer panics on a full Git Bash pipe (#74, 2026-11-06) + +- [VERIFIED] Zig 0.17.1 panics when stdout is an asynchronous pipe that fills up (`.PENDING => unreachable`, `lib/std/Io/Threaded.zig`); pipes of Git Bash/MSYS are asynchronous. `p3p-bindiff --elf` with 3,000 changed sections stopped at about 73 KB with exit 3. +- `tools/bindiff/src/main.zig`: the report is built in memory and written once, through `WriteFile` on Windows. Exit codes are unchanged, also with a closed stdout (exit 2). +- [VERIFIED] The same case through `| cat` now gives the 1,083,473 bytes of the output to a file, exit 1. The new test fails with exit 3 on the build before the fix. `python tools/bindiff/check.py` passes, 15/15 CLI tests; the `x86_64-linux-musl` cross-build passes. +- [VERIFIED] Normal use was rarely affected: at most about 6.3 KB in byte mode, 919 bytes for `--elf --json` of the real EBOOT against itself. + ## Builder 0.3.3 release preparation (2026-11-05) - `platform/pc/builder/builder.rc` FileVersion and ProductVersion 0.3.3 ([#69](https://github.com/endercodezz/P3P3DS/issues/69)). Contents since 0.3.2: Builder update notice (#67), texture packs (texpack converter, PPSSPP-compatible keys, replacement on the PC and in the 3DS renderer), native game preparation (#33), host time audit docs (#50), Zig BinDiff tool. diff --git a/tools/bindiff/src/main.zig b/tools/bindiff/src/main.zig index dcc04e96..6e7c6f0f 100644 --- a/tools/bindiff/src/main.zig +++ b/tools/bindiff/src/main.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const diff = @import("diff.zig"); const usage = @@ -25,6 +26,30 @@ pub fn main(init: std.process.Init) void { } fn run(init: std.process.Init) !u8 { + var output: std.Io.Writer.Allocating = .init(init.gpa); + defer output.deinit(); + const code = try report(init, &output.writer); + try writeStdout(init.io, output.written()); + return code; +} + +extern "kernel32" fn WriteFile(file: std.os.windows.HANDLE, buffer: [*]const u8, size: u32, written: *u32, overlapped: ?*anyopaque) callconv(.winapi) c_int; + +/// Pipes of Git Bash/MSYS are asynchronous, and the Zig 0.17.1 file writers panic +/// when such a pipe fills up (`.PENDING => unreachable`); WriteFile waits instead. +fn writeStdout(io: std.Io, bytes: []const u8) !void { + const stdout = std.Io.File.stdout(); + if (builtin.os.tag != .windows) return stdout.writeStreamingAll(io, bytes); + var rest = bytes; + while (rest.len != 0) { + var written: u32 = 0; + const count: u32 = @intCast(@min(rest.len, 1 << 20)); + if (WriteFile(stdout.handle, rest.ptr, count, &written, null) == 0) return error.WriteFailed; + rest = rest[written..]; + } +} + +fn report(init: std.process.Init, writer: *std.Io.Writer) !u8 { const allocator = init.gpa; const args = try init.minimal.args.toSlice(init.arena.allocator()); var elf_mode = false; @@ -32,17 +57,12 @@ fn run(init: std.process.Init) !u8 { var literal = false; var paths: [2][]const u8 = undefined; var count: usize = 0; - var buffer: [8192]u8 = undefined; - var stdout: std.Io.File.Writer = .init(.stdout(), init.io, &buffer); - const writer = &stdout.interface; if (args.len == 2 and std.mem.eql(u8, args[1], "--help")) { try writer.writeAll(usage); - try writer.flush(); return 0; } if (args.len == 2 and std.mem.eql(u8, args[1], "--version")) { try writer.writeAll("p3p-bindiff 0.1.0 (Zig 0.17.1)\n"); - try writer.flush(); return 0; } for (args[1..]) |arg| { @@ -130,7 +150,6 @@ fn run(init: std.process.Init) !u8 { try textEntries(diff.Section, writer, "section", d.sections.items); } } - try writer.flush(); return if (bytes.equal()) 0 else 1; } diff --git a/tools/bindiff/test_cli.py b/tools/bindiff/test_cli.py index f5ee00a5..56e24fba 100644 --- a/tools/bindiff/test_cli.py +++ b/tools/bindiff/test_cli.py @@ -30,6 +30,20 @@ def elf_fixture(prx=True): return b +def many_sections_elf(address, count): + """A section header table only: NULL, .shstrtab and `count` NOBITS sections.""" + names = b"\x00.s\x00" + table = (52 + len(names) + 3) & ~3 + b = bytearray(table + (count + 2) * 40) + b[:16] = b"\x7fELF\x01\x01\x01" + bytes(9) + struct.pack_into("