llvm.org GIT mirror llvm / 1fc660f
Revert "Add support for computing SHA1 in LLVM" This reverts commit r265096, r265095, and r265094. Windows build is broken, and the validation does not pass. From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265102 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
18 changed file(s) with 39 addition(s) and 598 deletion(s). Raw diff Collapse all Expand all
445445 using SimpleBitstreamCursor::canSkipToPos;
446446 using SimpleBitstreamCursor::AtEndOfStream;
447447 using SimpleBitstreamCursor::GetCurrentBitNo;
448 using SimpleBitstreamCursor::getCurrentByteNo;
449 using SimpleBitstreamCursor::getPointerToByte;
450448 using SimpleBitstreamCursor::getBitStreamReader;
451449 using SimpleBitstreamCursor::JumpToBit;
452450 using SimpleBitstreamCursor::fillCurWord;
106106
107107 // SOURCE_FILENAME: [namechar x N]
108108 MODULE_CODE_SOURCE_FILENAME = 16,
109
110 // HASH: [5*i32]
111 MODULE_CODE_HASH = 17,
112109 };
113110
114111 /// PARAMATTR blocks have code for defining a parameter attribute set.
185182 // The module path symbol table only has one code (MST_CODE_ENTRY).
186183 enum ModulePathSymtabCodes {
187184 MST_CODE_ENTRY = 1, // MST_ENTRY: [modid, namechar x N]
188 MST_CODE_HASH = 2, // MST_HASH: [5*i32]
189185 };
190186
191187 // The summary section uses different codes in the per-module
106106 /// for use in ThinLTO optimization).
107107 void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
108108 bool ShouldPreserveUseListOrder = false,
109 bool EmitSummaryIndex = false,
110 bool GenerateHash = false);
109 bool EmitSummaryIndex = false);
111110
112111 /// Write the specified module summary index to the given raw output stream,
113112 /// where it will be written in a new bitcode block. This is used when
2323 #include "llvm/IR/Module.h"
2424 #include "llvm/Support/MemoryBuffer.h"
2525 #include "llvm/Support/raw_ostream.h"
26
27 #include
2826
2927 namespace llvm {
3028
229227 void setBitcodeIndex(uint64_t Offset) { BitcodeIndex = Offset; }
230228 };
231229
232 /// 160 bits SHA1
233 typedef std::array ModuleHash;
234
235230 /// List of global value info structures for a particular value held
236231 /// in the GlobalValueMap. Requires a vector in the case of multiple
237232 /// COMDAT values of the same name.
249244 typedef GlobalValueInfoMapTy::iterator globalvalueinfo_iterator;
250245
251246 /// String table to hold/own module path strings, which additionally holds the
252 /// module ID assigned to each module during the plugin step, as well as a hash
253 /// of the module. The StringMap makes a copy of and owns inserted strings.
254 typedef StringMap> ModulePathStringTableTy;
247 /// module ID assigned to each module during the plugin step. The StringMap
248 /// makes a copy of and owns inserted strings.
249 typedef StringMap ModulePathStringTableTy;
255250
256251 /// Class to hold module path string table and global value map,
257252 /// and encapsulate methods for operating on them.
308303 GlobalValueMap[ValueGUID].push_back(std::move(Info));
309304 }
310305
311 /// Table of modules, containing module hash and id.
312 const StringMap> &modulePaths() const {
306 /// Table of modules, containing an id.
307 const StringMap &modulePaths() const {
313308 return ModulePathStringTable;
314309 }
315310
316 /// Table of modules, containing hash and id.
317 StringMap> &modulePaths() {
318 return ModulePathStringTable;
319 }
311 /// Table of modules, containing an id.
312 StringMap &modulePaths() { return ModulePathStringTable; }
320313
321314 /// Get the module ID recorded for the given module path.
322315 uint64_t getModuleId(const StringRef ModPath) const {
323 return ModulePathStringTable.lookup(ModPath).first;
324 }
325
326 /// Get the module SHA1 hash recorded for the given module path.
327 const ModuleHash &getModuleHash(const StringRef ModPath) const {
328 auto It = ModulePathStringTable.find(ModPath);
329 assert(It != ModulePathStringTable.end() && "Module not registered");
330 return It->second.second;
316 return ModulePathStringTable.lookup(ModPath);
331317 }
332318
333319 /// Add the given per-module index into this module index/summary,
346332 return NewName.str();
347333 }
348334
349 /// Add a new module path with the given \p Hash, mapped to the given \p
350 /// ModID, and return an iterator to the entry in the index.
351 ModulePathStringTableTy::iterator
352 addModulePath(StringRef ModPath, uint64_t ModId,
353 ModuleHash Hash = ModuleHash{{0}}) {
354 return ModulePathStringTable.insert(std::make_pair(
355 ModPath,
356 std::make_pair(ModId, Hash))).first;
335 /// Add a new module path, mapped to the given module Id, and return StringRef
336 /// owned by string table map.
337 StringRef addModulePath(StringRef ModPath, uint64_t ModId) {
338 return ModulePathStringTable.insert(std::make_pair(ModPath, ModId))
339 .first->first();
357340 }
358341
359342 /// Check if the given Module has any functions available for exporting
+0
-73
include/llvm/Support/SHA1.h less more
None //==- SHA1.h - SHA1 implementation for LLVM --*- C++ -*-==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 // This code is taken from public domain
9 // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)
10 // and modified by wrapping it in a C++ interface for LLVM,
11 // and removing unnecessary code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_SHA1_H
16 #define LLVM_SUPPORT_SHA1_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20
21 #include
22
23 namespace llvm {
24
25 /// A class that wrap the SHA1 algorithm.
26 class SHA1 {
27 public:
28 SHA1() { init(); }
29
30 /// Reinitialize the internal state
31 void init();
32
33 /// Digest more data.
34 void update(ArrayRef Data);
35
36 /// Return a reference to the current raw 160-bits SHA1 for the digested data
37 /// since the last call to init(). This call will add data to the internal
38 /// state and as such is not suited for getting an intermediate result
39 /// (see result()).
40 StringRef final();
41
42 /// Return a reference to the current raw 160-bits SHA1 for the digested data
43 /// since the last call to init(). This is suitable for getting the SHA1 at
44 /// any time without invalidating the internal state so that more calls can be
45 /// made into update.
46 StringRef result();
47
48 private:
49 static constexpr int BLOCK_LENGTH = 64;
50 static constexpr int HASH_LENGTH = 20;
51
52 // Internal State
53 struct {
54 uint32_t Buffer[BLOCK_LENGTH / 4];
55 uint32_t State[HASH_LENGTH / 4];
56 uint32_t ByteCount;
57 uint8_t BufferOffset;
58 } InternalState;
59
60 // Internal copy of the hash, populated and accessed on calls to result()
61 uint32_t HashResult[HASH_LENGTH / 4];
62
63 // Helper
64 void writebyte(uint8_t data);
65 void hashBlock();
66 void addUncounted(uint8_t data);
67 void pad();
68 };
69
70 } // end llvm namespace
71
72 #endif
+0
-46
include/llvm/Support/raw_sha1_ostream.h less more
None //==- raw_sha1_ostream.h - raw_ostream that compute SHA1 --*- C++ -*-==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the raw_sha1_ostream class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SUPPORT_RAW_SHA1_OSTREAM_H
14 #define LLVM_SUPPORT_RAW_SHA1_OSTREAM_H
15
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/Support/SHA1.h"
18
19 namespace llvm {
20
21 /// A raw_ostream that hash the content using the sha1 algorithm.
22 class raw_sha1_ostream : public raw_ostream {
23 SHA1 State;
24
25 /// See raw_ostream::write_impl.
26 void write_impl(const char *Ptr, size_t Size) override {
27 State.update(ArrayRef((uint8_t *)Ptr, Size));
28 }
29
30 public:
31 /// Return the current SHA1 hash for the content of the stream
32 StringRef sha1() {
33 flush();
34 return State.result();
35 }
36
37 /// Reset the internal state to start over from scratch.
38 void resetHash() { State.init(); }
39
40 uint64_t current_pos() const override { return 0; }
41 };
42
43 } // end llvm namespace
44
45 #endif
56315631 }
56325632 continue;
56335633
5634 case BitstreamEntry::Record: {
5634 case BitstreamEntry::Record:
5635 // Once we find the last record of interest, skip the rest.
5636 if (VSTOffset > 0)
5637 Stream.skipRecord(Entry.ID);
5638 else {
56355639 Record.clear();
56365640 auto BitCode = Stream.readRecord(Entry.ID, Record);
56375641 switch (BitCode) {
56435647 if (convertToString(Record, 0, ValueName))
56445648 return error("Invalid record");
56455649 SourceFileName = ValueName.c_str();
5646 break;
5647 }
5648 /// MODULE_CODE_HASH: [5*i32]
5649 case bitc::MODULE_CODE_HASH: {
5650 if (Record.size() != 5)
5651 return error("Invalid hash length " + Twine(Record.size()).str());
5652 if (!TheIndex)
5653 break;
5654 if (TheIndex->modulePaths().empty())
5655 // Does not have any summary emitted.
5656 break;
5657 if (TheIndex->modulePaths().size() != 1)
5658 return error("Don't expect multiple modules defined?");
5659 auto &Hash = TheIndex->modulePaths().begin()->second.second;
5660 int Pos = 0;
5661 for (auto &Val : Record) {
5662 assert(!(Val >> 32) && "Unexpected high bits set");
5663 Hash[Pos++] = Val;
5664 }
56655650 break;
56665651 }
56675652 /// MODULE_CODE_VSTOFFSET: [offset]
57755760 // module path string table entry with an empty (0) ID to take
57765761 // ownership.
57775762 FS->setModulePath(
5778 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
5763 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
57795764 static int RefListStartIndex = 4;
57805765 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
57815766 assert(Record.size() >= RefListStartIndex + NumRefs &&
58135798 std::unique_ptr FS =
58145799 llvm::make_unique(getDecodedLinkage(RawLinkage));
58155800 FS->setModulePath(
5816 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
5801 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
58175802 for (unsigned I = 2, E = Record.size(); I != E; ++I) {
58185803 unsigned RefValueId = Record[I];
58195804 uint64_t RefGUID = getGUIDFromValueId(RefValueId);
59015886 SmallVector Record;
59025887
59035888 SmallString<128> ModulePath;
5904 ModulePathStringTableTy::iterator LastSeenModulePath;
59055889 while (1) {
59065890 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
59075891
59225906 break;
59235907 case bitc::MST_CODE_ENTRY: {
59245908 // MST_ENTRY: [modid, namechar x N]
5909 if (convertToString(Record, 1, ModulePath))
5910 return error("Invalid record");
59255911 uint64_t ModuleId = Record[0];
5926
5927 if (convertToString(Record, 1, ModulePath))
5928 return error("Invalid record");
5929
5930 LastSeenModulePath = TheIndex->addModulePath(ModulePath, ModuleId);
5931 ModuleIdMap[ModuleId] = LastSeenModulePath->first();
5932
5912 StringRef ModulePathInMap = TheIndex->addModulePath(ModulePath, ModuleId);
5913 ModuleIdMap[ModuleId] = ModulePathInMap;
59335914 ModulePath.clear();
5934 break;
5935 }
5936 /// MST_CODE_HASH: [5*i32]
5937 case bitc::MST_CODE_HASH: {
5938 if (Record.size() != 5)
5939 return error("Invalid hash length " + Twine(Record.size()).str());
5940 if (LastSeenModulePath == TheIndex->modulePaths().end())
5941 return error("Invalid hash that does not follow a module path");
5942 int Pos = 0;
5943 for (auto &Val : Record) {
5944 assert(!(Val >> 32) && "Unexpected high bits set");
5945 LastSeenModulePath->second.second[Pos++] = Val;
5946 }
5947 // Reset LastSeenModulePath to avoid overriding the hash unexpectedly.
5948 LastSeenModulePath = TheIndex->modulePaths().end();
59495915 break;
59505916 }
59515917 }
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "ValueEnumerator.h"
14 #include "llvm/ADT/StringExtras.h"
1514 #include "llvm/ADT/STLExtras.h"
1615 #include "llvm/ADT/Triple.h"
1716 #include "llvm/Analysis/BlockFrequencyInfo.h"
3938 #include "llvm/Support/MathExtras.h"
4039 #include "llvm/Support/Program.h"
4140 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Support/SHA1.h"
4341 #include
4442 #include
4543 using namespace llvm;
28532851 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
28542852 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
28552853
2856 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
2857 Abbv = new BitCodeAbbrev();
2858 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
2859 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2860 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2861 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2862 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2863 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2864 unsigned AbbrevHash = Stream.EmitAbbrev(Abbv);
2865
2866 SmallVector Vals;
2867 for (const auto &MPSE : I.modulePaths()) {
2854 SmallVector NameVals;
2855 for (const StringMapEntry &MPSE : I.modulePaths()) {
28682856 StringEncoding Bits =
28692857 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
28702858 unsigned AbbrevToUse = Abbrev8Bit;
28732861 else if (Bits == SE_Fixed7)
28742862 AbbrevToUse = Abbrev7Bit;
28752863
2876 Vals.push_back(MPSE.getValue().first);
2864 NameVals.push_back(MPSE.getValue());
28772865
28782866 for (const auto P : MPSE.getKey())
2879 Vals.push_back((unsigned char)P);
2867 NameVals.push_back((unsigned char)P);
28802868
28812869 // Emit the finished record.
2882 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
2883
2884 Vals.clear();
2885 // Emit an optional hash for the module now
2886 auto &Hash = MPSE.getValue().second;
2887 bool AllZero = true; // Detect if the hash is empty, and do not generate it
2888 for (auto Val : Hash) {
2889 if (Val)
2890 AllZero = false;
2891 Vals.push_back(Val);
2892 }
2893 if (!AllZero) {
2894 // Emit the hash record.
2895 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
2896 }
2897
2898 Vals.clear();
2870 Stream.EmitRecord(bitc::MST_CODE_ENTRY, NameVals, AbbrevToUse);
2871 NameVals.clear();
28992872 }
29002873 Stream.ExitBlock();
29012874 }
32033176 Stream.ExitBlock();
32043177 }
32053178
3206 static void writeModuleHash(BitstreamWriter &Stream,
3207 SmallVectorImpl &Buffer,
3208 size_t BlockStartPos) {
3209 // Emit the module's hash.
3210 // MODULE_CODE_HASH: [5*i32]
3211 SHA1 Hasher;
3212 Hasher.update(ArrayRef((uint8_t *)&Buffer[BlockStartPos],
3213 Buffer.size() - BlockStartPos));
3214 auto Hash = Hasher.result();
3215 SmallVector Vals;
3216 auto LShift = [&](unsigned char Val, unsigned Amount)
3217 -> uint64_t { return ((uint64_t)Val) << Amount; };
3218 for (int Pos = 0; Pos < 20; Pos += 4) {
3219 uint32_t SubHash = LShift(Hash[Pos + 0], 24);
3220 SubHash |= LShift(Hash[Pos + 1], 16) | LShift(Hash[Pos + 2], 8) |
3221 (unsigned)(unsigned char)Hash[Pos + 3];
3222 Vals.push_back(SubHash);
3223 }
3224
3225 // Emit the finished record.
3226 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
3227 }
3228
32293179 /// WriteModule - Emit the specified module to the bitstream.
32303180 static void WriteModule(const Module *M, BitstreamWriter &Stream,
32313181 bool ShouldPreserveUseListOrder,
3232 uint64_t BitcodeStartBit, bool EmitSummaryIndex,
3233 bool GenerateHash, SmallVectorImpl &Buffer) {
3182 uint64_t BitcodeStartBit, bool EmitSummaryIndex) {
32343183 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3235 size_t BlockStartPos = Buffer.size();
32363184
32373185 SmallVector Vals;
32383186 unsigned CurVersion = 1;
32883236
32893237 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream,
32903238 VSTOffsetPlaceholder, BitcodeStartBit, &FunctionIndex);
3291
3292 if (GenerateHash) {
3293 writeModuleHash(Stream, Buffer, BlockStartPos);
3294 }
32953239
32963240 Stream.ExitBlock();
32973241 }
33773321 /// stream.
33783322 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
33793323 bool ShouldPreserveUseListOrder,
3380 bool EmitSummaryIndex, bool GenerateHash) {
3324 bool EmitSummaryIndex) {
33813325 SmallVector Buffer;
33823326 Buffer.reserve(256*1024);
33833327
34033347
34043348 // Emit the module.
34053349 WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit,
3406 EmitSummaryIndex, GenerateHash, Buffer);
3350 EmitSummaryIndex);
34073351 }
34083352
34093353 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
3636
3737 // Add the module path string ref for this module if we haven't already
3838 // saved a reference to it.
39 if (ModPath.empty()) {
40 auto Path = Info->summary()->modulePath();
41 ModPath = addModulePath(Path, NextModuleId, Other->getModuleHash(Path))
42 ->first();
43 } else
39 if (ModPath.empty())
40 ModPath = addModulePath(Info->summary()->modulePath(), NextModuleId);
41 else
4442 assert(ModPath == Info->summary()->modulePath() &&
4543 "Each module in the combined map should have a unique ID");
4644
7474 RandomNumberGenerator.cpp
7575 Regex.cpp
7676 ScaledNumber.cpp
77 SHA1.cpp
7877 SmallPtrSet.cpp
7978 SmallVector.cpp
8079 SourceMgr.cpp
+0
-168
lib/Support/SHA1.cpp less more
None //======- SHA1.h - Private copy of the SHA1 implementation ---*- C++ -* ======//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 // This code is taken from public domain
9 // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)
10 // and modified by wrapping it in a C++ interface for LLVM,
11 // and removing unnecessary code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Support/SHA1.h"
16 using namespace llvm;
17
18 #include
19 #include
20
21 #ifdef __BIG_ENDIAN__
22 #define SHA_BIG_ENDIAN
23 #endif
24
25 /* code */
26 #define SHA1_K0 0x5a827999
27 #define SHA1_K20 0x6ed9eba1
28 #define SHA1_K40 0x8f1bbcdc
29 #define SHA1_K60 0xca62c1d6
30
31 #define SEED_0 0x67452301
32 #define SEED_1 0xefcdab89
33 #define SEED_2 0x98badcfe
34 #define SEED_3 0x10325476
35 #define SEED_4 0xc3d2e1f0
36
37 void SHA1::init() {
38 InternalState.State[0] = SEED_0;
39 InternalState.State[1] = SEED_1;
40 InternalState.State[2] = SEED_2;
41 InternalState.State[3] = SEED_3;
42 InternalState.State[4] = SEED_4;
43 InternalState.ByteCount = 0;
44 InternalState.BufferOffset = 0;
45 }
46
47 static uint32_t rol32(uint32_t number, uint8_t bits) {
48 return ((number << bits) | (number >> (32 - bits)));
49 }
50
51 void SHA1::hashBlock() {
52 uint8_t i;
53 uint32_t a, b, c, d, e, t;
54
55 a = InternalState.State[0];
56 b = InternalState.State[1];
57 c = InternalState.State[2];
58 d = InternalState.State[3];
59 e = InternalState.State[4];
60 for (i = 0; i < 80; i++) {
61 if (i >= 16) {
62 t = InternalState.Buffer[(i + 13) & 15] ^
63 InternalState.Buffer[(i + 8) & 15] ^
64 InternalState.Buffer[(i + 2) & 15] ^ InternalState.Buffer[i & 15];
65 InternalState.Buffer[i & 15] = rol32(t, 1);
66 }
67 if (i < 20) {
68 t = (d ^ (b & (c ^ d))) + SHA1_K0;
69 } else if (i < 40) {
70 t = (b ^ c ^ d) + SHA1_K20;
71 } else if (i < 60) {
72 t = ((b & c) | (d & (b | c))) + SHA1_K40;
73 } else {
74 t = (b ^ c ^ d) + SHA1_K60;
75 }
76 t += rol32(a, 5) + e + InternalState.Buffer[i & 15];
77 e = d;
78 d = c;
79 c = rol32(b, 30);
80 b = a;
81 a = t;
82 }
83 InternalState.State[0] += a;
84 InternalState.State[1] += b;
85 InternalState.State[2] += c;
86 InternalState.State[3] += d;
87 InternalState.State[4] += e;
88 }
89
90 void SHA1::addUncounted(uint8_t data) {
91 uint8_t *const b = (uint8_t *)InternalState.Buffer;
92 #ifdef SHA_BIG_ENDIAN
93 b[InternalState.BufferOffset] = data;
94 #else
95 b[InternalState.BufferOffset ^ 3] = data;
96 #endif
97 InternalState.BufferOffset++;
98 if (InternalState.BufferOffset == BLOCK_LENGTH) {
99 hashBlock();
100 InternalState.BufferOffset = 0;
101 }
102 }
103
104 void SHA1::writebyte(uint8_t data) {
105 ++InternalState.ByteCount;
106 addUncounted(data);
107 }
108
109 void SHA1::update(ArrayRef Data) {
110 for (auto &C : Data)
111 writebyte(C);
112 }
113
114 void SHA1::pad() {
115 // Implement SHA-1 padding (fips180-2 §5.1.1)
116
117 // Pad with 0x80 followed by 0x00 until the end of the block
118 addUncounted(0x80);
119 while (InternalState.BufferOffset != 56)
120 addUncounted(0x00);
121
122 // Append length in the last 8 bytes
123 addUncounted(0); // We're only using 32 bit lengths
124 addUncounted(0); // But SHA-1 supports 64 bit lengths
125 addUncounted(0); // So zero pad the top bits
126 addUncounted(InternalState.ByteCount >> 29); // Shifting to multiply by 8
127 addUncounted(InternalState.ByteCount >>
128 21); // as SHA-1 supports bitstreams as well as
129 addUncounted(InternalState.ByteCount >> 13); // byte.
130 addUncounted(InternalState.ByteCount >> 5);
131 addUncounted(InternalState.ByteCount << 3);
132 }
133
134 StringRef SHA1::final() {
135 // Pad to complete the last block
136 pad();
137
138 #ifdef SHA_BIG_ENDIAN
139 // Just copy the current state
140 for (int i = 0; i < 5; i++) {
141 HashResult[i] = InternalState.State[i];
142 }
143 #else
144 // Swap byte order back
145 for (int i = 0; i < 5; i++) {
146 HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
147 (((InternalState.State[i]) << 8) & 0x00ff0000) |
148 (((InternalState.State[i]) >> 8) & 0x0000ff00) |
149 (((InternalState.State[i]) >> 24) & 0x000000ff);
150 }
151 #endif
152
153 // Return pointer to hash (20 characters)
154 return StringRef((char *)HashResult, HASH_LENGTH);
155 }
156
157 StringRef SHA1::result() {
158 auto StateToRestore = InternalState;
159
160 auto Hash = final();
161
162 // Restore the state
163 InternalState = StateToRestore;
164
165 // Return pointer to hash (20 characters)
166 return Hash;
167 }
6060 /* ShouldLazyLoadMetadata = */ true);
6161 if (!Result) {
6262 Err.print("function-import", errs());
63 report_fatal_error("Abort");
63 return nullptr;
6464 }
6565
6666 return Result;
+0
-4
test/Bitcode/Inputs/module_hash.ll less more
None ; Needs a function for the combined index to be populated
1 define void @bar() {
2 ret void
3 }
+0
-35
test/Bitcode/module_hash.ll less more
None ; Check per module hash.
1 ; RUN: llvm-as -module-hash %s -o - | llvm-bcanalyzer -dump | FileCheck %s --check-prefix=MOD1
2 ; MOD1:
3 ; RUN: llvm-as -module-hash %p/Inputs/module_hash.ll -o - | llvm-bcanalyzer -dump | FileCheck %s --check-prefix=MOD2
4 ; MOD2:
5
6 ; Check that the hash matches in the combined index.
7
8 ; First regenerate the modules with a summary
9 ; RUN: llvm-as -module-hash -module-summary %s -o %t.m1.bc
10 ; RUN: llvm-as -module-hash -module-summary %p/Inputs/module_hash.ll -o %t.m2.bc
11
12 ; Recover the hashes from the modules themselves.
13 ; RUN: llvm-bcanalyzer -dump %t1 | grep ' %t.hash
14 ; RUN: llvm-bcanalyzer -dump %t2 | grep '> %t.hash
15
16 ; Generate the combined index and gather the hashes there.
17 ; RUN: llvm-lto --thinlto-action=thinlink -o - %t.m1.bc %t.m2.bc | llvm-bcanalyzer -dump | grep '> %t.hash
18
19 ; Validate the output now, the hahes in the individual modules and the combined index are in the same file.
20 ; RUN: cat %t.hash | FileCheck %s --check-prefix=COMBINED
21
22 ; First capture the value of the hash for the two modules.
23 ; COMBINED:
24 ; COMBINED:
25
26 ; Validate against the value extracted from the combined index
27 ; COMBINED-DAG:
28 ; COMBINED-DAG:
29
30
31 ; Need a function for the combined index to be populated.
32 define void @foo() {
33 ret void
34 }
4747 cl::desc("Emit module summary index"),
4848 cl::init(false));
4949
50 static cl::opt EmitModuleHash("module-hash", cl::desc("Emit module hash"),
51 cl::init(false));
52
5350 static cl::opt
5451 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
5552
8481
8582 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
8683 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder,
87 EmitSummaryIndex, EmitModuleHash);
84 EmitSummaryIndex);
8885
8986 // Declare success.
9087 Out->keep();
2828
2929 #include "llvm/Bitcode/BitstreamReader.h"
3030 #include "llvm/ADT/Optional.h"
31 #include "llvm/ADT/StringExtras.h"
3231 #include "llvm/Bitcode/LLVMBitCodes.h"
3332 #include "llvm/Bitcode/ReaderWriter.h"
3433 #include "llvm/IR/Verifier.h"
3837 #include "llvm/Support/MemoryBuffer.h"
3938 #include "llvm/Support/PrettyStackTrace.h"
4039 #include "llvm/Support/Signals.h"
41 #include "llvm/Support/SHA1.h"
4240 #include "llvm/Support/raw_ostream.h"
4341 #include
44 #include
4542 #include
4643 #include
4744 #include
176173 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
177174 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
178175 STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
179 STRINGIFY_CODE(MODULE_CODE, HASH)
180176 }
181177 case bitc::IDENTIFICATION_BLOCK_ID:
182178 switch (CodeID) {
295291 default:
296292 return nullptr;
297293 STRINGIFY_CODE(MST_CODE, ENTRY)
298 STRINGIFY_CODE(MST_CODE, HASH)
299294 }
300295 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
301296 switch (CodeID) {
485480 if (Stream.EnterSubBlock(BlockID, &NumWords))
486481 return Error("Malformed block record");
487482
488 // Keep it for later, when we see a MODULE_HASH record
489 uint64_t BlockEntryPos = Stream.getCurrentByteNo();
490
491483 const char *BlockName = nullptr;
492484 if (DumpRecords) {
493485 outs() << Indent << "<";
559551 ++BlockStats.NumRecords;
560552
561553 StringRef Blob;
562 unsigned CurrentRecordPos = Stream.getCurrentByteNo();
563554 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
564555
565556 // Increment the # occurrences of this code.
593584
594585 for (unsigned i = 0, e = Record.size(); i != e; ++i)
595586 outs() << " op" << i << "=" << (int64_t)Record[i];
596
597 // If we found a module hash, let's verify that it matches!
598 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH) {
599 if (Record.size() != 5)
600 outs() << " (invalid)";
601 else {
602 // Recompute the hash and compare it to the one in the bitcode
603 SHA1 Hasher;
604 StringRef Hash;
605 {
606 int BlockSize = CurrentRecordPos - BlockEntryPos;
607 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
608 Hasher.update(ArrayRef(Ptr, BlockSize));
609 Hash = Hasher.result();
610 }
611 SmallString<20> RecordedHash;
612 RecordedHash.resize(20);
613 int Pos = 0;
614 for (auto &Val : Record) {
615 assert(!(Val >> 32) && "Unexpected high bits set");
616 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
617 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
618 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
619 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
620 }
621 if (Hash == RecordedHash)
622 outs() << " (match)";
623 else
624 outs() << " (!mismatch!)";
625 }
626 }
627587
628588 outs() << "/>";
629589
5151 formatted_raw_ostream_test.cpp
5252 raw_ostream_test.cpp
5353 raw_pwrite_stream_test.cpp
54 raw_sha1_ostream_test.cpp
5554 )
5655
5756 # ManagedStatic.cpp uses .
+0
-72
unittests/Support/raw_sha1_ostream_test.cpp less more
None //===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream tests -----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "gtest/gtest.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Support/Format.h"
12 #include "llvm/Support/raw_sha1_ostream.h"
13
14 #include
15
16 using namespace llvm;
17
18 static std::string toHex(StringRef Input) {
19 static const char *const LUT = "0123456789ABCDEF";
20 size_t Length = Input.size();
21
22 std::string Output;
23 Output.reserve(2 * Length);
24 for (size_t i = 0; i < Length; ++i) {
25 const unsigned char c = Input[i];
26 Output.push_back(LUT[c >> 4]);
27 Output.push_back(LUT[c & 15]);
28 }
29 return Output;
30 }
31
32 TEST(raw_sha1_ostreamTest, Basic) {
33 llvm::raw_sha1_ostream Sha1Stream;
34 Sha1Stream << "Hello World!";
35 auto Hash = toHex(Sha1Stream.sha1());
36
37 ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash);
38 }
39
40 // Check that getting the intermediate hash in the middle of the stream does
41 // not invalidate the final result.
42 TEST(raw_sha1_ostreamTest, Intermediate) {
43 llvm::raw_sha1_ostream Sha1Stream;
44 Sha1Stream << "Hello";
45 auto Hash = toHex(Sha1Stream.sha1());
46
47 ASSERT_EQ("F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0", Hash);
48 Sha1Stream << " World!";
49 Hash = toHex(Sha1Stream.sha1());
50
51 // Compute the non-split hash separately as a reference.
52 llvm::raw_sha1_ostream NonSplitSha1Stream;
53 NonSplitSha1Stream << "Hello World!";
54 auto NonSplitHash = toHex(NonSplitSha1Stream.sha1());
55
56 ASSERT_EQ(NonSplitHash, Hash);
57 }
58
59 TEST(raw_sha1_ostreamTest, Reset) {
60 llvm::raw_sha1_ostream Sha1Stream;
61 Sha1Stream << "Hello";
62 auto Hash = toHex(Sha1Stream.sha1());
63
64 ASSERT_EQ("F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0", Hash);
65
66 Sha1Stream.resetHash();
67 Sha1Stream << " World!";
68 Hash = toHex(Sha1Stream.sha1());
69
70 ASSERT_EQ("7447F2A5A42185C8CF91E632789C431830B59067", Hash);
71 }