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