Path: blob/master/src/hotspot/share/classfile/altHashing.cpp
64440 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* halfsiphash code adapted from reference implementation26* (https://github.com/veorq/SipHash/blob/master/halfsiphash.c)27* which is distributed with the following copyright:28*/2930/*31SipHash reference C implementation3233Copyright (c) 2012-2021 Jean-Philippe Aumasson34<[email protected]>35Copyright (c) 2012-2014 Daniel J. Bernstein <[email protected]>3637To the extent possible under law, the author(s) have dedicated all copyright38and related and neighboring rights to this software to the public domain39worldwide. This software is distributed without any warranty.4041You should have received a copy of the CC0 Public Domain Dedication along42with43this software. If not, see44<http://creativecommons.org/publicdomain/zero/1.0/>.45*/4647#include "precompiled.hpp"48#include "classfile/altHashing.hpp"49#include "classfile/vmClasses.hpp"50#include "oops/klass.inline.hpp"51#include "oops/markWord.hpp"52#include "oops/oop.inline.hpp"53#include "runtime/os.hpp"5455// Get the hash code of the classes mirror if it exists, otherwise just56// return a random number, which is one of the possible hash code used for57// objects. We don't want to call the synchronizer hash code to install58// this value because it may safepoint.59static intptr_t object_hash(Klass* k) {60intptr_t hc = k->java_mirror()->mark().hash();61return hc != markWord::no_hash ? hc : os::random();62}6364// Seed value used for each alternative hash calculated.65uint64_t AltHashing::compute_seed() {66uint64_t nanos = os::javaTimeNanos();67uint64_t now = os::javaTimeMillis();68uint32_t SEED_MATERIAL[8] = {69(uint32_t) object_hash(vmClasses::String_klass()),70(uint32_t) object_hash(vmClasses::System_klass()),71(uint32_t) os::random(), // current thread isn't a java thread72(uint32_t) (((uint64_t)nanos) >> 32),73(uint32_t) nanos,74(uint32_t) (((uint64_t)now) >> 32),75(uint32_t) now,76(uint32_t) (os::javaTimeNanos() >> 2)77};7879return halfsiphash_64(SEED_MATERIAL, 8);80}8182// utility function copied from java/lang/Integer83static uint32_t Integer_rotateLeft(uint32_t i, int distance) {84return (i << distance) | (i >> (32 - distance));85}8687static void halfsiphash_rounds(uint32_t v[4], int rounds) {88while (rounds-- > 0) {89v[0] += v[1];90v[1] = Integer_rotateLeft(v[1], 5);91v[1] ^= v[0];92v[0] = Integer_rotateLeft(v[0], 16);93v[2] += v[3];94v[3] = Integer_rotateLeft(v[3], 8);95v[3] ^= v[2];96v[0] += v[3];97v[3] = Integer_rotateLeft(v[3], 7);98v[3] ^= v[0];99v[2] += v[1];100v[1] = Integer_rotateLeft(v[1], 13);101v[1] ^= v[2];102v[2] = Integer_rotateLeft(v[2], 16);103}104}105106static void halfsiphash_adddata(uint32_t v[4], uint32_t newdata, int rounds) {107v[3] ^= newdata;108halfsiphash_rounds(v, rounds);109v[0] ^= newdata;110}111112static void halfsiphash_init32(uint32_t v[4], uint64_t seed) {113v[0] = seed & 0xffffffff;114v[1] = seed >> 32;115v[2] = 0x6c796765 ^ v[0];116v[3] = 0x74656462 ^ v[1];117}118119static void halfsiphash_init64(uint32_t v[4], uint64_t seed) {120halfsiphash_init32(v, seed);121v[1] ^= 0xee;122}123124uint32_t halfsiphash_finish32(uint32_t v[4], int rounds) {125v[2] ^= 0xff;126halfsiphash_rounds(v, rounds);127return (v[1] ^ v[3]);128}129130static uint64_t halfsiphash_finish64(uint32_t v[4], int rounds) {131uint64_t rv;132v[2] ^= 0xee;133halfsiphash_rounds(v, rounds);134rv = v[1] ^ v[3];135v[1] ^= 0xdd;136halfsiphash_rounds(v, rounds);137rv |= (uint64_t)(v[1] ^ v[3]) << 32;138return rv;139}140141// HalfSipHash-2-4 (32-bit output) for Symbols142uint32_t AltHashing::halfsiphash_32(uint64_t seed, const void* in, int len) {143144const unsigned char* data = (const unsigned char*)in;145uint32_t v[4];146uint32_t newdata;147int off = 0;148int count = len;149150halfsiphash_init32(v, seed);151152// body153while (count >= 4) {154155// Avoid sign extension with 0x0ff156newdata = (data[off] & 0x0FF)157| (data[off + 1] & 0x0FF) << 8158| (data[off + 2] & 0x0FF) << 16159| data[off + 3] << 24;160161count -= 4;162off += 4;163164halfsiphash_adddata(v, newdata, 2);165}166167// tail168newdata = ((uint32_t)len) << 24; // (Byte.SIZE / Byte.SIZE);169170if (count > 0) {171switch (count) {172case 3:173newdata |= (data[off + 2] & 0x0ff) << 16;174// fall through175case 2:176newdata |= (data[off + 1] & 0x0ff) << 8;177// fall through178case 1:179newdata |= (data[off] & 0x0ff);180// fall through181}182}183184halfsiphash_adddata(v, newdata, 2);185186// finalization187return halfsiphash_finish32(v, 4);188}189190// HalfSipHash-2-4 (32-bit output) for Strings191uint32_t AltHashing::halfsiphash_32(uint64_t seed, const uint16_t* data, int len) {192uint32_t v[4];193uint32_t newdata;194int off = 0;195int count = len;196197halfsiphash_init32(v, seed);198199// body200while (count >= 2) {201uint16_t d1 = data[off++] & 0x0FFFF;202uint16_t d2 = data[off++];203newdata = (d1 | d2 << 16);204205count -= 2;206207halfsiphash_adddata(v, newdata, 2);208}209210// tail211newdata = ((uint32_t)len * 2) << 24; // (Character.SIZE / Byte.SIZE);212if (count > 0) {213newdata |= (uint32_t)data[off];214}215halfsiphash_adddata(v, newdata, 2);216217// finalization218return halfsiphash_finish32(v, 4);219}220221// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)222uint64_t AltHashing::halfsiphash_64(uint64_t seed, const uint32_t* data, int len) {223uint32_t v[4];224225int off = 0;226int end = len;227228halfsiphash_init64(v, seed);229230// body231while (off < end) {232halfsiphash_adddata(v, (uint32_t)data[off++], 2);233}234235// tail (always empty, as body is always 32-bit chunks)236237// finalization238halfsiphash_adddata(v, ((uint32_t)len * 4) << 24, 2); // (Integer.SIZE / Byte.SIZE);239return halfsiphash_finish64(v, 4);240}241242// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)243uint64_t AltHashing::halfsiphash_64(const uint32_t* data, int len) {244return halfsiphash_64((uint64_t)0, data, len);245}246247248