t_hash.c revision 1.1 1 1.1 riastrad /* $NetBSD: t_hash.c,v 1.1 2023/07/30 09:22:02 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.1 riastrad
31 1.1 riastrad #include <atf-c.h>
32 1.1 riastrad #include <stdint.h>
33 1.1 riastrad
34 1.1 riastrad #include "hash.h"
35 1.1 riastrad
36 1.1 riastrad /* known-answer tests */
37 1.1 riastrad struct kat {
38 1.1 riastrad const char *in;
39 1.1 riastrad unsigned long long out;
40 1.1 riastrad };
41 1.1 riastrad
42 1.1 riastrad /*
43 1.1 riastrad * From the SysV spec, with uint64_t instead of unsigned long to
44 1.1 riastrad * illustrate the problem on all systems, not just LP64 ones.
45 1.1 riastrad *
46 1.1 riastrad * https://www.sco.com/developers/devspecs/gabi41.pdf#page=95
47 1.1 riastrad */
48 1.1 riastrad static uint64_t
49 1.1 riastrad sysv_broken_hash(const char *name)
50 1.1 riastrad {
51 1.1 riastrad uint64_t h = 0, g;
52 1.1 riastrad
53 1.1 riastrad while (*name) {
54 1.1 riastrad h = (h << 4) + *name++;
55 1.1 riastrad if ((g = h & 0xf0000000) != 0)
56 1.1 riastrad h ^= g >> 24;
57 1.1 riastrad h &= ~g;
58 1.1 riastrad }
59 1.1 riastrad
60 1.1 riastrad return h;
61 1.1 riastrad }
62 1.1 riastrad
63 1.1 riastrad ATF_TC(sysv);
64 1.1 riastrad ATF_TC_HEAD(sysv, tc)
65 1.1 riastrad {
66 1.1 riastrad atf_tc_set_md_var(tc, "descr", "SysV hash (32-bit)");
67 1.1 riastrad }
68 1.1 riastrad ATF_TC_BODY(sysv, tc)
69 1.1 riastrad {
70 1.1 riastrad static const struct kat kat[] = {
71 1.1 riastrad { "", 0x00000000 },
72 1.1 riastrad { "a", 0x00000061 },
73 1.1 riastrad { "aa", 0x00000671 },
74 1.1 riastrad { "aaa", 0x00006771 },
75 1.1 riastrad { "aaaa", 0x00067771 },
76 1.1 riastrad { "aaaaa", 0x00677771 },
77 1.1 riastrad { "aaaaaa", 0x06777771 },
78 1.1 riastrad { "aaaaaaa", 0x07777711 },
79 1.1 riastrad { "aaaaaaaa", 0x07777101 },
80 1.1 riastrad { "aaaaaaaaa", 0x07771001 },
81 1.1 riastrad { "ab", 0x00000672 },
82 1.1 riastrad { "abc", 0x00006783 },
83 1.1 riastrad { "abcd", 0x00067894 },
84 1.1 riastrad { "abcde", 0x006789a5 },
85 1.1 riastrad { "abcdef", 0x06789ab6 },
86 1.1 riastrad { "abcdefg", 0x0789aba7 },
87 1.1 riastrad { "abcdefgh", 0x089abaa8 },
88 1.1 riastrad { "abcdefghi", 0x09abaa69 },
89 1.1 riastrad /* https://maskray.me/blog/2023-04-12-elf-hash-function */
90 1.1 riastrad { "Z", 0x0000005a },
91 1.1 riastrad { "ZZ", 0x000005fa },
92 1.1 riastrad { "ZZZ", 0x00005ffa },
93 1.1 riastrad { "ZZZZ", 0x0005fffa },
94 1.1 riastrad { "ZZZZZ", 0x005ffffa },
95 1.1 riastrad { "ZZZZZW", 0x05fffff7 },
96 1.1 riastrad { "ZZZZZW9", 0x0ffffff9 },
97 1.1 riastrad { "ZZZZZW9p", 0x00000000 },
98 1.1 riastrad { "pneumonoultramicroscopicsilicovolcanoconiosis",
99 1.1 riastrad 0x051706b3 },
100 1.1 riastrad };
101 1.1 riastrad unsigned i;
102 1.1 riastrad
103 1.1 riastrad for (i = 0; i < __arraycount(kat); i++) {
104 1.1 riastrad unsigned long long h = _rtld_sysv_hash(kat[i].in);
105 1.1 riastrad
106 1.1 riastrad ATF_CHECK_EQ_MSG(h, kat[i].out,
107 1.1 riastrad "[%u] _rtld_hash_sysv(\"%s\") = 0x%08llx != 0x%08llx",
108 1.1 riastrad i, kat[i].in, h, kat[i].out);
109 1.1 riastrad }
110 1.1 riastrad }
111 1.1 riastrad
112 1.1 riastrad ATF_TC(sysv_broken);
113 1.1 riastrad ATF_TC_HEAD(sysv_broken, tc)
114 1.1 riastrad {
115 1.1 riastrad atf_tc_set_md_var(tc, "descr",
116 1.1 riastrad "SysV hash (broken with 64-bit unsigned long)");
117 1.1 riastrad }
118 1.1 riastrad ATF_TC_BODY(sysv_broken, tc)
119 1.1 riastrad {
120 1.1 riastrad static const struct kat kat[] = {
121 1.1 riastrad { "", 0x00000000 },
122 1.1 riastrad { "a", 0x00000061 },
123 1.1 riastrad { "aa", 0x00000671 },
124 1.1 riastrad { "aaa", 0x00006771 },
125 1.1 riastrad { "aaaa", 0x00067771 },
126 1.1 riastrad { "aaaaa", 0x00677771 },
127 1.1 riastrad { "aaaaaa", 0x06777771 },
128 1.1 riastrad { "aaaaaaa", 0x07777711 },
129 1.1 riastrad { "aaaaaaaa", 0x07777101 },
130 1.1 riastrad { "aaaaaaaaa", 0x07771001 },
131 1.1 riastrad { "ab", 0x00000672 },
132 1.1 riastrad { "abc", 0x00006783 },
133 1.1 riastrad { "abcd", 0x00067894 },
134 1.1 riastrad { "abcde", 0x006789a5 },
135 1.1 riastrad { "abcdef", 0x06789ab6 },
136 1.1 riastrad { "abcdefg", 0x0789aba7 },
137 1.1 riastrad { "abcdefgh", 0x089abaa8 },
138 1.1 riastrad { "abcdefghi", 0x09abaa69 },
139 1.1 riastrad /* https://maskray.me/blog/2023-04-12-elf-hash-function */
140 1.1 riastrad { "Z", 0x0000005a },
141 1.1 riastrad { "ZZ", 0x000005fa },
142 1.1 riastrad { "ZZZ", 0x00005ffa },
143 1.1 riastrad { "ZZZZ", 0x0005fffa },
144 1.1 riastrad { "ZZZZZ", 0x005ffffa },
145 1.1 riastrad { "ZZZZZW", 0x05fffff7 },
146 1.1 riastrad { "ZZZZZW9", 0x0ffffff9 },
147 1.1 riastrad { "ZZZZZW9p", 0x100000000 },
148 1.1 riastrad { "pneumonoultramicroscopicsilicovolcanoconiosis",
149 1.1 riastrad 0x051706b3 },
150 1.1 riastrad };
151 1.1 riastrad unsigned i;
152 1.1 riastrad
153 1.1 riastrad for (i = 0; i < __arraycount(kat); i++) {
154 1.1 riastrad unsigned long long h = sysv_broken_hash(kat[i].in);
155 1.1 riastrad
156 1.1 riastrad ATF_CHECK_EQ_MSG(h, kat[i].out,
157 1.1 riastrad "[%u] sysv_broken_hash(\"%s\") = 0x%08llx != 0x%08llx",
158 1.1 riastrad i, kat[i].in, h, kat[i].out);
159 1.1 riastrad }
160 1.1 riastrad }
161 1.1 riastrad
162 1.1 riastrad ATF_TC(gnu);
163 1.1 riastrad ATF_TC_HEAD(gnu, tc)
164 1.1 riastrad {
165 1.1 riastrad atf_tc_set_md_var(tc, "descr", "GNU hash (djb2)");
166 1.1 riastrad }
167 1.1 riastrad ATF_TC_BODY(gnu, tc)
168 1.1 riastrad {
169 1.1 riastrad static const struct kat kat[] = {
170 1.1 riastrad { """", 0x00001505 },
171 1.1 riastrad { "a", 0x0002b606 },
172 1.1 riastrad { "aa", 0x00597727 },
173 1.1 riastrad { "aaa", 0x0b885c68 },
174 1.1 riastrad { "aaaa", 0x7c93e9c9 },
175 1.1 riastrad { "aaaaa", 0x0f11234a },
176 1.1 riastrad { "aaaaaa", 0xf1358ceb },
177 1.1 riastrad { "aaaaaaa", 0x17e72aac },
178 1.1 riastrad { "aaaaaaaa", 0x14cc808d },
179 1.1 riastrad { "aaaaaaaaa", 0xae5c928e },
180 1.1 riastrad { "ab", 0x00597728 },
181 1.1 riastrad { "abc", 0x0b885c8b },
182 1.1 riastrad { "abcd", 0x7c93ee4f },
183 1.1 riastrad { "abcde", 0x0f11b894 },
184 1.1 riastrad { "abcdef", 0xf148cb7a },
185 1.1 riastrad { "abcdefg", 0x1a623b21 },
186 1.1 riastrad { "abcdefgh", 0x66a99fa9 },
187 1.1 riastrad { "abcdefghi", 0x3bdd9532 },
188 1.1 riastrad { "Z", 0x0002b5ff },
189 1.1 riastrad { "ZZ", 0x00597639 },
190 1.1 riastrad { "ZZZ", 0x0b883db3 },
191 1.1 riastrad { "ZZZZ", 0x7c8ff46d },
192 1.1 riastrad { "ZZZZZ", 0x0e8e8267 },
193 1.1 riastrad { "ZZZZZW", 0xe05ecf9e },
194 1.1 riastrad { "ZZZZZW9", 0xec38c397 },
195 1.1 riastrad { "ZZZZZW9p", 0x735136e7 },
196 1.1 riastrad { "pneumonoultramicroscopicsilicovolcanoconiosis",
197 1.1 riastrad 0xee6245b5 },
198 1.1 riastrad };
199 1.1 riastrad unsigned i;
200 1.1 riastrad
201 1.1 riastrad for (i = 0; i < __arraycount(kat); i++) {
202 1.1 riastrad unsigned long long h = _rtld_gnu_hash(kat[i].in);
203 1.1 riastrad
204 1.1 riastrad ATF_CHECK_EQ_MSG(h, kat[i].out,
205 1.1 riastrad "[%u] _rtld_gnu_hash(\"%s\") = 0x%08llx != 0x%08llx",
206 1.1 riastrad i, kat[i].in, h, kat[i].out);
207 1.1 riastrad }
208 1.1 riastrad }
209 1.1 riastrad
210 1.1 riastrad ATF_TP_ADD_TCS(tp)
211 1.1 riastrad {
212 1.1 riastrad
213 1.1 riastrad ATF_TP_ADD_TC(tp, gnu);
214 1.1 riastrad ATF_TP_ADD_TC(tp, sysv);
215 1.1 riastrad ATF_TP_ADD_TC(tp, sysv_broken);
216 1.1 riastrad
217 1.1 riastrad return atf_no_error();
218 1.1 riastrad }
219