Add sha1sum check for bitcode.
diff --git a/lib/bcc/Compiler.cpp b/lib/bcc/Compiler.cpp
index acb7ca2..777bf6c 100644
--- a/lib/bcc/Compiler.cpp
+++ b/lib/bcc/Compiler.cpp
@@ -106,6 +106,8 @@
#include <cutils/properties.h>
+#include <sha1.h>
+
#include <string>
#include <vector>
@@ -383,6 +385,10 @@
mSourceModTime = bitcodeFileModTime;
mSourceCRC32 = bitcodeFileCRC32;
+ // Compute the SHA1 hash of the input bitcode. So that we can use the hash
+ // to decide rather to update the cache or not.
+ computeSourceSHA1(bitcode, bitcodeSize);
+
if (resName) {
// Turn on mUseCache mode iff
// 1. Has resName
@@ -1289,6 +1295,18 @@
}
+void Compiler::computeSourceSHA1(char const *bitcode, size_t bitcodeSize) {
+ SHA1_CTX hashContext;
+
+ SHA1Init(&hashContext);
+ SHA1Update(&hashContext,
+ reinterpret_cast<const unsigned char *>(bitcode),
+ static_cast<unsigned long>(bitcodeSize));
+ SHA1Final(mSourceSHA1, &hashContext);
+
+}
+
+
// Design of caching EXE:
// ======================
// 1. Each process will have virtual address available starting at 0x7e00000.
@@ -1381,6 +1399,9 @@
// Source Bitcode File CRC32
hdr->sourceCRC32 = mSourceCRC32;
+ // Copy the hash checksum
+ memcpy(hdr->sourceSHA1, mSourceSHA1, 20);
+
// Current Memory Address (Saved for Recalculation)
hdr->cachedCodeDataAddr = reinterpret_cast<uint32_t>(mCodeDataAddr);
hdr->rootAddr = reinterpret_cast<uint32_t>(lookup("root"));
@@ -1857,7 +1878,6 @@
}
// Check the file dependencies
-
if (optHdr.sourceWhen && (optHdr.sourceWhen != sourceWhen)) {
LOGI("bcc: source file mod time mismatch (%08lx vs %08lx)\n",
(unsigned long)optHdr.sourceWhen, (unsigned long)sourceWhen);
@@ -1894,6 +1914,23 @@
return false;
}
+ if (memcmp(optHdr.sourceSHA1, mSourceSHA1, 20) != 0) {
+ LOGE("Bitcode SHA1 mismatch. Cache is outdated.\n");
+
+#define PRINT_SHA1SUM(PREFIX, POSTFIX, D) \
+ LOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
+ "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
+ D[0], D[1], D[2], D[3], D[4], D[5], D[6], D[7], D[8], D[9], \
+ D[10], D[11], D[12], D[13], D[14], D[15], D[16], D[17], D[18], D[19]);
+
+ PRINT_SHA1SUM("Note: Bitcode sha1sum: ", "\n", mSourceSHA1);
+ PRINT_SHA1SUM("Note: Cached sha1sum: ", "\n", optHdr.sourceSHA1);
+
+#undef PRINT_SHA1SUM
+
+ goto bail;
+ }
+
// Check the cache file has __isThreadable or not. If it is set,
// then we have to call mpSymbolLookupFn for __clearThreadable.
if (optHdr.libRSThreadable && mpSymbolLookupFn) {
diff --git a/lib/bcc/Compiler.h b/lib/bcc/Compiler.h
index fba7a01..2510aad 100644
--- a/lib/bcc/Compiler.h
+++ b/lib/bcc/Compiler.h
@@ -104,6 +104,8 @@
char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true.
// Used by genCacheFile() for dumping
+ unsigned char mSourceSHA1[20]; // Set by readBC()
+
PragmaList mPragmas;
ExportVarList mExportVars;
@@ -198,6 +200,8 @@
~Compiler();
private:
+ void computeSourceSHA1(char const *bitcode, size_t size);
+
// Note: loadCacheFile() and genCacheFile() go hand in hand
void genCacheFile();