annotate src/hotspot/share/oops/instanceKlass.cpp @ 53674:19b15ff2576b

8214208: Nestmate package validation logging/exception should include classloader information Reviewed-by: hseigel, coleenp, mchung
author dholmes
date Tue, 27 Nov 2018 21:20:16 -0500
parents 5b4242996d8c
children e99e3b3e2852 e70eb090a5f3 436097b038a1
rev   line source
duke@1 1 /*
coleenp@48939 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
coleenp@47946 26 #include "jvm.h"
kvn@42650 27 #include "aot/aotLoader.hpp"
mgronlun@34666 28 #include "classfile/classFileParser.hpp"
mockner@35917 29 #include "classfile/classFileStream.hpp"
kvn@42650 30 #include "classfile/classLoader.hpp"
coleenp@50319 31 #include "classfile/classLoaderData.inline.hpp"
stefank@7397 32 #include "classfile/javaClasses.hpp"
alanb@42307 33 #include "classfile/moduleEntry.hpp"
stefank@7397 34 #include "classfile/systemDictionary.hpp"
iklam@39713 35 #include "classfile/systemDictionaryShared.hpp"
stefank@7397 36 #include "classfile/verifier.hpp"
stefank@7397 37 #include "classfile/vmSymbols.hpp"
vlivanov@34195 38 #include "code/dependencyContext.hpp"
stefank@7397 39 #include "compiler/compileBroker.hpp"
pliden@30764 40 #include "gc/shared/collectedHeap.inline.hpp"
stefank@7397 41 #include "interpreter/oopMapCache.hpp"
stefank@7397 42 #include "interpreter/rewriter.hpp"
stefank@7397 43 #include "jvmtifiles/jvmti.h"
mockner@35917 44 #include "logging/log.hpp"
rprotacio@46444 45 #include "logging/logMessage.hpp"
stuefe@46701 46 #include "logging/logStream.hpp"
stefank@49825 47 #include "memory/allocation.inline.hpp"
acorn@15437 48 #include "memory/heapInspection.hpp"
stefank@25356 49 #include "memory/iterator.inline.hpp"
coleenp@13728 50 #include "memory/metadataFactory.hpp"
iklam@46746 51 #include "memory/metaspaceClosure.hpp"
jiangli@41182 52 #include "memory/metaspaceShared.hpp"
stefank@7397 53 #include "memory/oopFactory.hpp"
jprovino@37248 54 #include "memory/resourceArea.hpp"
never@10546 55 #include "oops/fieldStreams.hpp"
coleenp@13728 56 #include "oops/instanceClassLoaderKlass.hpp"
stefank@30150 57 #include "oops/instanceKlass.inline.hpp"
never@8725 58 #include "oops/instanceMirrorKlass.hpp"
stefank@7397 59 #include "oops/instanceOop.hpp"
coleenp@13728 60 #include "oops/klass.inline.hpp"
coleenp@13728 61 #include "oops/method.hpp"
stefank@7397 62 #include "oops/oop.inline.hpp"
coleenp@8076 63 #include "oops/symbol.hpp"
stefank@7397 64 #include "prims/jvmtiExport.hpp"
pliden@30764 65 #include "prims/jvmtiRedefineClasses.hpp"
jbachorik@22897 66 #include "prims/jvmtiThreadState.hpp"
coleenp@15099 67 #include "prims/methodComparator.hpp"
dholmes@40655 68 #include "runtime/atomic.hpp"
iklam@52193 69 #include "runtime/fieldDescriptor.inline.hpp"
stefank@7397 70 #include "runtime/handles.inline.hpp"
stefank@7397 71 #include "runtime/javaCalls.hpp"
stefank@7397 72 #include "runtime/mutexLocker.hpp"
coleenp@51033 73 #include "runtime/orderAccess.hpp"
stefank@14583 74 #include "runtime/thread.inline.hpp"
coleenp@17075 75 #include "services/classLoadingService.hpp"
stefank@7397 76 #include "services/threadService.hpp"
stefank@7397 77 #include "utilities/dtrace.hpp"
jprovino@15482 78 #include "utilities/macros.hpp"
acorn@40016 79 #include "utilities/stringUtils.hpp"
stefank@7397 80 #ifdef COMPILER1
stefank@7397 81 #include "c1/c1_Compiler.hpp"
stefank@7397 82 #endif
hseigel@52286 83 #if INCLUDE_JFR
hseigel@52286 84 #include "jfr/jfrEvents.hpp"
hseigel@52286 85 #endif
hseigel@52286 86
duke@1 87
fparain@5089 88 #ifdef DTRACE_ENABLED
fparain@5089 89
dcubed@10739 90
dcubed@10739 91 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
dcubed@10739 92 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
dcubed@10739 93 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
dcubed@10739 94 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
dcubed@10739 95 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
dcubed@10739 96 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
dcubed@10739 97 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
dcubed@10739 98 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
hseigel@46341 99 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \
dcubed@10739 100 { \
dcubed@10739 101 char* data = NULL; \
dcubed@10739 102 int len = 0; \
hseigel@46341 103 Symbol* clss_name = name(); \
hseigel@46341 104 if (clss_name != NULL) { \
hseigel@46341 105 data = (char*)clss_name->bytes(); \
hseigel@46341 106 len = clss_name->utf8_length(); \
dcubed@10739 107 } \
dcubed@10739 108 HOTSPOT_CLASS_INITIALIZATION_##type( \
sgehwolf@46766 109 data, len, (void*)class_loader(), thread_type); \
dcubed@10739 110 }
dcubed@10739 111
hseigel@46341 112 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait) \
dcubed@10739 113 { \
dcubed@10739 114 char* data = NULL; \
dcubed@10739 115 int len = 0; \
hseigel@46341 116 Symbol* clss_name = name(); \
hseigel@46341 117 if (clss_name != NULL) { \
hseigel@46341 118 data = (char*)clss_name->bytes(); \
hseigel@46341 119 len = clss_name->utf8_length(); \
dcubed@10739 120 } \
dcubed@10739 121 HOTSPOT_CLASS_INITIALIZATION_##type( \
sgehwolf@46766 122 data, len, (void*)class_loader(), thread_type, wait); \
dcubed@10739 123 }
fparain@5089 124
fparain@5089 125 #else // ndef DTRACE_ENABLED
fparain@5089 126
hseigel@46341 127 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
hseigel@46341 128 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
fparain@5089 129
fparain@5089 130 #endif // ndef DTRACE_ENABLED
fparain@5089 131
mgronlun@34666 132 static inline bool is_class_loader(const Symbol* class_name,
mgronlun@34666 133 const ClassFileParser& parser) {
mgronlun@34666 134 assert(class_name != NULL, "invariant");
mgronlun@34666 135
mgronlun@34666 136 if (class_name == vmSymbols::java_lang_ClassLoader()) {
mgronlun@34666 137 return true;
mgronlun@34666 138 }
mgronlun@34666 139
mgronlun@34666 140 if (SystemDictionary::ClassLoader_klass_loaded()) {
mgronlun@34666 141 const Klass* const super_klass = parser.super_klass();
mgronlun@34666 142 if (super_klass != NULL) {
mgronlun@34666 143 if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
mgronlun@34666 144 return true;
mgronlun@34666 145 }
mgronlun@34666 146 }
mgronlun@34666 147 }
mgronlun@34666 148 return false;
mgronlun@34666 149 }
mgronlun@34666 150
dholmes@51369 151 // called to verify that k is a member of this nest
dholmes@51369 152 bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
dholmes@51369 153 if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
dholmes@51369 154 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 155 ResourceMark rm(THREAD);
dholmes@51369 156 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
dholmes@51369 157 k->external_name(), this->external_name());
dholmes@51369 158 }
dholmes@51369 159 return false;
dholmes@51369 160 }
dholmes@51369 161
dholmes@51369 162 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 163 ResourceMark rm(THREAD);
dholmes@51369 164 log_trace(class, nestmates)("Checking nest membership of %s in %s",
dholmes@51369 165 k->external_name(), this->external_name());
dholmes@51369 166 }
dholmes@51369 167
dholmes@53161 168 // Check for a resolved cp entry , else fall back to a name check.
dholmes@53161 169 // We don't want to resolve any class other than the one being checked.
dholmes@51369 170 for (int i = 0; i < _nest_members->length(); i++) {
dholmes@51369 171 int cp_index = _nest_members->at(i);
dholmes@53161 172 if (_constants->tag_at(cp_index).is_klass()) {
dholmes@51369 173 Klass* k2 = _constants->klass_at(cp_index, CHECK_false);
dholmes@51369 174 if (k2 == k) {
dholmes@53161 175 log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
dholmes@51369 176 return true;
dholmes@53161 177 }
dholmes@53161 178 }
dholmes@53161 179 else {
dholmes@53161 180 Symbol* name = _constants->klass_name_at(cp_index);
dholmes@53161 181 if (name == k->name()) {
dholmes@53161 182 log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
dholmes@53161 183
dholmes@53161 184 // names match so check actual klass - this may trigger class loading if
dholmes@53161 185 // it doesn't match (but that should be impossible)
dholmes@53161 186 Klass* k2 = _constants->klass_at(cp_index, CHECK_false);
dholmes@53161 187 if (k2 == k) {
dholmes@53161 188 log_trace(class, nestmates)("- class is listed as a nest member");
dholmes@53161 189 return true;
dholmes@53161 190 }
dholmes@53161 191 else {
dholmes@53161 192 // same name but different klass!
dholmes@53161 193 log_trace(class, nestmates)(" - klass comparison failed!");
dholmes@53161 194 // can't have two names the same, so we're done
dholmes@53161 195 return false;
dholmes@53161 196 }
dholmes@51369 197 }
dholmes@51369 198 }
dholmes@51369 199 }
dholmes@51369 200 log_trace(class, nestmates)("- class is NOT a nest member!");
dholmes@51369 201 return false;
dholmes@51369 202 }
dholmes@51369 203
dholmes@51369 204 // Return nest-host class, resolving, validating and saving it if needed.
dholmes@51369 205 // In cases where this is called from a thread that can not do classloading
dholmes@51369 206 // (such as a native JIT thread) then we simply return NULL, which in turn
dholmes@51369 207 // causes the access check to return false. Such code will retry the access
dholmes@51369 208 // from a more suitable environment later.
dholmes@51369 209 InstanceKlass* InstanceKlass::nest_host(Symbol* validationException, TRAPS) {
dholmes@51369 210 InstanceKlass* nest_host_k = _nest_host;
dholmes@51369 211 if (nest_host_k == NULL) {
dholmes@51369 212 // need to resolve and save our nest-host class. This could be attempted
dholmes@51369 213 // concurrently but as the result is idempotent and we don't use the class
dholmes@51369 214 // then we do not need any synchronization beyond what is implicitly used
dholmes@51369 215 // during class loading.
dholmes@51369 216 if (_nest_host_index != 0) { // we have a real nest_host
dholmes@51369 217 // Before trying to resolve check if we're in a suitable context
dholmes@51369 218 if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
dholmes@51369 219 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 220 ResourceMark rm(THREAD);
dholmes@51369 221 log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
dholmes@51369 222 this->external_name());
dholmes@51369 223 }
dholmes@51369 224 return NULL;
dholmes@51369 225 }
dholmes@51369 226
dholmes@51369 227 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 228 ResourceMark rm(THREAD);
dholmes@51369 229 log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
dholmes@51369 230 this->external_name(),
dholmes@51369 231 _constants->klass_name_at(_nest_host_index)->as_C_string());
dholmes@51369 232 }
dholmes@51369 233
dholmes@51369 234 Klass* k = _constants->klass_at(_nest_host_index, THREAD);
dholmes@51369 235 if (HAS_PENDING_EXCEPTION) {
dholmes@51369 236 Handle exc_h = Handle(THREAD, PENDING_EXCEPTION);
dholmes@51369 237 if (exc_h->is_a(SystemDictionary::NoClassDefFoundError_klass())) {
dholmes@51369 238 // throw a new CDNFE with the original as its cause, and a clear msg
dholmes@51369 239 ResourceMark rm(THREAD);
dholmes@51369 240 char buf[200];
dholmes@51369 241 CLEAR_PENDING_EXCEPTION;
dholmes@51369 242 jio_snprintf(buf, sizeof(buf),
dholmes@51369 243 "Unable to load nest-host class (%s) of %s",
dholmes@51369 244 _constants->klass_name_at(_nest_host_index)->as_C_string(),
dholmes@51369 245 this->external_name());
dholmes@51369 246 log_trace(class, nestmates)("%s - NoClassDefFoundError", buf);
dholmes@51369 247 THROW_MSG_CAUSE_NULL(vmSymbols::java_lang_NoClassDefFoundError(), buf, exc_h);
dholmes@51369 248 }
dholmes@51369 249 // All other exceptions pass through (OOME, StackOverflowError, LinkageErrors etc).
dholmes@51369 250 return NULL;
dholmes@51369 251 }
dholmes@51369 252
dholmes@51369 253 // A valid nest-host is an instance class in the current package that lists this
dholmes@51369 254 // class as a nest member. If any of these conditions are not met we post the
dholmes@51369 255 // requested exception type (if any) and return NULL
dholmes@51369 256
dholmes@51369 257 const char* error = NULL;
dholmes@51369 258
dholmes@51369 259 // JVMS 5.4.4 indicates package check comes first
dholmes@51369 260 if (is_same_class_package(k)) {
dholmes@51369 261
dholmes@51369 262 // Now check actual membership. We can't be a member if our "host" is
dholmes@51369 263 // not an instance class.
dholmes@51369 264 if (k->is_instance_klass()) {
dholmes@51369 265 nest_host_k = InstanceKlass::cast(k);
dholmes@51369 266
dholmes@51369 267 bool is_member = nest_host_k->has_nest_member(this, CHECK_NULL);
dholmes@51369 268 if (is_member) {
dholmes@51369 269 // save resolved nest-host value
dholmes@51369 270 _nest_host = nest_host_k;
dholmes@51369 271
dholmes@51369 272 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 273 ResourceMark rm(THREAD);
dholmes@51369 274 log_trace(class, nestmates)("Resolved nest-host of %s to %s",
dholmes@51369 275 this->external_name(), k->external_name());
dholmes@51369 276 }
dholmes@51369 277 return nest_host_k;
dholmes@51369 278 }
dholmes@51369 279 }
dholmes@51369 280 error = "current type is not listed as a nest member";
dholmes@51369 281 } else {
dholmes@51369 282 error = "types are in different packages";
dholmes@51369 283 }
dholmes@51369 284
dholmes@51369 285 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 286 ResourceMark rm(THREAD);
dholmes@53674 287 log_trace(class, nestmates)
dholmes@53674 288 ("Type %s (loader: %s) is not a nest member of "
dholmes@53674 289 "resolved type %s (loader: %s): %s",
dholmes@53674 290 this->external_name(),
dholmes@53674 291 this->class_loader_data()->loader_name_and_id(),
dholmes@53674 292 k->external_name(),
dholmes@53674 293 k->class_loader_data()->loader_name_and_id(),
dholmes@53674 294 error);
dholmes@51369 295 }
dholmes@51369 296
dholmes@51369 297 if (validationException != NULL) {
dholmes@51369 298 ResourceMark rm(THREAD);
dholmes@51369 299 Exceptions::fthrow(THREAD_AND_LOCATION,
dholmes@51369 300 validationException,
dholmes@53674 301 "Type %s (loader: %s) is not a nest member of %s (loader: %s): %s",
dholmes@51369 302 this->external_name(),
dholmes@53674 303 this->class_loader_data()->loader_name_and_id(),
dholmes@51369 304 k->external_name(),
dholmes@53674 305 k->class_loader_data()->loader_name_and_id(),
dholmes@51369 306 error
dholmes@51369 307 );
dholmes@51369 308 }
dholmes@51369 309 return NULL;
dholmes@51369 310 } else {
dholmes@51369 311 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 312 ResourceMark rm(THREAD);
dholmes@51369 313 log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
dholmes@51369 314 this->external_name());
dholmes@51369 315 }
dholmes@51369 316 // save resolved nest-host value
dholmes@51369 317 return (_nest_host = this);
dholmes@51369 318 }
dholmes@51369 319 }
dholmes@51369 320 return nest_host_k;
dholmes@51369 321 }
dholmes@51369 322
dholmes@51369 323 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
dholmes@51369 324 // or we are k's nest_host - all of which is covered by comparing the two
dholmes@51369 325 // resolved_nest_hosts
dholmes@51369 326 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
dholmes@51369 327
dholmes@51369 328 assert(this != k, "this should be handled by higher-level code");
dholmes@51369 329
dholmes@51369 330 // Per JVMS 5.4.4 we first resolve and validate the current class, then
dholmes@51369 331 // the target class k. Resolution exceptions will be passed on by upper
dholmes@51369 332 // layers. IncompatibleClassChangeErrors from membership validation failures
dholmes@51369 333 // will also be passed through.
dholmes@51369 334
dholmes@51369 335 Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
dholmes@51369 336 InstanceKlass* cur_host = nest_host(icce, CHECK_false);
dholmes@51369 337 if (cur_host == NULL) {
dholmes@51369 338 return false;
dholmes@51369 339 }
dholmes@51369 340
dholmes@51369 341 Klass* k_nest_host = k->nest_host(icce, CHECK_false);
dholmes@51369 342 if (k_nest_host == NULL) {
dholmes@51369 343 return false;
dholmes@51369 344 }
dholmes@51369 345
dholmes@51369 346 bool access = (cur_host == k_nest_host);
dholmes@51369 347
dholmes@51369 348 if (log_is_enabled(Trace, class, nestmates)) {
dholmes@51369 349 ResourceMark rm(THREAD);
dholmes@51369 350 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
dholmes@51369 351 this->external_name(),
dholmes@51369 352 access ? "" : "NOT ",
dholmes@51369 353 k->external_name());
dholmes@51369 354 }
dholmes@51369 355
dholmes@51369 356 return access;
dholmes@51369 357 }
dholmes@51369 358
mgronlun@34666 359 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
mgronlun@34666 360 const int size = InstanceKlass::size(parser.vtable_size(),
mgronlun@34666 361 parser.itable_size(),
mgronlun@34666 362 nonstatic_oop_map_size(parser.total_oop_map_count()),
mgronlun@34666 363 parser.is_interface(),
lfoltan@52170 364 parser.is_unsafe_anonymous(),
lfoltan@52170 365 should_store_fingerprint(parser.is_unsafe_anonymous()));
mgronlun@34666 366
mgronlun@34666 367 const Symbol* const class_name = parser.class_name();
mgronlun@34666 368 assert(class_name != NULL, "invariant");
mgronlun@34666 369 ClassLoaderData* loader_data = parser.loader_data();
mgronlun@34666 370 assert(loader_data != NULL, "invariant");
mgronlun@34666 371
mgronlun@34666 372 InstanceKlass* ik;
coleenp@13728 373
coleenp@13728 374 // Allocation
mgronlun@34666 375 if (REF_NONE == parser.reference_type()) {
mgronlun@34666 376 if (class_name == vmSymbols::java_lang_Class()) {
mgronlun@34666 377 // mirror
mgronlun@34666 378 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
coleenp@13728 379 }
mgronlun@34666 380 else if (is_class_loader(class_name, parser)) {
mgronlun@34666 381 // class loader
mgronlun@34666 382 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
dholmes@51369 383 } else {
mgronlun@34666 384 // normal
mgronlun@34666 385 ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_misc_kind_other);
mgronlun@34666 386 }
dholmes@51369 387 } else {
mgronlun@34666 388 // reference
mgronlun@34666 389 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
coleenp@13728 390 }
coleenp@13728 391
coleenp@15935 392 // Check for pending exception before adding to the loader data and incrementing
coleenp@15935 393 // class count. Can get OOM here.
coleenp@15935 394 if (HAS_PENDING_EXCEPTION) {
coleenp@15935 395 return NULL;
coleenp@15935 396 }
coleenp@15935 397
coleenp@13728 398 return ik;
coleenp@13728 399 }
coleenp@13728 400
coleenp@15935 401
coleenp@15935 402 // copy method ordering from resource area to Metaspace
mgronlun@34666 403 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
coleenp@15935 404 if (m != NULL) {
coleenp@15935 405 // allocate a new array and copy contents (memcpy?)
coleenp@15935 406 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
coleenp@15935 407 for (int i = 0; i < m->length(); i++) {
coleenp@15935 408 _method_ordering->at_put(i, m->at(i));
coleenp@15935 409 }
coleenp@15935 410 } else {
coleenp@15935 411 _method_ordering = Universe::the_empty_int_array();
coleenp@15935 412 }
coleenp@15935 413 }
coleenp@15935 414
acorn@20391 415 // create a new array of vtable_indices for default methods
acorn@20391 416 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
acorn@20391 417 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
acorn@20391 418 assert(default_vtable_indices() == NULL, "only create once");
acorn@20391 419 set_default_vtable_indices(vtable_indices);
acorn@20391 420 return vtable_indices;
acorn@20391 421 }
coleenp@15935 422
stefank@51386 423 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
stefank@51386 424 Klass(id),
tschatzl@52035 425 _nest_members(NULL),
tschatzl@52035 426 _nest_host_index(0),
tschatzl@52035 427 _nest_host(NULL),
mgronlun@34666 428 _static_field_size(parser.static_field_size()),
mgronlun@34666 429 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
mgronlun@34666 430 _itable_len(parser.itable_size()),
iklam@53482 431 _reference_type(parser.reference_type())
iklam@53482 432 {
iklam@53482 433 set_vtable_length(parser.vtable_size());
iklam@53482 434 set_kind(kind);
iklam@53482 435 set_access_flags(parser.access_flags());
iklam@53482 436 set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
iklam@53482 437 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
mgronlun@34666 438 false));
mgronlun@34666 439
iklam@53482 440 assert(NULL == _methods, "underlying memory not zeroed?");
iklam@53482 441 assert(is_instance_klass(), "is layout incorrect?");
iklam@53482 442 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
iklam@53482 443
iklam@53482 444 if (DumpSharedSpaces) {
iklam@53482 445 SystemDictionaryShared::init_dumptime_info(this);
iklam@53482 446 }
coleenp@13728 447 }
coleenp@13728 448
coleenp@15935 449 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
coleenp@15935 450 Array<Method*>* methods) {
hseigel@20684 451 if (methods != NULL && methods != Universe::the_empty_method_array() &&
hseigel@20684 452 !methods->is_shared()) {
coleenp@15935 453 for (int i = 0; i < methods->length(); i++) {
coleenp@15935 454 Method* method = methods->at(i);
coleenp@15935 455 if (method == NULL) continue; // maybe null if error processing
coleenp@15935 456 // Only want to delete methods that are not executing for RedefineClasses.
coleenp@15935 457 // The previous version will point to them so they're not totally dangling
coleenp@15935 458 assert (!method->on_stack(), "shouldn't be called with methods on stack");
coleenp@15935 459 MetadataFactory::free_metadata(loader_data, method);
coleenp@15935 460 }
coleenp@15935 461 MetadataFactory::free_array<Method*>(loader_data, methods);
coleenp@15935 462 }
coleenp@15935 463 }
coleenp@15935 464
coleenp@15935 465 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
mgronlun@34666 466 const Klass* super_klass,
iklam@52030 467 Array<InstanceKlass*>* local_interfaces,
iklam@52030 468 Array<InstanceKlass*>* transitive_interfaces) {
coleenp@15935 469 // Only deallocate transitive interfaces if not empty, same as super class
coleenp@15935 470 // or same as local interfaces. See code in parseClassFile.
iklam@52030 471 Array<InstanceKlass*>* ti = transitive_interfaces;
iklam@52030 472 if (ti != Universe::the_empty_instance_klass_array() && ti != local_interfaces) {
coleenp@15935 473 // check that the interfaces don't come from super class
iklam@52030 474 Array<InstanceKlass*>* sti = (super_klass == NULL) ? NULL :
coleenp@15935 475 InstanceKlass::cast(super_klass)->transitive_interfaces();
hseigel@20684 476 if (ti != sti && ti != NULL && !ti->is_shared()) {
iklam@52030 477 MetadataFactory::free_array<InstanceKlass*>(loader_data, ti);
coleenp@15935 478 }
coleenp@15935 479 }
coleenp@15935 480
coleenp@15935 481 // local interfaces can be empty
iklam@52030 482 if (local_interfaces != Universe::the_empty_instance_klass_array() &&
hseigel@20684 483 local_interfaces != NULL && !local_interfaces->is_shared()) {
iklam@52030 484 MetadataFactory::free_array<InstanceKlass*>(loader_data, local_interfaces);
coleenp@15935 485 }
coleenp@15935 486 }
coleenp@15935 487
coleenp@13728 488 // This function deallocates the metadata and C heap pointers that the
coleenp@13728 489 // InstanceKlass points to.
coleenp@13728 490 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
coleenp@13728 491
coleenp@13728 492 // Orphan the mirror first, CMS thinks it's still live.
coleenp@15935 493 if (java_mirror() != NULL) {
coleenp@15935 494 java_lang_Class::set_klass(java_mirror(), NULL);
coleenp@15935 495 }
coleenp@13728 496
coleenp@47735 497 // Also remove mirror from handles
coleenp@47735 498 loader_data->remove_handle(_java_mirror);
coleenp@47735 499
coleenp@13728 500 // Need to take this class off the class loader data list.
coleenp@13728 501 loader_data->remove_class(this);
coleenp@13728 502
coleenp@13728 503 // The array_klass for this class is created later, after error handling.
coleenp@13728 504 // For class redefinition, we keep the original class so this scratch class
coleenp@13728 505 // doesn't have an array class. Either way, assert that there is nothing
coleenp@13728 506 // to deallocate.
coleenp@13728 507 assert(array_klasses() == NULL, "array classes shouldn't be created for this class yet");
coleenp@13728 508
coleenp@13728 509 // Release C heap allocated data that this might point to, which includes
coleenp@13728 510 // reference counting symbol names.
coleenp@13728 511 release_C_heap_structures();
coleenp@13728 512
coleenp@15935 513 deallocate_methods(loader_data, methods());
coleenp@13728 514 set_methods(NULL);
coleenp@13728 515
hseigel@20684 516 if (method_ordering() != NULL &&
hseigel@20684 517 method_ordering() != Universe::the_empty_int_array() &&
hseigel@20684 518 !method_ordering()->is_shared()) {
coleenp@13728 519 MetadataFactory::free_array<int>(loader_data, method_ordering());
coleenp@13728 520 }
coleenp@13728 521 set_method_ordering(NULL);
coleenp@13728 522
acorn@20391 523 // default methods can be empty
acorn@20391 524 if (default_methods() != NULL &&
hseigel@20684 525 default_methods() != Universe::the_empty_method_array() &&
hseigel@20684 526 !default_methods()->is_shared()) {
acorn@20391 527 MetadataFactory::free_array<Method*>(loader_data, default_methods());
acorn@20391 528 }
acorn@20391 529 // Do NOT deallocate the default methods, they are owned by superinterfaces.
acorn@20391 530 set_default_methods(NULL);
acorn@20391 531
acorn@20391 532 // default methods vtable indices can be empty
hseigel@20684 533 if (default_vtable_indices() != NULL &&
hseigel@20684 534 !default_vtable_indices()->is_shared()) {
acorn@20391 535 MetadataFactory::free_array<int>(loader_data, default_vtable_indices());
acorn@20391 536 }
acorn@20391 537 set_default_vtable_indices(NULL);
acorn@20391 538
acorn@20391 539
coleenp@13728 540 // This array is in Klass, but remove it with the InstanceKlass since
coleenp@13728 541 // this place would be the only caller and it can share memory with transitive
coleenp@13728 542 // interfaces.
hseigel@20684 543 if (secondary_supers() != NULL &&
hseigel@20684 544 secondary_supers() != Universe::the_empty_klass_array() &&
iklam@52030 545 // see comments in compute_secondary_supers about the following cast
iklam@52030 546 (address)(secondary_supers()) != (address)(transitive_interfaces()) &&
hseigel@20684 547 !secondary_supers()->is_shared()) {
coleenp@13728 548 MetadataFactory::free_array<Klass*>(loader_data, secondary_supers());
coleenp@13728 549 }
coleenp@13728 550 set_secondary_supers(NULL);
coleenp@13728 551
coleenp@15935 552 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
coleenp@13728 553 set_transitive_interfaces(NULL);
coleenp@13728 554 set_local_interfaces(NULL);
coleenp@13728 555
hseigel@20684 556 if (fields() != NULL && !fields()->is_shared()) {
hseigel@20684 557 MetadataFactory::free_array<jushort>(loader_data, fields());
hseigel@20684 558 }
coleenp@13728 559 set_fields(NULL, 0);
coleenp@13728 560
coleenp@13728 561 // If a method from a redefined class is using this constant pool, don't
coleenp@13728 562 // delete it, yet. The new class's previous version will point to this.
coleenp@15935 563 if (constants() != NULL) {
coleenp@15935 564 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
hseigel@20684 565 if (!constants()->is_shared()) {
hseigel@20684 566 MetadataFactory::free_metadata(loader_data, constants());
hseigel@20684 567 }
aeriksso@31353 568 // Delete any cached resolution errors for the constant pool
aeriksso@31353 569 SystemDictionary::delete_resolution_error(constants());
aeriksso@31353 570
coleenp@15935 571 set_constants(NULL);
coleenp@15935 572 }
coleenp@13728 573
hseigel@20684 574 if (inner_classes() != NULL &&
hseigel@20684 575 inner_classes() != Universe::the_empty_short_array() &&
hseigel@20684 576 !inner_classes()->is_shared()) {
coleenp@13728 577 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
coleenp@13728 578 }
coleenp@13728 579 set_inner_classes(NULL);
coleenp@13728 580
dholmes@51369 581 if (nest_members() != NULL &&
dholmes@51369 582 nest_members() != Universe::the_empty_short_array() &&
dholmes@51369 583 !nest_members()->is_shared()) {
dholmes@51369 584 MetadataFactory::free_array<jushort>(loader_data, nest_members());
dholmes@51369 585 }
dholmes@51369 586 set_nest_members(NULL);
dholmes@51369 587
hseigel@20684 588 // We should deallocate the Annotations instance if it's not in shared spaces.
hseigel@20684 589 if (annotations() != NULL && !annotations()->is_shared()) {
hseigel@20684 590 MetadataFactory::free_metadata(loader_data, annotations());
hseigel@20684 591 }
coleenp@13728 592 set_annotations(NULL);
iklam@53482 593
iklam@53482 594 if (DumpSharedSpaces) {
iklam@53482 595 SystemDictionaryShared::remove_dumptime_info(this);
iklam@53482 596 }
coleenp@13728 597 }
coleenp@13728 598
coleenp@13728 599 bool InstanceKlass::should_be_initialized() const {
duke@1 600 return !is_initialized();
duke@1 601 }
duke@1 602
iklam@46408 603 klassItable InstanceKlass::itable() const {
iklam@46408 604 return klassItable(const_cast<InstanceKlass*>(this));
duke@1 605 }
duke@1 606
coleenp@13728 607 void InstanceKlass::eager_initialize(Thread *thread) {
duke@1 608 if (!EagerInitialization) return;
duke@1 609
duke@1 610 if (this->is_not_initialized()) {
duke@1 611 // abort if the the class has a class initializer
duke@1 612 if (this->class_initializer() != NULL) return;
duke@1 613
duke@1 614 // abort if it is java.lang.Object (initialization is handled in genesis)
hseigel@46341 615 Klass* super_klass = super();
hseigel@46341 616 if (super_klass == NULL) return;
duke@1 617
duke@1 618 // abort if the super class should be initialized
hseigel@46341 619 if (!InstanceKlass::cast(super_klass)->is_initialized()) return;
duke@1 620
duke@1 621 // call body to expose the this pointer
hseigel@46341 622 eager_initialize_impl();
duke@1 623 }
duke@1 624 }
duke@1 625
coleenp@17826 626 // JVMTI spec thinks there are signers and protection domain in the
coleenp@17826 627 // instanceKlass. These accessors pretend these fields are there.
coleenp@17826 628 // The hprof specification also thinks these fields are in InstanceKlass.
coleenp@17826 629 oop InstanceKlass::protection_domain() const {
coleenp@17826 630 // return the protection_domain from the mirror
coleenp@17826 631 return java_lang_Class::protection_domain(java_mirror());
coleenp@17826 632 }
coleenp@17826 633
coleenp@17826 634 // To remove these from requires an incompatible change and CCC request.
coleenp@17826 635 objArrayOop InstanceKlass::signers() const {
coleenp@17826 636 // return the signers from the mirror
coleenp@17826 637 return java_lang_Class::signers(java_mirror());
coleenp@17826 638 }
coleenp@17826 639
coleenp@20379 640 oop InstanceKlass::init_lock() const {
coleenp@17826 641 // return the init lock from the mirror
iklam@21079 642 oop lock = java_lang_Class::init_lock(java_mirror());
dholmes@26556 643 // Prevent reordering with any access of initialization state
dholmes@26556 644 OrderAccess::loadload();
iklam@21079 645 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state
iklam@21079 646 "only fully initialized state can have a null lock");
iklam@21079 647 return lock;
iklam@21079 648 }
iklam@21079 649
iklam@21079 650 // Set the initialization lock to null so the object can be GC'ed. Any racing
iklam@21079 651 // threads to get this lock will see a null lock and will not lock.
iklam@21079 652 // That's okay because they all check for initialized state after getting
iklam@21079 653 // the lock and return.
iklam@21079 654 void InstanceKlass::fence_and_clear_init_lock() {
iklam@21079 655 // make sure previous stores are all done, notably the init_state.
iklam@21079 656 OrderAccess::storestore();
iklam@21079 657 java_lang_Class::set_init_lock(java_mirror(), NULL);
iklam@21079 658 assert(!is_not_initialized(), "class must be initialized now");
coleenp@17826 659 }
duke@1 660
hseigel@46341 661 void InstanceKlass::eager_initialize_impl() {
duke@1 662 EXCEPTION_MARK;
coleenp@46271 663 HandleMark hm(THREAD);
hseigel@46341 664 Handle h_init_lock(THREAD, init_lock());
hseigel@46562 665 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL);
duke@1 666
duke@1 667 // abort if someone beat us to the initialization
hseigel@46341 668 if (!is_not_initialized()) return; // note: not equivalent to is_initialized()
hseigel@46341 669
hseigel@46341 670 ClassState old_state = init_state();
hseigel@52465 671 link_class_impl(THREAD);
duke@1 672 if (HAS_PENDING_EXCEPTION) {
duke@1 673 CLEAR_PENDING_EXCEPTION;
duke@1 674 // Abort if linking the class throws an exception.
duke@1 675
duke@1 676 // Use a test to avoid redundantly resetting the state if there's
duke@1 677 // no change. Set_init_state() asserts that state changes make
duke@1 678 // progress, whereas here we might just be spinning in place.
hseigel@46341 679 if (old_state != _init_state)
hseigel@46341 680 set_init_state(old_state);
duke@1 681 } else {
duke@1 682 // linking successfull, mark class as initialized
hseigel@46341 683 set_init_state(fully_initialized);
hseigel@46341 684 fence_and_clear_init_lock();
duke@1 685 // trace
mockner@38151 686 if (log_is_enabled(Info, class, init)) {
duke@1 687 ResourceMark rm(THREAD);
hseigel@46341 688 log_info(class, init)("[Initialized %s without side effects]", external_name());
duke@1 689 }
duke@1 690 }
duke@1 691 }
duke@1 692
duke@1 693
duke@1 694 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
duke@1 695 // process. The step comments refers to the procedure described in that section.
duke@1 696 // Note: implementation moved to static method to expose the this pointer.
coleenp@13728 697 void InstanceKlass::initialize(TRAPS) {
duke@1 698 if (this->should_be_initialized()) {
hseigel@46341 699 initialize_impl(CHECK);
duke@1 700 // Note: at this point the class may be initialized
duke@1 701 // OR it may be in the state of being initialized
duke@1 702 // in case of recursive initialization!
duke@1 703 } else {
duke@1 704 assert(is_initialized(), "sanity check");
duke@1 705 }
duke@1 706 }
duke@1 707
duke@1 708
hseigel@52465 709 bool InstanceKlass::verify_code(TRAPS) {
duke@1 710 // 1) Verify the bytecodes
hseigel@52465 711 return Verifier::verify(this, should_verify_class(), THREAD);
duke@1 712 }
duke@1 713
coleenp@13728 714 void InstanceKlass::link_class(TRAPS) {
duke@1 715 assert(is_loaded(), "must be loaded");
duke@1 716 if (!is_linked()) {
hseigel@52465 717 link_class_impl(CHECK);
duke@1 718 }
duke@1 719 }
duke@1 720
duke@1 721 // Called to verify that a class can link during initialization, without
duke@1 722 // throwing a VerifyError.
coleenp@13728 723 bool InstanceKlass::link_class_or_fail(TRAPS) {
duke@1 724 assert(is_loaded(), "must be loaded");
duke@1 725 if (!is_linked()) {
hseigel@52465 726 link_class_impl(CHECK_false);
duke@1 727 }
duke@1 728 return is_linked();
duke@1 729 }
duke@1 730
hseigel@52465 731 bool InstanceKlass::link_class_impl(TRAPS) {
hseigel@46341 732 if (DumpSharedSpaces && is_in_error_state()) {
iklam@42031 733 // This is for CDS dumping phase only -- we use the in_error_state to indicate that
iklam@42031 734 // the class has failed verification. Throwing the NoClassDefFoundError here is just
iklam@42031 735 // a convenient way to stop repeat attempts to verify the same (bad) class.
iklam@42031 736 //
iklam@42031 737 // Note that the NoClassDefFoundError is not part of the JLS, and should not be thrown
iklam@42031 738 // if we are executing Java code. This is not a problem for CDS dumping phase since
iklam@42031 739 // it doesn't execute any Java code.
duke@1 740 ResourceMark rm(THREAD);
hseigel@46431 741 Exceptions::fthrow(THREAD_AND_LOCATION,
hseigel@46431 742 vmSymbols::java_lang_NoClassDefFoundError(),
hseigel@46431 743 "Class %s, or one of its supertypes, failed class initialization",
hseigel@46431 744 external_name());
hseigel@46431 745 return false;
duke@1 746 }
duke@1 747 // return if already verified
hseigel@46341 748 if (is_linked()) {
duke@1 749 return true;
duke@1 750 }
duke@1 751
duke@1 752 // Timing
duke@1 753 // timer handles recursion
duke@1 754 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl");
duke@1 755 JavaThread* jt = (JavaThread*)THREAD;
duke@1 756
duke@1 757 // link super class before linking this class
hseigel@46341 758 Klass* super_klass = super();
hseigel@46341 759 if (super_klass != NULL) {
hseigel@46341 760 if (super_klass->is_interface()) { // check if super class is an interface
duke@1 761 ResourceMark rm(THREAD);
duke@1 762 Exceptions::fthrow(
duke@1 763 THREAD_AND_LOCATION,
coleenp@8076 764 vmSymbols::java_lang_IncompatibleClassChangeError(),
duke@1 765 "class %s has interface %s as super class",
hseigel@46341 766 external_name(),
hseigel@46341 767 super_klass->external_name()
duke@1 768 );
duke@1 769 return false;
duke@1 770 }
duke@1 771
hseigel@46341 772 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
hseigel@52465 773 ik_super->link_class_impl(CHECK_false);
duke@1 774 }
duke@1 775
duke@1 776 // link all interfaces implemented by this class before linking this class
iklam@52030 777 Array<InstanceKlass*>* interfaces = local_interfaces();
duke@1 778 int num_interfaces = interfaces->length();
duke@1 779 for (int index = 0; index < num_interfaces; index++) {
iklam@52030 780 InstanceKlass* interk = interfaces->at(index);
hseigel@52465 781 interk->link_class_impl(CHECK_false);
duke@1 782 }
duke@1 783
duke@1 784 // in case the class is linked in the process of linking its superclasses
hseigel@46341 785 if (is_linked()) {
duke@1 786 return true;
duke@1 787 }
duke@1 788
mchung@3575 789 // trace only the link time for this klass that includes
mchung@3575 790 // the verification time
mchung@3575 791 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
mchung@3575 792 ClassLoader::perf_class_link_selftime(),
mchung@3575 793 ClassLoader::perf_classes_linked(),
mchung@3575 794 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@3575 795 jt->get_thread_stat()->perf_timers_addr(),
mchung@3575 796 PerfClassTraceTime::CLASS_LINK);
mchung@3575 797
duke@1 798 // verification & rewriting
duke@1 799 {
coleenp@46271 800 HandleMark hm(THREAD);
hseigel@46341 801 Handle h_init_lock(THREAD, init_lock());
hseigel@46562 802 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL);
duke@1 803 // rewritten will have been set if loader constraint error found
duke@1 804 // on an earlier link attempt
duke@1 805 // don't verify or rewrite if already rewritten
coleenp@46271 806 //
kvn@42650 807
hseigel@46341 808 if (!is_linked()) {
hseigel@46341 809 if (!is_rewritten()) {
duke@1 810 {
hseigel@52465 811 bool verify_ok = verify_code(THREAD);
duke@1 812 if (!verify_ok) {
duke@1 813 return false;
duke@1 814 }
duke@1 815 }
duke@1 816
duke@1 817 // Just in case a side-effect of verify linked this class already
duke@1 818 // (which can sometimes happen since the verifier loads classes
duke@1 819 // using custom class loaders, which are free to initialize things)
hseigel@46341 820 if (is_linked()) {
duke@1 821 return true;
duke@1 822 }
duke@1 823
duke@1 824 // also sets rewritten
hseigel@46341 825 rewrite_class(CHECK_false);
hseigel@46341 826 } else if (is_shared()) {
hseigel@46341 827 SystemDictionaryShared::check_verification_constraints(this, CHECK_false);
duke@1 828 }
duke@1 829
coleenp@9971 830 // relocate jsrs and link methods after they are all rewritten
hseigel@46341 831 link_methods(CHECK_false);
coleenp@9971 832
duke@1 833 // Initialize the vtable and interface table after
duke@1 834 // methods have been rewritten since rewrite may
coleenp@13728 835 // fabricate new Method*s.
duke@1 836 // also does loader constraint checking
jiangli@39714 837 //
jiangli@39714 838 // initialize_vtable and initialize_itable need to be rerun for
jiangli@39714 839 // a shared class if the class is not loaded by the NULL classloader.
hseigel@46341 840 ClassLoaderData * loader_data = class_loader_data();
hseigel@46341 841 if (!(is_shared() &&
jiangli@39714 842 loader_data->is_the_null_class_loader_data())) {
iklam@46408 843 vtable().initialize_vtable(true, CHECK_false);
iklam@46408 844 itable().initialize_itable(true, CHECK_false);
duke@1 845 }
duke@1 846 #ifdef ASSERT
duke@1 847 else {
iklam@46408 848 vtable().verify(tty, true);
duke@1 849 // In case itable verification is ever added.
iklam@46408 850 // itable().verify(tty, true);
duke@1 851 }
duke@1 852 #endif
hseigel@46341 853 set_init_state(linked);
duke@1 854 if (JvmtiExport::should_post_class_prepare()) {
duke@1 855 Thread *thread = THREAD;
duke@1 856 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
hseigel@46341 857 JvmtiExport::post_class_prepare((JavaThread *) thread, this);
duke@1 858 }
duke@1 859 }
duke@1 860 }
duke@1 861 return true;
duke@1 862 }
duke@1 863
duke@1 864 // Rewrite the byte codes of all of the methods of a class.
duke@1 865 // The rewriter must be called exactly once. Rewriting must happen after
duke@1 866 // verification but before the first method of the class is executed.
coleenp@13728 867 void InstanceKlass::rewrite_class(TRAPS) {
duke@1 868 assert(is_loaded(), "must be loaded");
coleenp@46329 869 if (is_rewritten()) {
coleenp@46329 870 assert(is_shared(), "rewriting an unshared class?");
duke@1 871 return;
duke@1 872 }
coleenp@46329 873 Rewriter::rewrite(this, CHECK);
coleenp@46329 874 set_rewritten();
duke@1 875 }
duke@1 876
coleenp@9971 877 // Now relocate and link method entry points after class is rewritten.
coleenp@9971 878 // This is outside is_rewritten flag. In case of an exception, it can be
coleenp@9971 879 // executed more than once.
coleenp@15099 880 void InstanceKlass::link_methods(TRAPS) {
coleenp@15099 881 int len = methods()->length();
coleenp@15099 882 for (int i = len-1; i >= 0; i--) {
coleenp@15099 883 methodHandle m(THREAD, methods()->at(i));
coleenp@15099 884
coleenp@15099 885 // Set up method entry points for compiler and interpreter .
coleenp@15099 886 m->link_method(m, CHECK);
coleenp@15099 887 }
coleenp@9971 888 }
coleenp@9971 889
acorn@27402 890 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
hseigel@46341 891 void InstanceKlass::initialize_super_interfaces(TRAPS) {
hseigel@46341 892 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
hseigel@46341 893 for (int i = 0; i < local_interfaces()->length(); ++i) {
iklam@52030 894 InstanceKlass* ik = local_interfaces()->at(i);
coleenp@41293 895
coleenp@41293 896 // Initialization is depth first search ie. we start with top of the inheritance tree
dholmes@41669 897 // has_nonstatic_concrete_methods drives searching superinterfaces since it
dholmes@41669 898 // means has_nonstatic_concrete_methods in its superinterface hierarchy
dholmes@41669 899 if (ik->has_nonstatic_concrete_methods()) {
hseigel@46341 900 ik->initialize_super_interfaces(CHECK);
coleenp@41293 901 }
coleenp@41293 902
coleenp@41293 903 // Only initialize() interfaces that "declare" concrete methods.
dholmes@41669 904 if (ik->should_be_initialized() && ik->declares_nonstatic_concrete_methods()) {
coleenp@41293 905 ik->initialize(CHECK);
acorn@27402 906 }
acorn@27402 907 }
acorn@27402 908 }
duke@1 909
hseigel@46341 910 void InstanceKlass::initialize_impl(TRAPS) {
coleenp@46271 911 HandleMark hm(THREAD);
coleenp@46271 912
duke@1 913 // Make sure klass is linked (verified) before initialization
duke@1 914 // A class could already be verified, since it has been reflected upon.
hseigel@46341 915 link_class(CHECK);
hseigel@46341 916
hseigel@46341 917 DTRACE_CLASSINIT_PROBE(required, -1);
fparain@5089 918
fparain@5089 919 bool wait = false;
fparain@5089 920
duke@1 921 // refer to the JVM book page 47 for description of steps
duke@1 922 // Step 1
coleenp@13728 923 {
hseigel@46341 924 Handle h_init_lock(THREAD, init_lock());
hseigel@46562 925 ObjectLocker ol(h_init_lock, THREAD, h_init_lock() != NULL);
duke@1 926
duke@1 927 Thread *self = THREAD; // it's passed the current thread
duke@1 928
duke@1 929 // Step 2
duke@1 930 // If we were to use wait() instead of waitInterruptibly() then
duke@1 931 // we might end up throwing IE from link/symbol resolution sites
duke@1 932 // that aren't expected to throw. This would wreak havoc. See 6320309.
hseigel@46341 933 while(is_being_initialized() && !is_reentrant_initialization(self)) {
fparain@5089 934 wait = true;
duke@1 935 ol.waitUninterruptibly(CHECK);
duke@1 936 }
duke@1 937
duke@1 938 // Step 3
hseigel@46341 939 if (is_being_initialized() && is_reentrant_initialization(self)) {
hseigel@46341 940 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
duke@1 941 return;
fparain@5089 942 }
duke@1 943
duke@1 944 // Step 4
hseigel@46341 945 if (is_initialized()) {
hseigel@46341 946 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, -1, wait);
duke@1 947 return;
fparain@5089 948 }
duke@1 949
duke@1 950 // Step 5
hseigel@46341 951 if (is_in_error_state()) {
hseigel@46341 952 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
duke@1 953 ResourceMark rm(THREAD);
duke@1 954 const char* desc = "Could not initialize class ";
hseigel@46341 955 const char* className = external_name();
duke@1 956 size_t msglen = strlen(desc) + strlen(className) + 1;
kamg@6421 957 char* message = NEW_RESOURCE_ARRAY(char, msglen);
duke@1 958 if (NULL == message) {
duke@1 959 // Out of memory: can't create detailed error message
ccheung@47103 960 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
duke@1 961 } else {
duke@1 962 jio_snprintf(message, msglen, "%s%s", desc, className);
ccheung@47103 963 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
duke@1 964 }
duke@1 965 }
duke@1 966
duke@1 967 // Step 6
hseigel@46341 968 set_init_state(being_initialized);
hseigel@46341 969 set_init_thread(self);
duke@1 970 }
duke@1 971
duke@1 972 // Step 7
coleenp@41293 973 // Next, if C is a class rather than an interface, initialize it's super class and super
coleenp@41293 974 // interfaces.
hseigel@46341 975 if (!is_interface()) {
hseigel@46341 976 Klass* super_klass = super();
coleenp@41293 977 if (super_klass != NULL && super_klass->should_be_initialized()) {
coleenp@41293 978 super_klass->initialize(THREAD);
coleenp@41293 979 }
dholmes@41669 980 // If C implements any interface that declares a non-static, concrete method,
coleenp@41293 981 // the initialization of C triggers initialization of its super interfaces.
dholmes@41669 982 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
dholmes@41669 983 // having a superinterface that declares, non-static, concrete methods
hseigel@46341 984 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
hseigel@46341 985 initialize_super_interfaces(THREAD);
coleenp@41293 986 }
coleenp@41293 987
coleenp@41293 988 // If any exceptions, complete abruptly, throwing the same exception as above.
duke@1 989 if (HAS_PENDING_EXCEPTION) {
duke@1 990 Handle e(THREAD, PENDING_EXCEPTION);
duke@1 991 CLEAR_PENDING_EXCEPTION;
duke@1 992 {
duke@1 993 EXCEPTION_MARK;
coleenp@41293 994 // Locks object, set state, and notify all waiting threads
hseigel@46341 995 set_initialization_state_and_notify(initialization_error, THREAD);
coleenp@41293 996 CLEAR_PENDING_EXCEPTION;
duke@1 997 }
hseigel@46341 998 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
duke@1 999 THROW_OOP(e());
duke@1 1000 }
duke@1 1001 }
duke@1 1002
kamg@14385 1003
kvn@42650 1004 // Look for aot compiled methods for this klass, including class initializer.
hseigel@46341 1005 AOTLoader::load_for_klass(this, THREAD);
kvn@42650 1006
duke@1 1007 // Step 8
duke@1 1008 {
duke@1 1009 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
duke@1 1010 JavaThread* jt = (JavaThread*)THREAD;
hseigel@46341 1011 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
duke@1 1012 // Timer includes any side effects of class initialization (resolution,
duke@1 1013 // etc), but not recursive entry into call_class_initializer().
mchung@3575 1014 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
mchung@3575 1015 ClassLoader::perf_class_init_selftime(),
mchung@3575 1016 ClassLoader::perf_classes_inited(),
mchung@3575 1017 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@3575 1018 jt->get_thread_stat()->perf_timers_addr(),
mchung@3575 1019 PerfClassTraceTime::CLASS_CLINIT);
hseigel@46341 1020 call_class_initializer(THREAD);
duke@1 1021 }
duke@1 1022
duke@1 1023 // Step 9
duke@1 1024 if (!HAS_PENDING_EXCEPTION) {
hseigel@46341 1025 set_initialization_state_and_notify(fully_initialized, CHECK);
iklam@46408 1026 {
iklam@46408 1027 debug_only(vtable().verify(tty, true);)
duke@1 1028 }
duke@1 1029 }
duke@1 1030 else {
duke@1 1031 // Step 10 and 11
duke@1 1032 Handle e(THREAD, PENDING_EXCEPTION);
duke@1 1033 CLEAR_PENDING_EXCEPTION;
jbachorik@22897 1034 // JVMTI has already reported the pending exception
jbachorik@22897 1035 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@22897 1036 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
duke@1 1037 {
duke@1 1038 EXCEPTION_MARK;
hseigel@46341 1039 set_initialization_state_and_notify(initialization_error, THREAD);
duke@1 1040 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
jbachorik@22897 1041 // JVMTI has already reported the pending exception
jbachorik@22897 1042 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@22897 1043 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
duke@1 1044 }
hseigel@46341 1045 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
never@4571 1046 if (e->is_a(SystemDictionary::Error_klass())) {
duke@1 1047 THROW_OOP(e());
duke@1 1048 } else {
duke@1 1049 JavaCallArguments args(e);
coleenp@8076 1050 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
coleenp@8076 1051 vmSymbols::throwable_void_signature(),
duke@1 1052 &args);
duke@1 1053 }
duke@1 1054 }
hseigel@46341 1055 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
duke@1 1056 }
duke@1 1057
duke@1 1058
coleenp@13728 1059 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
hseigel@46341 1060 Handle h_init_lock(THREAD, init_lock());
hseigel@46562 1061 if (h_init_lock() != NULL) {
hseigel@46341 1062 ObjectLocker ol(h_init_lock, THREAD);
hseigel@46341 1063 set_init_state(state);
hseigel@46341 1064 fence_and_clear_init_lock();
coleenp@41293 1065 ol.notify_all(CHECK);
coleenp@41293 1066 } else {
hseigel@46562 1067 assert(h_init_lock() != NULL, "The initialization state should never be set twice");
hseigel@46341 1068 set_init_state(state);
coleenp@41293 1069 }
duke@1 1070 }
duke@1 1071
coleenp@53285 1072 Klass* InstanceKlass::implementor() const {
coleenp@52091 1073 assert_locked_or_safepoint(Compile_lock);
coleenp@52091 1074 Klass** k = adr_implementor();
coleenp@52091 1075 if (k == NULL) {
coleenp@52091 1076 return NULL;
coleenp@52091 1077 } else {
coleenp@53285 1078 return *k;
coleenp@52091 1079 }
coleenp@52091 1080 }
coleenp@52091 1081
coleenp@52091 1082 void InstanceKlass::set_implementor(Klass* k) {
coleenp@52091 1083 assert_lock_strong(Compile_lock);
coleenp@52091 1084 assert(is_interface(), "not interface");
coleenp@52091 1085 Klass** addr = adr_implementor();
coleenp@52091 1086 assert(addr != NULL, "null addr");
coleenp@52091 1087 if (addr != NULL) {
coleenp@52091 1088 *addr = k;
coleenp@52091 1089 }
coleenp@52091 1090 }
coleenp@52091 1091
coleenp@52091 1092 int InstanceKlass::nof_implementors() const {
coleenp@52091 1093 assert_lock_strong(Compile_lock);
coleenp@52091 1094 Klass* k = implementor();
coleenp@52091 1095 if (k == NULL) {
coleenp@52091 1096 return 0;
coleenp@52091 1097 } else if (k != this) {
coleenp@52091 1098 return 1;
coleenp@52091 1099 } else {
coleenp@52091 1100 return 2;
coleenp@52091 1101 }
coleenp@52091 1102 }
coleenp@52091 1103
jiangli@12369 1104 // The embedded _implementor field can only record one implementor.
jiangli@12369 1105 // When there are more than one implementors, the _implementor field
coleenp@13728 1106 // is set to the interface Klass* itself. Following are the possible
jiangli@12369 1107 // values for the _implementor field:
jiangli@12369 1108 // NULL - no implementor
coleenp@13728 1109 // implementor Klass* - one implementor
jiangli@12369 1110 // self - more than one implementor
jiangli@12369 1111 //
jiangli@12369 1112 // The _implementor field only exists for interfaces.
coleenp@13728 1113 void InstanceKlass::add_implementor(Klass* k) {
coleenp@52091 1114 assert_lock_strong(Compile_lock);
jiangli@12369 1115 assert(is_interface(), "not interface");
duke@1 1116 // Filter out my subinterfaces.
duke@1 1117 // (Note: Interfaces are never on the subklass list.)
coleenp@13728 1118 if (InstanceKlass::cast(k)->is_interface()) return;
duke@1 1119
duke@1 1120 // Filter out subclasses whose supers already implement me.
duke@1 1121 // (Note: CHA must walk subclasses of direct implementors
duke@1 1122 // in order to locate indirect implementors.)
coleenp@33602 1123 Klass* sk = k->super();
coleenp@13728 1124 if (sk != NULL && InstanceKlass::cast(sk)->implements_interface(this))
duke@1 1125 // We only need to check one immediate superclass, since the
duke@1 1126 // implements_interface query looks at transitive_interfaces.
duke@1 1127 // Any supers of the super have the same (or fewer) transitive_interfaces.
duke@1 1128 return;
duke@1 1129
coleenp@13728 1130 Klass* ik = implementor();
jiangli@12369 1131 if (ik == NULL) {
jiangli@12369 1132 set_implementor(k);
coleenp@13728 1133 } else if (ik != this) {
jiangli@12369 1134 // There is already an implementor. Use itself as an indicator of
jiangli@12369 1135 // more than one implementors.
coleenp@13728 1136 set_implementor(this);
duke@1 1137 }
duke@1 1138
duke@1 1139 // The implementor also implements the transitive_interfaces
duke@1 1140 for (int index = 0; index < local_interfaces()->length(); index++) {
coleenp@13728 1141 InstanceKlass::cast(local_interfaces()->at(index))->add_implementor(k);
duke@1 1142 }
duke@1 1143 }
duke@1 1144
coleenp@13728 1145 void InstanceKlass::init_implementor() {
jiangli@12369 1146 if (is_interface()) {
jiangli@12369 1147 set_implementor(NULL);
jiangli@12369 1148 }
duke@1 1149 }
duke@1 1150
duke@1 1151
coleenp@13728 1152 void InstanceKlass::process_interfaces(Thread *thread) {
duke@1 1153 // link this class into the implementors list of every interface it implements
duke@1 1154 for (int i = local_interfaces()->length() - 1; i >= 0; i--) {
coleenp@13728 1155 assert(local_interfaces()->at(i)->is_klass(), "must be a klass");
coleenp@13728 1156 InstanceKlass* interf = InstanceKlass::cast(local_interfaces()->at(i));
duke@1 1157 assert(interf->is_interface(), "expected interface");
coleenp@23515 1158 interf->add_implementor(this);
duke@1 1159 }
duke@1 1160 }
duke@1 1161
coleenp@13728 1162 bool InstanceKlass::can_be_primary_super_slow() const {
duke@1 1163 if (is_interface())
duke@1 1164 return false;
duke@1 1165 else
duke@1 1166 return Klass::can_be_primary_super_slow();
duke@1 1167 }
duke@1 1168
ccheung@50473 1169 GrowableArray<Klass*>* InstanceKlass::compute_secondary_supers(int num_extra_slots,
iklam@52030 1170 Array<InstanceKlass*>* transitive_interfaces) {
duke@1 1171 // The secondaries are the implemented interfaces.
iklam@52030 1172 Array<InstanceKlass*>* interfaces = transitive_interfaces;
duke@1 1173 int num_secondaries = num_extra_slots + interfaces->length();
duke@1 1174 if (num_secondaries == 0) {
coleenp@13728 1175 // Must share this for correct bootstrapping!
coleenp@13728 1176 set_secondary_supers(Universe::the_empty_klass_array());
coleenp@13728 1177 return NULL;
duke@1 1178 } else if (num_extra_slots == 0) {
iklam@52030 1179 // The secondary super list is exactly the same as the transitive interfaces, so
iklam@52030 1180 // let's use it instead of making a copy.
coleenp@13728 1181 // Redefine classes has to be careful not to delete this!
iklam@52030 1182 // We need the cast because Array<Klass*> is NOT a supertype of Array<InstanceKlass*>,
iklam@52030 1183 // (but it's safe to do here because we won't write into _secondary_supers from this point on).
iklam@52030 1184 set_secondary_supers((Array<Klass*>*)(address)interfaces);
coleenp@13728 1185 return NULL;
duke@1 1186 } else {
coleenp@13728 1187 // Copy transitive interfaces to a temporary growable array to be constructed
coleenp@13728 1188 // into the secondary super list with extra slots.
coleenp@13728 1189 GrowableArray<Klass*>* secondaries = new GrowableArray<Klass*>(interfaces->length());
duke@1 1190 for (int i = 0; i < interfaces->length(); i++) {
coleenp@13728 1191 secondaries->push(interfaces->at(i));
duke@1 1192 }
duke@1 1193 return secondaries;
duke@1 1194 }
duke@1 1195 }
duke@1 1196
coleenp@13728 1197 bool InstanceKlass::compute_is_subtype_of(Klass* k) {
hseigel@14488 1198 if (k->is_interface()) {
duke@1 1199 return implements_interface(k);
duke@1 1200 } else {
duke@1 1201 return Klass::compute_is_subtype_of(k);
duke@1 1202 }
duke@1 1203 }
duke@1 1204
coleenp@13728 1205 bool InstanceKlass::implements_interface(Klass* k) const {
coleenp@13728 1206 if (this == k) return true;
hseigel@14488 1207 assert(k->is_interface(), "should be an interface class");
duke@1 1208 for (int i = 0; i < transitive_interfaces()->length(); i++) {
coleenp@13728 1209 if (transitive_interfaces()->at(i) == k) {
duke@1 1210 return true;
duke@1 1211 }
duke@1 1212 }
duke@1 1213 return false;
duke@1 1214 }
duke@1 1215
hseigel@21768 1216 bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
hseigel@21768 1217 // Verify direct super interface
hseigel@21768 1218 if (this == k) return true;
hseigel@21768 1219 assert(k->is_interface(), "should be an interface class");
hseigel@21768 1220 for (int i = 0; i < local_interfaces()->length(); i++) {
hseigel@21768 1221 if (local_interfaces()->at(i) == k) {
hseigel@21768 1222 return true;
hseigel@21768 1223 }
hseigel@21768 1224 }
hseigel@21768 1225 return false;
hseigel@21768 1226 }
hseigel@21768 1227
coleenp@13728 1228 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) {
dnsimon@52862 1229 check_array_allocation_length(length, arrayOopDesc::max_array_length(T_OBJECT), CHECK_NULL);
duke@1 1230 int size = objArrayOopDesc::object_size(length);
coleenp@13728 1231 Klass* ak = array_klass(n, CHECK_NULL);
eosterlund@51516 1232 objArrayOop o = (objArrayOop)Universe::heap()->array_allocate(ak, size, length,
eosterlund@51516 1233 /* do_zero */ true, CHECK_NULL);
duke@1 1234 return o;
duke@1 1235 }
duke@1 1236
coleenp@13728 1237 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
duke@1 1238 if (TraceFinalizerRegistration) {
duke@1 1239 tty->print("Registered ");
duke@1 1240 i->print_value_on(tty);
david@33148 1241 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", p2i(i));
duke@1 1242 }
duke@1 1243 instanceHandle h_i(THREAD, i);
duke@1 1244 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
duke@1 1245 JavaValue result(T_VOID);
duke@1 1246 JavaCallArguments args(h_i);
duke@1 1247 methodHandle mh (THREAD, Universe::finalizer_register_method());
duke@1 1248 JavaCalls::call(&result, mh, &args, CHECK_NULL);
duke@1 1249 return h_i();
duke@1 1250 }
duke@1 1251
coleenp@13728 1252 instanceOop InstanceKlass::allocate_instance(TRAPS) {
duke@1 1253 bool has_finalizer_flag = has_finalizer(); // Query before possible GC
duke@1 1254 int size = size_helper(); // Query before forming handle.
duke@1 1255
duke@1 1256 instanceOop i;
duke@1 1257
eosterlund@51516 1258 i = (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
duke@1 1259 if (has_finalizer_flag && !RegisterFinalizersAtInit) {
duke@1 1260 i = register_finalizer(i, CHECK_NULL);
duke@1 1261 }
duke@1 1262 return i;
duke@1 1263 }
duke@1 1264
simonis@49858 1265 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
simonis@49858 1266 return instanceHandle(THREAD, allocate_instance(THREAD));
simonis@49858 1267 }
simonis@49858 1268
coleenp@13728 1269 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
duke@1 1270 if (is_interface() || is_abstract()) {
duke@1 1271 ResourceMark rm(THREAD);
duke@1 1272 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
duke@1 1273 : vmSymbols::java_lang_InstantiationException(), external_name());
duke@1 1274 }
coleenp@13728 1275 if (this == SystemDictionary::Class_klass()) {
duke@1 1276 ResourceMark rm(THREAD);
duke@1 1277 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
duke@1 1278 : vmSymbols::java_lang_IllegalAccessException(), external_name());
duke@1 1279 }
duke@1 1280 }
duke@1 1281
coleenp@13728 1282 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
dholmes@40887 1283 // Need load-acquire for lock-free read
hseigel@46341 1284 if (array_klasses_acquire() == NULL) {
duke@1 1285 if (or_null) return NULL;
duke@1 1286
duke@1 1287 ResourceMark rm;
duke@1 1288 JavaThread *jt = (JavaThread *)THREAD;
duke@1 1289 {
duke@1 1290 // Atomic creation of array_klasses
duke@1 1291 MutexLocker ma(MultiArray_lock, THREAD);
duke@1 1292
duke@1 1293 // Check if update has already taken place
hseigel@46341 1294 if (array_klasses() == NULL) {
hseigel@46341 1295 Klass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
dholmes@40887 1296 // use 'release' to pair with lock-free load
hseigel@46341 1297 release_set_array_klasses(k);
duke@1 1298 }
duke@1 1299 }
duke@1 1300 }
duke@1 1301 // _this will always be set at this point
hseigel@46341 1302 ObjArrayKlass* oak = (ObjArrayKlass*)array_klasses();
duke@1 1303 if (or_null) {
duke@1 1304 return oak->array_klass_or_null(n);
duke@1 1305 }
stefank@27680 1306 return oak->array_klass(n, THREAD);
duke@1 1307 }
duke@1 1308
coleenp@13728 1309 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) {
duke@1 1310 return array_klass_impl(or_null, 1, THREAD);
duke@1 1311 }
duke@1 1312
hseigel@46341 1313 static int call_class_initializer_counter = 0; // for debugging
kvn@42650 1314
coleenp@46329 1315 Method* InstanceKlass::class_initializer() const {
coleenp@13728 1316 Method* clinit = find_method(
kamg@8653 1317 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
kamg@8653 1318 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
kamg@8653 1319 return clinit;
kamg@8653 1320 }
kamg@8653 1321 return NULL;
duke@1 1322 }
duke@1 1323
hseigel@46341 1324 void InstanceKlass::call_class_initializer(TRAPS) {
minqi@14477 1325 if (ReplayCompiles &&
minqi@14477 1326 (ReplaySuppressInitializers == 1 ||
jwilhelm@46630 1327 (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
minqi@14477 1328 // Hide the existence of the initializer for the purpose of replaying the compile
minqi@14477 1329 return;
minqi@14477 1330 }
minqi@14477 1331
hseigel@46341 1332 methodHandle h_method(THREAD, class_initializer());
hseigel@46341 1333 assert(!is_initialized(), "we cannot initialize twice");
stuefe@46701 1334 LogTarget(Info, class, init) lt;
stuefe@46701 1335 if (lt.is_enabled()) {
mockner@34628 1336 ResourceMark rm;
stuefe@46701 1337 LogStream ls(lt);
stuefe@46701 1338 ls.print("%d Initializing ", call_class_initializer_counter++);
stuefe@46701 1339 name()->print_value_on(&ls);
stuefe@46701 1340 ls.print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
duke@1 1341 }
duke@1 1342 if (h_method() != NULL) {
duke@1 1343 JavaCallArguments args; // No arguments
duke@1 1344 JavaValue result(T_VOID);
duke@1 1345 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
duke@1 1346 }
duke@1 1347 }
duke@1 1348
duke@1 1349
coleenp@33593 1350 void InstanceKlass::mask_for(const methodHandle& method, int bci,
duke@1 1351 InterpreterOopMap* entry_for) {
dholmes@38937 1352 // Lazily create the _oop_map_cache at first request
coleenp@47789 1353 // Lock-free access requires load_acquire.
coleenp@47789 1354 OopMapCache* oop_map_cache = OrderAccess::load_acquire(&_oop_map_cache);
dholmes@38937 1355 if (oop_map_cache == NULL) {
duke@1 1356 MutexLocker x(OopMapCacheAlloc_lock);
dholmes@38937 1357 // Check if _oop_map_cache was allocated while we were waiting for this lock
dholmes@38937 1358 if ((oop_map_cache = _oop_map_cache) == NULL) {
dholmes@38937 1359 oop_map_cache = new OopMapCache();
dholmes@38937 1360 // Ensure _oop_map_cache is stable, since it is examined without a lock
coleenp@47789 1361 OrderAccess::release_store(&_oop_map_cache, oop_map_cache);
duke@1 1362 }
duke@1 1363 }
dholmes@38937 1364 // _oop_map_cache is constant after init; lookup below does its own locking.
dholmes@38937 1365 oop_map_cache->lookup(method, bci, entry_for);
duke@1 1366 }
duke@1 1367
duke@1 1368
coleenp@13728 1369 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
coleenp@13728 1370 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@10546 1371 Symbol* f_name = fs.name();
never@10546 1372 Symbol* f_sig = fs.signature();
duke@1 1373 if (f_name == name && f_sig == sig) {
drchase@20017 1374 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
duke@1 1375 return true;
duke@1 1376 }
duke@1 1377 }
duke@1 1378 return false;
duke@1 1379 }
duke@1 1380
duke@1 1381
coleenp@13728 1382 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
duke@1 1383 const int n = local_interfaces()->length();
duke@1 1384 for (int i = 0; i < n; i++) {
coleenp@13728 1385 Klass* intf1 = local_interfaces()->at(i);
hseigel@14488 1386 assert(intf1->is_interface(), "just checking type");
duke@1 1387 // search for field in current interface
coleenp@13728 1388 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
duke@1 1389 assert(fd->is_static(), "interface field must be static");
duke@1 1390 return intf1;
duke@1 1391 }
duke@1 1392 // search for field in direct superinterfaces
coleenp@13728 1393 Klass* intf2 = InstanceKlass::cast(intf1)->find_interface_field(name, sig, fd);
duke@1 1394 if (intf2 != NULL) return intf2;
duke@1 1395 }
duke@1 1396 // otherwise field lookup fails
duke@1 1397 return NULL;
duke@1 1398 }
duke@1 1399
duke@1 1400
coleenp@13728 1401 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
duke@1 1402 // search order according to newest JVM spec (5.4.3.2, p.167).
duke@1 1403 // 1) search for field in current klass
duke@1 1404 if (find_local_field(name, sig, fd)) {
coleenp@13728 1405 return const_cast<InstanceKlass*>(this);
duke@1 1406 }
duke@1 1407 // 2) search for field recursively in direct superinterfaces
coleenp@13728 1408 { Klass* intf = find_interface_field(name, sig, fd);
duke@1 1409 if (intf != NULL) return intf;
duke@1 1410 }
duke@1 1411 // 3) apply field lookup recursively if superclass exists
coleenp@13728 1412 { Klass* supr = super();
coleenp@13728 1413 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, fd);
duke@1 1414 }
duke@1 1415 // 4) otherwise field lookup fails
duke@1 1416 return NULL;
duke@1 1417 }
duke@1 1418
duke@1 1419
coleenp@13728 1420 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
duke@1 1421 // search order according to newest JVM spec (5.4.3.2, p.167).
duke@1 1422 // 1) search for field in current klass
duke@1 1423 if (find_local_field(name, sig, fd)) {
coleenp@13728 1424 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
duke@1 1425 }
duke@1 1426 // 2) search for field recursively in direct superinterfaces
duke@1 1427 if (is_static) {
coleenp@13728 1428 Klass* intf = find_interface_field(name, sig, fd);
duke@1 1429 if (intf != NULL) return intf;
duke@1 1430 }
duke@1 1431 // 3) apply field lookup recursively if superclass exists
coleenp@13728 1432 { Klass* supr = super();
coleenp@13728 1433 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
duke@1 1434 }
duke@1 1435 // 4) otherwise field lookup fails
duke@1 1436 return NULL;
duke@1 1437 }
duke@1 1438
duke@1 1439
coleenp@13728 1440 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
coleenp@13728 1441 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@10546 1442 if (fs.offset() == offset) {
drchase@20017 1443 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
duke@1 1444 if (fd->is_static() == is_static) return true;
duke@1 1445 }
duke@1 1446 }
duke@1 1447 return false;
duke@1 1448 }
duke@1 1449
duke@1 1450
coleenp@13728 1451 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
coleenp@13728 1452 Klass* klass = const_cast<InstanceKlass*>(this);
duke@1 1453 while (klass != NULL) {
coleenp@13728 1454 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
duke@1 1455 return true;
duke@1 1456 }
hseigel@14488 1457 klass = klass->super();
duke@1 1458 }
duke@1 1459 return false;
duke@1 1460 }
duke@1 1461
duke@1 1462
coleenp@13728 1463 void InstanceKlass::methods_do(void f(Method* method)) {
coleenp@24658 1464 // Methods aren't stable until they are loaded. This can be read outside
coleenp@24658 1465 // a lock through the ClassLoaderData for profiling
coleenp@24658 1466 if (!is_loaded()) {
coleenp@24658 1467 return;
coleenp@24658 1468 }
coleenp@24658 1469
duke@1 1470 int len = methods()->length();
duke@1 1471 for (int index = 0; index < len; index++) {
coleenp@13728 1472 Method* m = methods()->at(index);
duke@1 1473 assert(m->is_method(), "must be method");
duke@1 1474 f(m);
duke@1 1475 }
duke@1 1476 }
duke@1 1477
never@8725 1478
coleenp@13728 1479 void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
never@10546 1480 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@10546 1481 if (fs.access_flags().is_static()) {
drchase@20017 1482 fieldDescriptor& fd = fs.field_descriptor();
never@10546 1483 cl->do_field(&fd);
never@10546 1484 }
duke@1 1485 }
duke@1 1486 }
duke@1 1487
duke@1 1488
coleenp@23872 1489 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
hseigel@46341 1490 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@10546 1491 if (fs.access_flags().is_static()) {
drchase@20017 1492 fieldDescriptor& fd = fs.field_descriptor();
coleenp@23872 1493 f(&fd, mirror, CHECK);
never@10546 1494 }
duke@1 1495 }
duke@1 1496 }
duke@1 1497
duke@1 1498
kvn@217 1499 static int compare_fields_by_offset(int* a, int* b) {
kvn@217 1500 return a[0] - b[0];
kvn@217 1501 }
kvn@217 1502
coleenp@13728 1503 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
coleenp@13728 1504 InstanceKlass* super = superklass();
duke@1 1505 if (super != NULL) {
duke@1 1506 super->do_nonstatic_fields(cl);
duke@1 1507 }
kvn@217 1508 fieldDescriptor fd;
never@10546 1509 int length = java_fields_count();
kvn@217 1510 // In DebugInfo nonstatic fields are sorted by offset.
zgu@13195 1511 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1), mtClass);
kvn@217 1512 int j = 0;
never@10546 1513 for (int i = 0; i < length; i += 1) {
drchase@20017 1514 fd.reinitialize(this, i);
kvn@217 1515 if (!fd.is_static()) {
kvn@217 1516 fields_sorted[j + 0] = fd.offset();
kvn@217 1517 fields_sorted[j + 1] = i;
kvn@217 1518 j += 2;
kvn@217 1519 }
duke@1 1520 }
kvn@217 1521 if (j > 0) {
kvn@217 1522 length = j;
kvn@217 1523 // _sort_Fn is defined in growableArray.hpp.
kvn@217 1524 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset);
kvn@217 1525 for (int i = 0; i < length; i += 2) {
drchase@20017 1526 fd.reinitialize(this, fields_sorted[i + 1]);
kvn@217 1527 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields");
kvn@217 1528 cl->do_field(&fd);
kvn@217 1529 }
kvn@217 1530 }
coleenp@27880 1531 FREE_C_HEAP_ARRAY(int, fields_sorted);
duke@1 1532 }
duke@1 1533
duke@1 1534
coleenp@13728 1535 void InstanceKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
coleenp@13728 1536 if (array_klasses() != NULL)
coleenp@13952 1537 ArrayKlass::cast(array_klasses())->array_klasses_do(f, THREAD);
coleenp@13728 1538 }
coleenp@13728 1539
coleenp@13728 1540 void InstanceKlass::array_klasses_do(void f(Klass* k)) {
duke@1 1541 if (array_klasses() != NULL)
coleenp@13952 1542 ArrayKlass::cast(array_klasses())->array_klasses_do(f);
duke@1 1543 }
duke@1 1544
duke@1 1545 #ifdef ASSERT
mgronlun@34666 1546 static int linear_search(const Array<Method*>* methods,
mgronlun@34666 1547 const Symbol* name,
mgronlun@34666 1548 const Symbol* signature) {
mgronlun@34666 1549 const int len = methods->length();
duke@1 1550 for (int index = 0; index < len; index++) {
mgronlun@34666 1551 const Method* const m = methods->at(index);
duke@1 1552 assert(m->is_method(), "must be method");
duke@1 1553 if (m->signature() == signature && m->name() == name) {
duke@1 1554 return index;
duke@1 1555 }
duke@1 1556 }
duke@1 1557 return -1;
duke@1 1558 }
duke@1 1559 #endif
duke@1 1560
mgronlun@34666 1561 static int binary_search(const Array<Method*>* methods, const Symbol* name) {
duke@1 1562 int len = methods->length();
duke@1 1563 // methods are sorted, so do binary search
duke@1 1564 int l = 0;
duke@1 1565 int h = len - 1;
duke@1 1566 while (l <= h) {
duke@1 1567 int mid = (l + h) >> 1;
coleenp@13728 1568 Method* m = methods->at(mid);
duke@1 1569 assert(m->is_method(), "must be method");
duke@1 1570 int res = m->name()->fast_compare(name);
duke@1 1571 if (res == 0) {
kamg@14385 1572 return mid;
duke@1 1573 } else if (res < 0) {
duke@1 1574 l = mid + 1;
duke@1 1575 } else {
duke@1 1576 h = mid - 1;
duke@1 1577 }
duke@1 1578 }
kamg@14385 1579 return -1;
kamg@14385 1580 }
kamg@14385 1581
acorn@20391 1582 // find_method looks up the name/signature in the local methods array
mgronlun@34666 1583 Method* InstanceKlass::find_method(const Symbol* name,
mgronlun@34666 1584 const Symbol* signature) const {
acorn@32189 1585 return find_method_impl(name, signature, find_overpass, find_static, find_private);
lfoltan@23999 1586 }
lfoltan@23999 1587
mgronlun@34666 1588 Method* InstanceKlass::find_method_impl(const Symbol* name,
mgronlun@34666 1589 const Symbol* signature,
acorn@32189 1590 OverpassLookupMode overpass_mode,
acorn@32189 1591 StaticLookupMode static_mode,
acorn@32189 1592 PrivateLookupMode private_mode) const {
mgronlun@34666 1593 return InstanceKlass::find_method_impl(methods(),
mgronlun@34666 1594 name,
mgronlun@34666 1595 signature,
mgronlun@34666 1596 overpass_mode,
mgronlun@34666 1597 static_mode,
mgronlun@34666 1598 private_mode);
kamg@14385 1599 }
kamg@14385 1600
acorn@21913 1601 // find_instance_method looks up the name/signature in the local methods array
acorn@21913 1602 // and skips over static methods
mgronlun@34666 1603 Method* InstanceKlass::find_instance_method(const Array<Method*>* methods,
mgronlun@34666 1604 const Symbol* name,
dholmes@51369 1605 const Symbol* signature,
dholmes@51369 1606 PrivateLookupMode private_mode) {
mgronlun@34666 1607 Method* const meth = InstanceKlass::find_method_impl(methods,
mgronlun@34666 1608 name,
mgronlun@34666 1609 signature,
mgronlun@34666 1610 find_overpass,
mgronlun@34666 1611 skip_static,
dholmes@51369 1612 private_mode);
mgronlun@34666 1613 assert(((meth == NULL) || !meth->is_static()),
mgronlun@34666 1614 "find_instance_method should have skipped statics");
acorn@21913 1615 return meth;
acorn@21913 1616 }
acorn@21913 1617
drchase@28514 1618 // find_instance_method looks up the name/signature in the local methods array
drchase@28514 1619 // and skips over static methods
dholmes@51369 1620 Method* InstanceKlass::find_instance_method(const Symbol* name,
dholmes@51369 1621 const Symbol* signature,
dholmes@51369 1622 PrivateLookupMode private_mode) const {
dholmes@51369 1623 return InstanceKlass::find_instance_method(methods(), name, signature, private_mode);
drchase@28514 1624 }
drchase@28514 1625
acorn@32189 1626 // Find looks up the name/signature in the local methods array
acorn@32189 1627 // and filters on the overpass, static and private flags
acorn@32189 1628 // This returns the first one found
acorn@32189 1629 // note that the local methods array can have up to one overpass, one static
acorn@32189 1630 // and one instance (private or not) with the same name/signature
mgronlun@34666 1631 Method* InstanceKlass::find_local_method(const Symbol* name,
mgronlun@34666 1632 const Symbol* signature,
mgronlun@34666 1633 OverpassLookupMode overpass_mode,
mgronlun@34666 1634 StaticLookupMode static_mode,
mgronlun@34666 1635 PrivateLookupMode private_mode) const {
mgronlun@34666 1636 return InstanceKlass::find_method_impl(methods(),
mgronlun@34666 1637 name,
mgronlun@34666 1638 signature,
mgronlun@34666 1639 overpass_mode,
mgronlun@34666 1640 static_mode,
mgronlun@34666 1641 private_mode);
acorn@32189 1642 }
acorn@32189 1643
acorn@32189 1644 // Find looks up the name/signature in the local methods array
acorn@32189 1645 // and filters on the overpass, static and private flags
acorn@32189 1646 // This returns the first one found
acorn@32189 1647 // note that the local methods array can have up to one overpass, one static
acorn@32189 1648 // and one instance (private or not) with the same name/signature
mgronlun@34666 1649 Method* InstanceKlass::find_local_method(const Array<Method*>* methods,
mgronlun@34666 1650 const Symbol* name,
mgronlun@34666 1651 const Symbol* signature,
mgronlun@34666 1652 OverpassLookupMode overpass_mode,
mgronlun@34666 1653 StaticLookupMode static_mode,
mgronlun@34666 1654 PrivateLookupMode private_mode) {
mgronlun@34666 1655 return InstanceKlass::find_method_impl(methods,
mgronlun@34666 1656 name,
mgronlun@34666 1657 signature,
mgronlun@34666 1658 overpass_mode,
mgronlun@34666 1659 static_mode,
mgronlun@34666 1660 private_mode);
mgronlun@34666 1661 }
mgronlun@34666 1662
mgronlun@34666 1663 Method* InstanceKlass::find_method(const Array<Method*>* methods,
mgronlun@34666 1664 const Symbol* name,
mgronlun@34666 1665 const Symbol* signature) {
mgronlun@34666 1666 return InstanceKlass::find_method_impl(methods,
mgronlun@34666 1667 name,
mgronlun@34666 1668 signature,
mgronlun@34666 1669 find_overpass,
mgronlun@34666 1670 find_static,
mgronlun@34666 1671 find_private);
mgronlun@34666 1672 }
mgronlun@34666 1673
mgronlun@34666 1674 Method* InstanceKlass::find_method_impl(const Array<Method*>* methods,
mgronlun@34666 1675 const Symbol* name,
mgronlun@34666 1676 const Symbol* signature,
acorn@32189 1677 OverpassLookupMode overpass_mode,
acorn@32189 1678 StaticLookupMode static_mode,
acorn@32189 1679 PrivateLookupMode private_mode) {
acorn@32189 1680 int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode);
acorn@20391 1681 return hit >= 0 ? methods->at(hit): NULL;
acorn@20391 1682 }
acorn@20391 1683
mgronlun@34666 1684 // true if method matches signature and conforms to skipping_X conditions.
mgronlun@34666 1685 static bool method_matches(const Method* m,
mgronlun@34666 1686 const Symbol* signature,
mgronlun@34666 1687 bool skipping_overpass,
mgronlun@34666 1688 bool skipping_static,
mgronlun@34666 1689 bool skipping_private) {
mgronlun@34666 1690 return ((m->signature() == signature) &&
mgronlun@34666 1691 (!skipping_overpass || !m->is_overpass()) &&
mgronlun@34666 1692 (!skipping_static || !m->is_static()) &&
mgronlun@34666 1693 (!skipping_private || !m->is_private()));
drchase@28514 1694 }
drchase@28514 1695
acorn@20391 1696 // Used directly for default_methods to find the index into the
acorn@20391 1697 // default_vtable_indices, and indirectly by find_method
acorn@20391 1698 // find_method_index looks in the local methods array to return the index
lfoltan@23999 1699 // of the matching name/signature. If, overpass methods are being ignored,
lfoltan@23999 1700 // the search continues to find a potential non-overpass match. This capability
lfoltan@23999 1701 // is important during method resolution to prefer a static method, for example,
lfoltan@23999 1702 // over an overpass method.
acorn@32189 1703 // There is the possibility in any _method's array to have the same name/signature
acorn@32189 1704 // for a static method, an overpass method and a local instance method
acorn@32189 1705 // To correctly catch a given method, the search criteria may need
acorn@32189 1706 // to explicitly skip the other two. For local instance methods, it
acorn@32189 1707 // is often necessary to skip private methods
mgronlun@34666 1708 int InstanceKlass::find_method_index(const Array<Method*>* methods,
mgronlun@34666 1709 const Symbol* name,
mgronlun@34666 1710 const Symbol* signature,
mgronlun@34666 1711 OverpassLookupMode overpass_mode,
mgronlun@34666 1712 StaticLookupMode static_mode,
mgronlun@34666 1713 PrivateLookupMode private_mode) {
mgronlun@34666 1714 const bool skipping_overpass = (overpass_mode == skip_overpass);
mgronlun@34666 1715 const bool skipping_static = (static_mode == skip_static);
mgronlun@34666 1716 const bool skipping_private = (private_mode == skip_private);
mgronlun@34666 1717 const int hit = binary_search(methods, name);
kamg@14385 1718 if (hit != -1) {
mgronlun@34666 1719 const Method* const m = methods->at(hit);
drchase@28514 1720
kamg@14385 1721 // Do linear search to find matching signature. First, quick check
lfoltan@23999 1722 // for common case, ignoring overpasses if requested.
mgronlun@34666 1723 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) {
dholmes@51369 1724 return hit;
mgronlun@34666 1725 }
lfoltan@23999 1726
kamg@14385 1727 // search downwards through overloaded methods
kamg@14385 1728 int i;
kamg@14385 1729 for (i = hit - 1; i >= 0; --i) {
mgronlun@34666 1730 const Method* const m = methods->at(i);
kamg@14385 1731 assert(m->is_method(), "must be method");
mgronlun@34666 1732 if (m->name() != name) {
mgronlun@34666 1733 break;
mgronlun@34666 1734 }
mgronlun@34666 1735 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) {
mgronlun@34666 1736 return i;
mgronlun@34666 1737 }
kamg@14385 1738 }
kamg@14385 1739 // search upwards
kamg@14385 1740 for (i = hit + 1; i < methods->length(); ++i) {
mgronlun@34666 1741 const Method* const m = methods->at(i);
kamg@14385 1742 assert(m->is_method(), "must be method");
mgronlun@34666 1743 if (m->name() != name) {
mgronlun@34666 1744 break;
mgronlun@34666 1745 }
mgronlun@34666 1746 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) {
mgronlun@34666 1747 return i;
mgronlun@34666 1748 }
kamg@14385 1749 }
kamg@14385 1750 // not found
duke@1 1751 #ifdef ASSERT
mgronlun@34666 1752 const int index = (skipping_overpass || skipping_static || skipping_private) ? -1 :
mgronlun@34666 1753 linear_search(methods, name, signature);
mgronlun@34666 1754 assert(-1 == index, "binary search should have found entry %d", index);
duke@1 1755 #endif
kamg@14385 1756 }
acorn@20391 1757 return -1;
duke@1 1758 }
mgronlun@34666 1759
mgronlun@34666 1760 int InstanceKlass::find_method_by_name(const Symbol* name, int* end) const {
kamg@14385 1761 return find_method_by_name(methods(), name, end);
kamg@14385 1762 }
kamg@14385 1763
mgronlun@34666 1764 int InstanceKlass::find_method_by_name(const Array<Method*>* methods,
mgronlun@34666 1765 const Symbol* name,
mgronlun@34666 1766 int* end_ptr) {
kamg@14385 1767 assert(end_ptr != NULL, "just checking");
kamg@14385 1768 int start = binary_search(methods, name);
kamg@14385 1769 int end = start + 1;
kamg@14385 1770 if (start != -1) {
kamg@14385 1771 while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
kamg@14385 1772 while (end < methods->length() && (methods->at(end))->name() == name) ++end;
kamg@14385 1773 *end_ptr = end;
kamg@14385 1774 return start;
kamg@14385 1775 }
kamg@14385 1776 return -1;
kamg@14385 1777 }
kamg@14385 1778
hseigel@22232 1779 // uncached_lookup_method searches both the local class methods array and all
dholmes@51369 1780 // superclasses methods arrays, skipping any overpass methods in superclasses,
dholmes@51369 1781 // and possibly skipping private methods.
mgronlun@34666 1782 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
mgronlun@34666 1783 const Symbol* signature,
dholmes@51369 1784 OverpassLookupMode overpass_mode,
dholmes@51369 1785 PrivateLookupMode private_mode) const {
lfoltan@28731 1786 OverpassLookupMode overpass_local_mode = overpass_mode;
mgronlun@34666 1787 const Klass* klass = this;
duke@1 1788 while (klass != NULL) {
mgronlun@34666 1789 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
mgronlun@34666 1790 signature,
mgronlun@34666 1791 overpass_local_mode,
mgronlun@34666 1792 find_static,
dholmes@51369 1793 private_mode);
lfoltan@23999 1794 if (method != NULL) {
hseigel@22232 1795 return method;
hseigel@22232 1796 }
coleenp@33602 1797 klass = klass->super();
lfoltan@28731 1798 overpass_local_mode = skip_overpass; // Always ignore overpass methods in superclasses
duke@1 1799 }
duke@1 1800 return NULL;
duke@1 1801 }
duke@1 1802
dsamersoff@24828 1803 #ifdef ASSERT
dsamersoff@24828 1804 // search through class hierarchy and return true if this class or
dsamersoff@24828 1805 // one of the superclasses was redefined
mgronlun@34666 1806 bool InstanceKlass::has_redefined_this_or_super() const {
mgronlun@34666 1807 const Klass* klass = this;
dsamersoff@24828 1808 while (klass != NULL) {
coleenp@33602 1809 if (InstanceKlass::cast(klass)->has_been_redefined()) {
dsamersoff@24828 1810 return true;
dsamersoff@24828 1811 }
coleenp@33602 1812 klass = klass->super();
dsamersoff@24828 1813 }
dsamersoff@24828 1814 return false;
dsamersoff@24828 1815 }
dsamersoff@24828 1816 #endif
dsamersoff@24828 1817
acorn@20391 1818 // lookup a method in the default methods list then in all transitive interfaces
acorn@20391 1819 // Do NOT return private or static methods
acorn@20391 1820 Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name,
acorn@20391 1821 Symbol* signature) const {
acorn@20396 1822 Method* m = NULL;
acorn@20391 1823 if (default_methods() != NULL) {
acorn@20391 1824 m = find_method(default_methods(), name, signature);
acorn@20391 1825 }
acorn@20391 1826 // Look up interfaces
acorn@20391 1827 if (m == NULL) {
lfoltan@28731 1828 m = lookup_method_in_all_interfaces(name, signature, find_defaults);
acorn@20391 1829 }
acorn@20391 1830 return m;
acorn@20391 1831 }
acorn@20391 1832
duke@1 1833 // lookup a method in all the interfaces that this class implements
acorn@20284 1834 // Do NOT return private or static methods, new in JDK8 which are not externally visible
acorn@20284 1835 // They should only be found in the initial InterfaceMethodRef
coleenp@13728 1836 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name,
hseigel@22232 1837 Symbol* signature,
lfoltan@28731 1838 DefaultsLookupMode defaults_mode) const {
iklam@52030 1839 Array<InstanceKlass*>* all_ifs = transitive_interfaces();
duke@1 1840 int num_ifs = all_ifs->length();
coleenp@13728 1841 InstanceKlass *ik = NULL;
duke@1 1842 for (int i = 0; i < num_ifs; i++) {
iklam@52030 1843 ik = all_ifs->at(i);
coleenp@13728 1844 Method* m = ik->lookup_method(name, signature);
hseigel@22232 1845 if (m != NULL && m->is_public() && !m->is_static() &&
lfoltan@28731 1846 ((defaults_mode != skip_defaults) || !m->is_default_method())) {
duke@1 1847 return m;
duke@1 1848 }
duke@1 1849 }
duke@1 1850 return NULL;
duke@1 1851 }
duke@1 1852
duke@1 1853 /* jni_id_for_impl for jfieldIds only */
hseigel@46341 1854 JNIid* InstanceKlass::jni_id_for_impl(int offset) {
duke@1 1855 MutexLocker ml(JfieldIdCreation_lock);
duke@1 1856 // Retry lookup after we got the lock
hseigel@46341 1857 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
duke@1 1858 if (probe == NULL) {
duke@1 1859 // Slow case, allocate new static field identifier
hseigel@46341 1860 probe = new JNIid(this, offset, jni_ids());
hseigel@46341 1861 set_jni_ids(probe);
duke@1 1862 }
duke@1 1863 return probe;
duke@1 1864 }
duke@1 1865
duke@1 1866
duke@1 1867 /* jni_id_for for jfieldIds only */
coleenp@13728 1868 JNIid* InstanceKlass::jni_id_for(int offset) {
duke@1 1869 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
duke@1 1870 if (probe == NULL) {
hseigel@46341 1871 probe = jni_id_for_impl(offset);
duke@1 1872 }
duke@1 1873 return probe;
duke@1 1874 }
duke@1 1875
mgronlun@34666 1876 u2 InstanceKlass::enclosing_method_data(int offset) const {
mgronlun@34666 1877 const Array<jushort>* const inner_class_list = inner_classes();
jiangli@12231 1878 if (inner_class_list == NULL) {
jiangli@12231 1879 return 0;
jiangli@12231 1880 }
mgronlun@34666 1881 const int length = inner_class_list->length();
jiangli@12231 1882 if (length % inner_class_next_offset == 0) {
jiangli@12231 1883 return 0;
jiangli@12231 1884 }
mgronlun@34666 1885 const int index = length - enclosing_method_attribute_size;
mgronlun@34666 1886 assert(offset < enclosing_method_attribute_size, "invalid offset");
mgronlun@34666 1887 return inner_class_list->at(index + offset);
jiangli@12231 1888 }
jiangli@12231 1889
coleenp@13728 1890 void InstanceKlass::set_enclosing_method_indices(u2 class_index,
jiangli@12231 1891 u2 method_index) {
coleenp@13728 1892 Array<jushort>* inner_class_list = inner_classes();
jiangli@12231 1893 assert (inner_class_list != NULL, "_inner_classes list is not set up");
jiangli@12231 1894 int length = inner_class_list->length();
jiangli@12231 1895 if (length % inner_class_next_offset == enclosing_method_attribute_size) {
jiangli@12231 1896 int index = length - enclosing_method_attribute_size;
coleenp@13728 1897 inner_class_list->at_put(
jiangli@12231 1898 index + enclosing_method_class_index_offset, class_index);
coleenp@13728 1899 inner_class_list->at_put(
jiangli@12231 1900 index + enclosing_method_method_index_offset, method_index);
jiangli@12231 1901 }
jiangli@12231 1902 }
duke@1 1903
duke@1 1904 // Lookup or create a jmethodID.
dcubed@3824 1905 // This code is called by the VMThread and JavaThreads so the
dcubed@3824 1906 // locking has to be done very carefully to avoid deadlocks
dcubed@3824 1907 // and/or other cache consistency problems.
dcubed@3824 1908 //
hseigel@46341 1909 jmethodID InstanceKlass::get_jmethod_id(const methodHandle& method_h) {
duke@1 1910 size_t idnum = (size_t)method_h->method_idnum();
hseigel@46341 1911 jmethodID* jmeths = methods_jmethod_ids_acquire();
duke@1 1912 size_t length = 0;
duke@1 1913 jmethodID id = NULL;
duke@1 1914
dcubed@3824 1915 // We use a double-check locking idiom here because this cache is
dcubed@3824 1916 // performance sensitive. In the normal system, this cache only
dcubed@3824 1917 // transitions from NULL to non-NULL which is safe because we use
dcubed@3824 1918 // release_set_methods_jmethod_ids() to advertise the new cache.
dcubed@3824 1919 // A partially constructed cache should never be seen by a racing
coleenp@47789 1920 // thread. We also use release_store() to save a new jmethodID
dcubed@3824 1921 // in the cache so a partially constructed jmethodID should never be
dcubed@3824 1922 // seen either. Cache reads of existing jmethodIDs proceed without a
dcubed@3824 1923 // lock, but cache writes of a new jmethodID requires uniqueness and
dcubed@3824 1924 // creation of the cache itself requires no leaks so a lock is
dcubed@3824 1925 // generally acquired in those two cases.
dcubed@3824 1926 //
dcubed@3824 1927 // If the RedefineClasses() API has been used, then this cache can
dcubed@3824 1928 // grow and we'll have transitions from non-NULL to bigger non-NULL.
dcubed@3824 1929 // Cache creation requires no leaks and we require safety between all
dcubed@3824 1930 // cache accesses and freeing of the old cache so a lock is generally
dcubed@3824 1931 // acquired when the RedefineClasses() API has been used.
duke@1 1932
dcubed@3824 1933 if (jmeths != NULL) {
dcubed@3824 1934 // the cache already exists
hseigel@46341 1935 if (!idnum_can_increment()) {
dcubed@3824 1936 // the cache can't grow so we can just get the current values
dcubed@3824 1937 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@3824 1938 } else {
dcubed@3824 1939 // cache can grow so we have to be more careful
dcubed@3824 1940 if (Threads::number_of_threads() == 0 ||
dcubed@3824 1941 SafepointSynchronize::is_at_safepoint()) {
dcubed@3824 1942 // we're single threaded or at a safepoint - no locking needed
dcubed@3824 1943 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@3824 1944 } else {
dcubed@3824 1945 MutexLocker ml(JmethodIdCreation_lock);
dcubed@3824 1946 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@3824 1947 }
dcubed@3824 1948 }
dcubed@3824 1949 }
dcubed@3824 1950 // implied else:
dcubed@3824 1951 // we need to allocate a cache so default length and id values are good
dcubed@3824 1952
dcubed@3824 1953 if (jmeths == NULL || // no cache yet
dcubed@3824 1954 length <= idnum || // cache is too short
dcubed@3824 1955 id == NULL) { // cache doesn't contain entry
dcubed@3824 1956
dcubed@3824 1957 // This function can be called by the VMThread so we have to do all
dcubed@3824 1958 // things that might block on a safepoint before grabbing the lock.
dcubed@3824 1959 // Otherwise, we can deadlock with the VMThread or have a cache
dcubed@3824 1960 // consistency issue. These vars keep track of what we might have
dcubed@3824 1961 // to free after the lock is dropped.
dcubed@3824 1962 jmethodID to_dealloc_id = NULL;
dcubed@3824 1963 jmethodID* to_dealloc_jmeths = NULL;
dcubed@3824 1964
dcubed@3824 1965 // may not allocate new_jmeths or use it if we allocate it
duke@1 1966 jmethodID* new_jmeths = NULL;
duke@1 1967 if (length <= idnum) {
dcubed@3824 1968 // allocate a new cache that might be used
hseigel@46341 1969 size_t size = MAX2(idnum+1, (size_t)idnum_allocated_count());
zgu@13195 1970 new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1, mtClass);
duke@1 1971 memset(new_jmeths, 0, (size+1)*sizeof(jmethodID));
dcubed@3824 1972 // cache size is stored in element[0], other elements offset by one
dcubed@3824 1973 new_jmeths[0] = (jmethodID)size;
duke@1 1974 }
duke@1 1975
dcubed@3824 1976 // allocate a new jmethodID that might be used
duke@1 1977 jmethodID new_id = NULL;
duke@1 1978 if (method_h->is_old() && !method_h->is_obsolete()) {
duke@1 1979 // The method passed in is old (but not obsolete), we need to use the current version
hseigel@46341 1980 Method* current_method = method_with_idnum((int)idnum);
duke@1 1981 assert(current_method != NULL, "old and but not obsolete, so should exist");
hseigel@46341 1982 new_id = Method::make_jmethod_id(class_loader_data(), current_method);
duke@1 1983 } else {
duke@1 1984 // It is the current version of the method or an obsolete method,
duke@1 1985 // use the version passed in
hseigel@46341 1986 new_id = Method::make_jmethod_id(class_loader_data(), method_h());
duke@1 1987 }
duke@1 1988
dcubed@3824 1989 if (Threads::number_of_threads() == 0 ||
dcubed@3824 1990 SafepointSynchronize::is_at_safepoint()) {
dcubed@3824 1991 // we're single threaded or at a safepoint - no locking needed
hseigel@46341 1992 id = get_jmethod_id_fetch_or_update(idnum, new_id, new_jmeths,
dcubed@3824 1993 &to_dealloc_id, &to_dealloc_jmeths);
dcubed@222 1994 } else {
duke@1 1995 MutexLocker ml(JmethodIdCreation_lock);
hseigel@46341 1996 id = get_jmethod_id_fetch_or_update(idnum, new_id, new_jmeths,
dcubed@3824 1997 &to_dealloc_id, &to_dealloc_jmeths);
dcubed@3824 1998 }
dcubed@3824 1999
dcubed@3824 2000 // The lock has been dropped so we can free resources.
dcubed@3824 2001 // Free up either the old cache or the new cache if we allocated one.
dcubed@3824 2002 if (to_dealloc_jmeths != NULL) {
dcubed@3824 2003 FreeHeap(to_dealloc_jmeths);
dcubed@3824 2004 }
dcubed@3824 2005 // free up the new ID since it wasn't needed
dcubed@3824 2006 if (to_dealloc_id != NULL) {
hseigel@46341 2007 Method::destroy_jmethod_id(class_loader_data(), to_dealloc_id);
dcubed@222 2008 }
dcubed@222 2009 }
dcubed@222 2010 return id;
dcubed@222 2011 }
duke@1 2012
jmanson@27478 2013 // Figure out how many jmethodIDs haven't been allocated, and make
jmanson@27478 2014 // sure space for them is pre-allocated. This makes getting all
jmanson@27478 2015 // method ids much, much faster with classes with more than 8
jmanson@27478 2016 // methods, and has a *substantial* effect on performance with jvmti
jmanson@27478 2017 // code that loads all jmethodIDs for all classes.
jmanson@27478 2018 void InstanceKlass::ensure_space_for_methodids(int start_offset) {
jmanson@27478 2019 int new_jmeths = 0;
jmanson@27478 2020 int length = methods()->length();
jmanson@27478 2021 for (int index = start_offset; index < length; index++) {
jmanson@27478 2022 Method* m = methods()->at(index);
jmanson@27478 2023 jmethodID id = m->find_jmethod_id_or_null();
jmanson@27478 2024 if (id == NULL) {
jmanson@27478 2025 new_jmeths++;
jmanson@27478 2026 }
jmanson@27478 2027 }
jmanson@27478 2028 if (new_jmeths != 0) {
jmanson@27478 2029 Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
jmanson@27478 2030 }
jmanson@27478 2031 }
duke@1 2032
dcubed@3824 2033 // Common code to fetch the jmethodID from the cache or update the
dcubed@3824 2034 // cache with the new jmethodID. This function should never do anything
dcubed@3824 2035 // that causes the caller to go to a safepoint or we can deadlock with
dcubed@3824 2036 // the VMThread or have cache consistency issues.
dcubed@3824 2037 //
coleenp@13728 2038 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update(
hseigel@46341 2039 size_t idnum, jmethodID new_id,
dcubed@3824 2040 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
dcubed@3824 2041 jmethodID** to_dealloc_jmeths_p) {
dcubed@3824 2042 assert(new_id != NULL, "sanity check");
dcubed@3824 2043 assert(to_dealloc_id_p != NULL, "sanity check");
dcubed@3824 2044 assert(to_dealloc_jmeths_p != NULL, "sanity check");
dcubed@3824 2045 assert(Threads::number_of_threads() == 0 ||
dcubed@3824 2046 SafepointSynchronize::is_at_safepoint() ||
dcubed@3824 2047 JmethodIdCreation_lock->owned_by_self(), "sanity check");
dcubed@3824 2048
dcubed@3824 2049 // reacquire the cache - we are locked, single threaded or at a safepoint
hseigel@46341 2050 jmethodID* jmeths = methods_jmethod_ids_acquire();
dcubed@3824 2051 jmethodID id = NULL;
dcubed@3824 2052 size_t length = 0;
dcubed@222 2053
dcubed@3824 2054 if (jmeths == NULL || // no cache yet
dcubed@3824 2055 (length = (size_t)jmeths[0]) <= idnum) { // cache is too short
dcubed@222 2056 if (jmeths != NULL) {
dcubed@3824 2057 // copy any existing entries from the old cache
dcubed@222 2058 for (size_t index = 0; index < length; index++) {
dcubed@222 2059 new_jmeths[index+1] = jmeths[index+1];
duke@1 2060 }
dcubed@3824 2061 *to_dealloc_jmeths_p = jmeths; // save old cache for later delete
duke@1 2062 }
hseigel@46341 2063 release_set_methods_jmethod_ids(jmeths = new_jmeths);
dcubed@222 2064 } else {
dcubed@3824 2065 // fetch jmethodID (if any) from the existing cache
dcubed@222 2066 id = jmeths[idnum+1];
dcubed@3824 2067 *to_dealloc_jmeths_p = new_jmeths; // save new cache for later delete
dcubed@222 2068 }
dcubed@222 2069 if (id == NULL) {
dcubed@3824 2070 // No matching jmethodID in the existing cache or we have a new
dcubed@3824 2071 // cache or we just grew the cache. This cache write is done here
dcubed@3824 2072 // by the first thread to win the foot race because a jmethodID
dcubed@3824 2073 // needs to be unique once it is generally available.
dcubed@222 2074 id = new_id;
dcubed@3824 2075
dcubed@3824 2076 // The jmethodID cache can be read while unlocked so we have to
dcubed@3824 2077 // make sure the new jmethodID is complete before installing it
dcubed@3824 2078 // in the cache.
coleenp@47789 2079 OrderAccess::release_store(&jmeths[idnum+1], id);
dcubed@222 2080 } else {
dcubed@3824 2081 *to_dealloc_id_p = new_id; // save new id for later delete
duke@1 2082 }
duke@1 2083 return id;
duke@1 2084 }
duke@1 2085
duke@1 2086
dcubed@3824 2087 // Common code to get the jmethodID cache length and the jmethodID
dcubed@3824 2088 // value at index idnum if there is one.
dcubed@3824 2089 //
coleenp@13728 2090 void InstanceKlass::get_jmethod_id_length_value(jmethodID* cache,
dcubed@3824 2091 size_t idnum, size_t *length_p, jmethodID* id_p) {
dcubed@3824 2092 assert(cache != NULL, "sanity check");
dcubed@3824 2093 assert(length_p != NULL, "sanity check");
dcubed@3824 2094 assert(id_p != NULL, "sanity check");
dcubed@3824 2095
dcubed@3824 2096 // cache size is stored in element[0], other elements offset by one
dcubed@3824 2097 *length_p = (size_t)cache[0];
dcubed@3824 2098 if (*length_p <= idnum) { // cache is too short
dcubed@3824 2099 *id_p = NULL;
dcubed@3824 2100 } else {
dcubed@3824 2101 *id_p = cache[idnum+1]; // fetch jmethodID (if any)
dcubed@3824 2102 }
dcubed@3824 2103 }
dcubed@3824 2104
dcubed@3824 2105
duke@1 2106 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles
coleenp@13728 2107 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
duke@1 2108 size_t idnum = (size_t)method->method_idnum();
duke@1 2109 jmethodID* jmeths = methods_jmethod_ids_acquire();
duke@1 2110 size_t length; // length assigned as debugging crumb
duke@1 2111 jmethodID id = NULL;
dcubed@3824 2112 if (jmeths != NULL && // If there is a cache
duke@1 2113 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
duke@1 2114 id = jmeths[idnum+1]; // Look up the id (may be NULL)
duke@1 2115 }
duke@1 2116 return id;
duke@1 2117 }
duke@1 2118
vlivanov@34195 2119 inline DependencyContext InstanceKlass::dependencies() {
vlivanov@34195 2120 DependencyContext dep_context(&_dep_context);
vlivanov@34195 2121 return dep_context;
stefank@25492 2122 }
duke@1 2123
vlivanov@36300 2124 int InstanceKlass::mark_dependent_nmethods(KlassDepChange& changes) {
vlivanov@34195 2125 return dependencies().mark_dependent_nmethods(changes);
duke@1 2126 }
duke@1 2127
vlivanov@34195 2128 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
vlivanov@34195 2129 dependencies().add_dependent_nmethod(nm);
vlivanov@31037 2130 }
vlivanov@31037 2131
stefank@33576 2132 void InstanceKlass::remove_dependent_nmethod(nmethod* nm, bool delete_immediately) {
vlivanov@34195 2133 dependencies().remove_dependent_nmethod(nm, delete_immediately);
duke@1 2134 }
duke@1 2135
duke@1 2136 #ifndef PRODUCT
coleenp@13728 2137 void InstanceKlass::print_dependent_nmethods(bool verbose) {
vlivanov@34195 2138 dependencies().print_dependent_nmethods(verbose);
duke@1 2139 }
duke@1 2140
coleenp@13728 2141 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
vlivanov@34195 2142 return dependencies().is_dependent_nmethod(nm);
duke@1 2143 }
duke@1 2144 #endif //PRODUCT
duke@1 2145
coleenp@50316 2146 void InstanceKlass::clean_weak_instanceklass_links() {
coleenp@50316 2147 clean_implementors_list();
coleenp@50316 2148 clean_method_data();
stefank@33576 2149
vlivanov@34195 2150 // Since GC iterates InstanceKlasses sequentially, it is safe to remove stale entries here.
vlivanov@34195 2151 DependencyContext dep_context(&_dep_context);
vlivanov@34195 2152 dep_context.expunge_stale_entries();
stefank@33576 2153 }
stefank@33576 2154
coleenp@50316 2155 void InstanceKlass::clean_implementors_list() {
coleenp@50316 2156 assert(is_loader_alive(), "this klass should be live");
jiangli@12369 2157 if (is_interface()) {
coleenp@53285 2158 if (ClassUnloading) {
coleenp@53285 2159 Klass* impl = implementor();
coleenp@53285 2160 if (impl != NULL) {
coleenp@53285 2161 if (!impl->is_loader_alive()) {
coleenp@53285 2162 // remove this guy
coleenp@53285 2163 Klass** klass = adr_implementor();
coleenp@53285 2164 assert(klass != NULL, "null klass");
coleenp@53285 2165 if (klass != NULL) {
coleenp@53285 2166 *klass = NULL;
coleenp@53285 2167 }
coleenp@53285 2168 }
coleenp@53285 2169 }
duke@1 2170 }
duke@1 2171 }
duke@1 2172 }
duke@1 2173
coleenp@50316 2174 void InstanceKlass::clean_method_data() {
coleenp@13728 2175 for (int m = 0; m < methods()->length(); m++) {
coleenp@13728 2176 MethodData* mdo = methods()->at(m)->method_data();
coleenp@13728 2177 if (mdo != NULL) {
coleenp@50316 2178 mdo->clean_method_data(/*always_clean*/false);
coleenp@13728 2179 }
coleenp@13728 2180 }
coleenp@13728 2181 }
coleenp@13728 2182
kvn@42650 2183 bool InstanceKlass::supers_have_passed_fingerprint_checks() {
kvn@42650 2184 if (java_super() != NULL && !java_super()->has_passed_fingerprint_check()) {
kvn@42650 2185 ResourceMark rm;
kvn@42650 2186 log_trace(class, fingerprint)("%s : super %s not fingerprinted", external_name(), java_super()->external_name());
kvn@42650 2187 return false;
kvn@42650 2188 }
kvn@42650 2189
iklam@52030 2190 Array<InstanceKlass*>* local_interfaces = this->local_interfaces();
kvn@42650 2191 if (local_interfaces != NULL) {
kvn@42650 2192 int length = local_interfaces->length();
kvn@42650 2193 for (int i = 0; i < length; i++) {
iklam@52030 2194 InstanceKlass* intf = local_interfaces->at(i);
kvn@42650 2195 if (!intf->has_passed_fingerprint_check()) {
kvn@42650 2196 ResourceMark rm;
kvn@42650 2197 log_trace(class, fingerprint)("%s : interface %s not fingerprinted", external_name(), intf->external_name());
kvn@42650 2198 return false;
kvn@42650 2199 }
kvn@42650 2200 }
kvn@42650 2201 }
kvn@42650 2202
kvn@42650 2203 return true;
kvn@42650 2204 }
kvn@42650 2205
lfoltan@52170 2206 bool InstanceKlass::should_store_fingerprint(bool is_unsafe_anonymous) {
kvn@42650 2207 #if INCLUDE_AOT
kvn@42650 2208 // We store the fingerprint into the InstanceKlass only in the following 2 cases:
kvn@46553 2209 if (CalculateClassFingerprint) {
kvn@42650 2210 // (1) We are running AOT to generate a shared library.
kvn@42650 2211 return true;
kvn@42650 2212 }
kvn@42650 2213 if (DumpSharedSpaces) {
kvn@42650 2214 // (2) We are running -Xshare:dump to create a shared archive
kvn@42650 2215 return true;
kvn@42650 2216 }
lfoltan@52170 2217 if (UseAOT && is_unsafe_anonymous) {
lfoltan@52170 2218 // (3) We are using AOT code from a shared library and see an unsafe anonymous class
dlong@47823 2219 return true;
dlong@47823 2220 }
kvn@42650 2221 #endif
kvn@42650 2222
kvn@42650 2223 // In all other cases we might set the _misc_has_passed_fingerprint_check bit,
kvn@42650 2224 // but do not store the 64-bit fingerprint to save space.
kvn@42650 2225 return false;
kvn@42650 2226 }
kvn@42650 2227
kvn@42650 2228 bool InstanceKlass::has_stored_fingerprint() const {
kvn@42650 2229 #if INCLUDE_AOT
kvn@42650 2230 return should_store_fingerprint() || is_shared();
kvn@42650 2231 #else
kvn@42650 2232 return false;
kvn@42650 2233 #endif
kvn@42650 2234 }
kvn@42650 2235
kvn@42650 2236 uint64_t InstanceKlass::get_stored_fingerprint() const {
kvn@42650 2237 address adr = adr_fingerprint();
kvn@42650 2238 if (adr != NULL) {
kvn@42650 2239 return (uint64_t)Bytes::get_native_u8(adr); // adr may not be 64-bit aligned
kvn@42650 2240 }
kvn@42650 2241 return 0;
kvn@42650 2242 }
kvn@42650 2243
kvn@42650 2244 void InstanceKlass::store_fingerprint(uint64_t fingerprint) {
kvn@42650 2245 address adr = adr_fingerprint();
kvn@42650 2246 if (adr != NULL) {
kvn@42650 2247 Bytes::put_native_u8(adr, (u8)fingerprint); // adr may not be 64-bit aligned
kvn@42650 2248
kvn@42650 2249 ResourceMark rm;
kvn@42650 2250 log_trace(class, fingerprint)("stored as " PTR64_FORMAT " for class %s", fingerprint, external_name());
kvn@42650 2251 }
kvn@42650 2252 }
coleenp@13728 2253
iklam@46746 2254 void InstanceKlass::metaspace_pointers_do(MetaspaceClosure* it) {
iklam@46746 2255 Klass::metaspace_pointers_do(it);
iklam@46746 2256
iklam@46746 2257 if (log_is_enabled(Trace, cds)) {
iklam@46746 2258 ResourceMark rm;
iklam@46746 2259 log_trace(cds)("Iter(InstanceKlass): %p (%s)", this, external_name());
iklam@46746 2260 }
iklam@46746 2261
iklam@46746 2262 it->push(&_annotations);
iklam@46746 2263 it->push((Klass**)&_array_klasses);
iklam@46746 2264 it->push(&_constants);
iklam@46746 2265 it->push(&_inner_classes);
iklam@46746 2266 it->push(&_array_name);
iklam@46746 2267 #if INCLUDE_JVMTI
iklam@46746 2268 it->push(&_previous_versions);
iklam@46746 2269 #endif
iklam@46746 2270 it->push(&_methods);
iklam@46746 2271 it->push(&_default_methods);
iklam@46746 2272 it->push(&_local_interfaces);
iklam@46746 2273 it->push(&_transitive_interfaces);
iklam@46746 2274 it->push(&_method_ordering);
iklam@46746 2275 it->push(&_default_vtable_indices);
iklam@46746 2276 it->push(&_fields);
iklam@46746 2277
iklam@46746 2278 if (itable_length() > 0) {
iklam@46746 2279 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
iklam@46746 2280 int method_table_offset_in_words = ioe->offset()/wordSize;
iklam@46746 2281 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
iklam@46746 2282 / itableOffsetEntry::size();
iklam@46746 2283
iklam@46746 2284 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
iklam@46746 2285 if (ioe->interface_klass() != NULL) {
iklam@46746 2286 it->push(ioe->interface_klass_addr());
iklam@46746 2287 itableMethodEntry* ime = ioe->first_method_entry(this);
iklam@46746 2288 int n = klassItable::method_count_for_interface(ioe->interface_klass());
iklam@46746 2289 for (int index = 0; index < n; index ++) {
iklam@46746 2290 it->push(ime[index].method_addr());
iklam@46746 2291 }
iklam@46746 2292 }
iklam@46746 2293 }
iklam@46746 2294 }
dholmes@51369 2295
dholmes@51369 2296 it->push(&_nest_members);
iklam@46746 2297 }
iklam@46746 2298
coleenp@13728 2299 void InstanceKlass::remove_unshareable_info() {
duke@1 2300 Klass::remove_unshareable_info();
coleenp@46464 2301
coleenp@46464 2302 if (is_in_error_state()) {
coleenp@46464 2303 // Classes are attempted to link during dumping and may fail,
coleenp@46464 2304 // but these classes are still in the dictionary and class list in CLD.
coleenp@46464 2305 // Check in_error state first because in_error is > linked state, so
coleenp@46464 2306 // is_linked() is true.
coleenp@46464 2307 // If there's a linking error, there is nothing else to remove.
coleenp@46464 2308 return;
coleenp@46464 2309 }
coleenp@46464 2310
jiangli@52791 2311 // Reset to the 'allocated' state to prevent any premature accessing to
jiangli@52791 2312 // a shared class at runtime while the class is still being loaded and
jiangli@52791 2313 // restored. A class' init_state is set to 'loaded' at runtime when it's
jiangli@52791 2314 // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
jiangli@52791 2315 _init_state = allocated;
jiangli@52791 2316
coleenp@52091 2317 {
coleenp@52091 2318 MutexLocker ml(Compile_lock);
coleenp@52091 2319 init_implementor();
coleenp@52091 2320 }
coleenp@13728 2321
coleenp@13728 2322 constants()->remove_unshareable_info();
coleenp@13728 2323
coleenp@13728 2324 for (int i = 0; i < methods()->length(); i++) {
coleenp@13728 2325 Method* m = methods()->at(i);
coleenp@13728 2326 m->remove_unshareable_info();
coleenp@13728 2327 }
iklam@46746 2328
ccheung@47103 2329 // do array classes also.
ccheung@47103 2330 if (array_klasses() != NULL) {
ccheung@47103 2331 array_klasses()->remove_unshareable_info();
ccheung@47103 2332 }
ccheung@47103 2333
iklam@46746 2334 // These are not allocated from metaspace, but they should should all be empty
ccheung@47103 2335 // during dump time, so we don't need to worry about them in InstanceKlass::iterate().
iklam@46746 2336 guarantee(_source_debug_extension == NULL, "must be");
iklam@46746 2337 guarantee(_dep_context == DependencyContext::EMPTY, "must be");
iklam@46746 2338 guarantee(_osr_nmethods_head == NULL, "must be");
iklam@46746 2339
iklam@46746 2340 #if INCLUDE_JVMTI
iklam@46746 2341 guarantee(_breakpoints == NULL, "must be");
iklam@46746 2342 guarantee(_previous_versions == NULL, "must be");
iklam@46746 2343 #endif
ccheung@47103 2344
dholmes@51369 2345 _init_thread = NULL;
dholmes@51369 2346 _methods_jmethod_ids = NULL;
dholmes@51369 2347 _jni_ids = NULL;
dholmes@51369 2348 _oop_map_cache = NULL;
dholmes@51369 2349 // clear _nest_host to ensure re-load at runtime
dholmes@51369 2350 _nest_host = NULL;
duke@1 2351 }
duke@1 2352
ccheung@47103 2353 void InstanceKlass::remove_java_mirror() {
ccheung@47103 2354 Klass::remove_java_mirror();
ccheung@47103 2355
ccheung@47103 2356 // do array classes also.
ccheung@47103 2357 if (array_klasses() != NULL) {
ccheung@47103 2358 array_klasses()->remove_java_mirror();
ccheung@47103 2359 }
coleenp@13728 2360 }
coleenp@13728 2361
iklam@26135 2362 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
jiangli@52791 2363 // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
jiangli@52791 2364 // before the InstanceKlass is added to the SystemDictionary. Make
jiangli@52791 2365 // sure the current state is <loaded.
jiangli@52791 2366 assert(!is_loaded(), "invalid init state");
coleenp@46329 2367 set_package(loader_data, CHECK);
iklam@26135 2368 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
coleenp@13728 2369
coleenp@46329 2370 Array<Method*>* methods = this->methods();
coleenp@13728 2371 int num_methods = methods->length();
coleenp@13728 2372 for (int index2 = 0; index2 < num_methods; ++index2) {
coleenp@13728 2373 methodHandle m(THREAD, methods->at(index2));
coleenp@23872 2374 m->restore_unshareable_info(CHECK);
coleenp@13728 2375 }
coleenp@13728 2376 if (JvmtiExport::has_redefined_a_class()) {
coleenp@13728 2377 // Reinitialize vtable because RedefineClasses may have changed some
coleenp@13728 2378 // entries in this vtable for super classes so the CDS vtable might
coleenp@13728 2379 // point to old or obsolete entries. RedefineClasses doesn't fix up
coleenp@13728 2380 // vtables in the shared system dictionary, only the main one.
coleenp@13728 2381 // It also redefines the itable too so fix that too.
iklam@46408 2382 vtable().initialize_vtable(false, CHECK);
iklam@46408 2383 itable().initialize_itable(false, CHECK);
coleenp@13728 2384 }
coleenp@13728 2385
coleenp@13728 2386 // restore constant pool resolved references
coleenp@46329 2387 constants()->restore_unshareable_info(CHECK);
coleenp@46329 2388
ccheung@47103 2389 if (array_klasses() != NULL) {
ccheung@47103 2390 // Array classes have null protection domain.
ccheung@47103 2391 // --> see ArrayKlass::complete_create_array_klass()
ccheung@47103 2392 array_klasses()->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
ccheung@47103 2393 }
coleenp@13728 2394 }
coleenp@13728 2395
iklam@26135 2396 // returns true IFF is_in_error_state() has been changed as a result of this call.
iklam@26135 2397 bool InstanceKlass::check_sharing_error_state() {
iklam@26135 2398 assert(DumpSharedSpaces, "should only be called during dumping");
iklam@26135 2399 bool old_state = is_in_error_state();
iklam@26135 2400
iklam@26135 2401 if (!is_in_error_state()) {
iklam@26135 2402 bool bad = false;
iklam@26135 2403 for (InstanceKlass* sup = java_super(); sup; sup = sup->java_super()) {
iklam@26135 2404 if (sup->is_in_error_state()) {
iklam@26135 2405 bad = true;
iklam@26135 2406 break;
iklam@26135 2407 }
iklam@26135 2408 }
iklam@26135 2409 if (!bad) {
iklam@52030 2410 Array<InstanceKlass*>* interfaces = transitive_interfaces();
iklam@26135 2411 for (int i = 0; i < interfaces->length(); i++) {
iklam@52030 2412 InstanceKlass* iface = interfaces->at(i);
iklam@52030 2413 if (iface->is_in_error_state()) {
iklam@26135 2414 bad = true;
iklam@26135 2415 break;
iklam@26135 2416 }
iklam@26135 2417 }
iklam@26135 2418 }
iklam@26135 2419
iklam@26135 2420 if (bad) {
iklam@26135 2421 set_in_error_state();
iklam@26135 2422 }
iklam@26135 2423 }
iklam@26135 2424
iklam@26135 2425 return (old_state != is_in_error_state());
iklam@26135 2426 }
iklam@26135 2427
cjplummer@38059 2428 #if INCLUDE_JVMTI
coleenp@13728 2429 static void clear_all_breakpoints(Method* m) {
duke@1 2430 m->clear_all_breakpoints();
duke@1 2431 }
cjplummer@38059 2432 #endif
coleenp@17075 2433
coleenp@52687 2434 void InstanceKlass::unload_class(InstanceKlass* ik) {
coleenp@52687 2435 // Release dependencies.
coleenp@52687 2436 ik->dependencies().remove_all_dependents();
coleenp@52687 2437
coleenp@17075 2438 // notify the debugger
coleenp@17075 2439 if (JvmtiExport::should_post_class_unload()) {
coleenp@17075 2440 JvmtiExport::post_class_unload(ik);
coleenp@17075 2441 }
coleenp@17075 2442
coleenp@17075 2443 // notify ClassLoadingService of class unload
coleenp@17075 2444 ClassLoadingService::notify_class_unloaded(ik);
hseigel@52286 2445
hseigel@52286 2446 #if INCLUDE_JFR
hseigel@52286 2447 assert(ik != NULL, "invariant");
hseigel@52286 2448 EventClassUnload event;
hseigel@52286 2449 event.set_unloadedClass(ik);
hseigel@52286 2450 event.set_definingClassLoader(ik->class_loader_data());
hseigel@52286 2451 event.commit();
hseigel@52286 2452 #endif
coleenp@17075 2453 }
coleenp@17075 2454
coleenp@17075 2455 void InstanceKlass::release_C_heap_structures(InstanceKlass* ik) {
coleenp@17075 2456 // Clean up C heap
coleenp@17075 2457 ik->release_C_heap_structures();
coleenp@17075 2458 ik->constants()->release_C_heap_structures();
coleenp@17075 2459 }
coleenp@17075 2460
coleenp@13728 2461 void InstanceKlass::release_C_heap_structures() {
coleenp@17075 2462 // Can't release the constant pool here because the constant pool can be
coleenp@17075 2463 // deallocated separately from the InstanceKlass for default methods and
coleenp@17075 2464 // redefine classes.
coleenp@17075 2465
duke@1 2466 // Deallocate oop map cache
duke@1 2467 if (_oop_map_cache != NULL) {
duke@1 2468 delete _oop_map_cache;
duke@1 2469 _oop_map_cache = NULL;
duke@1 2470 }
duke@1 2471
duke@1 2472 // Deallocate JNI identifiers for jfieldIDs
duke@1 2473 JNIid::deallocate(jni_ids());
duke@1 2474 set_jni_ids(NULL);
duke@1 2475
duke@1 2476 jmethodID* jmeths = methods_jmethod_ids_acquire();
duke@1 2477 if (jmeths != (jmethodID*)NULL) {
duke@1 2478 release_set_methods_jmethod_ids(NULL);
duke@1 2479 FreeHeap(jmeths);
duke@1 2480 }
duke@1 2481
coleenp@52687 2482 assert(_dep_context == DependencyContext::EMPTY,
coleenp@52687 2483 "dependencies should already be cleaned");
duke@1 2484
cjplummer@38059 2485 #if INCLUDE_JVMTI
duke@1 2486 // Deallocate breakpoint records
duke@1 2487 if (breakpoints() != 0x0) {
duke@1 2488 methods_do(clear_all_breakpoints);
duke@1 2489 assert(breakpoints() == 0x0, "should have cleared breakpoints");
duke@1 2490 }
duke@1 2491
duke@1 2492 // deallocate the cached class file
iklam@49178 2493 if (_cached_class_file != NULL && !MetaspaceShared::is_in_shared_metaspace(_cached_class_file)) {
coleenp@27880 2494 os::free(_cached_class_file);
jiangli@18940 2495 _cached_class_file = NULL;
duke@1 2496 }
cjplummer@38059 2497 #endif
coleenp@8076 2498
coleenp@8076 2499 // Decrement symbol reference counts associated with the unloaded class.
coleenp@8076 2500 if (_name != NULL) _name->decrement_refcount();
coleenp@8076 2501 // unreference array name derived from this class name (arrays of an unloaded
coleenp@8076 2502 // class can't be referenced anymore).
coleenp@8076 2503 if (_array_name != NULL) _array_name->decrement_refcount();
coleenp@27880 2504 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension);
coleenp@8076 2505 }
coleenp@8076 2506
mgronlun@34666 2507 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
fparain@13201 2508 if (array == NULL) {
fparain@13201 2509 _source_debug_extension = NULL;
fparain@13201 2510 } else {
fparain@13201 2511 // Adding one to the attribute length in order to store a null terminator
fparain@13201 2512 // character could cause an overflow because the attribute length is
fparain@13201 2513 // already coded with an u4 in the classfile, but in practice, it's
fparain@13201 2514 // unlikely to happen.
fparain@13201 2515 assert((length+1) > length, "Overflow checking");
fparain@13201 2516 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
fparain@13201 2517 for (int i = 0; i < length; i++) {
fparain@13201 2518 sde[i] = array[i];
fparain@13201 2519 }
fparain@13201 2520 sde[length] = '\0';
fparain@13201 2521 _source_debug_extension = sde;
fparain@13201 2522 }
duke@1 2523 }
duke@1 2524
coleenp@13728 2525 const char* InstanceKlass::signature_name() const {
farvidsson@21183 2526 int hash_len = 0;
farvidsson@21183 2527 char hash_buf[40];
farvidsson@21183 2528
lfoltan@52170 2529 // If this is an unsafe anonymous class, append a hash to make the name unique
lfoltan@52170 2530 if (is_unsafe_anonymous()) {
farvidsson@21183 2531 intptr_t hash = (java_mirror() != NULL) ? java_mirror()->identity_hash() : 0;
goetz@27471 2532 jio_snprintf(hash_buf, sizeof(hash_buf), "/" UINTX_FORMAT, (uintx)hash);
farvidsson@21183 2533 hash_len = (int)strlen(hash_buf);
farvidsson@21183 2534 }
farvidsson@21183 2535
farvidsson@21183 2536 // Get the internal name as a c string
duke@1 2537 const char* src = (const char*) (name()->as_C_string());
duke@1 2538 const int src_length = (int)strlen(src);
farvidsson@21183 2539
farvidsson@21183 2540 char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
farvidsson@21183 2541
farvidsson@21183 2542 // Add L as type indicator
duke@1 2543 int dest_index = 0;
duke@1 2544 dest[dest_index++] = 'L';
farvidsson@21183 2545
farvidsson@21183 2546 // Add the actual class name
farvidsson@21183 2547 for (int src_index = 0; src_index < src_length; ) {
duke@1 2548 dest[dest_index++] = src[src_index++];
duke@1 2549 }
farvidsson@21183 2550
farvidsson@21183 2551 // If we have a hash, append it
farvidsson@21183 2552 for (int hash_index = 0; hash_index < hash_len; ) {
farvidsson@21183 2553 dest[dest_index++] = hash_buf[hash_index++];
farvidsson@21183 2554 }
farvidsson@21183 2555
farvidsson@21183 2556 // Add the semicolon and the NULL
duke@1 2557 dest[dest_index++] = ';';
duke@1 2558 dest[dest_index] = '\0';
duke@1 2559 return dest;
duke@1 2560 }
duke@1 2561
rprotacio@39216 2562 // Used to obtain the package name from a fully qualified class name.
rprotacio@39216 2563 Symbol* InstanceKlass::package_from_name(const Symbol* name, TRAPS) {
alanb@36508 2564 if (name == NULL) {
alanb@36508 2565 return NULL;
alanb@36508 2566 } else {
rprotacio@39216 2567 if (name->utf8_length() <= 0) {
alanb@36508 2568 return NULL;
alanb@36508 2569 }
rprotacio@39216 2570 ResourceMark rm;
rprotacio@39216 2571 const char* package_name = ClassLoader::package_from_name((const char*) name->as_C_string());
rprotacio@39216 2572 if (package_name == NULL) {
rprotacio@39216 2573 return NULL;
rprotacio@39216 2574 }
rprotacio@39216 2575 Symbol* pkg_name = SymbolTable::new_symbol(package_name, THREAD);
rprotacio@39216 2576 return pkg_name;
alanb@36508 2577 }
alanb@36508 2578 }
alanb@36508 2579
alanb@36508 2580 ModuleEntry* InstanceKlass::module() const {
lfoltan@52170 2581 // For an unsafe anonymous class return the host class' module
lfoltan@52170 2582 if (is_unsafe_anonymous()) {
lfoltan@52170 2583 assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
lfoltan@52170 2584 return unsafe_anonymous_host()->module();
lfoltan@52170 2585 }
lfoltan@52170 2586
lfoltan@52170 2587 // Class is in a named package
alanb@36508 2588 if (!in_unnamed_package()) {
alanb@36508 2589 return _package_entry->module();
alanb@36508 2590 }
lfoltan@52170 2591
lfoltan@52170 2592 // Class is in an unnamed package, return its loader's unnamed module
lfoltan@52170 2593 return class_loader_data()->unnamed_module();
alanb@36508 2594 }
alanb@36508 2595
alanb@36508 2596 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
acorn@40016 2597
acorn@40016 2598 // ensure java/ packages only loaded by boot or platform builtin loaders
lfoltan@51268 2599 check_prohibited_package(name(), loader_data, CHECK);
acorn@40016 2600
rprotacio@39216 2601 TempNewSymbol pkg_name = package_from_name(name(), CHECK);
rprotacio@39216 2602
rprotacio@39216 2603 if (pkg_name != NULL && loader_data != NULL) {
acorn@40016 2604
alanb@36508 2605 // Find in class loader's package entry table.
alanb@36508 2606 _package_entry = loader_data->packages()->lookup_only(pkg_name);
alanb@36508 2607
alanb@36508 2608 // If the package name is not found in the loader's package
alanb@36508 2609 // entry table, it is an indication that the package has not
alanb@36508 2610 // been defined. Consider it defined within the unnamed module.
alanb@36508 2611 if (_package_entry == NULL) {
alanb@36508 2612 ResourceMark rm;
alanb@36508 2613
alanb@36508 2614 if (!ModuleEntryTable::javabase_defined()) {
alanb@36508 2615 // Before java.base is defined during bootstrapping, define all packages in
alanb@36508 2616 // the java.base module. If a non-java.base package is erroneously placed
alanb@36508 2617 // in the java.base module it will be caught later when java.base
alanb@36508 2618 // is defined by ModuleEntryTable::verify_javabase_packages check.
rprotacio@42636 2619 assert(ModuleEntryTable::javabase_moduleEntry() != NULL, JAVA_BASE_NAME " module is NULL");
lfoltan@41183 2620 _package_entry = loader_data->packages()->lookup(pkg_name, ModuleEntryTable::javabase_moduleEntry());
alanb@36508 2621 } else {
rprotacio@46387 2622 assert(loader_data->unnamed_module() != NULL, "unnamed module is NULL");
alanb@36508 2623 _package_entry = loader_data->packages()->lookup(pkg_name,
rprotacio@46387 2624 loader_data->unnamed_module());
alanb@36508 2625 }
alanb@36508 2626
alanb@36508 2627 // A package should have been successfully created
alanb@36508 2628 assert(_package_entry != NULL, "Package entry for class %s not found, loader %s",
lfoltan@51268 2629 name()->as_C_string(), loader_data->loader_name_and_id());
alanb@36508 2630 }
alanb@36508 2631
alanb@44993 2632 if (log_is_enabled(Debug, module)) {
alanb@36508 2633 ResourceMark rm;
alanb@36508 2634 ModuleEntry* m = _package_entry->module();
alanb@44993 2635 log_trace(module)("Setting package: class: %s, package: %s, loader: %s, module: %s",
alanb@44993 2636 external_name(),
alanb@44993 2637 pkg_name->as_C_string(),
lfoltan@51268 2638 loader_data->loader_name_and_id(),
alanb@44993 2639 (m->is_named() ? m->name()->as_C_string() : UNNAMED_MODULE));
alanb@36508 2640 }
alanb@36508 2641 } else {
alanb@36508 2642 ResourceMark rm;
alanb@44993 2643 log_trace(module)("Setting package: class: %s, package: unnamed, loader: %s, module: %s",
alanb@44993 2644 external_name(),
lfoltan@51268 2645 (loader_data != NULL) ? loader_data->loader_name_and_id() : "NULL",
alanb@44993 2646 UNNAMED_MODULE);
alanb@36508 2647 }
alanb@36508 2648 }
alanb@36508 2649
alanb@36508 2650
alanb@36508 2651 // different versions of is_same_class_package
alanb@36508 2652
mgronlun@34666 2653 bool InstanceKlass::is_same_class_package(const Klass* class2) const {
alanb@36508 2654 oop classloader1 = this->class_loader();
alanb@36508 2655 PackageEntry* classpkg1 = this->package();
coleenp@33611 2656 if (class2->is_objArray_klass()) {
coleenp@13952 2657 class2 = ObjArrayKlass::cast(class2)->bottom_klass();
duke@1 2658 }
alanb@36508 2659
mgronlun@34666 2660 oop classloader2;
alanb@36508 2661 PackageEntry* classpkg2;
mgronlun@34666 2662 if (class2->is_instance_klass()) {
alanb@36508 2663 classloader2 = class2->class_loader();
rprotacio@38094 2664 classpkg2 = class2->package();
mgronlun@34666 2665 } else {
mgronlun@34666 2666 assert(class2->is_typeArray_klass(), "should be type array");
mgronlun@34666 2667 classloader2 = NULL;
alanb@36508 2668 classpkg2 = NULL;
mgronlun@34666 2669 }
alanb@36508 2670
alanb@36508 2671 // Same package is determined by comparing class loader
alanb@36508 2672 // and package entries. Both must be the same. This rule
alanb@36508 2673 // applies even to classes that are defined in the unnamed
alanb@36508 2674 // package, they still must have the same class loader.
rkennke@50153 2675 if (oopDesc::equals(classloader1, classloader2) && (classpkg1 == classpkg2)) {
alanb@36508 2676 return true;
alanb@36508 2677 }
alanb@36508 2678
alanb@36508 2679 return false;
duke@1 2680 }
duke@1 2681
hseigel@46697 2682 // return true if this class and other_class are in the same package. Classloader
hseigel@46697 2683 // and classname information is enough to determine a class's package
mgronlun@34666 2684 bool InstanceKlass::is_same_class_package(oop other_class_loader,
mgronlun@34666 2685 const Symbol* other_class_name) const {
rkennke@50153 2686 if (!oopDesc::equals(class_loader(), other_class_loader)) {
duke@1 2687 return false;
hseigel@46697 2688 }
hseigel@46697 2689 if (name()->fast_compare(other_class_name) == 0) {
hseigel@46697 2690 return true;
hseigel@46697 2691 }
hseigel@46697 2692
hseigel@46697 2693 {
duke@1 2694 ResourceMark rm;
duke@1 2695
rprotacio@39216 2696 bool bad_class_name = false;
hseigel@46697 2697 const char* other_pkg =
hseigel@46697 2698 ClassLoader::package_from_name((const char*) other_class_name->as_C_string(), &bad_class_name);
rprotacio@39216 2699 if (bad_class_name) {
rprotacio@39216 2700 return false;
rprotacio@39216 2701 }
hseigel@46697 2702 // Check that package_from_name() returns NULL, not "", if there is no package.
hseigel@46697 2703 assert(other_pkg == NULL || strlen(other_pkg) > 0, "package name is empty string");
hseigel@46697 2704
hseigel@46697 2705 const Symbol* const this_package_name =
hseigel@46697 2706 this->package() != NULL ? this->package()->name() : NULL;
hseigel@46697 2707
hseigel@46697 2708 if (this_package_name == NULL || other_pkg == NULL) {
hseigel@46697 2709 // One of the two doesn't have a package. Only return true if the other
hseigel@46697 2710 // one also doesn't have a package.
hseigel@46697 2711 return (const char*)this_package_name == other_pkg;
alanb@36508 2712 }
alanb@36508 2713
hseigel@46697 2714 // Check if package is identical
hseigel@46697 2715 return this_package_name->equals(other_pkg);
duke@1 2716 }
duke@1 2717 }
duke@1 2718
acorn@2264 2719 // Returns true iff super_method can be overridden by a method in targetclassname
rprotacio@39216 2720 // See JLS 3rd edition 8.4.6.1
acorn@2264 2721 // Assumes name-signature match
coleenp@13728 2722 // "this" is InstanceKlass of super_method which must exist
coleenp@13728 2723 // note that the InstanceKlass of the method in the targetclassname has not always been created yet
coleenp@33593 2724 bool InstanceKlass::is_override(const methodHandle& super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
acorn@2264 2725 // Private methods can not be overridden
acorn@2264 2726 if (super_method->is_private()) {
acorn@2264 2727 return false;
acorn@2264 2728 }
acorn@2264 2729 // If super method is accessible, then override
acorn@2264 2730 if ((super_method->is_protected()) ||
acorn@2264 2731 (super_method->is_public())) {
acorn@2264 2732 return true;
acorn@2264 2733 }
acorn@2264 2734 // Package-private methods are not inherited outside of package
acorn@2264 2735 assert(super_method->is_package_private(), "must be package private");
coleenp@8076 2736 return(is_same_class_package(targetclassloader(), targetclassname));
acorn@2264 2737 }
twisti@2131 2738
acorn@40016 2739 // Only boot and platform class loaders can define classes in "java/" packages.
acorn@40016 2740 void InstanceKlass::check_prohibited_package(Symbol* class_name,
lfoltan@51268 2741 ClassLoaderData* loader_data,
alanb@42307 2742 TRAPS) {
lfoltan@51268 2743 if (!loader_data->is_boot_class_loader_data() &&
lfoltan@51268 2744 !loader_data->is_platform_class_loader_data() &&
alanb@44326 2745 class_name != NULL) {
alanb@44326 2746 ResourceMark rm(THREAD);
alanb@44326 2747 char* name = class_name->as_C_string();
alanb@44326 2748 if (strncmp(name, JAVAPKG, JAVAPKG_LEN) == 0 && name[JAVAPKG_LEN] == '/') {
alanb@44326 2749 TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK);
alanb@44326 2750 assert(pkg_name != NULL, "Error in parsing package name starting with 'java/'");
alanb@44326 2751 name = pkg_name->as_C_string();
lfoltan@51268 2752 const char* class_loader_name = loader_data->loader_name_and_id();
alanb@44326 2753 StringUtils::replace_no_expand(name, "/", ".");
alanb@44326 2754 const char* msg_text1 = "Class loader (instance of): ";
alanb@44326 2755 const char* msg_text2 = " tried to load prohibited package name: ";
alanb@44326 2756 size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(name) + 1;
alanb@44326 2757 char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
alanb@44326 2758 jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, name);
alanb@44326 2759 THROW_MSG(vmSymbols::java_lang_SecurityException(), message);
alanb@44326 2760 }
acorn@40016 2761 }
acorn@40016 2762 return;
acorn@40016 2763 }
acorn@40016 2764
hseigel@46341 2765 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
hseigel@46341 2766 constantPoolHandle i_cp(THREAD, constants());
hseigel@46341 2767 for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
vlivanov@30222 2768 int ioff = iter.inner_class_info_index();
vlivanov@30222 2769 if (ioff != 0) {
vlivanov@30222 2770 // Check to see if the name matches the class we're looking for
vlivanov@30222 2771 // before attempting to find the class.
hseigel@46341 2772 if (i_cp->klass_name_at_matches(this, ioff)) {
vlivanov@30222 2773 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
hseigel@46341 2774 if (this == inner_klass) {
vlivanov@30222 2775 *ooff = iter.outer_class_info_index();
vlivanov@30222 2776 *noff = iter.inner_name_index();
vlivanov@30222 2777 return true;
vlivanov@30222 2778 }
vlivanov@30222 2779 }
vlivanov@30222 2780 }
vlivanov@30222 2781 }
vlivanov@30222 2782 return false;
vlivanov@30222 2783 }
vlivanov@30222 2784
hseigel@46341 2785 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
mgronlun@34666 2786 InstanceKlass* outer_klass = NULL;
vlivanov@30222 2787 *inner_is_member = false;
vlivanov@30222 2788 int ooff = 0, noff = 0;
hseigel@46341 2789 bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
hseigel@46341 2790 if (has_inner_classes_attr) {
hseigel@46341 2791 constantPoolHandle i_cp(THREAD, constants());
vlivanov@30222 2792 if (ooff != 0) {
vlivanov@30222 2793 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
mgronlun@34666 2794 outer_klass = InstanceKlass::cast(ok);
vlivanov@30222 2795 *inner_is_member = true;
vlivanov@30222 2796 }
mgronlun@34666 2797 if (NULL == outer_klass) {
lfoltan@52170 2798 // It may be unsafe anonymous; try for that.
hseigel@46341 2799 int encl_method_class_idx = enclosing_method_class_index();
vlivanov@30222 2800 if (encl_method_class_idx != 0) {
vlivanov@30222 2801 Klass* ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
mgronlun@34666 2802 outer_klass = InstanceKlass::cast(ok);
vlivanov@30222 2803 *inner_is_member = false;
vlivanov@30222 2804 }
vlivanov@30222 2805 }
vlivanov@30222 2806 }
vlivanov@30222 2807
vlivanov@30222 2808 // If no inner class attribute found for this class.
mgronlun@34666 2809 if (NULL == outer_klass) return NULL;
vlivanov@30222 2810
vlivanov@30222 2811 // Throws an exception if outer klass has not declared k as an inner klass
vlivanov@30222 2812 // We need evidence that each klass knows about the other, or else
vlivanov@30222 2813 // the system could allow a spoof of an inner class to gain access rights.
hseigel@46341 2814 Reflection::check_for_inner_class(outer_klass, this, *inner_is_member, CHECK_NULL);
mgronlun@34666 2815 return outer_klass;
vlivanov@30222 2816 }
duke@1 2817
coleenp@13728 2818 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
duke@1 2819 jint access = access_flags().as_int();
duke@1 2820
duke@1 2821 // But check if it happens to be member class.
coleenp@46329 2822 InnerClassesIterator iter(this);
jiangli@12231 2823 for (; !iter.done(); iter.next()) {
jiangli@12231 2824 int ioff = iter.inner_class_info_index();
jiangli@12231 2825 // Inner class attribute can be zero, skip it.
jiangli@12231 2826 // Strange but true: JVM spec. allows null inner class refs.
jiangli@12231 2827 if (ioff == 0) continue;
jiangli@12231 2828
jiangli@12231 2829 // only look at classes that are already loaded
jiangli@12231 2830 // since we are looking for the flags for our self.
coleenp@46329 2831 Symbol* inner_name = constants()->klass_name_at(ioff);
coleenp@46329 2832 if (name() == inner_name) {
jiangli@12231 2833 // This is really a member class.
jiangli@12231 2834 access = iter.inner_access_flags();
jiangli@12231 2835 break;
duke@1 2836 }
duke@1 2837 }
duke@1 2838 // Remember to strip ACC_SUPER bit
duke@1 2839 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
duke@1 2840 }
duke@1 2841
coleenp@13728 2842 jint InstanceKlass::jvmti_class_status() const {
duke@1 2843 jint result = 0;
duke@1 2844
duke@1 2845 if (is_linked()) {
duke@1 2846 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
duke@1 2847 }
duke@1 2848
duke@1 2849 if (is_initialized()) {
duke@1 2850 assert(is_linked(), "Class status is not consistent");
duke@1 2851 result |= JVMTI_CLASS_STATUS_INITIALIZED;
duke@1 2852 }
duke@1 2853 if (is_in_error_state()) {
duke@1 2854 result |= JVMTI_CLASS_STATUS_ERROR;
duke@1 2855 }
duke@1 2856 return result;
duke@1 2857 }
duke@1 2858
coleenp@13728 2859 Method* InstanceKlass::method_at_itable(Klass* holder, int index, TRAPS) {
duke@1 2860 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
duke@1 2861 int method_table_offset_in_words = ioe->offset()/wordSize;
duke@1 2862 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
duke@1 2863 / itableOffsetEntry::size();
duke@1 2864
duke@1 2865 for (int cnt = 0 ; ; cnt ++, ioe ++) {
twisti@2131 2866 // If the interface isn't implemented by the receiver class,
duke@1 2867 // the VM should throw IncompatibleClassChangeError.
duke@1 2868 if (cnt >= nof_interfaces) {
goetz@51267 2869 ResourceMark rm(THREAD);
goetz@51267 2870 stringStream ss;
lfoltan@51395 2871 bool same_module = (module() == holder->module());
goetz@51267 2872 ss.print("Receiver class %s does not implement "
lfoltan@51395 2873 "the interface %s defining the method to be called "
lfoltan@51395 2874 "(%s%s%s)",
lfoltan@51395 2875 external_name(), holder->external_name(),
lfoltan@51395 2876 (same_module) ? joint_in_module_of_loader(holder) : class_in_module_of_loader(),
lfoltan@51395 2877 (same_module) ? "" : "; ",
lfoltan@51395 2878 (same_module) ? "" : holder->class_in_module_of_loader());
goetz@51267 2879 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
duke@1 2880 }
duke@1 2881
coleenp@13728 2882 Klass* ik = ioe->interface_klass();
duke@1 2883 if (ik == holder) break;
duke@1 2884 }
duke@1 2885
coleenp@13728 2886 itableMethodEntry* ime = ioe->first_method_entry(this);
coleenp@13728 2887 Method* m = ime[index].method();
duke@1 2888 if (m == NULL) {
coleenp@13728 2889 THROW_NULL(vmSymbols::java_lang_AbstractMethodError());
duke@1 2890 }
duke@1 2891 return m;
duke@1 2892 }
duke@1 2893
acorn@20391 2894
acorn@20391 2895 #if INCLUDE_JVMTI
acorn@20391 2896 // update default_methods for redefineclasses for methods that are
acorn@20391 2897 // not yet in the vtable due to concurrent subclass define and superinterface
acorn@20391 2898 // redefinition
acorn@20391 2899 // Note: those in the vtable, should have been updated via adjust_method_entries
sspitsyn@29316 2900 void InstanceKlass::adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed) {
acorn@20391 2901 // search the default_methods for uses of either obsolete or EMCP methods
acorn@20391 2902 if (default_methods() != NULL) {
sspitsyn@29316 2903 for (int index = 0; index < default_methods()->length(); index ++) {
sspitsyn@29316 2904 Method* old_method = default_methods()->at(index);
sspitsyn@29316 2905 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
sspitsyn@29316 2906 continue; // skip uninteresting entries
sspitsyn@29316 2907 }
sspitsyn@29316 2908 assert(!old_method->is_deleted(), "default methods may not be deleted");
sspitsyn@29316 2909
sspitsyn@29316 2910 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
sspitsyn@29316 2911
sspitsyn@29316 2912 assert(new_method != NULL, "method_with_idnum() should not be NULL");
sspitsyn@29316 2913 assert(old_method != new_method, "sanity check");
sspitsyn@29316 2914
sspitsyn@29316 2915 default_methods()->at_put(index, new_method);
rehn@38259 2916 if (log_is_enabled(Info, redefine, class, update)) {
rehn@38259 2917 ResourceMark rm;
sspitsyn@29316 2918 if (!(*trace_name_printed)) {
rehn@38259 2919 log_info(redefine, class, update)
rehn@38259 2920 ("adjust: klassname=%s default methods from name=%s",
rehn@38259 2921 external_name(), old_method->method_holder()->external_name());
sspitsyn@29316 2922 *trace_name_printed = true;
acorn@20391 2923 }
rehn@38259 2924 log_debug(redefine, class, update, vtables)
rehn@38259 2925 ("default method update: %s(%s) ",
rehn@38259 2926 new_method->name()->as_C_string(), new_method->signature()->as_C_string());
acorn@20391 2927 }
acorn@20391 2928 }
acorn@20391 2929 }
acorn@20391 2930 }
acorn@20391 2931 #endif // INCLUDE_JVMTI
acorn@20391 2932
duke@1 2933 // On-stack replacement stuff
coleenp@13728 2934 void InstanceKlass::add_osr_nmethod(nmethod* n) {
duke@1 2935 // only one compilation can be active
thartmann@27409 2936 {
thartmann@27409 2937 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27409 2938 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
thartmann@27409 2939 assert(n->is_osr_method(), "wrong kind of nmethod");
thartmann@27409 2940 n->set_osr_link(osr_nmethods_head());
thartmann@27409 2941 set_osr_nmethods_head(n);
thartmann@27409 2942 // Raise the highest osr level if necessary
thartmann@27409 2943 if (TieredCompilation) {
thartmann@27409 2944 Method* m = n->method();
thartmann@27409 2945 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
thartmann@27409 2946 }
iveresov@6453 2947 }
iveresov@6453 2948
iveresov@6453 2949 // Get rid of the osr methods for the same bci that have lower levels.
iveresov@6453 2950 if (TieredCompilation) {
iveresov@6453 2951 for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) {
iveresov@6453 2952 nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true);
iveresov@6453 2953 if (inv != NULL && inv->is_in_use()) {
iveresov@6453 2954 inv->make_not_entrant();
iveresov@6453 2955 }
iveresov@6453 2956 }
iveresov@6453 2957 }
duke@1 2958 }
duke@1 2959
thartmann@36802 2960 // Remove osr nmethod from the list. Return true if found and removed.
thartmann@36802 2961 bool InstanceKlass::remove_osr_nmethod(nmethod* n) {
duke@1 2962 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27409 2963 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
duke@1 2964 assert(n->is_osr_method(), "wrong kind of nmethod");
duke@1 2965 nmethod* last = NULL;
duke@1 2966 nmethod* cur = osr_nmethods_head();
iveresov@6453 2967 int max_level = CompLevel_none; // Find the max comp level excluding n
coleenp@13728 2968 Method* m = n->method();
duke@1 2969 // Search for match
thartmann@36802 2970 bool found = false;
duke@1 2971 while(cur != NULL && cur != n) {
neliasso@22920 2972 if (TieredCompilation && m == cur->method()) {
iveresov@6453 2973 // Find max level before n
iveresov@6453 2974 max_level = MAX2(max_level, cur->comp_level());
iveresov@6453 2975 }
duke@1 2976 last = cur;
jrose@3908 2977 cur = cur->osr_link();
duke@1 2978 }
iveresov@6453 2979 nmethod* next = NULL;
duke@1 2980 if (cur == n) {
thartmann@36802 2981 found = true;
iveresov@6453 2982 next = cur->osr_link();
duke@1 2983 if (last == NULL) {
duke@1 2984 // Remove first element
iveresov@6453 2985 set_osr_nmethods_head(next);
duke@1 2986 } else {
iveresov@6453 2987 last->set_osr_link(next);
duke@1 2988 }
duke@1 2989 }
jrose@3908 2990 n->set_osr_link(NULL);
iveresov@6453 2991 if (TieredCompilation) {
iveresov@6453 2992 cur = next;
iveresov@6453 2993 while (cur != NULL) {
iveresov@6453 2994 // Find max level after n
neliasso@22920 2995 if (m == cur->method()) {
neliasso@22920 2996 max_level = MAX2(max_level, cur->comp_level());
neliasso@22920 2997 }
iveresov@6453 2998 cur = cur->osr_link();
iveresov@6453 2999 }
iveresov@6453 3000 m->set_highest_osr_comp_level(max_level);
iveresov@6453 3001 }
thartmann@36802 3002 return found;
duke@1 3003 }
duke@1 3004
thartmann@27434 3005 int InstanceKlass::mark_osr_nmethods(const Method* m) {
thartmann@27434 3006 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27434 3007 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
thartmann@27434 3008 nmethod* osr = osr_nmethods_head();
thartmann@27434 3009 int found = 0;
thartmann@27434 3010 while (osr != NULL) {
thartmann@27434 3011 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
thartmann@27434 3012 if (osr->method() == m) {
thartmann@27434 3013 osr->mark_for_deoptimization();
thartmann@27434 3014 found++;
thartmann@27434 3015 }
thartmann@27434 3016 osr = osr->osr_link();
thartmann@27434 3017 }
thartmann@27434 3018 return found;
thartmann@27434 3019 }
thartmann@27434 3020
minqi@17370 3021 nmethod* InstanceKlass::lookup_osr_nmethod(const Method* m, int bci, int comp_level, bool match_level) const {
duke@1 3022 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27409 3023 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
duke@1 3024 nmethod* osr = osr_nmethods_head();
iveresov@6453 3025 nmethod* best = NULL;
duke@1 3026 while (osr != NULL) {
duke@1 3027 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
iveresov@6453 3028 // There can be a time when a c1 osr method exists but we are waiting
iveresov@6453 3029 // for a c2 version. When c2 completes its osr nmethod we will trash
iveresov@6453 3030 // the c1 version and only be able to find the c2 version. However
iveresov@6453 3031 // while we overflow in the c1 code at back branches we don't want to
iveresov@6453 3032 // try and switch to the same code as we are already running
iveresov@6453 3033
duke@1 3034 if (osr->method() == m &&
duke@1 3035 (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) {
iveresov@6453 3036 if (match_level) {
iveresov@6453 3037 if (osr->comp_level() == comp_level) {
iveresov@6453 3038 // Found a match - return it.
iveresov@6453 3039 return osr;
iveresov@6453 3040 }
iveresov@6453 3041 } else {
iveresov@6453 3042 if (best == NULL || (osr->comp_level() > best->comp_level())) {
iveresov@6453 3043 if (osr->comp_level() == CompLevel_highest_tier) {
iveresov@6453 3044 // Found the best possible - return it.
iveresov@6453 3045 return osr;
iveresov@6453 3046 }
iveresov@6453 3047 best = osr;
iveresov@6453 3048 }
iveresov@6453 3049 }
duke@1 3050 }
jrose@3908 3051 osr = osr->osr_link();
duke@1 3052 }
iveresov@6453 3053 if (best != NULL && best->comp_level() >= comp_level && match_level == false) {
iveresov@6453 3054 return best;
iveresov@6453 3055 }
duke@1 3056 return NULL;
duke@1 3057 }
duke@1 3058
duke@1 3059 // -----------------------------------------------------------------------------------------------------
coleenp@13728 3060 // Printing
coleenp@13728 3061
duke@1 3062 #ifndef PRODUCT
duke@1 3063
jrose@2332 3064 #define BULLET " - "
jrose@2332 3065
coleenp@13728 3066 static const char* state_names[] = {
coleenp@13728 3067 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
coleenp@13728 3068 };
coleenp@13728 3069
drchase@20017 3070 static void print_vtable(intptr_t* start, int len, outputStream* st) {
drchase@20017 3071 for (int i = 0; i < len; i++) {
drchase@20017 3072 intptr_t e = start[i];
drchase@20017 3073 st->print("%d : " INTPTR_FORMAT, i, e);
drchase@20017 3074 if (e != 0 && ((Metadata*)e)->is_metaspace_object()) {
drchase@20017 3075 st->print(" ");
drchase@20017 3076 ((Metadata*)e)->print_value_on(st);
drchase@20017 3077 }
drchase@20017 3078 st->cr();
drchase@20017 3079 }
drchase@20017 3080 }
drchase@20017 3081
mgerdin@35900 3082 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
mgerdin@35900 3083 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
mgerdin@35900 3084 }
mgerdin@35900 3085
coleenp@13728 3086 void InstanceKlass::print_on(outputStream* st) const {
coleenp@13728 3087 assert(is_klass(), "must be klass");
coleenp@13728 3088 Klass::print_on(st);
coleenp@13728 3089
coleenp@13728 3090 st->print(BULLET"instance size: %d", size_helper()); st->cr();
coleenp@13728 3091 st->print(BULLET"klass size: %d", size()); st->cr();
coleenp@13728 3092 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
drchase@24424 3093 st->print(BULLET"state: "); st->print_cr("%s", state_names[_init_state]);
coleenp@13728 3094 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
hseigel@51957 3095 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
coleenp@13728 3096 st->print(BULLET"sub: ");
coleenp@53285 3097 Klass* sub = subklass();
coleenp@53285 3098 int n;
coleenp@53285 3099 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
coleenp@53285 3100 if (n < MaxSubklassPrintSize) {
coleenp@53285 3101 sub->print_value_on(st);
coleenp@53285 3102 st->print(" ");
coleenp@13728 3103 }
coleenp@13728 3104 }
coleenp@53285 3105 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
coleenp@53285 3106 st->cr();
coleenp@53285 3107
coleenp@53285 3108 if (is_interface()) {
coleenp@53285 3109 MutexLocker ml(Compile_lock);
coleenp@53285 3110 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
coleenp@53285 3111 if (nof_implementors() == 1) {
coleenp@53285 3112 st->print_cr(BULLET"implementor: ");
coleenp@53285 3113 st->print(" ");
coleenp@53285 3114 implementor()->print_value_on(st);
coleenp@53285 3115 st->cr();
coleenp@53285 3116 }
coleenp@53285 3117 }
coleenp@53285 3118
hseigel@51957 3119 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
coleenp@13728 3120 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
drchase@20017 3121 if (Verbose || WizardMode) {
coleenp@13728 3122 Array<Method*>* method_array = methods();
acorn@20391 3123 for (int i = 0; i < method_array->length(); i++) {
coleenp@13728 3124 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
coleenp@13728 3125 }
coleenp@13728 3126 }
acorn@20391 3127 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
acorn@20391 3128 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
acorn@20391 3129 if (Verbose && default_methods() != NULL) {
acorn@20391 3130 Array<Method*>* method_array = default_methods();
acorn@20391 3131 for (int i = 0; i < method_array->length(); i++) {
acorn@20391 3132 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
acorn@20391 3133 }
acorn@20391 3134 }
acorn@20391 3135 if (default_vtable_indices() != NULL) {
acorn@20391 3136 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr();
acorn@20391 3137 }
coleenp@13728 3138 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
coleenp@13728 3139 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
coleenp@13728 3140 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
coleenp@13728 3141 if (class_loader_data() != NULL) {
coleenp@13728 3142 st->print(BULLET"class loader data: ");
coleenp@13728 3143 class_loader_data()->print_value_on(st);
coleenp@13728 3144 st->cr();
coleenp@13728 3145 }
lfoltan@52170 3146 st->print(BULLET"unsafe anonymous host class: "); Metadata::print_value_on_maybe_null(st, unsafe_anonymous_host()); st->cr();
coleenp@13728 3147 if (source_file_name() != NULL) {
coleenp@13728 3148 st->print(BULLET"source file: ");
coleenp@13728 3149 source_file_name()->print_value_on(st);
coleenp@13728 3150 st->cr();
coleenp@13728 3151 }
coleenp@13728 3152 if (source_debug_extension() != NULL) {
coleenp@13728 3153 st->print(BULLET"source debug extension: ");
coleenp@13728 3154 st->print("%s", source_debug_extension());
coleenp@13728 3155 st->cr();
coleenp@13728 3156 }
coleenp@15601 3157 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
coleenp@15601 3158 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
coleenp@15601 3159 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
coleenp@15601 3160 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
coleenp@13728 3161 {
coleenp@20059 3162 bool have_pv = false;
coleenp@26558 3163 // previous versions are linked together through the InstanceKlass
cjplummer@38059 3164 for (InstanceKlass* pv_node = previous_versions();
coleenp@26558 3165 pv_node != NULL;
coleenp@26558 3166 pv_node = pv_node->previous_versions()) {
coleenp@20059 3167 if (!have_pv)
coleenp@20059 3168 st->print(BULLET"previous version: ");
coleenp@20059 3169 have_pv = true;
coleenp@26558 3170 pv_node->constants()->print_value_on(st);
coleenp@20059 3171 }
coleenp@20059 3172 if (have_pv) st->cr();
coleenp@26558 3173 }
coleenp@13728 3174
coleenp@13728 3175 if (generic_signature() != NULL) {
coleenp@13728 3176 st->print(BULLET"generic signature: ");
coleenp@13728 3177 generic_signature()->print_value_on(st);
coleenp@13728 3178 st->cr();
coleenp@13728 3179 }
coleenp@13728 3180 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
dholmes@51369 3181 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
hseigel@51960 3182 if (java_mirror() != NULL) {
hseigel@51960 3183 st->print(BULLET"java mirror: ");
hseigel@51960 3184 java_mirror()->print_value_on(st);
hseigel@51960 3185 st->cr();
hseigel@51960 3186 } else {
hseigel@51960 3187 st->print_cr(BULLET"java mirror: NULL");
hseigel@51960 3188 }
david@33148 3189 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
drchase@20017 3190 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
david@33148 3191 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
drchase@20017 3192 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
coleenp@13728 3193 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
coleenp@13728 3194 FieldPrinter print_static_field(st);
coleenp@13728 3195 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
coleenp@13728 3196 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
coleenp@13728 3197 FieldPrinter print_nonstatic_field(st);
coleenp@33602 3198 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
coleenp@33602 3199 ik->do_nonstatic_fields(&print_nonstatic_field);
coleenp@13728 3200
coleenp@13728 3201 st->print(BULLET"non-static oop maps: ");