llvm.org GIT mirror llvm / 033bb22
[PDB] Fix hash function used to write /src/headerblock lld-link used to write PDB files that DIA couldn't recover natvis files from if: - The global strings table was > 64kiB - There were at least 3 natvis files The cause was that the hash function for the /src/headerblock stream was incorrect: It needs to be truncated to 16 bit. If the global strings table was <= 64kiB, truncating to 16 bit is a no-op, so this wasn't needed for small programs. If there are only 1 or 2 natvis files, then the growth strategy in HashTable::grow() would mean the hash table would have 2 buckets (for 1 natvis file) or 4 buckets (for 4 natvis files), and since the hash function is used modulo number of buckets, and since 2 and 4 divide 0x10000, the missing `% 0x10000` is a no-op there too. For 3 natvis files, the hash table grows to 6 buckets, which has a factor that's not common with 0x10000 and the difference starts to matter. Fixes PR41626. Differential Revision: https://reviews.llvm.org/D61277 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359515 91177308-0d34-0410-b5e6-96231b3b80d8 Nico Weber 1 year, 7 months ago
4 changed file(s) with 28 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
142142 return EC;
143143 if (Present.intersects(Deleted))
144144 return make_error(raw_error_code::corrupt_file,
145 "Present bit vector interesects deleted!");
145 "Present bit vector intersects deleted!");
146146
147147 for (uint32_t P : Present) {
148148 if (auto EC = Stream.readInteger(Buckets[P].first))
3333 // Here, the type HASH is a typedef of unsigned short.
3434 // ** It is not a bug that we truncate the result of hashStringV1, in fact
3535 // it is a bug if we do not! **
36 // See NMTNI::hash() in the reference implementation.
3637 return static_cast(hashStringV1(S));
3738 }
3839
2525 : Table(&Table) {}
2626
2727 uint32_t StringTableHashTraits::hashLookupKey(StringRef S) const {
28 return Table->getIdForString(S);
28 // The reference implementation doesn't include code for /src/headerblock
29 // handling, but it can only read natvis entries lld's PDB files if
30 // this hash function truncates the hash to 16 bit.
31 // PDB/include/misc.h in the reference implementation has a hashSz() function
32 // that returns an unsigned short, that seems what's being used for
33 // /src/headerblock.
34 return static_cast(Table->getIdForString(S));
2935 }
3036
3137 StringRef StringTableHashTraits::storageKeyToLookupKey(uint32_t Offset) const {
1818 using namespace llvm::pdb;
1919 using namespace llvm::support;
2020
21 namespace {
22 class StringTableBuilderTest : public ::testing::Test {};
23 }
24
25 TEST_F(StringTableBuilderTest, Simple) {
21 TEST(StringTableBuilderTest, Simple) {
2622 // Create /names table contents.
2723 PDBStringTableBuilder Builder;
2824
7773 EXPECT_THAT_EXPECTED(Table.getIDForString("bazz"), HasValue(BazzID));
7874 EXPECT_THAT_EXPECTED(Table.getIDForString("barr"), HasValue(BarrID));
7975 }
76
77 TEST(StringTableHashTraitsTest, Simple) {
78 PDBStringTableBuilder Builder;
79
80 // Create more than 64kiB of dummy entries.
81 for (int i = 0; i < 320; ++i) {
82 std::string aaaaa = std::string(220, 'a') + std::to_string(i);
83 Builder.insert(aaaaa);
84 }
85
86 std::string S = "foo.natvis";
87 uint32_t Pos = Builder.insert(S);
88
89 EXPECT_GT(Pos, 0xFFFFu);
90
91 StringTableHashTraits Traits(Builder);
92 EXPECT_LE(Traits.hashLookupKey(S), 0xFFFFu);
93 }