diff options
author | Marco Elver <elver@google.com> | 2025-09-26 22:37:43 +0200 |
---|---|---|
committer | Marco Elver <elver@google.com> | 2025-09-26 22:37:43 +0200 |
commit | 7510ebf60320ab99e8c85de06adc14e4006fc8fb (patch) | |
tree | 432c1b3aa18546ddeeb086cdbe1eb7e400168229 | |
parent | 5af7e7ccbd3d25bbc58fc4a91f8e23249eb8c89d (diff) | |
parent | 110cef2d6feada373aa40ea88410c78f66ecf119 (diff) | |
download | llvm-users/melver/spr/clang-introduce-fsanitizealloc-token.zip llvm-users/melver/spr/clang-introduce-fsanitizealloc-token.tar.gz llvm-users/melver/spr/clang-introduce-fsanitizealloc-token.tar.bz2 |
fixup! address more commentsusers/melver/spr/clang-introduce-fsanitizealloc-token
Created using spr 1.3.8-beta.1
-rw-r--r-- | llvm/include/llvm/Support/SipHash.h | 7 | ||||
-rw-r--r-- | llvm/lib/Support/SipHash.cpp | 11 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/AllocToken.cpp | 6 | ||||
-rw-r--r-- | llvm/test/Instrumentation/AllocToken/extralibfuncs.ll | 6 | ||||
-rw-r--r-- | llvm/test/Instrumentation/AllocToken/remark.ll | 2 | ||||
-rw-r--r-- | llvm/unittests/Support/SipHashTest.cpp | 7 |
6 files changed, 29 insertions, 10 deletions
diff --git a/llvm/include/llvm/Support/SipHash.h b/llvm/include/llvm/Support/SipHash.h index 910cf594..b090565 100644 --- a/llvm/include/llvm/Support/SipHash.h +++ b/llvm/include/llvm/Support/SipHash.h @@ -33,6 +33,13 @@ LLVM_ABI void getSipHash_2_4_64(ArrayRef<uint8_t> In, const uint8_t (&K)[16], LLVM_ABI void getSipHash_2_4_128(ArrayRef<uint8_t> In, const uint8_t (&K)[16], uint8_t (&Out)[16]); +/// Compute a stable 64-bit hash of the given string. +/// +/// 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 seed value which can be found in the source. +LLVM_ABI uint64_t getStableSipHash(StringRef Str); + /// Compute a stable non-zero 16-bit hash of the given string. /// /// The exact algorithm is the little-endian interpretation of the diff --git a/llvm/lib/Support/SipHash.cpp b/llvm/lib/Support/SipHash.cpp index 86dad66..382d36f 100644 --- a/llvm/lib/Support/SipHash.cpp +++ b/llvm/lib/Support/SipHash.cpp @@ -35,14 +35,19 @@ void llvm::getSipHash_2_4_128(ArrayRef<uint8_t> In, const uint8_t (&K)[16], siphash<2, 4>(In.data(), In.size(), K, Out); } -/// Compute an ABI-stable 16-bit hash of the given string. -uint16_t llvm::getPointerAuthStableSipHash(StringRef Str) { +/// Compute an ABI-stable 64-bit hash of the given string. +uint64_t llvm::getStableSipHash(StringRef Str) { static const uint8_t K[16] = {0xb5, 0xd4, 0xc9, 0xeb, 0x79, 0x10, 0x4a, 0x79, 0x6f, 0xec, 0x8b, 0x1b, 0x42, 0x87, 0x81, 0xd4}; uint8_t RawHashBytes[8]; getSipHash_2_4_64(arrayRefFromStringRef(Str), K, RawHashBytes); - uint64_t RawHash = endian::read64le(RawHashBytes); + return endian::read64le(RawHashBytes); +} + +/// Compute an ABI-stable 16-bit hash of the given string. +uint16_t llvm::getPointerAuthStableSipHash(StringRef Str) { + uint64_t RawHash = getStableSipHash(Str); // Produce a non-zero 16-bit discriminator. uint16_t Discriminator = (RawHash % 0xFFFF) + 1; diff --git a/llvm/lib/Transforms/Instrumentation/AllocToken.cpp b/llvm/lib/Transforms/Instrumentation/AllocToken.cpp index 3304080..2bc9125 100644 --- a/llvm/lib/Transforms/Instrumentation/AllocToken.cpp +++ b/llvm/lib/Transforms/Instrumentation/AllocToken.cpp @@ -40,8 +40,8 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/RandomNumberGenerator.h" +#include "llvm/Support/SipHash.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/xxhash.h" #include <cassert> #include <cstddef> #include <cstdint> @@ -176,7 +176,7 @@ private: }; /// Implementation for TokenMode::TypeHash. The implementation ensures -/// hashes are stable across different compiler invocations. Uses xxHash as the +/// hashes are stable across different compiler invocations. Uses SipHash as the /// hash function. class TypeHashMode : public ModeBase { public: @@ -185,7 +185,7 @@ public: uint64_t operator()(const CallBase &CB, OptimizationRemarkEmitter &ORE) { if (MDNode *N = getAllocTokenMetadata(CB)) { MDString *S = cast<MDString>(N->getOperand(0)); - return boundedToken(xxHash64(S->getString())); + return boundedToken(getStableSipHash(S->getString())); } remarkNoMetadata(CB, ORE); return ClFallbackToken; diff --git a/llvm/test/Instrumentation/AllocToken/extralibfuncs.ll b/llvm/test/Instrumentation/AllocToken/extralibfuncs.ll index 3c331fd..5f08552 100644 --- a/llvm/test/Instrumentation/AllocToken/extralibfuncs.ll +++ b/llvm/test/Instrumentation/AllocToken/extralibfuncs.ll @@ -11,7 +11,7 @@ define ptr @test_extra_libfuncs() sanitize_alloc_token { ; CHECK-LABEL: define ptr @test_extra_libfuncs( ; CHECK-SAME: ) #[[ATTR1:[0-9]+]] { ; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[TMP0:%.*]] = call { ptr, i64 } @__alloc_token___size_returning_new(i64 10, i64 6985720287680550851), !alloc_token [[META0:![0-9]+]] +; CHECK-NEXT: [[TMP0:%.*]] = call { ptr, i64 } @__alloc_token___size_returning_new(i64 10, i64 2689373973731826898), !alloc_token [[META0:![0-9]+]] ; CHECK-NEXT: [[PTR1:%.*]] = extractvalue { ptr, i64 } [[TMP0]], 0 ; CHECK-NEXT: ret ptr [[PTR1]] ; @@ -28,8 +28,8 @@ define ptr @test_replaceable_new() sanitize_alloc_token { ; CHECK-LABEL: define ptr @test_replaceable_new( ; CHECK-SAME: ) #[[ATTR1]] { ; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token__Znwm(i64 32, i64 6985720287680550851), !alloc_token [[META0]] -; CHECK-NEXT: [[TMP1:%.*]] = call ptr @__alloc_token__Znam(i64 64, i64 6985720287680550851), !alloc_token [[META0]] +; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token__Znwm(i64 32, i64 2689373973731826898), !alloc_token [[META0]] +; CHECK-NEXT: [[TMP1:%.*]] = call ptr @__alloc_token__Znam(i64 64, i64 2689373973731826898), !alloc_token [[META0]] ; CHECK-NEXT: ret ptr [[TMP0]] ; entry: diff --git a/llvm/test/Instrumentation/AllocToken/remark.ll b/llvm/test/Instrumentation/AllocToken/remark.ll index d58ceb0..a2404526 100644 --- a/llvm/test/Instrumentation/AllocToken/remark.ll +++ b/llvm/test/Instrumentation/AllocToken/remark.ll @@ -12,7 +12,7 @@ define ptr @test_has_metadata() sanitize_alloc_token { ; CHECK-LABEL: define ptr @test_has_metadata( ; CHECK-SAME: ) #[[ATTR1:[0-9]+]] { ; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token_malloc(i64 64, i64 6985720287680550851), !alloc_token [[META0:![0-9]+]] +; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__alloc_token_malloc(i64 64, i64 2689373973731826898), !alloc_token [[META0:![0-9]+]] ; CHECK-NEXT: ret ptr [[TMP0]] ; entry: diff --git a/llvm/unittests/Support/SipHashTest.cpp b/llvm/unittests/Support/SipHashTest.cpp index 7c557eb..3037e64 100644 --- a/llvm/unittests/Support/SipHashTest.cpp +++ b/llvm/unittests/Support/SipHashTest.cpp @@ -50,6 +50,13 @@ TEST(SipHashTest, SipHash_2_4_128) { } } +// Tests for the 64-bit stable SipHash wrapper. +TEST(SipHashTest, StableSipHash) { + EXPECT_EQ(0xB2BB69BB0A2AC0F1UL, getStableSipHash("")); + EXPECT_EQ(0x9304ABFF427B72E8UL, getStableSipHash("strlen")); + EXPECT_EQ(0x55F45179A08AE51BUL, getStableSipHash("_ZN1 ind; f")); +} + // Tests for the ptrauth-specific SipHash wrapper. TEST(SipHashTest, PointerAuthSipHash) { // Test some basic cases. |