diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 262dbe12ed48..863081fbd4ce 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -241,6 +241,11 @@ static Error parseSegmentLoadCommand( const char *Sec = getSectionPtr(Obj, Load, J); 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); } else return SegOrErr.takeError(); diff --git a/llvm/test/Object/Inputs/macho-invalid-segment-fileoff b/llvm/test/Object/Inputs/macho-invalid-segment-fileoff new file mode 100644 index 000000000000..7e8ba00002d9 Binary files /dev/null and b/llvm/test/Object/Inputs/macho-invalid-segment-fileoff differ diff --git a/llvm/test/Object/macho-invalid.test b/llvm/test/Object/macho-invalid.test index 9c4e37c65a6f..2236b0b03c18 100644 --- a/llvm/test/Object/macho-invalid.test +++ b/llvm/test/Object/macho-invalid.test @@ -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 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) diff --git a/llvm/test/tools/llvm-nm/invalid-input.test b/llvm/test/tools/llvm-nm/invalid-input.test index ad9195613d40..773306b267d6 100644 --- a/llvm/test/tools/llvm-nm/invalid-input.test +++ b/llvm/test/tools/llvm-nm/invalid-input.test @@ -1,2 +1,2 @@ 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 diff --git a/llvm/test/tools/llvm-size/basic.test b/llvm/test/tools/llvm-size/basic.test index 88b8dcbcbffa..c53d7e0134f0 100644 --- a/llvm/test/tools/llvm-size/basic.test +++ b/llvm/test/tools/llvm-size/basic.test @@ -1,2 +1,2 @@ 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 diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp index af3d7db1dda3..7eb937453ac5 100644 --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -1085,7 +1085,7 @@ static void dumpSymbolNamesFromFile(std::string &Filename) { Expected> BinaryOrErr = createBinary( BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context); if (!BinaryOrErr) { - error(errorToErrorCode(BinaryOrErr.takeError()), Filename); + error(BinaryOrErr.takeError(), Filename); return; } Binary &Bin = *BinaryOrErr.get(); diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp index 4ecfcc2df104..8da0ec677cb0 100644 --- a/llvm/tools/llvm-size/llvm-size.cpp +++ b/llvm/tools/llvm-size/llvm-size.cpp @@ -521,7 +521,7 @@ static void printFileSectionSizes(StringRef file) { // Attempt to open the binary. Expected> BinaryOrErr = createBinary(file); if (!BinaryOrErr) { - error(errorToErrorCode(BinaryOrErr.takeError())); + error(BinaryOrErr.takeError(), file); return; } Binary &Bin = *BinaryOrErr.get().getBinary();