LCOV - code coverage report
Current view: top level - lib/DebugInfo/MSF - MSFBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 152 178 85.4 %
Date: 2017-09-14 15:23:50 Functions: 16 19 84.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MSFBuilder.cpp -----------------------------------------------------===//
       2             : //
       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             : 
      10             : #include "llvm/ADT/ArrayRef.h"
      11             : #include "llvm/DebugInfo/MSF/MSFBuilder.h"
      12             : #include "llvm/DebugInfo/MSF/MSFError.h"
      13             : #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>
      22             : 
      23             : using namespace llvm;
      24             : using namespace llvm::msf;
      25             : using namespace llvm::support;
      26             : 
      27             : static const uint32_t kSuperBlockBlock = 0;
      28             : static const uint32_t kFreePageMap0Block = 1;
      29             : static const uint32_t kFreePageMap1Block = 2;
      30             : static const uint32_t kNumReservedPages = 3;
      31             : 
      32             : static const uint32_t kDefaultFreePageMap = kFreePageMap0Block;
      33             : static const uint32_t kDefaultBlockMapAddr = kNumReservedPages;
      34             : 
      35          75 : MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow,
      36          75 :                        BumpPtrAllocator &Allocator)
      37             :     : Allocator(Allocator), IsGrowable(CanGrow),
      38             :       FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize),
      39             :       MininumBlocks(MinBlockCount), BlockMapAddr(kDefaultBlockMapAddr),
      40         225 :       FreeBlocks(MinBlockCount, true) {
      41         225 :   FreeBlocks[kSuperBlockBlock] = false;
      42         225 :   FreeBlocks[kFreePageMap0Block] = false;
      43         225 :   FreeBlocks[kFreePageMap1Block] = false;
      44         225 :   FreeBlocks[BlockMapAddr] = false;
      45          75 : }
      46             : 
      47          75 : Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator,
      48             :                                         uint32_t BlockSize,
      49             :                                         uint32_t MinBlockCount, bool CanGrow) {
      50          75 :   if (!isValidBlockSize(BlockSize))
      51           0 :     return make_error<MSFError>(msf_error_code::invalid_format,
      52           0 :                                 "The requested block size is unsupported");
      53             : 
      54         225 :   return MSFBuilder(BlockSize,
      55         225 :                     std::max(MinBlockCount, msf::getMinimumBlockCount()),
      56          75 :                     CanGrow, Allocator);
      57             : }
      58             : 
      59           0 : Error MSFBuilder::setBlockMapAddr(uint32_t Addr) {
      60           0 :   if (Addr == BlockMapAddr)
      61           0 :     return Error::success();
      62             : 
      63           0 :   if (Addr >= FreeBlocks.size()) {
      64           0 :     if (!IsGrowable)
      65             :       return make_error<MSFError>(msf_error_code::insufficient_buffer,
      66           0 :                                   "Cannot grow the number of blocks");
      67           0 :     FreeBlocks.resize(Addr + 1, true);
      68             :   }
      69             : 
      70           0 :   if (!isBlockFree(Addr))
      71             :     return make_error<MSFError>(
      72             :         msf_error_code::block_in_use,
      73           0 :         "Requested block map address is already in use");
      74           0 :   FreeBlocks[BlockMapAddr] = true;
      75           0 :   FreeBlocks[Addr] = false;
      76           0 :   BlockMapAddr = Addr;
      77           0 :   return Error::success();
      78             : }
      79             : 
      80           0 : void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; }
      81             : 
      82           0 : void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; }
      83             : 
      84           3 : Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) {
      85          12 :   for (auto B : DirectoryBlocks)
      86           0 :     FreeBlocks[B] = true;
      87          10 :   for (auto B : DirBlocks) {
      88           4 :     if (!isBlockFree(B)) {
      89             :       return make_error<MSFError>(msf_error_code::unspecified,
      90           0 :                                   "Attempt to reuse an allocated block");
      91             :     }
      92          12 :     FreeBlocks[B] = false;
      93             :   }
      94             : 
      95           9 :   DirectoryBlocks = DirBlocks;
      96           9 :   return Error::success();
      97             : }
      98             : 
      99        1101 : Error MSFBuilder::allocateBlocks(uint32_t NumBlocks,
     100             :                                  MutableArrayRef<uint32_t> Blocks) {
     101        1101 :   if (NumBlocks == 0)
     102        1200 :     return Error::success();
     103             : 
     104         701 :   uint32_t NumFreeBlocks = FreeBlocks.count();
     105         701 :   if (NumFreeBlocks < NumBlocks) {
     106         701 :     if (!IsGrowable)
     107             :       return make_error<MSFError>(msf_error_code::insufficient_buffer,
     108           0 :                                   "There are no free Blocks in the file");
     109         701 :     uint32_t AllocBlocks = NumBlocks - NumFreeBlocks;
     110         701 :     uint32_t OldBlockCount = FreeBlocks.size();
     111         701 :     uint32_t NewBlockCount = AllocBlocks + OldBlockCount;
     112        1402 :     uint32_t NextFpmBlock = alignTo(OldBlockCount, BlockSize) + 1;
     113         701 :     FreeBlocks.resize(NewBlockCount, true);
     114             :     // If we crossed over an fpm page, we actually need to allocate 2 extra
     115             :     // blocks for each FPM group crossed and mark both blocks from the group as
     116             :     // used.  We may not actually use them since there are many more FPM blocks
     117             :     // present than are required to represent all blocks in a given PDB, but we
     118             :     // need to make sure they aren't allocated to a stream or something else.
     119             :     // At the end when committing the PDB, we'll go through and mark the
     120             :     // extraneous ones unused.
     121         707 :     while (NextFpmBlock < NewBlockCount) {
     122           3 :       NewBlockCount += 2;
     123           3 :       FreeBlocks.resize(NewBlockCount, true);
     124           3 :       FreeBlocks.reset(NextFpmBlock, NextFpmBlock + 2);
     125           3 :       NextFpmBlock += BlockSize;
     126             :     }
     127             :   }
     128             : 
     129         701 :   int I = 0;
     130        1402 :   int Block = FreeBlocks.find_first();
     131             :   do {
     132             :     assert(Block != -1 && "We ran out of Blocks!");
     133             : 
     134       15179 :     uint32_t NextBlock = static_cast<uint32_t>(Block);
     135       30358 :     Blocks[I++] = NextBlock;
     136       30358 :     FreeBlocks.reset(NextBlock);
     137       30358 :     Block = FreeBlocks.find_next(Block);
     138       15179 :   } while (--NumBlocks > 0);
     139        2103 :   return Error::success();
     140             : }
     141             : 
     142           4 : uint32_t MSFBuilder::getNumUsedBlocks() const {
     143           4 :   return getTotalBlockCount() - getNumFreeBlocks();
     144             : }
     145             : 
     146          16 : uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); }
     147             : 
     148           4 : uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); }
     149             : 
     150          46 : bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; }
     151             : 
     152           3 : Expected<uint32_t> MSFBuilder::addStream(uint32_t Size,
     153             :                                          ArrayRef<uint32_t> Blocks) {
     154             :   // Add a new stream mapped to the specified blocks.  Verify that the specified
     155             :   // blocks are both necessary and sufficient for holding the requested number
     156             :   // of bytes, and verify that all requested blocks are free.
     157           6 :   uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
     158           3 :   if (ReqBlocks != Blocks.size())
     159           0 :     return make_error<MSFError>(
     160             :         msf_error_code::invalid_format,
     161           0 :         "Incorrect number of blocks for requested stream size");
     162          16 :   for (auto Block : Blocks) {
     163          11 :     if (Block >= FreeBlocks.size())
     164           1 :       FreeBlocks.resize(Block + 1, true);
     165             : 
     166          22 :     if (!FreeBlocks.test(Block))
     167           3 :       return make_error<MSFError>(
     168             :           msf_error_code::unspecified,
     169           1 :           "Attempt to re-use an already allocated block");
     170             :   }
     171             :   // Mark all the blocks occupied by the new stream as not free.
     172          22 :   for (auto Block : Blocks) {
     173          20 :     FreeBlocks.reset(Block);
     174             :   }
     175          10 :   StreamData.push_back(std::make_pair(Size, Blocks));
     176           6 :   return StreamData.size() - 1;
     177             : }
     178             : 
     179         797 : Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) {
     180        1594 :   uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
     181        1594 :   std::vector<uint32_t> NewBlocks;
     182         797 :   NewBlocks.resize(ReqBlocks);
     183        2391 :   if (auto EC = allocateBlocks(ReqBlocks, NewBlocks))
     184           0 :     return std::move(EC);
     185        3188 :   StreamData.push_back(std::make_pair(Size, NewBlocks));
     186        2391 :   return StreamData.size() - 1;
     187             : }
     188             : 
     189         241 : Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) {
     190         241 :   uint32_t OldSize = getStreamSize(Idx);
     191         241 :   if (OldSize == Size)
     192           0 :     return Error::success();
     193             : 
     194         482 :   uint32_t NewBlocks = bytesToBlocks(Size, BlockSize);
     195         482 :   uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize);
     196             : 
     197         241 :   if (NewBlocks > OldBlocks) {
     198         238 :     uint32_t AddedBlocks = NewBlocks - OldBlocks;
     199             :     // If we're growing, we have to allocate new Blocks.
     200         476 :     std::vector<uint32_t> AddedBlockList;
     201         238 :     AddedBlockList.resize(AddedBlocks);
     202         714 :     if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList))
     203           0 :       return EC;
     204         476 :     auto &CurrentBlocks = StreamData[Idx].second;
     205         714 :     CurrentBlocks.insert(CurrentBlocks.end(), AddedBlockList.begin(),
     206         952 :                          AddedBlockList.end());
     207           3 :   } else if (OldBlocks > NewBlocks) {
     208             :     // For shrinking, free all the Blocks in the Block map, update the stream
     209             :     // data, then shrink the directory.
     210           1 :     uint32_t RemovedBlocks = OldBlocks - NewBlocks;
     211           3 :     auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second);
     212           2 :     auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks);
     213           3 :     for (auto P : RemovedBlockList)
     214           3 :       FreeBlocks[P] = true;
     215           6 :     StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks);
     216             :   }
     217             : 
     218         482 :   StreamData[Idx].first = Size;
     219         723 :   return Error::success();
     220             : }
     221             : 
     222           2 : uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); }
     223             : 
     224         250 : uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const {
     225         500 :   return StreamData[StreamIdx].first;
     226             : }
     227             : 
     228           9 : ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const {
     229          27 :   return StreamData[StreamIdx].second;
     230             : }
     231             : 
     232          68 : uint32_t MSFBuilder::computeDirectoryByteSize() const {
     233             :   // The directory has the following layout, where each item is a ulittle32_t:
     234             :   //    NumStreams
     235             :   //    StreamSizes[NumStreams]
     236             :   //    StreamBlocks[NumStreams][]
     237          68 :   uint32_t Size = sizeof(ulittle32_t);             // NumStreams
     238         136 :   Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes
     239        1062 :   for (const auto &D : StreamData) {
     240        1580 :     uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize);
     241             :     assert(ExpectedNumBlocks == D.second.size() &&
     242             :            "Unexpected number of blocks");
     243         790 :     Size += ExpectedNumBlocks * sizeof(ulittle32_t);
     244             :   }
     245          68 :   return Size;
     246             : }
     247             : 
     248             : static void finalizeFpmBlockStatus(uint32_t B, ArrayRef<ulittle32_t> &FpmBlocks,
     249             :                                    BitVector &Fpm) {
     250         216 :   if (FpmBlocks.empty() || FpmBlocks.front() != B) {
     251             :     Fpm.set(B);
     252             :     return;
     253             :   }
     254             : 
     255             :   // If the next block in the actual layout is this block, it should *not* be
     256             :   // free.
     257             :   assert(!Fpm.test(B));
     258          71 :   FpmBlocks = FpmBlocks.drop_front();
     259             : }
     260             : 
     261          68 : Expected<MSFLayout> MSFBuilder::build() {
     262         136 :   SuperBlock *SB = Allocator.Allocate<SuperBlock>();
     263         136 :   MSFLayout L;
     264          68 :   L.SB = SB;
     265             : 
     266          68 :   std::memcpy(SB->MagicBytes, Magic, sizeof(Magic));
     267         136 :   SB->BlockMapAddr = BlockMapAddr;
     268         136 :   SB->BlockSize = BlockSize;
     269         136 :   SB->NumDirectoryBytes = computeDirectoryByteSize();
     270         136 :   SB->FreeBlockMapBlock = FreePageMap;
     271         136 :   SB->Unknown1 = Unknown1;
     272             : 
     273         204 :   uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize);
     274         136 :   if (NumDirectoryBlocks > DirectoryBlocks.size()) {
     275             :     // Our hint wasn't enough to satisfy the entire directory.  Allocate
     276             :     // remaining pages.
     277         132 :     std::vector<uint32_t> ExtraBlocks;
     278          66 :     uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
     279          66 :     ExtraBlocks.resize(NumExtraBlocks);
     280         198 :     if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
     281           0 :       return std::move(EC);
     282         264 :     DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(),
     283         330 :                            ExtraBlocks.end());
     284           2 :   } else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
     285           1 :     uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
     286           2 :     for (auto B :
     287           4 :          ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks))
     288           3 :       FreeBlocks[B] = true;
     289           1 :     DirectoryBlocks.resize(NumDirectoryBlocks);
     290             :   }
     291             : 
     292             :   // Don't set the number of blocks in the file until after allocating Blocks
     293             :   // for the directory, since the allocation might cause the file to need to
     294             :   // grow.
     295         136 :   SB->NumBlocks = FreeBlocks.size();
     296             : 
     297         136 :   ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks);
     298         204 :   std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks,
     299             :                             DirBlocks);
     300          68 :   L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks);
     301             : 
     302             :   // The stream sizes should be re-allocated as a stable pointer and the stream
     303             :   // map should have each of its entries allocated as a separate stable pointer.
     304         136 :   if (!StreamData.empty()) {
     305         201 :     ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size());
     306         134 :     L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size());
     307          67 :     L.StreamMap.resize(StreamData.size());
     308        1714 :     for (uint32_t I = 0; I < StreamData.size(); ++I) {
     309        2370 :       Sizes[I] = StreamData[I].first;
     310             :       ulittle32_t *BlockList =
     311        2370 :           Allocator.Allocate<ulittle32_t>(StreamData[I].second.size());
     312        3160 :       std::uninitialized_copy_n(StreamData[I].second.begin(),
     313        1580 :                                 StreamData[I].second.size(), BlockList);
     314        1580 :       L.StreamMap[I] =
     315        1580 :           ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size());
     316             :     }
     317             :   }
     318             : 
     319             :   // FPM blocks occur in pairs at every `BlockLength` interval.  While blocks of
     320             :   // this form are reserved for FPM blocks, not all blocks of this form will
     321             :   // actually be needed for FPM data because there are more blocks of this form
     322             :   // than are required to represent a PDB file with a given number of blocks.
     323             :   // So we need to find out which blocks are *actually* going to be real FPM
     324             :   // blocks, then mark the reset of the reserved blocks as unallocated.
     325          68 :   MSFStreamLayout FpmLayout = msf::getFpmStreamLayout(L, true);
     326          68 :   auto FpmBlocks = makeArrayRef(FpmLayout.Blocks);
     327         278 :   for (uint32_t B = kFreePageMap0Block; B < SB->NumBlocks;
     328         142 :        B += msf::getFpmIntervalLength(L)) {
     329         142 :     finalizeFpmBlockStatus(B, FpmBlocks, FreeBlocks);
     330         142 :     finalizeFpmBlockStatus(B + 1, FpmBlocks, FreeBlocks);
     331             :   }
     332          68 :   L.FreePageMap = FreeBlocks;
     333             : 
     334          68 :   return L;
     335             : }

Generated by: LCOV version 1.13