xref: /freebsd-src/contrib/llvm-project/llvm/lib/ObjectYAML/MinidumpEmitter.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
1*8bcb0991SDimitry Andric //===- yaml2minidump.cpp - Convert a YAML file to a minidump file ---------===//
2*8bcb0991SDimitry Andric //
3*8bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*8bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8bcb0991SDimitry Andric //
7*8bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
8*8bcb0991SDimitry Andric 
9*8bcb0991SDimitry Andric #include "llvm/ObjectYAML/MinidumpYAML.h"
10*8bcb0991SDimitry Andric #include "llvm/ObjectYAML/yaml2obj.h"
11*8bcb0991SDimitry Andric #include "llvm/Support/ConvertUTF.h"
12*8bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
13*8bcb0991SDimitry Andric 
14*8bcb0991SDimitry Andric using namespace llvm;
15*8bcb0991SDimitry Andric using namespace llvm::minidump;
16*8bcb0991SDimitry Andric using namespace llvm::MinidumpYAML;
17*8bcb0991SDimitry Andric 
18*8bcb0991SDimitry Andric namespace {
19*8bcb0991SDimitry Andric /// A helper class to manage the placement of various structures into the final
20*8bcb0991SDimitry Andric /// minidump binary. Space for objects can be allocated via various allocate***
21*8bcb0991SDimitry Andric /// methods, while the final minidump file is written by calling the writeTo
22*8bcb0991SDimitry Andric /// method. The plain versions of allocation functions take a reference to the
23*8bcb0991SDimitry Andric /// data which is to be written (and hence the data must be available until
24*8bcb0991SDimitry Andric /// writeTo is called), while the "New" versions allocate the data in an
25*8bcb0991SDimitry Andric /// allocator-managed buffer, which is available until the allocator object is
26*8bcb0991SDimitry Andric /// destroyed. For both kinds of functions, it is possible to modify the
27*8bcb0991SDimitry Andric /// data for which the space has been "allocated" until the final writeTo call.
28*8bcb0991SDimitry Andric /// This is useful for "linking" the allocated structures via their offsets.
29*8bcb0991SDimitry Andric class BlobAllocator {
30*8bcb0991SDimitry Andric public:
31*8bcb0991SDimitry Andric   size_t tell() const { return NextOffset; }
32*8bcb0991SDimitry Andric 
33*8bcb0991SDimitry Andric   size_t allocateCallback(size_t Size,
34*8bcb0991SDimitry Andric                           std::function<void(raw_ostream &)> Callback) {
35*8bcb0991SDimitry Andric     size_t Offset = NextOffset;
36*8bcb0991SDimitry Andric     NextOffset += Size;
37*8bcb0991SDimitry Andric     Callbacks.push_back(std::move(Callback));
38*8bcb0991SDimitry Andric     return Offset;
39*8bcb0991SDimitry Andric   }
40*8bcb0991SDimitry Andric 
41*8bcb0991SDimitry Andric   size_t allocateBytes(ArrayRef<uint8_t> Data) {
42*8bcb0991SDimitry Andric     return allocateCallback(
43*8bcb0991SDimitry Andric         Data.size(), [Data](raw_ostream &OS) { OS << toStringRef(Data); });
44*8bcb0991SDimitry Andric   }
45*8bcb0991SDimitry Andric 
46*8bcb0991SDimitry Andric   size_t allocateBytes(yaml::BinaryRef Data) {
47*8bcb0991SDimitry Andric     return allocateCallback(Data.binary_size(), [Data](raw_ostream &OS) {
48*8bcb0991SDimitry Andric       Data.writeAsBinary(OS);
49*8bcb0991SDimitry Andric     });
50*8bcb0991SDimitry Andric   }
51*8bcb0991SDimitry Andric 
52*8bcb0991SDimitry Andric   template <typename T> size_t allocateArray(ArrayRef<T> Data) {
53*8bcb0991SDimitry Andric     return allocateBytes({reinterpret_cast<const uint8_t *>(Data.data()),
54*8bcb0991SDimitry Andric                           sizeof(T) * Data.size()});
55*8bcb0991SDimitry Andric   }
56*8bcb0991SDimitry Andric 
57*8bcb0991SDimitry Andric   template <typename T, typename RangeType>
58*8bcb0991SDimitry Andric   std::pair<size_t, MutableArrayRef<T>>
59*8bcb0991SDimitry Andric   allocateNewArray(const iterator_range<RangeType> &Range);
60*8bcb0991SDimitry Andric 
61*8bcb0991SDimitry Andric   template <typename T> size_t allocateObject(const T &Data) {
62*8bcb0991SDimitry Andric     return allocateArray(makeArrayRef(Data));
63*8bcb0991SDimitry Andric   }
64*8bcb0991SDimitry Andric 
65*8bcb0991SDimitry Andric   template <typename T, typename... Types>
66*8bcb0991SDimitry Andric   std::pair<size_t, T *> allocateNewObject(Types &&... Args) {
67*8bcb0991SDimitry Andric     T *Object = new (Temporaries.Allocate<T>()) T(std::forward<Types>(Args)...);
68*8bcb0991SDimitry Andric     return {allocateObject(*Object), Object};
69*8bcb0991SDimitry Andric   }
70*8bcb0991SDimitry Andric 
71*8bcb0991SDimitry Andric   size_t allocateString(StringRef Str);
72*8bcb0991SDimitry Andric 
73*8bcb0991SDimitry Andric   void writeTo(raw_ostream &OS) const;
74*8bcb0991SDimitry Andric 
75*8bcb0991SDimitry Andric private:
76*8bcb0991SDimitry Andric   size_t NextOffset = 0;
77*8bcb0991SDimitry Andric 
78*8bcb0991SDimitry Andric   BumpPtrAllocator Temporaries;
79*8bcb0991SDimitry Andric   std::vector<std::function<void(raw_ostream &)>> Callbacks;
80*8bcb0991SDimitry Andric };
81*8bcb0991SDimitry Andric } // namespace
82*8bcb0991SDimitry Andric 
83*8bcb0991SDimitry Andric template <typename T, typename RangeType>
84*8bcb0991SDimitry Andric std::pair<size_t, MutableArrayRef<T>>
85*8bcb0991SDimitry Andric BlobAllocator::allocateNewArray(const iterator_range<RangeType> &Range) {
86*8bcb0991SDimitry Andric   size_t Num = std::distance(Range.begin(), Range.end());
87*8bcb0991SDimitry Andric   MutableArrayRef<T> Array(Temporaries.Allocate<T>(Num), Num);
88*8bcb0991SDimitry Andric   std::uninitialized_copy(Range.begin(), Range.end(), Array.begin());
89*8bcb0991SDimitry Andric   return {allocateArray(Array), Array};
90*8bcb0991SDimitry Andric }
91*8bcb0991SDimitry Andric 
92*8bcb0991SDimitry Andric size_t BlobAllocator::allocateString(StringRef Str) {
93*8bcb0991SDimitry Andric   SmallVector<UTF16, 32> WStr;
94*8bcb0991SDimitry Andric   bool OK = convertUTF8ToUTF16String(Str, WStr);
95*8bcb0991SDimitry Andric   assert(OK && "Invalid UTF8 in Str?");
96*8bcb0991SDimitry Andric   (void)OK;
97*8bcb0991SDimitry Andric 
98*8bcb0991SDimitry Andric   // The utf16 string is null-terminated, but the terminator is not counted in
99*8bcb0991SDimitry Andric   // the string size.
100*8bcb0991SDimitry Andric   WStr.push_back(0);
101*8bcb0991SDimitry Andric   size_t Result =
102*8bcb0991SDimitry Andric       allocateNewObject<support::ulittle32_t>(2 * (WStr.size() - 1)).first;
103*8bcb0991SDimitry Andric   allocateNewArray<support::ulittle16_t>(make_range(WStr.begin(), WStr.end()));
104*8bcb0991SDimitry Andric   return Result;
105*8bcb0991SDimitry Andric }
106*8bcb0991SDimitry Andric 
107*8bcb0991SDimitry Andric void BlobAllocator::writeTo(raw_ostream &OS) const {
108*8bcb0991SDimitry Andric   size_t BeginOffset = OS.tell();
109*8bcb0991SDimitry Andric   for (const auto &Callback : Callbacks)
110*8bcb0991SDimitry Andric     Callback(OS);
111*8bcb0991SDimitry Andric   assert(OS.tell() == BeginOffset + NextOffset &&
112*8bcb0991SDimitry Andric          "Callbacks wrote an unexpected number of bytes.");
113*8bcb0991SDimitry Andric   (void)BeginOffset;
114*8bcb0991SDimitry Andric }
115*8bcb0991SDimitry Andric 
116*8bcb0991SDimitry Andric static LocationDescriptor layout(BlobAllocator &File, yaml::BinaryRef Data) {
117*8bcb0991SDimitry Andric   return {support::ulittle32_t(Data.binary_size()),
118*8bcb0991SDimitry Andric           support::ulittle32_t(File.allocateBytes(Data))};
119*8bcb0991SDimitry Andric }
120*8bcb0991SDimitry Andric 
121*8bcb0991SDimitry Andric static size_t layout(BlobAllocator &File, MinidumpYAML::ExceptionStream &S) {
122*8bcb0991SDimitry Andric   File.allocateObject(S.MDExceptionStream);
123*8bcb0991SDimitry Andric 
124*8bcb0991SDimitry Andric   size_t DataEnd = File.tell();
125*8bcb0991SDimitry Andric 
126*8bcb0991SDimitry Andric   // Lay out the thread context data, (which is not a part of the stream).
127*8bcb0991SDimitry Andric   // TODO: This usually (always?) matches the thread context of the
128*8bcb0991SDimitry Andric   // corresponding thread, and may overlap memory regions as well.  We could
129*8bcb0991SDimitry Andric   // add a level of indirection to the MinidumpYAML format (like an array of
130*8bcb0991SDimitry Andric   // Blobs that the LocationDescriptors index into) to be able to distinguish
131*8bcb0991SDimitry Andric   // the cases where location descriptions overlap vs happen to reference
132*8bcb0991SDimitry Andric   // identical data.
133*8bcb0991SDimitry Andric   S.MDExceptionStream.ThreadContext = layout(File, S.ThreadContext);
134*8bcb0991SDimitry Andric 
135*8bcb0991SDimitry Andric   return DataEnd;
136*8bcb0991SDimitry Andric }
137*8bcb0991SDimitry Andric 
138*8bcb0991SDimitry Andric static void layout(BlobAllocator &File, MemoryListStream::entry_type &Range) {
139*8bcb0991SDimitry Andric   Range.Entry.Memory = layout(File, Range.Content);
140*8bcb0991SDimitry Andric }
141*8bcb0991SDimitry Andric 
142*8bcb0991SDimitry Andric static void layout(BlobAllocator &File, ModuleListStream::entry_type &M) {
143*8bcb0991SDimitry Andric   M.Entry.ModuleNameRVA = File.allocateString(M.Name);
144*8bcb0991SDimitry Andric 
145*8bcb0991SDimitry Andric   M.Entry.CvRecord = layout(File, M.CvRecord);
146*8bcb0991SDimitry Andric   M.Entry.MiscRecord = layout(File, M.MiscRecord);
147*8bcb0991SDimitry Andric }
148*8bcb0991SDimitry Andric 
149*8bcb0991SDimitry Andric static void layout(BlobAllocator &File, ThreadListStream::entry_type &T) {
150*8bcb0991SDimitry Andric   T.Entry.Stack.Memory = layout(File, T.Stack);
151*8bcb0991SDimitry Andric   T.Entry.Context = layout(File, T.Context);
152*8bcb0991SDimitry Andric }
153*8bcb0991SDimitry Andric 
154*8bcb0991SDimitry Andric template <typename EntryT>
155*8bcb0991SDimitry Andric static size_t layout(BlobAllocator &File,
156*8bcb0991SDimitry Andric                      MinidumpYAML::detail::ListStream<EntryT> &S) {
157*8bcb0991SDimitry Andric 
158*8bcb0991SDimitry Andric   File.allocateNewObject<support::ulittle32_t>(S.Entries.size());
159*8bcb0991SDimitry Andric   for (auto &E : S.Entries)
160*8bcb0991SDimitry Andric     File.allocateObject(E.Entry);
161*8bcb0991SDimitry Andric 
162*8bcb0991SDimitry Andric   size_t DataEnd = File.tell();
163*8bcb0991SDimitry Andric 
164*8bcb0991SDimitry Andric   // Lay out the auxiliary data, (which is not a part of the stream).
165*8bcb0991SDimitry Andric   DataEnd = File.tell();
166*8bcb0991SDimitry Andric   for (auto &E : S.Entries)
167*8bcb0991SDimitry Andric     layout(File, E);
168*8bcb0991SDimitry Andric 
169*8bcb0991SDimitry Andric   return DataEnd;
170*8bcb0991SDimitry Andric }
171*8bcb0991SDimitry Andric 
172*8bcb0991SDimitry Andric static Directory layout(BlobAllocator &File, Stream &S) {
173*8bcb0991SDimitry Andric   Directory Result;
174*8bcb0991SDimitry Andric   Result.Type = S.Type;
175*8bcb0991SDimitry Andric   Result.Location.RVA = File.tell();
176*8bcb0991SDimitry Andric   Optional<size_t> DataEnd;
177*8bcb0991SDimitry Andric   switch (S.Kind) {
178*8bcb0991SDimitry Andric   case Stream::StreamKind::Exception:
179*8bcb0991SDimitry Andric     DataEnd = layout(File, cast<MinidumpYAML::ExceptionStream>(S));
180*8bcb0991SDimitry Andric     break;
181*8bcb0991SDimitry Andric   case Stream::StreamKind::MemoryInfoList: {
182*8bcb0991SDimitry Andric     MemoryInfoListStream &InfoList = cast<MemoryInfoListStream>(S);
183*8bcb0991SDimitry Andric     File.allocateNewObject<minidump::MemoryInfoListHeader>(
184*8bcb0991SDimitry Andric         sizeof(minidump::MemoryInfoListHeader), sizeof(minidump::MemoryInfo),
185*8bcb0991SDimitry Andric         InfoList.Infos.size());
186*8bcb0991SDimitry Andric     File.allocateArray(makeArrayRef(InfoList.Infos));
187*8bcb0991SDimitry Andric     break;
188*8bcb0991SDimitry Andric   }
189*8bcb0991SDimitry Andric   case Stream::StreamKind::MemoryList:
190*8bcb0991SDimitry Andric     DataEnd = layout(File, cast<MemoryListStream>(S));
191*8bcb0991SDimitry Andric     break;
192*8bcb0991SDimitry Andric   case Stream::StreamKind::ModuleList:
193*8bcb0991SDimitry Andric     DataEnd = layout(File, cast<ModuleListStream>(S));
194*8bcb0991SDimitry Andric     break;
195*8bcb0991SDimitry Andric   case Stream::StreamKind::RawContent: {
196*8bcb0991SDimitry Andric     RawContentStream &Raw = cast<RawContentStream>(S);
197*8bcb0991SDimitry Andric     File.allocateCallback(Raw.Size, [&Raw](raw_ostream &OS) {
198*8bcb0991SDimitry Andric       Raw.Content.writeAsBinary(OS);
199*8bcb0991SDimitry Andric       assert(Raw.Content.binary_size() <= Raw.Size);
200*8bcb0991SDimitry Andric       OS << std::string(Raw.Size - Raw.Content.binary_size(), '\0');
201*8bcb0991SDimitry Andric     });
202*8bcb0991SDimitry Andric     break;
203*8bcb0991SDimitry Andric   }
204*8bcb0991SDimitry Andric   case Stream::StreamKind::SystemInfo: {
205*8bcb0991SDimitry Andric     SystemInfoStream &SystemInfo = cast<SystemInfoStream>(S);
206*8bcb0991SDimitry Andric     File.allocateObject(SystemInfo.Info);
207*8bcb0991SDimitry Andric     // The CSD string is not a part of the stream.
208*8bcb0991SDimitry Andric     DataEnd = File.tell();
209*8bcb0991SDimitry Andric     SystemInfo.Info.CSDVersionRVA = File.allocateString(SystemInfo.CSDVersion);
210*8bcb0991SDimitry Andric     break;
211*8bcb0991SDimitry Andric   }
212*8bcb0991SDimitry Andric   case Stream::StreamKind::TextContent:
213*8bcb0991SDimitry Andric     File.allocateArray(arrayRefFromStringRef(cast<TextContentStream>(S).Text));
214*8bcb0991SDimitry Andric     break;
215*8bcb0991SDimitry Andric   case Stream::StreamKind::ThreadList:
216*8bcb0991SDimitry Andric     DataEnd = layout(File, cast<ThreadListStream>(S));
217*8bcb0991SDimitry Andric     break;
218*8bcb0991SDimitry Andric   }
219*8bcb0991SDimitry Andric   // If DataEnd is not set, we assume everything we generated is a part of the
220*8bcb0991SDimitry Andric   // stream.
221*8bcb0991SDimitry Andric   Result.Location.DataSize =
222*8bcb0991SDimitry Andric       DataEnd.getValueOr(File.tell()) - Result.Location.RVA;
223*8bcb0991SDimitry Andric   return Result;
224*8bcb0991SDimitry Andric }
225*8bcb0991SDimitry Andric 
226*8bcb0991SDimitry Andric namespace llvm {
227*8bcb0991SDimitry Andric namespace yaml {
228*8bcb0991SDimitry Andric 
229*8bcb0991SDimitry Andric bool yaml2minidump(MinidumpYAML::Object &Obj, raw_ostream &Out,
230*8bcb0991SDimitry Andric                    ErrorHandler /*EH*/) {
231*8bcb0991SDimitry Andric   BlobAllocator File;
232*8bcb0991SDimitry Andric   File.allocateObject(Obj.Header);
233*8bcb0991SDimitry Andric 
234*8bcb0991SDimitry Andric   std::vector<Directory> StreamDirectory(Obj.Streams.size());
235*8bcb0991SDimitry Andric   Obj.Header.StreamDirectoryRVA =
236*8bcb0991SDimitry Andric       File.allocateArray(makeArrayRef(StreamDirectory));
237*8bcb0991SDimitry Andric   Obj.Header.NumberOfStreams = StreamDirectory.size();
238*8bcb0991SDimitry Andric 
239*8bcb0991SDimitry Andric   for (auto &Stream : enumerate(Obj.Streams))
240*8bcb0991SDimitry Andric     StreamDirectory[Stream.index()] = layout(File, *Stream.value());
241*8bcb0991SDimitry Andric 
242*8bcb0991SDimitry Andric   File.writeTo(Out);
243*8bcb0991SDimitry Andric   return true;
244*8bcb0991SDimitry Andric }
245*8bcb0991SDimitry Andric 
246*8bcb0991SDimitry Andric } // namespace yaml
247*8bcb0991SDimitry Andric } // namespace llvm
248