8244955: Additional Fix for JDK-8240124

Reviewed-by: rehn, pliden, smarks
This commit is contained in:
Coleen Phillimore 2020-05-18 11:16:19 -04:00 committed by Henry Jen
parent 798bfb3b9c
commit 5f1918db26
8 changed files with 106 additions and 120 deletions

View File

@ -116,6 +116,12 @@ static void halfsiphash_init64(uint32_t v[4], uint64_t seed) {
v[1] ^= 0xee;
}
uint32_t halfsiphash_finish32(uint32_t v[4], int rounds) {
v[2] ^= 0xff;
halfsiphash_rounds(v, rounds);
return (v[1] ^ v[3]);
}
static uint64_t halfsiphash_finish64(uint32_t v[4], int rounds) {
uint64_t rv;
v[2] ^= 0xee;
@ -127,14 +133,14 @@ static uint64_t halfsiphash_finish64(uint32_t v[4], int rounds) {
return rv;
}
// HalfSipHash-2-4 (64-bit output) for Symbols
uint64_t AltHashing::halfsiphash_64(uint64_t seed, const int8_t* data, int len) {
// HalfSipHash-2-4 (32-bit output) for Symbols
uint32_t AltHashing::halfsiphash_32(uint64_t seed, const uint8_t* data, int len) {
uint32_t v[4];
uint32_t newdata;
int off = 0;
int count = len;
halfsiphash_init64(v, seed);
halfsiphash_init32(v, seed);
// body
while (count >= 4) {
@ -171,17 +177,17 @@ uint64_t AltHashing::halfsiphash_64(uint64_t seed, const int8_t* data, int len)
halfsiphash_adddata(v, newdata, 2);
// finalization
return halfsiphash_finish64(v, 4);
return halfsiphash_finish32(v, 4);
}
// HalfSipHash-2-4 (64-bit output) for Strings
uint64_t AltHashing::halfsiphash_64(uint64_t seed, const uint16_t* data, int len) {
// HalfSipHash-2-4 (32-bit output) for Strings
uint32_t AltHashing::halfsiphash_32(uint64_t seed, const uint16_t* data, int len) {
uint32_t v[4];
uint32_t newdata;
int off = 0;
int count = len;
halfsiphash_init64(v, seed);
halfsiphash_init32(v, seed);
// body
while (count >= 2) {
@ -202,7 +208,7 @@ uint64_t AltHashing::halfsiphash_64(uint64_t seed, const uint16_t* data, int len
halfsiphash_adddata(v, newdata, 2);
// finalization
return halfsiphash_finish64(v, 4);
return halfsiphash_finish32(v, 4);
}
// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)

View File

@ -43,8 +43,8 @@ class AltHashing : AllStatic {
static uint64_t compute_seed();
// For Symbols
static uint64_t halfsiphash_64(uint64_t seed, const int8_t* data, int len);
static uint32_t halfsiphash_32(uint64_t seed, const uint8_t* data, int len);
// For Strings
static uint64_t halfsiphash_64(uint64_t seed, const uint16_t* data, int len);
static uint32_t halfsiphash_32(uint64_t seed, const uint16_t* data, int len);
};
#endif // SHARE_CLASSFILE_ALTHASHING_HPP

View File

@ -95,7 +95,7 @@ static uint64_t _alt_hash_seed = 0;
uintx hash_string(const jchar* s, int len, bool useAlt) {
return useAlt ?
AltHashing::halfsiphash_64(_alt_hash_seed, s, len) :
AltHashing::halfsiphash_32(_alt_hash_seed, s, len) :
java_lang_String::hash_code(s, len);
}

View File

@ -108,7 +108,7 @@ static inline void log_trace_symboltable_helper(Symbol* sym, const char* msg) {
// Pick hashing algorithm.
static uintx hash_symbol(const char* s, int len, bool useAlt) {
return useAlt ?
AltHashing::halfsiphash_64(_alt_hash_seed, (const int8_t*)s, len) :
AltHashing::halfsiphash_32(_alt_hash_seed, (const uint8_t*)s, len) :
java_lang_String::hash_code((const jbyte*)s, len);
}

View File

@ -156,7 +156,7 @@ void StringDedupEntryCache::free(StringDedupEntry* entry, uint worker_id) {
assert(worker_id < _nlists, "Invalid worker id");
entry->set_obj(NULL);
entry->set_java_hash(0, false /* latin1 */);
entry->set_hash(0);
if (_cached[worker_id].length() < _max_list_length) {
// Cache is not full
@ -246,14 +246,11 @@ void StringDedupTable::create() {
_table = new StringDedupTable(_min_size);
}
void StringDedupTable::add(typeArrayOop value, bool latin1, uint64_t hash, StringDedupEntry** list) {
void StringDedupTable::add(typeArrayOop value, bool latin1, unsigned int hash, StringDedupEntry** list) {
StringDedupEntry* entry = _entry_cache->alloc();
entry->set_obj(value);
if (use_java_hash()) {
entry->set_java_hash((unsigned int)hash, latin1);
} else {
entry->set_alt_hash(hash);
}
entry->set_hash(hash);
entry->set_latin1(latin1);
entry->set_next(*list);
*list = entry;
_entries++;
@ -268,18 +265,17 @@ void StringDedupTable::remove(StringDedupEntry** pentry, uint worker_id) {
void StringDedupTable::transfer(StringDedupEntry** pentry, StringDedupTable* dest) {
StringDedupEntry* entry = *pentry;
*pentry = entry->next();
uint64_t hash = use_java_hash() ? entry->java_hash() : entry->alt_hash();
unsigned int hash = entry->hash();
size_t index = dest->hash_to_index(hash);
StringDedupEntry** list = dest->bucket(index);
entry->set_next(*list);
*list = entry;
}
typeArrayOop StringDedupTable::lookup(typeArrayOop value, bool latin1, uint64_t hash,
typeArrayOop StringDedupTable::lookup(typeArrayOop value, bool latin1, unsigned int hash,
StringDedupEntry** list, uintx &count) {
for (StringDedupEntry* entry = *list; entry != NULL; entry = entry->next()) {
if ((use_java_hash() && entry->java_hash() == hash && entry->java_hash_latin1() == latin1) ||
(!use_java_hash() && entry->alt_hash() == hash)) {
if (entry->hash() == hash && entry->latin1() == latin1) {
oop* obj_addr = (oop*)entry->obj_addr();
oop obj = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(obj_addr);
if (obj != NULL && java_lang_String::value_equals(value, static_cast<typeArrayOop>(obj))) {
@ -294,7 +290,7 @@ typeArrayOop StringDedupTable::lookup(typeArrayOop value, bool latin1, uint64_t
return NULL;
}
typeArrayOop StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool latin1, uint64_t hash) {
typeArrayOop StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool latin1, unsigned int hash) {
size_t index = hash_to_index(hash);
StringDedupEntry** list = bucket(index);
uintx count = 0;
@ -318,31 +314,29 @@ typeArrayOop StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool lati
return existing_value;
}
unsigned int StringDedupTable::java_hash_code(typeArrayOop value, bool latin1) {
assert(use_java_hash(), "Should not use java hash code");
unsigned int StringDedupTable::hash_code(typeArrayOop value, bool latin1) {
unsigned int hash;
int length = value->length();
if (latin1) {
const jbyte* data = (jbyte*)value->base(T_BYTE);
hash = java_lang_String::hash_code(data, length);
if (use_java_hash()) {
hash = java_lang_String::hash_code(data, length);
} else {
hash = AltHashing::halfsiphash_32(_table->_hash_seed, (const uint8_t*)data, length);
}
} else {
length /= sizeof(jchar) / sizeof(jbyte); // Convert number of bytes to number of chars
const jchar* data = (jchar*)value->base(T_CHAR);
hash = java_lang_String::hash_code(data, length);
if (use_java_hash()) {
hash = java_lang_String::hash_code(data, length);
} else {
hash = AltHashing::halfsiphash_32(_table->_hash_seed, (const uint16_t*)data, length);
}
}
return hash;
}
uint64_t StringDedupTable::alt_hash_code(typeArrayOop value) {
assert(!use_java_hash(), "Should not use alt hash code");
int length = value->length();
const jbyte* data = (jbyte*)value->base(T_BYTE);
return AltHashing::halfsiphash_64(_table->_hash_seed, (const int8_t*)data, length);
}
void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) {
assert(java_lang_String::is_instance(java_string), "Must be a string");
NoSafepointVerifier nsv;
@ -357,7 +351,7 @@ void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) {
}
bool latin1 = java_lang_String::is_latin1(java_string);
uint64_t hash = 0;
unsigned int hash = 0;
if (use_java_hash()) {
if (!java_lang_String::hash_is_set(java_string)) {
@ -366,7 +360,7 @@ void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) {
hash = java_lang_String::hash_code(java_string);
} else {
// Compute hash
hash = alt_hash_code(value);
hash = hash_code(value, latin1);
stat->inc_hashed();
}
@ -516,9 +510,9 @@ uintx StringDedupTable::unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl,
// destination partitions. finish_rehash() will do a single
// threaded transfer of all entries.
typeArrayOop value = (typeArrayOop)*p;
assert(!use_java_hash(), "Should not be using Java hash");
uint64_t hash = alt_hash_code(value);
(*entry)->set_alt_hash(hash);
bool latin1 = (*entry)->latin1();
unsigned int hash = hash_code(value, latin1);
(*entry)->set_hash(hash);
}
// Move to next entry
@ -612,14 +606,9 @@ void StringDedupTable::verify() {
guarantee(Universe::heap()->is_in(value), "Object must be on the heap");
guarantee(!value->is_forwarded(), "Object must not be forwarded");
guarantee(value->is_typeArray(), "Object must be a typeArrayOop");
uint64_t hash;
if (use_java_hash()) {
hash = (*entry)->java_hash();
guarantee(java_hash_code(value, (*entry)->java_hash_latin1()) == hash, "Table entry has incorrect hash");
} else {
hash = (*entry)->alt_hash();
guarantee(alt_hash_code(value) == hash, "Table entry has incorrect hash");
}
bool latin1 = (*entry)->latin1();
unsigned int hash = hash_code(value, latin1);
guarantee((*entry)->hash() == hash, "Table entry has inorrect hash");
guarantee(_table->hash_to_index(hash) == bucket, "Table entry has incorrect index");
entry = (*entry)->next_addr();
}
@ -631,14 +620,12 @@ void StringDedupTable::verify() {
StringDedupEntry** entry1 = _table->bucket(bucket);
while (*entry1 != NULL) {
typeArrayOop value1 = (*entry1)->obj();
bool latin1_1 = (*entry1)->latin1();
StringDedupEntry** entry2 = (*entry1)->next_addr();
while (*entry2 != NULL) {
typeArrayOop value2 = (*entry2)->obj();
guarantee(value1 != value2, "Table entries must not have the same array");
if (use_java_hash()) {
guarantee((*entry1)->java_hash_latin1() != (*entry2)->java_hash_latin1() ||
!java_lang_String::value_equals(value1, value2), "Table entries must not have identical arrays");
}
bool latin1_2 = (*entry2)->latin1();
guarantee(latin1_1 != latin1_2 || !java_lang_String::value_equals(value1, value2), "Table entries must not have identical arrays");
entry2 = (*entry2)->next_addr();
}
entry1 = (*entry1)->next_addr();
@ -660,6 +647,6 @@ void StringDedupTable::print_statistics() {
_table->_entries, percent_of((size_t)_table->_entries, _table->_size), _entry_cache->size(), _entries_added, _entries_removed);
log.debug(" Resize Count: " UINTX_FORMAT ", Shrink Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS "), Grow Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS ")",
_resize_count, _table->_shrink_threshold, _shrink_load_factor * 100.0, _table->_grow_threshold, _grow_load_factor * 100.0);
log.debug(" Rehash Count: " UINTX_FORMAT ", Rehash Threshold: " UINTX_FORMAT ", Hash Seed: " UINT64_FORMAT_X, _rehash_count, _rehash_threshold, _table->_hash_seed);
log.debug(" Rehash Count: " UINTX_FORMAT ", Rehash Threshold: " UINTX_FORMAT ", Hash Seed: " UINT64_FORMAT, _rehash_count, _rehash_threshold, _table->_hash_seed);
log.debug(" Age Threshold: " UINTX_FORMAT, StringDeduplicationAgeThreshold);
}

View File

@ -39,16 +39,15 @@ class StringDedupUnlinkOrOopsDoClosure;
class StringDedupEntry : public CHeapObj<mtGC> {
private:
StringDedupEntry* _next;
uint64_t _hash;
unsigned int _hash;
bool _latin1;
typeArrayOop _obj;
static const uint64_t java_hash_mask = ((uint64_t)1 << 32) - 1;
static const uint64_t java_latin1_mask = (uint64_t)1 << 63;
public:
StringDedupEntry() :
_next(NULL),
_hash(0),
_latin1(false),
_obj(NULL) {
}
@ -64,29 +63,22 @@ public:
_next = next;
}
unsigned int java_hash() {
return (unsigned int)(_hash & java_hash_mask);
}
bool java_hash_latin1() {
return (_hash & java_latin1_mask) != 0;
}
void set_java_hash(unsigned int hash, bool latin1) {
_hash = hash;
if (latin1) {
_hash |= java_latin1_mask;
}
}
uint64_t alt_hash() {
unsigned int hash() {
return _hash;
}
void set_alt_hash(uint64_t hash) {
void set_hash(unsigned int hash) {
_hash = hash;
}
bool latin1() {
return _latin1;
}
void set_latin1(bool latin1) {
_latin1 = latin1;
}
typeArrayOop obj() {
return _obj;
}
@ -138,7 +130,7 @@ private:
// The hash seed also dictates which hash function to use. A
// zero hash seed means we will use the Java compatible hash
// function (which doesn't use a seed), and a non-zero hash
// seed means we use the alternate and better hash function.
// seed means we use the murmur3 hash function.
uint64_t _hash_seed;
// Constants governing table resize/rehash/cache.
@ -170,12 +162,12 @@ private:
}
// Returns the hash bucket index for the given hash code.
size_t hash_to_index(uint64_t hash) {
size_t hash_to_index(unsigned int hash) {
return (size_t)hash & (_size - 1);
}
// Adds a new table entry to the given hash bucket.
void add(typeArrayOop value, bool latin1, uint64_t hash, StringDedupEntry** list);
void add(typeArrayOop value, bool latin1, unsigned int hash, StringDedupEntry** list);
// Removes the given table entry from the table.
void remove(StringDedupEntry** pentry, uint worker_id);
@ -185,15 +177,15 @@ private:
// Returns an existing character array in the given hash bucket, or NULL
// if no matching character array exists.
typeArrayOop lookup(typeArrayOop value, bool latin1, uint64_t hash,
typeArrayOop lookup(typeArrayOop value, bool latin1, unsigned int hash,
StringDedupEntry** list, uintx &count);
// Returns an existing character array in the table, or inserts a new
// table entry if no matching character array exists.
typeArrayOop lookup_or_add_inner(typeArrayOop value, bool latin1, uint64_t hash);
typeArrayOop lookup_or_add_inner(typeArrayOop value, bool latin1, unsigned int hash);
// Thread safe lookup or add of table entry
static typeArrayOop lookup_or_add(typeArrayOop value, bool latin1, uint64_t hash) {
static typeArrayOop lookup_or_add(typeArrayOop value, bool latin1, unsigned int hash) {
// Protect the table from concurrent access. Also note that this lock
// acts as a fence for _table, which could have been replaced by a new
// instance if the table was resized or rehashed.
@ -209,8 +201,7 @@ private:
// Computes the hash code for the given character array, using the
// currently active hash function and hash seed.
static unsigned int java_hash_code(typeArrayOop value, bool latin1);
static uint64_t alt_hash_code(typeArrayOop value);
static unsigned int hash_code(typeArrayOop value, bool latin1);
static uintx unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl,
size_t partition_begin,

View File

@ -150,13 +150,13 @@ template <int N> static void get_header_version(char (&header_version) [N]) {
} else {
// Get the hash value. Use a static seed because the hash needs to return the same
// value over multiple jvm invocations.
uint64_t hash = AltHashing::halfsiphash_64(8191, (const int8_t*)vm_version, version_len);
uint32_t hash = AltHashing::halfsiphash_32(8191, (const uint8_t*)vm_version, version_len);
// Truncate the ident, saving room for the 16 hex character hash value.
strncpy(header_version, vm_version, JVM_IDENT_MAX-17);
// Truncate the ident, saving room for the 8 hex character hash value.
strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
// Append the hash code as 16 hex digits.
sprintf(&header_version[JVM_IDENT_MAX-17], "%016" PRIx64, hash);
// Append the hash code as eight hex digits.
sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
header_version[JVM_IDENT_MAX-1] = 0; // Null terminate.
}

View File

@ -30,44 +30,44 @@ class AltHashingTest : public ::testing::Test {
public:
static void testHalfsiphash_64_ByteArray() {
// printf("testHalfsiphash_64_CharArray\n");
static void testHalfsiphash_32_ByteArray() {
const int factor = 4;
int8_t vector[256];
int8_t hashes[factor * 256];
uint8_t vector[256];
uint8_t hashes[factor * 256];
for (int i = 0; i < 256; i++) {
vector[i] = (int8_t) i;
vector[i] = (uint8_t) i;
}
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
for (int i = 0; i < 256; i++) {
uint64_t hash = AltHashing::halfsiphash_64(256 - i, vector, i);
hashes[i * factor] = (int8_t) hash;
hashes[i * factor + 1] = (int8_t)(hash >> 8);
hashes[i * factor + 2] = (int8_t)(hash >> 16);
hashes[i * factor + 3] = (int8_t)(hash >> 24);
uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);
hashes[i * factor] = (uint8_t) hash;
hashes[i * factor + 1] = (uint8_t)(hash >> 8);
hashes[i * factor + 2] = (uint8_t)(hash >> 16);
hashes[i * factor + 3] = (uint8_t)(hash >> 24);
}
// hash to get const result.
uint64_t final_hash = AltHashing::halfsiphash_64(0, hashes, factor*256);
uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);
// Value found using reference implementation for the hashes array.
// halfsiphash((const uint8_t*)hashes, factor*256, (const uint8_t *)&k,
// (uint8_t*)&reference, 8);
//uint64_t k = 0; // seed
//uint32_t reference;
//halfsiphash((const uint8_t*)hashes, factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);
//printf("0x%x", reference);
static const uint64_t HALFSIPHASH_64_BYTE_CHECK_VALUE = 0x15a7911e30917ee8;
static const uint32_t HALFSIPHASH_32_BYTE_CHECK_VALUE = 0xd2be7fd8;
ASSERT_EQ(HALFSIPHASH_64_BYTE_CHECK_VALUE, final_hash) <<
ASSERT_EQ(HALFSIPHASH_32_BYTE_CHECK_VALUE, final_hash) <<
err_msg(
"Calculated hash result not as expected. Expected " UINT64_FORMAT " got " UINT64_FORMAT,
HALFSIPHASH_64_BYTE_CHECK_VALUE,
"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,
HALFSIPHASH_32_BYTE_CHECK_VALUE,
final_hash);
}
static void testHalfsiphash_64_CharArray() {
// printf("testHalfsiphash_64_CharArray\n");
static void testHalfsiphash_32_CharArray() {
const int factor = 2;
uint16_t vector[256];
@ -79,30 +79,32 @@ class AltHashingTest : public ::testing::Test {
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
for (int i = 0; i < 256; i++) {
uint64_t hash = AltHashing::halfsiphash_64(256 - i, vector, i);
uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);
hashes[i * factor] = (uint16_t) hash;
hashes[i * factor + 1] = (uint16_t)(hash >> 16);
}
// hash to get const result.
uint64_t final_hash = AltHashing::halfsiphash_64(0, hashes, factor*256);
uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);
// Value found using reference implementation for the hashes array.
// halfsiphash((const uint8_t*)hashes, 2*factor*256, (const uint8_t *)&k,
// (uint8_t*)&reference, 8);
static const uint64_t HALFSIPHASH_64_CHAR_CHECK_VALUE = 0xf392d8a6a9e24103;
//uint64_t k = 0; // seed
//uint32_t reference;
//halfsiphash((const uint8_t*)hashes, 2*factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);
//printf("0x%x", reference);
ASSERT_EQ(HALFSIPHASH_64_CHAR_CHECK_VALUE, final_hash) <<
static const uint32_t HALFSIPHASH_32_CHAR_CHECK_VALUE = 0x428bf8a5;
ASSERT_EQ(HALFSIPHASH_32_CHAR_CHECK_VALUE, final_hash) <<
err_msg(
"Calculated hash result not as expected. Expected " UINT64_FORMAT " got " UINT64_FORMAT,
HALFSIPHASH_64_CHAR_CHECK_VALUE,
"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,
HALFSIPHASH_32_CHAR_CHECK_VALUE,
final_hash);
}
// Test against sample hashes published with the reference implementation:
// https://github.com/veorq/SipHash
static void testHalfsiphash_64_FromReference() {
// printf("testHalfsiphash_64_FromReference\n");
const uint64_t seed = 0x0706050403020100;
const uint64_t results[16] = {
@ -134,10 +136,10 @@ class AltHashingTest : public ::testing::Test {
};
TEST_F(AltHashingTest, halfsiphash_test_ByteArray) {
AltHashingTest::testHalfsiphash_64_ByteArray();
AltHashingTest::testHalfsiphash_32_ByteArray();
}
TEST_F(AltHashingTest, halfsiphash_test_CharArray) {
AltHashingTest::testHalfsiphash_64_CharArray();
AltHashingTest::testHalfsiphash_32_CharArray();
}
TEST_F(AltHashingTest, halfsiphash_test_FromReference) {
AltHashingTest::testHalfsiphash_64_FromReference();