1a0698ed9Schristos /* 2a0698ed9Schristos * This file is based on code that is part of SMHasher 3a0698ed9Schristos * (https://code.google.com/p/smhasher/), and is subject to the MIT license 4a0698ed9Schristos * (http://www.opensource.org/licenses/mit-license.php). Both email addresses 5a0698ed9Schristos * associated with the source code's revision history belong to Austin Appleby, 6a0698ed9Schristos * and the revision history ranges from 2010 to 2012. Therefore the copyright 7a0698ed9Schristos * and license are here taken to be: 8a0698ed9Schristos * 9a0698ed9Schristos * Copyright (c) 2010-2012 Austin Appleby 10a0698ed9Schristos * 11a0698ed9Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy 12a0698ed9Schristos * of this software and associated documentation files (the "Software"), to deal 13a0698ed9Schristos * in the Software without restriction, including without limitation the rights 14a0698ed9Schristos * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15a0698ed9Schristos * copies of the Software, and to permit persons to whom the Software is 16a0698ed9Schristos * furnished to do so, subject to the following conditions: 17a0698ed9Schristos * 18a0698ed9Schristos * The above copyright notice and this permission notice shall be included in 19a0698ed9Schristos * all copies or substantial portions of the Software. 20a0698ed9Schristos * 21a0698ed9Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22a0698ed9Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23a0698ed9Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24a0698ed9Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25a0698ed9Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26a0698ed9Schristos * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27a0698ed9Schristos * THE SOFTWARE. 28a0698ed9Schristos */ 29a0698ed9Schristos 30a0698ed9Schristos #include "test/jemalloc_test.h" 31a0698ed9Schristos #include "jemalloc/internal/hash.h" 32a0698ed9Schristos 33a0698ed9Schristos typedef enum { 34a0698ed9Schristos hash_variant_x86_32, 35a0698ed9Schristos hash_variant_x86_128, 36a0698ed9Schristos hash_variant_x64_128 37a0698ed9Schristos } hash_variant_t; 38a0698ed9Schristos 39a0698ed9Schristos static int 40a0698ed9Schristos hash_variant_bits(hash_variant_t variant) { 41a0698ed9Schristos switch (variant) { 42a0698ed9Schristos case hash_variant_x86_32: return 32; 43a0698ed9Schristos case hash_variant_x86_128: return 128; 44a0698ed9Schristos case hash_variant_x64_128: return 128; 45a0698ed9Schristos default: not_reached(); 46a0698ed9Schristos } 47a0698ed9Schristos } 48a0698ed9Schristos 49a0698ed9Schristos static const char * 50a0698ed9Schristos hash_variant_string(hash_variant_t variant) { 51a0698ed9Schristos switch (variant) { 52a0698ed9Schristos case hash_variant_x86_32: return "hash_x86_32"; 53a0698ed9Schristos case hash_variant_x86_128: return "hash_x86_128"; 54a0698ed9Schristos case hash_variant_x64_128: return "hash_x64_128"; 55a0698ed9Schristos default: not_reached(); 56a0698ed9Schristos } 57a0698ed9Schristos } 58a0698ed9Schristos 59a0698ed9Schristos #define KEY_SIZE 256 60a0698ed9Schristos static void 61a0698ed9Schristos hash_variant_verify_key(hash_variant_t variant, uint8_t *key) { 62a0698ed9Schristos const int hashbytes = hash_variant_bits(variant) / 8; 63a0698ed9Schristos const int hashes_size = hashbytes * 256; 64a0698ed9Schristos VARIABLE_ARRAY(uint8_t, hashes, hashes_size); 65a0698ed9Schristos VARIABLE_ARRAY(uint8_t, final, hashbytes); 66a0698ed9Schristos unsigned i; 67a0698ed9Schristos uint32_t computed, expected; 68a0698ed9Schristos 69a0698ed9Schristos memset(key, 0, KEY_SIZE); 70a0698ed9Schristos memset(hashes, 0, hashes_size); 71a0698ed9Schristos memset(final, 0, hashbytes); 72a0698ed9Schristos 73a0698ed9Schristos /* 74a0698ed9Schristos * Hash keys of the form {0}, {0,1}, {0,1,2}, ..., {0,1,...,255} as the 75a0698ed9Schristos * seed. 76a0698ed9Schristos */ 77a0698ed9Schristos for (i = 0; i < 256; i++) { 78a0698ed9Schristos key[i] = (uint8_t)i; 79a0698ed9Schristos switch (variant) { 80a0698ed9Schristos case hash_variant_x86_32: { 81a0698ed9Schristos uint32_t out; 82a0698ed9Schristos out = hash_x86_32(key, i, 256-i); 83a0698ed9Schristos memcpy(&hashes[i*hashbytes], &out, hashbytes); 84a0698ed9Schristos break; 85a0698ed9Schristos } case hash_variant_x86_128: { 86a0698ed9Schristos uint64_t out[2]; 87a0698ed9Schristos hash_x86_128(key, i, 256-i, out); 88a0698ed9Schristos memcpy(&hashes[i*hashbytes], out, hashbytes); 89a0698ed9Schristos break; 90a0698ed9Schristos } case hash_variant_x64_128: { 91a0698ed9Schristos uint64_t out[2]; 92a0698ed9Schristos hash_x64_128(key, i, 256-i, out); 93a0698ed9Schristos memcpy(&hashes[i*hashbytes], out, hashbytes); 94a0698ed9Schristos break; 95a0698ed9Schristos } default: not_reached(); 96a0698ed9Schristos } 97a0698ed9Schristos } 98a0698ed9Schristos 99a0698ed9Schristos /* Hash the result array. */ 100a0698ed9Schristos switch (variant) { 101a0698ed9Schristos case hash_variant_x86_32: { 102a0698ed9Schristos uint32_t out = hash_x86_32(hashes, hashes_size, 0); 103a0698ed9Schristos memcpy(final, &out, sizeof(out)); 104a0698ed9Schristos break; 105a0698ed9Schristos } case hash_variant_x86_128: { 106a0698ed9Schristos uint64_t out[2]; 107a0698ed9Schristos hash_x86_128(hashes, hashes_size, 0, out); 108a0698ed9Schristos memcpy(final, out, sizeof(out)); 109a0698ed9Schristos break; 110a0698ed9Schristos } case hash_variant_x64_128: { 111a0698ed9Schristos uint64_t out[2]; 112a0698ed9Schristos hash_x64_128(hashes, hashes_size, 0, out); 113a0698ed9Schristos memcpy(final, out, sizeof(out)); 114a0698ed9Schristos break; 115a0698ed9Schristos } default: not_reached(); 116a0698ed9Schristos } 117a0698ed9Schristos 118a0698ed9Schristos computed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) | 119a0698ed9Schristos (final[3] << 24); 120a0698ed9Schristos 121a0698ed9Schristos switch (variant) { 122a0698ed9Schristos #ifdef JEMALLOC_BIG_ENDIAN 123a0698ed9Schristos case hash_variant_x86_32: expected = 0x6213303eU; break; 124a0698ed9Schristos case hash_variant_x86_128: expected = 0x266820caU; break; 125a0698ed9Schristos case hash_variant_x64_128: expected = 0xcc622b6fU; break; 126a0698ed9Schristos #else 127a0698ed9Schristos case hash_variant_x86_32: expected = 0xb0f57ee3U; break; 128a0698ed9Schristos case hash_variant_x86_128: expected = 0xb3ece62aU; break; 129a0698ed9Schristos case hash_variant_x64_128: expected = 0x6384ba69U; break; 130a0698ed9Schristos #endif 131a0698ed9Schristos default: not_reached(); 132a0698ed9Schristos } 133a0698ed9Schristos 134*7bdf38e5Schristos expect_u32_eq(computed, expected, 135a0698ed9Schristos "Hash mismatch for %s(): expected %#x but got %#x", 136a0698ed9Schristos hash_variant_string(variant), expected, computed); 137a0698ed9Schristos } 138a0698ed9Schristos 139a0698ed9Schristos static void 140a0698ed9Schristos hash_variant_verify(hash_variant_t variant) { 141a0698ed9Schristos #define MAX_ALIGN 16 142a0698ed9Schristos uint8_t key[KEY_SIZE + (MAX_ALIGN - 1)]; 143a0698ed9Schristos unsigned i; 144a0698ed9Schristos 145a0698ed9Schristos for (i = 0; i < MAX_ALIGN; i++) { 146a0698ed9Schristos hash_variant_verify_key(variant, &key[i]); 147a0698ed9Schristos } 148a0698ed9Schristos #undef MAX_ALIGN 149a0698ed9Schristos } 150a0698ed9Schristos #undef KEY_SIZE 151a0698ed9Schristos 152a0698ed9Schristos TEST_BEGIN(test_hash_x86_32) { 153a0698ed9Schristos hash_variant_verify(hash_variant_x86_32); 154a0698ed9Schristos } 155a0698ed9Schristos TEST_END 156a0698ed9Schristos 157a0698ed9Schristos TEST_BEGIN(test_hash_x86_128) { 158a0698ed9Schristos hash_variant_verify(hash_variant_x86_128); 159a0698ed9Schristos } 160a0698ed9Schristos TEST_END 161a0698ed9Schristos 162a0698ed9Schristos TEST_BEGIN(test_hash_x64_128) { 163a0698ed9Schristos hash_variant_verify(hash_variant_x64_128); 164a0698ed9Schristos } 165a0698ed9Schristos TEST_END 166a0698ed9Schristos 167a0698ed9Schristos int 168a0698ed9Schristos main(void) { 169a0698ed9Schristos return test( 170a0698ed9Schristos test_hash_x86_32, 171a0698ed9Schristos test_hash_x86_128, 172a0698ed9Schristos test_hash_x64_128); 173a0698ed9Schristos } 174