Skip to content

Commit deebe93

Browse files
committed
fixup! address more comments
Created using spr 1.3.8-beta.1
2 parents e356bbb + 3fc5e61 commit deebe93

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
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/lib/Transforms/Instrumentation/AllocToken.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
#include "llvm/Support/Compiler.h"
4141
#include "llvm/Support/ErrorHandling.h"
4242
#include "llvm/Support/RandomNumberGenerator.h"
43+
#include "llvm/Support/SipHash.h"
4344
#include "llvm/Support/raw_ostream.h"
44-
#include "llvm/Support/xxhash.h"
4545
#include <cassert>
4646
#include <cstddef>
4747
#include <cstdint>
@@ -176,7 +176,7 @@ class RandomMode : public ModeBase {
176176
};
177177

178178
/// Implementation for TokenMode::TypeHash. The implementation ensures
179-
/// hashes are stable across different compiler invocations. Uses xxHash as the
179+
/// hashes are stable across different compiler invocations. Uses SipHash as the
180180
/// hash function.
181181
class TypeHashMode : public ModeBase {
182182
public:
@@ -185,7 +185,7 @@ class TypeHashMode : public ModeBase {
185185
uint64_t operator()(const CallBase &CB, OptimizationRemarkEmitter &ORE) {
186186
if (MDNode *N = getAllocTokenMetadata(CB)) {
187187
MDString *S = cast<MDString>(N->getOperand(0));
188-
return boundedToken(xxHash64(S->getString()));
188+
return boundedToken(getStableSipHash(S->getString()));
189189
}
190190
remarkNoMetadata(CB, ORE);
191191
return ClFallbackToken;

llvm/test/Instrumentation/AllocToken/extralibfuncs.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ define ptr @test_extra_libfuncs() sanitize_alloc_token {
1111
; CHECK-LABEL: define ptr @test_extra_libfuncs(
1212
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
1313
; CHECK-NEXT: [[ENTRY:.*:]]
14-
; CHECK-NEXT: [[TMP0:%.*]] = call { ptr, i64 } @__alloc_token___size_returning_new(i64 10, i64 6985720287680550851), !alloc_token [[META0:![0-9]+]]
14+
; CHECK-NEXT: [[TMP0:%.*]] = call { ptr, i64 } @__alloc_token___size_returning_new(i64 10, i64 2689373973731826898), !alloc_token [[META0:![0-9]+]]
1515
; CHECK-NEXT: [[PTR1:%.*]] = extractvalue { ptr, i64 } [[TMP0]], 0
1616
; CHECK-NEXT: ret ptr [[PTR1]]
1717
;
@@ -28,8 +28,8 @@ define ptr @test_replaceable_new() sanitize_alloc_token {
2828
; CHECK-LABEL: define ptr @test_replaceable_new(
2929
; CHECK-SAME: ) #[[ATTR1]] {
3030
; CHECK-NEXT: [[ENTRY:.*:]]
31-
; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token__Znwm(i64 32, i64 6985720287680550851), !alloc_token [[META0]]
32-
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @__alloc_token__Znam(i64 64, i64 6985720287680550851), !alloc_token [[META0]]
31+
; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token__Znwm(i64 32, i64 2689373973731826898), !alloc_token [[META0]]
32+
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @__alloc_token__Znam(i64 64, i64 2689373973731826898), !alloc_token [[META0]]
3333
; CHECK-NEXT: ret ptr [[TMP0]]
3434
;
3535
entry:

llvm/test/Instrumentation/AllocToken/remark.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ define ptr @test_has_metadata() sanitize_alloc_token {
1212
; CHECK-LABEL: define ptr @test_has_metadata(
1313
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
1414
; CHECK-NEXT: [[ENTRY:.*:]]
15-
; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token_malloc(i64 64, i64 6985720287680550851), !alloc_token [[META0:![0-9]+]]
15+
; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token_malloc(i64 64, i64 2689373973731826898), !alloc_token [[META0:![0-9]+]]
1616
; CHECK-NEXT: ret ptr [[TMP0]]
1717
;
1818
entry:

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)