提交 601e30e0 编写于 作者: M minqi

8033792: AltHashing used jint for imprecise bit shifting

Summary: AltHashing used jint the way of juint in bit shifting which could lead loss of precision. Fix  by change _seed defined as juint.
Reviewed-by: coleenp, ccheung
Contributed-by: yumin.qi@oracle.com
上级 cf94d9df
/* /*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -39,18 +39,18 @@ intptr_t object_hash(Klass* k) { ...@@ -39,18 +39,18 @@ intptr_t object_hash(Klass* k) {
} }
// Seed value used for each alternative hash calculated. // Seed value used for each alternative hash calculated.
jint AltHashing::compute_seed() { juint AltHashing::compute_seed() {
jlong nanos = os::javaTimeNanos(); jlong nanos = os::javaTimeNanos();
jlong now = os::javaTimeMillis(); jlong now = os::javaTimeMillis();
jint SEED_MATERIAL[8] = { int SEED_MATERIAL[8] = {
(jint) object_hash(SystemDictionary::String_klass()), (int) object_hash(SystemDictionary::String_klass()),
(jint) object_hash(SystemDictionary::System_klass()), (int) object_hash(SystemDictionary::System_klass()),
(jint) os::random(), // current thread isn't a java thread (int) os::random(), // current thread isn't a java thread
(jint) (((julong)nanos) >> 32), (int) (((julong)nanos) >> 32),
(jint) nanos, (int) nanos,
(jint) (((julong)now) >> 32), (int) (((julong)now) >> 32),
(jint) now, (int) now,
(jint) (os::javaTimeNanos() >> 2) (int) (os::javaTimeNanos() >> 2)
}; };
return murmur3_32(SEED_MATERIAL, 8); return murmur3_32(SEED_MATERIAL, 8);
...@@ -58,14 +58,14 @@ jint AltHashing::compute_seed() { ...@@ -58,14 +58,14 @@ jint AltHashing::compute_seed() {
// Murmur3 hashing for Symbol // Murmur3 hashing for Symbol
jint AltHashing::murmur3_32(jint seed, const jbyte* data, int len) { juint AltHashing::murmur3_32(juint seed, const jbyte* data, int len) {
jint h1 = seed; juint h1 = seed;
int count = len; int count = len;
int offset = 0; int offset = 0;
// body // body
while (count >= 4) { while (count >= 4) {
jint k1 = (data[offset] & 0x0FF) juint k1 = (data[offset] & 0x0FF)
| (data[offset + 1] & 0x0FF) << 8 | (data[offset + 1] & 0x0FF) << 8
| (data[offset + 2] & 0x0FF) << 16 | (data[offset + 2] & 0x0FF) << 16
| data[offset + 3] << 24; | data[offset + 3] << 24;
...@@ -85,7 +85,7 @@ jint AltHashing::murmur3_32(jint seed, const jbyte* data, int len) { ...@@ -85,7 +85,7 @@ jint AltHashing::murmur3_32(jint seed, const jbyte* data, int len) {
// tail // tail
if (count > 0) { if (count > 0) {
jint k1 = 0; juint k1 = 0;
switch (count) { switch (count) {
case 3: case 3:
...@@ -109,18 +109,18 @@ jint AltHashing::murmur3_32(jint seed, const jbyte* data, int len) { ...@@ -109,18 +109,18 @@ jint AltHashing::murmur3_32(jint seed, const jbyte* data, int len) {
h1 ^= len; h1 ^= len;
// finalization mix force all bits of a hash block to avalanche // finalization mix force all bits of a hash block to avalanche
h1 ^= ((unsigned int)h1) >> 16; h1 ^= h1 >> 16;
h1 *= 0x85ebca6b; h1 *= 0x85ebca6b;
h1 ^= ((unsigned int)h1) >> 13; h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35; h1 *= 0xc2b2ae35;
h1 ^= ((unsigned int)h1) >> 16; h1 ^= h1 >> 16;
return h1; return h1;
} }
// Murmur3 hashing for Strings // Murmur3 hashing for Strings
jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) { juint AltHashing::murmur3_32(juint seed, const jchar* data, int len) {
jint h1 = seed; juint h1 = seed;
int off = 0; int off = 0;
int count = len; int count = len;
...@@ -129,7 +129,7 @@ jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) { ...@@ -129,7 +129,7 @@ jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) {
while (count >= 2) { while (count >= 2) {
jchar d1 = data[off++] & 0xFFFF; jchar d1 = data[off++] & 0xFFFF;
jchar d2 = data[off++]; jchar d2 = data[off++];
jint k1 = (d1 | d2 << 16); juint k1 = (d1 | d2 << 16);
count -= 2; count -= 2;
...@@ -145,7 +145,7 @@ jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) { ...@@ -145,7 +145,7 @@ jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) {
// tail // tail
if (count > 0) { if (count > 0) {
int k1 = data[off]; juint k1 = (juint)data[off];
k1 *= 0xcc9e2d51; k1 *= 0xcc9e2d51;
k1 = Integer_rotateLeft(k1, 15); k1 = Integer_rotateLeft(k1, 15);
...@@ -157,25 +157,25 @@ jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) { ...@@ -157,25 +157,25 @@ jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) {
h1 ^= len * 2; // (Character.SIZE / Byte.SIZE); h1 ^= len * 2; // (Character.SIZE / Byte.SIZE);
// finalization mix force all bits of a hash block to avalanche // finalization mix force all bits of a hash block to avalanche
h1 ^= ((unsigned int)h1) >> 16; h1 ^= h1 >> 16;
h1 *= 0x85ebca6b; h1 *= 0x85ebca6b;
h1 ^= ((unsigned int)h1) >> 13; h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35; h1 *= 0xc2b2ae35;
h1 ^= ((unsigned int)h1) >> 16; h1 ^= h1 >> 16;
return h1; return h1;
} }
// Hash used for the seed. // Hash used for the seed.
jint AltHashing::murmur3_32(jint seed, const int* data, int len) { juint AltHashing::murmur3_32(juint seed, const int* data, int len) {
jint h1 = seed; juint h1 = seed;
int off = 0; int off = 0;
int end = len; int end = len;
// body // body
while (off < end) { while (off < end) {
jint k1 = data[off++]; juint k1 = (juint)data[off++];
k1 *= 0xcc9e2d51; k1 *= 0xcc9e2d51;
k1 = Integer_rotateLeft(k1, 15); k1 = Integer_rotateLeft(k1, 15);
...@@ -193,26 +193,26 @@ jint AltHashing::murmur3_32(jint seed, const int* data, int len) { ...@@ -193,26 +193,26 @@ jint AltHashing::murmur3_32(jint seed, const int* data, int len) {
h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE); h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE);
// finalization mix force all bits of a hash block to avalanche // finalization mix force all bits of a hash block to avalanche
h1 ^= ((juint)h1) >> 16; h1 ^= h1 >> 16;
h1 *= 0x85ebca6b; h1 *= 0x85ebca6b;
h1 ^= ((juint)h1) >> 13; h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35; h1 *= 0xc2b2ae35;
h1 ^= ((juint)h1) >> 16; h1 ^= h1 >> 16;
return h1; return h1;
} }
jint AltHashing::murmur3_32(const int* data, int len) { juint AltHashing::murmur3_32(const int* data, int len) {
return murmur3_32(0, data, len); return murmur3_32(0, data, len);
} }
#ifndef PRODUCT #ifndef PRODUCT
// Overloaded versions for internal test. // Overloaded versions for internal test.
jint AltHashing::murmur3_32(const jbyte* data, int len) { juint AltHashing::murmur3_32(const jbyte* data, int len) {
return murmur3_32(0, data, len); return murmur3_32(0, data, len);
} }
jint AltHashing::murmur3_32(const jchar* data, int len) { juint AltHashing::murmur3_32(const jchar* data, int len) {
return murmur3_32(0, data, len); return murmur3_32(0, data, len);
} }
...@@ -251,11 +251,11 @@ void AltHashing::testMurmur3_32_ByteArray() { ...@@ -251,11 +251,11 @@ void AltHashing::testMurmur3_32_ByteArray() {
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255} // Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
jint hash = murmur3_32(256 - i, vector, i); juint hash = murmur3_32(256 - i, vector, i);
hashes[i * 4] = (jbyte) hash; hashes[i * 4] = (jbyte) hash;
hashes[i * 4 + 1] = (jbyte) (((juint)hash) >> 8); hashes[i * 4 + 1] = (jbyte)(hash >> 8);
hashes[i * 4 + 2] = (jbyte) (((juint)hash) >> 16); hashes[i * 4 + 2] = (jbyte)(hash >> 16);
hashes[i * 4 + 3] = (jbyte) (((juint)hash) >> 24); hashes[i * 4 + 3] = (jbyte)(hash >> 24);
} }
// hash to get const result. // hash to get const result.
...@@ -269,7 +269,7 @@ void AltHashing::testMurmur3_32_ByteArray() { ...@@ -269,7 +269,7 @@ void AltHashing::testMurmur3_32_ByteArray() {
} }
void AltHashing::testEquivalentHashes() { void AltHashing::testEquivalentHashes() {
jint jbytes, jchars, ints; juint jbytes, jchars, ints;
// printf("testEquivalentHashes\n"); // printf("testEquivalentHashes\n");
......
/* /*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -39,24 +39,24 @@ ...@@ -39,24 +39,24 @@
class AltHashing : AllStatic { class AltHashing : AllStatic {
// utility function copied from java/lang/Integer // utility function copied from java/lang/Integer
static jint Integer_rotateLeft(jint i, int distance) { static juint Integer_rotateLeft(juint i, int distance) {
return (i << distance) | (((juint)i) >> (32-distance)); return (i << distance) | (i >> (32-distance));
} }
static jint murmur3_32(const int* data, int len); static juint murmur3_32(const int* data, int len);
static jint murmur3_32(jint seed, const int* data, int len); static juint murmur3_32(juint seed, const int* data, int len);
#ifndef PRODUCT #ifndef PRODUCT
// Hashing functions used for internal testing // Hashing functions used for internal testing
static jint murmur3_32(const jbyte* data, int len); static juint murmur3_32(const jbyte* data, int len);
static jint murmur3_32(const jchar* data, int len); static juint murmur3_32(const jchar* data, int len);
static void testMurmur3_32_ByteArray(); static void testMurmur3_32_ByteArray();
static void testEquivalentHashes(); static void testEquivalentHashes();
#endif // PRODUCT #endif // PRODUCT
public: public:
static jint compute_seed(); static juint compute_seed();
static jint murmur3_32(jint seed, const jbyte* data, int len); static juint murmur3_32(juint seed, const jbyte* data, int len);
static jint murmur3_32(jint seed, const jchar* data, int len); static juint murmur3_32(juint seed, const jchar* data, int len);
NOT_PRODUCT(static void test_alt_hash();) NOT_PRODUCT(static void test_alt_hash();)
}; };
#endif // SHARE_VM_CLASSFILE_ALTHASHING_HPP #endif // SHARE_VM_CLASSFILE_ALTHASHING_HPP
/* /*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -554,6 +554,7 @@ class InstanceKlass: public Klass { ...@@ -554,6 +554,7 @@ class InstanceKlass: public Klass {
if (hk == NULL) { if (hk == NULL) {
return NULL; return NULL;
} else { } else {
assert(*hk != NULL, "host klass should always be set if the address is not null");
return *hk; return *hk;
} }
} }
......
/* /*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -40,7 +40,7 @@ class Metadata : public MetaspaceObj { ...@@ -40,7 +40,7 @@ class Metadata : public MetaspaceObj {
int identity_hash() { return (int)(uintptr_t)this; } int identity_hash() { return (int)(uintptr_t)this; }
// Rehashing support for tables containing pointers to this // Rehashing support for tables containing pointers to this
unsigned int new_hash(jint seed) { ShouldNotReachHere(); return 0; } unsigned int new_hash(juint seed) { ShouldNotReachHere(); return 0; }
virtual bool is_klass() const volatile { return false; } virtual bool is_klass() const volatile { return false; }
virtual bool is_method() const volatile { return false; } virtual bool is_method() const volatile { return false; }
......
/* /*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -102,7 +102,7 @@ intptr_t oopDesc::slow_identity_hash() { ...@@ -102,7 +102,7 @@ intptr_t oopDesc::slow_identity_hash() {
} }
// When String table needs to rehash // When String table needs to rehash
unsigned int oopDesc::new_hash(jint seed) { unsigned int oopDesc::new_hash(juint seed) {
EXCEPTION_MARK; EXCEPTION_MARK;
ResourceMark rm; ResourceMark rm;
int length; int length;
......
/* /*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -362,7 +362,7 @@ class oopDesc { ...@@ -362,7 +362,7 @@ class oopDesc {
intptr_t slow_identity_hash(); intptr_t slow_identity_hash();
// Alternate hashing code if string table is rehashed // Alternate hashing code if string table is rehashed
unsigned int new_hash(jint seed); unsigned int new_hash(juint seed);
// marks are forwarded to stack when object is locked // marks are forwarded to stack when object is locked
bool has_displaced_mark() const; bool has_displaced_mark() const;
......
/* /*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -207,7 +207,7 @@ const char* Symbol::as_klass_external_name() const { ...@@ -207,7 +207,7 @@ const char* Symbol::as_klass_external_name() const {
} }
// Alternate hashing for unbalanced symbol tables. // Alternate hashing for unbalanced symbol tables.
unsigned int Symbol::new_hash(jint seed) { unsigned int Symbol::new_hash(juint seed) {
ResourceMark rm; ResourceMark rm;
// Use alternate hashing algorithm on this symbol. // Use alternate hashing algorithm on this symbol.
return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length()); return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
......
/* /*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -154,7 +154,7 @@ class Symbol : private SymbolBase { ...@@ -154,7 +154,7 @@ class Symbol : private SymbolBase {
int identity_hash() { return _identity_hash; } int identity_hash() { return _identity_hash; }
// For symbol table alternate hashing // For symbol table alternate hashing
unsigned int new_hash(jint seed); unsigned int new_hash(juint seed);
// Reference counting. See comments above this class for when to use. // Reference counting. See comments above this class for when to use.
int refcount() const { return _refcount; } int refcount() const { return _refcount; }
......
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -93,7 +93,7 @@ template <MEMFLAGS F> bool BasicHashtable<F>::check_rehash_table(int count) { ...@@ -93,7 +93,7 @@ template <MEMFLAGS F> bool BasicHashtable<F>::check_rehash_table(int count) {
return false; return false;
} }
template <class T, MEMFLAGS F> jint Hashtable<T, F>::_seed = 0; template <class T, MEMFLAGS F> juint Hashtable<T, F>::_seed = 0;
// Create a new table and using alternate hash code, populate the new table // Create a new table and using alternate hash code, populate the new table
// with the existing elements. This can be used to change the hash code // with the existing elements. This can be used to change the hash code
......
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -280,7 +280,7 @@ protected: ...@@ -280,7 +280,7 @@ protected:
// Function to move these elements into the new table. // Function to move these elements into the new table.
void move_to(Hashtable<T, F>* new_table); void move_to(Hashtable<T, F>* new_table);
static bool use_alternate_hashcode() { return _seed != 0; } static bool use_alternate_hashcode() { return _seed != 0; }
static jint seed() { return _seed; } static juint seed() { return _seed; }
static int literal_size(Symbol *symbol); static int literal_size(Symbol *symbol);
static int literal_size(oop oop); static int literal_size(oop oop);
...@@ -296,7 +296,7 @@ public: ...@@ -296,7 +296,7 @@ public:
void dump_table(outputStream* st, const char *table_name); void dump_table(outputStream* st, const char *table_name);
private: private:
static jint _seed; static juint _seed;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册