8304069: ClassFileParser has ad-hoc hashtables

Reviewed-by: coleenp, dholmes
This commit is contained in:
Matias Saavedra Silva 2023-03-24 15:45:18 +00:00 committed by Coleen Phillimore
parent 9a8a60f7d6
commit d8ba227aa4

View File

@ -771,52 +771,27 @@ class NameSigHash: public ResourceObj {
public: public:
const Symbol* _name; // name const Symbol* _name; // name
const Symbol* _sig; // signature const Symbol* _sig; // signature
NameSigHash* _next; // Next entry in hash table
};
static const int HASH_ROW_SIZE = 256; static const int HASH_ROW_SIZE = 256;
static unsigned int hash(const Symbol* name, const Symbol* sig) { NameSigHash(Symbol* name, Symbol* sig) :
unsigned int raw_hash = 0; _name(name),
raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2); _sig(sig) {}
raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize;
return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE; static unsigned int hash(NameSigHash const& namesig) {
return namesig._name->identity_hash() ^ namesig._sig->identity_hash();
} }
static bool equals(NameSigHash const& e0, NameSigHash const& e1) {
static void initialize_hashtable(NameSigHash** table) { return (e0._name == e1._name) &&
memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE); (e0._sig == e1._sig);
} }
// Return false if the name/sig combination is found in table. };
// Return true if no duplicate is found. And name/sig is added as a new entry in table.
// The old format checker uses heap sort to find duplicates.
// NOTE: caller should guarantee that GC doesn't happen during the life cycle
// of table since we don't expect Symbol*'s to move.
static bool put_after_lookup(const Symbol* name, const Symbol* sig, NameSigHash** table) {
assert(name != nullptr, "name in constant pool is null");
// First lookup for duplicates using NameSigHashtable = ResourceHashtable<NameSigHash, int,
int index = hash(name, sig); NameSigHash::HASH_ROW_SIZE,
NameSigHash* entry = table[index]; AnyObj::RESOURCE_AREA, mtInternal,
while (entry != nullptr) { &NameSigHash::hash, &NameSigHash::equals>;
if (entry->_name == name && entry->_sig == sig) {
return false;
}
entry = entry->_next;
}
// No duplicate is found, allocate a new entry and fill it.
entry = new NameSigHash();
entry->_name = name;
entry->_sig = sig;
// Insert into hash table
entry->_next = table[index];
table[index] = entry;
return true;
}
// Side-effects: populates the _local_interfaces field // Side-effects: populates the _local_interfaces field
void ClassFileParser::parse_interfaces(const ClassFileStream* const stream, void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
@ -882,27 +857,16 @@ void ClassFileParser::parse_interfaces(const ClassFileStream* const stream,
// Check if there's any duplicates in interfaces // Check if there's any duplicates in interfaces
ResourceMark rm(THREAD); ResourceMark rm(THREAD);
NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, // Set containing interface names
NameSigHash*, ResourceHashtable<Symbol*, int>* interface_names = new ResourceHashtable<Symbol*, int>();
HASH_ROW_SIZE);
initialize_hashtable(interface_names);
bool dup = false;
const Symbol* name = nullptr;
{
debug_only(NoSafepointVerifier nsv;)
for (index = 0; index < itfs_len; index++) { for (index = 0; index < itfs_len; index++) {
const InstanceKlass* const k = _local_interfaces->at(index); const InstanceKlass* const k = _local_interfaces->at(index);
name = k->name(); Symbol* interface_name = k->name();
// If no duplicates, add (name, nullptr) in hashtable interface_names. // If no duplicates, add (name, nullptr) in hashtable interface_names.
if (!put_after_lookup(name, nullptr, interface_names)) { if (!interface_names->put(interface_name, 0)) {
dup = true;
break;
}
}
}
if (dup) {
classfile_parse_error("Duplicate interface name \"%s\" in class file %s", classfile_parse_error("Duplicate interface name \"%s\" in class file %s",
name->as_C_string(), THREAD); interface_name->as_C_string(), THREAD);
}
} }
} }
} }
@ -1621,27 +1585,16 @@ void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
if (_need_verify && length > 1) { if (_need_verify && length > 1) {
// Check duplicated fields // Check duplicated fields
ResourceMark rm(THREAD); ResourceMark rm(THREAD);
NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( // Set containing name-signature pairs
THREAD, NameSigHash*, HASH_ROW_SIZE); NameSigHashtable* names_and_sigs = new NameSigHashtable();
initialize_hashtable(names_and_sigs);
bool dup = false;
const Symbol* name = nullptr;
const Symbol* sig = nullptr;
{
debug_only(NoSafepointVerifier nsv;)
for (int i = 0; i < _temp_field_info->length(); i++) { for (int i = 0; i < _temp_field_info->length(); i++) {
name = _temp_field_info->adr_at(i)->name(_cp); NameSigHash name_and_sig(_temp_field_info->adr_at(i)->name(_cp),
sig = _temp_field_info->adr_at(i)->signature(_cp); _temp_field_info->adr_at(i)->signature(_cp));
// If no duplicates, add name/signature in hashtable names_and_sigs. // If no duplicates, add name/signature in hashtable names_and_sigs.
if (!put_after_lookup(name, sig, names_and_sigs)) { if(!names_and_sigs->put(name_and_sig, 0)) {
dup = true;
break;
}
}
}
if (dup) {
classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s", classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s",
name->as_C_string(), sig->as_klass_external_name(), THREAD); name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
}
} }
} }
} }
@ -2871,28 +2824,16 @@ void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
if (_need_verify && length > 1) { if (_need_verify && length > 1) {
// Check duplicated methods // Check duplicated methods
ResourceMark rm(THREAD); ResourceMark rm(THREAD);
NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( // Set containing name-signature pairs
THREAD, NameSigHash*, HASH_ROW_SIZE); NameSigHashtable* names_and_sigs = new NameSigHashtable();
initialize_hashtable(names_and_sigs);
bool dup = false;
const Symbol* name = nullptr;
const Symbol* sig = nullptr;
{
debug_only(NoSafepointVerifier nsv;)
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
const Method* const m = _methods->at(i); const Method* const m = _methods->at(i);
name = m->name(); NameSigHash name_and_sig(m->name(), m->signature());
sig = m->signature();
// If no duplicates, add name/signature in hashtable names_and_sigs. // If no duplicates, add name/signature in hashtable names_and_sigs.
if (!put_after_lookup(name, sig, names_and_sigs)) { if(!names_and_sigs->put(name_and_sig, 0)) {
dup = true;
break;
}
}
}
if (dup) {
classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s", classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s",
name->as_C_string(), sig->as_klass_external_name(), THREAD); name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
}
} }
} }
} }