• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/DebugInfo/CodeView/TypeHashingTest.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/CodeView/TypeHashing.h"
10 #include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h"
11 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
12 
13 #include "gtest/gtest.h"
14 
15 using namespace llvm;
16 using namespace llvm::codeview;
17 
createPointerRecord(AppendingTypeTableBuilder & Builder,TypeIndex TI)18 static TypeIndex createPointerRecord(AppendingTypeTableBuilder &Builder,
19                                      TypeIndex TI) {
20   PointerRecord PR(TypeRecordKind::Pointer);
21   PR.setAttrs(PointerKind::Near32, PointerMode::Pointer, PointerOptions::None,
22               4);
23   PR.ReferentType = TI;
24   return Builder.writeLeafType(PR);
25 }
26 
createArgListRecord(AppendingTypeTableBuilder & Builder,TypeIndex Q,TypeIndex R)27 static TypeIndex createArgListRecord(AppendingTypeTableBuilder &Builder,
28                                      TypeIndex Q, TypeIndex R) {
29   ArgListRecord AR(TypeRecordKind::ArgList);
30   AR.ArgIndices.push_back(Q);
31   AR.ArgIndices.push_back(R);
32   return Builder.writeLeafType(AR);
33 }
34 
createProcedureRecord(AppendingTypeTableBuilder & Builder,uint32_t ParamCount,TypeIndex Return,TypeIndex ArgList)35 static TypeIndex createProcedureRecord(AppendingTypeTableBuilder &Builder,
36                                        uint32_t ParamCount, TypeIndex Return,
37                                        TypeIndex ArgList) {
38   ProcedureRecord PR(TypeRecordKind::Procedure);
39   PR.ArgumentList = ArgList;
40   PR.CallConv = CallingConvention::NearC;
41   PR.Options = FunctionOptions::None;
42   PR.ParameterCount = ParamCount;
43   PR.ReturnType = Return;
44   return Builder.writeLeafType(PR);
45 }
46 
hash_of(ArrayRef<GloballyHashedType> Hashes,TypeIndex TI)47 static ArrayRef<uint8_t> hash_of(ArrayRef<GloballyHashedType> Hashes,
48                                  TypeIndex TI) {
49   return Hashes[TI.toArrayIndex()].Hash;
50 }
51 
verifyHashUniqueness(ArrayRef<GloballyHashedType> Hashes)52 static void verifyHashUniqueness(ArrayRef<GloballyHashedType> Hashes) {
53   assert(!Hashes.empty());
54 
55   for (size_t I = 0; I < Hashes.size() - 1; ++I) {
56     for (size_t J = I + 1; J < Hashes.size(); ++J) {
57       EXPECT_NE(Hashes[I].Hash, Hashes[J].Hash);
58     }
59   }
60 }
61 
TEST(TypeHashingTest,ContentHash)62 TEST(TypeHashingTest, ContentHash) {
63   SimpleTypeSerializer Serializer;
64 
65   TypeIndex CharStar(SimpleTypeKind::SignedCharacter,
66                      SimpleTypeMode::NearPointer32);
67 
68   BumpPtrAllocator Alloc;
69   AppendingTypeTableBuilder Ordering1(Alloc);
70   AppendingTypeTableBuilder Ordering2(Alloc);
71 
72   TypeIndex CharP(SimpleTypeKind::SignedCharacter, SimpleTypeMode::NearPointer);
73   TypeIndex IntP(SimpleTypeKind::Int32, SimpleTypeMode::NearPointer);
74   TypeIndex DoubleP(SimpleTypeKind::Float64, SimpleTypeMode::NearPointer);
75 
76   // We're going to the same type sequence with two different orderings, and
77   // then confirm all records are hashed the same.
78 
79   TypeIndex CharPP[2];
80   TypeIndex IntPP[2];
81   TypeIndex IntPPP[2];
82   TypeIndex DoublePP[2];
83   TypeIndex Args[2];
84   TypeIndex Proc[2];
85 
86   // Ordering 1
87   // ----------------------------------------
88   // LF_POINTER             0x1000   {char**}
89   //   Referent = char*
90   // LF_POINTER             0x1001   {int**}
91   //   Referent = int*
92   // LF_POINTER             0x1002   {int***}
93   //   Referent = 0x1001
94   // LF_ARGLIST             0x1003   {(char**, int***)}
95   //   Arg[0] = 0x1000
96   //   Arg[1] = 0x1002
97   // LF_PROCEDURE           0x1004   {int** func(char**, int***)}
98   //   ArgList = 0x1003
99   //   ReturnType = 0x1001
100   std::vector<GloballyHashedType> Ordering1Hashes;
101   CharPP[0] = createPointerRecord(Ordering1, CharP);
102   IntPP[0] = createPointerRecord(Ordering1, IntP);
103   IntPPP[0] = createPointerRecord(Ordering1, IntPP[0]);
104   Args[0] = createArgListRecord(Ordering1, CharPP[0], IntPPP[0]);
105   Proc[0] = createProcedureRecord(Ordering1, 2, IntPP[0], Args[0]);
106 
107   ASSERT_EQ(0x1000U, CharPP[0].getIndex());
108   ASSERT_EQ(0x1001U, IntPP[0].getIndex());
109   ASSERT_EQ(0x1002U, IntPPP[0].getIndex());
110   ASSERT_EQ(0x1003U, Args[0].getIndex());
111   ASSERT_EQ(0x1004U, Proc[0].getIndex());
112 
113   auto Hashes1 = GloballyHashedType::hashTypes(Ordering1.records());
114 
115   // Ordering 2
116   // ----------------------------------------
117   // LF_POINTER             0x1000   {int**}
118   //   Referent = int*
119   // LF_POINTER             0x1001   {int***}
120   //   Referent = 0x1000
121   // LF_POINTER             0x1002   {char**}
122   //   Referent = char*
123   // LF_POINTER             0x1003   {double**}
124   //   Referent = double*
125   // LF_ARGLIST             0x1004   {(char**, int***)}
126   //   Arg[0] = 0x1002
127   //   Arg[1] = 0x1001
128   // LF_PROCEDURE           0x1005   {int** func(char**, int***)}
129   //   ArgList = 0x1004
130   //   ReturnType = 0x1000
131   IntPP[1] = createPointerRecord(Ordering2, IntP);
132   IntPPP[1] = createPointerRecord(Ordering2, IntPP[1]);
133   CharPP[1] = createPointerRecord(Ordering2, CharP);
134   DoublePP[1] = createPointerRecord(Ordering2, DoubleP);
135   Args[1] = createArgListRecord(Ordering2, CharPP[1], IntPPP[1]);
136   Proc[1] = createProcedureRecord(Ordering2, 2, IntPP[1], Args[1]);
137   auto Hashes2 = GloballyHashedType::hashTypes(Ordering2.records());
138 
139   ASSERT_EQ(0x1000U, IntPP[1].getIndex());
140   ASSERT_EQ(0x1001U, IntPPP[1].getIndex());
141   ASSERT_EQ(0x1002U, CharPP[1].getIndex());
142   ASSERT_EQ(0x1003U, DoublePP[1].getIndex());
143   ASSERT_EQ(0x1004U, Args[1].getIndex());
144   ASSERT_EQ(0x1005U, Proc[1].getIndex());
145 
146   // Sanity check to make sure all same-ordering hashes are different
147   // from each other.
148   verifyHashUniqueness(Hashes1);
149   verifyHashUniqueness(Hashes2);
150 
151   EXPECT_EQ(hash_of(Hashes1, IntPP[0]), hash_of(Hashes2, IntPP[1]));
152   EXPECT_EQ(hash_of(Hashes1, IntPPP[0]), hash_of(Hashes2, IntPPP[1]));
153   EXPECT_EQ(hash_of(Hashes1, CharPP[0]), hash_of(Hashes2, CharPP[1]));
154   EXPECT_EQ(hash_of(Hashes1, Args[0]), hash_of(Hashes2, Args[1]));
155   EXPECT_EQ(hash_of(Hashes1, Proc[0]), hash_of(Hashes2, Proc[1]));
156 }
157