Skip to content

Commit

Permalink
LTOModule::isBitcodeFile() shouldn't assert when returning false.
Browse files Browse the repository at this point in the history
Fixes a bunch of assert-on-invalid-bitcode regressions after 315483.
Expected<> calls assertIsChecked() in its dtor, and operator bool() only calls
setChecked() if there's no error. So for functions that don't return an error
itself, the Expected<> version needs explicit code to disarm the error that the
ErrorOr<> code didn't need.

https://reviews.llvm.org/D39437


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317010 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
nico committed Oct 31, 2017
1 parent 7166565 commit 003140e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/LTO/LTOModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ LTOModule::~LTOModule() {}
bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {
Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));
return bool(BCData);
if (!BCData) {
consumeError(BCData.takeError());
return false;
}
return true;
}

bool LTOModule::isBitcodeFile(StringRef Path) {
Expand All @@ -73,7 +77,11 @@ bool LTOModule::isBitcodeFile(StringRef Path) {

Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
BufferOrErr.get()->getMemBufferRef());
return bool(BCData);
if (!BCData) {
consumeError(BCData.takeError());
return false;
}
return true;
}

bool LTOModule::isThinLTO() {
Expand All @@ -89,8 +97,10 @@ bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
StringRef TriplePrefix) {
Expected<MemoryBufferRef> BCOrErr =
IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
if (!BCOrErr)
if (!BCOrErr) {
consumeError(BCOrErr.takeError());
return false;
}
LLVMContext Context;
ErrorOr<std::string> TripleOrErr =
expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(*BCOrErr));
Expand All @@ -102,8 +112,10 @@ bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
Expected<MemoryBufferRef> BCOrErr =
IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
if (!BCOrErr)
if (!BCOrErr) {
consumeError(BCOrErr.takeError());
return "";
}
LLVMContext Context;
ErrorOr<std::string> ProducerOrErr = expectedToErrorOrAndEmitErrors(
Context, getBitcodeProducerString(*BCOrErr));
Expand Down
4 changes: 4 additions & 0 deletions test/tools/lto/no-bitcode.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
; libLTO.dylib shouldn't assert on invalid inputs.
; RUN: llvm-mc -triple=arm64-apple-ios7.0.0 -filetype=obj -o %t.o
; RUN: llvm-ar r %t.a %t.o
; RUN: %ld64 -lto_library %llvmshlibdir/libLTO.dylib -arch x86_64 -dylib -mllvm -O0 -o %t.dylib %t.a

0 comments on commit 003140e

Please sign in to comment.