LLVM 22.0.0git
SipHash.cpp
Go to the documentation of this file.
1//===--- SipHash.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
15#include "siphash/SipHash.h"
16#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/Endian.h"
21#include <cstdint>
22
23using namespace llvm;
24using namespace support;
25
26#define DEBUG_TYPE "llvm-siphash"
27
29 uint8_t (&Out)[8]) {
30 siphash<2, 4>(In.data(), In.size(), K, Out);
31}
32
34 uint8_t (&Out)[16]) {
35 siphash<2, 4>(In.data(), In.size(), K, Out);
36}
37
38/// Compute an ABI-stable 16-bit hash of the given string.
40 static const uint8_t K[16] = {0xb5, 0xd4, 0xc9, 0xeb, 0x79, 0x10, 0x4a, 0x79,
41 0x6f, 0xec, 0x8b, 0x1b, 0x42, 0x87, 0x81, 0xd4};
42
43 uint8_t RawHashBytes[8];
44 getSipHash_2_4_64(arrayRefFromStringRef(Str), K, RawHashBytes);
45 uint64_t RawHash = endian::read64le(RawHashBytes);
46
47 // Produce a non-zero 16-bit discriminator.
48 uint16_t Discriminator = (RawHash % 0xFFFF) + 1;
50 dbgs() << "ptrauth stable hash discriminator: " << utostr(Discriminator)
51 << " (0x"
52 << utohexstr(Discriminator, /*Lowercase=*/false, /*Width=*/4)
53 << ")"
54 << " of: " << Str << "\n");
55 return Discriminator;
56}
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition: Debug.h:119
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
uint64_t read64le(const void *P)
Definition: Endian.h:432
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void getSipHash_2_4_64(ArrayRef< uint8_t > In, const uint8_t(&K)[16], uint8_t(&Out)[8])
Computes a SipHash-2-4 64-bit result.
Definition: SipHash.cpp:28
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
LLVM_ABI uint16_t getPointerAuthStableSipHash(StringRef S)
Compute a stable non-zero 16-bit hash of the given string.
Definition: SipHash.cpp:39
LLVM_ABI void getSipHash_2_4_128(ArrayRef< uint8_t > In, const uint8_t(&K)[16], uint8_t(&Out)[16])
Computes a SipHash-2-4 128-bit result.
Definition: SipHash.cpp:33