1 /* 2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/altHashing.hpp" 27 #include "classfile/classLoaderData.inline.hpp" 28 #include "classfile/javaClasses.inline.hpp" 29 #include "classfile/moduleEntry.hpp" 30 #include "classfile/stringTable.hpp" 31 #include "classfile/vmSymbols.hpp" 32 #include "code/debugInfo.hpp" 33 #include "code/dependencyContext.hpp" 34 #include "code/pcDesc.hpp" 35 #include "interpreter/interpreter.hpp" 36 #include "interpreter/linkResolver.hpp" 37 #include "logging/log.hpp" 38 #include "logging/logStream.hpp" 39 #include "memory/heapShared.inline.hpp" 40 #include "memory/metaspaceShared.hpp" 41 #include "memory/oopFactory.hpp" 42 #include "memory/resourceArea.hpp" 43 #include "memory/universe.hpp" 44 #include "oops/fieldStreams.hpp" 45 #include "oops/instanceKlass.hpp" 46 #include "oops/instanceMirrorKlass.hpp" 47 #include "oops/klass.hpp" 48 #include "oops/method.inline.hpp" 49 #include "oops/objArrayOop.inline.hpp" 50 #include "oops/oop.inline.hpp" 51 #include "oops/symbol.hpp" 52 #include "oops/typeArrayOop.inline.hpp" 53 #include "prims/jvmtiExport.hpp" 54 #include "prims/resolvedMethodTable.hpp" 55 #include "runtime/fieldDescriptor.inline.hpp" 56 #include "runtime/frame.inline.hpp" 57 #include "runtime/handles.inline.hpp" 58 #include "runtime/interfaceSupport.inline.hpp" 59 #include "runtime/java.hpp" 60 #include "runtime/javaCalls.hpp" 61 #include "runtime/jniHandles.inline.hpp" 62 #include "runtime/safepoint.hpp" 63 #include "runtime/safepointVerifiers.hpp" 64 #include "runtime/thread.inline.hpp" 65 #include "runtime/vframe.inline.hpp" 66 #include "utilities/align.hpp" 67 #include "utilities/preserveException.hpp" 68 #if INCLUDE_JVMCI 69 #include "jvmci/jvmciJavaClasses.hpp" 70 #endif 71 72 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java) \ 73 klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum); 74 75 #if INCLUDE_CDS 76 #define INJECTED_FIELD_SERIALIZE_OFFSET(klass, name, signature, may_be_java) \ 77 f->do_u4((u4*)&_##name##_offset); 78 #endif 79 80 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java) \ 81 { SystemDictionary::WK_KLASS_ENUM_NAME(klass), vmSymbols::VM_SYMBOL_ENUM_NAME(name##_name), vmSymbols::VM_SYMBOL_ENUM_NAME(signature), may_be_java }, 82 83 InjectedField JavaClasses::_injected_fields[] = { 84 ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD) 85 }; 86 87 int JavaClasses::compute_injected_offset(InjectedFieldID id) { 88 return _injected_fields[id].compute_offset(); 89 } 90 91 InjectedField* JavaClasses::get_injected(Symbol* class_name, int* field_count) { 92 *field_count = 0; 93 94 vmSymbols::SID sid = vmSymbols::find_sid(class_name); 95 if (sid == vmSymbols::NO_SID) { 96 // Only well known classes can inject fields 97 return NULL; 98 } 99 100 int count = 0; 101 int start = -1; 102 103 #define LOOKUP_INJECTED_FIELD(klass, name, signature, may_be_java) \ 104 if (sid == vmSymbols::VM_SYMBOL_ENUM_NAME(klass)) { \ 105 count++; \ 106 if (start == -1) start = klass##_##name##_enum; \ 107 } 108 ALL_INJECTED_FIELDS(LOOKUP_INJECTED_FIELD); 109 #undef LOOKUP_INJECTED_FIELD 110 111 if (start != -1) { 112 *field_count = count; 113 return _injected_fields + start; 114 } 115 return NULL; 116 } 117 118 119 // Helpful routine for computing field offsets at run time rather than hardcoding them 120 // Finds local fields only, including static fields. Static field offsets are from the 121 // beginning of the mirror. 122 static void compute_offset(int &dest_offset, 123 InstanceKlass* ik, Symbol* name_symbol, Symbol* signature_symbol, 124 bool is_static = false) { 125 fieldDescriptor fd; 126 if (ik == NULL) { 127 ResourceMark rm; 128 log_error(class)("Mismatch JDK version for field: %s type: %s", name_symbol->as_C_string(), signature_symbol->as_C_string()); 129 vm_exit_during_initialization("Invalid layout of well-known class"); 130 } 131 132 if (!ik->find_local_field(name_symbol, signature_symbol, &fd) || fd.is_static() != is_static) { 133 ResourceMark rm; 134 log_error(class)("Invalid layout of %s field: %s type: %s", ik->external_name(), 135 name_symbol->as_C_string(), signature_symbol->as_C_string()); 136 #ifndef PRODUCT 137 // Prints all fields and offsets 138 Log(class) lt; 139 LogStream ls(lt.error()); 140 ik->print_on(&ls); 141 #endif //PRODUCT 142 vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class"); 143 } 144 dest_offset = fd.offset(); 145 } 146 147 // Overloading to pass name as a string. 148 static void compute_offset(int& dest_offset, InstanceKlass* ik, 149 const char* name_string, Symbol* signature_symbol, 150 bool is_static = false) { 151 TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string)); 152 if (name == NULL) { 153 ResourceMark rm; 154 log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string); 155 vm_exit_during_initialization("Invalid layout of well-known class", ik->external_name()); 156 } 157 compute_offset(dest_offset, ik, name, signature_symbol, is_static); 158 } 159 160 int java_lang_String::value_offset = 0; 161 int java_lang_String::hash_offset = 0; 162 int java_lang_String::coder_offset = 0; 163 164 bool java_lang_String::initialized = false; 165 166 bool java_lang_String::is_instance(oop obj) { 167 return is_instance_inlined(obj); 168 } 169 170 #if INCLUDE_CDS 171 #define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \ 172 f->do_u4((u4*)&offset) 173 #endif 174 175 #define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \ 176 compute_offset(offset, klass, name, vmSymbols::signature(), is_static) 177 178 #define STRING_FIELDS_DO(macro) \ 179 macro(value_offset, k, vmSymbols::value_name(), byte_array_signature, false); \ 180 macro(hash_offset, k, "hash", int_signature, false); \ 181 macro(coder_offset, k, "coder", byte_signature, false) 182 183 void java_lang_String::compute_offsets() { 184 if (initialized) { 185 return; 186 } 187 188 InstanceKlass* k = SystemDictionary::String_klass(); 189 STRING_FIELDS_DO(FIELD_COMPUTE_OFFSET); 190 191 initialized = true; 192 } 193 194 #if INCLUDE_CDS 195 void java_lang_String::serialize_offsets(SerializeClosure* f) { 196 STRING_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 197 f->do_u4((u4*)&initialized); 198 } 199 #endif 200 201 class CompactStringsFixup : public FieldClosure { 202 private: 203 bool _value; 204 205 public: 206 CompactStringsFixup(bool value) : _value(value) {} 207 208 void do_field(fieldDescriptor* fd) { 209 if (fd->name() == vmSymbols::compact_strings_name()) { 210 oop mirror = fd->field_holder()->java_mirror(); 211 assert(fd->field_holder() == SystemDictionary::String_klass(), "Should be String"); 212 assert(mirror != NULL, "String must have mirror already"); 213 mirror->bool_field_put(fd->offset(), _value); 214 } 215 } 216 }; 217 218 void java_lang_String::set_compact_strings(bool value) { 219 CompactStringsFixup fix(value); 220 InstanceKlass::cast(SystemDictionary::String_klass())->do_local_static_fields(&fix); 221 } 222 223 Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) { 224 assert(initialized, "Must be initialized"); 225 assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings"); 226 227 // Create the String object first, so there's a chance that the String 228 // and the char array it points to end up in the same cache line. 229 oop obj; 230 obj = SystemDictionary::String_klass()->allocate_instance(CHECK_NH); 231 232 // Create the char array. The String object must be handlized here 233 // because GC can happen as a result of the allocation attempt. 234 Handle h_obj(THREAD, obj); 235 int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16. 236 typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);; 237 238 // Point the String at the char array 239 obj = h_obj(); 240 set_value(obj, buffer); 241 // No need to zero the offset, allocation zero'ed the entire String object 242 set_coder(obj, is_latin1 ? CODER_LATIN1 : CODER_UTF16); 243 return h_obj; 244 } 245 246 Handle java_lang_String::create_from_unicode(const jchar* unicode, int length, TRAPS) { 247 bool is_latin1 = CompactStrings && UNICODE::is_latin1(unicode, length); 248 Handle h_obj = basic_create(length, is_latin1, CHECK_NH); 249 typeArrayOop buffer = value(h_obj()); 250 assert(TypeArrayKlass::cast(buffer->klass())->element_type() == T_BYTE, "only byte[]"); 251 if (is_latin1) { 252 for (int index = 0; index < length; index++) { 253 buffer->byte_at_put(index, (jbyte)unicode[index]); 254 } 255 } else { 256 for (int index = 0; index < length; index++) { 257 buffer->char_at_put(index, unicode[index]); 258 } 259 } 260 261 #ifdef ASSERT 262 { 263 ResourceMark rm; 264 char* expected = UNICODE::as_utf8(unicode, length); 265 char* actual = as_utf8_string(h_obj()); 266 if (strcmp(expected, actual) != 0) { 267 tty->print_cr("Unicode conversion failure: %s --> %s", expected, actual); 268 ShouldNotReachHere(); 269 } 270 } 271 #endif 272 273 return h_obj; 274 } 275 276 oop java_lang_String::create_oop_from_unicode(const jchar* unicode, int length, TRAPS) { 277 Handle h_obj = create_from_unicode(unicode, length, CHECK_0); 278 return h_obj(); 279 } 280 281 Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) { 282 if (utf8_str == NULL) { 283 return Handle(); 284 } 285 bool has_multibyte, is_latin1; 286 int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte); 287 if (!CompactStrings) { 288 has_multibyte = true; 289 is_latin1 = false; 290 } 291 292 Handle h_obj = basic_create(length, is_latin1, CHECK_NH); 293 if (length > 0) { 294 if (!has_multibyte) { 295 const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str); 296 ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length); 297 } else if (is_latin1) { 298 UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length); 299 } else { 300 UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length); 301 } 302 } 303 304 #ifdef ASSERT 305 // This check is too strict because the input string is not necessarily valid UTF8. 306 // For example, it may be created with arbitrary content via jni_NewStringUTF. 307 /* 308 { 309 ResourceMark rm; 310 const char* expected = utf8_str; 311 char* actual = as_utf8_string(h_obj()); 312 if (strcmp(expected, actual) != 0) { 313 tty->print_cr("String conversion failure: %s --> %s", expected, actual); 314 ShouldNotReachHere(); 315 } 316 } 317 */ 318 #endif 319 320 return h_obj; 321 } 322 323 oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) { 324 Handle h_obj = create_from_str(utf8_str, CHECK_0); 325 return h_obj(); 326 } 327 328 Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) { 329 const char* utf8_str = (char*)symbol->bytes(); 330 int utf8_len = symbol->utf8_length(); 331 332 bool has_multibyte, is_latin1; 333 int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte); 334 if (!CompactStrings) { 335 has_multibyte = true; 336 is_latin1 = false; 337 } 338 339 Handle h_obj = basic_create(length, is_latin1, CHECK_NH); 340 if (length > 0) { 341 if (!has_multibyte) { 342 const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str); 343 ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length); 344 } else if (is_latin1) { 345 UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length); 346 } else { 347 UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length); 348 } 349 } 350 351 #ifdef ASSERT 352 { 353 ResourceMark rm; 354 const char* expected = symbol->as_utf8(); 355 char* actual = as_utf8_string(h_obj()); 356 if (strncmp(expected, actual, utf8_len) != 0) { 357 tty->print_cr("Symbol conversion failure: %s --> %s", expected, actual); 358 ShouldNotReachHere(); 359 } 360 } 361 #endif 362 363 return h_obj; 364 } 365 366 // Converts a C string to a Java String based on current encoding 367 Handle java_lang_String::create_from_platform_dependent_str(const char* str, TRAPS) { 368 assert(str != NULL, "bad arguments"); 369 370 typedef jstring (*to_java_string_fn_t)(JNIEnv*, const char *); 371 static to_java_string_fn_t _to_java_string_fn = NULL; 372 373 if (_to_java_string_fn == NULL) { 374 void *lib_handle = os::native_java_library(); 375 _to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "NewStringPlatform")); 376 if (_to_java_string_fn == NULL) { 377 fatal("NewStringPlatform missing"); 378 } 379 } 380 381 jstring js = NULL; 382 { JavaThread* thread = (JavaThread*)THREAD; 383 assert(thread->is_Java_thread(), "must be java thread"); 384 HandleMark hm(thread); 385 ThreadToNativeFromVM ttn(thread); 386 js = (_to_java_string_fn)(thread->jni_environment(), str); 387 } 388 return Handle(THREAD, JNIHandles::resolve(js)); 389 } 390 391 // Converts a Java String to a native C string that can be used for 392 // native OS calls. 393 char* java_lang_String::as_platform_dependent_str(Handle java_string, TRAPS) { 394 typedef char* (*to_platform_string_fn_t)(JNIEnv*, jstring, bool*); 395 static to_platform_string_fn_t _to_platform_string_fn = NULL; 396 397 if (_to_platform_string_fn == NULL) { 398 void *lib_handle = os::native_java_library(); 399 _to_platform_string_fn = CAST_TO_FN_PTR(to_platform_string_fn_t, os::dll_lookup(lib_handle, "GetStringPlatformChars")); 400 if (_to_platform_string_fn == NULL) { 401 fatal("GetStringPlatformChars missing"); 402 } 403 } 404 405 char *native_platform_string; 406 { JavaThread* thread = (JavaThread*)THREAD; 407 assert(thread->is_Java_thread(), "must be java thread"); 408 JNIEnv *env = thread->jni_environment(); 409 jstring js = (jstring) JNIHandles::make_local(env, java_string()); 410 bool is_copy; 411 HandleMark hm(thread); 412 ThreadToNativeFromVM ttn(thread); 413 native_platform_string = (_to_platform_string_fn)(env, js, &is_copy); 414 assert(is_copy == JNI_TRUE, "is_copy value changed"); 415 JNIHandles::destroy_local(js); 416 } 417 return native_platform_string; 418 } 419 420 Handle java_lang_String::char_converter(Handle java_string, jchar from_char, jchar to_char, TRAPS) { 421 oop obj = java_string(); 422 // Typical usage is to convert all '/' to '.' in string. 423 typeArrayOop value = java_lang_String::value(obj); 424 int length = java_lang_String::length(obj); 425 bool is_latin1 = java_lang_String::is_latin1(obj); 426 427 // First check if any from_char exist 428 int index; // Declared outside, used later 429 for (index = 0; index < length; index++) { 430 jchar c = !is_latin1 ? value->char_at(index) : 431 ((jchar) value->byte_at(index)) & 0xff; 432 if (c == from_char) { 433 break; 434 } 435 } 436 if (index == length) { 437 // No from_char, so do not copy. 438 return java_string; 439 } 440 441 // Check if result string will be latin1 442 bool to_is_latin1 = false; 443 444 // Replacement char must be latin1 445 if (CompactStrings && UNICODE::is_latin1(to_char)) { 446 if (is_latin1) { 447 // Source string is latin1 as well 448 to_is_latin1 = true; 449 } else if (!UNICODE::is_latin1(from_char)) { 450 // We are replacing an UTF16 char. Scan string to 451 // check if result can be latin1 encoded. 452 to_is_latin1 = true; 453 for (index = 0; index < length; index++) { 454 jchar c = value->char_at(index); 455 if (c != from_char && !UNICODE::is_latin1(c)) { 456 to_is_latin1 = false; 457 break; 458 } 459 } 460 } 461 } 462 463 // Create new UNICODE (or byte) buffer. Must handlize value because GC 464 // may happen during String and char array creation. 465 typeArrayHandle h_value(THREAD, value); 466 Handle string = basic_create(length, to_is_latin1, CHECK_NH); 467 typeArrayOop from_buffer = h_value(); 468 typeArrayOop to_buffer = java_lang_String::value(string()); 469 470 // Copy contents 471 for (index = 0; index < length; index++) { 472 jchar c = (!is_latin1) ? from_buffer->char_at(index) : 473 ((jchar) from_buffer->byte_at(index)) & 0xff; 474 if (c == from_char) { 475 c = to_char; 476 } 477 if (!to_is_latin1) { 478 to_buffer->char_at_put(index, c); 479 } else { 480 to_buffer->byte_at_put(index, (jbyte) c); 481 } 482 } 483 return string; 484 } 485 486 jchar* java_lang_String::as_unicode_string(oop java_string, int& length, TRAPS) { 487 typeArrayOop value = java_lang_String::value(java_string); 488 length = java_lang_String::length(java_string); 489 bool is_latin1 = java_lang_String::is_latin1(java_string); 490 491 jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length); 492 if (result != NULL) { 493 if (!is_latin1) { 494 for (int index = 0; index < length; index++) { 495 result[index] = value->char_at(index); 496 } 497 } else { 498 for (int index = 0; index < length; index++) { 499 result[index] = ((jchar) value->byte_at(index)) & 0xff; 500 } 501 } 502 } else { 503 THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string"); 504 } 505 return result; 506 } 507 508 unsigned int java_lang_String::hash_code(oop java_string) { 509 int length = java_lang_String::length(java_string); 510 // Zero length string will hash to zero with String.hashCode() function. 511 if (length == 0) return 0; 512 513 typeArrayOop value = java_lang_String::value(java_string); 514 bool is_latin1 = java_lang_String::is_latin1(java_string); 515 516 if (is_latin1) { 517 return java_lang_String::hash_code(value->byte_at_addr(0), length); 518 } else { 519 return java_lang_String::hash_code(value->char_at_addr(0), length); 520 } 521 } 522 523 char* java_lang_String::as_quoted_ascii(oop java_string) { 524 typeArrayOop value = java_lang_String::value(java_string); 525 int length = java_lang_String::length(java_string); 526 bool is_latin1 = java_lang_String::is_latin1(java_string); 527 528 if (length == 0) return NULL; 529 530 char* result; 531 int result_length; 532 if (!is_latin1) { 533 jchar* base = value->char_at_addr(0); 534 result_length = UNICODE::quoted_ascii_length(base, length) + 1; 535 result = NEW_RESOURCE_ARRAY(char, result_length); 536 UNICODE::as_quoted_ascii(base, length, result, result_length); 537 } else { 538 jbyte* base = value->byte_at_addr(0); 539 result_length = UNICODE::quoted_ascii_length(base, length) + 1; 540 result = NEW_RESOURCE_ARRAY(char, result_length); 541 UNICODE::as_quoted_ascii(base, length, result, result_length); 542 } 543 assert(result_length >= length + 1, "must not be shorter"); 544 assert(result_length == (int)strlen(result) + 1, "must match"); 545 return result; 546 } 547 548 Symbol* java_lang_String::as_symbol(oop java_string, TRAPS) { 549 typeArrayOop value = java_lang_String::value(java_string); 550 int length = java_lang_String::length(java_string); 551 bool is_latin1 = java_lang_String::is_latin1(java_string); 552 if (!is_latin1) { 553 jchar* base = (length == 0) ? NULL : value->char_at_addr(0); 554 Symbol* sym = SymbolTable::lookup_unicode(base, length, THREAD); 555 return sym; 556 } else { 557 ResourceMark rm; 558 jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0); 559 const char* base = UNICODE::as_utf8(position, length); 560 Symbol* sym = SymbolTable::lookup(base, length, THREAD); 561 return sym; 562 } 563 } 564 565 Symbol* java_lang_String::as_symbol_or_null(oop java_string) { 566 typeArrayOop value = java_lang_String::value(java_string); 567 int length = java_lang_String::length(java_string); 568 bool is_latin1 = java_lang_String::is_latin1(java_string); 569 if (!is_latin1) { 570 jchar* base = (length == 0) ? NULL : value->char_at_addr(0); 571 return SymbolTable::probe_unicode(base, length); 572 } else { 573 ResourceMark rm; 574 jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0); 575 const char* base = UNICODE::as_utf8(position, length); 576 return SymbolTable::probe(base, length); 577 } 578 } 579 580 int java_lang_String::utf8_length(oop java_string) { 581 typeArrayOop value = java_lang_String::value(java_string); 582 int length = java_lang_String::length(java_string); 583 bool is_latin1 = java_lang_String::is_latin1(java_string); 584 if (length == 0) { 585 return 0; 586 } 587 if (!is_latin1) { 588 return UNICODE::utf8_length(value->char_at_addr(0), length); 589 } else { 590 return UNICODE::utf8_length(value->byte_at_addr(0), length); 591 } 592 } 593 594 char* java_lang_String::as_utf8_string(oop java_string) { 595 typeArrayOop value = java_lang_String::value(java_string); 596 int length = java_lang_String::length(java_string); 597 bool is_latin1 = java_lang_String::is_latin1(java_string); 598 if (!is_latin1) { 599 jchar* position = (length == 0) ? NULL : value->char_at_addr(0); 600 return UNICODE::as_utf8(position, length); 601 } else { 602 jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0); 603 return UNICODE::as_utf8(position, length); 604 } 605 } 606 607 char* java_lang_String::as_utf8_string(oop java_string, char* buf, int buflen) { 608 typeArrayOop value = java_lang_String::value(java_string); 609 int length = java_lang_String::length(java_string); 610 bool is_latin1 = java_lang_String::is_latin1(java_string); 611 if (!is_latin1) { 612 jchar* position = (length == 0) ? NULL : value->char_at_addr(0); 613 return UNICODE::as_utf8(position, length, buf, buflen); 614 } else { 615 jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0); 616 return UNICODE::as_utf8(position, length, buf, buflen); 617 } 618 } 619 620 char* java_lang_String::as_utf8_string(oop java_string, int start, int len) { 621 typeArrayOop value = java_lang_String::value(java_string); 622 int length = java_lang_String::length(java_string); 623 assert(start + len <= length, "just checking"); 624 bool is_latin1 = java_lang_String::is_latin1(java_string); 625 if (!is_latin1) { 626 jchar* position = value->char_at_addr(start); 627 return UNICODE::as_utf8(position, len); 628 } else { 629 jbyte* position = value->byte_at_addr(start); 630 return UNICODE::as_utf8(position, len); 631 } 632 } 633 634 char* java_lang_String::as_utf8_string(oop java_string, int start, int len, char* buf, int buflen) { 635 typeArrayOop value = java_lang_String::value(java_string); 636 int length = java_lang_String::length(java_string); 637 assert(start + len <= length, "just checking"); 638 bool is_latin1 = java_lang_String::is_latin1(java_string); 639 if (!is_latin1) { 640 jchar* position = value->char_at_addr(start); 641 return UNICODE::as_utf8(position, len, buf, buflen); 642 } else { 643 jbyte* position = value->byte_at_addr(start); 644 return UNICODE::as_utf8(position, len, buf, buflen); 645 } 646 } 647 648 bool java_lang_String::equals(oop java_string, const jchar* chars, int len) { 649 assert(java_string->klass() == SystemDictionary::String_klass(), 650 "must be java_string"); 651 typeArrayOop value = java_lang_String::value_no_keepalive(java_string); 652 int length = java_lang_String::length(java_string); 653 if (length != len) { 654 return false; 655 } 656 bool is_latin1 = java_lang_String::is_latin1(java_string); 657 if (!is_latin1) { 658 for (int i = 0; i < len; i++) { 659 if (value->char_at(i) != chars[i]) { 660 return false; 661 } 662 } 663 } else { 664 for (int i = 0; i < len; i++) { 665 if ((((jchar) value->byte_at(i)) & 0xff) != chars[i]) { 666 return false; 667 } 668 } 669 } 670 return true; 671 } 672 673 bool java_lang_String::equals(oop str1, oop str2) { 674 assert(str1->klass() == SystemDictionary::String_klass(), 675 "must be java String"); 676 assert(str2->klass() == SystemDictionary::String_klass(), 677 "must be java String"); 678 typeArrayOop value1 = java_lang_String::value_no_keepalive(str1); 679 int length1 = java_lang_String::length(str1); 680 bool is_latin1 = java_lang_String::is_latin1(str1); 681 typeArrayOop value2 = java_lang_String::value_no_keepalive(str2); 682 int length2 = java_lang_String::length(str2); 683 bool is_latin2 = java_lang_String::is_latin1(str2); 684 685 if ((length1 != length2) || (is_latin1 != is_latin2)) { 686 // Strings of different size or with different 687 // coders are never equal. 688 return false; 689 } 690 int blength1 = value1->length(); 691 for (int i = 0; i < blength1; i++) { 692 if (value1->byte_at(i) != value2->byte_at(i)) { 693 return false; 694 } 695 } 696 return true; 697 } 698 699 void java_lang_String::print(oop java_string, outputStream* st) { 700 assert(java_string->klass() == SystemDictionary::String_klass(), "must be java_string"); 701 typeArrayOop value = java_lang_String::value_no_keepalive(java_string); 702 703 if (value == NULL) { 704 // This can happen if, e.g., printing a String 705 // object before its initializer has been called 706 st->print("NULL"); 707 return; 708 } 709 710 int length = java_lang_String::length(java_string); 711 bool is_latin1 = java_lang_String::is_latin1(java_string); 712 713 st->print("\""); 714 for (int index = 0; index < length; index++) { 715 st->print("%c", (!is_latin1) ? value->char_at(index) : 716 ((jchar) value->byte_at(index)) & 0xff ); 717 } 718 st->print("\""); 719 } 720 721 722 static void initialize_static_field(fieldDescriptor* fd, Handle mirror, TRAPS) { 723 assert(mirror.not_null() && fd->is_static(), "just checking"); 724 if (fd->has_initial_value()) { 725 BasicType t = fd->field_type(); 726 switch (t) { 727 case T_BYTE: 728 mirror()->byte_field_put(fd->offset(), fd->int_initial_value()); 729 break; 730 case T_BOOLEAN: 731 mirror()->bool_field_put(fd->offset(), fd->int_initial_value()); 732 break; 733 case T_CHAR: 734 mirror()->char_field_put(fd->offset(), fd->int_initial_value()); 735 break; 736 case T_SHORT: 737 mirror()->short_field_put(fd->offset(), fd->int_initial_value()); 738 break; 739 case T_INT: 740 mirror()->int_field_put(fd->offset(), fd->int_initial_value()); 741 break; 742 case T_FLOAT: 743 mirror()->float_field_put(fd->offset(), fd->float_initial_value()); 744 break; 745 case T_DOUBLE: 746 mirror()->double_field_put(fd->offset(), fd->double_initial_value()); 747 break; 748 case T_LONG: 749 mirror()->long_field_put(fd->offset(), fd->long_initial_value()); 750 break; 751 case T_OBJECT: 752 { 753 assert(fd->signature() == vmSymbols::string_signature(), 754 "just checking"); 755 if (DumpSharedSpaces && HeapShared::is_archived_object(mirror())) { 756 // Archive the String field and update the pointer. 757 oop s = mirror()->obj_field(fd->offset()); 758 oop archived_s = StringTable::create_archived_string(s, CHECK); 759 mirror()->obj_field_put(fd->offset(), archived_s); 760 } else { 761 oop string = fd->string_initial_value(CHECK); 762 mirror()->obj_field_put(fd->offset(), string); 763 } 764 } 765 break; 766 default: 767 THROW_MSG(vmSymbols::java_lang_ClassFormatError(), 768 "Illegal ConstantValue attribute in class file"); 769 } 770 } 771 } 772 773 774 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) { 775 assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already"); 776 777 // If the offset was read from the shared archive, it was fixed up already 778 if (!k->is_shared()) { 779 if (k->is_instance_klass()) { 780 // During bootstrap, java.lang.Class wasn't loaded so static field 781 // offsets were computed without the size added it. Go back and 782 // update all the static field offsets to included the size. 783 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) { 784 if (fs.access_flags().is_static()) { 785 int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields(); 786 fs.set_offset(real_offset); 787 } 788 } 789 } 790 } 791 792 if (k->is_shared() && k->has_raw_archived_mirror()) { 793 if (HeapShared::open_archive_heap_region_mapped()) { 794 bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK); 795 assert(present, "Missing archived mirror for %s", k->external_name()); 796 return; 797 } else { 798 k->set_java_mirror_handle(NULL); 799 k->clear_has_raw_archived_mirror(); 800 } 801 } 802 create_mirror(k, Handle(), Handle(), Handle(), CHECK); 803 } 804 805 void java_lang_Class::initialize_mirror_fields(Klass* k, 806 Handle mirror, 807 Handle protection_domain, 808 TRAPS) { 809 // Allocate a simple java object for a lock. 810 // This needs to be a java object because during class initialization 811 // it can be held across a java call. 812 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK); 813 set_init_lock(mirror(), r); 814 815 // Set protection domain also 816 set_protection_domain(mirror(), protection_domain()); 817 818 // Initialize static fields 819 InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK); 820 } 821 822 // Set the java.lang.Module module field in the java_lang_Class mirror 823 void java_lang_Class::set_mirror_module_field(Klass* k, Handle mirror, Handle module, TRAPS) { 824 if (module.is_null()) { 825 // During startup, the module may be NULL only if java.base has not been defined yet. 826 // Put the class on the fixup_module_list to patch later when the java.lang.Module 827 // for java.base is known. 828 assert(!Universe::is_module_initialized(), "Incorrect java.lang.Module pre module system initialization"); 829 830 bool javabase_was_defined = false; 831 { 832 MutexLocker m1(Module_lock, THREAD); 833 // Keep list of classes needing java.base module fixup 834 if (!ModuleEntryTable::javabase_defined()) { 835 assert(k->java_mirror() != NULL, "Class's mirror is null"); 836 k->class_loader_data()->inc_keep_alive(); 837 assert(fixup_module_field_list() != NULL, "fixup_module_field_list not initialized"); 838 fixup_module_field_list()->push(k); 839 } else { 840 javabase_was_defined = true; 841 } 842 } 843 844 // If java.base was already defined then patch this particular class with java.base. 845 if (javabase_was_defined) { 846 ModuleEntry *javabase_entry = ModuleEntryTable::javabase_moduleEntry(); 847 assert(javabase_entry != NULL && javabase_entry->module() != NULL, 848 "Setting class module field, " JAVA_BASE_NAME " should be defined"); 849 Handle javabase_handle(THREAD, javabase_entry->module()); 850 set_module(mirror(), javabase_handle()); 851 } 852 } else { 853 assert(Universe::is_module_initialized() || 854 (ModuleEntryTable::javabase_defined() && 855 (oopDesc::equals(module(), ModuleEntryTable::javabase_moduleEntry()->module()))), 856 "Incorrect java.lang.Module specification while creating mirror"); 857 set_module(mirror(), module()); 858 } 859 } 860 861 // Statically allocate fixup lists because they always get created. 862 void java_lang_Class::allocate_fixup_lists() { 863 GrowableArray<Klass*>* mirror_list = 864 new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true); 865 set_fixup_mirror_list(mirror_list); 866 867 GrowableArray<Klass*>* module_list = 868 new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, true); 869 set_fixup_module_field_list(module_list); 870 } 871 872 void java_lang_Class::create_mirror(Klass* k, Handle class_loader, 873 Handle module, Handle protection_domain, TRAPS) { 874 assert(k != NULL, "Use create_basic_type_mirror for primitive types"); 875 assert(k->java_mirror() == NULL, "should only assign mirror once"); 876 877 // Use this moment of initialization to cache modifier_flags also, 878 // to support Class.getModifiers(). Instance classes recalculate 879 // the cached flags after the class file is parsed, but before the 880 // class is put into the system dictionary. 881 int computed_modifiers = k->compute_modifier_flags(CHECK); 882 k->set_modifier_flags(computed_modifiers); 883 // Class_klass has to be loaded because it is used to allocate 884 // the mirror. 885 if (SystemDictionary::Class_klass_loaded()) { 886 // Allocate mirror (java.lang.Class instance) 887 oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK); 888 Handle mirror(THREAD, mirror_oop); 889 Handle comp_mirror; 890 891 // Setup indirection from mirror->klass 892 java_lang_Class::set_klass(mirror(), k); 893 894 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass()); 895 assert(oop_size(mirror()) == mk->instance_size(k), "should have been set"); 896 897 java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror())); 898 899 // It might also have a component mirror. This mirror must already exist. 900 if (k->is_array_klass()) { 901 if (k->is_typeArray_klass()) { 902 BasicType type = TypeArrayKlass::cast(k)->element_type(); 903 comp_mirror = Handle(THREAD, Universe::java_mirror(type)); 904 } else { 905 assert(k->is_objArray_klass(), "Must be"); 906 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass(); 907 assert(element_klass != NULL, "Must have an element klass"); 908 comp_mirror = Handle(THREAD, element_klass->java_mirror()); 909 } 910 assert(comp_mirror() != NULL, "must have a mirror"); 911 912 // Two-way link between the array klass and its component mirror: 913 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k 914 set_component_mirror(mirror(), comp_mirror()); 915 // See below for ordering dependencies between field array_klass in component mirror 916 // and java_mirror in this klass. 917 } else { 918 assert(k->is_instance_klass(), "Must be"); 919 920 initialize_mirror_fields(k, mirror, protection_domain, THREAD); 921 if (HAS_PENDING_EXCEPTION) { 922 // If any of the fields throws an exception like OOM remove the klass field 923 // from the mirror so GC doesn't follow it after the klass has been deallocated. 924 // This mirror looks like a primitive type, which logically it is because it 925 // it represents no class. 926 java_lang_Class::set_klass(mirror(), NULL); 927 return; 928 } 929 } 930 931 // set the classLoader field in the java_lang_Class instance 932 assert(oopDesc::equals(class_loader(), k->class_loader()), "should be same"); 933 set_class_loader(mirror(), class_loader()); 934 935 // Setup indirection from klass->mirror 936 // after any exceptions can happen during allocations. 937 k->set_java_mirror(mirror); 938 939 // Set the module field in the java_lang_Class instance. This must be done 940 // after the mirror is set. 941 set_mirror_module_field(k, mirror, module, THREAD); 942 943 if (comp_mirror() != NULL) { 944 // Set after k->java_mirror() is published, because compiled code running 945 // concurrently doesn't expect a k to have a null java_mirror. 946 release_set_array_klass(comp_mirror(), k); 947 } 948 } else { 949 assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized"); 950 fixup_mirror_list()->push(k); 951 } 952 } 953 954 #if INCLUDE_CDS_JAVA_HEAP 955 // Clears mirror fields. Static final fields with initial values are reloaded 956 // from constant pool. The object identity hash is in the object header and is 957 // not affected. 958 class ResetMirrorField: public FieldClosure { 959 private: 960 Handle _m; 961 962 public: 963 ResetMirrorField(Handle mirror) : _m(mirror) {} 964 965 void do_field(fieldDescriptor* fd) { 966 assert(DumpSharedSpaces, "dump time only"); 967 assert(_m.not_null(), "Mirror cannot be NULL"); 968 969 if (fd->is_static() && fd->has_initial_value()) { 970 initialize_static_field(fd, _m, Thread::current()); 971 return; 972 } 973 974 BasicType ft = fd->field_type(); 975 switch (ft) { 976 case T_BYTE: 977 _m()->byte_field_put(fd->offset(), 0); 978 break; 979 case T_CHAR: 980 _m()->char_field_put(fd->offset(), 0); 981 break; 982 case T_DOUBLE: 983 _m()->double_field_put(fd->offset(), 0); 984 break; 985 case T_FLOAT: 986 _m()->float_field_put(fd->offset(), 0); 987 break; 988 case T_INT: 989 _m()->int_field_put(fd->offset(), 0); 990 break; 991 case T_LONG: 992 _m()->long_field_put(fd->offset(), 0); 993 break; 994 case T_SHORT: 995 _m()->short_field_put(fd->offset(), 0); 996 break; 997 case T_BOOLEAN: 998 _m()->bool_field_put(fd->offset(), false); 999 break; 1000 case T_ARRAY: 1001 case T_OBJECT: { 1002 // It might be useful to cache the String field, but 1003 // for now just clear out any reference field 1004 oop o = _m()->obj_field(fd->offset()); 1005 _m()->obj_field_put(fd->offset(), NULL); 1006 break; 1007 } 1008 default: 1009 ShouldNotReachHere(); 1010 break; 1011 } 1012 } 1013 }; 1014 1015 void java_lang_Class::archive_basic_type_mirrors(TRAPS) { 1016 assert(HeapShared::is_heap_object_archiving_allowed(), 1017 "HeapShared::is_heap_object_archiving_allowed() must be true"); 1018 1019 for (int t = 0; t <= T_VOID; t++) { 1020 oop m = Universe::_mirrors[t]; 1021 if (m != NULL) { 1022 // Update the field at _array_klass_offset to point to the relocated array klass. 1023 oop archived_m = HeapShared::archive_heap_object(m, THREAD); 1024 assert(archived_m != NULL, "sanity"); 1025 Klass *ak = (Klass*)(archived_m->metadata_field(_array_klass_offset)); 1026 assert(ak != NULL || t == T_VOID, "should not be NULL"); 1027 if (ak != NULL) { 1028 Klass *reloc_ak = MetaspaceShared::get_relocated_klass(ak); 1029 archived_m->metadata_field_put(_array_klass_offset, reloc_ak); 1030 } 1031 1032 // Clear the fields. Just to be safe 1033 Klass *k = m->klass(); 1034 Handle archived_mirror_h(THREAD, archived_m); 1035 ResetMirrorField reset(archived_mirror_h); 1036 InstanceKlass::cast(k)->do_nonstatic_fields(&reset); 1037 1038 log_trace(cds, heap, mirror)( 1039 "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT, 1040 type2name((BasicType)t), p2i(Universe::_mirrors[t]), p2i(archived_m)); 1041 1042 Universe::_mirrors[t] = archived_m; 1043 } 1044 } 1045 1046 assert(Universe::_mirrors[T_INT] != NULL && 1047 Universe::_mirrors[T_FLOAT] != NULL && 1048 Universe::_mirrors[T_DOUBLE] != NULL && 1049 Universe::_mirrors[T_BYTE] != NULL && 1050 Universe::_mirrors[T_BOOLEAN] != NULL && 1051 Universe::_mirrors[T_CHAR] != NULL && 1052 Universe::_mirrors[T_LONG] != NULL && 1053 Universe::_mirrors[T_SHORT] != NULL && 1054 Universe::_mirrors[T_VOID] != NULL, "sanity"); 1055 1056 Universe::set_int_mirror(Universe::_mirrors[T_INT]); 1057 Universe::set_float_mirror(Universe::_mirrors[T_FLOAT]); 1058 Universe::set_double_mirror(Universe::_mirrors[T_DOUBLE]); 1059 Universe::set_byte_mirror(Universe::_mirrors[T_BYTE]); 1060 Universe::set_bool_mirror(Universe::_mirrors[T_BOOLEAN]); 1061 Universe::set_char_mirror(Universe::_mirrors[T_CHAR]); 1062 Universe::set_long_mirror(Universe::_mirrors[T_LONG]); 1063 Universe::set_short_mirror(Universe::_mirrors[T_SHORT]); 1064 Universe::set_void_mirror(Universe::_mirrors[T_VOID]); 1065 } 1066 1067 // 1068 // After the mirror object is successfully archived, the archived 1069 // klass is set with _has_archived_raw_mirror flag. 1070 // 1071 // The _has_archived_raw_mirror flag is cleared at runtime when the 1072 // archived mirror is restored. If archived java heap data cannot 1073 // be used at runtime, new mirror object is created for the shared 1074 // class. The _has_archived_raw_mirror is cleared also during the process. 1075 oop java_lang_Class::archive_mirror(Klass* k, TRAPS) { 1076 assert(HeapShared::is_heap_object_archiving_allowed(), 1077 "HeapShared::is_heap_object_archiving_allowed() must be true"); 1078 1079 // Mirror is already archived 1080 if (k->has_raw_archived_mirror()) { 1081 assert(k->archived_java_mirror_raw() != NULL, "no archived mirror"); 1082 return k->archived_java_mirror_raw(); 1083 } 1084 1085 // No mirror 1086 oop mirror = k->java_mirror(); 1087 if (mirror == NULL) { 1088 return NULL; 1089 } 1090 1091 if (k->is_instance_klass()) { 1092 InstanceKlass *ik = InstanceKlass::cast(k); 1093 assert(ik->signers() == NULL, "class with signer should have been excluded"); 1094 1095 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() || 1096 ik->is_shared_app_class())) { 1097 // Archiving mirror for classes from non-builtin loaders is not 1098 // supported. Clear the _java_mirror within the archived class. 1099 k->set_java_mirror_handle(NULL); 1100 return NULL; 1101 } 1102 } 1103 1104 // Now start archiving the mirror object 1105 oop archived_mirror = HeapShared::archive_heap_object(mirror, THREAD); 1106 if (archived_mirror == NULL) { 1107 return NULL; 1108 } 1109 1110 archived_mirror = process_archived_mirror(k, mirror, archived_mirror, THREAD); 1111 if (archived_mirror == NULL) { 1112 return NULL; 1113 } 1114 1115 k->set_archived_java_mirror_raw(archived_mirror); 1116 1117 k->set_has_raw_archived_mirror(); 1118 1119 ResourceMark rm; 1120 log_trace(cds, heap, mirror)( 1121 "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT, 1122 k->external_name(), p2i(mirror), p2i(archived_mirror)); 1123 1124 return archived_mirror; 1125 } 1126 1127 // The process is based on create_mirror(). 1128 oop java_lang_Class::process_archived_mirror(Klass* k, oop mirror, 1129 oop archived_mirror, 1130 Thread *THREAD) { 1131 // Clear nonstatic fields in archived mirror. Some of the fields will be set 1132 // to archived metadata and objects below. 1133 Klass *c = archived_mirror->klass(); 1134 Handle archived_mirror_h(THREAD, archived_mirror); 1135 ResetMirrorField reset(archived_mirror_h); 1136 InstanceKlass::cast(c)->do_nonstatic_fields(&reset); 1137 1138 if (k->is_array_klass()) { 1139 oop archived_comp_mirror; 1140 if (k->is_typeArray_klass()) { 1141 // The primitive type mirrors are already archived. Get the archived mirror. 1142 oop comp_mirror = java_lang_Class::component_mirror(mirror); 1143 archived_comp_mirror = HeapShared::find_archived_heap_object(comp_mirror); 1144 assert(archived_comp_mirror != NULL, "Must be"); 1145 } else { 1146 assert(k->is_objArray_klass(), "Must be"); 1147 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass(); 1148 assert(element_klass != NULL, "Must have an element klass"); 1149 archived_comp_mirror = archive_mirror(element_klass, THREAD); 1150 if (archived_comp_mirror == NULL) { 1151 return NULL; 1152 } 1153 } 1154 java_lang_Class::set_component_mirror(archived_mirror, archived_comp_mirror); 1155 } else { 1156 assert(k->is_instance_klass(), "Must be"); 1157 1158 // Reset local static fields in the mirror 1159 InstanceKlass::cast(k)->do_local_static_fields(&reset); 1160 1161 java_lang_Class:set_init_lock(archived_mirror, NULL); 1162 1163 set_protection_domain(archived_mirror, NULL); 1164 } 1165 1166 // clear class loader and mirror_module_field 1167 set_class_loader(archived_mirror, NULL); 1168 set_module(archived_mirror, NULL); 1169 1170 // The archived mirror's field at _klass_offset is still pointing to the original 1171 // klass. Updated the field in the archived mirror to point to the relocated 1172 // klass in the archive. 1173 Klass *reloc_k = MetaspaceShared::get_relocated_klass(as_Klass(mirror)); 1174 log_debug(cds, heap, mirror)( 1175 "Relocate mirror metadata field at _klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT, 1176 p2i(as_Klass(mirror)), p2i(reloc_k)); 1177 archived_mirror->metadata_field_put(_klass_offset, reloc_k); 1178 1179 // The field at _array_klass_offset is pointing to the original one dimension 1180 // higher array klass if exists. Relocate the pointer. 1181 Klass *arr = array_klass_acquire(mirror); 1182 if (arr != NULL) { 1183 Klass *reloc_arr = MetaspaceShared::get_relocated_klass(arr); 1184 log_debug(cds, heap, mirror)( 1185 "Relocate mirror metadata field at _array_klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT, 1186 p2i(arr), p2i(reloc_arr)); 1187 archived_mirror->metadata_field_put(_array_klass_offset, reloc_arr); 1188 } 1189 return archived_mirror; 1190 } 1191 1192 // Returns true if the mirror is updated, false if no archived mirror 1193 // data is present. After the archived mirror object is restored, the 1194 // shared klass' _has_raw_archived_mirror flag is cleared. 1195 bool java_lang_Class::restore_archived_mirror(Klass *k, 1196 Handle class_loader, Handle module, 1197 Handle protection_domain, TRAPS) { 1198 // Postpone restoring archived mirror until java.lang.Class is loaded. Please 1199 // see more details in SystemDictionary::resolve_well_known_classes(). 1200 if (!SystemDictionary::Class_klass_loaded()) { 1201 assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized"); 1202 fixup_mirror_list()->push(k); 1203 return true; 1204 } 1205 1206 oop m = HeapShared::materialize_archived_object(k->archived_java_mirror_raw_narrow()); 1207 1208 if (m == NULL) { 1209 return false; 1210 } 1211 1212 log_debug(cds, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m)); 1213 1214 // mirror is archived, restore 1215 assert(HeapShared::is_archived_object(m), "must be archived mirror object"); 1216 Handle mirror(THREAD, m); 1217 1218 if (!k->is_array_klass()) { 1219 // - local static final fields with initial values were initialized at dump time 1220 1221 // create the init_lock 1222 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false)); 1223 set_init_lock(mirror(), r); 1224 1225 if (protection_domain.not_null()) { 1226 set_protection_domain(mirror(), protection_domain()); 1227 } 1228 } 1229 1230 assert(class_loader() == k->class_loader(), "should be same"); 1231 if (class_loader.not_null()) { 1232 set_class_loader(mirror(), class_loader()); 1233 } 1234 1235 k->set_java_mirror(mirror); 1236 k->clear_has_raw_archived_mirror(); 1237 1238 set_mirror_module_field(k, mirror, module, THREAD); 1239 1240 ResourceMark rm; 1241 log_trace(cds, heap, mirror)( 1242 "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror())); 1243 1244 return true; 1245 } 1246 #endif // INCLUDE_CDS_JAVA_HEAP 1247 1248 void java_lang_Class::fixup_module_field(Klass* k, Handle module) { 1249 assert(_module_offset != 0, "must have been computed already"); 1250 java_lang_Class::set_module(k->java_mirror(), module()); 1251 } 1252 1253 int java_lang_Class::oop_size(oop java_class) { 1254 assert(_oop_size_offset != 0, "must be set"); 1255 int size = java_class->int_field(_oop_size_offset); 1256 assert(size > 0, "Oop size must be greater than zero, not %d", size); 1257 return size; 1258 } 1259 1260 int java_lang_Class::oop_size_raw(oop java_class) { 1261 assert(_oop_size_offset != 0, "must be set"); 1262 int size = java_class->int_field_raw(_oop_size_offset); 1263 assert(size > 0, "Oop size must be greater than zero, not %d", size); 1264 return size; 1265 } 1266 1267 void java_lang_Class::set_oop_size(HeapWord* java_class, int size) { 1268 assert(_oop_size_offset != 0, "must be set"); 1269 assert(size > 0, "Oop size must be greater than zero, not %d", size); 1270 *(int*)(((char*)java_class) + _oop_size_offset) = size; 1271 } 1272 1273 int java_lang_Class::static_oop_field_count(oop java_class) { 1274 assert(_static_oop_field_count_offset != 0, "must be set"); 1275 return java_class->int_field(_static_oop_field_count_offset); 1276 } 1277 1278 int java_lang_Class::static_oop_field_count_raw(oop java_class) { 1279 assert(_static_oop_field_count_offset != 0, "must be set"); 1280 return java_class->int_field_raw(_static_oop_field_count_offset); 1281 } 1282 1283 void java_lang_Class::set_static_oop_field_count(oop java_class, int size) { 1284 assert(_static_oop_field_count_offset != 0, "must be set"); 1285 java_class->int_field_put(_static_oop_field_count_offset, size); 1286 } 1287 1288 oop java_lang_Class::protection_domain(oop java_class) { 1289 assert(_protection_domain_offset != 0, "must be set"); 1290 return java_class->obj_field(_protection_domain_offset); 1291 } 1292 void java_lang_Class::set_protection_domain(oop java_class, oop pd) { 1293 assert(_protection_domain_offset != 0, "must be set"); 1294 java_class->obj_field_put(_protection_domain_offset, pd); 1295 } 1296 1297 void java_lang_Class::set_component_mirror(oop java_class, oop comp_mirror) { 1298 assert(_component_mirror_offset != 0, "must be set"); 1299 java_class->obj_field_put(_component_mirror_offset, comp_mirror); 1300 } 1301 oop java_lang_Class::component_mirror(oop java_class) { 1302 assert(_component_mirror_offset != 0, "must be set"); 1303 return java_class->obj_field(_component_mirror_offset); 1304 } 1305 1306 oop java_lang_Class::init_lock(oop java_class) { 1307 assert(_init_lock_offset != 0, "must be set"); 1308 return java_class->obj_field(_init_lock_offset); 1309 } 1310 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) { 1311 assert(_init_lock_offset != 0, "must be set"); 1312 java_class->obj_field_put(_init_lock_offset, init_lock); 1313 } 1314 1315 objArrayOop java_lang_Class::signers(oop java_class) { 1316 assert(_signers_offset != 0, "must be set"); 1317 return (objArrayOop)java_class->obj_field(_signers_offset); 1318 } 1319 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) { 1320 assert(_signers_offset != 0, "must be set"); 1321 java_class->obj_field_put(_signers_offset, (oop)signers); 1322 } 1323 1324 1325 void java_lang_Class::set_class_loader(oop java_class, oop loader) { 1326 // jdk7 runs Queens in bootstrapping and jdk8-9 has no coordinated pushes yet. 1327 if (_class_loader_offset != 0) { 1328 java_class->obj_field_put(_class_loader_offset, loader); 1329 } 1330 } 1331 1332 oop java_lang_Class::class_loader(oop java_class) { 1333 assert(_class_loader_offset != 0, "must be set"); 1334 return java_class->obj_field(_class_loader_offset); 1335 } 1336 1337 oop java_lang_Class::module(oop java_class) { 1338 assert(_module_offset != 0, "must be set"); 1339 return java_class->obj_field(_module_offset); 1340 } 1341 1342 void java_lang_Class::set_module(oop java_class, oop module) { 1343 assert(_module_offset != 0, "must be set"); 1344 java_class->obj_field_put(_module_offset, module); 1345 } 1346 1347 oop java_lang_Class::name(Handle java_class, TRAPS) { 1348 assert(_name_offset != 0, "must be set"); 1349 oop o = java_class->obj_field(_name_offset); 1350 if (o == NULL) { 1351 o = StringTable::intern(java_lang_Class::as_external_name(java_class()), THREAD); 1352 java_class->obj_field_put(_name_offset, o); 1353 } 1354 return o; 1355 } 1356 1357 oop java_lang_Class::source_file(oop java_class) { 1358 assert(_source_file_offset != 0, "must be set"); 1359 return java_class->obj_field(_source_file_offset); 1360 } 1361 1362 void java_lang_Class::set_source_file(oop java_class, oop source_file) { 1363 assert(_source_file_offset != 0, "must be set"); 1364 java_class->obj_field_put(_source_file_offset, source_file); 1365 } 1366 1367 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) { 1368 // This should be improved by adding a field at the Java level or by 1369 // introducing a new VM klass (see comment in ClassFileParser) 1370 oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0); 1371 if (type != T_VOID) { 1372 Klass* aklass = Universe::typeArrayKlassObj(type); 1373 assert(aklass != NULL, "correct bootstrap"); 1374 release_set_array_klass(java_class, aklass); 1375 } 1376 #ifdef ASSERT 1377 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass()); 1378 assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation"); 1379 #endif 1380 return java_class; 1381 } 1382 1383 1384 Klass* java_lang_Class::as_Klass(oop java_class) { 1385 //%note memory_2 1386 assert(java_lang_Class::is_instance(java_class), "must be a Class object"); 1387 Klass* k = ((Klass*)java_class->metadata_field(_klass_offset)); 1388 assert(k == NULL || k->is_klass(), "type check"); 1389 return k; 1390 } 1391 1392 Klass* java_lang_Class::as_Klass_raw(oop java_class) { 1393 //%note memory_2 1394 assert(java_lang_Class::is_instance(java_class), "must be a Class object"); 1395 Klass* k = ((Klass*)java_class->metadata_field_raw(_klass_offset)); 1396 assert(k == NULL || k->is_klass(), "type check"); 1397 return k; 1398 } 1399 1400 1401 void java_lang_Class::set_klass(oop java_class, Klass* klass) { 1402 assert(java_lang_Class::is_instance(java_class), "must be a Class object"); 1403 java_class->metadata_field_put(_klass_offset, klass); 1404 } 1405 1406 1407 void java_lang_Class::print_signature(oop java_class, outputStream* st) { 1408 assert(java_lang_Class::is_instance(java_class), "must be a Class object"); 1409 Symbol* name = NULL; 1410 bool is_instance = false; 1411 if (is_primitive(java_class)) { 1412 name = vmSymbols::type_signature(primitive_type(java_class)); 1413 } else { 1414 Klass* k = as_Klass(java_class); 1415 is_instance = k->is_instance_klass(); 1416 name = k->name(); 1417 } 1418 if (name == NULL) { 1419 st->print("<null>"); 1420 return; 1421 } 1422 if (is_instance) st->print("L"); 1423 st->write((char*) name->base(), (int) name->utf8_length()); 1424 if (is_instance) st->print(";"); 1425 } 1426 1427 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) { 1428 assert(java_lang_Class::is_instance(java_class), "must be a Class object"); 1429 Symbol* name; 1430 if (is_primitive(java_class)) { 1431 name = vmSymbols::type_signature(primitive_type(java_class)); 1432 // Because this can create a new symbol, the caller has to decrement 1433 // the refcount, so make adjustment here and below for symbols returned 1434 // that are not created or incremented due to a successful lookup. 1435 name->increment_refcount(); 1436 } else { 1437 Klass* k = as_Klass(java_class); 1438 if (!k->is_instance_klass()) { 1439 name = k->name(); 1440 name->increment_refcount(); 1441 } else { 1442 ResourceMark rm; 1443 const char* sigstr = k->signature_name(); 1444 int siglen = (int) strlen(sigstr); 1445 if (!intern_if_not_found) { 1446 name = SymbolTable::probe(sigstr, siglen); 1447 } else { 1448 name = SymbolTable::new_symbol(sigstr, siglen, THREAD); 1449 } 1450 } 1451 } 1452 return name; 1453 } 1454 1455 // Returns the Java name for this Java mirror (Resource allocated) 1456 // See Klass::external_name(). 1457 // For primitive type Java mirrors, its type name is returned. 1458 const char* java_lang_Class::as_external_name(oop java_class) { 1459 assert(java_lang_Class::is_instance(java_class), "must be a Class object"); 1460 const char* name = NULL; 1461 if (is_primitive(java_class)) { 1462 name = type2name(primitive_type(java_class)); 1463 } else { 1464 name = as_Klass(java_class)->external_name(); 1465 } 1466 if (name == NULL) { 1467 name = "<null>"; 1468 } 1469 return name; 1470 } 1471 1472 Klass* java_lang_Class::array_klass_acquire(oop java_class) { 1473 Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset)); 1474 assert(k == NULL || k->is_klass() && k->is_array_klass(), "should be array klass"); 1475 return k; 1476 } 1477 1478 1479 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) { 1480 assert(klass->is_klass() && klass->is_array_klass(), "should be array klass"); 1481 java_class->release_metadata_field_put(_array_klass_offset, klass); 1482 } 1483 1484 1485 BasicType java_lang_Class::primitive_type(oop java_class) { 1486 assert(java_lang_Class::is_primitive(java_class), "just checking"); 1487 Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset)); 1488 BasicType type = T_VOID; 1489 if (ak != NULL) { 1490 // Note: create_basic_type_mirror above initializes ak to a non-null value. 1491 type = ArrayKlass::cast(ak)->element_type(); 1492 } else { 1493 assert(oopDesc::equals(java_class, Universe::void_mirror()), "only valid non-array primitive"); 1494 } 1495 assert(oopDesc::equals(Universe::java_mirror(type), java_class), "must be consistent"); 1496 return type; 1497 } 1498 1499 BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) { 1500 assert(java_lang_Class::is_instance(java_class), "must be a Class object"); 1501 if (is_primitive(java_class)) { 1502 if (reference_klass != NULL) 1503 (*reference_klass) = NULL; 1504 return primitive_type(java_class); 1505 } else { 1506 if (reference_klass != NULL) 1507 (*reference_klass) = as_Klass(java_class); 1508 return T_OBJECT; 1509 } 1510 } 1511 1512 1513 oop java_lang_Class::primitive_mirror(BasicType t) { 1514 oop mirror = Universe::java_mirror(t); 1515 assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class"); 1516 assert(java_lang_Class::is_primitive(mirror), "must be primitive"); 1517 return mirror; 1518 } 1519 1520 bool java_lang_Class::offsets_computed = false; 1521 int java_lang_Class::classRedefinedCount_offset = -1; 1522 1523 #define CLASS_FIELDS_DO(macro) \ 1524 macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false) ; \ 1525 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \ 1526 macro(_component_mirror_offset, k, "componentType", class_signature, false); \ 1527 macro(_module_offset, k, "module", module_signature, false); \ 1528 macro(_name_offset, k, "name", string_signature, false); \ 1529 1530 void java_lang_Class::compute_offsets() { 1531 if (offsets_computed) { 1532 return; 1533 } 1534 1535 offsets_computed = true; 1536 1537 InstanceKlass* k = SystemDictionary::Class_klass(); 1538 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1539 1540 // Init lock is a C union with component_mirror. Only instanceKlass mirrors have 1541 // init_lock and only ArrayKlass mirrors have component_mirror. Since both are oops 1542 // GC treats them the same. 1543 _init_lock_offset = _component_mirror_offset; 1544 1545 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 1546 } 1547 1548 #if INCLUDE_CDS 1549 void java_lang_Class::serialize_offsets(SerializeClosure* f) { 1550 f->do_u4((u4*)&offsets_computed); 1551 f->do_u4((u4*)&_init_lock_offset); 1552 1553 CLASS_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1554 1555 CLASS_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 1556 } 1557 #endif 1558 1559 int java_lang_Class::classRedefinedCount(oop the_class_mirror) { 1560 if (classRedefinedCount_offset == -1) { 1561 // If we don't have an offset for it then just return -1 as a marker. 1562 return -1; 1563 } 1564 1565 return the_class_mirror->int_field(classRedefinedCount_offset); 1566 } 1567 1568 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) { 1569 if (classRedefinedCount_offset == -1) { 1570 // If we don't have an offset for it then nothing to set. 1571 return; 1572 } 1573 1574 the_class_mirror->int_field_put(classRedefinedCount_offset, value); 1575 } 1576 1577 1578 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the 1579 // platform thread structure, and a eetop offset which was used for thread 1580 // local storage (and unused by the HotSpot VM). In JDK1.2 the two structures 1581 // merged, so in the HotSpot VM we just use the eetop field for the thread 1582 // instead of the privateInfo_offset. 1583 // 1584 // Note: The stackSize field is only present starting in 1.4. 1585 1586 int java_lang_Thread::_name_offset = 0; 1587 int java_lang_Thread::_group_offset = 0; 1588 int java_lang_Thread::_contextClassLoader_offset = 0; 1589 int java_lang_Thread::_inheritedAccessControlContext_offset = 0; 1590 int java_lang_Thread::_priority_offset = 0; 1591 int java_lang_Thread::_eetop_offset = 0; 1592 int java_lang_Thread::_daemon_offset = 0; 1593 int java_lang_Thread::_stillborn_offset = 0; 1594 int java_lang_Thread::_stackSize_offset = 0; 1595 int java_lang_Thread::_tid_offset = 0; 1596 int java_lang_Thread::_thread_status_offset = 0; 1597 int java_lang_Thread::_park_blocker_offset = 0; 1598 int java_lang_Thread::_park_event_offset = 0 ; 1599 1600 #define THREAD_FIELDS_DO(macro) \ 1601 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 1602 macro(_group_offset, k, vmSymbols::group_name(), threadgroup_signature, false); \ 1603 macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \ 1604 macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \ 1605 macro(_priority_offset, k, vmSymbols::priority_name(), int_signature, false); \ 1606 macro(_daemon_offset, k, vmSymbols::daemon_name(), bool_signature, false); \ 1607 macro(_eetop_offset, k, "eetop", long_signature, false); \ 1608 macro(_stillborn_offset, k, "stillborn", bool_signature, false); \ 1609 macro(_stackSize_offset, k, "stackSize", long_signature, false); \ 1610 macro(_tid_offset, k, "tid", long_signature, false); \ 1611 macro(_thread_status_offset, k, "threadStatus", int_signature, false); \ 1612 macro(_park_blocker_offset, k, "parkBlocker", object_signature, false); \ 1613 macro(_park_event_offset, k, "nativeParkEventPointer", long_signature, false) 1614 1615 void java_lang_Thread::compute_offsets() { 1616 assert(_group_offset == 0, "offsets should be initialized only once"); 1617 1618 InstanceKlass* k = SystemDictionary::Thread_klass(); 1619 THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1620 } 1621 1622 #if INCLUDE_CDS 1623 void java_lang_Thread::serialize_offsets(SerializeClosure* f) { 1624 THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1625 } 1626 #endif 1627 1628 JavaThread* java_lang_Thread::thread(oop java_thread) { 1629 return (JavaThread*)java_thread->address_field(_eetop_offset); 1630 } 1631 1632 1633 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) { 1634 java_thread->address_field_put(_eetop_offset, (address)thread); 1635 } 1636 1637 1638 oop java_lang_Thread::name(oop java_thread) { 1639 return java_thread->obj_field(_name_offset); 1640 } 1641 1642 1643 void java_lang_Thread::set_name(oop java_thread, oop name) { 1644 java_thread->obj_field_put(_name_offset, name); 1645 } 1646 1647 1648 ThreadPriority java_lang_Thread::priority(oop java_thread) { 1649 return (ThreadPriority)java_thread->int_field(_priority_offset); 1650 } 1651 1652 1653 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) { 1654 java_thread->int_field_put(_priority_offset, priority); 1655 } 1656 1657 1658 oop java_lang_Thread::threadGroup(oop java_thread) { 1659 return java_thread->obj_field(_group_offset); 1660 } 1661 1662 1663 bool java_lang_Thread::is_stillborn(oop java_thread) { 1664 return java_thread->bool_field(_stillborn_offset) != 0; 1665 } 1666 1667 1668 // We never have reason to turn the stillborn bit off 1669 void java_lang_Thread::set_stillborn(oop java_thread) { 1670 java_thread->bool_field_put(_stillborn_offset, true); 1671 } 1672 1673 1674 bool java_lang_Thread::is_alive(oop java_thread) { 1675 JavaThread* thr = java_lang_Thread::thread(java_thread); 1676 return (thr != NULL); 1677 } 1678 1679 1680 bool java_lang_Thread::is_daemon(oop java_thread) { 1681 return java_thread->bool_field(_daemon_offset) != 0; 1682 } 1683 1684 1685 void java_lang_Thread::set_daemon(oop java_thread) { 1686 java_thread->bool_field_put(_daemon_offset, true); 1687 } 1688 1689 oop java_lang_Thread::context_class_loader(oop java_thread) { 1690 return java_thread->obj_field(_contextClassLoader_offset); 1691 } 1692 1693 oop java_lang_Thread::inherited_access_control_context(oop java_thread) { 1694 return java_thread->obj_field(_inheritedAccessControlContext_offset); 1695 } 1696 1697 1698 jlong java_lang_Thread::stackSize(oop java_thread) { 1699 if (_stackSize_offset > 0) { 1700 return java_thread->long_field(_stackSize_offset); 1701 } else { 1702 return 0; 1703 } 1704 } 1705 1706 // Write the thread status value to threadStatus field in java.lang.Thread java class. 1707 void java_lang_Thread::set_thread_status(oop java_thread, 1708 java_lang_Thread::ThreadStatus status) { 1709 // The threadStatus is only present starting in 1.5 1710 if (_thread_status_offset > 0) { 1711 java_thread->int_field_put(_thread_status_offset, status); 1712 } 1713 } 1714 1715 // Read thread status value from threadStatus field in java.lang.Thread java class. 1716 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) { 1717 // Make sure the caller is operating on behalf of the VM or is 1718 // running VM code (state == _thread_in_vm). 1719 assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() || 1720 JavaThread::current()->thread_state() == _thread_in_vm, 1721 "Java Thread is not running in vm"); 1722 // The threadStatus is only present starting in 1.5 1723 if (_thread_status_offset > 0) { 1724 return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset); 1725 } else { 1726 // All we can easily figure out is if it is alive, but that is 1727 // enough info for a valid unknown status. 1728 // These aren't restricted to valid set ThreadStatus values, so 1729 // use JVMTI values and cast. 1730 JavaThread* thr = java_lang_Thread::thread(java_thread); 1731 if (thr == NULL) { 1732 // the thread hasn't run yet or is in the process of exiting 1733 return NEW; 1734 } 1735 return (java_lang_Thread::ThreadStatus)JVMTI_THREAD_STATE_ALIVE; 1736 } 1737 } 1738 1739 1740 jlong java_lang_Thread::thread_id(oop java_thread) { 1741 // The thread ID field is only present starting in 1.5 1742 if (_tid_offset > 0) { 1743 return java_thread->long_field(_tid_offset); 1744 } else { 1745 return 0; 1746 } 1747 } 1748 1749 oop java_lang_Thread::park_blocker(oop java_thread) { 1750 assert(JDK_Version::current().supports_thread_park_blocker() && 1751 _park_blocker_offset != 0, "Must support parkBlocker field"); 1752 1753 if (_park_blocker_offset > 0) { 1754 return java_thread->obj_field(_park_blocker_offset); 1755 } 1756 1757 return NULL; 1758 } 1759 1760 jlong java_lang_Thread::park_event(oop java_thread) { 1761 if (_park_event_offset > 0) { 1762 return java_thread->long_field(_park_event_offset); 1763 } 1764 return 0; 1765 } 1766 1767 bool java_lang_Thread::set_park_event(oop java_thread, jlong ptr) { 1768 if (_park_event_offset > 0) { 1769 java_thread->long_field_put(_park_event_offset, ptr); 1770 return true; 1771 } 1772 return false; 1773 } 1774 1775 1776 const char* java_lang_Thread::thread_status_name(oop java_thread) { 1777 assert(_thread_status_offset != 0, "Must have thread status"); 1778 ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset); 1779 switch (status) { 1780 case NEW : return "NEW"; 1781 case RUNNABLE : return "RUNNABLE"; 1782 case SLEEPING : return "TIMED_WAITING (sleeping)"; 1783 case IN_OBJECT_WAIT : return "WAITING (on object monitor)"; 1784 case IN_OBJECT_WAIT_TIMED : return "TIMED_WAITING (on object monitor)"; 1785 case PARKED : return "WAITING (parking)"; 1786 case PARKED_TIMED : return "TIMED_WAITING (parking)"; 1787 case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)"; 1788 case TERMINATED : return "TERMINATED"; 1789 default : return "UNKNOWN"; 1790 }; 1791 } 1792 int java_lang_ThreadGroup::_parent_offset = 0; 1793 int java_lang_ThreadGroup::_name_offset = 0; 1794 int java_lang_ThreadGroup::_threads_offset = 0; 1795 int java_lang_ThreadGroup::_groups_offset = 0; 1796 int java_lang_ThreadGroup::_maxPriority_offset = 0; 1797 int java_lang_ThreadGroup::_destroyed_offset = 0; 1798 int java_lang_ThreadGroup::_daemon_offset = 0; 1799 int java_lang_ThreadGroup::_nthreads_offset = 0; 1800 int java_lang_ThreadGroup::_ngroups_offset = 0; 1801 1802 oop java_lang_ThreadGroup::parent(oop java_thread_group) { 1803 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 1804 return java_thread_group->obj_field(_parent_offset); 1805 } 1806 1807 // ("name as oop" accessor is not necessary) 1808 1809 const char* java_lang_ThreadGroup::name(oop java_thread_group) { 1810 oop name = java_thread_group->obj_field(_name_offset); 1811 // ThreadGroup.name can be null 1812 if (name != NULL) { 1813 return java_lang_String::as_utf8_string(name); 1814 } 1815 return NULL; 1816 } 1817 1818 int java_lang_ThreadGroup::nthreads(oop java_thread_group) { 1819 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 1820 return java_thread_group->int_field(_nthreads_offset); 1821 } 1822 1823 objArrayOop java_lang_ThreadGroup::threads(oop java_thread_group) { 1824 oop threads = java_thread_group->obj_field(_threads_offset); 1825 assert(threads != NULL, "threadgroups should have threads"); 1826 assert(threads->is_objArray(), "just checking"); // Todo: Add better type checking code 1827 return objArrayOop(threads); 1828 } 1829 1830 int java_lang_ThreadGroup::ngroups(oop java_thread_group) { 1831 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 1832 return java_thread_group->int_field(_ngroups_offset); 1833 } 1834 1835 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) { 1836 oop groups = java_thread_group->obj_field(_groups_offset); 1837 assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code 1838 return objArrayOop(groups); 1839 } 1840 1841 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) { 1842 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 1843 return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset); 1844 } 1845 1846 bool java_lang_ThreadGroup::is_destroyed(oop java_thread_group) { 1847 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 1848 return java_thread_group->bool_field(_destroyed_offset) != 0; 1849 } 1850 1851 bool java_lang_ThreadGroup::is_daemon(oop java_thread_group) { 1852 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 1853 return java_thread_group->bool_field(_daemon_offset) != 0; 1854 } 1855 1856 #define THREADGROUP_FIELDS_DO(macro) \ 1857 macro(_parent_offset, k, vmSymbols::parent_name(), threadgroup_signature, false); \ 1858 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 1859 macro(_threads_offset, k, vmSymbols::threads_name(), thread_array_signature, false); \ 1860 macro(_groups_offset, k, vmSymbols::groups_name(), threadgroup_array_signature, false); \ 1861 macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature, false); \ 1862 macro(_destroyed_offset, k, vmSymbols::destroyed_name(), bool_signature, false); \ 1863 macro(_daemon_offset, k, vmSymbols::daemon_name(), bool_signature, false); \ 1864 macro(_nthreads_offset, k, vmSymbols::nthreads_name(), int_signature, false); \ 1865 macro(_ngroups_offset, k, vmSymbols::ngroups_name(), int_signature, false) 1866 1867 void java_lang_ThreadGroup::compute_offsets() { 1868 assert(_parent_offset == 0, "offsets should be initialized only once"); 1869 1870 InstanceKlass* k = SystemDictionary::ThreadGroup_klass(); 1871 THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1872 } 1873 1874 #if INCLUDE_CDS 1875 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) { 1876 THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1877 } 1878 #endif 1879 1880 #define THROWABLE_FIELDS_DO(macro) \ 1881 macro(backtrace_offset, k, "backtrace", object_signature, false); \ 1882 macro(detailMessage_offset, k, "detailMessage", string_signature, false); \ 1883 macro(stackTrace_offset, k, "stackTrace", java_lang_StackTraceElement_array, false); \ 1884 macro(depth_offset, k, "depth", int_signature, false); \ 1885 macro(static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true) 1886 1887 void java_lang_Throwable::compute_offsets() { 1888 InstanceKlass* k = SystemDictionary::Throwable_klass(); 1889 THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1890 } 1891 1892 #if INCLUDE_CDS 1893 void java_lang_Throwable::serialize_offsets(SerializeClosure* f) { 1894 THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1895 } 1896 #endif 1897 1898 oop java_lang_Throwable::unassigned_stacktrace() { 1899 InstanceKlass* ik = SystemDictionary::Throwable_klass(); 1900 oop base = ik->static_field_base_raw(); 1901 return base->obj_field(static_unassigned_stacktrace_offset); 1902 } 1903 1904 oop java_lang_Throwable::backtrace(oop throwable) { 1905 return throwable->obj_field_acquire(backtrace_offset); 1906 } 1907 1908 1909 void java_lang_Throwable::set_backtrace(oop throwable, oop value) { 1910 throwable->release_obj_field_put(backtrace_offset, value); 1911 } 1912 1913 int java_lang_Throwable::depth(oop throwable) { 1914 return throwable->int_field(depth_offset); 1915 } 1916 1917 void java_lang_Throwable::set_depth(oop throwable, int value) { 1918 throwable->int_field_put(depth_offset, value); 1919 } 1920 1921 oop java_lang_Throwable::message(oop throwable) { 1922 return throwable->obj_field(detailMessage_offset); 1923 } 1924 1925 1926 // Return Symbol for detailed_message or NULL 1927 Symbol* java_lang_Throwable::detail_message(oop throwable) { 1928 PRESERVE_EXCEPTION_MARK; // Keep original exception 1929 oop detailed_message = java_lang_Throwable::message(throwable); 1930 if (detailed_message != NULL) { 1931 return java_lang_String::as_symbol(detailed_message, THREAD); 1932 } 1933 return NULL; 1934 } 1935 1936 void java_lang_Throwable::set_message(oop throwable, oop value) { 1937 throwable->obj_field_put(detailMessage_offset, value); 1938 } 1939 1940 1941 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) { 1942 throwable->obj_field_put(stackTrace_offset, st_element_array); 1943 } 1944 1945 void java_lang_Throwable::clear_stacktrace(oop throwable) { 1946 set_stacktrace(throwable, NULL); 1947 } 1948 1949 1950 void java_lang_Throwable::print(oop throwable, outputStream* st) { 1951 ResourceMark rm; 1952 Klass* k = throwable->klass(); 1953 assert(k != NULL, "just checking"); 1954 st->print("%s", k->external_name()); 1955 oop msg = message(throwable); 1956 if (msg != NULL) { 1957 st->print(": %s", java_lang_String::as_utf8_string(msg)); 1958 } 1959 } 1960 1961 // After this many redefines, the stack trace is unreliable. 1962 const int MAX_VERSION = USHRT_MAX; 1963 1964 static inline bool version_matches(Method* method, int version) { 1965 assert(version < MAX_VERSION, "version is too big"); 1966 return method != NULL && (method->constants()->version() == version); 1967 } 1968 1969 1970 // This class provides a simple wrapper over the internal structure of 1971 // exception backtrace to insulate users of the backtrace from needing 1972 // to know what it looks like. 1973 class BacktraceBuilder: public StackObj { 1974 friend class BacktraceIterator; 1975 private: 1976 Handle _backtrace; 1977 objArrayOop _head; 1978 typeArrayOop _methods; 1979 typeArrayOop _bcis; 1980 objArrayOop _mirrors; 1981 typeArrayOop _names; // needed to insulate method name against redefinition 1982 int _index; 1983 NoSafepointVerifier _nsv; 1984 1985 enum { 1986 trace_methods_offset = java_lang_Throwable::trace_methods_offset, 1987 trace_bcis_offset = java_lang_Throwable::trace_bcis_offset, 1988 trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset, 1989 trace_names_offset = java_lang_Throwable::trace_names_offset, 1990 trace_next_offset = java_lang_Throwable::trace_next_offset, 1991 trace_size = java_lang_Throwable::trace_size, 1992 trace_chunk_size = java_lang_Throwable::trace_chunk_size 1993 }; 1994 1995 // get info out of chunks 1996 static typeArrayOop get_methods(objArrayHandle chunk) { 1997 typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset)); 1998 assert(methods != NULL, "method array should be initialized in backtrace"); 1999 return methods; 2000 } 2001 static typeArrayOop get_bcis(objArrayHandle chunk) { 2002 typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset)); 2003 assert(bcis != NULL, "bci array should be initialized in backtrace"); 2004 return bcis; 2005 } 2006 static objArrayOop get_mirrors(objArrayHandle chunk) { 2007 objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset)); 2008 assert(mirrors != NULL, "mirror array should be initialized in backtrace"); 2009 return mirrors; 2010 } 2011 static typeArrayOop get_names(objArrayHandle chunk) { 2012 typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset)); 2013 assert(names != NULL, "names array should be initialized in backtrace"); 2014 return names; 2015 } 2016 2017 public: 2018 2019 // constructor for new backtrace 2020 BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL) { 2021 expand(CHECK); 2022 _backtrace = Handle(THREAD, _head); 2023 _index = 0; 2024 } 2025 2026 BacktraceBuilder(Thread* thread, objArrayHandle backtrace) { 2027 _methods = get_methods(backtrace); 2028 _bcis = get_bcis(backtrace); 2029 _mirrors = get_mirrors(backtrace); 2030 _names = get_names(backtrace); 2031 assert(_methods->length() == _bcis->length() && 2032 _methods->length() == _mirrors->length() && 2033 _mirrors->length() == _names->length(), 2034 "method and source information arrays should match"); 2035 2036 // head is the preallocated backtrace 2037 _head = backtrace(); 2038 _backtrace = Handle(thread, _head); 2039 _index = 0; 2040 } 2041 2042 void expand(TRAPS) { 2043 objArrayHandle old_head(THREAD, _head); 2044 PauseNoSafepointVerifier pnsv(&_nsv); 2045 2046 objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK); 2047 objArrayHandle new_head(THREAD, head); 2048 2049 typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK); 2050 typeArrayHandle new_methods(THREAD, methods); 2051 2052 typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK); 2053 typeArrayHandle new_bcis(THREAD, bcis); 2054 2055 objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK); 2056 objArrayHandle new_mirrors(THREAD, mirrors); 2057 2058 typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK); 2059 typeArrayHandle new_names(THREAD, names); 2060 2061 if (!old_head.is_null()) { 2062 old_head->obj_at_put(trace_next_offset, new_head()); 2063 } 2064 new_head->obj_at_put(trace_methods_offset, new_methods()); 2065 new_head->obj_at_put(trace_bcis_offset, new_bcis()); 2066 new_head->obj_at_put(trace_mirrors_offset, new_mirrors()); 2067 new_head->obj_at_put(trace_names_offset, new_names()); 2068 2069 _head = new_head(); 2070 _methods = new_methods(); 2071 _bcis = new_bcis(); 2072 _mirrors = new_mirrors(); 2073 _names = new_names(); 2074 _index = 0; 2075 } 2076 2077 oop backtrace() { 2078 return _backtrace(); 2079 } 2080 2081 inline void push(Method* method, int bci, TRAPS) { 2082 // Smear the -1 bci to 0 since the array only holds unsigned 2083 // shorts. The later line number lookup would just smear the -1 2084 // to a 0 even if it could be recorded. 2085 if (bci == SynchronizationEntryBCI) bci = 0; 2086 2087 if (_index >= trace_chunk_size) { 2088 methodHandle mhandle(THREAD, method); 2089 expand(CHECK); 2090 method = mhandle(); 2091 } 2092 2093 _methods->ushort_at_put(_index, method->orig_method_idnum()); 2094 _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version())); 2095 2096 // Note:this doesn't leak symbols because the mirror in the backtrace keeps the 2097 // klass owning the symbols alive so their refcounts aren't decremented. 2098 Symbol* name = method->name(); 2099 _names->symbol_at_put(_index, name); 2100 2101 // We need to save the mirrors in the backtrace to keep the class 2102 // from being unloaded while we still have this stack trace. 2103 assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror"); 2104 _mirrors->obj_at_put(_index, method->method_holder()->java_mirror()); 2105 _index++; 2106 } 2107 2108 }; 2109 2110 struct BacktraceElement : public StackObj { 2111 int _method_id; 2112 int _bci; 2113 int _version; 2114 Symbol* _name; 2115 Handle _mirror; 2116 BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name) : 2117 _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror) {} 2118 }; 2119 2120 class BacktraceIterator : public StackObj { 2121 int _index; 2122 objArrayHandle _result; 2123 objArrayHandle _mirrors; 2124 typeArrayHandle _methods; 2125 typeArrayHandle _bcis; 2126 typeArrayHandle _names; 2127 2128 void init(objArrayHandle result, Thread* thread) { 2129 // Get method id, bci, version and mirror from chunk 2130 _result = result; 2131 if (_result.not_null()) { 2132 _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result)); 2133 _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result)); 2134 _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result)); 2135 _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result)); 2136 _index = 0; 2137 } 2138 } 2139 public: 2140 BacktraceIterator(objArrayHandle result, Thread* thread) { 2141 init(result, thread); 2142 assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match"); 2143 } 2144 2145 BacktraceElement next(Thread* thread) { 2146 BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)), 2147 _methods->ushort_at(_index), 2148 Backtrace::version_at(_bcis->int_at(_index)), 2149 Backtrace::bci_at(_bcis->int_at(_index)), 2150 _names->symbol_at(_index)); 2151 _index++; 2152 2153 if (_index >= java_lang_Throwable::trace_chunk_size) { 2154 int next_offset = java_lang_Throwable::trace_next_offset; 2155 // Get next chunk 2156 objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset))); 2157 init(result, thread); 2158 } 2159 return e; 2160 } 2161 2162 bool repeat() { 2163 return _result.not_null() && _mirrors->obj_at(_index) != NULL; 2164 } 2165 }; 2166 2167 2168 // Print stack trace element to resource allocated buffer 2169 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id, 2170 int version, int bci, Symbol* name) { 2171 ResourceMark rm; 2172 2173 // Get strings and string lengths 2174 InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror())); 2175 const char* klass_name = holder->external_name(); 2176 int buf_len = (int)strlen(klass_name); 2177 2178 char* method_name = name->as_C_string(); 2179 buf_len += (int)strlen(method_name); 2180 2181 char* source_file_name = NULL; 2182 Symbol* source = Backtrace::get_source_file_name(holder, version); 2183 if (source != NULL) { 2184 source_file_name = source->as_C_string(); 2185 buf_len += (int)strlen(source_file_name); 2186 } 2187 2188 char *module_name = NULL, *module_version = NULL; 2189 ModuleEntry* module = holder->module(); 2190 if (module->is_named()) { 2191 module_name = module->name()->as_C_string(); 2192 buf_len += (int)strlen(module_name); 2193 if (module->version() != NULL) { 2194 module_version = module->version()->as_C_string(); 2195 buf_len += (int)strlen(module_version); 2196 } 2197 } 2198 2199 // Allocate temporary buffer with extra space for formatting and line number 2200 char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64); 2201 2202 // Print stack trace line in buffer 2203 sprintf(buf, "\tat %s.%s(", klass_name, method_name); 2204 2205 // Print module information 2206 if (module_name != NULL) { 2207 if (module_version != NULL) { 2208 sprintf(buf + (int)strlen(buf), "%s@%s/", module_name, module_version); 2209 } else { 2210 sprintf(buf + (int)strlen(buf), "%s/", module_name); 2211 } 2212 } 2213 2214 // The method can be NULL if the requested class version is gone 2215 Method* method = holder->method_with_orig_idnum(method_id, version); 2216 if (!version_matches(method, version)) { 2217 strcat(buf, "Redefined)"); 2218 } else { 2219 int line_number = Backtrace::get_line_number(method, bci); 2220 if (line_number == -2) { 2221 strcat(buf, "Native Method)"); 2222 } else { 2223 if (source_file_name != NULL && (line_number != -1)) { 2224 // Sourcename and linenumber 2225 sprintf(buf + (int)strlen(buf), "%s:%d)", source_file_name, line_number); 2226 } else if (source_file_name != NULL) { 2227 // Just sourcename 2228 sprintf(buf + (int)strlen(buf), "%s)", source_file_name); 2229 } else { 2230 // Neither sourcename nor linenumber 2231 sprintf(buf + (int)strlen(buf), "Unknown Source)"); 2232 } 2233 CompiledMethod* nm = method->code(); 2234 if (WizardMode && nm != NULL) { 2235 sprintf(buf + (int)strlen(buf), "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm); 2236 } 2237 } 2238 } 2239 2240 st->print_cr("%s", buf); 2241 } 2242 2243 void java_lang_Throwable::print_stack_element(outputStream *st, const methodHandle& method, int bci) { 2244 Handle mirror (Thread::current(), method->method_holder()->java_mirror()); 2245 int method_id = method->orig_method_idnum(); 2246 int version = method->constants()->version(); 2247 print_stack_element_to_stream(st, mirror, method_id, version, bci, method->name()); 2248 } 2249 2250 /** 2251 * Print the throwable message and its stack trace plus all causes by walking the 2252 * cause chain. The output looks the same as of Throwable.printStackTrace(). 2253 */ 2254 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) { 2255 // First, print the message. 2256 print(throwable(), st); 2257 st->cr(); 2258 2259 // Now print the stack trace. 2260 Thread* THREAD = Thread::current(); 2261 while (throwable.not_null()) { 2262 objArrayHandle result (THREAD, objArrayOop(backtrace(throwable()))); 2263 if (result.is_null()) { 2264 st->print_raw_cr("\t<<no stack trace available>>"); 2265 return; 2266 } 2267 BacktraceIterator iter(result, THREAD); 2268 2269 while (iter.repeat()) { 2270 BacktraceElement bte = iter.next(THREAD); 2271 print_stack_element_to_stream(st, bte._mirror, bte._method_id, bte._version, bte._bci, bte._name); 2272 } 2273 { 2274 // Call getCause() which doesn't necessarily return the _cause field. 2275 EXCEPTION_MARK; 2276 JavaValue cause(T_OBJECT); 2277 JavaCalls::call_virtual(&cause, 2278 throwable, 2279 throwable->klass(), 2280 vmSymbols::getCause_name(), 2281 vmSymbols::void_throwable_signature(), 2282 THREAD); 2283 // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM. 2284 if (HAS_PENDING_EXCEPTION) { 2285 CLEAR_PENDING_EXCEPTION; 2286 throwable = Handle(); 2287 } else { 2288 throwable = Handle(THREAD, (oop) cause.get_jobject()); 2289 if (throwable.not_null()) { 2290 st->print("Caused by: "); 2291 print(throwable(), st); 2292 st->cr(); 2293 } 2294 } 2295 } 2296 } 2297 } 2298 2299 /** 2300 * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace(). 2301 */ 2302 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) { 2303 assert(throwable->is_a(SystemDictionary::Throwable_klass()), "Throwable instance expected"); 2304 JavaValue result(T_VOID); 2305 JavaCalls::call_virtual(&result, 2306 throwable, 2307 SystemDictionary::Throwable_klass(), 2308 vmSymbols::printStackTrace_name(), 2309 vmSymbols::void_method_signature(), 2310 THREAD); 2311 } 2312 2313 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) { 2314 if (!StackTraceInThrowable) return; 2315 ResourceMark rm(THREAD); 2316 2317 // Start out by clearing the backtrace for this object, in case the VM 2318 // runs out of memory while allocating the stack trace 2319 set_backtrace(throwable(), NULL); 2320 // Clear lazily constructed Java level stacktrace if refilling occurs 2321 // This is unnecessary in 1.7+ but harmless 2322 clear_stacktrace(throwable()); 2323 2324 int max_depth = MaxJavaStackTraceDepth; 2325 JavaThread* thread = (JavaThread*)THREAD; 2326 2327 BacktraceBuilder bt(CHECK); 2328 2329 // If there is no Java frame just return the method that was being called 2330 // with bci 0 2331 if (!thread->has_last_Java_frame()) { 2332 if (max_depth >= 1 && method() != NULL) { 2333 bt.push(method(), 0, CHECK); 2334 log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1); 2335 set_depth(throwable(), 1); 2336 set_backtrace(throwable(), bt.backtrace()); 2337 } 2338 return; 2339 } 2340 2341 // Instead of using vframe directly, this version of fill_in_stack_trace 2342 // basically handles everything by hand. This significantly improved the 2343 // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows. 2344 // See bug 6333838 for more details. 2345 // The "ASSERT" here is to verify this method generates the exactly same stack 2346 // trace as utilizing vframe. 2347 #ifdef ASSERT 2348 vframeStream st(thread); 2349 methodHandle st_method(THREAD, st.method()); 2350 #endif 2351 int total_count = 0; 2352 RegisterMap map(thread, false); 2353 int decode_offset = 0; 2354 CompiledMethod* nm = NULL; 2355 bool skip_fillInStackTrace_check = false; 2356 bool skip_throwableInit_check = false; 2357 bool skip_hidden = !ShowHiddenFrames; 2358 2359 for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) { 2360 Method* method = NULL; 2361 int bci = 0; 2362 2363 // Compiled java method case. 2364 if (decode_offset != 0) { 2365 DebugInfoReadStream stream(nm, decode_offset); 2366 decode_offset = stream.read_int(); 2367 method = (Method*)nm->metadata_at(stream.read_int()); 2368 bci = stream.read_bci(); 2369 } else { 2370 if (fr.is_first_frame()) break; 2371 address pc = fr.pc(); 2372 if (fr.is_interpreted_frame()) { 2373 address bcp = fr.interpreter_frame_bcp(); 2374 method = fr.interpreter_frame_method(); 2375 bci = method->bci_from(bcp); 2376 fr = fr.sender(&map); 2377 } else { 2378 CodeBlob* cb = fr.cb(); 2379 // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL 2380 // but non nmethod 2381 fr = fr.sender(&map); 2382 if (cb == NULL || !cb->is_compiled()) { 2383 continue; 2384 } 2385 nm = cb->as_compiled_method(); 2386 if (nm->method()->is_native()) { 2387 method = nm->method(); 2388 bci = 0; 2389 } else { 2390 PcDesc* pd = nm->pc_desc_at(pc); 2391 decode_offset = pd->scope_decode_offset(); 2392 // if decode_offset is not equal to 0, it will execute the 2393 // "compiled java method case" at the beginning of the loop. 2394 continue; 2395 } 2396 } 2397 } 2398 #ifdef ASSERT 2399 assert(st_method() == method && st.bci() == bci, 2400 "Wrong stack trace"); 2401 st.next(); 2402 // vframeStream::method isn't GC-safe so store off a copy 2403 // of the Method* in case we GC. 2404 if (!st.at_end()) { 2405 st_method = st.method(); 2406 } 2407 #endif 2408 2409 // the format of the stacktrace will be: 2410 // - 1 or more fillInStackTrace frames for the exception class (skipped) 2411 // - 0 or more <init> methods for the exception class (skipped) 2412 // - rest of the stack 2413 2414 if (!skip_fillInStackTrace_check) { 2415 if (method->name() == vmSymbols::fillInStackTrace_name() && 2416 throwable->is_a(method->method_holder())) { 2417 continue; 2418 } 2419 else { 2420 skip_fillInStackTrace_check = true; // gone past them all 2421 } 2422 } 2423 if (!skip_throwableInit_check) { 2424 assert(skip_fillInStackTrace_check, "logic error in backtrace filtering"); 2425 2426 // skip <init> methods of the exception class and superclasses 2427 // This is simlar to classic VM. 2428 if (method->name() == vmSymbols::object_initializer_name() && 2429 throwable->is_a(method->method_holder())) { 2430 continue; 2431 } else { 2432 // there are none or we've seen them all - either way stop checking 2433 skip_throwableInit_check = true; 2434 } 2435 } 2436 if (method->is_hidden()) { 2437 if (skip_hidden) continue; 2438 } 2439 bt.push(method, bci, CHECK); 2440 total_count++; 2441 } 2442 2443 log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count); 2444 2445 // Put completed stack trace into throwable object 2446 set_backtrace(throwable(), bt.backtrace()); 2447 set_depth(throwable(), total_count); 2448 } 2449 2450 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) { 2451 // No-op if stack trace is disabled 2452 if (!StackTraceInThrowable) { 2453 return; 2454 } 2455 2456 // Disable stack traces for some preallocated out of memory errors 2457 if (!Universe::should_fill_in_stack_trace(throwable)) { 2458 return; 2459 } 2460 2461 PRESERVE_EXCEPTION_MARK; 2462 2463 JavaThread* thread = JavaThread::active(); 2464 fill_in_stack_trace(throwable, method, thread); 2465 // ignore exceptions thrown during stack trace filling 2466 CLEAR_PENDING_EXCEPTION; 2467 } 2468 2469 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) { 2470 // Allocate stack trace - backtrace is created but not filled in 2471 2472 // No-op if stack trace is disabled 2473 if (!StackTraceInThrowable) return; 2474 BacktraceBuilder bt(CHECK); // creates a backtrace 2475 set_backtrace(throwable(), bt.backtrace()); 2476 } 2477 2478 2479 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) { 2480 // Fill in stack trace into preallocated backtrace (no GC) 2481 2482 // No-op if stack trace is disabled 2483 if (!StackTraceInThrowable) return; 2484 2485 assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check"); 2486 2487 JavaThread* THREAD = JavaThread::current(); 2488 2489 objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable())); 2490 assert(backtrace.not_null(), "backtrace should have been preallocated"); 2491 2492 ResourceMark rm(THREAD); 2493 vframeStream st(THREAD); 2494 2495 BacktraceBuilder bt(THREAD, backtrace); 2496 2497 // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init 2498 // methods as preallocated errors aren't created by "java" code. 2499 2500 // fill in as much stack trace as possible 2501 int chunk_count = 0; 2502 for (;!st.at_end(); st.next()) { 2503 bt.push(st.method(), st.bci(), CHECK); 2504 chunk_count++; 2505 2506 // Bail-out for deep stacks 2507 if (chunk_count >= trace_chunk_size) break; 2508 } 2509 set_depth(throwable(), chunk_count); 2510 log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count); 2511 2512 // We support the Throwable immutability protocol defined for Java 7. 2513 java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace()); 2514 assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized"); 2515 } 2516 2517 void java_lang_Throwable::get_stack_trace_elements(Handle throwable, 2518 objArrayHandle stack_trace_array_h, TRAPS) { 2519 2520 if (throwable.is_null() || stack_trace_array_h.is_null()) { 2521 THROW(vmSymbols::java_lang_NullPointerException()); 2522 } 2523 2524 assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent"); 2525 2526 if (stack_trace_array_h->length() != depth(throwable())) { 2527 THROW(vmSymbols::java_lang_IndexOutOfBoundsException()); 2528 } 2529 2530 objArrayHandle result(THREAD, objArrayOop(backtrace(throwable()))); 2531 BacktraceIterator iter(result, THREAD); 2532 2533 int index = 0; 2534 while (iter.repeat()) { 2535 BacktraceElement bte = iter.next(THREAD); 2536 2537 Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++)); 2538 2539 if (stack_trace_element.is_null()) { 2540 THROW(vmSymbols::java_lang_NullPointerException()); 2541 } 2542 2543 InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror())); 2544 methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version)); 2545 2546 java_lang_StackTraceElement::fill_in(stack_trace_element, holder, 2547 method, 2548 bte._version, 2549 bte._bci, 2550 bte._name, CHECK); 2551 } 2552 } 2553 2554 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) { 2555 // Allocate java.lang.StackTraceElement instance 2556 InstanceKlass* k = SystemDictionary::StackTraceElement_klass(); 2557 assert(k != NULL, "must be loaded in 1.4+"); 2558 if (k->should_be_initialized()) { 2559 k->initialize(CHECK_0); 2560 } 2561 2562 Handle element = k->allocate_instance_handle(CHECK_0); 2563 2564 int version = method->constants()->version(); 2565 fill_in(element, method->method_holder(), method, version, bci, method->name(), CHECK_0); 2566 return element(); 2567 } 2568 2569 void java_lang_StackTraceElement::fill_in(Handle element, 2570 InstanceKlass* holder, const methodHandle& method, 2571 int version, int bci, Symbol* name, TRAPS) { 2572 assert(element->is_a(SystemDictionary::StackTraceElement_klass()), "sanity check"); 2573 2574 ResourceMark rm(THREAD); 2575 HandleMark hm(THREAD); 2576 2577 // Fill in class name 2578 Handle java_class(THREAD, holder->java_mirror()); 2579 oop classname = java_lang_Class::name(java_class, CHECK); 2580 java_lang_StackTraceElement::set_declaringClass(element(), classname); 2581 java_lang_StackTraceElement::set_declaringClassObject(element(), java_class()); 2582 2583 oop loader = holder->class_loader(); 2584 if (loader != NULL) { 2585 oop loader_name = java_lang_ClassLoader::name(loader); 2586 if (loader_name != NULL) 2587 java_lang_StackTraceElement::set_classLoaderName(element(), loader_name); 2588 } 2589 2590 // Fill in method name 2591 oop methodname = StringTable::intern(name, CHECK); 2592 java_lang_StackTraceElement::set_methodName(element(), methodname); 2593 2594 // Fill in module name and version 2595 ModuleEntry* module = holder->module(); 2596 if (module->is_named()) { 2597 oop module_name = StringTable::intern(module->name(), CHECK); 2598 java_lang_StackTraceElement::set_moduleName(element(), module_name); 2599 oop module_version; 2600 if (module->version() != NULL) { 2601 module_version = StringTable::intern(module->version(), CHECK); 2602 } else { 2603 module_version = NULL; 2604 } 2605 java_lang_StackTraceElement::set_moduleVersion(element(), module_version); 2606 } 2607 2608 if (method() == NULL || !version_matches(method(), version)) { 2609 // The method was redefined, accurate line number information isn't available 2610 java_lang_StackTraceElement::set_fileName(element(), NULL); 2611 java_lang_StackTraceElement::set_lineNumber(element(), -1); 2612 } else { 2613 // Fill in source file name and line number. 2614 Symbol* source = Backtrace::get_source_file_name(holder, version); 2615 oop source_file = java_lang_Class::source_file(java_class()); 2616 if (source != NULL) { 2617 // Class was not redefined. We can trust its cache if set, 2618 // else we have to initialize it. 2619 if (source_file == NULL) { 2620 source_file = StringTable::intern(source, CHECK); 2621 java_lang_Class::set_source_file(java_class(), source_file); 2622 } 2623 } else { 2624 // Class was redefined. Dump the cache if it was set. 2625 if (source_file != NULL) { 2626 source_file = NULL; 2627 java_lang_Class::set_source_file(java_class(), source_file); 2628 } 2629 if (ShowHiddenFrames) { 2630 source = vmSymbols::unknown_class_name(); 2631 source_file = StringTable::intern(source, CHECK); 2632 } 2633 } 2634 java_lang_StackTraceElement::set_fileName(element(), source_file); 2635 2636 int line_number = Backtrace::get_line_number(method, bci); 2637 java_lang_StackTraceElement::set_lineNumber(element(), line_number); 2638 } 2639 } 2640 2641 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) { 2642 HandleMark hm(THREAD); 2643 Handle mname(THREAD, stackFrame->obj_field(_memberName_offset)); 2644 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname()); 2645 // we should expand MemberName::name when Throwable uses StackTrace 2646 // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL); 2647 return method; 2648 } 2649 2650 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, TRAPS) { 2651 // set Method* or mid/cpref 2652 HandleMark hm(THREAD); 2653 Handle mname(Thread::current(), stackFrame->obj_field(_memberName_offset)); 2654 InstanceKlass* ik = method->method_holder(); 2655 CallInfo info(method(), ik, CHECK); 2656 MethodHandles::init_method_MemberName(mname, info); 2657 // set bci 2658 java_lang_StackFrameInfo::set_bci(stackFrame(), bci); 2659 // method may be redefined; store the version 2660 int version = method->constants()->version(); 2661 assert((jushort)version == version, "version should be short"); 2662 java_lang_StackFrameInfo::set_version(stackFrame(), (short)version); 2663 } 2664 2665 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) { 2666 ResourceMark rm(THREAD); 2667 HandleMark hm(THREAD); 2668 Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset)); 2669 Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname())); 2670 InstanceKlass* holder = InstanceKlass::cast(clazz); 2671 Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK); 2672 2673 short version = stackFrame->short_field(_version_offset); 2674 short bci = stackFrame->short_field(_bci_offset); 2675 Symbol* name = method->name(); 2676 java_lang_StackTraceElement::fill_in(stack_trace_element, holder, method, version, bci, name, CHECK); 2677 } 2678 2679 #define STACKFRAMEINFO_FIELDS_DO(macro) \ 2680 macro(_memberName_offset, k, "memberName", object_signature, false); \ 2681 macro(_bci_offset, k, "bci", short_signature, false) 2682 2683 void java_lang_StackFrameInfo::compute_offsets() { 2684 InstanceKlass* k = SystemDictionary::StackFrameInfo_klass(); 2685 STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2686 STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 2687 } 2688 2689 #if INCLUDE_CDS 2690 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) { 2691 STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2692 STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 2693 } 2694 #endif 2695 2696 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \ 2697 macro(_monitors_offset, k, "monitors", object_array_signature, false); \ 2698 macro(_locals_offset, k, "locals", object_array_signature, false); \ 2699 macro(_operands_offset, k, "operands", object_array_signature, false); \ 2700 macro(_mode_offset, k, "mode", int_signature, false) 2701 2702 void java_lang_LiveStackFrameInfo::compute_offsets() { 2703 InstanceKlass* k = SystemDictionary::LiveStackFrameInfo_klass(); 2704 LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2705 } 2706 2707 #if INCLUDE_CDS 2708 void java_lang_LiveStackFrameInfo::serialize_offsets(SerializeClosure* f) { 2709 LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2710 } 2711 #endif 2712 2713 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \ 2714 macro(override_offset, k, "override", bool_signature, false) 2715 2716 void java_lang_reflect_AccessibleObject::compute_offsets() { 2717 InstanceKlass* k = SystemDictionary::reflect_AccessibleObject_klass(); 2718 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2719 } 2720 2721 #if INCLUDE_CDS 2722 void java_lang_reflect_AccessibleObject::serialize_offsets(SerializeClosure* f) { 2723 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2724 } 2725 #endif 2726 2727 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) { 2728 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2729 return (jboolean) reflect->bool_field(override_offset); 2730 } 2731 2732 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) { 2733 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2734 reflect->bool_field_put(override_offset, (int) value); 2735 } 2736 2737 #define METHOD_FIELDS_DO(macro) \ 2738 macro(clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 2739 macro(name_offset, k, vmSymbols::name_name(), string_signature, false); \ 2740 macro(returnType_offset, k, vmSymbols::returnType_name(), class_signature, false); \ 2741 macro(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \ 2742 macro(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \ 2743 macro(slot_offset, k, vmSymbols::slot_name(), int_signature, false); \ 2744 macro(modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 2745 macro(signature_offset, k, vmSymbols::signature_name(), string_signature, false); \ 2746 macro(annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false); \ 2747 macro(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false); \ 2748 macro(annotation_default_offset, k, vmSymbols::annotation_default_name(), byte_array_signature, false); 2749 2750 void java_lang_reflect_Method::compute_offsets() { 2751 InstanceKlass* k = SystemDictionary::reflect_Method_klass(); 2752 METHOD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2753 } 2754 2755 #if INCLUDE_CDS 2756 void java_lang_reflect_Method::serialize_offsets(SerializeClosure* f) { 2757 METHOD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2758 } 2759 #endif 2760 2761 Handle java_lang_reflect_Method::create(TRAPS) { 2762 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2763 Klass* klass = SystemDictionary::reflect_Method_klass(); 2764 // This class is eagerly initialized during VM initialization, since we keep a refence 2765 // to one of the methods 2766 assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized"); 2767 return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD); 2768 } 2769 2770 oop java_lang_reflect_Method::clazz(oop reflect) { 2771 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2772 return reflect->obj_field(clazz_offset); 2773 } 2774 2775 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) { 2776 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2777 reflect->obj_field_put(clazz_offset, value); 2778 } 2779 2780 int java_lang_reflect_Method::slot(oop reflect) { 2781 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2782 return reflect->int_field(slot_offset); 2783 } 2784 2785 void java_lang_reflect_Method::set_slot(oop reflect, int value) { 2786 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2787 reflect->int_field_put(slot_offset, value); 2788 } 2789 2790 void java_lang_reflect_Method::set_name(oop method, oop value) { 2791 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2792 method->obj_field_put(name_offset, value); 2793 } 2794 2795 oop java_lang_reflect_Method::return_type(oop method) { 2796 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2797 return method->obj_field(returnType_offset); 2798 } 2799 2800 void java_lang_reflect_Method::set_return_type(oop method, oop value) { 2801 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2802 method->obj_field_put(returnType_offset, value); 2803 } 2804 2805 oop java_lang_reflect_Method::parameter_types(oop method) { 2806 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2807 return method->obj_field(parameterTypes_offset); 2808 } 2809 2810 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) { 2811 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2812 method->obj_field_put(parameterTypes_offset, value); 2813 } 2814 2815 void java_lang_reflect_Method::set_exception_types(oop method, oop value) { 2816 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2817 method->obj_field_put(exceptionTypes_offset, value); 2818 } 2819 2820 void java_lang_reflect_Method::set_modifiers(oop method, int value) { 2821 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2822 method->int_field_put(modifiers_offset, value); 2823 } 2824 2825 void java_lang_reflect_Method::set_signature(oop method, oop value) { 2826 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2827 method->obj_field_put(signature_offset, value); 2828 } 2829 2830 void java_lang_reflect_Method::set_annotations(oop method, oop value) { 2831 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2832 method->obj_field_put(annotations_offset, value); 2833 } 2834 2835 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) { 2836 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2837 method->obj_field_put(parameter_annotations_offset, value); 2838 } 2839 2840 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) { 2841 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2842 method->obj_field_put(annotation_default_offset, value); 2843 } 2844 2845 #define CONSTRUCTOR_FIELDS_DO(macro) \ 2846 macro(clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 2847 macro(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \ 2848 macro(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \ 2849 macro(slot_offset, k, vmSymbols::slot_name(), int_signature, false); \ 2850 macro(modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 2851 macro(signature_offset, k, vmSymbols::signature_name(), string_signature, false); \ 2852 macro(annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false); \ 2853 macro(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false); 2854 2855 void java_lang_reflect_Constructor::compute_offsets() { 2856 InstanceKlass* k = SystemDictionary::reflect_Constructor_klass(); 2857 CONSTRUCTOR_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2858 } 2859 2860 #if INCLUDE_CDS 2861 void java_lang_reflect_Constructor::serialize_offsets(SerializeClosure* f) { 2862 CONSTRUCTOR_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2863 } 2864 #endif 2865 2866 Handle java_lang_reflect_Constructor::create(TRAPS) { 2867 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2868 Symbol* name = vmSymbols::java_lang_reflect_Constructor(); 2869 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH); 2870 InstanceKlass* ik = InstanceKlass::cast(k); 2871 // Ensure it is initialized 2872 ik->initialize(CHECK_NH); 2873 return ik->allocate_instance_handle(THREAD); 2874 } 2875 2876 oop java_lang_reflect_Constructor::clazz(oop reflect) { 2877 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2878 return reflect->obj_field(clazz_offset); 2879 } 2880 2881 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) { 2882 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2883 reflect->obj_field_put(clazz_offset, value); 2884 } 2885 2886 oop java_lang_reflect_Constructor::parameter_types(oop constructor) { 2887 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2888 return constructor->obj_field(parameterTypes_offset); 2889 } 2890 2891 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) { 2892 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2893 constructor->obj_field_put(parameterTypes_offset, value); 2894 } 2895 2896 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) { 2897 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2898 constructor->obj_field_put(exceptionTypes_offset, value); 2899 } 2900 2901 int java_lang_reflect_Constructor::slot(oop reflect) { 2902 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2903 return reflect->int_field(slot_offset); 2904 } 2905 2906 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) { 2907 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2908 reflect->int_field_put(slot_offset, value); 2909 } 2910 2911 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) { 2912 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2913 constructor->int_field_put(modifiers_offset, value); 2914 } 2915 2916 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) { 2917 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2918 constructor->obj_field_put(signature_offset, value); 2919 } 2920 2921 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) { 2922 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2923 constructor->obj_field_put(annotations_offset, value); 2924 } 2925 2926 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) { 2927 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2928 method->obj_field_put(parameter_annotations_offset, value); 2929 } 2930 2931 #define FIELD_FIELDS_DO(macro) \ 2932 macro(clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 2933 macro(name_offset, k, vmSymbols::name_name(), string_signature, false); \ 2934 macro(type_offset, k, vmSymbols::type_name(), class_signature, false); \ 2935 macro(slot_offset, k, vmSymbols::slot_name(), int_signature, false); \ 2936 macro(modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 2937 macro(signature_offset, k, vmSymbols::signature_name(), string_signature, false); \ 2938 macro(annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false); 2939 2940 void java_lang_reflect_Field::compute_offsets() { 2941 InstanceKlass* k = SystemDictionary::reflect_Field_klass(); 2942 FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2943 } 2944 2945 #if INCLUDE_CDS 2946 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) { 2947 FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2948 } 2949 #endif 2950 2951 Handle java_lang_reflect_Field::create(TRAPS) { 2952 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2953 Symbol* name = vmSymbols::java_lang_reflect_Field(); 2954 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH); 2955 InstanceKlass* ik = InstanceKlass::cast(k); 2956 // Ensure it is initialized 2957 ik->initialize(CHECK_NH); 2958 return ik->allocate_instance_handle(THREAD); 2959 } 2960 2961 oop java_lang_reflect_Field::clazz(oop reflect) { 2962 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2963 return reflect->obj_field(clazz_offset); 2964 } 2965 2966 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) { 2967 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2968 reflect->obj_field_put(clazz_offset, value); 2969 } 2970 2971 oop java_lang_reflect_Field::name(oop field) { 2972 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2973 return field->obj_field(name_offset); 2974 } 2975 2976 void java_lang_reflect_Field::set_name(oop field, oop value) { 2977 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2978 field->obj_field_put(name_offset, value); 2979 } 2980 2981 oop java_lang_reflect_Field::type(oop field) { 2982 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2983 return field->obj_field(type_offset); 2984 } 2985 2986 void java_lang_reflect_Field::set_type(oop field, oop value) { 2987 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2988 field->obj_field_put(type_offset, value); 2989 } 2990 2991 int java_lang_reflect_Field::slot(oop reflect) { 2992 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2993 return reflect->int_field(slot_offset); 2994 } 2995 2996 void java_lang_reflect_Field::set_slot(oop reflect, int value) { 2997 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 2998 reflect->int_field_put(slot_offset, value); 2999 } 3000 3001 int java_lang_reflect_Field::modifiers(oop field) { 3002 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3003 return field->int_field(modifiers_offset); 3004 } 3005 3006 void java_lang_reflect_Field::set_modifiers(oop field, int value) { 3007 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3008 field->int_field_put(modifiers_offset, value); 3009 } 3010 3011 void java_lang_reflect_Field::set_signature(oop field, oop value) { 3012 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3013 field->obj_field_put(signature_offset, value); 3014 } 3015 3016 void java_lang_reflect_Field::set_annotations(oop field, oop value) { 3017 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3018 field->obj_field_put(annotations_offset, value); 3019 } 3020 3021 #define CONSTANTPOOL_FIELDS_DO(macro) \ 3022 macro(_oop_offset, k, "constantPoolOop", object_signature, false) 3023 3024 void reflect_ConstantPool::compute_offsets() { 3025 InstanceKlass* k = SystemDictionary::reflect_ConstantPool_klass(); 3026 // The field is called ConstantPool* in the sun.reflect.ConstantPool class. 3027 CONSTANTPOOL_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3028 } 3029 3030 #if INCLUDE_CDS 3031 void reflect_ConstantPool::serialize_offsets(SerializeClosure* f) { 3032 CONSTANTPOOL_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3033 } 3034 #endif 3035 3036 #define PARAMETER_FIELDS_DO(macro) \ 3037 macro(name_offset, k, vmSymbols::name_name(), string_signature, false); \ 3038 macro(modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 3039 macro(index_offset, k, vmSymbols::index_name(), int_signature, false); \ 3040 macro(executable_offset, k, vmSymbols::executable_name(), executable_signature, false) 3041 3042 void java_lang_reflect_Parameter::compute_offsets() { 3043 InstanceKlass* k = SystemDictionary::reflect_Parameter_klass(); 3044 PARAMETER_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3045 } 3046 3047 #if INCLUDE_CDS 3048 void java_lang_reflect_Parameter::serialize_offsets(SerializeClosure* f) { 3049 PARAMETER_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3050 } 3051 #endif 3052 3053 Handle java_lang_reflect_Parameter::create(TRAPS) { 3054 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3055 Symbol* name = vmSymbols::java_lang_reflect_Parameter(); 3056 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH); 3057 InstanceKlass* ik = InstanceKlass::cast(k); 3058 // Ensure it is initialized 3059 ik->initialize(CHECK_NH); 3060 return ik->allocate_instance_handle(THREAD); 3061 } 3062 3063 oop java_lang_reflect_Parameter::name(oop param) { 3064 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3065 return param->obj_field(name_offset); 3066 } 3067 3068 void java_lang_reflect_Parameter::set_name(oop param, oop value) { 3069 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3070 param->obj_field_put(name_offset, value); 3071 } 3072 3073 int java_lang_reflect_Parameter::modifiers(oop param) { 3074 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3075 return param->int_field(modifiers_offset); 3076 } 3077 3078 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) { 3079 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3080 param->int_field_put(modifiers_offset, value); 3081 } 3082 3083 int java_lang_reflect_Parameter::index(oop param) { 3084 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3085 return param->int_field(index_offset); 3086 } 3087 3088 void java_lang_reflect_Parameter::set_index(oop param, int value) { 3089 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3090 param->int_field_put(index_offset, value); 3091 } 3092 3093 oop java_lang_reflect_Parameter::executable(oop param) { 3094 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3095 return param->obj_field(executable_offset); 3096 } 3097 3098 void java_lang_reflect_Parameter::set_executable(oop param, oop value) { 3099 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3100 param->obj_field_put(executable_offset, value); 3101 } 3102 3103 3104 int java_lang_Module::loader_offset; 3105 int java_lang_Module::name_offset; 3106 int java_lang_Module::_module_entry_offset = -1; 3107 3108 Handle java_lang_Module::create(Handle loader, Handle module_name, TRAPS) { 3109 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3110 return JavaCalls::construct_new_instance(SystemDictionary::Module_klass(), 3111 vmSymbols::java_lang_module_init_signature(), 3112 loader, module_name, CHECK_NH); 3113 } 3114 3115 #define MODULE_FIELDS_DO(macro) \ 3116 macro(loader_offset, k, vmSymbols::loader_name(), classloader_signature, false); \ 3117 macro(name_offset, k, vmSymbols::name_name(), string_signature, false) 3118 3119 void java_lang_Module::compute_offsets() { 3120 InstanceKlass* k = SystemDictionary::Module_klass(); 3121 MODULE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3122 MODULE_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 3123 } 3124 3125 #if INCLUDE_CDS 3126 void java_lang_Module::serialize_offsets(SerializeClosure* f) { 3127 MODULE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3128 MODULE_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 3129 } 3130 #endif 3131 3132 oop java_lang_Module::loader(oop module) { 3133 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3134 return module->obj_field(loader_offset); 3135 } 3136 3137 void java_lang_Module::set_loader(oop module, oop value) { 3138 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3139 module->obj_field_put(loader_offset, value); 3140 } 3141 3142 oop java_lang_Module::name(oop module) { 3143 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3144 return module->obj_field(name_offset); 3145 } 3146 3147 void java_lang_Module::set_name(oop module, oop value) { 3148 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3149 module->obj_field_put(name_offset, value); 3150 } 3151 3152 ModuleEntry* java_lang_Module::module_entry(oop module) { 3153 assert(_module_entry_offset != -1, "Uninitialized module_entry_offset"); 3154 assert(module != NULL, "module can't be null"); 3155 assert(oopDesc::is_oop(module), "module must be oop"); 3156 3157 ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset); 3158 if (module_entry == NULL) { 3159 // If the inject field containing the ModuleEntry* is null then return the 3160 // class loader's unnamed module. 3161 oop loader = java_lang_Module::loader(module); 3162 Handle h_loader = Handle(Thread::current(), loader); 3163 ClassLoaderData* loader_cld = SystemDictionary::register_loader(h_loader); 3164 return loader_cld->unnamed_module(); 3165 } 3166 return module_entry; 3167 } 3168 3169 void java_lang_Module::set_module_entry(oop module, ModuleEntry* module_entry) { 3170 assert(_module_entry_offset != -1, "Uninitialized module_entry_offset"); 3171 assert(module != NULL, "module can't be null"); 3172 assert(oopDesc::is_oop(module), "module must be oop"); 3173 module->address_field_put(_module_entry_offset, (address)module_entry); 3174 } 3175 3176 Handle reflect_ConstantPool::create(TRAPS) { 3177 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3178 InstanceKlass* k = SystemDictionary::reflect_ConstantPool_klass(); 3179 // Ensure it is initialized 3180 k->initialize(CHECK_NH); 3181 return k->allocate_instance_handle(THREAD); 3182 } 3183 3184 3185 void reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) { 3186 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3187 oop mirror = value->pool_holder()->java_mirror(); 3188 // Save the mirror to get back the constant pool. 3189 reflect->obj_field_put(_oop_offset, mirror); 3190 } 3191 3192 ConstantPool* reflect_ConstantPool::get_cp(oop reflect) { 3193 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3194 3195 oop mirror = reflect->obj_field(_oop_offset); 3196 Klass* k = java_lang_Class::as_Klass(mirror); 3197 assert(k->is_instance_klass(), "Must be"); 3198 3199 // Get the constant pool back from the klass. Since class redefinition 3200 // merges the new constant pool into the old, this is essentially the 3201 // same constant pool as the original. If constant pool merging is 3202 // no longer done in the future, this will have to change to save 3203 // the original. 3204 return InstanceKlass::cast(k)->constants(); 3205 } 3206 3207 #define UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(macro) \ 3208 macro(_base_offset, k, "base", object_signature, false) 3209 3210 void reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() { 3211 InstanceKlass* k = SystemDictionary::reflect_UnsafeStaticFieldAccessorImpl_klass(); 3212 UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3213 } 3214 3215 #if INCLUDE_CDS 3216 void reflect_UnsafeStaticFieldAccessorImpl::serialize_offsets(SerializeClosure* f) { 3217 UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3218 } 3219 #endif 3220 3221 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) { 3222 Klass* k = SystemDictionary::box_klass(type); 3223 if (k == NULL) return NULL; 3224 InstanceKlass* ik = InstanceKlass::cast(k); 3225 if (!ik->is_initialized()) ik->initialize(CHECK_0); 3226 return ik->allocate_instance(THREAD); 3227 } 3228 3229 3230 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) { 3231 oop box = initialize_and_allocate(type, CHECK_0); 3232 if (box == NULL) return NULL; 3233 switch (type) { 3234 case T_BOOLEAN: 3235 box->bool_field_put(value_offset, value->z); 3236 break; 3237 case T_CHAR: 3238 box->char_field_put(value_offset, value->c); 3239 break; 3240 case T_FLOAT: 3241 box->float_field_put(value_offset, value->f); 3242 break; 3243 case T_DOUBLE: 3244 box->double_field_put(long_value_offset, value->d); 3245 break; 3246 case T_BYTE: 3247 box->byte_field_put(value_offset, value->b); 3248 break; 3249 case T_SHORT: 3250 box->short_field_put(value_offset, value->s); 3251 break; 3252 case T_INT: 3253 box->int_field_put(value_offset, value->i); 3254 break; 3255 case T_LONG: 3256 box->long_field_put(long_value_offset, value->j); 3257 break; 3258 default: 3259 return NULL; 3260 } 3261 return box; 3262 } 3263 3264 3265 BasicType java_lang_boxing_object::basic_type(oop box) { 3266 if (box == NULL) return T_ILLEGAL; 3267 BasicType type = SystemDictionary::box_klass_type(box->klass()); 3268 if (type == T_OBJECT) // 'unknown' value returned by SD::bkt 3269 return T_ILLEGAL; 3270 return type; 3271 } 3272 3273 3274 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) { 3275 BasicType type = SystemDictionary::box_klass_type(box->klass()); 3276 switch (type) { 3277 case T_BOOLEAN: 3278 value->z = box->bool_field(value_offset); 3279 break; 3280 case T_CHAR: 3281 value->c = box->char_field(value_offset); 3282 break; 3283 case T_FLOAT: 3284 value->f = box->float_field(value_offset); 3285 break; 3286 case T_DOUBLE: 3287 value->d = box->double_field(long_value_offset); 3288 break; 3289 case T_BYTE: 3290 value->b = box->byte_field(value_offset); 3291 break; 3292 case T_SHORT: 3293 value->s = box->short_field(value_offset); 3294 break; 3295 case T_INT: 3296 value->i = box->int_field(value_offset); 3297 break; 3298 case T_LONG: 3299 value->j = box->long_field(long_value_offset); 3300 break; 3301 default: 3302 return T_ILLEGAL; 3303 } // end switch 3304 return type; 3305 } 3306 3307 3308 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) { 3309 BasicType type = SystemDictionary::box_klass_type(box->klass()); 3310 switch (type) { 3311 case T_BOOLEAN: 3312 box->bool_field_put(value_offset, value->z); 3313 break; 3314 case T_CHAR: 3315 box->char_field_put(value_offset, value->c); 3316 break; 3317 case T_FLOAT: 3318 box->float_field_put(value_offset, value->f); 3319 break; 3320 case T_DOUBLE: 3321 box->double_field_put(long_value_offset, value->d); 3322 break; 3323 case T_BYTE: 3324 box->byte_field_put(value_offset, value->b); 3325 break; 3326 case T_SHORT: 3327 box->short_field_put(value_offset, value->s); 3328 break; 3329 case T_INT: 3330 box->int_field_put(value_offset, value->i); 3331 break; 3332 case T_LONG: 3333 box->long_field_put(long_value_offset, value->j); 3334 break; 3335 default: 3336 return T_ILLEGAL; 3337 } // end switch 3338 return type; 3339 } 3340 3341 3342 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) { 3343 switch (type) { 3344 case T_BOOLEAN: st->print("%s", value->z ? "true" : "false"); break; 3345 case T_CHAR: st->print("%d", value->c); break; 3346 case T_BYTE: st->print("%d", value->b); break; 3347 case T_SHORT: st->print("%d", value->s); break; 3348 case T_INT: st->print("%d", value->i); break; 3349 case T_LONG: st->print(JLONG_FORMAT, value->j); break; 3350 case T_FLOAT: st->print("%f", value->f); break; 3351 case T_DOUBLE: st->print("%lf", value->d); break; 3352 default: st->print("type %d?", type); break; 3353 } 3354 } 3355 3356 // Support for java_lang_ref_Reference 3357 3358 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) { 3359 assert(obj != NULL, "sanity"); 3360 if (offset != java_lang_ref_Reference::referent_offset) { 3361 return false; 3362 } 3363 3364 Klass* k = obj->klass(); 3365 if (!k->is_instance_klass()) { 3366 return false; 3367 } 3368 3369 InstanceKlass* ik = InstanceKlass::cast(obj->klass()); 3370 bool is_reference = ik->reference_type() != REF_NONE; 3371 assert(!is_reference || ik->is_subclass_of(SystemDictionary::Reference_klass()), "sanity"); 3372 return is_reference; 3373 } 3374 3375 // Support for java_lang_ref_SoftReference 3376 // 3377 3378 #define SOFTREFERENCE_FIELDS_DO(macro) \ 3379 macro(timestamp_offset, k, "timestamp", long_signature, false); \ 3380 macro(static_clock_offset, k, "clock", long_signature, true) 3381 3382 void java_lang_ref_SoftReference::compute_offsets() { 3383 InstanceKlass* k = SystemDictionary::SoftReference_klass(); 3384 SOFTREFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3385 } 3386 3387 #if INCLUDE_CDS 3388 void java_lang_ref_SoftReference::serialize_offsets(SerializeClosure* f) { 3389 SOFTREFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3390 } 3391 #endif 3392 3393 jlong java_lang_ref_SoftReference::timestamp(oop ref) { 3394 return ref->long_field(timestamp_offset); 3395 } 3396 3397 jlong java_lang_ref_SoftReference::clock() { 3398 InstanceKlass* ik = SystemDictionary::SoftReference_klass(); 3399 oop base = ik->static_field_base_raw(); 3400 return base->long_field(static_clock_offset); 3401 } 3402 3403 void java_lang_ref_SoftReference::set_clock(jlong value) { 3404 InstanceKlass* ik = SystemDictionary::SoftReference_klass(); 3405 oop base = ik->static_field_base_raw(); 3406 base->long_field_put(static_clock_offset, value); 3407 } 3408 3409 // Support for java_lang_invoke_DirectMethodHandle 3410 3411 int java_lang_invoke_DirectMethodHandle::_member_offset; 3412 3413 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) { 3414 oop member_name = NULL; 3415 assert(oopDesc::is_oop(dmh) && java_lang_invoke_DirectMethodHandle::is_instance(dmh), 3416 "a DirectMethodHandle oop is expected"); 3417 return dmh->obj_field(member_offset_in_bytes()); 3418 } 3419 3420 #define DIRECTMETHODHANDLE_FIELDS_DO(macro) \ 3421 macro(_member_offset, k, "member", java_lang_invoke_MemberName_signature, false) 3422 3423 void java_lang_invoke_DirectMethodHandle::compute_offsets() { 3424 InstanceKlass* k = SystemDictionary::DirectMethodHandle_klass(); 3425 DIRECTMETHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3426 } 3427 3428 #if INCLUDE_CDS 3429 void java_lang_invoke_DirectMethodHandle::serialize_offsets(SerializeClosure* f) { 3430 DIRECTMETHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3431 } 3432 #endif 3433 3434 // Support for java_lang_invoke_MethodHandle 3435 3436 int java_lang_invoke_MethodHandle::_type_offset; 3437 int java_lang_invoke_MethodHandle::_form_offset; 3438 3439 int java_lang_invoke_MemberName::_clazz_offset; 3440 int java_lang_invoke_MemberName::_name_offset; 3441 int java_lang_invoke_MemberName::_type_offset; 3442 int java_lang_invoke_MemberName::_flags_offset; 3443 int java_lang_invoke_MemberName::_method_offset; 3444 int java_lang_invoke_MemberName::_vmindex_offset; 3445 3446 int java_lang_invoke_ResolvedMethodName::_vmtarget_offset; 3447 int java_lang_invoke_ResolvedMethodName::_vmholder_offset; 3448 3449 int java_lang_invoke_LambdaForm::_vmentry_offset; 3450 3451 #define METHODHANDLE_FIELDS_DO(macro) \ 3452 macro(_type_offset, k, vmSymbols::type_name(), java_lang_invoke_MethodType_signature, false); \ 3453 macro(_form_offset, k, "form", java_lang_invoke_LambdaForm_signature, false) 3454 3455 void java_lang_invoke_MethodHandle::compute_offsets() { 3456 InstanceKlass* k = SystemDictionary::MethodHandle_klass(); 3457 METHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3458 } 3459 3460 #if INCLUDE_CDS 3461 void java_lang_invoke_MethodHandle::serialize_offsets(SerializeClosure* f) { 3462 METHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3463 } 3464 #endif 3465 3466 #define MEMBERNAME_FIELDS_DO(macro) \ 3467 macro(_clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 3468 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 3469 macro(_type_offset, k, vmSymbols::type_name(), object_signature, false); \ 3470 macro(_flags_offset, k, vmSymbols::flags_name(), int_signature, false); \ 3471 macro(_method_offset, k, vmSymbols::method_name(), java_lang_invoke_ResolvedMethodName_signature, false) 3472 3473 void java_lang_invoke_MemberName::compute_offsets() { 3474 InstanceKlass* k = SystemDictionary::MemberName_klass(); 3475 MEMBERNAME_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3476 MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 3477 } 3478 3479 #if INCLUDE_CDS 3480 void java_lang_invoke_MemberName::serialize_offsets(SerializeClosure* f) { 3481 MEMBERNAME_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3482 MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 3483 } 3484 #endif 3485 3486 void java_lang_invoke_ResolvedMethodName::compute_offsets() { 3487 InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass(); 3488 assert(k != NULL, "jdk mismatch"); 3489 RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 3490 } 3491 3492 #if INCLUDE_CDS 3493 void java_lang_invoke_ResolvedMethodName::serialize_offsets(SerializeClosure* f) { 3494 RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 3495 } 3496 #endif 3497 3498 #define LAMBDAFORM_FIELDS_DO(macro) \ 3499 macro(_vmentry_offset, k, "vmentry", java_lang_invoke_MemberName_signature, false) 3500 3501 void java_lang_invoke_LambdaForm::compute_offsets() { 3502 InstanceKlass* k = SystemDictionary::LambdaForm_klass(); 3503 assert (k != NULL, "jdk mismatch"); 3504 LAMBDAFORM_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3505 } 3506 3507 #if INCLUDE_CDS 3508 void java_lang_invoke_LambdaForm::serialize_offsets(SerializeClosure* f) { 3509 LAMBDAFORM_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3510 } 3511 #endif 3512 3513 bool java_lang_invoke_LambdaForm::is_instance(oop obj) { 3514 return obj != NULL && is_subclass(obj->klass()); 3515 } 3516 3517 3518 oop java_lang_invoke_MethodHandle::type(oop mh) { 3519 return mh->obj_field(_type_offset); 3520 } 3521 3522 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) { 3523 mh->obj_field_put(_type_offset, mtype); 3524 } 3525 3526 oop java_lang_invoke_MethodHandle::form(oop mh) { 3527 assert(_form_offset != 0, ""); 3528 return mh->obj_field(_form_offset); 3529 } 3530 3531 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) { 3532 assert(_form_offset != 0, ""); 3533 mh->obj_field_put(_form_offset, lform); 3534 } 3535 3536 /// MemberName accessors 3537 3538 oop java_lang_invoke_MemberName::clazz(oop mname) { 3539 assert(is_instance(mname), "wrong type"); 3540 return mname->obj_field(_clazz_offset); 3541 } 3542 3543 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) { 3544 assert(is_instance(mname), "wrong type"); 3545 mname->obj_field_put(_clazz_offset, clazz); 3546 } 3547 3548 oop java_lang_invoke_MemberName::name(oop mname) { 3549 assert(is_instance(mname), "wrong type"); 3550 return mname->obj_field(_name_offset); 3551 } 3552 3553 void java_lang_invoke_MemberName::set_name(oop mname, oop name) { 3554 assert(is_instance(mname), "wrong type"); 3555 mname->obj_field_put(_name_offset, name); 3556 } 3557 3558 oop java_lang_invoke_MemberName::type(oop mname) { 3559 assert(is_instance(mname), "wrong type"); 3560 return mname->obj_field(_type_offset); 3561 } 3562 3563 void java_lang_invoke_MemberName::set_type(oop mname, oop type) { 3564 assert(is_instance(mname), "wrong type"); 3565 mname->obj_field_put(_type_offset, type); 3566 } 3567 3568 int java_lang_invoke_MemberName::flags(oop mname) { 3569 assert(is_instance(mname), "wrong type"); 3570 return mname->int_field(_flags_offset); 3571 } 3572 3573 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) { 3574 assert(is_instance(mname), "wrong type"); 3575 mname->int_field_put(_flags_offset, flags); 3576 } 3577 3578 3579 // Return vmtarget from ResolvedMethodName method field through indirection 3580 Method* java_lang_invoke_MemberName::vmtarget(oop mname) { 3581 assert(is_instance(mname), "wrong type"); 3582 oop method = mname->obj_field(_method_offset); 3583 return method == NULL ? NULL : java_lang_invoke_ResolvedMethodName::vmtarget(method); 3584 } 3585 3586 bool java_lang_invoke_MemberName::is_method(oop mname) { 3587 assert(is_instance(mname), "must be MemberName"); 3588 return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0; 3589 } 3590 3591 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) { 3592 assert(is_instance(mname), "wrong type"); 3593 mname->obj_field_put(_method_offset, resolved_method); 3594 } 3595 3596 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) { 3597 assert(is_instance(mname), "wrong type"); 3598 return (intptr_t) mname->address_field(_vmindex_offset); 3599 } 3600 3601 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) { 3602 assert(is_instance(mname), "wrong type"); 3603 mname->address_field_put(_vmindex_offset, (address) index); 3604 } 3605 3606 3607 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) { 3608 assert(is_instance(resolved_method), "wrong type"); 3609 Method* m = (Method*)resolved_method->address_field(_vmtarget_offset); 3610 assert(m->is_method(), "must be"); 3611 return m; 3612 } 3613 3614 // Used by redefinition to change Method* to new Method* with same hash (name, signature) 3615 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) { 3616 assert(is_instance(resolved_method), "wrong type"); 3617 resolved_method->address_field_put(_vmtarget_offset, (address)m); 3618 } 3619 3620 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) { 3621 // lookup ResolvedMethod oop in the table, or create a new one and intern it 3622 oop resolved_method = ResolvedMethodTable::find_method(m()); 3623 if (resolved_method == NULL) { 3624 InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass(); 3625 if (!k->is_initialized()) { 3626 k->initialize(CHECK_NULL); 3627 } 3628 oop new_resolved_method = k->allocate_instance(CHECK_NULL); 3629 new_resolved_method->address_field_put(_vmtarget_offset, (address)m()); 3630 // Add a reference to the loader (actually mirror because unsafe anonymous classes will not have 3631 // distinct loaders) to ensure the metadata is kept alive. 3632 // This mirror may be different than the one in clazz field. 3633 new_resolved_method->obj_field_put(_vmholder_offset, m->method_holder()->java_mirror()); 3634 resolved_method = ResolvedMethodTable::add_method(Handle(THREAD, new_resolved_method)); 3635 } 3636 return resolved_method; 3637 } 3638 3639 oop java_lang_invoke_LambdaForm::vmentry(oop lform) { 3640 assert(is_instance(lform), "wrong type"); 3641 return lform->obj_field(_vmentry_offset); 3642 } 3643 3644 3645 // Support for java_lang_invoke_MethodType 3646 3647 int java_lang_invoke_MethodType::_rtype_offset; 3648 int java_lang_invoke_MethodType::_ptypes_offset; 3649 3650 #define METHODTYPE_FIELDS_DO(macro) \ 3651 macro(_rtype_offset, k, "rtype", class_signature, false); \ 3652 macro(_ptypes_offset, k, "ptypes", class_array_signature, false) 3653 3654 void java_lang_invoke_MethodType::compute_offsets() { 3655 InstanceKlass* k = SystemDictionary::MethodType_klass(); 3656 METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3657 } 3658 3659 #if INCLUDE_CDS 3660 void java_lang_invoke_MethodType::serialize_offsets(SerializeClosure* f) { 3661 METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3662 } 3663 #endif 3664 3665 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) { 3666 st->print("("); 3667 objArrayOop pts = ptypes(mt); 3668 for (int i = 0, limit = pts->length(); i < limit; i++) { 3669 java_lang_Class::print_signature(pts->obj_at(i), st); 3670 } 3671 st->print(")"); 3672 java_lang_Class::print_signature(rtype(mt), st); 3673 } 3674 3675 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found, TRAPS) { 3676 ResourceMark rm; 3677 stringStream buffer(128); 3678 print_signature(mt, &buffer); 3679 const char* sigstr = buffer.base(); 3680 int siglen = (int) buffer.size(); 3681 Symbol *name; 3682 if (!intern_if_not_found) { 3683 name = SymbolTable::probe(sigstr, siglen); 3684 } else { 3685 name = SymbolTable::new_symbol(sigstr, siglen, THREAD); 3686 } 3687 return name; 3688 } 3689 3690 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) { 3691 if (oopDesc::equals(mt1, mt2)) 3692 return true; 3693 if (!oopDesc::equals(rtype(mt1), rtype(mt2))) 3694 return false; 3695 if (ptype_count(mt1) != ptype_count(mt2)) 3696 return false; 3697 for (int i = ptype_count(mt1) - 1; i >= 0; i--) { 3698 if (!oopDesc::equals(ptype(mt1, i), ptype(mt2, i))) 3699 return false; 3700 } 3701 return true; 3702 } 3703 3704 oop java_lang_invoke_MethodType::rtype(oop mt) { 3705 assert(is_instance(mt), "must be a MethodType"); 3706 return mt->obj_field(_rtype_offset); 3707 } 3708 3709 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) { 3710 assert(is_instance(mt), "must be a MethodType"); 3711 return (objArrayOop) mt->obj_field(_ptypes_offset); 3712 } 3713 3714 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) { 3715 return ptypes(mt)->obj_at(idx); 3716 } 3717 3718 int java_lang_invoke_MethodType::ptype_count(oop mt) { 3719 return ptypes(mt)->length(); 3720 } 3721 3722 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) { 3723 objArrayOop pts = ptypes(mt); 3724 int count = pts->length(); 3725 int slots = 0; 3726 for (int i = 0; i < count; i++) { 3727 BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i)); 3728 slots += type2size[bt]; 3729 } 3730 return slots; 3731 } 3732 3733 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) { 3734 BasicType bt = java_lang_Class::as_BasicType(rtype(mt)); 3735 return type2size[bt]; 3736 } 3737 3738 3739 // Support for java_lang_invoke_CallSite 3740 3741 int java_lang_invoke_CallSite::_target_offset; 3742 int java_lang_invoke_CallSite::_context_offset; 3743 3744 #define CALLSITE_FIELDS_DO(macro) \ 3745 macro(_target_offset, k, "target", java_lang_invoke_MethodHandle_signature, false); \ 3746 macro(_context_offset, k, "context", java_lang_invoke_MethodHandleNatives_CallSiteContext_signature, false) 3747 3748 void java_lang_invoke_CallSite::compute_offsets() { 3749 InstanceKlass* k = SystemDictionary::CallSite_klass(); 3750 CALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3751 } 3752 3753 #if INCLUDE_CDS 3754 void java_lang_invoke_CallSite::serialize_offsets(SerializeClosure* f) { 3755 CALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3756 } 3757 #endif 3758 3759 oop java_lang_invoke_CallSite::context_no_keepalive(oop call_site) { 3760 assert(java_lang_invoke_CallSite::is_instance(call_site), ""); 3761 3762 oop dep_oop = call_site->obj_field_access<AS_NO_KEEPALIVE>(_context_offset); 3763 return dep_oop; 3764 } 3765 3766 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext 3767 3768 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset; 3769 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_last_cleanup_offset; 3770 3771 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() { 3772 InstanceKlass* k = SystemDictionary::Context_klass(); 3773 CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 3774 } 3775 3776 #if INCLUDE_CDS 3777 void java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize_offsets(SerializeClosure* f) { 3778 CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 3779 } 3780 #endif 3781 3782 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) { 3783 assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), ""); 3784 nmethodBucket* volatile* vmdeps_addr = (nmethodBucket* volatile*)call_site->field_addr(_vmdependencies_offset); 3785 volatile uint64_t* last_cleanup_addr = (volatile uint64_t*)call_site->field_addr(_last_cleanup_offset); 3786 DependencyContext dep_ctx(vmdeps_addr, last_cleanup_addr); 3787 return dep_ctx; 3788 } 3789 3790 // Support for java_security_AccessControlContext 3791 3792 int java_security_AccessControlContext::_context_offset = 0; 3793 int java_security_AccessControlContext::_privilegedContext_offset = 0; 3794 int java_security_AccessControlContext::_isPrivileged_offset = 0; 3795 int java_security_AccessControlContext::_isAuthorized_offset = -1; 3796 3797 #define ACCESSCONTROLCONTEXT_FIELDS_DO(macro) \ 3798 macro(_context_offset, k, "context", protectiondomain_signature, false); \ 3799 macro(_privilegedContext_offset, k, "privilegedContext", accesscontrolcontext_signature, false); \ 3800 macro(_isPrivileged_offset, k, "isPrivileged", bool_signature, false); \ 3801 macro(_isAuthorized_offset, k, "isAuthorized", bool_signature, false) 3802 3803 void java_security_AccessControlContext::compute_offsets() { 3804 assert(_isPrivileged_offset == 0, "offsets should be initialized only once"); 3805 InstanceKlass* k = SystemDictionary::AccessControlContext_klass(); 3806 ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3807 } 3808 3809 #if INCLUDE_CDS 3810 void java_security_AccessControlContext::serialize_offsets(SerializeClosure* f) { 3811 ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3812 } 3813 #endif 3814 3815 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) { 3816 assert(_isPrivileged_offset != 0, "offsets should have been initialized"); 3817 // Ensure klass is initialized 3818 SystemDictionary::AccessControlContext_klass()->initialize(CHECK_0); 3819 // Allocate result 3820 oop result = SystemDictionary::AccessControlContext_klass()->allocate_instance(CHECK_0); 3821 // Fill in values 3822 result->obj_field_put(_context_offset, context()); 3823 result->obj_field_put(_privilegedContext_offset, privileged_context()); 3824 result->bool_field_put(_isPrivileged_offset, isPrivileged); 3825 // whitelist AccessControlContexts created by the JVM if present 3826 if (_isAuthorized_offset != -1) { 3827 result->bool_field_put(_isAuthorized_offset, true); 3828 } 3829 return result; 3830 } 3831 3832 3833 // Support for java_lang_ClassLoader 3834 3835 bool java_lang_ClassLoader::offsets_computed = false; 3836 int java_lang_ClassLoader::_loader_data_offset = -1; 3837 int java_lang_ClassLoader::parallelCapable_offset = -1; 3838 int java_lang_ClassLoader::name_offset = -1; 3839 int java_lang_ClassLoader::nameAndId_offset = -1; 3840 int java_lang_ClassLoader::unnamedModule_offset = -1; 3841 3842 ClassLoaderData* java_lang_ClassLoader::loader_data_acquire(oop loader) { 3843 assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop"); 3844 return HeapAccess<MO_ACQUIRE>::load_at(loader, _loader_data_offset); 3845 } 3846 3847 ClassLoaderData* java_lang_ClassLoader::loader_data_raw(oop loader) { 3848 assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop"); 3849 return RawAccess<>::load_at(loader, _loader_data_offset); 3850 } 3851 3852 void java_lang_ClassLoader::release_set_loader_data(oop loader, ClassLoaderData* new_data) { 3853 assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop"); 3854 HeapAccess<MO_RELEASE>::store_at(loader, _loader_data_offset, new_data); 3855 } 3856 3857 #define CLASSLOADER_FIELDS_DO(macro) \ 3858 macro(parallelCapable_offset, k1, "parallelLockMap", concurrenthashmap_signature, false); \ 3859 macro(name_offset, k1, vmSymbols::name_name(), string_signature, false); \ 3860 macro(nameAndId_offset, k1, "nameAndId", string_signature, false); \ 3861 macro(unnamedModule_offset, k1, "unnamedModule", module_signature, false); \ 3862 macro(parent_offset, k1, "parent", classloader_signature, false) 3863 3864 void java_lang_ClassLoader::compute_offsets() { 3865 assert(!offsets_computed, "offsets should be initialized only once"); 3866 offsets_computed = true; 3867 3868 InstanceKlass* k1 = SystemDictionary::ClassLoader_klass(); 3869 CLASSLOADER_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3870 3871 CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 3872 } 3873 3874 #if INCLUDE_CDS 3875 void java_lang_ClassLoader::serialize_offsets(SerializeClosure* f) { 3876 CLASSLOADER_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3877 CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 3878 } 3879 #endif 3880 3881 oop java_lang_ClassLoader::parent(oop loader) { 3882 assert(is_instance(loader), "loader must be oop"); 3883 return loader->obj_field(parent_offset); 3884 } 3885 3886 // Returns the name field of this class loader. If the name field has not 3887 // been set, null will be returned. 3888 oop java_lang_ClassLoader::name(oop loader) { 3889 assert(is_instance(loader), "loader must be oop"); 3890 return loader->obj_field(name_offset); 3891 } 3892 3893 // Returns the nameAndId field of this class loader. The format is 3894 // as follows: 3895 // If the defining loader has a name explicitly set then '<loader-name>' @<id> 3896 // If the defining loader has no name then <qualified-class-name> @<id> 3897 // If built-in loader, then omit '@<id>' as there is only one instance. 3898 // Use ClassLoader::loader_name_id() to obtain this String as a char*. 3899 oop java_lang_ClassLoader::nameAndId(oop loader) { 3900 assert(is_instance(loader), "loader must be oop"); 3901 return loader->obj_field(nameAndId_offset); 3902 } 3903 3904 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) { 3905 assert(is_instance(loader), "loader must be oop"); 3906 assert(cl == NULL || is_instance(cl), "cl argument must be oop"); 3907 oop acl = loader; 3908 debug_only(jint loop_count = 0); 3909 // This loop taken verbatim from ClassLoader.java: 3910 do { 3911 acl = parent(acl); 3912 if (oopDesc::equals(cl, acl)) { 3913 return true; 3914 } 3915 assert(++loop_count > 0, "loop_count overflow"); 3916 } while (acl != NULL); 3917 return false; 3918 } 3919 3920 bool java_lang_ClassLoader::is_instance(oop obj) { 3921 return obj != NULL && is_subclass(obj->klass()); 3922 } 3923 3924 3925 // For class loader classes, parallelCapable defined 3926 // based on non-null field 3927 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it 3928 bool java_lang_ClassLoader::parallelCapable(oop class_loader) { 3929 if (parallelCapable_offset == -1) { 3930 // Default for backward compatibility is false 3931 return false; 3932 } 3933 return (class_loader->obj_field(parallelCapable_offset) != NULL); 3934 } 3935 3936 bool java_lang_ClassLoader::is_trusted_loader(oop loader) { 3937 // Fix for 4474172; see evaluation for more details 3938 loader = non_reflection_class_loader(loader); 3939 3940 oop cl = SystemDictionary::java_system_loader(); 3941 while(cl != NULL) { 3942 if (oopDesc::equals(cl, loader)) return true; 3943 cl = parent(cl); 3944 } 3945 return false; 3946 } 3947 3948 // Return true if this is one of the class loaders associated with 3949 // the generated bytecodes for reflection. 3950 bool java_lang_ClassLoader::is_reflection_class_loader(oop loader) { 3951 if (loader != NULL) { 3952 Klass* delegating_cl_class = SystemDictionary::reflect_DelegatingClassLoader_klass(); 3953 // This might be null in non-1.4 JDKs 3954 return (delegating_cl_class != NULL && loader->is_a(delegating_cl_class)); 3955 } 3956 return false; 3957 } 3958 3959 oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) { 3960 // See whether this is one of the class loaders associated with 3961 // the generated bytecodes for reflection, and if so, "magically" 3962 // delegate to its parent to prevent class loading from occurring 3963 // in places where applications using reflection didn't expect it. 3964 if (is_reflection_class_loader(loader)) { 3965 return parent(loader); 3966 } 3967 return loader; 3968 } 3969 3970 oop java_lang_ClassLoader::unnamedModule(oop loader) { 3971 assert(is_instance(loader), "loader must be oop"); 3972 return loader->obj_field(unnamedModule_offset); 3973 } 3974 3975 // Support for java_lang_System 3976 // 3977 #define SYSTEM_FIELDS_DO(macro) \ 3978 macro(static_in_offset, k, "in", input_stream_signature, true); \ 3979 macro(static_out_offset, k, "out", print_stream_signature, true); \ 3980 macro(static_err_offset, k, "err", print_stream_signature, true); \ 3981 macro(static_security_offset, k, "security", security_manager_signature, true) 3982 3983 void java_lang_System::compute_offsets() { 3984 InstanceKlass* k = SystemDictionary::System_klass(); 3985 SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3986 } 3987 3988 #if INCLUDE_CDS 3989 void java_lang_System::serialize_offsets(SerializeClosure* f) { 3990 SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3991 } 3992 #endif 3993 3994 int java_lang_System::in_offset_in_bytes() { return static_in_offset; } 3995 int java_lang_System::out_offset_in_bytes() { return static_out_offset; } 3996 int java_lang_System::err_offset_in_bytes() { return static_err_offset; } 3997 3998 int java_lang_Class::_klass_offset; 3999 int java_lang_Class::_array_klass_offset; 4000 int java_lang_Class::_oop_size_offset; 4001 int java_lang_Class::_static_oop_field_count_offset; 4002 int java_lang_Class::_class_loader_offset; 4003 int java_lang_Class::_module_offset; 4004 int java_lang_Class::_protection_domain_offset; 4005 int java_lang_Class::_component_mirror_offset; 4006 int java_lang_Class::_init_lock_offset; 4007 int java_lang_Class::_signers_offset; 4008 int java_lang_Class::_name_offset; 4009 int java_lang_Class::_source_file_offset; 4010 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL; 4011 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL; 4012 int java_lang_Throwable::backtrace_offset; 4013 int java_lang_Throwable::detailMessage_offset; 4014 int java_lang_Throwable::stackTrace_offset; 4015 int java_lang_Throwable::depth_offset; 4016 int java_lang_Throwable::static_unassigned_stacktrace_offset; 4017 int java_lang_reflect_AccessibleObject::override_offset; 4018 int java_lang_reflect_Method::clazz_offset; 4019 int java_lang_reflect_Method::name_offset; 4020 int java_lang_reflect_Method::returnType_offset; 4021 int java_lang_reflect_Method::parameterTypes_offset; 4022 int java_lang_reflect_Method::exceptionTypes_offset; 4023 int java_lang_reflect_Method::slot_offset; 4024 int java_lang_reflect_Method::modifiers_offset; 4025 int java_lang_reflect_Method::signature_offset; 4026 int java_lang_reflect_Method::annotations_offset; 4027 int java_lang_reflect_Method::parameter_annotations_offset; 4028 int java_lang_reflect_Method::annotation_default_offset; 4029 int java_lang_reflect_Constructor::clazz_offset; 4030 int java_lang_reflect_Constructor::parameterTypes_offset; 4031 int java_lang_reflect_Constructor::exceptionTypes_offset; 4032 int java_lang_reflect_Constructor::slot_offset; 4033 int java_lang_reflect_Constructor::modifiers_offset; 4034 int java_lang_reflect_Constructor::signature_offset; 4035 int java_lang_reflect_Constructor::annotations_offset; 4036 int java_lang_reflect_Constructor::parameter_annotations_offset; 4037 int java_lang_reflect_Field::clazz_offset; 4038 int java_lang_reflect_Field::name_offset; 4039 int java_lang_reflect_Field::type_offset; 4040 int java_lang_reflect_Field::slot_offset; 4041 int java_lang_reflect_Field::modifiers_offset; 4042 int java_lang_reflect_Field::signature_offset; 4043 int java_lang_reflect_Field::annotations_offset; 4044 int java_lang_reflect_Parameter::name_offset; 4045 int java_lang_reflect_Parameter::modifiers_offset; 4046 int java_lang_reflect_Parameter::index_offset; 4047 int java_lang_reflect_Parameter::executable_offset; 4048 int java_lang_boxing_object::value_offset; 4049 int java_lang_boxing_object::long_value_offset; 4050 int java_lang_ref_Reference::referent_offset; 4051 int java_lang_ref_Reference::queue_offset; 4052 int java_lang_ref_Reference::next_offset; 4053 int java_lang_ref_Reference::discovered_offset; 4054 int java_lang_ref_SoftReference::timestamp_offset; 4055 int java_lang_ref_SoftReference::static_clock_offset; 4056 int java_lang_ClassLoader::parent_offset; 4057 int java_lang_System::static_in_offset; 4058 int java_lang_System::static_out_offset; 4059 int java_lang_System::static_err_offset; 4060 int java_lang_System::static_security_offset; 4061 int java_lang_StackTraceElement::methodName_offset; 4062 int java_lang_StackTraceElement::fileName_offset; 4063 int java_lang_StackTraceElement::lineNumber_offset; 4064 int java_lang_StackTraceElement::moduleName_offset; 4065 int java_lang_StackTraceElement::moduleVersion_offset; 4066 int java_lang_StackTraceElement::classLoaderName_offset; 4067 int java_lang_StackTraceElement::declaringClass_offset; 4068 int java_lang_StackTraceElement::declaringClassObject_offset; 4069 int java_lang_StackFrameInfo::_memberName_offset; 4070 int java_lang_StackFrameInfo::_bci_offset; 4071 int java_lang_StackFrameInfo::_version_offset; 4072 int java_lang_LiveStackFrameInfo::_monitors_offset; 4073 int java_lang_LiveStackFrameInfo::_locals_offset; 4074 int java_lang_LiveStackFrameInfo::_operands_offset; 4075 int java_lang_LiveStackFrameInfo::_mode_offset; 4076 int java_lang_AssertionStatusDirectives::classes_offset; 4077 int java_lang_AssertionStatusDirectives::classEnabled_offset; 4078 int java_lang_AssertionStatusDirectives::packages_offset; 4079 int java_lang_AssertionStatusDirectives::packageEnabled_offset; 4080 int java_lang_AssertionStatusDirectives::deflt_offset; 4081 int java_nio_Buffer::_limit_offset; 4082 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset; 4083 int reflect_ConstantPool::_oop_offset; 4084 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset; 4085 4086 4087 #define STACKTRACEELEMENT_FIELDS_DO(macro) \ 4088 macro(declaringClassObject_offset, k, "declaringClassObject", class_signature, false); \ 4089 macro(classLoaderName_offset, k, "classLoaderName", string_signature, false); \ 4090 macro(moduleName_offset, k, "moduleName", string_signature, false); \ 4091 macro(moduleVersion_offset, k, "moduleVersion", string_signature, false); \ 4092 macro(declaringClass_offset, k, "declaringClass", string_signature, false); \ 4093 macro(methodName_offset, k, "methodName", string_signature, false); \ 4094 macro(fileName_offset, k, "fileName", string_signature, false); \ 4095 macro(lineNumber_offset, k, "lineNumber", int_signature, false) 4096 4097 // Support for java_lang_StackTraceElement 4098 void java_lang_StackTraceElement::compute_offsets() { 4099 InstanceKlass* k = SystemDictionary::StackTraceElement_klass(); 4100 STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4101 } 4102 4103 #if INCLUDE_CDS 4104 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) { 4105 STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4106 } 4107 #endif 4108 4109 void java_lang_StackTraceElement::set_fileName(oop element, oop value) { 4110 element->obj_field_put(fileName_offset, value); 4111 } 4112 4113 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) { 4114 element->obj_field_put(declaringClass_offset, value); 4115 } 4116 4117 void java_lang_StackTraceElement::set_methodName(oop element, oop value) { 4118 element->obj_field_put(methodName_offset, value); 4119 } 4120 4121 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) { 4122 element->int_field_put(lineNumber_offset, value); 4123 } 4124 4125 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) { 4126 element->obj_field_put(moduleName_offset, value); 4127 } 4128 4129 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) { 4130 element->obj_field_put(moduleVersion_offset, value); 4131 } 4132 4133 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) { 4134 element->obj_field_put(classLoaderName_offset, value); 4135 } 4136 4137 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) { 4138 element->obj_field_put(declaringClassObject_offset, value); 4139 } 4140 4141 void java_lang_StackFrameInfo::set_version(oop element, short value) { 4142 element->short_field_put(_version_offset, value); 4143 } 4144 4145 void java_lang_StackFrameInfo::set_bci(oop element, int value) { 4146 element->int_field_put(_bci_offset, value); 4147 } 4148 4149 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) { 4150 element->obj_field_put(_monitors_offset, value); 4151 } 4152 4153 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) { 4154 element->obj_field_put(_locals_offset, value); 4155 } 4156 4157 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) { 4158 element->obj_field_put(_operands_offset, value); 4159 } 4160 4161 void java_lang_LiveStackFrameInfo::set_mode(oop element, int value) { 4162 element->int_field_put(_mode_offset, value); 4163 } 4164 4165 // Support for java Assertions - java_lang_AssertionStatusDirectives. 4166 #define ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(macro) \ 4167 macro(classes_offset, k, "classes", string_array_signature, false); \ 4168 macro(classEnabled_offset, k, "classEnabled", bool_array_signature, false); \ 4169 macro(packages_offset, k, "packages", string_array_signature, false); \ 4170 macro(packageEnabled_offset, k, "packageEnabled", bool_array_signature, false); \ 4171 macro(deflt_offset, k, "deflt", bool_signature, false) 4172 4173 void java_lang_AssertionStatusDirectives::compute_offsets() { 4174 InstanceKlass* k = SystemDictionary::AssertionStatusDirectives_klass(); 4175 ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4176 } 4177 4178 #if INCLUDE_CDS 4179 void java_lang_AssertionStatusDirectives::serialize_offsets(SerializeClosure* f) { 4180 ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4181 } 4182 #endif 4183 4184 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) { 4185 o->obj_field_put(classes_offset, val); 4186 } 4187 4188 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) { 4189 o->obj_field_put(classEnabled_offset, val); 4190 } 4191 4192 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) { 4193 o->obj_field_put(packages_offset, val); 4194 } 4195 4196 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) { 4197 o->obj_field_put(packageEnabled_offset, val); 4198 } 4199 4200 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) { 4201 o->bool_field_put(deflt_offset, val); 4202 } 4203 4204 4205 // Support for intrinsification of java.nio.Buffer.checkIndex 4206 int java_nio_Buffer::limit_offset() { 4207 return _limit_offset; 4208 } 4209 4210 #define BUFFER_FIELDS_DO(macro) \ 4211 macro(_limit_offset, k, "limit", int_signature, false) 4212 4213 void java_nio_Buffer::compute_offsets() { 4214 InstanceKlass* k = SystemDictionary::nio_Buffer_klass(); 4215 assert(k != NULL, "must be loaded in 1.4+"); 4216 BUFFER_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4217 } 4218 4219 #if INCLUDE_CDS 4220 void java_nio_Buffer::serialize_offsets(SerializeClosure* f) { 4221 BUFFER_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4222 } 4223 #endif 4224 4225 #define AOS_FIELDS_DO(macro) \ 4226 macro(_owner_offset, k, "exclusiveOwnerThread", thread_signature, false) 4227 4228 void java_util_concurrent_locks_AbstractOwnableSynchronizer::compute_offsets() { 4229 InstanceKlass* k = SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass(); 4230 AOS_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4231 } 4232 4233 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) { 4234 assert(_owner_offset != 0, "Must be initialized"); 4235 return obj->obj_field(_owner_offset); 4236 } 4237 4238 #if INCLUDE_CDS 4239 void java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize_offsets(SerializeClosure* f) { 4240 AOS_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4241 } 4242 #endif 4243 4244 static int member_offset(int hardcoded_offset) { 4245 return (hardcoded_offset * heapOopSize) + instanceOopDesc::base_offset_in_bytes(); 4246 } 4247 4248 // Compute hard-coded offsets 4249 // Invoked before SystemDictionary::initialize, so pre-loaded classes 4250 // are not available to determine the offset_of_static_fields. 4251 void JavaClasses::compute_hard_coded_offsets() { 4252 4253 // java_lang_boxing_object 4254 java_lang_boxing_object::value_offset = member_offset(java_lang_boxing_object::hc_value_offset); 4255 java_lang_boxing_object::long_value_offset = align_up(member_offset(java_lang_boxing_object::hc_value_offset), BytesPerLong); 4256 4257 // java_lang_ref_Reference 4258 java_lang_ref_Reference::referent_offset = member_offset(java_lang_ref_Reference::hc_referent_offset); 4259 java_lang_ref_Reference::queue_offset = member_offset(java_lang_ref_Reference::hc_queue_offset); 4260 java_lang_ref_Reference::next_offset = member_offset(java_lang_ref_Reference::hc_next_offset); 4261 java_lang_ref_Reference::discovered_offset = member_offset(java_lang_ref_Reference::hc_discovered_offset); 4262 } 4263 4264 #define DO_COMPUTE_OFFSETS(k) k::compute_offsets(); 4265 4266 // Compute non-hard-coded field offsets of all the classes in this file 4267 void JavaClasses::compute_offsets() { 4268 if (UseSharedSpaces) { 4269 assert(JvmtiExport::is_early_phase() && !(JvmtiExport::should_post_class_file_load_hook() && 4270 JvmtiExport::has_early_class_hook_env()), 4271 "JavaClasses::compute_offsets() must be called in early JVMTI phase."); 4272 // None of the classes used by the rest of this function can be replaced by 4273 // JMVTI ClassFileLoadHook. 4274 // We are safe to use the archived offsets, which have already been restored 4275 // by JavaClasses::serialize_offsets, without computing the offsets again. 4276 return; 4277 } 4278 4279 // We have already called the compute_offsets() of the 4280 // BASIC_JAVA_CLASSES_DO_PART1 classes (java_lang_String and java_lang_Class) 4281 // earlier inside SystemDictionary::resolve_well_known_classes() 4282 BASIC_JAVA_CLASSES_DO_PART2(DO_COMPUTE_OFFSETS); 4283 4284 // generated interpreter code wants to know about the offsets we just computed: 4285 AbstractAssembler::update_delayed_values(); 4286 } 4287 4288 #if INCLUDE_CDS 4289 #define DO_SERIALIZE_OFFSETS(k) k::serialize_offsets(soc); 4290 4291 void JavaClasses::serialize_offsets(SerializeClosure* soc) { 4292 BASIC_JAVA_CLASSES_DO(DO_SERIALIZE_OFFSETS); 4293 } 4294 #endif 4295 4296 4297 #ifndef PRODUCT 4298 4299 // These functions exist to assert the validity of hard-coded field offsets to guard 4300 // against changes in the class files 4301 4302 bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) { 4303 EXCEPTION_MARK; 4304 fieldDescriptor fd; 4305 TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH); 4306 Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH); 4307 InstanceKlass* ik = InstanceKlass::cast(k); 4308 TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH); 4309 TempNewSymbol f_sig = SymbolTable::new_symbol(field_sig, CATCH); 4310 if (!ik->find_local_field(f_name, f_sig, &fd)) { 4311 tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name); 4312 return false; 4313 } 4314 if (fd.is_static()) { 4315 tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name); 4316 return false; 4317 } 4318 if (fd.offset() == hardcoded_offset ) { 4319 return true; 4320 } else { 4321 tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.", 4322 klass_name, field_name, hardcoded_offset, fd.offset()); 4323 return false; 4324 } 4325 } 4326 4327 // Check the hard-coded field offsets of all the classes in this file 4328 4329 void JavaClasses::check_offsets() { 4330 bool valid = true; 4331 4332 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \ 4333 valid &= check_offset(klass_name, cpp_klass_name :: field_name ## _offset, #field_name, field_sig) 4334 4335 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \ 4336 valid &= check_offset(klass_name, cpp_klass_name :: long_ ## field_name ## _offset, #field_name, field_sig) 4337 4338 // Boxed primitive objects (java_lang_boxing_object) 4339 4340 CHECK_OFFSET("java/lang/Boolean", java_lang_boxing_object, value, "Z"); 4341 CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C"); 4342 CHECK_OFFSET("java/lang/Float", java_lang_boxing_object, value, "F"); 4343 CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D"); 4344 CHECK_OFFSET("java/lang/Byte", java_lang_boxing_object, value, "B"); 4345 CHECK_OFFSET("java/lang/Short", java_lang_boxing_object, value, "S"); 4346 CHECK_OFFSET("java/lang/Integer", java_lang_boxing_object, value, "I"); 4347 CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J"); 4348 4349 // java.lang.ref.Reference 4350 4351 CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;"); 4352 CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;"); 4353 CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;"); 4354 // Fake field 4355 //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;"); 4356 4357 if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed"); 4358 } 4359 4360 #endif // PRODUCT 4361 4362 int InjectedField::compute_offset() { 4363 InstanceKlass* ik = InstanceKlass::cast(klass()); 4364 for (AllFieldStream fs(ik); !fs.done(); fs.next()) { 4365 if (!may_be_java && !fs.access_flags().is_internal()) { 4366 // Only look at injected fields 4367 continue; 4368 } 4369 if (fs.name() == name() && fs.signature() == signature()) { 4370 return fs.offset(); 4371 } 4372 } 4373 ResourceMark rm; 4374 tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : ""); 4375 #ifndef PRODUCT 4376 ik->print(); 4377 tty->print_cr("all fields:"); 4378 for (AllFieldStream fs(ik); !fs.done(); fs.next()) { 4379 tty->print_cr(" name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int()); 4380 } 4381 #endif //PRODUCT 4382 vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class"); 4383 return -1; 4384 } 4385 4386 void javaClasses_init() { 4387 JavaClasses::compute_offsets(); 4388 JavaClasses::check_offsets(); 4389 FilteredFieldsMap::initialize(); // must be done after computing offsets. 4390 }