1 1.2 riastrad /* $NetBSD: t_hash.c,v 1.2 2025/04/16 02:27:17 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.2 riastrad * https://web.archive.org/web/20241216221811/https://www.sco.com/developers/devspecs/gabi41.pdf#page=95 48 1.1 riastrad */ 49 1.1 riastrad static uint64_t 50 1.1 riastrad sysv_broken_hash(const char *name) 51 1.1 riastrad { 52 1.1 riastrad uint64_t h = 0, g; 53 1.1 riastrad 54 1.1 riastrad while (*name) { 55 1.1 riastrad h = (h << 4) + *name++; 56 1.1 riastrad if ((g = h & 0xf0000000) != 0) 57 1.1 riastrad h ^= g >> 24; 58 1.1 riastrad h &= ~g; 59 1.1 riastrad } 60 1.1 riastrad 61 1.1 riastrad return h; 62 1.1 riastrad } 63 1.1 riastrad 64 1.1 riastrad ATF_TC(sysv); 65 1.1 riastrad ATF_TC_HEAD(sysv, tc) 66 1.1 riastrad { 67 1.1 riastrad atf_tc_set_md_var(tc, "descr", "SysV hash (32-bit)"); 68 1.1 riastrad } 69 1.1 riastrad ATF_TC_BODY(sysv, tc) 70 1.1 riastrad { 71 1.1 riastrad static const struct kat kat[] = { 72 1.1 riastrad { "", 0x00000000 }, 73 1.1 riastrad { "a", 0x00000061 }, 74 1.1 riastrad { "aa", 0x00000671 }, 75 1.1 riastrad { "aaa", 0x00006771 }, 76 1.1 riastrad { "aaaa", 0x00067771 }, 77 1.1 riastrad { "aaaaa", 0x00677771 }, 78 1.1 riastrad { "aaaaaa", 0x06777771 }, 79 1.1 riastrad { "aaaaaaa", 0x07777711 }, 80 1.1 riastrad { "aaaaaaaa", 0x07777101 }, 81 1.1 riastrad { "aaaaaaaaa", 0x07771001 }, 82 1.1 riastrad { "ab", 0x00000672 }, 83 1.1 riastrad { "abc", 0x00006783 }, 84 1.1 riastrad { "abcd", 0x00067894 }, 85 1.1 riastrad { "abcde", 0x006789a5 }, 86 1.1 riastrad { "abcdef", 0x06789ab6 }, 87 1.1 riastrad { "abcdefg", 0x0789aba7 }, 88 1.1 riastrad { "abcdefgh", 0x089abaa8 }, 89 1.1 riastrad { "abcdefghi", 0x09abaa69 }, 90 1.1 riastrad /* https://maskray.me/blog/2023-04-12-elf-hash-function */ 91 1.1 riastrad { "Z", 0x0000005a }, 92 1.1 riastrad { "ZZ", 0x000005fa }, 93 1.1 riastrad { "ZZZ", 0x00005ffa }, 94 1.1 riastrad { "ZZZZ", 0x0005fffa }, 95 1.1 riastrad { "ZZZZZ", 0x005ffffa }, 96 1.1 riastrad { "ZZZZZW", 0x05fffff7 }, 97 1.1 riastrad { "ZZZZZW9", 0x0ffffff9 }, 98 1.1 riastrad { "ZZZZZW9p", 0x00000000 }, 99 1.1 riastrad { "pneumonoultramicroscopicsilicovolcanoconiosis", 100 1.1 riastrad 0x051706b3 }, 101 1.1 riastrad }; 102 1.1 riastrad unsigned i; 103 1.1 riastrad 104 1.1 riastrad for (i = 0; i < __arraycount(kat); i++) { 105 1.1 riastrad unsigned long long h = _rtld_sysv_hash(kat[i].in); 106 1.1 riastrad 107 1.1 riastrad ATF_CHECK_EQ_MSG(h, kat[i].out, 108 1.1 riastrad "[%u] _rtld_hash_sysv(\"%s\") = 0x%08llx != 0x%08llx", 109 1.1 riastrad i, kat[i].in, h, kat[i].out); 110 1.1 riastrad } 111 1.1 riastrad } 112 1.1 riastrad 113 1.1 riastrad ATF_TC(sysv_broken); 114 1.1 riastrad ATF_TC_HEAD(sysv_broken, tc) 115 1.1 riastrad { 116 1.1 riastrad atf_tc_set_md_var(tc, "descr", 117 1.1 riastrad "SysV hash (broken with 64-bit unsigned long)"); 118 1.1 riastrad } 119 1.1 riastrad ATF_TC_BODY(sysv_broken, tc) 120 1.1 riastrad { 121 1.1 riastrad static const struct kat kat[] = { 122 1.1 riastrad { "", 0x00000000 }, 123 1.1 riastrad { "a", 0x00000061 }, 124 1.1 riastrad { "aa", 0x00000671 }, 125 1.1 riastrad { "aaa", 0x00006771 }, 126 1.1 riastrad { "aaaa", 0x00067771 }, 127 1.1 riastrad { "aaaaa", 0x00677771 }, 128 1.1 riastrad { "aaaaaa", 0x06777771 }, 129 1.1 riastrad { "aaaaaaa", 0x07777711 }, 130 1.1 riastrad { "aaaaaaaa", 0x07777101 }, 131 1.1 riastrad { "aaaaaaaaa", 0x07771001 }, 132 1.1 riastrad { "ab", 0x00000672 }, 133 1.1 riastrad { "abc", 0x00006783 }, 134 1.1 riastrad { "abcd", 0x00067894 }, 135 1.1 riastrad { "abcde", 0x006789a5 }, 136 1.1 riastrad { "abcdef", 0x06789ab6 }, 137 1.1 riastrad { "abcdefg", 0x0789aba7 }, 138 1.1 riastrad { "abcdefgh", 0x089abaa8 }, 139 1.1 riastrad { "abcdefghi", 0x09abaa69 }, 140 1.1 riastrad /* https://maskray.me/blog/2023-04-12-elf-hash-function */ 141 1.1 riastrad { "Z", 0x0000005a }, 142 1.1 riastrad { "ZZ", 0x000005fa }, 143 1.1 riastrad { "ZZZ", 0x00005ffa }, 144 1.1 riastrad { "ZZZZ", 0x0005fffa }, 145 1.1 riastrad { "ZZZZZ", 0x005ffffa }, 146 1.1 riastrad { "ZZZZZW", 0x05fffff7 }, 147 1.1 riastrad { "ZZZZZW9", 0x0ffffff9 }, 148 1.1 riastrad { "ZZZZZW9p", 0x100000000 }, 149 1.1 riastrad { "pneumonoultramicroscopicsilicovolcanoconiosis", 150 1.1 riastrad 0x051706b3 }, 151 1.1 riastrad }; 152 1.1 riastrad unsigned i; 153 1.1 riastrad 154 1.1 riastrad for (i = 0; i < __arraycount(kat); i++) { 155 1.1 riastrad unsigned long long h = sysv_broken_hash(kat[i].in); 156 1.1 riastrad 157 1.1 riastrad ATF_CHECK_EQ_MSG(h, kat[i].out, 158 1.1 riastrad "[%u] sysv_broken_hash(\"%s\") = 0x%08llx != 0x%08llx", 159 1.1 riastrad i, kat[i].in, h, kat[i].out); 160 1.1 riastrad } 161 1.1 riastrad } 162 1.1 riastrad 163 1.1 riastrad ATF_TC(gnu); 164 1.1 riastrad ATF_TC_HEAD(gnu, tc) 165 1.1 riastrad { 166 1.1 riastrad atf_tc_set_md_var(tc, "descr", "GNU hash (djb2)"); 167 1.1 riastrad } 168 1.1 riastrad ATF_TC_BODY(gnu, tc) 169 1.1 riastrad { 170 1.1 riastrad static const struct kat kat[] = { 171 1.1 riastrad { """", 0x00001505 }, 172 1.1 riastrad { "a", 0x0002b606 }, 173 1.1 riastrad { "aa", 0x00597727 }, 174 1.1 riastrad { "aaa", 0x0b885c68 }, 175 1.1 riastrad { "aaaa", 0x7c93e9c9 }, 176 1.1 riastrad { "aaaaa", 0x0f11234a }, 177 1.1 riastrad { "aaaaaa", 0xf1358ceb }, 178 1.1 riastrad { "aaaaaaa", 0x17e72aac }, 179 1.1 riastrad { "aaaaaaaa", 0x14cc808d }, 180 1.1 riastrad { "aaaaaaaaa", 0xae5c928e }, 181 1.1 riastrad { "ab", 0x00597728 }, 182 1.1 riastrad { "abc", 0x0b885c8b }, 183 1.1 riastrad { "abcd", 0x7c93ee4f }, 184 1.1 riastrad { "abcde", 0x0f11b894 }, 185 1.1 riastrad { "abcdef", 0xf148cb7a }, 186 1.1 riastrad { "abcdefg", 0x1a623b21 }, 187 1.1 riastrad { "abcdefgh", 0x66a99fa9 }, 188 1.1 riastrad { "abcdefghi", 0x3bdd9532 }, 189 1.1 riastrad { "Z", 0x0002b5ff }, 190 1.1 riastrad { "ZZ", 0x00597639 }, 191 1.1 riastrad { "ZZZ", 0x0b883db3 }, 192 1.1 riastrad { "ZZZZ", 0x7c8ff46d }, 193 1.1 riastrad { "ZZZZZ", 0x0e8e8267 }, 194 1.1 riastrad { "ZZZZZW", 0xe05ecf9e }, 195 1.1 riastrad { "ZZZZZW9", 0xec38c397 }, 196 1.1 riastrad { "ZZZZZW9p", 0x735136e7 }, 197 1.1 riastrad { "pneumonoultramicroscopicsilicovolcanoconiosis", 198 1.1 riastrad 0xee6245b5 }, 199 1.1 riastrad }; 200 1.1 riastrad unsigned i; 201 1.1 riastrad 202 1.1 riastrad for (i = 0; i < __arraycount(kat); i++) { 203 1.1 riastrad unsigned long long h = _rtld_gnu_hash(kat[i].in); 204 1.1 riastrad 205 1.1 riastrad ATF_CHECK_EQ_MSG(h, kat[i].out, 206 1.1 riastrad "[%u] _rtld_gnu_hash(\"%s\") = 0x%08llx != 0x%08llx", 207 1.1 riastrad i, kat[i].in, h, kat[i].out); 208 1.1 riastrad } 209 1.1 riastrad } 210 1.1 riastrad 211 1.1 riastrad ATF_TP_ADD_TCS(tp) 212 1.1 riastrad { 213 1.1 riastrad 214 1.1 riastrad ATF_TP_ADD_TC(tp, gnu); 215 1.1 riastrad ATF_TP_ADD_TC(tp, sysv); 216 1.1 riastrad ATF_TP_ADD_TC(tp, sysv_broken); 217 1.1 riastrad 218 1.1 riastrad return atf_no_error(); 219 1.1 riastrad } 220