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