Skip to content

Commit 42cb73f

Browse files
[Support] Add SipHash-based 16/64-bit ptrauth stable hash.
Based on the SipHash reference implementation: https://github.com/veorq/SipHash which has very graciously been licensed under our llvm license (Apache-2.0 WITH LLVM-exception) by Jean-Philippe Aumasson. This lightly modifies it to fit into libSupport, and wraps it for the two main interfaces we're interested in (16/64-bit). This intentionally doesn't expose a raw interface beyond that to encourage others to carefully consider their use. The exact algorithm is the little-endian interpretation of the non-doubled (i.e. 64-bit) result of applying a SipHash-2-4 using a specific key value which can be found in the source. By "stable" we mean that the result of this hash algorithm will the same across different compiler versions and target platforms. The 16-bit variant is used extensively for the AArch64 ptrauth ABI, because AArch64 can efficiently load a 16-bit immediate into the high bits of a register without disturbing the remainder of the value, which serves as a nice blend operation. 16 bits is also sufficiently compact to not inflate a loader relocation. We disallow zero to guarantee a different discriminator from the places in the ABI that use a constant zero. Co-Authored-By: John McCall <[email protected]>
1 parent fc8f76b commit 42cb73f

File tree

5 files changed

+266
-0
lines changed

5 files changed

+266
-0
lines changed

llvm/include/llvm/Support/SipHash.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- SipHash.h - An ABI-stable string SipHash ---------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// A family of ABI-stable string hash algorithms based on SipHash, currently
10+
// used to compute ptrauth discriminators.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_SUPPORT_SIPHASH_H
15+
#define LLVM_SUPPORT_SIPHASH_H
16+
17+
#include <cstdint>
18+
19+
namespace llvm {
20+
class StringRef;
21+
22+
/// Compute a stable 64-bit hash of the given string.
23+
///
24+
/// The exact algorithm is the little-endian interpretation of the
25+
/// non-doubled (i.e. 64-bit) result of applying a SipHash-2-4 using
26+
/// a specific key value which can be found in the source.
27+
///
28+
/// By "stable" we mean that the result of this hash algorithm will
29+
/// the same across different compiler versions and target platforms.
30+
uint64_t getPointerAuthStableSipHash64(StringRef S);
31+
32+
/// Compute a stable non-zero 16-bit hash of the given string.
33+
///
34+
/// This computes the full getPointerAuthStableSipHash64, but additionally
35+
/// truncates it down to a non-zero 16-bit value.
36+
///
37+
/// We use a 16-bit discriminator because ARM64 can efficiently load
38+
/// a 16-bit immediate into the high bits of a register without disturbing
39+
/// the remainder of the value, which serves as a nice blend operation.
40+
/// 16 bits is also sufficiently compact to not inflate a loader relocation.
41+
/// We disallow zero to guarantee a different discriminator from the places
42+
/// in the ABI that use a constant zero.
43+
uint64_t getPointerAuthStableSipHash16(StringRef S);
44+
45+
} // end namespace llvm
46+
47+
#endif

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ add_llvm_component_library(LLVMSupport
222222
SHA1.cpp
223223
SHA256.cpp
224224
Signposts.cpp
225+
SipHash.cpp
225226
SmallPtrSet.cpp
226227
SmallVector.cpp
227228
SourceMgr.cpp

llvm/lib/Support/SipHash.cpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
//===--- StableHash.cpp - An ABI-stable string hash -----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements an ABI-stable string hash based on SipHash, used to
10+
// compute ptrauth discriminators.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "llvm/Support/SipHash.h"
15+
#include "llvm/ADT/StringExtras.h"
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Support/Debug.h"
18+
#include <cstdint>
19+
#include <cstring>
20+
21+
using namespace llvm;
22+
23+
#define DEBUG_TYPE "llvm-siphash"
24+
25+
// Lightly adapted from the SipHash reference C implementation by
26+
// Jean-Philippe Aumasson and Daniel J. Bernstein.
27+
28+
#define SIPHASH_ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
29+
30+
#define SIPHASH_U8TO64_LE(p) \
31+
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
32+
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
33+
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
34+
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
35+
36+
#define SIPHASH_SIPROUND \
37+
do { \
38+
v0 += v1; \
39+
v1 = SIPHASH_ROTL(v1, 13); \
40+
v1 ^= v0; \
41+
v0 = SIPHASH_ROTL(v0, 32); \
42+
v2 += v3; \
43+
v3 = SIPHASH_ROTL(v3, 16); \
44+
v3 ^= v2; \
45+
v0 += v3; \
46+
v3 = SIPHASH_ROTL(v3, 21); \
47+
v3 ^= v0; \
48+
v2 += v1; \
49+
v1 = SIPHASH_ROTL(v1, 17); \
50+
v1 ^= v2; \
51+
v2 = SIPHASH_ROTL(v2, 32); \
52+
} while (0)
53+
54+
template <int cROUNDS, int dROUNDS, class ResultTy>
55+
static inline ResultTy siphash(const uint8_t *in, uint64_t inlen,
56+
const uint8_t (&k)[16]) {
57+
static_assert(sizeof(ResultTy) == 8 || sizeof(ResultTy) == 16,
58+
"result type should be uint64_t or uint128_t");
59+
uint64_t v0 = 0x736f6d6570736575ULL;
60+
uint64_t v1 = 0x646f72616e646f6dULL;
61+
uint64_t v2 = 0x6c7967656e657261ULL;
62+
uint64_t v3 = 0x7465646279746573ULL;
63+
uint64_t b;
64+
uint64_t k0 = SIPHASH_U8TO64_LE(k);
65+
uint64_t k1 = SIPHASH_U8TO64_LE(k + 8);
66+
uint64_t m;
67+
int i;
68+
const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
69+
const int left = inlen & 7;
70+
b = ((uint64_t)inlen) << 56;
71+
v3 ^= k1;
72+
v2 ^= k0;
73+
v1 ^= k1;
74+
v0 ^= k0;
75+
76+
if (sizeof(ResultTy) == 16) {
77+
v1 ^= 0xee;
78+
}
79+
80+
for (; in != end; in += 8) {
81+
m = SIPHASH_U8TO64_LE(in);
82+
v3 ^= m;
83+
84+
for (i = 0; i < cROUNDS; ++i)
85+
SIPHASH_SIPROUND;
86+
87+
v0 ^= m;
88+
}
89+
90+
switch (left) {
91+
case 7:
92+
b |= ((uint64_t)in[6]) << 48;
93+
LLVM_FALLTHROUGH;
94+
case 6:
95+
b |= ((uint64_t)in[5]) << 40;
96+
LLVM_FALLTHROUGH;
97+
case 5:
98+
b |= ((uint64_t)in[4]) << 32;
99+
LLVM_FALLTHROUGH;
100+
case 4:
101+
b |= ((uint64_t)in[3]) << 24;
102+
LLVM_FALLTHROUGH;
103+
case 3:
104+
b |= ((uint64_t)in[2]) << 16;
105+
LLVM_FALLTHROUGH;
106+
case 2:
107+
b |= ((uint64_t)in[1]) << 8;
108+
LLVM_FALLTHROUGH;
109+
case 1:
110+
b |= ((uint64_t)in[0]);
111+
break;
112+
case 0:
113+
break;
114+
}
115+
116+
v3 ^= b;
117+
118+
for (i = 0; i < cROUNDS; ++i)
119+
SIPHASH_SIPROUND;
120+
121+
v0 ^= b;
122+
123+
if (sizeof(ResultTy) == 8) {
124+
v2 ^= 0xff;
125+
} else {
126+
v2 ^= 0xee;
127+
}
128+
129+
for (i = 0; i < dROUNDS; ++i)
130+
SIPHASH_SIPROUND;
131+
132+
b = v0 ^ v1 ^ v2 ^ v3;
133+
134+
// This mess with the result type would be easier with 'if constexpr'.
135+
136+
uint64_t firstHalf = b;
137+
if (sizeof(ResultTy) == 8)
138+
return firstHalf;
139+
140+
v1 ^= 0xdd;
141+
142+
for (i = 0; i < dROUNDS; ++i)
143+
SIPHASH_SIPROUND;
144+
145+
b = v0 ^ v1 ^ v2 ^ v3;
146+
uint64_t secondHalf = b;
147+
148+
return firstHalf | (ResultTy(secondHalf) << (sizeof(ResultTy) == 8 ? 0 : 64));
149+
}
150+
151+
//===--- LLVM-specific wrappers around siphash.
152+
153+
/// Compute an ABI-stable 64-bit hash of the given string.
154+
uint64_t llvm::getPointerAuthStableSipHash64(StringRef Str) {
155+
static const uint8_t K[16] = {0xb5, 0xd4, 0xc9, 0xeb, 0x79, 0x10, 0x4a, 0x79,
156+
0x6f, 0xec, 0x8b, 0x1b, 0x42, 0x87, 0x81, 0xd4};
157+
158+
// The aliasing is fine here because of omnipotent char.
159+
auto *Data = reinterpret_cast<const uint8_t *>(Str.data());
160+
return siphash<2, 4, uint64_t>(Data, Str.size(), K);
161+
}
162+
163+
/// Compute an ABI-stable 16-bit hash of the given string.
164+
uint64_t llvm::getPointerAuthStableSipHash16(StringRef Str) {
165+
uint64_t RawHash = getPointerAuthStableSipHash64(Str);
166+
167+
// Produce a non-zero 16-bit discriminator.
168+
uint64_t Discriminator = (RawHash % 0xFFFF) + 1;
169+
LLVM_DEBUG(dbgs() << "ptrauth stable hash 16-bit discriminator: "
170+
<< utostr(Discriminator) << " (0x"
171+
<< utohexstr(Discriminator) << ")"
172+
<< " of: " << Str << "\n");
173+
return Discriminator;
174+
}

llvm/unittests/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_llvm_unittest(SupportTests
7575
ScopedPrinterTest.cpp
7676
SHA256.cpp
7777
SignalsTest.cpp
78+
SipHashTest.cpp
7879
SourceMgrTest.cpp
7980
SpecialCaseListTest.cpp
8081
SuffixTreeTest.cpp
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===- llvm/unittest/Support/SipHashTest.cpp ------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Support/SipHash.h"
10+
#include "llvm/ADT/StringRef.h"
11+
#include "llvm/ADT/StringExtras.h"
12+
#include "llvm/Support/raw_ostream.h"
13+
#include "gtest/gtest.h"
14+
15+
using namespace llvm;
16+
17+
namespace {
18+
19+
TEST(SipHashTest, PointerAuthSipHash) {
20+
// Test some basic cases, for 16 bit and 64 bit results.
21+
EXPECT_EQ(0xE793U, getPointerAuthStableSipHash16(""));
22+
EXPECT_EQ(0xF468U, getPointerAuthStableSipHash16("strlen"));
23+
EXPECT_EQ(0x2D15U, getPointerAuthStableSipHash16("_ZN1 ind; f"));
24+
25+
EXPECT_EQ(0xB2BB69BB0A2AC0F1U, getPointerAuthStableSipHash64(""));
26+
EXPECT_EQ(0x9304ABFF427B72E8U, getPointerAuthStableSipHash64("strlen"));
27+
EXPECT_EQ(0x55F45179A08AE51BU, getPointerAuthStableSipHash64("_ZN1 ind; f"));
28+
29+
// Test some known strings that are already enshrined in the ABI.
30+
EXPECT_EQ(0x6AE1U, getPointerAuthStableSipHash16("isa"));
31+
EXPECT_EQ(0xB5ABU, getPointerAuthStableSipHash16("objc_class:superclass"));
32+
EXPECT_EQ(0xC0BBU, getPointerAuthStableSipHash16("block_descriptor"));
33+
EXPECT_EQ(0xC310U, getPointerAuthStableSipHash16("method_list_t"));
34+
35+
// Test the limits that apply to 16 bit results but don't to 64 bit results.
36+
EXPECT_EQ(1U, getPointerAuthStableSipHash16("_Zptrkvttf"));
37+
EXPECT_EQ(0x314FD87E0611F020U, getPointerAuthStableSipHash64("_Zptrkvttf"));
38+
39+
EXPECT_EQ(0xFFFFU, getPointerAuthStableSipHash16("_Zaflhllod"));
40+
EXPECT_EQ(0x1292F635FB3DFBF8U, getPointerAuthStableSipHash64("_Zaflhllod"));
41+
}
42+
43+
} // end anonymous namespace

0 commit comments

Comments
 (0)