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