Index: cfe/trunk/lib/CodeGen/ObjectFilePCHContainerOperations.cpp =================================================================== --- cfe/trunk/lib/CodeGen/ObjectFilePCHContainerOperations.cpp +++ cfe/trunk/lib/CodeGen/ObjectFilePCHContainerOperations.cpp @@ -325,8 +325,8 @@ if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) { StringRef Buf; Section.getContents(Buf); - return StreamFile.init((const unsigned char *)Buf.begin(), - (const unsigned char *)Buf.end()); + StreamFile = llvm::BitstreamReader(Buf); + return; } } } @@ -334,8 +334,7 @@ if (EIB.convertToErrorCode() == llvm::object::object_error::invalid_file_type) // As a fallback, treat the buffer as a raw AST. - StreamFile.init((const unsigned char *)Buffer.getBufferStart(), - (const unsigned char *)Buffer.getBufferEnd()); + StreamFile = llvm::BitstreamReader(Buffer); else EIB.log(llvm::errs()); }); Index: cfe/trunk/lib/Frontend/PCHContainerOperations.cpp =================================================================== --- cfe/trunk/lib/Frontend/PCHContainerOperations.cpp +++ cfe/trunk/lib/Frontend/PCHContainerOperations.cpp @@ -60,8 +60,7 @@ void RawPCHContainerReader::ExtractPCH( llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const { - StreamFile.init((const unsigned char *)Buffer.getBufferStart(), - (const unsigned char *)Buffer.getBufferEnd()); + StreamFile = llvm::BitstreamReader(Buffer); } PCHContainerOperations::PCHContainerOperations() { Index: cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp =================================================================== --- cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp +++ cfe/trunk/lib/Frontend/SerializedDiagnosticReader.cpp @@ -24,10 +24,7 @@ if (!Buffer) return SDError::CouldNotLoad; - llvm::BitstreamReader StreamFile; - StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(), - (const unsigned char *)(*Buffer)->getBufferEnd()); - + llvm::BitstreamReader StreamFile(**Buffer); llvm::BitstreamCursor Stream(StreamFile); // Sniff for the signature. Index: cfe/trunk/lib/Serialization/ASTReader.cpp =================================================================== --- cfe/trunk/lib/Serialization/ASTReader.cpp +++ cfe/trunk/lib/Serialization/ASTReader.cpp @@ -3802,7 +3802,8 @@ /// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'. static bool startsWithASTFileMagic(BitstreamCursor &Stream) { - return Stream.Read(8) == 'C' && + return Stream.canSkipToPos(4) && + Stream.Read(8) == 'C' && Stream.Read(8) == 'P' && Stream.Read(8) == 'C' && Stream.Read(8) == 'H'; Index: cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp =================================================================== --- cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp +++ cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp @@ -246,8 +246,7 @@ std::unique_ptr Buffer = std::move(BufferOrErr.get()); /// \brief The bitstream reader from which we'll read the AST file. - llvm::BitstreamReader Reader((const unsigned char *)Buffer->getBufferStart(), - (const unsigned char *)Buffer->getBufferEnd()); + llvm::BitstreamReader Reader(*Buffer); /// \brief The main bitstream cursor for the main block. llvm::BitstreamCursor Cursor(Reader); Index: llvm/trunk/include/llvm/Bitcode/BitstreamReader.h =================================================================== --- llvm/trunk/include/llvm/Bitcode/BitstreamReader.h +++ llvm/trunk/include/llvm/Bitcode/BitstreamReader.h @@ -15,13 +15,14 @@ #ifndef LLVM_BITCODE_BITSTREAMREADER_H #define LLVM_BITCODE_BITSTREAMREADER_H +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Bitcode/BitCodes.h" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" -#include "llvm/Support/StreamingMemoryObject.h" +#include "llvm/Support/MemoryBuffer.h" #include #include #include @@ -50,32 +51,25 @@ }; private: - std::unique_ptr BitcodeBytes; + ArrayRef BitcodeBytes; std::vector BlockInfoRecords; /// This is set to true if we don't care about the block/record name /// information in the BlockInfo block. Only llvm-bcanalyzer uses this. - bool IgnoreBlockInfoNames; + bool IgnoreBlockInfoNames = true; public: - BitstreamReader() : IgnoreBlockInfoNames(true) { - } - - BitstreamReader(const unsigned char *Start, const unsigned char *End) - : IgnoreBlockInfoNames(true) { - init(Start, End); - } - - BitstreamReader(std::unique_ptr BitcodeBytes) - : BitcodeBytes(std::move(BitcodeBytes)), IgnoreBlockInfoNames(true) {} - - void init(const unsigned char *Start, const unsigned char *End) { - assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes"); - BitcodeBytes.reset(getNonStreamedMemoryObject(Start, End)); - } + BitstreamReader() = default; + BitstreamReader(ArrayRef BitcodeBytes) + : BitcodeBytes(BitcodeBytes) {} + BitstreamReader(StringRef BitcodeBytes) + : BitcodeBytes(reinterpret_cast(BitcodeBytes.data()), + BitcodeBytes.size()) {} + BitstreamReader(MemoryBufferRef BitcodeBytes) + : BitstreamReader(BitcodeBytes.getBuffer()) {} - MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } + ArrayRef getBitcodeBytes() { return BitcodeBytes; } /// This is called by clients that want block/record name information. void CollectBlockInfoNames() { IgnoreBlockInfoNames = false; } @@ -131,9 +125,6 @@ BitstreamReader *R = nullptr; size_t NextChar = 0; - // The size of the bicode. 0 if we don't know it yet. - size_t Size = 0; - public: /// This is the current data we have pulled from the stream but have not /// returned to the client. This is specifically and intentionally defined to @@ -159,17 +150,11 @@ bool canSkipToPos(size_t pos) const { // pos can be skipped to if it is a valid address or one byte past the end. - return pos == 0 || - R->getBitcodeBytes().isValidAddress(static_cast(pos - 1)); + return pos <= R->getBitcodeBytes().size(); } bool AtEndOfStream() { - if (BitsInCurWord != 0) - return false; - if (Size != 0) - return Size <= NextChar; - fillCurWord(); - return BitsInCurWord == 0; + return BitsInCurWord == 0 && R->getBitcodeBytes().size() <= NextChar; } /// Return the bit # of the bit we are reading. @@ -218,7 +203,7 @@ /// Get a pointer into the bitstream at the specified byte offset. const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) { - return R->getBitcodeBytes().getPointer(ByteNo, NumBytes); + return R->getBitcodeBytes().data() + ByteNo; } /// Get a pointer into the bitstream at the specified bit offset. @@ -230,26 +215,25 @@ } void fillCurWord() { - if (Size != 0 && NextChar >= Size) + ArrayRef Buf = R->getBitcodeBytes(); + if (NextChar >= Buf.size()) report_fatal_error("Unexpected end of file"); // Read the next word from the stream. - uint8_t Array[sizeof(word_t)] = {0}; - - uint64_t BytesRead = - R->getBitcodeBytes().readBytes(Array, sizeof(Array), NextChar); - - // If we run out of data, stop at the end of the stream. - if (BytesRead == 0) { + const uint8_t *NextCharPtr = Buf.data() + NextChar; + unsigned BytesRead; + if (Buf.size() >= NextChar + sizeof(word_t)) { + BytesRead = sizeof(word_t); + CurWord = + support::endian::read( + NextCharPtr); + } else { + // Short read. + BytesRead = Buf.size() - NextChar; CurWord = 0; - BitsInCurWord = 0; - Size = NextChar; - return; + for (unsigned B = 0; B != BytesRead; ++B) + CurWord |= NextCharPtr[B] << (B * 8); } - - CurWord = - support::endian::read( - Array); NextChar += BytesRead; BitsInCurWord = BytesRead * 8; } @@ -278,9 +262,9 @@ fillCurWord(); - // If we run out of data, stop at the end of the stream. + // If we run out of data, abort. if (BitsLeft > BitsInCurWord) - return 0; + report_fatal_error("Unexpected end of file"); word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft)); @@ -346,31 +330,7 @@ } /// Skip to the end of the file. - void skipToEnd() { NextChar = R->getBitcodeBytes().getExtent(); } - - /// Prevent the cursor from reading past a byte boundary. - /// - /// Prevent the cursor from requesting byte reads past \c Limit. This is - /// useful when working with a cursor on a StreamingMemoryObject, when it's - /// desirable to avoid invalidating the result of getPointerToByte(). - /// - /// If \c Limit is on a word boundary, AtEndOfStream() will return true if - /// the cursor position reaches or exceeds \c Limit, regardless of the true - /// number of available bytes. Otherwise, AtEndOfStream() returns true when - /// it reaches or exceeds the next word boundary. - void setArtificialByteLimit(uint64_t Limit) { - assert(getCurrentByteNo() < Limit && "Move cursor before lowering limit"); - - // Round to word boundary. - Limit = alignTo(Limit, sizeof(word_t)); - - // Only change size if the new one is lower. - if (!Size || Size > Limit) - Size = Limit; - } - - /// Return the Size, if known. - uint64_t getSizeIfKnown() const { return Size; } + void skipToEnd() { NextChar = R->getBitcodeBytes().size(); } }; /// When advancing through a bitstream cursor, each advance can discover a few @@ -470,6 +430,9 @@ /// Advance the current bitstream, returning the next entry in the stream. BitstreamEntry advance(unsigned Flags = 0) { while (true) { + if (AtEndOfStream()) + return BitstreamEntry::getError(); + unsigned Code = ReadCode(); if (Code == bitc::END_BLOCK) { // Pop the end of the block unless Flags tells us not to. Index: llvm/trunk/include/llvm/Bitcode/ReaderWriter.h =================================================================== --- llvm/trunk/include/llvm/Bitcode/ReaderWriter.h +++ llvm/trunk/include/llvm/Bitcode/ReaderWriter.h @@ -24,7 +24,6 @@ namespace llvm { class BitstreamWriter; - class DataStreamer; class LLVMContext; class Module; class ModulePass; @@ -47,13 +46,6 @@ LLVMContext &Context, bool ShouldLazyLoadMetadata = false); - /// Read the header of the specified stream and prepare for lazy - /// deserialization and streaming of function bodies. - ErrorOr> - getStreamedBitcodeModule(StringRef Name, - std::unique_ptr Streamer, - LLVMContext &Context); - /// Read the header of the specified bitcode buffer and extract just the /// triple information. If successful, this returns a string. On error, this /// returns "". Index: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp @@ -61,14 +61,12 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" -#include "llvm/Support/DataStream.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/StreamingMemoryObject.h" #include #include #include @@ -232,29 +230,19 @@ class BitcodeReaderBase { protected: - BitcodeReaderBase() = default; BitcodeReaderBase(MemoryBuffer *Buffer) : Buffer(Buffer) {} std::unique_ptr Buffer; std::unique_ptr StreamFile; BitstreamCursor Stream; - std::error_code initStream(std::unique_ptr Streamer); - std::error_code initStreamFromBuffer(); - std::error_code initLazyStream(std::unique_ptr Streamer); + std::error_code initStream(); virtual std::error_code error(const Twine &Message) = 0; virtual ~BitcodeReaderBase() = default; }; -std::error_code -BitcodeReaderBase::initStream(std::unique_ptr Streamer) { - if (Streamer) - return initLazyStream(std::move(Streamer)); - return initStreamFromBuffer(); -} - -std::error_code BitcodeReaderBase::initStreamFromBuffer() { +std::error_code BitcodeReaderBase::initStream() { const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart(); const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); @@ -267,39 +255,12 @@ if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) return error("Invalid bitcode wrapper header"); - StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); + StreamFile.reset(new BitstreamReader(ArrayRef(BufPtr, BufEnd))); Stream.init(&*StreamFile); return std::error_code(); } -std::error_code -BitcodeReaderBase::initLazyStream(std::unique_ptr Streamer) { - // Check and strip off the bitcode wrapper; BitstreamReader expects never to - // see it. - auto OwnedBytes = - llvm::make_unique(std::move(Streamer)); - StreamingMemoryObject &Bytes = *OwnedBytes; - StreamFile = llvm::make_unique(std::move(OwnedBytes)); - Stream.init(&*StreamFile); - - unsigned char buf[16]; - if (Bytes.readBytes(buf, 16, 0) != 16) - return error("Invalid bitcode signature"); - - if (!isBitcode(buf, buf + 16)) - return error("Invalid bitcode signature"); - - if (isBitcodeWrapper(buf, buf + 4)) { - const unsigned char *bitcodeStart = buf; - const unsigned char *bitcodeEnd = buf + 16; - SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); - Bytes.dropLeadingBytes(bitcodeStart - buf); - Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart); - } - return std::error_code(); -} - class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { LLVMContext &Context; Module *TheModule = nullptr; @@ -399,7 +360,6 @@ std::error_code error(const Twine &Message) override; BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context); - BitcodeReader(LLVMContext &Context); ~BitcodeReader() override { freeState(); } std::error_code materializeForwardReferencedFunctions(); @@ -414,8 +374,7 @@ /// \brief Main interface to parsing a bitcode buffer. /// \returns true if an error occurred. - std::error_code parseBitcodeInto(std::unique_ptr Streamer, - Module *M, + std::error_code parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false); /// \brief Cheap mechanism to just extract module triple @@ -638,8 +597,7 @@ /// \brief Main interface to parsing a bitcode buffer. /// \returns true if an error occurred. - std::error_code parseSummaryIndexInto(std::unique_ptr Streamer, - ModuleSummaryIndex *I); + std::error_code parseSummaryIndexInto(ModuleSummaryIndex *I); private: std::error_code parseModule(); @@ -706,9 +664,6 @@ : BitcodeReaderBase(Buffer), Context(Context), ValueList(Context), MetadataList(Context) {} -BitcodeReader::BitcodeReader(LLVMContext &Context) - : Context(Context), ValueList(Context), MetadataList(Context) {} - std::error_code BitcodeReader::materializeForwardReferencedFunctions() { if (WillMaterializeAllForwardRefs) return std::error_code(); @@ -2166,10 +2121,6 @@ SimpleBitstreamCursor R(*StreamFile); R.jumpToPointer(Lengths.begin()); - // Ensure that Blob doesn't get invalidated, even if this is reading from - // a StreamingMemoryObject with corrupt data. - R.setArtificialByteLimit(R.getCurrentByteNo() + StringsOffset); - StringRef Strings = Blob.drop_front(StringsOffset); do { if (R.AtEndOfStream()) @@ -4203,12 +4154,11 @@ return true; } -std::error_code -BitcodeReader::parseBitcodeInto(std::unique_ptr Streamer, - Module *M, bool ShouldLazyLoadMetadata) { +std::error_code BitcodeReader::parseBitcodeInto(Module *M, + bool ShouldLazyLoadMetadata) { TheModule = M; - if (std::error_code EC = initStream(std::move(Streamer))) + if (std::error_code EC = initStream()) return EC; // Sniff for the signature. @@ -4282,7 +4232,7 @@ } ErrorOr BitcodeReader::parseTriple() { - if (std::error_code EC = initStream(nullptr)) + if (std::error_code EC = initStream()) return EC; // Sniff for the signature. @@ -4317,7 +4267,7 @@ } ErrorOr BitcodeReader::parseIdentificationBlock() { - if (std::error_code EC = initStream(nullptr)) + if (std::error_code EC = initStream()) return EC; // Sniff for the signature. @@ -4367,7 +4317,7 @@ } ErrorOr BitcodeReader::hasObjCCategory() { - if (std::error_code EC = initStream(nullptr)) + if (std::error_code EC = initStream()) return EC; // Sniff for the signature. @@ -5955,7 +5905,8 @@ ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler, bool CheckGlobalValSummaryPresenceOnly) - : BitcodeReaderBase(Buffer), DiagnosticHandler(std::move(DiagnosticHandler)), + : BitcodeReaderBase(Buffer), + DiagnosticHandler(std::move(DiagnosticHandler)), CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {} void ModuleSummaryIndexBitcodeReader::freeState() { Buffer = nullptr; } @@ -6555,11 +6506,11 @@ } // Parse the function info index from the bitcode streamer into the given index. -std::error_code ModuleSummaryIndexBitcodeReader::parseSummaryIndexInto( - std::unique_ptr Streamer, ModuleSummaryIndex *I) { +std::error_code +ModuleSummaryIndexBitcodeReader::parseSummaryIndexInto(ModuleSummaryIndex *I) { TheIndex = I; - if (std::error_code EC = initStream(std::move(Streamer))) + if (std::error_code EC = initStream()) return EC; // Sniff for the signature. @@ -6624,8 +6575,7 @@ //===----------------------------------------------------------------------===// static ErrorOr> -getBitcodeModuleImpl(std::unique_ptr Streamer, StringRef Name, - BitcodeReader *R, LLVMContext &Context, +getBitcodeModuleImpl(StringRef Name, BitcodeReader *R, LLVMContext &Context, bool MaterializeAll, bool ShouldLazyLoadMetadata) { std::unique_ptr M = llvm::make_unique(Name, Context); M->setMaterializer(R); @@ -6636,8 +6586,7 @@ }; // Delay parsing Metadata if ShouldLazyLoadMetadata is true. - if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get(), - ShouldLazyLoadMetadata)) + if (std::error_code EC = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata)) return cleanupOnError(EC); if (MaterializeAll) { @@ -6667,7 +6616,7 @@ BitcodeReader *R = new BitcodeReader(Buffer.get(), Context); ErrorOr> Ret = - getBitcodeModuleImpl(nullptr, Buffer->getBufferIdentifier(), R, Context, + getBitcodeModuleImpl(Buffer->getBufferIdentifier(), R, Context, MaterializeAll, ShouldLazyLoadMetadata); if (!Ret) return Ret; @@ -6683,17 +6632,6 @@ ShouldLazyLoadMetadata); } -ErrorOr> -llvm::getStreamedBitcodeModule(StringRef Name, - std::unique_ptr Streamer, - LLVMContext &Context) { - std::unique_ptr M = llvm::make_unique(Name, Context); - BitcodeReader *R = new BitcodeReader(Context); - - return getBitcodeModuleImpl(std::move(Streamer), Name, R, Context, false, - false); -} - ErrorOr> llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context) { std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false); @@ -6746,7 +6684,7 @@ return EC; }; - if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get())) + if (std::error_code EC = R.parseSummaryIndexInto(Index.get())) return cleanupOnError(EC); Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now. @@ -6765,7 +6703,7 @@ return false; }; - if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr)) + if (std::error_code EC = R.parseSummaryIndexInto(nullptr)) return cleanupOnError(EC); Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now. Index: llvm/trunk/test/Bitcode/Inputs/invalid-unexpected-eof.bc =================================================================== --- llvm/trunk/test/Bitcode/Inputs/invalid-unexpected-eof.bc +++ llvm/trunk/test/Bitcode/Inputs/invalid-unexpected-eof.bc @@ -1 +1 @@ -BC折! 000000 00000000000000 \ No newline at end of file +BC折! 000000 000000000000000 \ No newline at end of file Index: llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp =================================================================== --- llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp +++ llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp @@ -436,10 +436,6 @@ SimpleBitstreamCursor R(Reader); R.jumpToPointer(Lengths.begin()); - // Ensure that Blob doesn't get invalidated, even if this is reading from a - // StreamingMemoryObject with corrupt data. - R.setArtificialByteLimit(R.getCurrentByteNo() + StringsOffset); - StringRef Strings = Blob.drop_front(StringsOffset); do { if (R.AtEndOfStream()) @@ -735,7 +731,7 @@ return ReportError("Invalid bitcode wrapper header"); } - StreamFile = BitstreamReader(BufPtr, EndBufPtr); + StreamFile = BitstreamReader(ArrayRef(BufPtr, EndBufPtr)); Stream = BitstreamCursor(StreamFile); StreamFile.CollectBlockInfoNames(); @@ -814,7 +810,7 @@ if (Dump) outs() << "\n\n"; - uint64_t BufferSizeBits = StreamFile.getBitcodeBytes().getExtent() * CHAR_BIT; + uint64_t BufferSizeBits = StreamFile.getBitcodeBytes().size() * CHAR_BIT; // Print a summary of the read file. outs() << "Summary of " << InputFilename << ":\n"; outs() << " Total size: "; Index: llvm/trunk/tools/llvm-dis/llvm-dis.cpp =================================================================== --- llvm/trunk/tools/llvm-dis/llvm-dis.cpp +++ llvm/trunk/tools/llvm-dis/llvm-dis.cpp @@ -26,7 +26,6 @@ #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/DataStream.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormattedStream.h" @@ -139,34 +138,20 @@ } static Expected> openInputFile(LLVMContext &Context) { - if (MaterializeMetadata) { - ErrorOr> MBOrErr = - MemoryBuffer::getFileOrSTDIN(InputFilename); - if (!MBOrErr) - return errorCodeToError(MBOrErr.getError()); - ErrorOr> MOrErr = - getLazyBitcodeModule(std::move(*MBOrErr), Context, - /*ShouldLazyLoadMetadata=*/true); - if (!MOrErr) - return errorCodeToError(MOrErr.getError()); + ErrorOr> MBOrErr = + MemoryBuffer::getFileOrSTDIN(InputFilename); + if (!MBOrErr) + return errorCodeToError(MBOrErr.getError()); + ErrorOr> MOrErr = + getLazyBitcodeModule(std::move(*MBOrErr), Context, + /*ShouldLazyLoadMetadata=*/true); + if (!MOrErr) + return errorCodeToError(MOrErr.getError()); + if (MaterializeMetadata) (*MOrErr)->materializeMetadata(); - return std::move(*MOrErr); - } else { - std::string ErrorMessage; - std::unique_ptr Streamer = - getDataFileStreamer(InputFilename, &ErrorMessage); - if (!Streamer) - return make_error(ErrorMessage, inconvertibleErrorCode()); - std::string DisplayFilename; - if (InputFilename == "-") - DisplayFilename = ""; - else - DisplayFilename = InputFilename; - ErrorOr> MOrErr = - getStreamedBitcodeModule(DisplayFilename, std::move(Streamer), Context); + else (*MOrErr)->materializeAll(); - return std::move(*MOrErr); - } + return std::move(*MOrErr); } int main(int argc, char **argv) { Index: llvm/trunk/unittests/Bitcode/BitReaderTest.cpp =================================================================== --- llvm/trunk/unittests/Bitcode/BitReaderTest.cpp +++ llvm/trunk/unittests/Bitcode/BitReaderTest.cpp @@ -18,11 +18,9 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" -#include "llvm/Support/DataStream.h" #include "llvm/Support/Debug.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/SourceMgr.h" -#include "llvm/Support/StreamingMemoryObject.h" #include "gtest/gtest.h" using namespace llvm; @@ -62,84 +60,6 @@ return std::move(ModuleOrErr.get()); } -class BufferDataStreamer : public DataStreamer { - std::unique_ptr Buffer; - unsigned Pos = 0; - size_t GetBytes(unsigned char *Out, size_t Len) override { - StringRef Buf = Buffer->getBuffer(); - size_t Left = Buf.size() - Pos; - Len = std::min(Left, Len); - memcpy(Out, Buffer->getBuffer().substr(Pos).data(), Len); - Pos += Len; - return Len; - } - -public: - BufferDataStreamer(std::unique_ptr Buffer) - : Buffer(std::move(Buffer)) {} -}; - -static std::unique_ptr -getStreamedModuleFromAssembly(LLVMContext &Context, SmallString<1024> &Mem, - const char *Assembly) { - writeModuleToBuffer(parseAssembly(Context, Assembly), Mem); - std::unique_ptr Buffer = - MemoryBuffer::getMemBuffer(Mem.str(), "test", false); - auto Streamer = llvm::make_unique(std::move(Buffer)); - ErrorOr> ModuleOrErr = - getStreamedBitcodeModule("test", std::move(Streamer), Context); - return std::move(ModuleOrErr.get()); -} - -// Checks if we correctly detect eof if we try to read N bits when there are not -// enough bits left on the input stream to read N bits, and we are using a data -// streamer. In particular, it checks if we properly set the object size when -// the eof is reached under such conditions. -TEST(BitReaderTest, TestForEofAfterReadFailureOnDataStreamer) { - // Note: Because StreamingMemoryObject does a call to method GetBytes in it's - // constructor, using internal constant kChunkSize, we must fill the input - // with more characters than that amount. - static size_t InputSize = StreamingMemoryObject::kChunkSize + 5; - char *Text = new char[InputSize]; - std::memset(Text, 'a', InputSize); - Text[InputSize - 1] = '\0'; - StringRef Input(Text); - - // Build bitsteam reader using data streamer. - auto MemoryBuf = MemoryBuffer::getMemBuffer(Input); - std::unique_ptr Streamer( - new BufferDataStreamer(std::move(MemoryBuf))); - auto OwnedBytes = - llvm::make_unique(std::move(Streamer)); - auto Reader = llvm::make_unique(std::move(OwnedBytes)); - BitstreamCursor Cursor; - Cursor.init(Reader.get()); - - // Jump to two bytes before end of stream. - Cursor.JumpToBit((InputSize - 4) * CHAR_BIT); - // Try to read 4 bytes when only 2 are present, resulting in error value 0. - const size_t ReadErrorValue = 0; - EXPECT_EQ(ReadErrorValue, Cursor.Read(32)); - // Should be at eof now. - EXPECT_TRUE(Cursor.AtEndOfStream()); - - delete[] Text; -} - -TEST(BitReaderTest, MateralizeForwardRefWithStream) { - SmallString<1024> Mem; - - LLVMContext Context; - std::unique_ptr M = getStreamedModuleFromAssembly( - Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" - "define void @func() {\n" - " unreachable\n" - "bb:\n" - " unreachable\n" - "}\n"); - EXPECT_FALSE(M->getFunction("func")->empty()); -} - // Tests that lazy evaluation can parse functions out of order. TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) { SmallString<1024> Mem; @@ -216,6 +136,7 @@ " unreachable\n" "}\n"); EXPECT_FALSE(verifyModule(*M, &dbgs())); + EXPECT_FALSE(M->getFunction("func")->empty()); } TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) { Index: llvm/trunk/unittests/Bitcode/BitstreamReaderTest.cpp =================================================================== --- llvm/trunk/unittests/Bitcode/BitstreamReaderTest.cpp +++ llvm/trunk/unittests/Bitcode/BitstreamReaderTest.cpp @@ -10,33 +10,17 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/Bitcode/BitstreamReader.h" #include "llvm/Bitcode/BitstreamWriter.h" -#include "llvm/Support/StreamingMemoryObject.h" #include "gtest/gtest.h" using namespace llvm; namespace { -class BufferStreamer : public DataStreamer { - StringRef Buffer; - -public: - BufferStreamer(StringRef Buffer) : Buffer(Buffer) {} - size_t GetBytes(unsigned char *OutBuffer, size_t Length) override { - if (Length >= Buffer.size()) - Length = Buffer.size(); - - std::copy(Buffer.begin(), Buffer.begin() + Length, OutBuffer); - Buffer = Buffer.drop_front(Length); - return Length; - } -}; - TEST(BitstreamReaderTest, AtEndOfStream) { uint8_t Bytes[4] = { 0x00, 0x01, 0x02, 0x03 }; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); + BitstreamReader Reader(Bytes); BitstreamCursor Cursor(Reader); EXPECT_FALSE(Cursor.AtEndOfStream()); @@ -56,7 +40,7 @@ uint8_t Bytes[4] = { 0x00, 0x01, 0x02, 0x03 }; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); + BitstreamReader Reader(Bytes); BitstreamCursor Cursor(Reader); Cursor.JumpToBit(32); @@ -64,8 +48,7 @@ } TEST(BitstreamReaderTest, AtEndOfStreamEmpty) { - uint8_t Dummy = 0xFF; - BitstreamReader Reader(&Dummy, &Dummy); + BitstreamReader Reader(ArrayRef{}); BitstreamCursor Cursor(Reader); EXPECT_TRUE(Cursor.AtEndOfStream()); @@ -73,10 +56,10 @@ TEST(BitstreamReaderTest, getCurrentByteNo) { uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); + BitstreamReader Reader(Bytes); SimpleBitstreamCursor Cursor(Reader); - for (unsigned I = 0, E = 33; I != E; ++I) { + for (unsigned I = 0, E = 32; I != E; ++I) { EXPECT_EQ(I / 8, Cursor.getCurrentByteNo()); (void)Cursor.Read(1); } @@ -85,7 +68,7 @@ TEST(BitstreamReaderTest, getPointerToByte) { uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); + BitstreamReader Reader(Bytes); SimpleBitstreamCursor Cursor(Reader); for (unsigned I = 0, E = 8; I != E; ++I) { @@ -95,7 +78,7 @@ TEST(BitstreamReaderTest, getPointerToBit) { uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); + BitstreamReader Reader(Bytes); SimpleBitstreamCursor Cursor(Reader); for (unsigned I = 0, E = 8; I != E; ++I) { @@ -105,7 +88,7 @@ TEST(BitstreamReaderTest, jumpToPointer) { uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); + BitstreamReader Reader(Bytes); SimpleBitstreamCursor Cursor(Reader); for (unsigned I : {0, 6, 2, 7}) { @@ -114,68 +97,6 @@ } } -TEST(BitstreamReaderTest, setArtificialByteLimit) { - uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); - SimpleBitstreamCursor Cursor(Reader); - - Cursor.setArtificialByteLimit(8); - EXPECT_EQ(8u, Cursor.getSizeIfKnown()); - while (!Cursor.AtEndOfStream()) - (void)Cursor.Read(1); - - EXPECT_EQ(8u, Cursor.getCurrentByteNo()); -} - -TEST(BitstreamReaderTest, setArtificialByteLimitNotWordBoundary) { - uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); - SimpleBitstreamCursor Cursor(Reader); - - Cursor.setArtificialByteLimit(5); - EXPECT_EQ(8u, Cursor.getSizeIfKnown()); - while (!Cursor.AtEndOfStream()) - (void)Cursor.Read(1); - - EXPECT_EQ(8u, Cursor.getCurrentByteNo()); -} - -TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEnd) { - uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); - SimpleBitstreamCursor Cursor(Reader); - - // The size of the memory object isn't known yet. Set it too high and - // confirm that we don't read too far. - Cursor.setArtificialByteLimit(24); - EXPECT_EQ(24u, Cursor.getSizeIfKnown()); - while (!Cursor.AtEndOfStream()) - (void)Cursor.Read(1); - - EXPECT_EQ(12u, Cursor.getCurrentByteNo()); - EXPECT_EQ(12u, Cursor.getSizeIfKnown()); -} - -TEST(BitstreamReaderTest, setArtificialByteLimitPastTheEndKnown) { - uint8_t Bytes[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b}; - BitstreamReader Reader(std::begin(Bytes), std::end(Bytes)); - SimpleBitstreamCursor Cursor(Reader); - - // Save the size of the memory object in the cursor. - while (!Cursor.AtEndOfStream()) - (void)Cursor.Read(1); - EXPECT_EQ(12u, Cursor.getCurrentByteNo()); - EXPECT_EQ(12u, Cursor.getSizeIfKnown()); - - Cursor.setArtificialByteLimit(20); - EXPECT_TRUE(Cursor.AtEndOfStream()); - EXPECT_EQ(12u, Cursor.getSizeIfKnown()); -} - TEST(BitstreamReaderTest, readRecordWithBlobWhileStreaming) { SmallVector BlobData; for (unsigned I = 0, E = 1024; I != E; ++I) @@ -208,9 +129,8 @@ } // Stream the buffer into the reader. - BitstreamReader R(llvm::make_unique( - llvm::make_unique( - StringRef(Buffer.begin(), Buffer.size())))); + BitstreamReader R( + ArrayRef((const uint8_t *)Buffer.begin(), Buffer.size())); BitstreamCursor Stream(R); // Header. Included in test so that we can run llvm-bcanalyzer to debug