Files
llvm-project/llvm/lib/Support/Hash.cpp
Kees Cook 6e479668ba [CodeGen][KCFI] Allow setting type hash from xxHash64 to FNV-1a (#167254)
When emitting the assembly .set directive, KCFI needs to use
getZExtValue(). However, this means that FileCheck pattern matching can't
match between the .set directive and the IR when the high bit of a 32-bit
value is set. We had gotten lucky with the existing tests happening to
just not have had the high bit set. The coming hash change will expose
this, though.

LLVM IR's default printing behavior uses APInt::operator<<, which calls
APInt::print(OS, /*isSigned=*/true). This means KCFI operand bundles in
call instructions print as signed (e.g. [ "kcfi"(i32 -1208803271) ]),
and KCFI type metadata prints as signed (e.g. !3 = !{i32 -1208803271}).
Changing the IR to print unsigned i32 values would impact hundreds of
existing tests, so it is best to just leave it be.

Update the KCFI .set direct to use getSExtValue() in a comment so that
we can both build correctly and use FileCheck with pattern matching in
tests.

KCFI generates hashes in two places. Instead of exposing the hash
implementation in both places, introduce a helper that wraps the
specific hash implementation in a single place, llvm::getKCFITypeID.

In order to transition between KCFI hash, we need to be able to specify
them. Add the Clang option -fsanitize-kcfi-hash= and a IR module option
"kcfi-hash" that can choose between xxHash64 and FNV-1a. Default to
xxHash64 to stay backward compatible, as we'll need to also update rustc
to take a new option to change the hash to FNV-1a for interop with the
coming GCC KCFI.
2025-12-03 15:09:04 -08:00

52 lines
1.6 KiB
C++

//===- Hash.cpp - Hash functions ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements hash functions.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Hash.h"
#include "llvm/Support/xxhash.h"
using namespace llvm;
KCFIHashAlgorithm llvm::parseKCFIHashAlgorithm(StringRef Name) {
if (Name == "FNV-1a")
return KCFIHashAlgorithm::FNV1a;
// Default to xxHash64 for backward compatibility
return KCFIHashAlgorithm::xxHash64;
}
StringRef llvm::stringifyKCFIHashAlgorithm(KCFIHashAlgorithm Algorithm) {
switch (Algorithm) {
case KCFIHashAlgorithm::xxHash64:
return "xxHash64";
case KCFIHashAlgorithm::FNV1a:
return "FNV-1a";
}
llvm_unreachable("Unknown KCFI hash algorithm");
}
uint32_t llvm::getKCFITypeID(StringRef MangledTypeName,
KCFIHashAlgorithm Algorithm) {
switch (Algorithm) {
case KCFIHashAlgorithm::xxHash64:
// Use lower 32 bits of xxHash64
return static_cast<uint32_t>(xxHash64(MangledTypeName));
case KCFIHashAlgorithm::FNV1a:
// FNV-1a hash (32-bit)
uint32_t Hash = 2166136261u; // FNV offset basis
for (unsigned char C : MangledTypeName) {
Hash ^= C;
Hash *= 16777619u; // FNV prime
}
return Hash;
}
llvm_unreachable("Unknown KCFI hash algorithm");
}