Skip to content

Commit 232c60a

Browse files
Added CPUTypeMismatchException (#450)
1. New exception triggers when there is a cputype or cputype mismatch between fat header and internal macho slice in the FatFile#populate_machos method. 2. Added a test for it along with adding a new binary file to the test/bin/llvm folder.
1 parent 8700955 commit 232c60a

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

lib/macho/exceptions.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ def initialize
6363
end
6464
end
6565

66+
# Raised when there is a mismatch between the fat arch
67+
# and internal slice cputype or cpusubtype.
68+
class CPUTypeMismatchError < NotAMachOError
69+
def initialize(fat_cputype, fat_cpusubtype, macho_cputype, macho_cpusubtype)
70+
# @param cputype_fat [Integer] the CPU type in the fat header
71+
# @param cpusubtype_fat [Integer] the CPU subtype in the fat header
72+
# @param cputype_macho [Integer] the CPU type in the macho header
73+
# @param cpusubtype_macho [Integer] the CPU subtype in the macho header
74+
super ("Mismatch between cputypes >> 0x%08<fat_cputype>x and 0x%08<macho_cputype>x\n" \
75+
"and/or cpusubtypes >> 0x%08<fat_cpusubtype>x and 0x%08<macho_cpusubtype>x" %
76+
{ :fat_cputype => fat_cputype, :macho_cputype => macho_cputype,
77+
:fat_cpusubtype => fat_cpusubtype, :macho_cpusubtype => macho_cpusubtype })
78+
end
79+
end
80+
6681
# Raised when a fat binary is loaded with MachOFile.
6782
class FatBinaryError < MachOError
6883
def initialize

lib/macho/fat_file.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,13 @@ def populate_machos
379379

380380
fat_archs.each do |arch|
381381
machos << MachOFile.new_from_bin(@raw_data[arch.offset, arch.size], **options)
382+
383+
# Make sure that each fat_arch and internal slice.
384+
# contain matching cputypes and cpusubtypes
385+
next if machos.last.header.cputype == arch.cputype &&
386+
machos.last.header.cpusubtype == arch.cpusubtype
387+
388+
raise CPUTypeMismatchError.new(arch.cputype, arch.cpusubtype, machos.last.header.cputype, machos.last.header.cpusubtype)
382389
end
383390

384391
machos
56 Bytes
Binary file not shown.

test/test_fat.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def test_zero_arch_file
4242
end
4343
end
4444

45+
def test_mismatch_cpu_arch_file
46+
assert_raises MachO::CPUTypeMismatchError do
47+
MachO::FatFile.new("test/bin/llvm/macho-invalid-fat_cputype")
48+
end
49+
end
50+
4551
def test_fat_header
4652
filenames = FAT_ARCH_PAIRS.map { |a| fixture(a, "hello.bin") }
4753

0 commit comments

Comments
 (0)