From f3005d5b86ca947977f6056552b2a4648b9f0460 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 22 Jun 2024 14:21:36 -0700 Subject: [Serialization] Change input file content hash from size_t to uint64_t https://reviews.llvm.org/D67249 added content hash (see -fvalidate-ast-input-files-content) using llvm::hash_code (size_t). The hash value is 32-bit on 32-bit systems, which was unintentional. Fix #96379: #96136 switched the hash function to xxh3_64bit but did not update the ContentHash type, leading to mismatch between ASTReader and ASTWriter. --- clang/lib/Serialization/ASTReader.cpp | 2 +- clang/lib/Serialization/ASTWriter.cpp | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index c0b7db7..e8b3986 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -2629,7 +2629,7 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { "We should only check the content of the inputs with " "ValidateASTInputFilesContent enabled."); - if (StoredContentHash == static_cast(llvm::hash_code(-1))) + if (StoredContentHash == 0) return OriginalChange; auto MemBuffOrError = FileMgr.getBufferForFile(*File); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index e6a58dc..d99d856 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -1776,7 +1776,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, Entry.IsTopLevel = getAffectingIncludeLoc(SourceMgr, File).isInvalid(); Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic()); - auto ContentHash = hash_code(-1); + uint64_t ContentHash = 0; if (PP->getHeaderSearchInfo() .getHeaderSearchOpts() .ValidateASTInputFilesContent) { @@ -1787,12 +1787,8 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content) << Entry.File.getName(); } - auto CH = llvm::APInt(64, ContentHash); - Entry.ContentHash[0] = - static_cast(CH.getLoBits(32).getZExtValue()); - Entry.ContentHash[1] = - static_cast(CH.getHiBits(32).getZExtValue()); - + Entry.ContentHash[0] = uint32_t(ContentHash); + Entry.ContentHash[1] = uint32_t(ContentHash >> 32); if (Entry.IsSystemFile) SystemFiles.push_back(Entry); else -- cgit v1.1