blob: 919f870b34aa9cc0ab4d2ff8bcf7cfb956f91f74 [file] [log] [blame]
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +00001//===- MSFBuilder.cpp -----------------------------------------------------===//
Zachary Turnerf52a8992016-07-15 20:43:38 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000010#include "llvm/ADT/ArrayRef.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000011#include "llvm/DebugInfo/MSF/MSFBuilder.h"
12#include "llvm/DebugInfo/MSF/MSFError.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000013#include "llvm/Support/Endian.h"
14#include "llvm/Support/Error.h"
15#include <algorithm>
16#include <cassert>
17#include <cstdint>
18#include <cstring>
19#include <memory>
20#include <utility>
21#include <vector>
Zachary Turnerf52a8992016-07-15 20:43:38 +000022
23using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000024using namespace llvm::msf;
Zachary Turnerf52a8992016-07-15 20:43:38 +000025using namespace llvm::support;
26
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000027static const uint32_t kSuperBlockBlock = 0;
28static const uint32_t kFreePageMap0Block = 1;
29static const uint32_t kFreePageMap1Block = 2;
30static const uint32_t kNumReservedPages = 3;
Zachary Turnerb927e022016-07-15 22:17:19 +000031
Zachary Turner3203e272018-03-29 18:34:15 +000032static const uint32_t kDefaultFreePageMap = kFreePageMap1Block;
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000033static const uint32_t kDefaultBlockMapAddr = kNumReservedPages;
Zachary Turnerf52a8992016-07-15 20:43:38 +000034
Zachary Turnera3225b02016-07-29 20:56:36 +000035MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow,
Zachary Turnerf52a8992016-07-15 20:43:38 +000036 BumpPtrAllocator &Allocator)
Rui Ueyama5d6714e2016-09-30 20:52:12 +000037 : Allocator(Allocator), IsGrowable(CanGrow),
38 FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize),
NAKAMURA Takumi1657f2a2017-11-01 13:47:55 +000039 BlockMapAddr(kDefaultBlockMapAddr), FreeBlocks(MinBlockCount, true) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000040 FreeBlocks[kSuperBlockBlock] = false;
Zachary Turnerb927e022016-07-15 22:17:19 +000041 FreeBlocks[kFreePageMap0Block] = false;
42 FreeBlocks[kFreePageMap1Block] = false;
Zachary Turnerf52a8992016-07-15 20:43:38 +000043 FreeBlocks[BlockMapAddr] = false;
44}
45
Zachary Turnera3225b02016-07-29 20:56:36 +000046Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator,
Zachary Turnerf52a8992016-07-15 20:43:38 +000047 uint32_t BlockSize,
48 uint32_t MinBlockCount, bool CanGrow) {
Zachary Turnerbac69d32016-07-22 19:56:05 +000049 if (!isValidBlockSize(BlockSize))
Zachary Turnera3225b02016-07-29 20:56:36 +000050 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000051 "The requested block size is unsupported");
52
Zachary Turnera3225b02016-07-29 20:56:36 +000053 return MSFBuilder(BlockSize,
Zachary Turnerb927e022016-07-15 22:17:19 +000054 std::max(MinBlockCount, msf::getMinimumBlockCount()),
55 CanGrow, Allocator);
Zachary Turnerf52a8992016-07-15 20:43:38 +000056}
57
Zachary Turnera3225b02016-07-29 20:56:36 +000058Error MSFBuilder::setBlockMapAddr(uint32_t Addr) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000059 if (Addr == BlockMapAddr)
60 return Error::success();
61
62 if (Addr >= FreeBlocks.size()) {
63 if (!IsGrowable)
Zachary Turnera3225b02016-07-29 20:56:36 +000064 return make_error<MSFError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +000065 "Cannot grow the number of blocks");
Rui Ueyama4ee7f3c2016-08-02 21:56:37 +000066 FreeBlocks.resize(Addr + 1, true);
Zachary Turnerf52a8992016-07-15 20:43:38 +000067 }
68
69 if (!isBlockFree(Addr))
Zachary Turnera3225b02016-07-29 20:56:36 +000070 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +000071 msf_error_code::block_in_use,
72 "Requested block map address is already in use");
Zachary Turnerf52a8992016-07-15 20:43:38 +000073 FreeBlocks[BlockMapAddr] = true;
74 FreeBlocks[Addr] = false;
75 BlockMapAddr = Addr;
76 return Error::success();
77}
78
Zachary Turnera3225b02016-07-29 20:56:36 +000079void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000080
Zachary Turnera3225b02016-07-29 20:56:36 +000081void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000082
Zachary Turnera3225b02016-07-29 20:56:36 +000083Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000084 for (auto B : DirectoryBlocks)
85 FreeBlocks[B] = true;
86 for (auto B : DirBlocks) {
87 if (!isBlockFree(B)) {
Zachary Turnera3225b02016-07-29 20:56:36 +000088 return make_error<MSFError>(msf_error_code::unspecified,
Zachary Turnerfaa554b2016-07-15 22:16:56 +000089 "Attempt to reuse an allocated block");
90 }
91 FreeBlocks[B] = false;
92 }
93
94 DirectoryBlocks = DirBlocks;
95 return Error::success();
96}
97
Zachary Turnera3225b02016-07-29 20:56:36 +000098Error MSFBuilder::allocateBlocks(uint32_t NumBlocks,
Zachary Turnerf52a8992016-07-15 20:43:38 +000099 MutableArrayRef<uint32_t> Blocks) {
100 if (NumBlocks == 0)
101 return Error::success();
102
103 uint32_t NumFreeBlocks = FreeBlocks.count();
104 if (NumFreeBlocks < NumBlocks) {
105 if (!IsGrowable)
Zachary Turnera3225b02016-07-29 20:56:36 +0000106 return make_error<MSFError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +0000107 "There are no free Blocks in the file");
108 uint32_t AllocBlocks = NumBlocks - NumFreeBlocks;
Zachary Turner9fb9d712017-08-02 22:31:39 +0000109 uint32_t OldBlockCount = FreeBlocks.size();
110 uint32_t NewBlockCount = AllocBlocks + OldBlockCount;
111 uint32_t NextFpmBlock = alignTo(OldBlockCount, BlockSize) + 1;
112 FreeBlocks.resize(NewBlockCount, true);
113 // If we crossed over an fpm page, we actually need to allocate 2 extra
114 // blocks for each FPM group crossed and mark both blocks from the group as
Zachary Turner3203e272018-03-29 18:34:15 +0000115 // used. FPM blocks are marked as allocated regardless of whether or not
116 // they ultimately describe the status of blocks in the file. This means
117 // that not only are extraneous blocks at the end of the main FPM marked as
118 // allocated, but also blocks from the alternate FPM are always marked as
119 // allocated.
Zachary Turner9fb9d712017-08-02 22:31:39 +0000120 while (NextFpmBlock < NewBlockCount) {
121 NewBlockCount += 2;
122 FreeBlocks.resize(NewBlockCount, true);
123 FreeBlocks.reset(NextFpmBlock, NextFpmBlock + 2);
124 NextFpmBlock += BlockSize;
125 }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000126 }
127
128 int I = 0;
129 int Block = FreeBlocks.find_first();
130 do {
131 assert(Block != -1 && "We ran out of Blocks!");
132
133 uint32_t NextBlock = static_cast<uint32_t>(Block);
134 Blocks[I++] = NextBlock;
135 FreeBlocks.reset(NextBlock);
136 Block = FreeBlocks.find_next(Block);
137 } while (--NumBlocks > 0);
138 return Error::success();
139}
140
Zachary Turnera3225b02016-07-29 20:56:36 +0000141uint32_t MSFBuilder::getNumUsedBlocks() const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000142 return getTotalBlockCount() - getNumFreeBlocks();
143}
144
Zachary Turnera3225b02016-07-29 20:56:36 +0000145uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000146
Zachary Turnera3225b02016-07-29 20:56:36 +0000147uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000148
Zachary Turnera3225b02016-07-29 20:56:36 +0000149bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000150
Zachary Turner620961d2016-09-14 23:00:02 +0000151Expected<uint32_t> MSFBuilder::addStream(uint32_t Size,
152 ArrayRef<uint32_t> Blocks) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000153 // Add a new stream mapped to the specified blocks. Verify that the specified
154 // blocks are both necessary and sufficient for holding the requested number
155 // of bytes, and verify that all requested blocks are free.
156 uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
157 if (ReqBlocks != Blocks.size())
Zachary Turnera3225b02016-07-29 20:56:36 +0000158 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +0000159 msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +0000160 "Incorrect number of blocks for requested stream size");
161 for (auto Block : Blocks) {
162 if (Block >= FreeBlocks.size())
163 FreeBlocks.resize(Block + 1, true);
164
165 if (!FreeBlocks.test(Block))
Zachary Turnera3225b02016-07-29 20:56:36 +0000166 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +0000167 msf_error_code::unspecified,
Zachary Turnerf52a8992016-07-15 20:43:38 +0000168 "Attempt to re-use an already allocated block");
169 }
170 // Mark all the blocks occupied by the new stream as not free.
171 for (auto Block : Blocks) {
172 FreeBlocks.reset(Block);
173 }
174 StreamData.push_back(std::make_pair(Size, Blocks));
Zachary Turner620961d2016-09-14 23:00:02 +0000175 return StreamData.size() - 1;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000176}
177
Zachary Turner620961d2016-09-14 23:00:02 +0000178Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000179 uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
180 std::vector<uint32_t> NewBlocks;
181 NewBlocks.resize(ReqBlocks);
182 if (auto EC = allocateBlocks(ReqBlocks, NewBlocks))
Zachary Turner620961d2016-09-14 23:00:02 +0000183 return std::move(EC);
Zachary Turnerf52a8992016-07-15 20:43:38 +0000184 StreamData.push_back(std::make_pair(Size, NewBlocks));
Zachary Turner620961d2016-09-14 23:00:02 +0000185 return StreamData.size() - 1;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000186}
187
Zachary Turnera3225b02016-07-29 20:56:36 +0000188Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000189 uint32_t OldSize = getStreamSize(Idx);
190 if (OldSize == Size)
191 return Error::success();
192
193 uint32_t NewBlocks = bytesToBlocks(Size, BlockSize);
194 uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize);
195
196 if (NewBlocks > OldBlocks) {
197 uint32_t AddedBlocks = NewBlocks - OldBlocks;
198 // If we're growing, we have to allocate new Blocks.
199 std::vector<uint32_t> AddedBlockList;
200 AddedBlockList.resize(AddedBlocks);
201 if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList))
202 return EC;
203 auto &CurrentBlocks = StreamData[Idx].second;
204 CurrentBlocks.insert(CurrentBlocks.end(), AddedBlockList.begin(),
205 AddedBlockList.end());
206 } else if (OldBlocks > NewBlocks) {
207 // For shrinking, free all the Blocks in the Block map, update the stream
208 // data, then shrink the directory.
209 uint32_t RemovedBlocks = OldBlocks - NewBlocks;
210 auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second);
211 auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks);
212 for (auto P : RemovedBlockList)
213 FreeBlocks[P] = true;
214 StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks);
215 }
216
217 StreamData[Idx].first = Size;
218 return Error::success();
219}
220
Zachary Turnera3225b02016-07-29 20:56:36 +0000221uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000222
Zachary Turnera3225b02016-07-29 20:56:36 +0000223uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000224 return StreamData[StreamIdx].first;
225}
226
Zachary Turnera3225b02016-07-29 20:56:36 +0000227ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000228 return StreamData[StreamIdx].second;
229}
230
Zachary Turnera3225b02016-07-29 20:56:36 +0000231uint32_t MSFBuilder::computeDirectoryByteSize() const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000232 // The directory has the following layout, where each item is a ulittle32_t:
233 // NumStreams
234 // StreamSizes[NumStreams]
235 // StreamBlocks[NumStreams][]
236 uint32_t Size = sizeof(ulittle32_t); // NumStreams
237 Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes
238 for (const auto &D : StreamData) {
239 uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize);
240 assert(ExpectedNumBlocks == D.second.size() &&
241 "Unexpected number of blocks");
242 Size += ExpectedNumBlocks * sizeof(ulittle32_t);
243 }
244 return Size;
245}
246
Zachary Turnera3225b02016-07-29 20:56:36 +0000247Expected<MSFLayout> MSFBuilder::build() {
Zachary Turnere4a4f332016-07-22 19:56:33 +0000248 SuperBlock *SB = Allocator.Allocate<SuperBlock>();
Zachary Turnera3225b02016-07-29 20:56:36 +0000249 MSFLayout L;
Zachary Turnere4a4f332016-07-22 19:56:33 +0000250 L.SB = SB;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000251
Zachary Turnere4a4f332016-07-22 19:56:33 +0000252 std::memcpy(SB->MagicBytes, Magic, sizeof(Magic));
253 SB->BlockMapAddr = BlockMapAddr;
254 SB->BlockSize = BlockSize;
255 SB->NumDirectoryBytes = computeDirectoryByteSize();
256 SB->FreeBlockMapBlock = FreePageMap;
257 SB->Unknown1 = Unknown1;
258
259 uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize);
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000260 if (NumDirectoryBlocks > DirectoryBlocks.size()) {
261 // Our hint wasn't enough to satisfy the entire directory. Allocate
262 // remaining pages.
263 std::vector<uint32_t> ExtraBlocks;
264 uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
265 ExtraBlocks.resize(NumExtraBlocks);
266 if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
267 return std::move(EC);
268 DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(),
269 ExtraBlocks.end());
270 } else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
271 uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
272 for (auto B :
273 ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks))
274 FreeBlocks[B] = true;
275 DirectoryBlocks.resize(NumDirectoryBlocks);
276 }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000277
278 // Don't set the number of blocks in the file until after allocating Blocks
Zachary Turnerbac69d32016-07-22 19:56:05 +0000279 // for the directory, since the allocation might cause the file to need to
280 // grow.
Zachary Turnere4a4f332016-07-22 19:56:33 +0000281 SB->NumBlocks = FreeBlocks.size();
Zachary Turnerf52a8992016-07-15 20:43:38 +0000282
283 ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks);
284 std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks,
285 DirBlocks);
286 L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks);
287
288 // The stream sizes should be re-allocated as a stable pointer and the stream
289 // map should have each of its entries allocated as a separate stable pointer.
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000290 if (!StreamData.empty()) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000291 ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size());
292 L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size());
293 L.StreamMap.resize(StreamData.size());
294 for (uint32_t I = 0; I < StreamData.size(); ++I) {
295 Sizes[I] = StreamData[I].first;
296 ulittle32_t *BlockList =
297 Allocator.Allocate<ulittle32_t>(StreamData[I].second.size());
298 std::uninitialized_copy_n(StreamData[I].second.begin(),
299 StreamData[I].second.size(), BlockList);
300 L.StreamMap[I] =
301 ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size());
302 }
303 }
304
Zachary Turner9fb9d712017-08-02 22:31:39 +0000305 L.FreePageMap = FreeBlocks;
306
Zachary Turnerf52a8992016-07-15 20:43:38 +0000307 return L;
308}