2
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "classfile/resolutionErrors.hpp"
27
#include "memory/allocation.hpp"
28
#include "oops/constantPool.hpp"
29
#include "oops/instanceKlass.hpp"
30
#include "oops/klass.inline.hpp"
31
#include "oops/symbol.hpp"
32
#include "runtime/handles.inline.hpp"
33
#include "runtime/mutexLocker.hpp"
34
#include "utilities/resourceHash.hpp"
36
class ResolutionErrorKey {
41
ResolutionErrorKey(ConstantPool* cpool, int index) : _cpool(cpool), _index(index) {
42
assert(_index > 0, "should be already encoded or otherwise greater than zero");
45
ConstantPool* cpool() const { return _cpool; }
47
static unsigned hash(const ResolutionErrorKey& key) {
48
Symbol* name = key._cpool->pool_holder()->name();
49
return (unsigned int)(name->identity_hash() ^ key._index);
52
static bool equals(const ResolutionErrorKey& l, const ResolutionErrorKey& r) {
53
return (l._cpool == r._cpool) && (l._index == r._index);
57
using InternalResolutionErrorTable = ResourceHashtable<ResolutionErrorKey, ResolutionErrorEntry*, 107, AnyObj::C_HEAP, mtClass,
58
ResolutionErrorKey::hash,
59
ResolutionErrorKey::equals>;
61
static InternalResolutionErrorTable* _resolution_error_table;
63
void ResolutionErrorTable::initialize() {
64
_resolution_error_table = new (mtClass) InternalResolutionErrorTable();
67
// create new error entry
68
void ResolutionErrorTable::add_entry(const constantPoolHandle& pool, int cp_index,
69
Symbol* error, const char* message,
70
Symbol* cause, const char* cause_msg)
72
assert_locked_or_safepoint(SystemDictionary_lock);
73
assert(!pool.is_null() && error != nullptr, "adding null obj");
75
ResolutionErrorKey key(pool(), cp_index);
76
ResolutionErrorEntry *entry = new ResolutionErrorEntry(error, message, cause, cause_msg);
77
_resolution_error_table->put(key, entry);
80
// create new nest host error entry
81
void ResolutionErrorTable::add_entry(const constantPoolHandle& pool, int cp_index,
84
assert_locked_or_safepoint(SystemDictionary_lock);
85
assert(!pool.is_null() && message != nullptr, "adding null obj");
87
ResolutionErrorKey key(pool(), cp_index);
88
ResolutionErrorEntry *entry = new ResolutionErrorEntry(message);
89
_resolution_error_table->put(key, entry);
92
// find entry in the table
93
ResolutionErrorEntry* ResolutionErrorTable::find_entry(const constantPoolHandle& pool, int cp_index) {
94
assert_locked_or_safepoint(SystemDictionary_lock);
95
ResolutionErrorKey key(pool(), cp_index);
96
ResolutionErrorEntry** entry = _resolution_error_table->get(key);
97
return entry == nullptr ? nullptr : *entry;
100
ResolutionErrorEntry::ResolutionErrorEntry(Symbol* error, const char* message,
101
Symbol* cause, const char* cause_msg):
103
_message(message != nullptr ? os::strdup(message) : nullptr),
105
_cause_msg(cause_msg != nullptr ? os::strdup(cause_msg) : nullptr),
106
_nest_host_error(nullptr) {
108
Symbol::maybe_increment_refcount(_error);
109
Symbol::maybe_increment_refcount(_cause);
112
ResolutionErrorEntry::~ResolutionErrorEntry() {
113
// decrement error refcount
114
Symbol::maybe_decrement_refcount(_error);
115
Symbol::maybe_decrement_refcount(_cause);
117
if (_message != nullptr) {
118
FREE_C_HEAP_ARRAY(char, _message);
121
if (_cause_msg != nullptr) {
122
FREE_C_HEAP_ARRAY(char, _cause_msg);
125
if (nest_host_error() != nullptr) {
126
FREE_C_HEAP_ARRAY(char, nest_host_error());
130
class ResolutionErrorDeleteIterate : StackObj {
134
ResolutionErrorDeleteIterate(ConstantPool* pool):
137
bool do_entry(const ResolutionErrorKey& key, ResolutionErrorEntry* value){
138
if (key.cpool() == p) {
147
// Delete entries in the table that match with ConstantPool c
148
void ResolutionErrorTable::delete_entry(ConstantPool* c) {
149
assert_locked_or_safepoint(SystemDictionary_lock);
151
ResolutionErrorDeleteIterate deleteIterator(c);
152
_resolution_error_table->unlink(&deleteIterator);
155
class ResolutionIteratePurgeErrors : StackObj {
157
bool do_entry(const ResolutionErrorKey& key, ResolutionErrorEntry* value){
158
ConstantPool* pool = key.cpool();
159
if (!(pool->pool_holder()->is_loader_alive())) {
168
// Remove unloaded entries from the table
169
void ResolutionErrorTable::purge_resolution_errors() {
170
assert_locked_or_safepoint(SystemDictionary_lock);
172
ResolutionIteratePurgeErrors purgeErrorsIterator;
173
_resolution_error_table->unlink(&purgeErrorsIterator);