Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/classfile/altHashing.cpp
40949 views
1
/*
2
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
/*
26
* halfsiphash code adapted from reference implementation
27
* (https://github.com/veorq/SipHash/blob/master/halfsiphash.c)
28
* which is distributed with the following copyright:
29
*
30
* SipHash reference C implementation
31
*
32
* Copyright (c) 2016 Jean-Philippe Aumasson <[email protected]>
33
*
34
* To the extent possible under law, the author(s) have dedicated all copyright
35
* and related and neighboring rights to this software to the public domain
36
* worldwide. This software is distributed without any warranty.
37
*
38
* You should have received a copy of the CC0 Public Domain Dedication along
39
* with this software. If not, see
40
* <http://creativecommons.org/publicdomain/zero/1.0/>.
41
*/
42
43
#include "precompiled.hpp"
44
#include "classfile/altHashing.hpp"
45
#include "classfile/vmClasses.hpp"
46
#include "oops/klass.inline.hpp"
47
#include "oops/markWord.hpp"
48
#include "oops/oop.inline.hpp"
49
#include "runtime/os.hpp"
50
51
// Get the hash code of the classes mirror if it exists, otherwise just
52
// return a random number, which is one of the possible hash code used for
53
// objects. We don't want to call the synchronizer hash code to install
54
// this value because it may safepoint.
55
static intptr_t object_hash(Klass* k) {
56
intptr_t hc = k->java_mirror()->mark().hash();
57
return hc != markWord::no_hash ? hc : os::random();
58
}
59
60
// Seed value used for each alternative hash calculated.
61
uint64_t AltHashing::compute_seed() {
62
uint64_t nanos = os::javaTimeNanos();
63
uint64_t now = os::javaTimeMillis();
64
uint32_t SEED_MATERIAL[8] = {
65
(uint32_t) object_hash(vmClasses::String_klass()),
66
(uint32_t) object_hash(vmClasses::System_klass()),
67
(uint32_t) os::random(), // current thread isn't a java thread
68
(uint32_t) (((uint64_t)nanos) >> 32),
69
(uint32_t) nanos,
70
(uint32_t) (((uint64_t)now) >> 32),
71
(uint32_t) now,
72
(uint32_t) (os::javaTimeNanos() >> 2)
73
};
74
75
return halfsiphash_64(SEED_MATERIAL, 8);
76
}
77
78
// utility function copied from java/lang/Integer
79
static uint32_t Integer_rotateLeft(uint32_t i, int distance) {
80
return (i << distance) | (i >> (32 - distance));
81
}
82
83
static void halfsiphash_rounds(uint32_t v[4], int rounds) {
84
while (rounds-- > 0) {
85
v[0] += v[1];
86
v[1] = Integer_rotateLeft(v[1], 5);
87
v[1] ^= v[0];
88
v[0] = Integer_rotateLeft(v[0], 16);
89
v[2] += v[3];
90
v[3] = Integer_rotateLeft(v[3], 8);
91
v[3] ^= v[2];
92
v[0] += v[3];
93
v[3] = Integer_rotateLeft(v[3], 7);
94
v[3] ^= v[0];
95
v[2] += v[1];
96
v[1] = Integer_rotateLeft(v[1], 13);
97
v[1] ^= v[2];
98
v[2] = Integer_rotateLeft(v[2], 16);
99
}
100
}
101
102
static void halfsiphash_adddata(uint32_t v[4], uint32_t newdata, int rounds) {
103
v[3] ^= newdata;
104
halfsiphash_rounds(v, rounds);
105
v[0] ^= newdata;
106
}
107
108
static void halfsiphash_init32(uint32_t v[4], uint64_t seed) {
109
v[0] = seed & 0xffffffff;
110
v[1] = seed >> 32;
111
v[2] = 0x6c796765 ^ v[0];
112
v[3] = 0x74656462 ^ v[1];
113
}
114
115
static void halfsiphash_init64(uint32_t v[4], uint64_t seed) {
116
halfsiphash_init32(v, seed);
117
v[1] ^= 0xee;
118
}
119
120
uint32_t halfsiphash_finish32(uint32_t v[4], int rounds) {
121
v[2] ^= 0xff;
122
halfsiphash_rounds(v, rounds);
123
return (v[1] ^ v[3]);
124
}
125
126
static uint64_t halfsiphash_finish64(uint32_t v[4], int rounds) {
127
uint64_t rv;
128
v[2] ^= 0xee;
129
halfsiphash_rounds(v, rounds);
130
rv = v[1] ^ v[3];
131
v[1] ^= 0xdd;
132
halfsiphash_rounds(v, rounds);
133
rv |= (uint64_t)(v[1] ^ v[3]) << 32;
134
return rv;
135
}
136
137
// HalfSipHash-2-4 (32-bit output) for Symbols
138
uint32_t AltHashing::halfsiphash_32(uint64_t seed, const uint8_t* data, int len) {
139
uint32_t v[4];
140
uint32_t newdata;
141
int off = 0;
142
int count = len;
143
144
halfsiphash_init32(v, seed);
145
146
// body
147
while (count >= 4) {
148
149
// Avoid sign extension with 0x0ff
150
newdata = (data[off] & 0x0FF)
151
| (data[off + 1] & 0x0FF) << 8
152
| (data[off + 2] & 0x0FF) << 16
153
| data[off + 3] << 24;
154
155
count -= 4;
156
off += 4;
157
158
halfsiphash_adddata(v, newdata, 2);
159
}
160
161
// tail
162
newdata = ((uint32_t)len) << 24; // (Byte.SIZE / Byte.SIZE);
163
164
if (count > 0) {
165
switch (count) {
166
case 3:
167
newdata |= (data[off + 2] & 0x0ff) << 16;
168
// fall through
169
case 2:
170
newdata |= (data[off + 1] & 0x0ff) << 8;
171
// fall through
172
case 1:
173
newdata |= (data[off] & 0x0ff);
174
// fall through
175
}
176
}
177
178
halfsiphash_adddata(v, newdata, 2);
179
180
// finalization
181
return halfsiphash_finish32(v, 4);
182
}
183
184
// HalfSipHash-2-4 (32-bit output) for Strings
185
uint32_t AltHashing::halfsiphash_32(uint64_t seed, const uint16_t* data, int len) {
186
uint32_t v[4];
187
uint32_t newdata;
188
int off = 0;
189
int count = len;
190
191
halfsiphash_init32(v, seed);
192
193
// body
194
while (count >= 2) {
195
uint16_t d1 = data[off++] & 0x0FFFF;
196
uint16_t d2 = data[off++];
197
newdata = (d1 | d2 << 16);
198
199
count -= 2;
200
201
halfsiphash_adddata(v, newdata, 2);
202
}
203
204
// tail
205
newdata = ((uint32_t)len * 2) << 24; // (Character.SIZE / Byte.SIZE);
206
if (count > 0) {
207
newdata |= (uint32_t)data[off];
208
}
209
halfsiphash_adddata(v, newdata, 2);
210
211
// finalization
212
return halfsiphash_finish32(v, 4);
213
}
214
215
// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)
216
uint64_t AltHashing::halfsiphash_64(uint64_t seed, const uint32_t* data, int len) {
217
uint32_t v[4];
218
219
int off = 0;
220
int end = len;
221
222
halfsiphash_init64(v, seed);
223
224
// body
225
while (off < end) {
226
halfsiphash_adddata(v, (uint32_t)data[off++], 2);
227
}
228
229
// tail (always empty, as body is always 32-bit chunks)
230
231
// finalization
232
halfsiphash_adddata(v, ((uint32_t)len * 4) << 24, 2); // (Integer.SIZE / Byte.SIZE);
233
return halfsiphash_finish64(v, 4);
234
}
235
236
// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)
237
uint64_t AltHashing::halfsiphash_64(const uint32_t* data, int len) {
238
return halfsiphash_64((uint64_t)0, data, len);
239
}
240
241