const std = @import("std"); pub const mach_header_64_byte_size: u32 = 32; pub const load_command_byte_size: u32 = 8; pub const segment_command_64_byte_size: u32 = 72; pub const section_64_byte_size: u32 = 80; pub const symtab_command_byte_size: u32 = 24; pub const uuid_command_byte_size: u32 = 24; pub const entry_point_command_byte_size: u32 = 24; pub const MachO = struct { bytes: []const u8, header: MachHeader64, endian: std.builtin.Endian, load_commands: []const u8, pub fn parse(bytes: []const u8) (ParseError || std.Io.Reader.Error)!MachO { var reader: std.Io.Reader = .fixed(bytes); std.debug.assert(reader.seek == 0); std.debug.assert(reader.end == bytes.len); const raw_magic = try reader.takeInt(u32, .little); const magic: Magic, const endian: std.builtin.Endian = switch (raw_magic) { @intFromEnum(Magic.mh_magic_64) => .{ .mh_magic_64, .little }, @intFromEnum(Magic.mh_cigam_64) => .{ .mh_cigam_64, .big }, @intFromEnum(Magic.mh_magic) => return error.Unsupported32Bit, @intFromEnum(Magic.mh_cigam) => return error.Unsupported32Bit, else => return error.InvalidMagic, }; const header = try readHeader(&reader, magic, endian); const load_commands: []const u8 = try reader.take(@intCast(header.command_bytes)); std.debug.assert(load_commands.len == header.command_bytes); std.debug.assert(reader.seek <= reader.end); return .{ .bytes = bytes, .header = header, .endian = endian, .load_commands = load_commands, }; } pub fn commands(macho: MachO) LoadCommandIterator { const reader: std.Io.Reader = .fixed(macho.load_commands); std.debug.assert(reader.seek == 0); std.debug.assert(reader.end == macho.load_commands.len); return .{ .reader = reader, .remaining = macho.header.command_count, .endian = macho.endian, }; } fn readHeader( reader: *std.Io.Reader, magic: Magic, endian: std.builtin.Endian, ) std.Io.Reader.Error!MachHeader64 { std.debug.assert(reader.seek == @sizeOf(u32)); std.debug.assert(reader.seek <= reader.end); const header: MachHeader64 = .{ .magic = magic, .cpu_type = @enumFromInt(try reader.takeInt(i32, endian)), .cpu_subtype = @enumFromInt(try reader.takeInt(i32, endian)), .file_type = @enumFromInt(try reader.takeInt(u32, endian)), .command_count = try reader.takeInt(u32, endian), .command_bytes = try reader.takeInt(u32, endian), .flags = @enumFromInt(try reader.takeInt(u32, endian)), .reserved = try reader.takeInt(u32, endian), }; std.debug.assert(reader.seek == mach_header_64_byte_size); std.debug.assert(reader.seek <= reader.end); return header; } }; pub const MachHeader64 = struct { magic: Magic, cpu_type: CpuType, cpu_subtype: CpuSubtype, file_type: FileType, command_count: u32, command_bytes: u32, flags: HeaderFlags, reserved: u32, }; pub const LoadCommandIterator = struct { reader: std.Io.Reader, remaining: u32, endian: std.builtin.Endian, pub fn next(iterator: *LoadCommandIterator) (ParseError || std.Io.Reader.Error)!?LoadCommand { std.debug.assert(iterator.reader.seek <= iterator.reader.end); std.debug.assert(iterator.reader.end <= iterator.reader.buffer.len); if (iterator.remaining == 0) return null; const header_bytes = try iterator.reader.peek(load_command_byte_size); const command: LoadCommandType = @enumFromInt(std.mem.readInt( u32, header_bytes[0..4], iterator.endian, )); const byte_size = std.mem.readInt(u32, header_bytes[4..8], iterator.endian); if (byte_size < load_command_byte_size) return error.InvalidLoadCommand; const bytes: []const u8 = try iterator.reader.take(@intCast(byte_size)); iterator.remaining -= 1; std.debug.assert(iterator.reader.seek <= iterator.reader.end); std.debug.assert(bytes.len == byte_size); return .{ .header = .{ .command = command, .byte_size = byte_size, }, .bytes = bytes, }; } }; pub const LoadCommand = struct { header: LoadCommandHeader, bytes: []const u8, pub fn payload(command: LoadCommand) []const u8 { std.debug.assert(command.bytes.len >= load_command_byte_size); std.debug.assert(command.bytes.len == command.header.byte_size); return command.bytes[load_command_byte_size..]; } // TODO: make LoadCommand a tagged union over the command type to // avoid using parse* on wrong comands pub fn parseSegment64(command: LoadCommand, endian: std.builtin.Endian) ParseError!SegmentCommand64 { std.debug.assert(command.header.command == .segment_64); std.debug.assert(command.bytes.len == command.header.byte_size); if (command.bytes.len < segment_command_64_byte_size) return error.InvalidLoadCommand; var reader: std.Io.Reader = .fixed(command.payload()[0 .. segment_command_64_byte_size - load_command_byte_size]); var segment: SegmentCommand64 = .{ .header = command.header, .name = .{ .bytes = (reader.takeArray(16) catch unreachable).* }, .virtual_address = @enumFromInt(reader.takeInt(u64, endian) catch unreachable), .virtual_size = reader.takeInt(u64, endian) catch unreachable, .file_offset = @enumFromInt(reader.takeInt(u64, endian) catch unreachable), .file_size = reader.takeInt(u64, endian) catch unreachable, .maximum_protection = @enumFromInt(reader.takeInt(i32, endian) catch unreachable), .initial_protection = @enumFromInt(reader.takeInt(i32, endian) catch unreachable), .section_count = reader.takeInt(u32, endian) catch unreachable, .flags = @enumFromInt(reader.takeInt(u32, endian) catch unreachable), .section_bytes = &.{}, .endian = endian, }; const section_bytes = std.math.mul(u32, segment.section_count, section_64_byte_size) catch return error.InvalidLoadCommand; const expected_size = std.math.add(u32, segment_command_64_byte_size, section_bytes) catch return error.InvalidLoadCommand; if (expected_size != command.header.byte_size) return error.InvalidLoadCommand; segment.section_bytes = command.bytes[segment_command_64_byte_size..]; std.debug.assert(reader.seek == reader.end); std.debug.assert(segment.section_bytes.len == section_bytes); return segment; } pub fn parseEntryPoint(command: LoadCommand, endian: std.builtin.Endian) ParseError!EntryPointCommand { std.debug.assert(command.header.command == .main); std.debug.assert(command.bytes.len == command.header.byte_size); if (command.bytes.len != entry_point_command_byte_size) return error.InvalidLoadCommand; var reader: std.Io.Reader = .fixed(command.payload()); const entry_point: EntryPointCommand = .{ .header = command.header, .entry_offset = @enumFromInt(reader.takeInt(u64, endian) catch unreachable), .stack_size = reader.takeInt(u64, endian) catch unreachable, }; std.debug.assert(reader.seek == reader.end); std.debug.assert(reader.end == entry_point_command_byte_size - load_command_byte_size); return entry_point; } pub fn parseSymbolTable(command: LoadCommand, endian: std.builtin.Endian) ParseError!SymbolTableCommand { std.debug.assert(command.header.command == .symtab); std.debug.assert(command.bytes.len == command.header.byte_size); if (command.bytes.len != symtab_command_byte_size) return error.InvalidLoadCommand; var reader: std.Io.Reader = .fixed(command.payload()); const symbol_table: SymbolTableCommand = .{ .header = command.header, .symbol_offset = @enumFromInt(@as(u64, reader.takeInt(u32, endian) catch unreachable)), .symbol_count = reader.takeInt(u32, endian) catch unreachable, .string_offset = @enumFromInt(@as(u64, reader.takeInt(u32, endian) catch unreachable)), .string_bytes = reader.takeInt(u32, endian) catch unreachable, }; std.debug.assert(reader.seek == reader.end); std.debug.assert(reader.end == symtab_command_byte_size - load_command_byte_size); return symbol_table; } pub fn parseUuid(command: LoadCommand) ParseError!UuidCommand { std.debug.assert(command.header.command == .uuid); std.debug.assert(command.bytes.len == command.header.byte_size); if (command.bytes.len != uuid_command_byte_size) return error.InvalidLoadCommand; const uuid_bytes = command.payload(); std.debug.assert(uuid_bytes.len == @sizeOf(Uuid)); std.debug.assert(uuid_bytes.len == uuid_command_byte_size - load_command_byte_size); return .{ .header = command.header, .uuid = .{ .bytes = uuid_bytes[0..16].* }, }; } }; pub const SectionIterator = struct { reader: std.Io.Reader, remaining: u32, endian: std.builtin.Endian, pub fn next(iterator: *SectionIterator) ?Section64 { std.debug.assert(iterator.reader.seek <= iterator.reader.end); std.debug.assert(iterator.reader.end <= iterator.reader.buffer.len); if (iterator.remaining == 0) return null; const section: Section64 = .{ .name = .{ .bytes = (iterator.reader.takeArray(16) catch unreachable).* }, .segment_name = .{ .bytes = (iterator.reader.takeArray(16) catch unreachable).* }, .address = @enumFromInt(iterator.reader.takeInt(u64, iterator.endian) catch unreachable), .byte_size = iterator.reader.takeInt(u64, iterator.endian) catch unreachable, .file_offset = @enumFromInt(@as(u64, iterator.reader.takeInt(u32, iterator.endian) catch unreachable)), .alignment_power = iterator.reader.takeInt(u32, iterator.endian) catch unreachable, .relocation_offset = @enumFromInt(@as(u64, iterator.reader.takeInt(u32, iterator.endian) catch unreachable)), .relocation_count = iterator.reader.takeInt(u32, iterator.endian) catch unreachable, .flags = @enumFromInt(iterator.reader.takeInt(u32, iterator.endian) catch unreachable), .reserved_1 = iterator.reader.takeInt(u32, iterator.endian) catch unreachable, .reserved_2 = iterator.reader.takeInt(u32, iterator.endian) catch unreachable, .reserved_3 = iterator.reader.takeInt(u32, iterator.endian) catch unreachable, }; iterator.remaining -= 1; std.debug.assert(iterator.reader.seek <= iterator.reader.end); std.debug.assert(iterator.reader.seek % section_64_byte_size == 0); return section; } }; pub const Magic = enum(u32) { mh_magic = 0xfeedface, // little-endian mh_cigam = 0xcefaedfe, // big-endian mh_magic_64 = 0xfeedfacf, mh_cigam_64 = 0xcffaedfe, }; pub const CpuType = enum(i32) { x86_64 = 0x01000007, arm64 = 0x0100000c, _, }; pub const CpuSubtype = enum(i32) { _, }; pub const FileType = enum(u32) { object = 0x1, execute = 0x2, fixed_vm_library = 0x3, core = 0x4, preload = 0x5, dynamic_library = 0x6, dynamic_linker = 0x7, bundle = 0x8, dynamic_library_stub = 0x9, dsym = 0xa, kernel_extension_bundle = 0xb, fileset = 0xc, gpu_execute = 0xd, gpu_dynamic_library = 0xe, _, }; pub const LoadCommandType = enum(u32) { symtab = 0x2, segment_64 = 0x19, uuid = 0x1b, main = 0x80000028, _, }; // https://ziglang.org/devlog/2024/#2024-11-04 pub const FileOffset = enum(u64) { _, }; pub const VirtualAddress = enum(u64) { _, }; pub const HeaderFlags = enum(u32) { none = 0, _, }; pub const SegmentFlags = enum(u32) { none = 0, _, }; pub const SectionFlags = enum(u32) { none = 0, _, }; pub const VmProtection = enum(i32) { none = 0, _, }; pub const Name = struct { bytes: [16]u8, }; pub const Uuid = struct { bytes: [16]u8, }; pub const ParseError = error{ InvalidLoadCommand, InvalidMagic, Unsupported32Bit, }; pub const LoadCommandHeader = struct { command: LoadCommandType, byte_size: u32, }; pub const SegmentCommand64 = struct { header: LoadCommandHeader, name: Name, virtual_address: VirtualAddress, virtual_size: u64, file_offset: FileOffset, file_size: u64, maximum_protection: VmProtection, initial_protection: VmProtection, section_count: u32, flags: SegmentFlags, section_bytes: []const u8, endian: std.builtin.Endian, pub fn sections(segment: SegmentCommand64) SectionIterator { std.debug.assert(segment.header.command == .segment_64); std.debug.assert(segment.section_bytes.len == @as(usize, segment.section_count) * section_64_byte_size); const reader: std.Io.Reader = .fixed(segment.section_bytes); std.debug.assert(reader.seek == 0); std.debug.assert(reader.end == segment.section_bytes.len); return .{ .reader = reader, .remaining = segment.section_count, .endian = segment.endian, }; } }; pub const Section64 = struct { name: Name, segment_name: Name, address: VirtualAddress, byte_size: u64, file_offset: FileOffset, alignment_power: u32, relocation_offset: FileOffset, relocation_count: u32, flags: SectionFlags, reserved_1: u32, reserved_2: u32, reserved_3: u32, }; pub const SymbolTableCommand = struct { header: LoadCommandHeader, symbol_offset: FileOffset, symbol_count: u32, string_offset: FileOffset, string_bytes: u32, }; pub const UuidCommand = struct { header: LoadCommandHeader, uuid: Uuid, }; pub const EntryPointCommand = struct { header: LoadCommandHeader, entry_offset: FileOffset, stack_size: u64, }; comptime { std.debug.assert(@sizeOf(Magic) == @sizeOf(u32)); std.debug.assert(@alignOf(Magic) == @alignOf(u32)); std.debug.assert(@sizeOf(CpuType) == @sizeOf(i32)); std.debug.assert(@sizeOf(LoadCommandType) == @sizeOf(u32)); std.debug.assert(@sizeOf(FileOffset) == @sizeOf(u64)); std.debug.assert(@sizeOf(VirtualAddress) == @sizeOf(u64)); std.debug.assert(@sizeOf(Name) == 16); std.debug.assert(@sizeOf(Uuid) == 16); std.debug.assert(mach_header_64_byte_size == 8 * @sizeOf(u32)); std.debug.assert(load_command_byte_size == 2 * @sizeOf(u32)); std.debug.assert(segment_command_64_byte_size == 72); std.debug.assert(section_64_byte_size == 80); std.debug.assert(symtab_command_byte_size == 6 * @sizeOf(u32)); std.debug.assert(entry_point_command_byte_size == load_command_byte_size + 2 * @sizeOf(u64)); } test "thin 64-bit Mach-O magic values" { try std.testing.expectEqual(@as(u32, 0xfeedfacf), @intFromEnum(Magic.mh_magic_64)); try std.testing.expectEqual(@as(u32, 0xcffaedfe), @intFromEnum(Magic.mh_cigam_64)); } test "open load command types preserve unknown values" { const unknown: LoadCommandType = @enumFromInt(0x12345678); try std.testing.expectEqual(@as(u32, 0x12345678), @intFromEnum(unknown)); try std.testing.expect(unknown != .segment_64); } test "parse little-endian 64-bit header" { const bytes = [_]u8{ 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const macho = try MachO.parse(&bytes); try std.testing.expectEqual(Magic.mh_magic_64, macho.header.magic); try std.testing.expectEqual(CpuType.arm64, macho.header.cpu_type); try std.testing.expectEqual(FileType.execute, macho.header.file_type); try std.testing.expectEqual(@as(u32, 0), macho.header.command_count); try std.testing.expectEqual(@as(usize, 0), macho.load_commands.len); } test "parse big-endian 64-bit header" { const bytes = [_]u8{ 0xfe, 0xed, 0xfa, 0xcf, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const macho = try MachO.parse(&bytes); try std.testing.expectEqual(Magic.mh_cigam_64, macho.header.magic); try std.testing.expectEqual(CpuType.x86_64, macho.header.cpu_type); try std.testing.expectEqual(@as(i32, 3), @intFromEnum(macho.header.cpu_subtype)); try std.testing.expectEqual(FileType.execute, macho.header.file_type); try std.testing.expectEqual(std.builtin.Endian.big, macho.endian); } test "reject every truncated 64-bit header" { const bytes = [_]u8{ 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; for (0..bytes.len) |length| { try std.testing.expectError(error.EndOfStream, MachO.parse(bytes[0..length])); } } test "reject invalid and 32-bit magic" { const invalid = [_]u8{ 0, 0, 0, 0 }; const mach_32 = [_]u8{ 0xce, 0xfa, 0xed, 0xfe }; try std.testing.expectError(error.InvalidMagic, MachO.parse(&invalid)); try std.testing.expectError(error.Unsupported32Bit, MachO.parse(&mach_32)); } test "iterate borrowed load commands" { const bytes = [_]u8{ 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12, 0x08, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; const macho = try MachO.parse(&bytes); var commands = macho.commands(); const unknown = (try commands.next()).?; try std.testing.expectEqual(@as(u32, 0x12345678), @intFromEnum(unknown.header.command)); try std.testing.expectEqual(@as(u32, 8), unknown.header.byte_size); try std.testing.expectEqualSlices(u8, bytes[32..40], unknown.bytes); try std.testing.expectEqual(@as(usize, 0), unknown.payload().len); const uuid = (try commands.next()).?; try std.testing.expectEqual(LoadCommandType.uuid, uuid.header.command); try std.testing.expectEqual(@as(u32, 24), uuid.header.byte_size); try std.testing.expectEqualSlices(u8, bytes[40..64], uuid.bytes); try std.testing.expectEqualSlices(u8, bytes[48..64], uuid.payload()); const parsed_uuid = try uuid.parseUuid(); try std.testing.expectEqualSlices(u8, bytes[48..64], &parsed_uuid.uuid.bytes); try std.testing.expectEqual(@as(usize, 16), parsed_uuid.uuid.bytes.len); try std.testing.expectEqual(@as(?LoadCommand, null), try commands.next()); } test "reject load commands smaller than their header" { const bytes = [_]u8{ 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, }; const macho = try MachO.parse(&bytes); var commands = macho.commands(); try std.testing.expectError(error.InvalidLoadCommand, commands.next()); try std.testing.expectEqual(@as(u32, 1), commands.remaining); } test "parse segment command and sections" { const bytes = [_]u8{ 0x19, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const command: LoadCommand = .{ .header = .{ .command = .segment_64, .byte_size = bytes.len }, .bytes = &bytes, }; const segment = try command.parseSegment64(.little); try std.testing.expectEqualStrings("__TEXT", std.mem.sliceTo(&segment.name.bytes, 0)); try std.testing.expectEqual(@as(u64, 0x100000000), @intFromEnum(segment.virtual_address)); try std.testing.expectEqual(@as(u64, 0x2000), segment.virtual_size); try std.testing.expectEqual(@as(u32, 1), segment.section_count); var sections = segment.sections(); const section = sections.next().?; try std.testing.expectEqualStrings("__text", std.mem.sliceTo(§ion.name.bytes, 0)); try std.testing.expectEqualStrings("__TEXT", std.mem.sliceTo(§ion.segment_name.bytes, 0)); try std.testing.expectEqual(@as(u64, 0x100000f00), @intFromEnum(section.address)); try std.testing.expectEqual(@as(u64, 0x10), section.byte_size); try std.testing.expectEqual(@as(u64, 0xf00), @intFromEnum(section.file_offset)); try std.testing.expectEqual(@as(?Section64, null), sections.next()); } test "parse entry point command" { const bytes = [_]u8{ 0x28, 0x00, 0x00, 0x80, 0x18, 0x00, 0x00, 0x00, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe, }; const command: LoadCommand = .{ .header = .{ .command = .main, .byte_size = bytes.len }, .bytes = &bytes, }; const entry_point = try command.parseEntryPoint(.little); try std.testing.expectEqual(@as(u64, 0x0123456789abcdef), @intFromEnum(entry_point.entry_offset)); try std.testing.expectEqual(@as(u64, 0xfedcba9876543210), entry_point.stack_size); } test "reject incorrectly sized entry point commands" { const short_bytes = [_]u8{0} ** (entry_point_command_byte_size - 1); const long_bytes = [_]u8{0} ** (entry_point_command_byte_size + 1); const short_command: LoadCommand = .{ .header = .{ .command = .main, .byte_size = short_bytes.len }, .bytes = &short_bytes, }; const long_command: LoadCommand = .{ .header = .{ .command = .main, .byte_size = long_bytes.len }, .bytes = &long_bytes, }; try std.testing.expectError(error.InvalidLoadCommand, short_command.parseEntryPoint(.little)); try std.testing.expectError(error.InvalidLoadCommand, long_command.parseEntryPoint(.little)); } test "parse symbol table command" { const bytes = [_]u8{ 0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12, 0x03, 0x00, 0x00, 0x00, 0xf0, 0xde, 0xbc, 0x9a, 0x44, 0x33, 0x22, 0x11, }; const command: LoadCommand = .{ .header = .{ .command = .symtab, .byte_size = bytes.len }, .bytes = &bytes, }; const symbol_table = try command.parseSymbolTable(.little); try std.testing.expectEqual(@as(u64, 0x12345678), @intFromEnum(symbol_table.symbol_offset)); try std.testing.expectEqual(@as(u32, 3), symbol_table.symbol_count); try std.testing.expectEqual(@as(u64, 0x9abcdef0), @intFromEnum(symbol_table.string_offset)); try std.testing.expectEqual(@as(u32, 0x11223344), symbol_table.string_bytes); } test "reject incorrectly sized symbol table commands" { const short_bytes = [_]u8{0} ** (symtab_command_byte_size - 1); const long_bytes = [_]u8{0} ** (symtab_command_byte_size + 1); const short_command: LoadCommand = .{ .header = .{ .command = .symtab, .byte_size = short_bytes.len }, .bytes = &short_bytes, }; const long_command: LoadCommand = .{ .header = .{ .command = .symtab, .byte_size = long_bytes.len }, .bytes = &long_bytes, }; try std.testing.expectError(error.InvalidLoadCommand, short_command.parseSymbolTable(.little)); try std.testing.expectError(error.InvalidLoadCommand, long_command.parseSymbolTable(.little)); } test "reject segment command with inconsistent section count" { var bytes = [_]u8{0} ** segment_command_64_byte_size; bytes[0] = 0x19; bytes[4] = segment_command_64_byte_size; bytes[64] = 1; const command: LoadCommand = .{ .header = .{ .command = .segment_64, .byte_size = bytes.len }, .bytes = &bytes, }; try std.testing.expectError(error.InvalidLoadCommand, command.parseSegment64(.little)); try std.testing.expectEqual(@as(u8, 1), bytes[64]); } test "fuzz Mach-O parser" { // NOTE: these are added to help the fuzzer get to valid data. My // understanding is that Zig's fuzzer currently doesn't instrument // comparisons so it has a hard time finding these by pure // brute-force. const little_endian_empty_seed = [_]u8{ 0x20, 0x00, 0x00, 0x00, 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const big_endian_empty_seed = [_]u8{ 0x20, 0x00, 0x00, 0x00, 0xfe, 0xed, 0xfa, 0xcf, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const segment_seed = [_]u8{ 0x68, 0x00, 0x00, 0x00, 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const uuid_seed = [_]u8{ 0x38, 0x00, 0x00, 0x00, 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; const entry_point_seed = [_]u8{ 0x38, 0x00, 0x00, 0x00, 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x80, 0x18, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const symbol_table_seed = [_]u8{ 0x38, 0x00, 0x00, 0x00, 0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; try std.testing.fuzz({}, struct { fn testOne(_: void, smith: *std.testing.Smith) !void { var bytes: [4096]u8 = undefined; const length = smith.slice(&bytes); std.debug.assert(length <= bytes.len); std.debug.assert(bytes[0..length].len == length); const macho = MachO.parse(bytes[0..length]) catch return; var commands = macho.commands(); while (commands.next() catch return) |command| { if (command.header.command == .segment_64) { const segment = command.parseSegment64(macho.endian) catch return; var sections = segment.sections(); while (sections.next()) |_| {} } if (command.header.command == .main) { _ = command.parseEntryPoint(macho.endian) catch return; } if (command.header.command == .symtab) { _ = command.parseSymbolTable(macho.endian) catch return; } } } }.testOne, .{ .corpus = &.{ &little_endian_empty_seed, &big_endian_empty_seed, &segment_seed, &uuid_seed, &entry_point_seed, &symbol_table_seed, } }); }