1 //===- llvm/unittest/Support/SipHashTest.cpp ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/SipHash.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "gtest/gtest.h"
18 // Tests from the SipHash reference implementation:
19 // https://github.com/veorq/SipHash
20 // from test.c and vectors.h, with the latter vectors at the end of the file,
21 // forward-declared here.
22 extern const uint8_t ExpectedSipHash64
[64][8];
23 extern const uint8_t ExpectedSipHash128
[64][16];
25 TEST(SipHashTest
, SipHash_2_4_64
) {
27 for (size_t KI
= 0; KI
< sizeof(K
); ++KI
)
30 uint8_t In
[64], Out
[8];
31 for (size_t InLen
= 0; InLen
< sizeof(In
); ++InLen
) {
33 getSipHash_2_4_64(ArrayRef(In
, InLen
), K
, Out
);
34 for (size_t I
= 0; I
< sizeof(Out
); ++I
)
35 EXPECT_EQ(ExpectedSipHash64
[InLen
][I
], Out
[I
]);
39 TEST(SipHashTest
, SipHash_2_4_128
) {
41 for (size_t KI
= 0; KI
< sizeof(K
); ++KI
)
44 uint8_t In
[64], Out
[16];
45 for (size_t InLen
= 0; InLen
< sizeof(In
); ++InLen
) {
47 getSipHash_2_4_128(ArrayRef(In
, InLen
), K
, Out
);
48 for (size_t I
= 0; I
< sizeof(Out
); ++I
)
49 EXPECT_EQ(ExpectedSipHash128
[InLen
][I
], Out
[I
]);
53 // Tests for the ptrauth-specific SipHash wrapper.
54 TEST(SipHashTest
, PointerAuthSipHash
) {
55 // Test some basic cases.
56 EXPECT_EQ(0xE793, getPointerAuthStableSipHash(""));
57 EXPECT_EQ(0xF468, getPointerAuthStableSipHash("strlen"));
58 EXPECT_EQ(0x2D15, getPointerAuthStableSipHash("_ZN1 ind; f"));
60 // Test some known strings that are already enshrined in the ABI.
61 EXPECT_EQ(0x6AE1, getPointerAuthStableSipHash("isa"));
62 EXPECT_EQ(0xB5AB, getPointerAuthStableSipHash("objc_class:superclass"));
63 EXPECT_EQ(0xC0BB, getPointerAuthStableSipHash("block_descriptor"));
64 EXPECT_EQ(0xC310, getPointerAuthStableSipHash("method_list_t"));
66 // Test limit cases where we differ from naive truncations from 64-bit hashes.
67 EXPECT_EQ(1, getPointerAuthStableSipHash("_Zptrkvttf"));
68 EXPECT_EQ(0xFFFF, getPointerAuthStableSipHash("_Zaflhllod"));
71 // Below are the unmodified expected outputs from the reference vectors.h.
73 const uint8_t ExpectedSipHash64
[64][8] = {
716 const uint8_t ExpectedSipHash128
[64][16] = {
1871 } // end anonymous namespace