Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CPUTypeMismatchException #450

Merged
merged 1 commit into from
Mar 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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