[llvm] r328162 - [PDB] Don't ignore bucket 0 when writing the PDB string table.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 21 15:24:00 PDT 2018


Author: zturner
Date: Wed Mar 21 15:23:59 2018
New Revision: 328162

URL: http://llvm.org/viewvc/llvm-project?rev=328162&view=rev
Log:
[PDB] Don't ignore bucket 0 when writing the PDB string table.

The hash table is a list of buckets, and the *value* stored in
the bucket cannot be 0 since that is reserved.  However, the code
here was incorrectly skipping over the 0'th bucket entirely.
The 0'th bucket is perfectly fine, just none of these buckets
can contain the value 0.

As a result, whenever there was a string where hash(S) % Size
was equal to 0, we would write the value in the next bucket
instead.  We never caught this in our tests due to *another*
bug, which is that we would iterate the entire list of buckets
looking for the value, only using the hash value as a starting
point.  However, the real algorithm stops when it finds 0 in
a bucket since it takes that to mean "the item is not in the
hash table".

The unit test is updated to carefully construct a set of hash
values that will cause one item to hash to 0 mod bucket count,
and the reader is also updated to return an error indicating that
the item is not found when it encounters a 0 bucket.

Modified:
    llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTable.cpp
    llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp
    llvm/trunk/unittests/DebugInfo/PDB/StringTableBuilderTest.cpp

Modified: llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTable.cpp?rev=328162&r1=328161&r2=328162&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTable.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTable.cpp Wed Mar 21 15:23:59 2018
@@ -122,7 +122,10 @@ Expected<uint32_t> PDBStringTable::getID
     // we iterate the entire array.
     uint32_t Index = (Start + I) % Count;
 
+    // If we find 0, it means the item isn't in the hash table.
     uint32_t ID = IDs[Index];
+    if (ID == 0)
+      return make_error<RawError>(raw_error_code::no_entry);
     auto ExpectedStr = getStringForID(ID);
     if (!ExpectedStr)
       return ExpectedStr.takeError();

Modified: llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp?rev=328162&r1=328161&r2=328162&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp Wed Mar 21 15:23:59 2018
@@ -89,8 +89,6 @@ Error PDBStringTableBuilder::writeHashTa
 
     for (uint32_t I = 0; I != BucketCount; ++I) {
       uint32_t Slot = (Hash + I) % BucketCount;
-      if (Slot == 0)
-        continue; // Skip reserved slot
       if (Buckets[Slot] != 0)
         continue;
       Buckets[Slot] = Offset;

Modified: llvm/trunk/unittests/DebugInfo/PDB/StringTableBuilderTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/PDB/StringTableBuilderTest.cpp?rev=328162&r1=328161&r2=328162&view=diff
==============================================================================
--- llvm/trunk/unittests/DebugInfo/PDB/StringTableBuilderTest.cpp (original)
+++ llvm/trunk/unittests/DebugInfo/PDB/StringTableBuilderTest.cpp Wed Mar 21 15:23:59 2018
@@ -27,10 +27,29 @@ class StringTableBuilderTest : public ::
 TEST_F(StringTableBuilderTest, Simple) {
   // Create /names table contents.
   PDBStringTableBuilder Builder;
-  EXPECT_EQ(1U, Builder.insert("foo"));
-  EXPECT_EQ(5U, Builder.insert("bar"));
-  EXPECT_EQ(1U, Builder.insert("foo"));
-  EXPECT_EQ(9U, Builder.insert("baz"));
+
+  // This test case is carefully constructed to ensure that at least one
+  // string gets bucketed into slot 0, *and* to ensure that at least one
+  // has a hash collision at the end of the bucket list so it has to
+  // wrap around.
+  uint32_t FooID = Builder.insert("foo");
+  uint32_t BarID = Builder.insert("bar");
+  uint32_t BazID = Builder.insert("baz");
+  uint32_t BuzzID = Builder.insert("buzz");
+  uint32_t BazzID = Builder.insert("bazz");
+  uint32_t BarrID = Builder.insert("barr");
+
+  // Re-inserting the same item should return the same id.
+  EXPECT_EQ(FooID, Builder.insert("foo"));
+  EXPECT_EQ(BarID, Builder.insert("bar"));
+  EXPECT_EQ(BazID, Builder.insert("baz"));
+  EXPECT_EQ(BuzzID, Builder.insert("buzz"));
+  EXPECT_EQ(BazzID, Builder.insert("bazz"));
+  EXPECT_EQ(BarrID, Builder.insert("barr"));
+
+  // Each ID should be distinct.
+  std::set<uint32_t> Distinct{FooID, BarID, BazID, BuzzID, BazzID, BarrID};
+  EXPECT_EQ(6U, Distinct.size());
 
   std::vector<uint8_t> Buffer(Builder.calculateSerializedSize());
   MutableBinaryByteStream OutStream(Buffer, little);
@@ -43,13 +62,20 @@ TEST_F(StringTableBuilderTest, Simple) {
   PDBStringTable Table;
   EXPECT_THAT_ERROR(Table.reload(Reader), Succeeded());
 
-  EXPECT_EQ(3U, Table.getNameCount());
+  EXPECT_EQ(6U, Table.getNameCount());
   EXPECT_EQ(1U, Table.getHashVersion());
 
-  EXPECT_THAT_EXPECTED(Table.getStringForID(1), HasValue("foo"));
-  EXPECT_THAT_EXPECTED(Table.getStringForID(5), HasValue("bar"));
-  EXPECT_THAT_EXPECTED(Table.getStringForID(9), HasValue("baz"));
-  EXPECT_THAT_EXPECTED(Table.getIDForString("foo"), HasValue(1U));
-  EXPECT_THAT_EXPECTED(Table.getIDForString("bar"), HasValue(5U));
-  EXPECT_THAT_EXPECTED(Table.getIDForString("baz"), HasValue(9U));
+  EXPECT_THAT_EXPECTED(Table.getStringForID(FooID), HasValue("foo"));
+  EXPECT_THAT_EXPECTED(Table.getStringForID(BarID), HasValue("bar"));
+  EXPECT_THAT_EXPECTED(Table.getStringForID(BazID), HasValue("baz"));
+  EXPECT_THAT_EXPECTED(Table.getStringForID(BuzzID), HasValue("buzz"));
+  EXPECT_THAT_EXPECTED(Table.getStringForID(BazzID), HasValue("bazz"));
+  EXPECT_THAT_EXPECTED(Table.getStringForID(BarrID), HasValue("barr"));
+
+  EXPECT_THAT_EXPECTED(Table.getIDForString("foo"), HasValue(FooID));
+  EXPECT_THAT_EXPECTED(Table.getIDForString("bar"), HasValue(BarID));
+  EXPECT_THAT_EXPECTED(Table.getIDForString("baz"), HasValue(BazID));
+  EXPECT_THAT_EXPECTED(Table.getIDForString("buzz"), HasValue(BuzzID));
+  EXPECT_THAT_EXPECTED(Table.getIDForString("bazz"), HasValue(BazzID));
+  EXPECT_THAT_EXPECTED(Table.getIDForString("barr"), HasValue(BarrID));
 }




More information about the llvm-commits mailing list