Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/CURRENT_STATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 25 additions & 6 deletions tools/bindiff/src/main.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const diff = @import("diff.zig");

const usage =
Expand All @@ -25,24 +26,43 @@ 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;
var json = false;
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| {
Expand Down Expand Up @@ -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;
}

Expand Down
45 changes: 45 additions & 0 deletions tools/bindiff/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<HHIIIIIHHHHHH", b, 16, 2, 8, 1, 0x1000, 0, table, 0, 52, 32, 0, 40, count + 2, 1)
b[52:52 + len(names)] = names
struct.pack_into("<10I", b, table + 40, 1, 3, 0, 0, 52, len(names), 0, 0, 1, 0)
for index in range(count):
struct.pack_into("<10I", b, table + (index + 2) * 40, 1, 8, 3, address + index * 4, 0x100, 4, 0, 0, 4, 0)
return b


class CliTests(unittest.TestCase):
def setUp(self):
root = ROOT / ".tmp/bindiff-tests"
Expand Down Expand Up @@ -200,6 +214,37 @@ def test_malformed_elf(self):
(self.dir / "right").write_bytes(b"~PSP")
self.run_cli("--elf", "--json", "left", "right", code=2)

@unittest.skipUnless(os.name == "nt", "Windows pipes")
def test_output_to_a_full_asynchronous_pipe(self):
# Git Bash gives programs pipes like this one; the pipes of subprocess are synchronous.
import ctypes
from ctypes import wintypes
import msvcrt

(self.dir / "left").write_bytes(many_sections_elf(0x1000, 3000))
(self.dir / "right").write_bytes(many_sections_elf(0x9000, 3000))
expected = self.run_cli("--elf", "left", "right", code=1).stdout
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.CreateNamedPipeW.restype = wintypes.HANDLE
kernel32.CreateFileW.restype = wintypes.HANDLE
name = rf"\\.\pipe\p3p-bindiff-test-{os.getpid()}"
inbound, overlapped, generic_write, open_existing = 1, 0x40000000, 0x40000000, 3
server = kernel32.CreateNamedPipeW(name, inbound, 0, 1, 4096, 4096, 0, None)
client = kernel32.CreateFileW(name, generic_write, 0, None, open_existing, overlapped, None)
self.assertNotIn(wintypes.HANDLE(-1).value, (server, client))
descriptor = msvcrt.open_osfhandle(client, 0)
process = subprocess.Popen([str(EXE), "--elf", "left", "right"], cwd=self.dir, stdout=descriptor, stderr=subprocess.PIPE)
os.close(descriptor)
received = bytearray()
chunk = ctypes.create_string_buffer(65536)
count = wintypes.DWORD()
while kernel32.ReadFile(wintypes.HANDLE(server), chunk, len(chunk), ctypes.byref(count), None):
received += chunk.raw[:count.value]
kernel32.CloseHandle(wintypes.HANDLE(server))
stderr = process.communicate()[1]
self.assertEqual(process.returncode, 1, stderr[-400:])
self.assertEqual(bytes(received), expected)

def test_output_error(self):
(self.dir / "left").write_bytes(bytes(600))
(self.dir / "right").write_bytes(b"\x00\x01" * 300)
Expand Down