Skip to content

Commit

Permalink
Added CPUTypeMismatchException (#450)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apainintheneck committed Mar 21, 2022
1 parent 8700955 commit 232c60a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/macho/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ def initialize
end
end

# Raised when there is a mismatch between the fat arch
# and internal slice cputype or cpusubtype.
class CPUTypeMismatchError < NotAMachOError
def initialize(fat_cputype, fat_cpusubtype, macho_cputype, macho_cpusubtype)
# @param cputype_fat [Integer] the CPU type in the fat header
# @param cpusubtype_fat [Integer] the CPU subtype in the fat header
# @param cputype_macho [Integer] the CPU type in the macho header
# @param cpusubtype_macho [Integer] the CPU subtype in the macho header
super ("Mismatch between cputypes >> 0x%08<fat_cputype>x and 0x%08<macho_cputype>x\n" \
"and/or cpusubtypes >> 0x%08<fat_cpusubtype>x and 0x%08<macho_cpusubtype>x" %
{ :fat_cputype => fat_cputype, :macho_cputype => macho_cputype,
:fat_cpusubtype => fat_cpusubtype, :macho_cpusubtype => macho_cpusubtype })
end
end

# Raised when a fat binary is loaded with MachOFile.
class FatBinaryError < MachOError
def initialize
Expand Down
7 changes: 7 additions & 0 deletions lib/macho/fat_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ def populate_machos

fat_archs.each do |arch|
machos << MachOFile.new_from_bin(@raw_data[arch.offset, arch.size], **options)

# Make sure that each fat_arch and internal slice.
# contain matching cputypes and cpusubtypes
next if machos.last.header.cputype == arch.cputype &&
machos.last.header.cpusubtype == arch.cpusubtype

raise CPUTypeMismatchError.new(arch.cputype, arch.cpusubtype, machos.last.header.cputype, machos.last.header.cpusubtype)
end

machos
Expand Down
Binary file added test/bin/llvm/macho-invalid-fat_cputype
Binary file not shown.
6 changes: 6 additions & 0 deletions test/test_fat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def test_zero_arch_file
end
end

def test_mismatch_cpu_arch_file
assert_raises MachO::CPUTypeMismatchError do
MachO::FatFile.new("test/bin/llvm/macho-invalid-fat_cputype")
end
end

def test_fat_header
filenames = FAT_ARCH_PAIRS.map { |a| fixture(a, "hello.bin") }

Expand Down

0 comments on commit 232c60a

Please sign in to comment.