From 05c988473f508a1c37ab739898ed95b5456c47cf Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 31 Oct 2017 16:39:47 +0000 Subject: [PATCH] LTOModule::isBitcodeFile() shouldn't assert when returning false. 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 llvm-svn: 317010 --- llvm/lib/LTO/LTOModule.cpp | 20 ++++++++++++++++---- llvm/test/tools/lto/no-bitcode.s | 4 ++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 llvm/test/tools/lto/no-bitcode.s diff --git a/llvm/lib/LTO/LTOModule.cpp b/llvm/lib/LTO/LTOModule.cpp index 6a0fbb6..42a5935 100644 --- a/llvm/lib/LTO/LTOModule.cpp +++ b/llvm/lib/LTO/LTOModule.cpp @@ -62,7 +62,11 @@ LTOModule::~LTOModule() {} bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) { Expected BCData = IRObjectFile::findBitcodeInMemBuffer( MemoryBufferRef(StringRef((const char *)Mem, Length), "")); - return bool(BCData); + if (!BCData) { + consumeError(BCData.takeError()); + return false; + } + return true; } bool LTOModule::isBitcodeFile(StringRef Path) { @@ -73,7 +77,11 @@ bool LTOModule::isBitcodeFile(StringRef Path) { Expected BCData = IRObjectFile::findBitcodeInMemBuffer( BufferOrErr.get()->getMemBufferRef()); - return bool(BCData); + if (!BCData) { + consumeError(BCData.takeError()); + return false; + } + return true; } bool LTOModule::isThinLTO() { @@ -89,8 +97,10 @@ bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer, StringRef TriplePrefix) { Expected BCOrErr = IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef()); - if (!BCOrErr) + if (!BCOrErr) { + consumeError(BCOrErr.takeError()); return false; + } LLVMContext Context; ErrorOr TripleOrErr = expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(*BCOrErr)); @@ -102,8 +112,10 @@ bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer, std::string LTOModule::getProducerString(MemoryBuffer *Buffer) { Expected BCOrErr = IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef()); - if (!BCOrErr) + if (!BCOrErr) { + consumeError(BCOrErr.takeError()); return ""; + } LLVMContext Context; ErrorOr ProducerOrErr = expectedToErrorOrAndEmitErrors( Context, getBitcodeProducerString(*BCOrErr)); diff --git a/llvm/test/tools/lto/no-bitcode.s b/llvm/test/tools/lto/no-bitcode.s new file mode 100644 index 0000000..360ebf1 --- /dev/null +++ b/llvm/test/tools/lto/no-bitcode.s @@ -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 -- 2.7.4