llvm.org GIT mirror llvm / 0546208
Change all but the last ErrorOr<...> use for MachOUniversalBinary to Expected<...> to allow a good error message to be produced. I added the one test case that the object file tools could produce an error message. The other two errors can’t be triggered if the input file is passed through sys::fs::identify_magic(). But the malformedError("bad magic number") does get triggered by the logic in llvm-dsymutil when dealing with a normal Mach-O file. The other "File too small ..." error would take a logic error currently to produce and is not tested for. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273946 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 3 years ago
6 changed file(s) with 32 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
127127 }
128128 };
129129
130 MachOUniversalBinary(MemoryBufferRef Souce, std::error_code &EC);
131 static ErrorOr>
130 MachOUniversalBinary(MemoryBufferRef Souce, Error &Err);
131 static Expected>
132132 create(MemoryBufferRef Source);
133133
134134 object_iterator begin_objects() const {
6464 case sys::fs::file_magic::bitcode:
6565 return ObjectFile::createSymbolicFile(Buffer, Type, Context);
6666 case sys::fs::file_magic::macho_universal_binary:
67 return errorOrToExpected(MachOUniversalBinary::create(Buffer));
67 return MachOUniversalBinary::create(Buffer);
6868 case sys::fs::file_magic::unknown:
6969 case sys::fs::file_magic::windows_resource:
7070 // Unrecognized object file format.
2020
2121 using namespace llvm;
2222 using namespace object;
23
24 static Error
25 malformedError(Twine Msg) {
26 std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
27 return make_error(std::move(StringMsg),
28 object_error::parse_failed);
29 }
2330
2431 template
2532 static T getUniversalBinaryStruct(const char *Ptr) {
9198
9299 void MachOUniversalBinary::anchor() { }
93100
94 ErrorOr>
101 Expected>
95102 MachOUniversalBinary::create(MemoryBufferRef Source) {
96 std::error_code EC;
103 Error Err;
97104 std::unique_ptr Ret(
98 new MachOUniversalBinary(Source, EC));
99 if (EC)
100 return EC;
105 new MachOUniversalBinary(Source, Err));
106 if (Err)
107 return std::move(Err);
101108 return std::move(Ret);
102109 }
103110
104 MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source,
105 std::error_code &ec)
111 MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
106112 : Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
107113 NumberOfObjects(0) {
114 ErrorAsOutParameter ErrAsOutParam(Err);
108115 if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
109 ec = object_error::invalid_file_type;
116 Err = make_error("File too small to be a Mach-O "
117 "universal file",
118 object_error::invalid_file_type);
110119 return;
111120 }
112121 // Check for magic value and sufficient header size.
120129 else if (Magic == MachO::FAT_MAGIC_64)
121130 MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
122131 else {
123 ec = object_error::parse_failed;
132 Err = malformedError("bad magic number");
124133 return;
125134 }
126135 if (Buf.size() < MinSize) {
127 ec = object_error::parse_failed;
136 Err = malformedError("fat_arch" +
137 Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") +
138 " structs would extend past the end of the file");
128139 return;
129140 }
130 ec = std::error_code();
141 Err = Error::success();
131142 }
132143
133144 Expected>
9393
9494 RUN: not llvm-objdump -macho -private-headers -arch all %p/Inputs/macho-universal-archive-bad2.x86_64.i386 2>&1 | FileCheck -check-prefix INCOMPLETE-SEGMENT-LOADC-FAT-ARCHIVE %s
9595 INCOMPLETE-SEGMENT-LOADC-FAT-ARCHIVE: macho-universal-archive-bad2.x86_64.i386(macho64-invalid-incomplete-segment-load-command) (for architecture x86_64) truncated or malformed object (load commands extend past the end of the file)
96
97 RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat 2>&1 | FileCheck -check-prefix INVALID-FAT %s
98 INVALID-FAT: truncated or malformed fat file (fat_arch_64 structs would extend past the end of the file)
7272
7373 auto ErrOrFat = object::MachOUniversalBinary::create(
7474 CurrentMemoryBuffer->getMemBufferRef());
75 if (ErrOrFat.getError()) {
75 if (!ErrOrFat) {
76 consumeError(ErrOrFat.takeError());
7677 // Not a fat binary must be a standard one. Return a one element vector.
7778 return std::vector{CurrentMemoryBuffer->getMemBufferRef()};
7879 }
144145 std::vector ArchiveBuffers;
145146 auto ErrOrFat = object::MachOUniversalBinary::create(
146147 CurrentMemoryBuffer->getMemBufferRef());
147 if (ErrOrFat.getError()) {
148 if (!ErrOrFat) {
149 consumeError(ErrOrFat.takeError());
148150 // Not a fat binary must be a standard one.
149151 ArchiveBuffers.push_back(CurrentMemoryBuffer->getMemBufferRef());
150152 } else {