1 //===- MSFBuilderTest.cpp Tests manipulation of MSF stream metadata ------===//
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/DebugInfo/MSF/MSFCommon.h"
11 #include "llvm/Testing/Support/Error.h"
12
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16 using namespace llvm::msf;
17
TEST(MSFCommonTest,BytesToBlocks)18 TEST(MSFCommonTest, BytesToBlocks) {
19 EXPECT_EQ(0ULL, bytesToBlocks(0, 4096));
20 EXPECT_EQ(1ULL, bytesToBlocks(12, 4096));
21 EXPECT_EQ(1ULL, bytesToBlocks(4096, 4096));
22 EXPECT_EQ(2ULL, bytesToBlocks(4097, 4096));
23 EXPECT_EQ(2ULL, bytesToBlocks(600, 512));
24 }
25
TEST(MSFCommonTest,FpmIntervals)26 TEST(MSFCommonTest, FpmIntervals) {
27 SuperBlock SB;
28 SB.FreeBlockMapBlock = 1;
29 SB.BlockSize = 4096;
30
31 MSFLayout L;
32 L.SB = &SB;
33
34 SB.NumBlocks = 12;
35 EXPECT_EQ(1u, getNumFpmIntervals(L, false));
36 SB.NumBlocks = SB.BlockSize;
37 EXPECT_EQ(1u, getNumFpmIntervals(L, false));
38 SB.NumBlocks = SB.BlockSize + 1;
39 EXPECT_EQ(1u, getNumFpmIntervals(L, false));
40 SB.NumBlocks = SB.BlockSize * 8;
41 EXPECT_EQ(1u, getNumFpmIntervals(L, false));
42 SB.NumBlocks = SB.BlockSize * 8 + 1;
43 EXPECT_EQ(2u, getNumFpmIntervals(L, false));
44
45 SB.NumBlocks = 12;
46 EXPECT_EQ(1u, getNumFpmIntervals(L, true));
47 SB.NumBlocks = SB.BlockSize;
48 EXPECT_EQ(1u, getNumFpmIntervals(L, true));
49 SB.NumBlocks = SB.BlockSize * 8;
50 EXPECT_EQ(8u, getNumFpmIntervals(L, true));
51
52 // The FPM is going to look like this:
53 // | 0 | 1 | 2 | ... | 4096 | 4097 | 4098 | ... |
54 // | SB | FPM0 | FPM1 | Data | Data | FPM0 | FPM1 | ... |
55 //
56 // So when there are up to 4097 blocks (last index 4096), the final blocks
57 // are data blocks. When there are 4098 blocks (last index 4097), there is
58 // one terminating FPM block, and when there are 4099 blocks, there are two
59 // terminating FPM blocks. Make sure all these cases are handled.
60
61 // With 4096 or 4097 blocks, the last block is a data block so we only have
62 // 1 interval.
63 for (uint32_t I : {4096, 4097}) {
64 // 1 FPM0 interval
65 EXPECT_EQ(1U, getNumFpmIntervals(4096, I, true, 1));
66 EXPECT_EQ(1U, getNumFpmIntervals(4096, I, false, 1));
67
68 // 1 FPM1 interval
69 EXPECT_EQ(1U, getNumFpmIntervals(4096, I, true, 2));
70 EXPECT_EQ(1U, getNumFpmIntervals(4096, I, false, 2));
71 }
72
73 // With 4098 blocks, the last block belongs to FPM0 so we should have 2 FPM0
74 // intervals.
75 EXPECT_EQ(2U, getNumFpmIntervals(4096, 4098, true, 1));
76 EXPECT_EQ(1U, getNumFpmIntervals(4096, 4098, false, 1));
77
78 // And 1 FPM1 interval.
79 EXPECT_EQ(1U, getNumFpmIntervals(4096, 4098, true, 2));
80 EXPECT_EQ(1U, getNumFpmIntervals(4096, 4098, false, 2));
81
82 // With 4099 blocks, the last block belongs to FPM1 so we should have 2
83 // FPM0 intervals.
84 EXPECT_EQ(2U, getNumFpmIntervals(4096, 4099, true, 1));
85 EXPECT_EQ(1U, getNumFpmIntervals(4096, 4099, false, 1));
86
87 // And 2 FPM1 intervals.
88 EXPECT_EQ(2U, getNumFpmIntervals(4096, 4099, true, 2));
89 EXPECT_EQ(1U, getNumFpmIntervals(4096, 4099, false, 2));
90 }
91
TEST(MSFCommonTest,FpmStreamLayout)92 TEST(MSFCommonTest, FpmStreamLayout) {
93 SuperBlock SB;
94 MSFLayout L;
95 L.SB = &SB;
96 SB.FreeBlockMapBlock = 1;
97
98 // Each FPM block has 4096 bytes for a maximum of 4096*8 allocation states.
99 SB.BlockSize = 4096;
100
101 // 1. When we're not including unused FPM data, the length of the FPM stream
102 // should be only long enough to contain 1 bit for each block.
103
104 // 1a. When the PDB has <= 4096*8 blocks, there should only be one FPM block.
105 SB.NumBlocks = 8000;
106 MSFStreamLayout SL = getFpmStreamLayout(L, false, false);
107 EXPECT_EQ(1000u, SL.Length);
108 EXPECT_EQ(1u, SL.Blocks.size());
109 EXPECT_EQ(SB.FreeBlockMapBlock, SL.Blocks.front());
110
111 SL = getFpmStreamLayout(L, false, true);
112 EXPECT_EQ(1000u, SL.Length);
113 EXPECT_EQ(1u, SL.Blocks.size());
114 EXPECT_EQ(3u - SB.FreeBlockMapBlock, SL.Blocks.front());
115
116 // 1b. When the PDB has > 4096*8 blocks, there should be multiple FPM blocks.
117 SB.NumBlocks = SB.BlockSize * 8 + 1;
118 SL = getFpmStreamLayout(L, false, false);
119 EXPECT_EQ(SB.BlockSize + 1, SL.Length);
120 EXPECT_EQ(2u, SL.Blocks.size());
121 EXPECT_EQ(SB.FreeBlockMapBlock, SL.Blocks[0]);
122 EXPECT_EQ(SB.FreeBlockMapBlock + SB.BlockSize, SL.Blocks[1]);
123
124 SL = getFpmStreamLayout(L, false, true);
125 EXPECT_EQ(SB.BlockSize + 1, SL.Length);
126 EXPECT_EQ(2u, SL.Blocks.size());
127 EXPECT_EQ(3u - SB.FreeBlockMapBlock, SL.Blocks[0]);
128 EXPECT_EQ(3u - SB.FreeBlockMapBlock + SB.BlockSize, SL.Blocks[1]);
129
130 // 2. When we are including unused FPM data, there should be one FPM block
131 // at every BlockSize interval in the file, even if entire FPM blocks are
132 // unused.
133 SB.NumBlocks = SB.BlockSize * 8 + 3;
134 SL = getFpmStreamLayout(L, true, false);
135 EXPECT_EQ(SB.BlockSize * 9, SL.Length);
136 EXPECT_EQ(9u, SL.Blocks.size());
137 for (int I = 0; I < 9; ++I)
138 EXPECT_EQ(I * SB.BlockSize + SB.FreeBlockMapBlock, SL.Blocks[I]);
139 }
140