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