BitstreamReader.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This header defines the BitstreamReader class. This class can be used to
  10. // read an arbitrary bitstream, regardless of its contents.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_BITSTREAM_BITSTREAMREADER_H
  14. #define LLVM_BITSTREAM_BITSTREAMREADER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Bitstream/BitCodes.h"
  18. #include "llvm/Support/Endian.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/MathExtras.h"
  22. #include "llvm/Support/MemoryBuffer.h"
  23. #include <algorithm>
  24. #include <cassert>
  25. #include <climits>
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <memory>
  29. #include <string>
  30. #include <utility>
  31. #include <vector>
  32. namespace llvm {
  33. /// This class maintains the abbreviations read from a block info block.
  34. class BitstreamBlockInfo {
  35. public:
  36. /// This contains information emitted to BLOCKINFO_BLOCK blocks. These
  37. /// describe abbreviations that all blocks of the specified ID inherit.
  38. struct BlockInfo {
  39. unsigned BlockID = 0;
  40. std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
  41. std::string Name;
  42. std::vector<std::pair<unsigned, std::string>> RecordNames;
  43. };
  44. private:
  45. std::vector<BlockInfo> BlockInfoRecords;
  46. public:
  47. /// If there is block info for the specified ID, return it, otherwise return
  48. /// null.
  49. const BlockInfo *getBlockInfo(unsigned BlockID) const {
  50. // Common case, the most recent entry matches BlockID.
  51. if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
  52. return &BlockInfoRecords.back();
  53. for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
  54. i != e; ++i)
  55. if (BlockInfoRecords[i].BlockID == BlockID)
  56. return &BlockInfoRecords[i];
  57. return nullptr;
  58. }
  59. BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
  60. if (const BlockInfo *BI = getBlockInfo(BlockID))
  61. return *const_cast<BlockInfo*>(BI);
  62. // Otherwise, add a new record.
  63. BlockInfoRecords.emplace_back();
  64. BlockInfoRecords.back().BlockID = BlockID;
  65. return BlockInfoRecords.back();
  66. }
  67. };
  68. /// This represents a position within a bitstream. There may be multiple
  69. /// independent cursors reading within one bitstream, each maintaining their
  70. /// own local state.
  71. class SimpleBitstreamCursor {
  72. ArrayRef<uint8_t> BitcodeBytes;
  73. size_t NextChar = 0;
  74. public:
  75. /// This is the current data we have pulled from the stream but have not
  76. /// returned to the client. This is specifically and intentionally defined to
  77. /// follow the word size of the host machine for efficiency. We use word_t in
  78. /// places that are aware of this to make it perfectly explicit what is going
  79. /// on.
  80. using word_t = size_t;
  81. private:
  82. word_t CurWord = 0;
  83. /// This is the number of bits in CurWord that are valid. This is always from
  84. /// [0...bits_of(size_t)-1] inclusive.
  85. unsigned BitsInCurWord = 0;
  86. public:
  87. static const constexpr size_t MaxChunkSize = sizeof(word_t) * 8;
  88. SimpleBitstreamCursor() = default;
  89. explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
  90. : BitcodeBytes(BitcodeBytes) {}
  91. explicit SimpleBitstreamCursor(StringRef BitcodeBytes)
  92. : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {}
  93. explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
  94. : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {}
  95. bool canSkipToPos(size_t pos) const {
  96. // pos can be skipped to if it is a valid address or one byte past the end.
  97. return pos <= BitcodeBytes.size();
  98. }
  99. bool AtEndOfStream() {
  100. return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar;
  101. }
  102. /// Return the bit # of the bit we are reading.
  103. uint64_t GetCurrentBitNo() const {
  104. return NextChar*CHAR_BIT - BitsInCurWord;
  105. }
  106. // Return the byte # of the current bit.
  107. uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; }
  108. ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; }
  109. /// Reset the stream to the specified bit number.
  110. Error JumpToBit(uint64_t BitNo) {
  111. size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1);
  112. unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
  113. assert(canSkipToPos(ByteNo) && "Invalid location");
  114. // Move the cursor to the right word.
  115. NextChar = ByteNo;
  116. BitsInCurWord = 0;
  117. // Skip over any bits that are already consumed.
  118. if (WordBitNo) {
  119. if (Expected<word_t> Res = Read(WordBitNo))
  120. return Error::success();
  121. else
  122. return Res.takeError();
  123. }
  124. return Error::success();
  125. }
  126. /// Get a pointer into the bitstream at the specified byte offset.
  127. const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) {
  128. return BitcodeBytes.data() + ByteNo;
  129. }
  130. /// Get a pointer into the bitstream at the specified bit offset.
  131. ///
  132. /// The bit offset must be on a byte boundary.
  133. const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) {
  134. assert(!(BitNo % 8) && "Expected bit on byte boundary");
  135. return getPointerToByte(BitNo / 8, NumBytes);
  136. }
  137. Error fillCurWord() {
  138. if (NextChar >= BitcodeBytes.size())
  139. return createStringError(std::errc::io_error,
  140. "Unexpected end of file reading %u of %u bytes",
  141. NextChar, BitcodeBytes.size());
  142. // Read the next word from the stream.
  143. const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar;
  144. unsigned BytesRead;
  145. if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) {
  146. BytesRead = sizeof(word_t);
  147. CurWord =
  148. support::endian::read<word_t, support::little, support::unaligned>(
  149. NextCharPtr);
  150. } else {
  151. // Short read.
  152. BytesRead = BitcodeBytes.size() - NextChar;
  153. CurWord = 0;
  154. for (unsigned B = 0; B != BytesRead; ++B)
  155. CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
  156. }
  157. NextChar += BytesRead;
  158. BitsInCurWord = BytesRead * 8;
  159. return Error::success();
  160. }
  161. Expected<word_t> Read(unsigned NumBits) {
  162. static const unsigned BitsInWord = MaxChunkSize;
  163. assert(NumBits && NumBits <= BitsInWord &&
  164. "Cannot return zero or more than BitsInWord bits!");
  165. static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
  166. // If the field is fully contained by CurWord, return it quickly.
  167. if (BitsInCurWord >= NumBits) {
  168. word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
  169. // Use a mask to avoid undefined behavior.
  170. CurWord >>= (NumBits & Mask);
  171. BitsInCurWord -= NumBits;
  172. return R;
  173. }
  174. word_t R = BitsInCurWord ? CurWord : 0;
  175. unsigned BitsLeft = NumBits - BitsInCurWord;
  176. if (Error fillResult = fillCurWord())
  177. return std::move(fillResult);
  178. // If we run out of data, abort.
  179. if (BitsLeft > BitsInCurWord)
  180. return createStringError(std::errc::io_error,
  181. "Unexpected end of file reading %u of %u bits",
  182. BitsInCurWord, BitsLeft);
  183. word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
  184. // Use a mask to avoid undefined behavior.
  185. CurWord >>= (BitsLeft & Mask);
  186. BitsInCurWord -= BitsLeft;
  187. R |= R2 << (NumBits - BitsLeft);
  188. return R;
  189. }
  190. Expected<uint32_t> ReadVBR(unsigned NumBits) {
  191. Expected<unsigned> MaybeRead = Read(NumBits);
  192. if (!MaybeRead)
  193. return MaybeRead;
  194. uint32_t Piece = MaybeRead.get();
  195. if ((Piece & (1U << (NumBits-1))) == 0)
  196. return Piece;
  197. uint32_t Result = 0;
  198. unsigned NextBit = 0;
  199. while (true) {
  200. Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
  201. if ((Piece & (1U << (NumBits-1))) == 0)
  202. return Result;
  203. NextBit += NumBits-1;
  204. MaybeRead = Read(NumBits);
  205. if (!MaybeRead)
  206. return MaybeRead;
  207. Piece = MaybeRead.get();
  208. }
  209. }
  210. // Read a VBR that may have a value up to 64-bits in size. The chunk size of
  211. // the VBR must still be <= 32 bits though.
  212. Expected<uint64_t> ReadVBR64(unsigned NumBits) {
  213. Expected<uint64_t> MaybeRead = Read(NumBits);
  214. if (!MaybeRead)
  215. return MaybeRead;
  216. uint32_t Piece = MaybeRead.get();
  217. if ((Piece & (1U << (NumBits-1))) == 0)
  218. return uint64_t(Piece);
  219. uint64_t Result = 0;
  220. unsigned NextBit = 0;
  221. while (true) {
  222. Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
  223. if ((Piece & (1U << (NumBits-1))) == 0)
  224. return Result;
  225. NextBit += NumBits-1;
  226. MaybeRead = Read(NumBits);
  227. if (!MaybeRead)
  228. return MaybeRead;
  229. Piece = MaybeRead.get();
  230. }
  231. }
  232. void SkipToFourByteBoundary() {
  233. // If word_t is 64-bits and if we've read less than 32 bits, just dump
  234. // the bits we have up to the next 32-bit boundary.
  235. if (sizeof(word_t) > 4 &&
  236. BitsInCurWord >= 32) {
  237. CurWord >>= BitsInCurWord-32;
  238. BitsInCurWord = 32;
  239. return;
  240. }
  241. BitsInCurWord = 0;
  242. }
  243. /// Return the size of the stream in bytes.
  244. size_t SizeInBytes() const { return BitcodeBytes.size(); }
  245. /// Skip to the end of the file.
  246. void skipToEnd() { NextChar = BitcodeBytes.size(); }
  247. };
  248. /// When advancing through a bitstream cursor, each advance can discover a few
  249. /// different kinds of entries:
  250. struct BitstreamEntry {
  251. enum {
  252. Error, // Malformed bitcode was found.
  253. EndBlock, // We've reached the end of the current block, (or the end of the
  254. // file, which is treated like a series of EndBlock records.
  255. SubBlock, // This is the start of a new subblock of a specific ID.
  256. Record // This is a record with a specific AbbrevID.
  257. } Kind;
  258. unsigned ID;
  259. static BitstreamEntry getError() {
  260. BitstreamEntry E; E.Kind = Error; return E;
  261. }
  262. static BitstreamEntry getEndBlock() {
  263. BitstreamEntry E; E.Kind = EndBlock; return E;
  264. }
  265. static BitstreamEntry getSubBlock(unsigned ID) {
  266. BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E;
  267. }
  268. static BitstreamEntry getRecord(unsigned AbbrevID) {
  269. BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E;
  270. }
  271. };
  272. /// This represents a position within a bitcode file, implemented on top of a
  273. /// SimpleBitstreamCursor.
  274. ///
  275. /// Unlike iterators, BitstreamCursors are heavy-weight objects that should not
  276. /// be passed by value.
  277. class BitstreamCursor : SimpleBitstreamCursor {
  278. // This is the declared size of code values used for the current block, in
  279. // bits.
  280. unsigned CurCodeSize = 2;
  281. /// Abbrevs installed at in this block.
  282. std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
  283. struct Block {
  284. unsigned PrevCodeSize;
  285. std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
  286. explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
  287. };
  288. /// This tracks the codesize of parent blocks.
  289. SmallVector<Block, 8> BlockScope;
  290. BitstreamBlockInfo *BlockInfo = nullptr;
  291. public:
  292. static const size_t MaxChunkSize = sizeof(word_t) * 8;
  293. BitstreamCursor() = default;
  294. explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
  295. : SimpleBitstreamCursor(BitcodeBytes) {}
  296. explicit BitstreamCursor(StringRef BitcodeBytes)
  297. : SimpleBitstreamCursor(BitcodeBytes) {}
  298. explicit BitstreamCursor(MemoryBufferRef BitcodeBytes)
  299. : SimpleBitstreamCursor(BitcodeBytes) {}
  300. using SimpleBitstreamCursor::AtEndOfStream;
  301. using SimpleBitstreamCursor::canSkipToPos;
  302. using SimpleBitstreamCursor::fillCurWord;
  303. using SimpleBitstreamCursor::getBitcodeBytes;
  304. using SimpleBitstreamCursor::GetCurrentBitNo;
  305. using SimpleBitstreamCursor::getCurrentByteNo;
  306. using SimpleBitstreamCursor::getPointerToByte;
  307. using SimpleBitstreamCursor::JumpToBit;
  308. using SimpleBitstreamCursor::Read;
  309. using SimpleBitstreamCursor::ReadVBR;
  310. using SimpleBitstreamCursor::ReadVBR64;
  311. using SimpleBitstreamCursor::SizeInBytes;
  312. using SimpleBitstreamCursor::skipToEnd;
  313. /// Return the number of bits used to encode an abbrev #.
  314. unsigned getAbbrevIDWidth() const { return CurCodeSize; }
  315. /// Flags that modify the behavior of advance().
  316. enum {
  317. /// If this flag is used, the advance() method does not automatically pop
  318. /// the block scope when the end of a block is reached.
  319. AF_DontPopBlockAtEnd = 1,
  320. /// If this flag is used, abbrev entries are returned just like normal
  321. /// records.
  322. AF_DontAutoprocessAbbrevs = 2
  323. };
  324. /// Advance the current bitstream, returning the next entry in the stream.
  325. Expected<BitstreamEntry> advance(unsigned Flags = 0) {
  326. while (true) {
  327. if (AtEndOfStream())
  328. return BitstreamEntry::getError();
  329. Expected<unsigned> MaybeCode = ReadCode();
  330. if (!MaybeCode)
  331. return MaybeCode.takeError();
  332. unsigned Code = MaybeCode.get();
  333. if (Code == bitc::END_BLOCK) {
  334. // Pop the end of the block unless Flags tells us not to.
  335. if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
  336. return BitstreamEntry::getError();
  337. return BitstreamEntry::getEndBlock();
  338. }
  339. if (Code == bitc::ENTER_SUBBLOCK) {
  340. if (Expected<unsigned> MaybeSubBlock = ReadSubBlockID())
  341. return BitstreamEntry::getSubBlock(MaybeSubBlock.get());
  342. else
  343. return MaybeSubBlock.takeError();
  344. }
  345. if (Code == bitc::DEFINE_ABBREV &&
  346. !(Flags & AF_DontAutoprocessAbbrevs)) {
  347. // We read and accumulate abbrev's, the client can't do anything with
  348. // them anyway.
  349. if (Error Err = ReadAbbrevRecord())
  350. return std::move(Err);
  351. continue;
  352. }
  353. return BitstreamEntry::getRecord(Code);
  354. }
  355. }
  356. /// This is a convenience function for clients that don't expect any
  357. /// subblocks. This just skips over them automatically.
  358. Expected<BitstreamEntry> advanceSkippingSubblocks(unsigned Flags = 0) {
  359. while (true) {
  360. // If we found a normal entry, return it.
  361. Expected<BitstreamEntry> MaybeEntry = advance(Flags);
  362. if (!MaybeEntry)
  363. return MaybeEntry;
  364. BitstreamEntry Entry = MaybeEntry.get();
  365. if (Entry.Kind != BitstreamEntry::SubBlock)
  366. return Entry;
  367. // If we found a sub-block, just skip over it and check the next entry.
  368. if (Error Err = SkipBlock())
  369. return std::move(Err);
  370. }
  371. }
  372. Expected<unsigned> ReadCode() { return Read(CurCodeSize); }
  373. // Block header:
  374. // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
  375. /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
  376. Expected<unsigned> ReadSubBlockID() { return ReadVBR(bitc::BlockIDWidth); }
  377. /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body
  378. /// of this block.
  379. Error SkipBlock() {
  380. // Read and ignore the codelen value.
  381. if (Expected<uint32_t> Res = ReadVBR(bitc::CodeLenWidth))
  382. ; // Since we are skipping this block, we don't care what code widths are
  383. // used inside of it.
  384. else
  385. return Res.takeError();
  386. SkipToFourByteBoundary();
  387. Expected<unsigned> MaybeNum = Read(bitc::BlockSizeWidth);
  388. if (!MaybeNum)
  389. return MaybeNum.takeError();
  390. size_t NumFourBytes = MaybeNum.get();
  391. // Check that the block wasn't partially defined, and that the offset isn't
  392. // bogus.
  393. size_t SkipTo = GetCurrentBitNo() + NumFourBytes * 4 * 8;
  394. if (AtEndOfStream())
  395. return createStringError(std::errc::illegal_byte_sequence,
  396. "can't skip block: already at end of stream");
  397. if (!canSkipToPos(SkipTo / 8))
  398. return createStringError(std::errc::illegal_byte_sequence,
  399. "can't skip to bit %zu from %" PRIu64, SkipTo,
  400. GetCurrentBitNo());
  401. if (Error Res = JumpToBit(SkipTo))
  402. return Res;
  403. return Error::success();
  404. }
  405. /// Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
  406. Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr);
  407. bool ReadBlockEnd() {
  408. if (BlockScope.empty()) return true;
  409. // Block tail:
  410. // [END_BLOCK, <align4bytes>]
  411. SkipToFourByteBoundary();
  412. popBlockScope();
  413. return false;
  414. }
  415. private:
  416. void popBlockScope() {
  417. CurCodeSize = BlockScope.back().PrevCodeSize;
  418. CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
  419. BlockScope.pop_back();
  420. }
  421. //===--------------------------------------------------------------------===//
  422. // Record Processing
  423. //===--------------------------------------------------------------------===//
  424. public:
  425. /// Return the abbreviation for the specified AbbrevId.
  426. const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
  427. unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
  428. if (AbbrevNo >= CurAbbrevs.size())
  429. report_fatal_error("Invalid abbrev number");
  430. return CurAbbrevs[AbbrevNo].get();
  431. }
  432. /// Read the current record and discard it, returning the code for the record.
  433. Expected<unsigned> skipRecord(unsigned AbbrevID);
  434. Expected<unsigned> readRecord(unsigned AbbrevID,
  435. SmallVectorImpl<uint64_t> &Vals,
  436. StringRef *Blob = nullptr);
  437. //===--------------------------------------------------------------------===//
  438. // Abbrev Processing
  439. //===--------------------------------------------------------------------===//
  440. Error ReadAbbrevRecord();
  441. /// Read and return a block info block from the bitstream. If an error was
  442. /// encountered, return None.
  443. ///
  444. /// \param ReadBlockInfoNames Whether to read block/record name information in
  445. /// the BlockInfo block. Only llvm-bcanalyzer uses this.
  446. Expected<Optional<BitstreamBlockInfo>>
  447. ReadBlockInfoBlock(bool ReadBlockInfoNames = false);
  448. /// Set the block info to be used by this BitstreamCursor to interpret
  449. /// abbreviated records.
  450. void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; }
  451. };
  452. } // end llvm namespace
  453. #endif // LLVM_BITSTREAM_BITSTREAMREADER_H