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
4 changes: 2 additions & 2 deletions src/COFF/COFFHandle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function readmeta(io::IO, ::Type{H}) where {H <: COFFHandle}
magic = [read(io, UInt8) for idx in 1:4]
if any(magic .!= PE_magic)
msg = """
Magic Number 0x$(join(string.(magic, base=16),"")) does not match expected PE
magic number 0x$(join("", string.(PE_magic, base=16)))
Comment on lines -52 to -53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How....did this ever work 😕 I guess it never did. Also in v0.6, when this line

magic number 0x$(join("", hex.(PE_magic)))
was written, it gave the wrong result:

julia> join("", hex.(UInt8['P','E','\0','\0']))
""

Magic Number 0x$(join(string.(magic, base=16, pad=2),"")) does not match expected PE
magic number 0x$(join(string.(PE_magic, base=16, pad=2),""))
"""
throw(MagicMismatch(replace(strip(msg), "\n" => " ")))
end
Expand Down
4 changes: 2 additions & 2 deletions src/ELF/ELFHandle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function readmeta(io::IO, ::Type{H}) where {H <: ELFHandle}
magic = [read(io, UInt8) for idx in 1:4]
if any(magic .!= elven_magic)
msg = """
Magic Number 0x$(join(string.(magic, base=16),"")) does not match expected ELF
magic number 0x$(join("", string.(elven_magic, base=16)))
Magic Number 0x$(join(string.(magic, base=16, pad=2),"")) does not match expected ELF
magic number 0x$(join(string.(elven_magic, base=16, pad=2),""))
"""
throw(MagicMismatch(replace(strip(msg), "\n" => " ")))
end
Expand Down
6 changes: 3 additions & 3 deletions src/MachO/MachOHeader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function macho_header_type(magic::UInt32)
elseif magic in (METALLIB_MAGIC,)
return MetallibHeader{MachOHandle}
else
throw(MagicMismatch("Invalid Magic ($(string(magic, base=16)))!"))
throw(MagicMismatch("Invalid Magic (0x$(string(magic, base=16, pad=8)))!"))
end
end

Expand All @@ -63,7 +63,7 @@ function macho_is64bit(magic::UInt32)
elseif magic in (MH_MAGIC, MH_CIGAM, FAT_MAGIC, FAT_CIGAM, FAT_MAGIC_METAL, FAT_CIGAM_METAL, METALLIB_MAGIC)
return false
else
throw(MagicMismatch("Invalid Magic ($(string(magic, base=16)))!"))
throw(MagicMismatch("Invalid Magic (0x$(string(magic, base=16, pad=8)))!"))
end
end

Expand All @@ -79,7 +79,7 @@ function macho_endianness(magic::UInt32)
elseif magic in (MH_MAGIC, MH_MAGIC_64, FAT_MAGIC, FAT_MAGIC_METAL, METALLIB_MAGIC)
return :LittleEndian
else
throw(MagicMismatch("Invalid Magic ($(string(magic, base=16)))!"))
throw(MagicMismatch("Invalid Magic (0x$(string(magic, base=16, pad=8)))!"))
end
end

Expand Down
27 changes: 27 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ using Test
end
end

function check_magic_mismatch(path, HandleType)
err = try
readmeta(open(path, "r"), HandleType)
catch err
err
end

@test err isa MagicMismatch
if HandleType === ELFHandle
@test occursin(r"Magic Number 0x[a-f0-9]{8} does not match expected ELF magic number 0x7f454c46", repr(err))
elseif HandleType === COFFHandle
@test occursin(r"Magic Number 0x[a-f0-9]{8} does not match expected PE magic number 0x50450000", repr(err))
elseif HandleType === MachOHandle
@test occursin(r"Invalid Magic \(0x[a-f0-9]{8}\)\!", repr(err))
else
@assert false "unexpected handle type"
end

return nothing
end

function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
# Actually read it in
oh_exe = only(readmeta(open(fooifier_path, "r")))
Expand Down Expand Up @@ -43,6 +64,12 @@ function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
# Test that we got the right type
@test typeof(oh) <: H

# Test that the wrong types all error as expected
for MismatchedType in (ELFHandle, COFFHandle, MachOHandle)
H === MismatchedType && continue
check_magic_mismatch(fooifier_path, MismatchedType)
end

# Test that we got the right number of bits
@test is64bit(oh) == (bits == "64")
@test platforms_match(Platform(oh), platforms[dir_path])
Expand Down
Loading