summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Enderby <enderby@apple.com>2016-08-05 18:19:40 +0000
committerKevin Enderby <enderby@apple.com>2016-08-05 18:19:40 +0000
commit600fb3f28eb5e1a3ad5e04118e74cdd65def4aa3 (patch)
tree82243b582e31930d055fd580daeff2a8f3c1f75b
parent0f4f0c5d5378703a7d0156120ddf75e3546e41b1 (diff)
downloadbcm5719-llvm-600fb3f28eb5e1a3ad5e04118e74cdd65def4aa3.tar.gz
bcm5719-llvm-600fb3f28eb5e1a3ad5e04118e74cdd65def4aa3.zip
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
-rw-r--r--llvm/lib/Object/MachOObjectFile.cpp5
-rw-r--r--llvm/test/Object/Inputs/macho-invalid-segment-fileoffbin0 -> 84 bytes
-rw-r--r--llvm/test/Object/macho-invalid.test9
-rw-r--r--llvm/test/tools/llvm-nm/invalid-input.test2
-rw-r--r--llvm/test/tools/llvm-size/basic.test2
-rw-r--r--llvm/tools/llvm-nm/llvm-nm.cpp2
-rw-r--r--llvm/tools/llvm-size/llvm-size.cpp2
7 files changed, 18 insertions, 4 deletions
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp
index 262dbe12ed4..863081fbd4c 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 00000000000..7e8ba00002d
--- /dev/null
+++ b/llvm/test/Object/Inputs/macho-invalid-segment-fileoff
Binary files differ
diff --git a/llvm/test/Object/macho-invalid.test b/llvm/test/Object/macho-invalid.test
index 9c4e37c65a6..2236b0b03c1 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 ad9195613d4..773306b267d 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 88b8dcbcbff..c53d7e0134f 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 af3d7db1dda..7eb937453ac 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<std::unique_ptr<Binary>> 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 4ecfcc2df10..8da0ec677cb 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<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
if (!BinaryOrErr) {
- error(errorToErrorCode(BinaryOrErr.takeError()));
+ error(BinaryOrErr.takeError(), file);
return;
}
Binary &Bin = *BinaryOrErr.get().getBinary();
OpenPOWER on IntegriCloud