CodeViewRecordIO.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //===- CodeViewRecordIO.cpp -------------------------------------*- 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. #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
  9. #include "llvm/DebugInfo/CodeView/CodeView.h"
  10. #include "llvm/DebugInfo/CodeView/GUID.h"
  11. #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
  12. #include "llvm/DebugInfo/CodeView/TypeIndex.h"
  13. #include "llvm/Support/BinaryStreamReader.h"
  14. #include "llvm/Support/BinaryStreamWriter.h"
  15. using namespace llvm;
  16. using namespace llvm::codeview;
  17. Error CodeViewRecordIO::beginRecord(std::optional<uint32_t> MaxLength) {
  18. RecordLimit Limit;
  19. Limit.MaxLength = MaxLength;
  20. Limit.BeginOffset = getCurrentOffset();
  21. Limits.push_back(Limit);
  22. return Error::success();
  23. }
  24. Error CodeViewRecordIO::endRecord() {
  25. assert(!Limits.empty() && "Not in a record!");
  26. Limits.pop_back();
  27. // We would like to assert that we actually read / wrote all the bytes that we
  28. // expected to for this record, but unfortunately we can't do this. Some
  29. // producers such as MASM over-allocate for certain types of records and
  30. // commit the extraneous data, so when reading we can't be sure every byte
  31. // will have been read. And when writing we over-allocate temporarily since
  32. // we don't know how big the record is until we're finished writing it, so
  33. // even though we don't commit the extraneous data, we still can't guarantee
  34. // we're at the end of the allocated data.
  35. if (isStreaming()) {
  36. // For streaming mode, add padding to align with 4 byte boundaries for each
  37. // record
  38. uint32_t Align = getStreamedLen() % 4;
  39. if (Align == 0)
  40. return Error::success();
  41. int PaddingBytes = 4 - Align;
  42. while (PaddingBytes > 0) {
  43. char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
  44. StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
  45. Streamer->emitBytes(BytesSR);
  46. --PaddingBytes;
  47. }
  48. resetStreamedLen();
  49. }
  50. return Error::success();
  51. }
  52. uint32_t CodeViewRecordIO::maxFieldLength() const {
  53. if (isStreaming())
  54. return 0;
  55. assert(!Limits.empty() && "Not in a record!");
  56. // The max length of the next field is the minimum of all lengths that would
  57. // be allowed by any of the sub-records we're in. In practice, we can only
  58. // ever be at most 1 sub-record deep (in a FieldList), but this works for
  59. // the general case.
  60. uint32_t Offset = getCurrentOffset();
  61. std::optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
  62. for (auto X : ArrayRef(Limits).drop_front()) {
  63. std::optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
  64. if (ThisMin)
  65. Min = Min ? std::min(*Min, *ThisMin) : *ThisMin;
  66. }
  67. assert(Min && "Every field must have a maximum length!");
  68. return *Min;
  69. }
  70. Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
  71. if (isReading())
  72. return Reader->padToAlignment(Align);
  73. return Writer->padToAlignment(Align);
  74. }
  75. Error CodeViewRecordIO::skipPadding() {
  76. assert(!isWriting() && "Cannot skip padding while writing!");
  77. if (Reader->bytesRemaining() == 0)
  78. return Error::success();
  79. uint8_t Leaf = Reader->peek();
  80. if (Leaf < LF_PAD0)
  81. return Error::success();
  82. // Leaf is greater than 0xf0. We should advance by the number of bytes in
  83. // the low 4 bits.
  84. unsigned BytesToAdvance = Leaf & 0x0F;
  85. return Reader->skip(BytesToAdvance);
  86. }
  87. Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes,
  88. const Twine &Comment) {
  89. if (isStreaming()) {
  90. emitComment(Comment);
  91. Streamer->emitBinaryData(toStringRef(Bytes));
  92. incrStreamedLen(Bytes.size());
  93. } else if (isWriting()) {
  94. if (auto EC = Writer->writeBytes(Bytes))
  95. return EC;
  96. } else {
  97. if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
  98. return EC;
  99. }
  100. return Error::success();
  101. }
  102. Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
  103. const Twine &Comment) {
  104. ArrayRef<uint8_t> BytesRef(Bytes);
  105. if (auto EC = mapByteVectorTail(BytesRef, Comment))
  106. return EC;
  107. if (!isWriting())
  108. Bytes.assign(BytesRef.begin(), BytesRef.end());
  109. return Error::success();
  110. }
  111. Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd, const Twine &Comment) {
  112. if (isStreaming()) {
  113. std::string TypeNameStr = Streamer->getTypeName(TypeInd);
  114. if (!TypeNameStr.empty())
  115. emitComment(Comment + ": " + TypeNameStr);
  116. else
  117. emitComment(Comment);
  118. Streamer->emitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
  119. incrStreamedLen(sizeof(TypeInd.getIndex()));
  120. } else if (isWriting()) {
  121. if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
  122. return EC;
  123. } else {
  124. uint32_t I;
  125. if (auto EC = Reader->readInteger(I))
  126. return EC;
  127. TypeInd.setIndex(I);
  128. }
  129. return Error::success();
  130. }
  131. Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value,
  132. const Twine &Comment) {
  133. if (isStreaming()) {
  134. if (Value >= 0)
  135. emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
  136. else
  137. emitEncodedSignedInteger(Value, Comment);
  138. } else if (isWriting()) {
  139. if (Value >= 0) {
  140. if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
  141. return EC;
  142. } else {
  143. if (auto EC = writeEncodedSignedInteger(Value))
  144. return EC;
  145. }
  146. } else {
  147. APSInt N;
  148. if (auto EC = consume(*Reader, N))
  149. return EC;
  150. Value = N.getExtValue();
  151. }
  152. return Error::success();
  153. }
  154. Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value,
  155. const Twine &Comment) {
  156. if (isStreaming())
  157. emitEncodedUnsignedInteger(Value, Comment);
  158. else if (isWriting()) {
  159. if (auto EC = writeEncodedUnsignedInteger(Value))
  160. return EC;
  161. } else {
  162. APSInt N;
  163. if (auto EC = consume(*Reader, N))
  164. return EC;
  165. Value = N.getZExtValue();
  166. }
  167. return Error::success();
  168. }
  169. Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value, const Twine &Comment) {
  170. if (isStreaming()) {
  171. // FIXME: We also need to handle big values here, but it's
  172. // not clear how we can excercise this code path yet.
  173. if (Value.isSigned())
  174. emitEncodedSignedInteger(Value.getSExtValue(), Comment);
  175. else
  176. emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
  177. } else if (isWriting()) {
  178. if (Value.isSigned())
  179. return writeEncodedSignedInteger(
  180. Value.isSingleWord() ? Value.getSExtValue() : INT64_MIN);
  181. return writeEncodedUnsignedInteger(Value.getLimitedValue());
  182. } else
  183. return consume(*Reader, Value);
  184. return Error::success();
  185. }
  186. Error CodeViewRecordIO::mapStringZ(StringRef &Value, const Twine &Comment) {
  187. if (isStreaming()) {
  188. auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
  189. emitComment(Comment);
  190. Streamer->emitBytes(NullTerminatedString);
  191. incrStreamedLen(NullTerminatedString.size());
  192. } else if (isWriting()) {
  193. // Truncate if we attempt to write too much.
  194. StringRef S = Value.take_front(maxFieldLength() - 1);
  195. if (auto EC = Writer->writeCString(S))
  196. return EC;
  197. } else {
  198. if (auto EC = Reader->readCString(Value))
  199. return EC;
  200. }
  201. return Error::success();
  202. }
  203. Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
  204. constexpr uint32_t GuidSize = 16;
  205. if (isStreaming()) {
  206. StringRef GuidSR =
  207. StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
  208. emitComment(Comment);
  209. Streamer->emitBytes(GuidSR);
  210. incrStreamedLen(GuidSize);
  211. return Error::success();
  212. }
  213. if (maxFieldLength() < GuidSize)
  214. return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
  215. if (isWriting()) {
  216. if (auto EC = Writer->writeBytes(Guid.Guid))
  217. return EC;
  218. } else {
  219. ArrayRef<uint8_t> GuidBytes;
  220. if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
  221. return EC;
  222. memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
  223. }
  224. return Error::success();
  225. }
  226. Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value,
  227. const Twine &Comment) {
  228. if (!isReading()) {
  229. emitComment(Comment);
  230. for (auto V : Value) {
  231. if (auto EC = mapStringZ(V))
  232. return EC;
  233. }
  234. uint8_t FinalZero = 0;
  235. if (auto EC = mapInteger(FinalZero))
  236. return EC;
  237. } else {
  238. StringRef S;
  239. if (auto EC = mapStringZ(S))
  240. return EC;
  241. while (!S.empty()) {
  242. Value.push_back(S);
  243. if (auto EC = mapStringZ(S))
  244. return EC;
  245. };
  246. }
  247. return Error::success();
  248. }
  249. void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
  250. const Twine &Comment) {
  251. // FIXME: There are no test cases covering this function.
  252. // This may be because we always consider enumerators to be unsigned.
  253. // See FIXME at CodeViewDebug.cpp : CodeViewDebug::lowerTypeEnum.
  254. if (Value < LF_NUMERIC && Value >= 0) {
  255. emitComment(Comment);
  256. Streamer->emitIntValue(Value, 2);
  257. incrStreamedLen(2);
  258. } else if (Value >= std::numeric_limits<int8_t>::min() &&
  259. Value <= std::numeric_limits<int8_t>::max()) {
  260. Streamer->emitIntValue(LF_CHAR, 2);
  261. emitComment(Comment);
  262. Streamer->emitIntValue(Value, 1);
  263. incrStreamedLen(3);
  264. } else if (Value >= std::numeric_limits<int16_t>::min() &&
  265. Value <= std::numeric_limits<int16_t>::max()) {
  266. Streamer->emitIntValue(LF_SHORT, 2);
  267. emitComment(Comment);
  268. Streamer->emitIntValue(Value, 2);
  269. incrStreamedLen(4);
  270. } else if (Value >= std::numeric_limits<int32_t>::min() &&
  271. Value <= std::numeric_limits<int32_t>::max()) {
  272. Streamer->emitIntValue(LF_LONG, 2);
  273. emitComment(Comment);
  274. Streamer->emitIntValue(Value, 4);
  275. incrStreamedLen(6);
  276. } else {
  277. Streamer->emitIntValue(LF_QUADWORD, 2);
  278. emitComment(Comment);
  279. Streamer->emitIntValue(Value, 4); // FIXME: Why not 8 (size of quadword)?
  280. incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
  281. }
  282. }
  283. void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
  284. const Twine &Comment) {
  285. if (Value < LF_NUMERIC) {
  286. emitComment(Comment);
  287. Streamer->emitIntValue(Value, 2);
  288. incrStreamedLen(2);
  289. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  290. Streamer->emitIntValue(LF_USHORT, 2);
  291. emitComment(Comment);
  292. Streamer->emitIntValue(Value, 2);
  293. incrStreamedLen(4);
  294. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  295. Streamer->emitIntValue(LF_ULONG, 2);
  296. emitComment(Comment);
  297. Streamer->emitIntValue(Value, 4);
  298. incrStreamedLen(6);
  299. } else {
  300. // FIXME: There are no test cases covering this block.
  301. Streamer->emitIntValue(LF_UQUADWORD, 2);
  302. emitComment(Comment);
  303. Streamer->emitIntValue(Value, 8);
  304. incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
  305. }
  306. }
  307. Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
  308. if (Value < LF_NUMERIC && Value >= 0) {
  309. if (auto EC = Writer->writeInteger<int16_t>(Value))
  310. return EC;
  311. } else if (Value >= std::numeric_limits<int8_t>::min() &&
  312. Value <= std::numeric_limits<int8_t>::max()) {
  313. if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
  314. return EC;
  315. if (auto EC = Writer->writeInteger<int8_t>(Value))
  316. return EC;
  317. } else if (Value >= std::numeric_limits<int16_t>::min() &&
  318. Value <= std::numeric_limits<int16_t>::max()) {
  319. if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
  320. return EC;
  321. if (auto EC = Writer->writeInteger<int16_t>(Value))
  322. return EC;
  323. } else if (Value >= std::numeric_limits<int32_t>::min() &&
  324. Value <= std::numeric_limits<int32_t>::max()) {
  325. if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
  326. return EC;
  327. if (auto EC = Writer->writeInteger<int32_t>(Value))
  328. return EC;
  329. } else {
  330. if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
  331. return EC;
  332. if (auto EC = Writer->writeInteger(Value))
  333. return EC;
  334. }
  335. return Error::success();
  336. }
  337. Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
  338. if (Value < LF_NUMERIC) {
  339. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  340. return EC;
  341. } else if (Value <= std::numeric_limits<uint16_t>::max()) {
  342. if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
  343. return EC;
  344. if (auto EC = Writer->writeInteger<uint16_t>(Value))
  345. return EC;
  346. } else if (Value <= std::numeric_limits<uint32_t>::max()) {
  347. if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
  348. return EC;
  349. if (auto EC = Writer->writeInteger<uint32_t>(Value))
  350. return EC;
  351. } else {
  352. if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
  353. return EC;
  354. if (auto EC = Writer->writeInteger(Value))
  355. return EC;
  356. }
  357. return Error::success();
  358. }