llvm.org GIT mirror llvm / 67a3f7f
Bitcode: Include any strings added to the string table in the module hash. Differential Revision: https://reviews.llvm.org/D35037 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307286 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
6 changed file(s) with 61 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
112112
113113 /// True if a module hash record should be written.
114114 bool GenerateHash;
115
116 SHA1 Hasher;
115117
116118 /// If non-null, when GenerateHash is true, the resulting hash is written
117119 /// into ModHash. When GenerateHash is false, that specified value
174176
175177 private:
176178 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
179
180 size_t addToStrtab(StringRef Str);
177181
178182 void writeAttributeGroupTable();
179183 void writeAttributeTable();
946950 llvm_unreachable("Invalid unnamed_addr");
947951 }
948952
953 size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
954 if (GenerateHash)
955 Hasher.update(Str);
956 return StrtabBuilder.add(Str);
957 }
958
949959 void ModuleBitcodeWriter::writeComdats() {
950960 SmallVector Vals;
951961 for (const Comdat *C : VE.getComdats()) {
952962 // COMDAT: [strtab offset, strtab size, selection_kind]
953 Vals.push_back(StrtabBuilder.add(C->getName()));
963 Vals.push_back(addToStrtab(C->getName()));
954964 Vals.push_back(C->getName().size());
955965 Vals.push_back(getEncodedComdatSelectionKind(*C));
956966 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
11211131 // linkage, alignment, section, visibility, threadlocal,
11221132 // unnamed_addr, externally_initialized, dllstorageclass,
11231133 // comdat, attributes]
1124 Vals.push_back(StrtabBuilder.add(GV.getName()));
1134 Vals.push_back(addToStrtab(GV.getName()));
11251135 Vals.push_back(GV.getName().size());
11261136 Vals.push_back(VE.getTypeID(GV.getValueType()));
11271137 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
11601170 // linkage, paramattrs, alignment, section, visibility, gc,
11611171 // unnamed_addr, prologuedata, dllstorageclass, comdat,
11621172 // prefixdata, personalityfn]
1163 Vals.push_back(StrtabBuilder.add(F.getName()));
1173 Vals.push_back(addToStrtab(F.getName()));
11641174 Vals.push_back(F.getName().size());
11651175 Vals.push_back(VE.getTypeID(F.getFunctionType()));
11661176 Vals.push_back(F.getCallingConv());
11901200 for (const GlobalAlias &A : M.aliases()) {
11911201 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
11921202 // visibility, dllstorageclass, threadlocal, unnamed_addr]
1193 Vals.push_back(StrtabBuilder.add(A.getName()));
1203 Vals.push_back(addToStrtab(A.getName()));
11941204 Vals.push_back(A.getName().size());
11951205 Vals.push_back(VE.getTypeID(A.getValueType()));
11961206 Vals.push_back(A.getType()->getAddressSpace());
12091219 for (const GlobalIFunc &I : M.ifuncs()) {
12101220 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
12111221 // val#, linkage, visibility]
1212 Vals.push_back(StrtabBuilder.add(I.getName()));
1222 Vals.push_back(addToStrtab(I.getName()));
12131223 Vals.push_back(I.getName().size());
12141224 Vals.push_back(VE.getTypeID(I.getValueType()));
12151225 Vals.push_back(I.getType()->getAddressSpace());
36473657 // Emit the module's hash.
36483658 // MODULE_CODE_HASH: [5*i32]
36493659 if (GenerateHash) {
3650 SHA1 Hasher;
36513660 uint32_t Vals[5];
36523661 Hasher.update(ArrayRef((const uint8_t *)&(Buffer)[BlockStartPos],
36533662 Buffer.size() - BlockStartPos));
0 source_filename = "foo.c"
1
2 $com = comdat any
3
4 define void @main() comdat($com) {
5 call void @bar()
6 ret void
7 }
8
9 declare void @bar()
0 source_filename = "foo.c"
1
2 $dat = comdat any
3
4 define void @main() comdat($dat) {
5 call void @foo()
6 ret void
7 }
8
9 declare void @foo()
0 ; RUN: opt -module-hash %s -o - | llvm-bcanalyzer -dump | grep ' %t
1 ; RUN: opt -module-hash %S/Inputs/module-hash-strtab1.ll -o - | llvm-bcanalyzer -dump | grep '> %t
2 ; RUN: opt -module-hash %S/Inputs/module-hash-strtab2.ll -o - | llvm-bcanalyzer -dump | grep '> %t
3 ; RUN: sort %t | uniq | count 3
4
5 source_filename = "foo.c"
6
7 $com = comdat any
8
9 define void @main() comdat($com) {
10 call void @foo()
11 ret void
12 }
13
14 declare void @foo()
0 ; Check per module hash.
1 ; RUN: opt -module-hash %s -o - | llvm-bcanalyzer -dump | FileCheck %s --check-prefix=MOD1
1 ; RUN: opt -module-hash %s -o - | llvm-bcanalyzer -dump -check-hash=foo | FileCheck %s --check-prefix=MOD1
22 ; MOD1:
3 ; RUN: opt -module-hash %p/Inputs/module_hash.ll -o - | llvm-bcanalyzer -dump | FileCheck %s --check-prefix=MOD2
3 ; RUN: opt -module-hash %p/Inputs/module_hash.ll -o - | llvm-bcanalyzer -dump -check-hash=bar | FileCheck %s --check-prefix=MOD2
44 ; MOD2:
55
66 ; Check that the hash matches in the combined index.
2020 ; RUN: cat %t.hash | FileCheck %s --check-prefix=COMBINED
2121
2222 ; First capture the value of the hash for the two modules.
23 ; COMBINED:
24 ; COMBINED: 2_1:[0-9]*]] op1=[[HASH2_2:[0-9]*]] op2=[[HASH2_3:[0-9]*]] op3=[[HASH2_4:[0-9]*]] op4=[[HASH2_5:[0-9]*]] (match)/>
23 ; COMBINED: 1_1:[0-9]*]] op1=[[HASH1_2:[0-9]*]] op2=[[HASH1_3:[0-9]*]] op3=[[HASH1_4:[0-9]*]] op4=[[HASH1_5:[0-9]*]]/>
24 ; COMBINED:
2525
2626 ; Validate against the value extracted from the combined index
2727 ; COMBINED-DAG:
6969 static cl::opt
7070 ShowBinaryBlobs("show-binary-blobs",
7171 cl::desc("Print binary blobs using hex escapes"));
72
73 static cl::opt CheckHash(
74 "check-hash",
75 cl::desc("Check module hash using the argument as a string table"));
7276
7377 namespace {
7478
651655 }
652656
653657 // If we found a module hash, let's verify that it matches!
654 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH) {
658 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH &&
659 !CheckHash.empty()) {
655660 if (Record.size() != 5)
656661 outs() << " (invalid)";
657662 else {
658663 // Recompute the hash and compare it to the one in the bitcode
659664 SHA1 Hasher;
660665 StringRef Hash;
666 Hasher.update(CheckHash);
661667 {
662668 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
663669 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);