xref: /linux-6.15/lib/tests/test_hash.c (revision db6fe4d6)
1*db6fe4d6SKees Cook // SPDX-License-Identifier: GPL-2.0-only
2*db6fe4d6SKees Cook /*
3*db6fe4d6SKees Cook  * Test cases for <linux/hash.h> and <linux/stringhash.h>
4*db6fe4d6SKees Cook  * This just verifies that various ways of computing a hash
5*db6fe4d6SKees Cook  * produce the same thing and, for cases where a k-bit hash
6*db6fe4d6SKees Cook  * value is requested, is of the requested size.
7*db6fe4d6SKees Cook  *
8*db6fe4d6SKees Cook  * We fill a buffer with a 255-byte null-terminated string,
9*db6fe4d6SKees Cook  * and use both full_name_hash() and hashlen_string() to hash the
10*db6fe4d6SKees Cook  * substrings from i to j, where 0 <= i < j < 256.
11*db6fe4d6SKees Cook  *
12*db6fe4d6SKees Cook  * The returned values are used to check that __hash_32() and
13*db6fe4d6SKees Cook  * __hash_32_generic() compute the same thing.  Likewise hash_32()
14*db6fe4d6SKees Cook  * and hash_64().
15*db6fe4d6SKees Cook  */
16*db6fe4d6SKees Cook 
17*db6fe4d6SKees Cook #include <linux/compiler.h>
18*db6fe4d6SKees Cook #include <linux/types.h>
19*db6fe4d6SKees Cook #include <linux/module.h>
20*db6fe4d6SKees Cook #include <linux/hash.h>
21*db6fe4d6SKees Cook #include <linux/stringhash.h>
22*db6fe4d6SKees Cook #include <kunit/test.h>
23*db6fe4d6SKees Cook 
24*db6fe4d6SKees Cook /* 32-bit XORSHIFT generator.  Seed must not be zero. */
25*db6fe4d6SKees Cook static u32 __attribute_const__
xorshift(u32 seed)26*db6fe4d6SKees Cook xorshift(u32 seed)
27*db6fe4d6SKees Cook {
28*db6fe4d6SKees Cook 	seed ^= seed << 13;
29*db6fe4d6SKees Cook 	seed ^= seed >> 17;
30*db6fe4d6SKees Cook 	seed ^= seed << 5;
31*db6fe4d6SKees Cook 	return seed;
32*db6fe4d6SKees Cook }
33*db6fe4d6SKees Cook 
34*db6fe4d6SKees Cook /* Given a non-zero x, returns a non-zero byte. */
35*db6fe4d6SKees Cook static u8 __attribute_const__
mod255(u32 x)36*db6fe4d6SKees Cook mod255(u32 x)
37*db6fe4d6SKees Cook {
38*db6fe4d6SKees Cook 	x = (x & 0xffff) + (x >> 16);	/* 1 <= x <= 0x1fffe */
39*db6fe4d6SKees Cook 	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0x2fd */
40*db6fe4d6SKees Cook 	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0x100 */
41*db6fe4d6SKees Cook 	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0xff */
42*db6fe4d6SKees Cook 	return x;
43*db6fe4d6SKees Cook }
44*db6fe4d6SKees Cook 
45*db6fe4d6SKees Cook /* Fill the buffer with non-zero bytes. */
fill_buf(char * buf,size_t len,u32 seed)46*db6fe4d6SKees Cook static void fill_buf(char *buf, size_t len, u32 seed)
47*db6fe4d6SKees Cook {
48*db6fe4d6SKees Cook 	size_t i;
49*db6fe4d6SKees Cook 
50*db6fe4d6SKees Cook 	for (i = 0; i < len; i++) {
51*db6fe4d6SKees Cook 		seed = xorshift(seed);
52*db6fe4d6SKees Cook 		buf[i] = mod255(seed);
53*db6fe4d6SKees Cook 	}
54*db6fe4d6SKees Cook }
55*db6fe4d6SKees Cook 
56*db6fe4d6SKees Cook /* Holds most testing variables for the int test. */
57*db6fe4d6SKees Cook struct test_hash_params {
58*db6fe4d6SKees Cook         /* Pointer to integer to be hashed. */
59*db6fe4d6SKees Cook 	unsigned long long *h64;
60*db6fe4d6SKees Cook         /* Low 32-bits of integer to be hashed. */
61*db6fe4d6SKees Cook 	u32 h0;
62*db6fe4d6SKees Cook         /* Arch-specific hash result. */
63*db6fe4d6SKees Cook 	u32 h1;
64*db6fe4d6SKees Cook         /* Generic hash result. */
65*db6fe4d6SKees Cook 	u32 h2;
66*db6fe4d6SKees Cook         /* ORed hashes of given size (in bits). */
67*db6fe4d6SKees Cook 	u32 (*hash_or)[33];
68*db6fe4d6SKees Cook };
69*db6fe4d6SKees Cook 
70*db6fe4d6SKees Cook #ifdef HAVE_ARCH__HASH_32
71*db6fe4d6SKees Cook static void
test_int__hash_32(struct kunit * test,struct test_hash_params * params)72*db6fe4d6SKees Cook test_int__hash_32(struct kunit *test, struct test_hash_params *params)
73*db6fe4d6SKees Cook {
74*db6fe4d6SKees Cook 	params->hash_or[1][0] |= params->h2 = __hash_32_generic(params->h0);
75*db6fe4d6SKees Cook #if HAVE_ARCH__HASH_32 == 1
76*db6fe4d6SKees Cook 	KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
77*db6fe4d6SKees Cook 			    "__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
78*db6fe4d6SKees Cook 			    params->h0, params->h1, params->h2);
79*db6fe4d6SKees Cook #endif
80*db6fe4d6SKees Cook }
81*db6fe4d6SKees Cook #endif
82*db6fe4d6SKees Cook 
83*db6fe4d6SKees Cook #ifdef HAVE_ARCH_HASH_64
84*db6fe4d6SKees Cook static void
test_int_hash_64(struct kunit * test,struct test_hash_params * params,u32 const * m,int * k)85*db6fe4d6SKees Cook test_int_hash_64(struct kunit *test, struct test_hash_params *params, u32 const *m, int *k)
86*db6fe4d6SKees Cook {
87*db6fe4d6SKees Cook 	params->h2 = hash_64_generic(*params->h64, *k);
88*db6fe4d6SKees Cook #if HAVE_ARCH_HASH_64 == 1
89*db6fe4d6SKees Cook 	KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
90*db6fe4d6SKees Cook 			    "hash_64(%#llx, %d) = %#x != hash_64_generic() = %#x",
91*db6fe4d6SKees Cook 			    *params->h64, *k, params->h1, params->h2);
92*db6fe4d6SKees Cook #else
93*db6fe4d6SKees Cook 	KUNIT_EXPECT_LE_MSG(test, params->h1, params->h2,
94*db6fe4d6SKees Cook 			    "hash_64_generic(%#llx, %d) = %#x > %#x",
95*db6fe4d6SKees Cook 			    *params->h64, *k, params->h1, *m);
96*db6fe4d6SKees Cook #endif
97*db6fe4d6SKees Cook }
98*db6fe4d6SKees Cook #endif
99*db6fe4d6SKees Cook 
100*db6fe4d6SKees Cook /*
101*db6fe4d6SKees Cook  * Test the various integer hash functions.  h64 (or its low-order bits)
102*db6fe4d6SKees Cook  * is the integer to hash.  hash_or accumulates the OR of the hash values,
103*db6fe4d6SKees Cook  * which are later checked to see that they cover all the requested bits.
104*db6fe4d6SKees Cook  *
105*db6fe4d6SKees Cook  * Because these functions (as opposed to the string hashes) are all
106*db6fe4d6SKees Cook  * inline, the code being tested is actually in the module, and you can
107*db6fe4d6SKees Cook  * recompile and re-test the module without rebooting.
108*db6fe4d6SKees Cook  */
109*db6fe4d6SKees Cook static void
test_int_hash(struct kunit * test,unsigned long long h64,u32 hash_or[2][33])110*db6fe4d6SKees Cook test_int_hash(struct kunit *test, unsigned long long h64, u32 hash_or[2][33])
111*db6fe4d6SKees Cook {
112*db6fe4d6SKees Cook 	int k;
113*db6fe4d6SKees Cook 	struct test_hash_params params = { &h64, (u32)h64, 0, 0, hash_or };
114*db6fe4d6SKees Cook 
115*db6fe4d6SKees Cook 	/* Test __hash32 */
116*db6fe4d6SKees Cook 	hash_or[0][0] |= params.h1 = __hash_32(params.h0);
117*db6fe4d6SKees Cook #ifdef HAVE_ARCH__HASH_32
118*db6fe4d6SKees Cook 	test_int__hash_32(test, &params);
119*db6fe4d6SKees Cook #endif
120*db6fe4d6SKees Cook 
121*db6fe4d6SKees Cook 	/* Test k = 1..32 bits */
122*db6fe4d6SKees Cook 	for (k = 1; k <= 32; k++) {
123*db6fe4d6SKees Cook 		u32 const m = ((u32)2 << (k-1)) - 1;	/* Low k bits set */
124*db6fe4d6SKees Cook 
125*db6fe4d6SKees Cook 		/* Test hash_32 */
126*db6fe4d6SKees Cook 		hash_or[0][k] |= params.h1 = hash_32(params.h0, k);
127*db6fe4d6SKees Cook 		KUNIT_EXPECT_LE_MSG(test, params.h1, m,
128*db6fe4d6SKees Cook 				    "hash_32(%#x, %d) = %#x > %#x",
129*db6fe4d6SKees Cook 				    params.h0, k, params.h1, m);
130*db6fe4d6SKees Cook 
131*db6fe4d6SKees Cook 		/* Test hash_64 */
132*db6fe4d6SKees Cook 		hash_or[1][k] |= params.h1 = hash_64(h64, k);
133*db6fe4d6SKees Cook 		KUNIT_EXPECT_LE_MSG(test, params.h1, m,
134*db6fe4d6SKees Cook 				    "hash_64(%#llx, %d) = %#x > %#x",
135*db6fe4d6SKees Cook 				    h64, k, params.h1, m);
136*db6fe4d6SKees Cook #ifdef HAVE_ARCH_HASH_64
137*db6fe4d6SKees Cook 		test_int_hash_64(test, &params, &m, &k);
138*db6fe4d6SKees Cook #endif
139*db6fe4d6SKees Cook 	}
140*db6fe4d6SKees Cook }
141*db6fe4d6SKees Cook 
142*db6fe4d6SKees Cook #define SIZE 256	/* Run time is cubic in SIZE */
143*db6fe4d6SKees Cook 
test_string_or(struct kunit * test)144*db6fe4d6SKees Cook static void test_string_or(struct kunit *test)
145*db6fe4d6SKees Cook {
146*db6fe4d6SKees Cook 	char buf[SIZE+1];
147*db6fe4d6SKees Cook 	u32 string_or = 0;
148*db6fe4d6SKees Cook 	int i, j;
149*db6fe4d6SKees Cook 
150*db6fe4d6SKees Cook 	fill_buf(buf, SIZE, 1);
151*db6fe4d6SKees Cook 
152*db6fe4d6SKees Cook 	/* Test every possible non-empty substring in the buffer. */
153*db6fe4d6SKees Cook 	for (j = SIZE; j > 0; --j) {
154*db6fe4d6SKees Cook 		buf[j] = '\0';
155*db6fe4d6SKees Cook 
156*db6fe4d6SKees Cook 		for (i = 0; i <= j; i++) {
157*db6fe4d6SKees Cook 			u32 h0 = full_name_hash(buf+i, buf+i, j-i);
158*db6fe4d6SKees Cook 
159*db6fe4d6SKees Cook 			string_or |= h0;
160*db6fe4d6SKees Cook 		} /* i */
161*db6fe4d6SKees Cook 	} /* j */
162*db6fe4d6SKees Cook 
163*db6fe4d6SKees Cook 	/* The OR of all the hash values should cover all the bits */
164*db6fe4d6SKees Cook 	KUNIT_EXPECT_EQ_MSG(test, string_or, -1u,
165*db6fe4d6SKees Cook 			    "OR of all string hash results = %#x != %#x",
166*db6fe4d6SKees Cook 			    string_or, -1u);
167*db6fe4d6SKees Cook }
168*db6fe4d6SKees Cook 
test_hash_or(struct kunit * test)169*db6fe4d6SKees Cook static void test_hash_or(struct kunit *test)
170*db6fe4d6SKees Cook {
171*db6fe4d6SKees Cook 	char buf[SIZE+1];
172*db6fe4d6SKees Cook 	u32 hash_or[2][33] = { { 0, } };
173*db6fe4d6SKees Cook 	unsigned long long h64 = 0;
174*db6fe4d6SKees Cook 	int i, j;
175*db6fe4d6SKees Cook 
176*db6fe4d6SKees Cook 	fill_buf(buf, SIZE, 1);
177*db6fe4d6SKees Cook 
178*db6fe4d6SKees Cook 	/* Test every possible non-empty substring in the buffer. */
179*db6fe4d6SKees Cook 	for (j = SIZE; j > 0; --j) {
180*db6fe4d6SKees Cook 		buf[j] = '\0';
181*db6fe4d6SKees Cook 
182*db6fe4d6SKees Cook 		for (i = 0; i <= j; i++) {
183*db6fe4d6SKees Cook 			u64 hashlen = hashlen_string(buf+i, buf+i);
184*db6fe4d6SKees Cook 			u32 h0 = full_name_hash(buf+i, buf+i, j-i);
185*db6fe4d6SKees Cook 
186*db6fe4d6SKees Cook 			/* Check that hashlen_string gets the length right */
187*db6fe4d6SKees Cook 			KUNIT_EXPECT_EQ_MSG(test, hashlen_len(hashlen), j-i,
188*db6fe4d6SKees Cook 					    "hashlen_string(%d..%d) returned length %u, expected %d",
189*db6fe4d6SKees Cook 					    i, j, hashlen_len(hashlen), j-i);
190*db6fe4d6SKees Cook 			/* Check that the hashes match */
191*db6fe4d6SKees Cook 			KUNIT_EXPECT_EQ_MSG(test, hashlen_hash(hashlen), h0,
192*db6fe4d6SKees Cook 					    "hashlen_string(%d..%d) = %08x != full_name_hash() = %08x",
193*db6fe4d6SKees Cook 					    i, j, hashlen_hash(hashlen), h0);
194*db6fe4d6SKees Cook 
195*db6fe4d6SKees Cook 			h64 = h64 << 32 | h0;	/* For use with hash_64 */
196*db6fe4d6SKees Cook 			test_int_hash(test, h64, hash_or);
197*db6fe4d6SKees Cook 		} /* i */
198*db6fe4d6SKees Cook 	} /* j */
199*db6fe4d6SKees Cook 
200*db6fe4d6SKees Cook 	KUNIT_EXPECT_EQ_MSG(test, hash_or[0][0], -1u,
201*db6fe4d6SKees Cook 			    "OR of all __hash_32 results = %#x != %#x",
202*db6fe4d6SKees Cook 			    hash_or[0][0], -1u);
203*db6fe4d6SKees Cook #ifdef HAVE_ARCH__HASH_32
204*db6fe4d6SKees Cook #if HAVE_ARCH__HASH_32 != 1	/* Test is pointless if results match */
205*db6fe4d6SKees Cook 	KUNIT_EXPECT_EQ_MSG(test, hash_or[1][0], -1u,
206*db6fe4d6SKees Cook 			    "OR of all __hash_32_generic results = %#x != %#x",
207*db6fe4d6SKees Cook 			    hash_or[1][0], -1u);
208*db6fe4d6SKees Cook #endif
209*db6fe4d6SKees Cook #endif
210*db6fe4d6SKees Cook 
211*db6fe4d6SKees Cook 	/* Likewise for all the i-bit hash values */
212*db6fe4d6SKees Cook 	for (i = 1; i <= 32; i++) {
213*db6fe4d6SKees Cook 		u32 const m = ((u32)2 << (i-1)) - 1;	/* Low i bits set */
214*db6fe4d6SKees Cook 
215*db6fe4d6SKees Cook 		KUNIT_EXPECT_EQ_MSG(test, hash_or[0][i], m,
216*db6fe4d6SKees Cook 				    "OR of all hash_32(%d) results = %#x (%#x expected)",
217*db6fe4d6SKees Cook 				    i, hash_or[0][i], m);
218*db6fe4d6SKees Cook 		KUNIT_EXPECT_EQ_MSG(test, hash_or[1][i], m,
219*db6fe4d6SKees Cook 				    "OR of all hash_64(%d) results = %#x (%#x expected)",
220*db6fe4d6SKees Cook 				    i, hash_or[1][i], m);
221*db6fe4d6SKees Cook 	}
222*db6fe4d6SKees Cook }
223*db6fe4d6SKees Cook 
224*db6fe4d6SKees Cook static struct kunit_case hash_test_cases[] __refdata = {
225*db6fe4d6SKees Cook 	KUNIT_CASE(test_string_or),
226*db6fe4d6SKees Cook 	KUNIT_CASE(test_hash_or),
227*db6fe4d6SKees Cook 	{}
228*db6fe4d6SKees Cook };
229*db6fe4d6SKees Cook 
230*db6fe4d6SKees Cook static struct kunit_suite hash_test_suite = {
231*db6fe4d6SKees Cook 	.name = "hash",
232*db6fe4d6SKees Cook 	.test_cases = hash_test_cases,
233*db6fe4d6SKees Cook };
234*db6fe4d6SKees Cook 
235*db6fe4d6SKees Cook 
236*db6fe4d6SKees Cook kunit_test_suite(hash_test_suite);
237*db6fe4d6SKees Cook 
238*db6fe4d6SKees Cook MODULE_DESCRIPTION("Test cases for <linux/hash.h> and <linux/stringhash.h>");
239*db6fe4d6SKees Cook MODULE_LICENSE("GPL");
240