| 1 | //===- MSFBuilder.cpp -----------------------------------------------------===// |
| 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/MSF/MSFBuilder.h" |
| 10 | #include "llvm/ADT/ArrayRef.h" |
| 11 | #include "llvm/DebugInfo/MSF/MSFError.h" |
| 12 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
| 13 | #include "llvm/Support/BinaryByteStream.h" |
| 14 | #include "llvm/Support/BinaryStreamWriter.h" |
| 15 | #include "llvm/Support/Endian.h" |
| 16 | #include "llvm/Support/Error.h" |
| 17 | #include "llvm/Support/FileOutputBuffer.h" |
| 18 | #include "llvm/Support/FormatVariadic.h" |
| 19 | #include "llvm/Support/TimeProfiler.h" |
| 20 | #include <algorithm> |
| 21 | #include <cassert> |
| 22 | #include <cstdint> |
| 23 | #include <cstring> |
| 24 | #include <memory> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace llvm::msf; |
| 30 | using namespace llvm::support; |
| 31 | |
| 32 | static const uint32_t kSuperBlockBlock = 0; |
| 33 | static const uint32_t kFreePageMap0Block = 1; |
| 34 | static const uint32_t kFreePageMap1Block = 2; |
| 35 | static const uint32_t kNumReservedPages = 3; |
| 36 | |
| 37 | static const uint32_t kDefaultFreePageMap = kFreePageMap1Block; |
| 38 | static const uint32_t kDefaultBlockMapAddr = kNumReservedPages; |
| 39 | |
| 40 | MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow, |
| 41 | BumpPtrAllocator &Allocator) |
| 42 | : Allocator(Allocator), IsGrowable(CanGrow), |
| 43 | FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize), |
| 44 | BlockMapAddr(kDefaultBlockMapAddr), FreeBlocks(MinBlockCount, true) { |
| 45 | FreeBlocks[kSuperBlockBlock] = false; |
| 46 | FreeBlocks[kFreePageMap0Block] = false; |
| 47 | FreeBlocks[kFreePageMap1Block] = false; |
| 48 | FreeBlocks[BlockMapAddr] = false; |
| 49 | } |
| 50 | |
| 51 | Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator, |
| 52 | uint32_t BlockSize, |
| 53 | uint32_t MinBlockCount, bool CanGrow) { |
| 54 | if (!isValidBlockSize(Size: BlockSize)) |
| 55 | return make_error<MSFError>(Args: msf_error_code::invalid_format, |
| 56 | Args: "The requested block size is unsupported" ); |
| 57 | |
| 58 | return MSFBuilder(BlockSize, |
| 59 | std::max(a: MinBlockCount, b: msf::getMinimumBlockCount()), |
| 60 | CanGrow, Allocator); |
| 61 | } |
| 62 | |
| 63 | Error MSFBuilder::setBlockMapAddr(uint32_t Addr) { |
| 64 | if (Addr == BlockMapAddr) |
| 65 | return Error::success(); |
| 66 | |
| 67 | if (Addr >= FreeBlocks.size()) { |
| 68 | if (!IsGrowable) |
| 69 | return make_error<MSFError>(Args: msf_error_code::insufficient_buffer, |
| 70 | Args: "Cannot grow the number of blocks" ); |
| 71 | FreeBlocks.resize(N: Addr + 1, t: true); |
| 72 | } |
| 73 | |
| 74 | if (!isBlockFree(Idx: Addr)) |
| 75 | return make_error<MSFError>( |
| 76 | Args: msf_error_code::block_in_use, |
| 77 | Args: "Requested block map address is already in use" ); |
| 78 | FreeBlocks[BlockMapAddr] = true; |
| 79 | FreeBlocks[Addr] = false; |
| 80 | BlockMapAddr = Addr; |
| 81 | return Error::success(); |
| 82 | } |
| 83 | |
| 84 | void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; } |
| 85 | |
| 86 | void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; } |
| 87 | |
| 88 | Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) { |
| 89 | for (auto B : DirectoryBlocks) |
| 90 | FreeBlocks[B] = true; |
| 91 | for (auto B : DirBlocks) { |
| 92 | if (!isBlockFree(Idx: B)) { |
| 93 | return make_error<MSFError>(Args: msf_error_code::unspecified, |
| 94 | Args: "Attempt to reuse an allocated block" ); |
| 95 | } |
| 96 | FreeBlocks[B] = false; |
| 97 | } |
| 98 | |
| 99 | DirectoryBlocks = DirBlocks; |
| 100 | return Error::success(); |
| 101 | } |
| 102 | |
| 103 | Error MSFBuilder::allocateBlocks(uint32_t NumBlocks, |
| 104 | MutableArrayRef<uint32_t> Blocks) { |
| 105 | if (NumBlocks == 0) |
| 106 | return Error::success(); |
| 107 | |
| 108 | uint32_t NumFreeBlocks = FreeBlocks.count(); |
| 109 | if (NumFreeBlocks < NumBlocks) { |
| 110 | if (!IsGrowable) |
| 111 | return make_error<MSFError>(Args: msf_error_code::insufficient_buffer, |
| 112 | Args: "There are no free Blocks in the file" ); |
| 113 | uint32_t AllocBlocks = NumBlocks - NumFreeBlocks; |
| 114 | uint32_t OldBlockCount = FreeBlocks.size(); |
| 115 | uint32_t NewBlockCount = AllocBlocks + OldBlockCount; |
| 116 | uint32_t NextFpmBlock = alignTo(Value: OldBlockCount, Align: BlockSize) + 1; |
| 117 | FreeBlocks.resize(N: NewBlockCount, t: true); |
| 118 | // If we crossed over an fpm page, we actually need to allocate 2 extra |
| 119 | // blocks for each FPM group crossed and mark both blocks from the group as |
| 120 | // used. FPM blocks are marked as allocated regardless of whether or not |
| 121 | // they ultimately describe the status of blocks in the file. This means |
| 122 | // that not only are extraneous blocks at the end of the main FPM marked as |
| 123 | // allocated, but also blocks from the alternate FPM are always marked as |
| 124 | // allocated. |
| 125 | while (NextFpmBlock < NewBlockCount) { |
| 126 | NewBlockCount += 2; |
| 127 | FreeBlocks.resize(N: NewBlockCount, t: true); |
| 128 | FreeBlocks.reset(I: NextFpmBlock, E: NextFpmBlock + 2); |
| 129 | NextFpmBlock += BlockSize; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | int I = 0; |
| 134 | int Block = FreeBlocks.find_first(); |
| 135 | do { |
| 136 | assert(Block != -1 && "We ran out of Blocks!" ); |
| 137 | |
| 138 | uint32_t NextBlock = static_cast<uint32_t>(Block); |
| 139 | Blocks[I++] = NextBlock; |
| 140 | FreeBlocks.reset(Idx: NextBlock); |
| 141 | Block = FreeBlocks.find_next(Prev: Block); |
| 142 | } while (--NumBlocks > 0); |
| 143 | return Error::success(); |
| 144 | } |
| 145 | |
| 146 | uint32_t MSFBuilder::getNumUsedBlocks() const { |
| 147 | return getTotalBlockCount() - getNumFreeBlocks(); |
| 148 | } |
| 149 | |
| 150 | uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); } |
| 151 | |
| 152 | uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); } |
| 153 | |
| 154 | bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; } |
| 155 | |
| 156 | Expected<uint32_t> MSFBuilder::addStream(uint32_t Size, |
| 157 | ArrayRef<uint32_t> Blocks) { |
| 158 | // Add a new stream mapped to the specified blocks. Verify that the specified |
| 159 | // blocks are both necessary and sufficient for holding the requested number |
| 160 | // of bytes, and verify that all requested blocks are free. |
| 161 | uint32_t ReqBlocks = bytesToBlocks(NumBytes: Size, BlockSize); |
| 162 | if (ReqBlocks != Blocks.size()) |
| 163 | return make_error<MSFError>( |
| 164 | Args: msf_error_code::invalid_format, |
| 165 | Args: "Incorrect number of blocks for requested stream size" ); |
| 166 | for (auto Block : Blocks) { |
| 167 | if (Block >= FreeBlocks.size()) |
| 168 | FreeBlocks.resize(N: Block + 1, t: true); |
| 169 | |
| 170 | if (!FreeBlocks.test(Idx: Block)) |
| 171 | return make_error<MSFError>( |
| 172 | Args: msf_error_code::unspecified, |
| 173 | Args: "Attempt to re-use an already allocated block" ); |
| 174 | } |
| 175 | // Mark all the blocks occupied by the new stream as not free. |
| 176 | for (auto Block : Blocks) { |
| 177 | FreeBlocks.reset(Idx: Block); |
| 178 | } |
| 179 | StreamData.push_back(x: std::make_pair(x&: Size, y&: Blocks)); |
| 180 | return StreamData.size() - 1; |
| 181 | } |
| 182 | |
| 183 | Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) { |
| 184 | uint32_t ReqBlocks = bytesToBlocks(NumBytes: Size, BlockSize); |
| 185 | std::vector<uint32_t> NewBlocks; |
| 186 | NewBlocks.resize(new_size: ReqBlocks); |
| 187 | if (auto EC = allocateBlocks(NumBlocks: ReqBlocks, Blocks: NewBlocks)) |
| 188 | return std::move(EC); |
| 189 | StreamData.push_back(x: std::make_pair(x&: Size, y&: NewBlocks)); |
| 190 | return StreamData.size() - 1; |
| 191 | } |
| 192 | |
| 193 | Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) { |
| 194 | uint32_t OldSize = getStreamSize(StreamIdx: Idx); |
| 195 | if (OldSize == Size) |
| 196 | return Error::success(); |
| 197 | |
| 198 | uint32_t NewBlocks = bytesToBlocks(NumBytes: Size, BlockSize); |
| 199 | uint32_t OldBlocks = bytesToBlocks(NumBytes: OldSize, BlockSize); |
| 200 | |
| 201 | if (NewBlocks > OldBlocks) { |
| 202 | uint32_t AddedBlocks = NewBlocks - OldBlocks; |
| 203 | // If we're growing, we have to allocate new Blocks. |
| 204 | std::vector<uint32_t> AddedBlockList; |
| 205 | AddedBlockList.resize(new_size: AddedBlocks); |
| 206 | if (auto EC = allocateBlocks(NumBlocks: AddedBlocks, Blocks: AddedBlockList)) |
| 207 | return EC; |
| 208 | auto &CurrentBlocks = StreamData[Idx].second; |
| 209 | llvm::append_range(C&: CurrentBlocks, R&: AddedBlockList); |
| 210 | } else if (OldBlocks > NewBlocks) { |
| 211 | // For shrinking, free all the Blocks in the Block map, update the stream |
| 212 | // data, then shrink the directory. |
| 213 | uint32_t RemovedBlocks = OldBlocks - NewBlocks; |
| 214 | auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second); |
| 215 | auto RemovedBlockList = CurrentBlocks.drop_front(N: NewBlocks); |
| 216 | for (auto P : RemovedBlockList) |
| 217 | FreeBlocks[P] = true; |
| 218 | StreamData[Idx].second = CurrentBlocks.drop_back(N: RemovedBlocks); |
| 219 | } |
| 220 | |
| 221 | StreamData[Idx].first = Size; |
| 222 | return Error::success(); |
| 223 | } |
| 224 | |
| 225 | uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); } |
| 226 | |
| 227 | uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const { |
| 228 | return StreamData[StreamIdx].first; |
| 229 | } |
| 230 | |
| 231 | ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const { |
| 232 | return StreamData[StreamIdx].second; |
| 233 | } |
| 234 | |
| 235 | uint32_t MSFBuilder::computeDirectoryByteSize() const { |
| 236 | // The directory has the following layout, where each item is a ulittle32_t: |
| 237 | // NumStreams |
| 238 | // StreamSizes[NumStreams] |
| 239 | // StreamBlocks[NumStreams][] |
| 240 | uint32_t Size = sizeof(ulittle32_t); // NumStreams |
| 241 | Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes |
| 242 | for (const auto &D : StreamData) { |
| 243 | uint32_t ExpectedNumBlocks = bytesToBlocks(NumBytes: D.first, BlockSize); |
| 244 | assert(ExpectedNumBlocks == D.second.size() && |
| 245 | "Unexpected number of blocks" ); |
| 246 | Size += ExpectedNumBlocks * sizeof(ulittle32_t); |
| 247 | } |
| 248 | return Size; |
| 249 | } |
| 250 | |
| 251 | Expected<MSFLayout> MSFBuilder::generateLayout() { |
| 252 | llvm::TimeTraceScope timeScope("MSF: Generate layout" ); |
| 253 | |
| 254 | SuperBlock *SB = Allocator.Allocate<SuperBlock>(); |
| 255 | MSFLayout L; |
| 256 | L.SB = SB; |
| 257 | |
| 258 | std::memcpy(dest: SB->MagicBytes, src: Magic, n: sizeof(Magic)); |
| 259 | SB->BlockMapAddr = BlockMapAddr; |
| 260 | SB->BlockSize = BlockSize; |
| 261 | SB->NumDirectoryBytes = computeDirectoryByteSize(); |
| 262 | SB->FreeBlockMapBlock = FreePageMap; |
| 263 | SB->Unknown1 = Unknown1; |
| 264 | |
| 265 | uint32_t NumDirectoryBlocks = bytesToBlocks(NumBytes: SB->NumDirectoryBytes, BlockSize); |
| 266 | if (NumDirectoryBlocks > DirectoryBlocks.size()) { |
| 267 | // Our hint wasn't enough to satisfy the entire directory. Allocate |
| 268 | // remaining pages. |
| 269 | std::vector<uint32_t> ; |
| 270 | uint32_t = NumDirectoryBlocks - DirectoryBlocks.size(); |
| 271 | ExtraBlocks.resize(new_size: NumExtraBlocks); |
| 272 | if (auto EC = allocateBlocks(NumBlocks: NumExtraBlocks, Blocks: ExtraBlocks)) |
| 273 | return std::move(EC); |
| 274 | llvm::append_range(C&: DirectoryBlocks, R&: ExtraBlocks); |
| 275 | } else if (NumDirectoryBlocks < DirectoryBlocks.size()) { |
| 276 | uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks; |
| 277 | for (auto B : |
| 278 | ArrayRef<uint32_t>(DirectoryBlocks).drop_back(N: NumUnnecessaryBlocks)) |
| 279 | FreeBlocks[B] = true; |
| 280 | DirectoryBlocks.resize(new_size: NumDirectoryBlocks); |
| 281 | } |
| 282 | |
| 283 | // Don't set the number of blocks in the file until after allocating Blocks |
| 284 | // for the directory, since the allocation might cause the file to need to |
| 285 | // grow. |
| 286 | SB->NumBlocks = FreeBlocks.size(); |
| 287 | |
| 288 | ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(Num: NumDirectoryBlocks); |
| 289 | llvm::uninitialized_copy(Src&: DirectoryBlocks, Dst: DirBlocks); |
| 290 | L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks); |
| 291 | |
| 292 | // The stream sizes should be re-allocated as a stable pointer and the stream |
| 293 | // map should have each of its entries allocated as a separate stable pointer. |
| 294 | if (!StreamData.empty()) { |
| 295 | ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(Num: StreamData.size()); |
| 296 | L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size()); |
| 297 | L.StreamMap.resize(new_size: StreamData.size()); |
| 298 | for (uint32_t I = 0; I < StreamData.size(); ++I) { |
| 299 | Sizes[I] = StreamData[I].first; |
| 300 | ulittle32_t *BlockList = |
| 301 | Allocator.Allocate<ulittle32_t>(Num: StreamData[I].second.size()); |
| 302 | llvm::uninitialized_copy(Src&: StreamData[I].second, Dst: BlockList); |
| 303 | L.StreamMap[I] = |
| 304 | ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size()); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | L.FreePageMap = FreeBlocks; |
| 309 | |
| 310 | return L; |
| 311 | } |
| 312 | |
| 313 | static void commitFpm(WritableBinaryStream &MsfBuffer, const MSFLayout &Layout, |
| 314 | BumpPtrAllocator &Allocator) { |
| 315 | auto FpmStream = |
| 316 | WritableMappedBlockStream::createFpmStream(Layout, MsfData: MsfBuffer, Allocator); |
| 317 | |
| 318 | // We only need to create the alt fpm stream so that it gets initialized. |
| 319 | WritableMappedBlockStream::createFpmStream(Layout, MsfData: MsfBuffer, Allocator, |
| 320 | AltFpm: true); |
| 321 | |
| 322 | uint32_t BI = 0; |
| 323 | BinaryStreamWriter FpmWriter(*FpmStream); |
| 324 | while (BI < Layout.SB->NumBlocks) { |
| 325 | uint8_t ThisByte = 0; |
| 326 | for (uint32_t I = 0; I < 8; ++I) { |
| 327 | bool IsFree = |
| 328 | (BI < Layout.SB->NumBlocks) ? Layout.FreePageMap.test(Idx: BI) : true; |
| 329 | uint8_t Mask = uint8_t(IsFree) << I; |
| 330 | ThisByte |= Mask; |
| 331 | ++BI; |
| 332 | } |
| 333 | cantFail(Err: FpmWriter.writeObject(Obj: ThisByte)); |
| 334 | } |
| 335 | assert(FpmWriter.bytesRemaining() == 0); |
| 336 | } |
| 337 | |
| 338 | Expected<FileBufferByteStream> MSFBuilder::commit(StringRef Path, |
| 339 | MSFLayout &Layout) { |
| 340 | llvm::TimeTraceScope timeScope("Commit MSF" ); |
| 341 | |
| 342 | Expected<MSFLayout> L = generateLayout(); |
| 343 | if (!L) |
| 344 | return L.takeError(); |
| 345 | |
| 346 | Layout = std::move(*L); |
| 347 | |
| 348 | uint64_t FileSize = uint64_t(Layout.SB->BlockSize) * Layout.SB->NumBlocks; |
| 349 | // Ensure that the file size is under the limit for the specified block size. |
| 350 | if (FileSize > getMaxFileSizeFromBlockSize(Size: Layout.SB->BlockSize)) { |
| 351 | msf_error_code error_code = [](uint32_t BlockSize) { |
| 352 | switch (BlockSize) { |
| 353 | case 8192: |
| 354 | return msf_error_code::size_overflow_8192; |
| 355 | case 16384: |
| 356 | return msf_error_code::size_overflow_16384; |
| 357 | case 32768: |
| 358 | return msf_error_code::size_overflow_32768; |
| 359 | default: |
| 360 | return msf_error_code::size_overflow_4096; |
| 361 | } |
| 362 | }(Layout.SB->BlockSize); |
| 363 | |
| 364 | return make_error<MSFError>( |
| 365 | Args&: error_code, |
| 366 | Args: formatv(Fmt: "File size {0,1:N} too large for current PDB page size {1}" , |
| 367 | Vals&: FileSize, Vals: Layout.SB->BlockSize)); |
| 368 | } |
| 369 | |
| 370 | uint64_t NumDirectoryBlocks = |
| 371 | bytesToBlocks(NumBytes: Layout.SB->NumDirectoryBytes, BlockSize: Layout.SB->BlockSize); |
| 372 | uint64_t DirectoryBlockMapSize = |
| 373 | NumDirectoryBlocks * sizeof(support::ulittle32_t); |
| 374 | if (DirectoryBlockMapSize > Layout.SB->BlockSize) { |
| 375 | return make_error<MSFError>(Args: msf_error_code::stream_directory_overflow, |
| 376 | Args: formatv(Fmt: "The directory block map ({0} bytes) " |
| 377 | "doesn't fit in a block ({1} bytes)" , |
| 378 | Vals&: DirectoryBlockMapSize, |
| 379 | Vals: Layout.SB->BlockSize)); |
| 380 | } |
| 381 | |
| 382 | auto OutFileOrError = FileOutputBuffer::create(FilePath: Path, Size: FileSize); |
| 383 | if (auto EC = OutFileOrError.takeError()) |
| 384 | return std::move(EC); |
| 385 | |
| 386 | FileBufferByteStream Buffer(std::move(*OutFileOrError), |
| 387 | llvm::endianness::little); |
| 388 | BinaryStreamWriter Writer(Buffer); |
| 389 | |
| 390 | if (auto EC = Writer.writeObject(Obj: *Layout.SB)) |
| 391 | return std::move(EC); |
| 392 | |
| 393 | commitFpm(MsfBuffer&: Buffer, Layout, Allocator); |
| 394 | |
| 395 | uint32_t BlockMapOffset = |
| 396 | msf::blockToOffset(BlockNumber: Layout.SB->BlockMapAddr, BlockSize: Layout.SB->BlockSize); |
| 397 | Writer.setOffset(BlockMapOffset); |
| 398 | if (auto EC = Writer.writeArray(Array: Layout.DirectoryBlocks)) |
| 399 | return std::move(EC); |
| 400 | |
| 401 | auto DirStream = WritableMappedBlockStream::createDirectoryStream( |
| 402 | Layout, MsfData: Buffer, Allocator); |
| 403 | BinaryStreamWriter DW(*DirStream); |
| 404 | if (auto EC = DW.writeInteger<uint32_t>(Value: Layout.StreamSizes.size())) |
| 405 | return std::move(EC); |
| 406 | |
| 407 | if (auto EC = DW.writeArray(Array: Layout.StreamSizes)) |
| 408 | return std::move(EC); |
| 409 | |
| 410 | for (const auto &Blocks : Layout.StreamMap) { |
| 411 | if (auto EC = DW.writeArray(Array: Blocks)) |
| 412 | return std::move(EC); |
| 413 | } |
| 414 | |
| 415 | return std::move(Buffer); |
| 416 | } |
| 417 | |