Skip to content

Commit f5215ea

Browse files
melvermahesh-attarde
authored andcommitted
[Support] Add SipHash-based 64-bit stable hash function (llvm#160945)
Factor out the 64-bit hash calculation in getPointerAuthStableSipHash() as getStableSipHash(). This allows using the full 64-bit hash where we require a stable hash. Similar to getPointerAuthStableSipHash(), the new hash function is meant to be stable across platforms and compiler versions.
1 parent c48b9a8 commit f5215ea

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

llvm/include/llvm/Support/SipHash.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ LLVM_ABI void getSipHash_2_4_64(ArrayRef<uint8_t> In, const uint8_t (&K)[16],
3333
LLVM_ABI void getSipHash_2_4_128(ArrayRef<uint8_t> In, const uint8_t (&K)[16],
3434
uint8_t (&Out)[16]);
3535

36+
/// Compute a stable 64-bit hash of the given string.
37+
///
38+
/// The exact algorithm is the little-endian interpretation of the
39+
/// non-doubled (i.e. 64-bit) result of applying a SipHash-2-4 using
40+
/// a specific seed value which can be found in the source.
41+
LLVM_ABI uint64_t getStableSipHash(StringRef Str);
42+
3643
/// Compute a stable non-zero 16-bit hash of the given string.
3744
///
3845
/// The exact algorithm is the little-endian interpretation of the

llvm/lib/Support/SipHash.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ void llvm::getSipHash_2_4_128(ArrayRef<uint8_t> In, const uint8_t (&K)[16],
3535
siphash<2, 4>(In.data(), In.size(), K, Out);
3636
}
3737

38-
/// Compute an ABI-stable 16-bit hash of the given string.
39-
uint16_t llvm::getPointerAuthStableSipHash(StringRef Str) {
38+
/// Compute an ABI-stable 64-bit hash of the given string.
39+
uint64_t llvm::getStableSipHash(StringRef Str) {
4040
static const uint8_t K[16] = {0xb5, 0xd4, 0xc9, 0xeb, 0x79, 0x10, 0x4a, 0x79,
4141
0x6f, 0xec, 0x8b, 0x1b, 0x42, 0x87, 0x81, 0xd4};
4242

4343
uint8_t RawHashBytes[8];
4444
getSipHash_2_4_64(arrayRefFromStringRef(Str), K, RawHashBytes);
45-
uint64_t RawHash = endian::read64le(RawHashBytes);
45+
return endian::read64le(RawHashBytes);
46+
}
47+
48+
/// Compute an ABI-stable 16-bit hash of the given string.
49+
uint16_t llvm::getPointerAuthStableSipHash(StringRef Str) {
50+
uint64_t RawHash = getStableSipHash(Str);
4651

4752
// Produce a non-zero 16-bit discriminator.
4853
uint16_t Discriminator = (RawHash % 0xFFFF) + 1;

llvm/unittests/Support/SipHashTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ TEST(SipHashTest, SipHash_2_4_128) {
5050
}
5151
}
5252

53+
// Tests for the 64-bit stable SipHash wrapper.
54+
TEST(SipHashTest, StableSipHash) {
55+
EXPECT_EQ(0xB2BB69BB0A2AC0F1UL, getStableSipHash(""));
56+
EXPECT_EQ(0x9304ABFF427B72E8UL, getStableSipHash("strlen"));
57+
EXPECT_EQ(0x55F45179A08AE51BUL, getStableSipHash("_ZN1 ind; f"));
58+
}
59+
5360
// Tests for the ptrauth-specific SipHash wrapper.
5461
TEST(SipHashTest, PointerAuthSipHash) {
5562
// Test some basic cases.

0 commit comments

Comments
 (0)