Add the first of what will be a long line of additional error checks for invalid Mach-O files.

This is where an LC_SEGMENT load command has a fileoff field that
extends past the end of the file.

Also fix llvm-nm and llvm-size to remove the errorToErrorCode() call so error messages are printed.
And needed to update a few test cases now that they do print the error messages just a
bit differently.

llvm-svn: 277845
This commit is contained in:
Kevin Enderby 2016-08-05 18:19:40 +00:00
parent 0f4f0c5d53
commit 600fb3f28e
7 changed files with 18 additions and 4 deletions

View File

@ -241,6 +241,11 @@ static Error parseSegmentLoadCommand(
const char *Sec = getSectionPtr(Obj, Load, J); const char *Sec = getSectionPtr(Obj, Load, J);
Sections.push_back(Sec); Sections.push_back(Sec);
} }
uint64_t FileSize = Obj->getData().size();
if (S.fileoff > FileSize)
return malformedError("load command " + Twine(LoadCommandIndex) +
" fileoff field in " + CmdName +
" extends past the end of the file");
IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname); IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname);
} else } else
return SegOrErr.takeError(); return SegOrErr.takeError();

Binary file not shown.

View File

@ -103,3 +103,12 @@ INVALID-FAT: truncated or malformed fat file (fat_arch_64 structs would extend p
RUN: not llvm-objdump -macho -private-headers -arch all %p/Inputs/macho-invalid-fat.obj.elf-x86_64 2>&1 | FileCheck -check-prefix INVALID-FAT-ELF %s RUN: not llvm-objdump -macho -private-headers -arch all %p/Inputs/macho-invalid-fat.obj.elf-x86_64 2>&1 | FileCheck -check-prefix INVALID-FAT-ELF %s
INVALID-FAT-ELF: Mach-O universal file: {{.*}}/macho-invalid-fat.obj.elf-x86_64 for architecture x86_64 is not a Mach-O file or an archive file INVALID-FAT-ELF: Mach-O universal file: {{.*}}/macho-invalid-fat.obj.elf-x86_64 for architecture x86_64 is not a Mach-O file or an archive file
RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-segment-fileoff 2>&1 | FileCheck -check-prefix INVALID-SEGMENT-FILEOFF %s
INVALID-SEGMENT-FILEOFF: macho-invalid-segment-fileoff': truncated or malformed object (load command 0 fileoff field in LC_SEGMENT extends past the end of the file)
RUN: not llvm-nm %p/Inputs/macho-invalid-segment-fileoff 2>&1 | FileCheck -check-prefix INVALID-SEGMENT-FILEOFF-NM %s
INVALID-SEGMENT-FILEOFF-NM: macho-invalid-segment-fileoff truncated or malformed object (load command 0 fileoff field in LC_SEGMENT extends past the end of the file)
RUN: not llvm-size %p/Inputs/macho-invalid-segment-fileoff 2>&1 | FileCheck -check-prefix INVALID-SEGMENT-FILEOFF-SIZE %s
INVALID-SEGMENT-FILEOFF-SIZE: macho-invalid-segment-fileoff truncated or malformed object (load command 0 fileoff field in LC_SEGMENT extends past the end of the file)

View File

@ -1,2 +1,2 @@
RUN: not llvm-nm %s 2>&1 | FileCheck %s -check-prefix=UNKNOWN_FILE_TYPE RUN: not llvm-nm %s 2>&1 | FileCheck %s -check-prefix=UNKNOWN_FILE_TYPE
UNKNOWN_FILE_TYPE: {{.*}}invalid-input.test: The file was not recognized as a valid object file UNKNOWN_FILE_TYPE: {{.*}}invalid-input.test The file was not recognized as a valid object file

View File

@ -1,2 +1,2 @@
RUN: not llvm-size %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s RUN: not llvm-size %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s
ENOENT: {{.*}}llvm-size{{(\.EXE|\.exe)?}}: error reading file: {{[Nn]}}o such file or directory ENOENT: {{.*}}llvm-size{{(\.EXE|\.exe)?}}: {{.*}}basic.test.tmp.blah {{[Nn]}}o such file or directory

View File

@ -1085,7 +1085,7 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary( Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context); BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
if (!BinaryOrErr) { if (!BinaryOrErr) {
error(errorToErrorCode(BinaryOrErr.takeError()), Filename); error(BinaryOrErr.takeError(), Filename);
return; return;
} }
Binary &Bin = *BinaryOrErr.get(); Binary &Bin = *BinaryOrErr.get();

View File

@ -521,7 +521,7 @@ static void printFileSectionSizes(StringRef file) {
// Attempt to open the binary. // Attempt to open the binary.
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file); Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
if (!BinaryOrErr) { if (!BinaryOrErr) {
error(errorToErrorCode(BinaryOrErr.takeError())); error(BinaryOrErr.takeError(), file);
return; return;
} }
Binary &Bin = *BinaryOrErr.get().getBinary(); Binary &Bin = *BinaryOrErr.get().getBinary();