1 /* 2 * Copyright (c) 1997, 2014, 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 #ifndef SHARE_VM_CLASSFILE_JAVACLASSES_HPP 26 #define SHARE_VM_CLASSFILE_JAVACLASSES_HPP 27 28 #include "classfile/systemDictionary.hpp" 29 #include "jvmtifiles/jvmti.h" 30 #include "oops/oop.hpp" 31 #include "oops/methodOop.hpp" 32 #include "runtime/os.hpp" 33 #include "utilities/utf8.hpp" 34 35 // Interface for manipulating the basic Java classes. 36 // 37 // All dependencies on layout of actual Java classes should be kept here. 38 // If the layout of any of the classes above changes the offsets must be adjusted. 39 // 40 // For most classes we hardwire the offsets for performance reasons. In certain 41 // cases (e.g. java.security.AccessControlContext) we compute the offsets at 42 // startup since the layout here differs between JDK1.2 and JDK1.3. 43 // 44 // Note that fields (static and non-static) are arranged with oops before non-oops 45 // on a per class basis. The offsets below have to reflect this ordering. 46 // 47 // When editing the layouts please update the check_offset verification code 48 // correspondingly. The names in the enums must be identical to the actual field 49 // names in order for the verification code to work. 50 51 52 // Interface to java.lang.String objects 53 54 class java_lang_String : AllStatic { 55 private: 56 static int value_offset; 57 static int offset_offset; 58 static int count_offset; 59 static int hash_offset; 60 61 static bool initialized; 62 63 static Handle basic_create(int length, bool tenured, TRAPS); 64 static Handle basic_create_from_unicode(jchar* unicode, int length, bool tenured, TRAPS); 65 66 static void set_value( oop string, typeArrayOop buffer) { 67 assert(initialized, "Must be initialized"); 68 string->obj_field_put(value_offset, (oop)buffer); 69 } 70 static void set_offset(oop string, int offset) { 71 assert(initialized, "Must be initialized"); 72 if (offset_offset > 0) { 73 string->int_field_put(offset_offset, offset); 74 } 75 } 76 static void set_count( oop string, int count) { 77 assert(initialized, "Must be initialized"); 78 if (count_offset > 0) { 79 string->int_field_put(count_offset, count); 80 } 81 } 82 83 public: 84 static void compute_offsets(); 85 86 // Instance creation 87 static Handle create_from_unicode(jchar* unicode, int len, TRAPS); 88 static Handle create_tenured_from_unicode(jchar* unicode, int len, TRAPS); 89 static oop create_oop_from_unicode(jchar* unicode, int len, TRAPS); 90 static Handle create_from_str(const char* utf8_str, TRAPS); 91 static oop create_oop_from_str(const char* utf8_str, TRAPS); 92 static Handle create_from_symbol(Symbol* symbol, TRAPS); 93 static Handle create_from_platform_dependent_str(const char* str, TRAPS); 94 static Handle char_converter(Handle java_string, jchar from_char, jchar to_char, TRAPS); 95 96 static bool has_offset_field() { 97 assert(initialized, "Must be initialized"); 98 return (offset_offset > 0); 99 } 100 101 static bool has_count_field() { 102 assert(initialized, "Must be initialized"); 103 return (count_offset > 0); 104 } 105 106 static bool has_hash_field() { 107 assert(initialized, "Must be initialized"); 108 return (hash_offset > 0); 109 } 110 111 static int value_offset_in_bytes() { 112 assert(initialized && (value_offset > 0), "Must be initialized"); 113 return value_offset; 114 } 115 static int count_offset_in_bytes() { 116 assert(initialized && (count_offset > 0), "Must be initialized"); 117 return count_offset; 118 } 119 static int offset_offset_in_bytes() { 120 assert(initialized && (offset_offset > 0), "Must be initialized"); 121 return offset_offset; 122 } 123 static int hash_offset_in_bytes() { 124 assert(initialized && (hash_offset > 0), "Must be initialized"); 125 return hash_offset; 126 } 127 128 // Accessors 129 static typeArrayOop value(oop java_string) { 130 assert(initialized && (value_offset > 0), "Must be initialized"); 131 assert(is_instance(java_string), "must be java_string"); 132 return (typeArrayOop) java_string->obj_field(value_offset); 133 } 134 static int offset(oop java_string) { 135 assert(initialized, "Must be initialized"); 136 assert(is_instance(java_string), "must be java_string"); 137 if (offset_offset > 0) { 138 return java_string->int_field(offset_offset); 139 } else { 140 return 0; 141 } 142 } 143 static int length(oop java_string) { 144 assert(initialized, "Must be initialized"); 145 assert(is_instance(java_string), "must be java_string"); 146 if (count_offset > 0) { 147 return java_string->int_field(count_offset); 148 } else { 149 return ((typeArrayOop)java_string->obj_field(value_offset))->length(); 150 } 151 } 152 static int utf8_length(oop java_string); 153 154 // String converters 155 static char* as_utf8_string(oop java_string); 156 static char* as_utf8_string(oop java_string, char* buf, int buflen); 157 static char* as_utf8_string(oop java_string, int start, int len); 158 static char* as_platform_dependent_str(Handle java_string, TRAPS); 159 static jchar* as_unicode_string(oop java_string, int& length); 160 161 // Compute the hash value for a java.lang.String object which would 162 // contain the characters passed in. 163 // 164 // As the hash value used by the String object itself, in 165 // String.hashCode(). This value is normally calculated in Java code 166 // in the String.hashCode method(), but is precomputed for String 167 // objects in the shared archive file. 168 // hash P(31) from Kernighan & Ritchie 169 // 170 // For this reason, THIS ALGORITHM MUST MATCH String.toHash(). 171 template <typename T> static unsigned int to_hash(T* s, int len) { 172 unsigned int h = 0; 173 while (len-- > 0) { 174 h = 31*h + (unsigned int) *s; 175 s++; 176 } 177 return h; 178 } 179 static unsigned int to_hash(oop java_string); 180 181 // This is the string hash code used by the StringTable, which may be 182 // the same as String.toHash or an alternate hash code. 183 static unsigned int hash_string(oop java_string); 184 185 static bool equals(oop java_string, jchar* chars, int len); 186 187 // Conversion between '.' and '/' formats 188 static Handle externalize_classname(Handle java_string, TRAPS) { return char_converter(java_string, '/', '.', THREAD); } 189 static Handle internalize_classname(Handle java_string, TRAPS) { return char_converter(java_string, '.', '/', THREAD); } 190 191 // Conversion 192 static Symbol* as_symbol(Handle java_string, TRAPS); 193 static Symbol* as_symbol_or_null(oop java_string); 194 195 // Testers 196 static bool is_instance(oop obj) { 197 return obj != NULL && obj->klass() == SystemDictionary::String_klass(); 198 } 199 200 // Debugging 201 static void print(Handle java_string, outputStream* st); 202 friend class JavaClasses; 203 }; 204 205 206 // Interface to java.lang.Class objects 207 208 #define CLASS_INJECTED_FIELDS(macro) \ 209 macro(java_lang_Class, klass, object_signature, false) \ 210 macro(java_lang_Class, resolved_constructor, object_signature, false) \ 211 macro(java_lang_Class, array_klass, object_signature, false) \ 212 macro(java_lang_Class, oop_size, int_signature, false) \ 213 macro(java_lang_Class, static_oop_field_count, int_signature, false) 214 215 class java_lang_Class : AllStatic { 216 friend class VMStructs; 217 218 private: 219 // The fake offsets are added by the class loader when java.lang.Class is loaded 220 221 static int _klass_offset; 222 static int _resolved_constructor_offset; 223 static int _array_klass_offset; 224 225 static int _oop_size_offset; 226 static int _static_oop_field_count_offset; 227 228 static int _class_loader_offset; 229 230 static bool offsets_computed; 231 static int classRedefinedCount_offset; 232 233 static void set_class_loader(oop java_class, oop class_loader); 234 public: 235 static void compute_offsets(); 236 237 // Instance creation 238 static oop create_mirror(KlassHandle k, Handle class_loader, TRAPS); 239 static oop create_mirror(KlassHandle k, TRAPS) { 240 return create_mirror(k, Handle(), THREAD); 241 } 242 243 static void fixup_mirror(KlassHandle k, TRAPS); 244 static oop create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS); 245 // Conversion 246 static klassOop as_klassOop(oop java_class); 247 static void set_klass(oop java_class, klassOop klass); 248 static BasicType as_BasicType(oop java_class, klassOop* reference_klass = NULL); 249 static BasicType as_BasicType(oop java_class, KlassHandle* reference_klass) { 250 klassOop refk_oop = NULL; 251 BasicType result = as_BasicType(java_class, &refk_oop); 252 (*reference_klass) = KlassHandle(refk_oop); 253 return result; 254 } 255 static Symbol* as_signature(oop java_class, bool intern_if_not_found, TRAPS); 256 static void print_signature(oop java_class, outputStream *st); 257 // Testing 258 static bool is_instance(oop obj) { 259 return obj != NULL && obj->klass() == SystemDictionary::Class_klass(); 260 } 261 static bool is_primitive(oop java_class); 262 static BasicType primitive_type(oop java_class); 263 static oop primitive_mirror(BasicType t); 264 // JVM_NewInstance support 265 static methodOop resolved_constructor(oop java_class); 266 static void set_resolved_constructor(oop java_class, methodOop constructor); 267 // JVM_NewArray support 268 static klassOop array_klass(oop java_class); 269 static void set_array_klass(oop java_class, klassOop klass); 270 // compiler support for class operations 271 static int klass_offset_in_bytes() { return _klass_offset; } 272 static int resolved_constructor_offset_in_bytes() { return _resolved_constructor_offset; } 273 static int array_klass_offset_in_bytes() { return _array_klass_offset; } 274 // Support for classRedefinedCount field 275 static int classRedefinedCount(oop the_class_mirror); 276 static void set_classRedefinedCount(oop the_class_mirror, int value); 277 278 static oop class_loader(oop java_class); 279 280 static int oop_size(oop java_class); 281 static void set_oop_size(oop java_class, int size); 282 static int static_oop_field_count(oop java_class); 283 static void set_static_oop_field_count(oop java_class, int size); 284 285 // Debugging 286 friend class JavaClasses; 287 friend class instanceKlass; // verification code accesses offsets 288 friend class ClassFileParser; // access to number_of_fake_fields 289 }; 290 291 // Interface to java.lang.Thread objects 292 293 class java_lang_Thread : AllStatic { 294 private: 295 // Note that for this class the layout changed between JDK1.2 and JDK1.3, 296 // so we compute the offsets at startup rather than hard-wiring them. 297 static int _name_offset; 298 static int _group_offset; 299 static int _contextClassLoader_offset; 300 static int _inheritedAccessControlContext_offset; 301 static int _priority_offset; 302 static int _eetop_offset; 303 static int _daemon_offset; 304 static int _stillborn_offset; 305 static int _stackSize_offset; 306 static int _tid_offset; 307 static int _thread_status_offset; 308 static int _park_blocker_offset; 309 static int _park_event_offset ; 310 311 static void compute_offsets(); 312 313 public: 314 // Instance creation 315 static oop create(); 316 // Returns the JavaThread associated with the thread obj 317 static JavaThread* thread(oop java_thread); 318 // Set JavaThread for instance 319 static void set_thread(oop java_thread, JavaThread* thread); 320 // Name 321 static typeArrayOop name(oop java_thread); 322 static void set_name(oop java_thread, typeArrayOop name); 323 // Priority 324 static ThreadPriority priority(oop java_thread); 325 static void set_priority(oop java_thread, ThreadPriority priority); 326 // Thread group 327 static oop threadGroup(oop java_thread); 328 // Stillborn 329 static bool is_stillborn(oop java_thread); 330 static void set_stillborn(oop java_thread); 331 // Alive (NOTE: this is not really a field, but provides the correct 332 // definition without doing a Java call) 333 static bool is_alive(oop java_thread); 334 // Daemon 335 static bool is_daemon(oop java_thread); 336 static void set_daemon(oop java_thread); 337 // Context ClassLoader 338 static oop context_class_loader(oop java_thread); 339 // Control context 340 static oop inherited_access_control_context(oop java_thread); 341 // Stack size hint 342 static jlong stackSize(oop java_thread); 343 // Thread ID 344 static jlong thread_id(oop java_thread); 345 346 // Blocker object responsible for thread parking 347 static oop park_blocker(oop java_thread); 348 349 // Pointer to type-stable park handler, encoded as jlong. 350 // Should be set when apparently null 351 // For details, see unsafe.cpp Unsafe_Unpark 352 static jlong park_event(oop java_thread); 353 static bool set_park_event(oop java_thread, jlong ptr); 354 355 // Java Thread Status for JVMTI and M&M use. 356 // This thread status info is saved in threadStatus field of 357 // java.lang.Thread java class. 358 enum ThreadStatus { 359 NEW = 0, 360 RUNNABLE = JVMTI_THREAD_STATE_ALIVE + // runnable / running 361 JVMTI_THREAD_STATE_RUNNABLE, 362 SLEEPING = JVMTI_THREAD_STATE_ALIVE + // Thread.sleep() 363 JVMTI_THREAD_STATE_WAITING + 364 JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT + 365 JVMTI_THREAD_STATE_SLEEPING, 366 IN_OBJECT_WAIT = JVMTI_THREAD_STATE_ALIVE + // Object.wait() 367 JVMTI_THREAD_STATE_WAITING + 368 JVMTI_THREAD_STATE_WAITING_INDEFINITELY + 369 JVMTI_THREAD_STATE_IN_OBJECT_WAIT, 370 IN_OBJECT_WAIT_TIMED = JVMTI_THREAD_STATE_ALIVE + // Object.wait(long) 371 JVMTI_THREAD_STATE_WAITING + 372 JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT + 373 JVMTI_THREAD_STATE_IN_OBJECT_WAIT, 374 PARKED = JVMTI_THREAD_STATE_ALIVE + // LockSupport.park() 375 JVMTI_THREAD_STATE_WAITING + 376 JVMTI_THREAD_STATE_WAITING_INDEFINITELY + 377 JVMTI_THREAD_STATE_PARKED, 378 PARKED_TIMED = JVMTI_THREAD_STATE_ALIVE + // LockSupport.park(long) 379 JVMTI_THREAD_STATE_WAITING + 380 JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT + 381 JVMTI_THREAD_STATE_PARKED, 382 BLOCKED_ON_MONITOR_ENTER = JVMTI_THREAD_STATE_ALIVE + // (re-)entering a synchronization block 383 JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER, 384 TERMINATED = JVMTI_THREAD_STATE_TERMINATED 385 }; 386 // Write thread status info to threadStatus field of java.lang.Thread. 387 static void set_thread_status(oop java_thread_oop, ThreadStatus status); 388 // Read thread status info from threadStatus field of java.lang.Thread. 389 static ThreadStatus get_thread_status(oop java_thread_oop); 390 391 static const char* thread_status_name(oop java_thread_oop); 392 393 // Debugging 394 friend class JavaClasses; 395 }; 396 397 // Interface to java.lang.ThreadGroup objects 398 399 class java_lang_ThreadGroup : AllStatic { 400 private: 401 static int _parent_offset; 402 static int _name_offset; 403 static int _threads_offset; 404 static int _groups_offset; 405 static int _maxPriority_offset; 406 static int _destroyed_offset; 407 static int _daemon_offset; 408 static int _vmAllowSuspension_offset; 409 static int _nthreads_offset; 410 static int _ngroups_offset; 411 412 static void compute_offsets(); 413 414 public: 415 // parent ThreadGroup 416 static oop parent(oop java_thread_group); 417 // name 418 static typeArrayOop name(oop java_thread_group); 419 // ("name as oop" accessor is not necessary) 420 // Number of threads in group 421 static int nthreads(oop java_thread_group); 422 // threads 423 static objArrayOop threads(oop java_thread_group); 424 // Number of threads in group 425 static int ngroups(oop java_thread_group); 426 // groups 427 static objArrayOop groups(oop java_thread_group); 428 // maxPriority in group 429 static ThreadPriority maxPriority(oop java_thread_group); 430 // Destroyed 431 static bool is_destroyed(oop java_thread_group); 432 // Daemon 433 static bool is_daemon(oop java_thread_group); 434 // vmAllowSuspension 435 static bool is_vmAllowSuspension(oop java_thread_group); 436 // Debugging 437 friend class JavaClasses; 438 }; 439 440 441 442 // Interface to java.lang.Throwable objects 443 444 class java_lang_Throwable: AllStatic { 445 friend class BacktraceBuilder; 446 447 private: 448 // Offsets 449 enum { 450 hc_backtrace_offset = 0, 451 hc_detailMessage_offset = 1, 452 hc_cause_offset = 2, // New since 1.4 453 hc_stackTrace_offset = 3 // New since 1.4 454 }; 455 enum { 456 hc_static_unassigned_stacktrace_offset = 0 // New since 1.7 457 }; 458 // Trace constants 459 enum { 460 trace_methods_offset = 0, 461 trace_bcis_offset = 1, 462 trace_next_offset = 2, 463 trace_size = 3, 464 trace_chunk_size = 32 465 }; 466 467 static int backtrace_offset; 468 static int detailMessage_offset; 469 static int cause_offset; 470 static int stackTrace_offset; 471 static int static_unassigned_stacktrace_offset; 472 473 // Printing 474 static char* print_stack_element_to_buffer(methodOop method, int bci); 475 static void print_to_stream(Handle stream, const char* str); 476 // StackTrace (programmatic access, new since 1.4) 477 static void clear_stacktrace(oop throwable); 478 // No stack trace available 479 static const char* no_stack_trace_message(); 480 // Stacktrace (post JDK 1.7.0 to allow immutability protocol to be followed) 481 static void set_stacktrace(oop throwable, oop st_element_array); 482 static oop unassigned_stacktrace(); 483 484 public: 485 // Backtrace 486 static oop backtrace(oop throwable); 487 static void set_backtrace(oop throwable, oop value); 488 // Needed by JVMTI to filter out this internal field. 489 static int get_backtrace_offset() { return backtrace_offset;} 490 static int get_detailMessage_offset() { return detailMessage_offset;} 491 // Message 492 static oop message(oop throwable); 493 static oop message(Handle throwable); 494 static void set_message(oop throwable, oop value); 495 // Print stack trace stored in exception by call-back to Java 496 // Note: this is no longer used in Merlin, but we still suppport 497 // it for compatibility. 498 static void print_stack_trace(oop throwable, oop print_stream); 499 static void print_stack_element(Handle stream, methodOop method, int bci); 500 static void print_stack_element(outputStream *st, methodOop method, int bci); 501 static void print_stack_usage(Handle stream); 502 503 // Allocate space for backtrace (created but stack trace not filled in) 504 static void allocate_backtrace(Handle throwable, TRAPS); 505 // Fill in current stack trace for throwable with preallocated backtrace (no GC) 506 static void fill_in_stack_trace_of_preallocated_backtrace(Handle throwable); 507 // Fill in current stack trace, can cause GC 508 static void fill_in_stack_trace(Handle throwable, methodHandle method, TRAPS); 509 static void fill_in_stack_trace(Handle throwable, methodHandle method = methodHandle()); 510 // Programmatic access to stack trace 511 static oop get_stack_trace_element(oop throwable, int index, TRAPS); 512 static int get_stack_trace_depth(oop throwable, TRAPS); 513 // Printing 514 static void print(oop throwable, outputStream* st); 515 static void print(Handle throwable, outputStream* st); 516 static void print_stack_trace(oop throwable, outputStream* st); 517 // Debugging 518 friend class JavaClasses; 519 }; 520 521 522 // Interface to java.lang.reflect.AccessibleObject objects 523 524 class java_lang_reflect_AccessibleObject: AllStatic { 525 private: 526 // Note that to reduce dependencies on the JDK we compute these 527 // offsets at run-time. 528 static int override_offset; 529 530 static void compute_offsets(); 531 532 public: 533 // Accessors 534 static jboolean override(oop reflect); 535 static void set_override(oop reflect, jboolean value); 536 537 // Debugging 538 friend class JavaClasses; 539 }; 540 541 542 // Interface to java.lang.reflect.Method objects 543 544 class java_lang_reflect_Method : public java_lang_reflect_AccessibleObject { 545 private: 546 // Note that to reduce dependencies on the JDK we compute these 547 // offsets at run-time. 548 static int clazz_offset; 549 static int name_offset; 550 static int returnType_offset; 551 static int parameterTypes_offset; 552 static int exceptionTypes_offset; 553 static int slot_offset; 554 static int modifiers_offset; 555 static int signature_offset; 556 static int annotations_offset; 557 static int parameter_annotations_offset; 558 static int annotation_default_offset; 559 560 static void compute_offsets(); 561 562 public: 563 // Allocation 564 static Handle create(TRAPS); 565 566 // Accessors 567 static oop clazz(oop reflect); 568 static void set_clazz(oop reflect, oop value); 569 570 static oop name(oop method); 571 static void set_name(oop method, oop value); 572 573 static oop return_type(oop method); 574 static void set_return_type(oop method, oop value); 575 576 static oop parameter_types(oop method); 577 static void set_parameter_types(oop method, oop value); 578 579 static oop exception_types(oop method); 580 static void set_exception_types(oop method, oop value); 581 582 static int slot(oop reflect); 583 static void set_slot(oop reflect, int value); 584 585 static int modifiers(oop method); 586 static void set_modifiers(oop method, int value); 587 588 static bool has_signature_field(); 589 static oop signature(oop method); 590 static void set_signature(oop method, oop value); 591 592 static bool has_annotations_field(); 593 static oop annotations(oop method); 594 static void set_annotations(oop method, oop value); 595 596 static bool has_parameter_annotations_field(); 597 static oop parameter_annotations(oop method); 598 static void set_parameter_annotations(oop method, oop value); 599 600 static bool has_annotation_default_field(); 601 static oop annotation_default(oop method); 602 static void set_annotation_default(oop method, oop value); 603 604 // Debugging 605 friend class JavaClasses; 606 }; 607 608 609 // Interface to java.lang.reflect.Constructor objects 610 611 class java_lang_reflect_Constructor : public java_lang_reflect_AccessibleObject { 612 private: 613 // Note that to reduce dependencies on the JDK we compute these 614 // offsets at run-time. 615 static int clazz_offset; 616 static int parameterTypes_offset; 617 static int exceptionTypes_offset; 618 static int slot_offset; 619 static int modifiers_offset; 620 static int signature_offset; 621 static int annotations_offset; 622 static int parameter_annotations_offset; 623 624 static void compute_offsets(); 625 626 public: 627 // Allocation 628 static Handle create(TRAPS); 629 630 // Accessors 631 static oop clazz(oop reflect); 632 static void set_clazz(oop reflect, oop value); 633 634 static oop parameter_types(oop constructor); 635 static void set_parameter_types(oop constructor, oop value); 636 637 static oop exception_types(oop constructor); 638 static void set_exception_types(oop constructor, oop value); 639 640 static int slot(oop reflect); 641 static void set_slot(oop reflect, int value); 642 643 static int modifiers(oop constructor); 644 static void set_modifiers(oop constructor, int value); 645 646 static bool has_signature_field(); 647 static oop signature(oop constructor); 648 static void set_signature(oop constructor, oop value); 649 650 static bool has_annotations_field(); 651 static oop annotations(oop constructor); 652 static void set_annotations(oop constructor, oop value); 653 654 static bool has_parameter_annotations_field(); 655 static oop parameter_annotations(oop method); 656 static void set_parameter_annotations(oop method, oop value); 657 658 // Debugging 659 friend class JavaClasses; 660 }; 661 662 663 // Interface to java.lang.reflect.Field objects 664 665 class java_lang_reflect_Field : public java_lang_reflect_AccessibleObject { 666 private: 667 // Note that to reduce dependencies on the JDK we compute these 668 // offsets at run-time. 669 static int clazz_offset; 670 static int name_offset; 671 static int type_offset; 672 static int slot_offset; 673 static int modifiers_offset; 674 static int signature_offset; 675 static int annotations_offset; 676 677 static void compute_offsets(); 678 679 public: 680 // Allocation 681 static Handle create(TRAPS); 682 683 // Accessors 684 static oop clazz(oop reflect); 685 static void set_clazz(oop reflect, oop value); 686 687 static oop name(oop field); 688 static void set_name(oop field, oop value); 689 690 static oop type(oop field); 691 static void set_type(oop field, oop value); 692 693 static int slot(oop reflect); 694 static void set_slot(oop reflect, int value); 695 696 static int modifiers(oop field); 697 static void set_modifiers(oop field, int value); 698 699 static bool has_signature_field(); 700 static oop signature(oop constructor); 701 static void set_signature(oop constructor, oop value); 702 703 static bool has_annotations_field(); 704 static oop annotations(oop constructor); 705 static void set_annotations(oop constructor, oop value); 706 707 static bool has_parameter_annotations_field(); 708 static oop parameter_annotations(oop method); 709 static void set_parameter_annotations(oop method, oop value); 710 711 static bool has_annotation_default_field(); 712 static oop annotation_default(oop method); 713 static void set_annotation_default(oop method, oop value); 714 715 // Debugging 716 friend class JavaClasses; 717 }; 718 719 // Interface to sun.reflect.ConstantPool objects 720 class sun_reflect_ConstantPool { 721 private: 722 // Note that to reduce dependencies on the JDK we compute these 723 // offsets at run-time. 724 static int _cp_oop_offset; 725 726 static void compute_offsets(); 727 728 public: 729 // Allocation 730 static Handle create(TRAPS); 731 732 // Accessors 733 static oop cp_oop(oop reflect); 734 static void set_cp_oop(oop reflect, oop value); 735 static int cp_oop_offset() { 736 return _cp_oop_offset; 737 } 738 739 // Debugging 740 friend class JavaClasses; 741 }; 742 743 // Interface to sun.reflect.UnsafeStaticFieldAccessorImpl objects 744 class sun_reflect_UnsafeStaticFieldAccessorImpl { 745 private: 746 static int _base_offset; 747 static void compute_offsets(); 748 749 public: 750 static int base_offset() { 751 return _base_offset; 752 } 753 754 // Debugging 755 friend class JavaClasses; 756 }; 757 758 // Interface to java.lang primitive type boxing objects: 759 // - java.lang.Boolean 760 // - java.lang.Character 761 // - java.lang.Float 762 // - java.lang.Double 763 // - java.lang.Byte 764 // - java.lang.Short 765 // - java.lang.Integer 766 // - java.lang.Long 767 768 // This could be separated out into 8 individual classes. 769 770 class java_lang_boxing_object: AllStatic { 771 private: 772 enum { 773 hc_value_offset = 0 774 }; 775 static int value_offset; 776 static int long_value_offset; 777 778 static oop initialize_and_allocate(BasicType type, TRAPS); 779 public: 780 // Allocation. Returns a boxed value, or NULL for invalid type. 781 static oop create(BasicType type, jvalue* value, TRAPS); 782 // Accessors. Returns the basic type being boxed, or T_ILLEGAL for invalid oop. 783 static BasicType get_value(oop box, jvalue* value); 784 static BasicType set_value(oop box, jvalue* value); 785 static BasicType basic_type(oop box); 786 static bool is_instance(oop box) { return basic_type(box) != T_ILLEGAL; } 787 static bool is_instance(oop box, BasicType type) { return basic_type(box) == type; } 788 static void print(oop box, outputStream* st) { jvalue value; print(get_value(box, &value), &value, st); } 789 static void print(BasicType type, jvalue* value, outputStream* st); 790 791 static int value_offset_in_bytes(BasicType type) { 792 return ( type == T_LONG || type == T_DOUBLE ) ? long_value_offset : 793 value_offset; 794 } 795 796 // Debugging 797 friend class JavaClasses; 798 }; 799 800 801 802 // Interface to java.lang.ref.Reference objects 803 804 class java_lang_ref_Reference: AllStatic { 805 public: 806 enum { 807 hc_referent_offset = 0, 808 hc_queue_offset = 1, 809 hc_next_offset = 2, 810 hc_discovered_offset = 3 // Is not last, see SoftRefs. 811 }; 812 enum { 813 hc_static_lock_offset = 0, 814 hc_static_pending_offset = 1 815 }; 816 817 static int referent_offset; 818 static int queue_offset; 819 static int next_offset; 820 static int discovered_offset; 821 static int static_lock_offset; 822 static int static_pending_offset; 823 static int number_of_fake_oop_fields; 824 825 // Accessors 826 static oop referent(oop ref) { 827 return ref->obj_field(referent_offset); 828 } 829 static void set_referent(oop ref, oop value) { 830 ref->obj_field_put(referent_offset, value); 831 } 832 static void set_referent_raw(oop ref, oop value) { 833 ref->obj_field_put_raw(referent_offset, value); 834 } 835 static HeapWord* referent_addr(oop ref) { 836 return ref->obj_field_addr<HeapWord>(referent_offset); 837 } 838 static oop next(oop ref) { 839 return ref->obj_field(next_offset); 840 } 841 static void set_next(oop ref, oop value) { 842 ref->obj_field_put(next_offset, value); 843 } 844 static void set_next_raw(oop ref, oop value) { 845 ref->obj_field_put_raw(next_offset, value); 846 } 847 static HeapWord* next_addr(oop ref) { 848 return ref->obj_field_addr<HeapWord>(next_offset); 849 } 850 static oop discovered(oop ref) { 851 return ref->obj_field(discovered_offset); 852 } 853 static void set_discovered(oop ref, oop value) { 854 ref->obj_field_put(discovered_offset, value); 855 } 856 static void set_discovered_raw(oop ref, oop value) { 857 ref->obj_field_put_raw(discovered_offset, value); 858 } 859 static HeapWord* discovered_addr(oop ref) { 860 return ref->obj_field_addr<HeapWord>(discovered_offset); 861 } 862 // Accessors for statics 863 static oop pending_list_lock(); 864 static oop pending_list(); 865 866 static HeapWord* pending_list_addr(); 867 }; 868 869 870 // Interface to java.lang.ref.SoftReference objects 871 872 class java_lang_ref_SoftReference: public java_lang_ref_Reference { 873 public: 874 enum { 875 // The timestamp is a long field and may need to be adjusted for alignment. 876 hc_timestamp_offset = hc_discovered_offset + 1 877 }; 878 enum { 879 hc_static_clock_offset = 0 880 }; 881 882 static int timestamp_offset; 883 static int static_clock_offset; 884 885 // Accessors 886 static jlong timestamp(oop ref); 887 888 // Accessors for statics 889 static jlong clock(); 890 static void set_clock(jlong value); 891 }; 892 893 894 // Interface to java.lang.invoke.MethodHandle objects 895 896 class MethodHandleEntry; 897 898 class java_lang_invoke_MethodHandle: AllStatic { 899 friend class JavaClasses; 900 901 private: 902 static int _type_offset; // the MethodType of this MH 903 static int _form_offset; // the LambdaForm of this MH 904 905 static void compute_offsets(); 906 907 public: 908 // Accessors 909 static oop type(oop mh); 910 static void set_type(oop mh, oop mtype); 911 912 static oop form(oop mh); 913 static void set_form(oop mh, oop lform); 914 915 // Testers 916 static bool is_subclass(klassOop klass) { 917 return Klass::cast(klass)->is_subclass_of(SystemDictionary::MethodHandle_klass()); 918 } 919 static bool is_instance(oop obj) { 920 return obj != NULL && is_subclass(obj->klass()); 921 } 922 923 // Accessors for code generation: 924 static int type_offset_in_bytes() { return _type_offset; } 925 static int form_offset_in_bytes() { return _form_offset; } 926 }; 927 928 // Interface to java.lang.invoke.DirectMethodHandle objects 929 930 class java_lang_invoke_DirectMethodHandle: AllStatic { 931 friend class JavaClasses; 932 933 private: 934 static int _member_offset; // the MemberName of this DMH 935 936 static void compute_offsets(); 937 938 public: 939 // Accessors 940 static oop member(oop mh); 941 942 // Testers 943 static bool is_subclass(klassOop klass) { 944 return Klass::cast(klass)->is_subclass_of(SystemDictionary::DirectMethodHandle_klass()); 945 } 946 static bool is_instance(oop obj) { 947 return obj != NULL && is_subclass(obj->klass()); 948 } 949 950 // Accessors for code generation: 951 static int member_offset_in_bytes() { return _member_offset; } 952 }; 953 954 // Interface to java.lang.invoke.LambdaForm objects 955 // (These are a private interface for managing adapter code generation.) 956 957 class java_lang_invoke_LambdaForm: AllStatic { 958 friend class JavaClasses; 959 960 private: 961 static int _vmentry_offset; // type is MemberName 962 963 static void compute_offsets(); 964 965 public: 966 // Accessors 967 static oop vmentry(oop lform); 968 static void set_vmentry(oop lform, oop invoker); 969 970 // Testers 971 static bool is_subclass(klassOop klass) { 972 return SystemDictionary::LambdaForm_klass() != NULL && 973 Klass::cast(klass)->is_subclass_of(SystemDictionary::LambdaForm_klass()); 974 } 975 static bool is_instance(oop obj) { 976 return obj != NULL && is_subclass(obj->klass()); 977 } 978 979 // Accessors for code generation: 980 static int vmentry_offset_in_bytes() { return _vmentry_offset; } 981 }; 982 983 984 // Interface to java.lang.invoke.MemberName objects 985 // (These are a private interface for Java code to query the class hierarchy.) 986 987 #define MEMBERNAME_INJECTED_FIELDS(macro) \ 988 macro(java_lang_invoke_MemberName, vmindex, intptr_signature, false) \ 989 macro(java_lang_invoke_MemberName, vmtarget, object_signature, false) 990 991 class java_lang_invoke_MemberName: AllStatic { 992 friend class JavaClasses; 993 994 private: 995 // From java.lang.invoke.MemberName: 996 // private Class<?> clazz; // class in which the method is defined 997 // private String name; // may be null if not yet materialized 998 // private Object type; // may be null if not yet materialized 999 // private int flags; // modifier bits; see reflect.Modifier 1000 // private Object vmtarget; // VM-specific target value 1001 // private intptr_t vmindex; // member index within class or interface 1002 static int _clazz_offset; 1003 static int _name_offset; 1004 static int _type_offset; 1005 static int _flags_offset; 1006 static int _vmtarget_offset; 1007 static int _vmindex_offset; 1008 1009 static void compute_offsets(); 1010 1011 public: 1012 // Accessors 1013 static oop clazz(oop mname); 1014 static void set_clazz(oop mname, oop clazz); 1015 1016 static oop type(oop mname); 1017 static void set_type(oop mname, oop type); 1018 1019 static oop name(oop mname); 1020 static void set_name(oop mname, oop name); 1021 1022 static int flags(oop mname); 1023 static void set_flags(oop mname, int flags); 1024 1025 static oop vmtarget(oop mname); 1026 static void set_vmtarget(oop mname, oop target); 1027 static void adjust_vmtarget(oop mname, methodOop old_method, methodOop new_method, 1028 bool* trace_name_printed); 1029 1030 static intptr_t vmindex(oop mname); 1031 static void set_vmindex(oop mname, intptr_t index); 1032 1033 // Testers 1034 static bool is_subclass(klassOop klass) { 1035 return Klass::cast(klass)->is_subclass_of(SystemDictionary::MemberName_klass()); 1036 } 1037 static bool is_instance(oop obj) { 1038 return obj != NULL && is_subclass(obj->klass()); 1039 } 1040 1041 static bool is_method(oop obj); 1042 1043 // Relevant integer codes (keep these in synch. with MethodHandleNatives.Constants): 1044 enum { 1045 MN_IS_METHOD = 0x00010000, // method (not constructor) 1046 MN_IS_CONSTRUCTOR = 0x00020000, // constructor 1047 MN_IS_FIELD = 0x00040000, // field 1048 MN_IS_TYPE = 0x00080000, // nested type 1049 MN_REFERENCE_KIND_SHIFT = 24, // refKind 1050 MN_REFERENCE_KIND_MASK = 0x0F000000 >> MN_REFERENCE_KIND_SHIFT, 1051 // The SEARCH_* bits are not for MN.flags but for the matchFlags argument of MHN.getMembers: 1052 MN_SEARCH_SUPERCLASSES = 0x00100000, // walk super classes 1053 MN_SEARCH_INTERFACES = 0x00200000 // walk implemented interfaces 1054 }; 1055 1056 // Accessors for code generation: 1057 static int clazz_offset_in_bytes() { return _clazz_offset; } 1058 static int type_offset_in_bytes() { return _type_offset; } 1059 static int name_offset_in_bytes() { return _name_offset; } 1060 static int flags_offset_in_bytes() { return _flags_offset; } 1061 static int vmtarget_offset_in_bytes() { return _vmtarget_offset; } 1062 static int vmindex_offset_in_bytes() { return _vmindex_offset; } 1063 }; 1064 1065 1066 // Interface to java.lang.invoke.MethodType objects 1067 1068 class java_lang_invoke_MethodType: AllStatic { 1069 friend class JavaClasses; 1070 1071 private: 1072 static int _rtype_offset; 1073 static int _ptypes_offset; 1074 1075 static void compute_offsets(); 1076 1077 public: 1078 // Accessors 1079 static oop rtype(oop mt); 1080 static objArrayOop ptypes(oop mt); 1081 1082 static oop ptype(oop mt, int index); 1083 static int ptype_count(oop mt); 1084 1085 static int ptype_slot_count(oop mt); // extra counts for long/double 1086 static int rtype_slot_count(oop mt); // extra counts for long/double 1087 1088 static Symbol* as_signature(oop mt, bool intern_if_not_found, TRAPS); 1089 static void print_signature(oop mt, outputStream* st); 1090 1091 static bool is_instance(oop obj) { 1092 return obj != NULL && obj->klass() == SystemDictionary::MethodType_klass(); 1093 } 1094 1095 static bool equals(oop mt1, oop mt2); 1096 1097 // Accessors for code generation: 1098 static int rtype_offset_in_bytes() { return _rtype_offset; } 1099 static int ptypes_offset_in_bytes() { return _ptypes_offset; } 1100 }; 1101 1102 1103 // Interface to java.lang.invoke.CallSite objects 1104 1105 class java_lang_invoke_CallSite: AllStatic { 1106 friend class JavaClasses; 1107 1108 private: 1109 static int _target_offset; 1110 1111 static void compute_offsets(); 1112 1113 public: 1114 // Accessors 1115 static oop target( oop site) { return site->obj_field( _target_offset); } 1116 static void set_target( oop site, oop target) { site->obj_field_put( _target_offset, target); } 1117 1118 static volatile oop target_volatile(oop site) { return site->obj_field_volatile( _target_offset); } 1119 static void set_target_volatile(oop site, oop target) { site->obj_field_put_volatile(_target_offset, target); } 1120 1121 // Testers 1122 static bool is_subclass(klassOop klass) { 1123 return Klass::cast(klass)->is_subclass_of(SystemDictionary::CallSite_klass()); 1124 } 1125 static bool is_instance(oop obj) { 1126 return obj != NULL && is_subclass(obj->klass()); 1127 } 1128 1129 // Accessors for code generation: 1130 static int target_offset_in_bytes() { return _target_offset; } 1131 }; 1132 1133 1134 // Interface to java.security.AccessControlContext objects 1135 1136 class java_security_AccessControlContext: AllStatic { 1137 private: 1138 // Note that for this class the layout changed between JDK1.2 and JDK1.3, 1139 // so we compute the offsets at startup rather than hard-wiring them. 1140 static int _context_offset; 1141 static int _privilegedContext_offset; 1142 static int _isPrivileged_offset; 1143 static int _isAuthorized_offset; 1144 1145 static void compute_offsets(); 1146 public: 1147 static oop create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS); 1148 1149 static bool is_authorized(Handle context); 1150 1151 // Debugging/initialization 1152 friend class JavaClasses; 1153 }; 1154 1155 1156 // Interface to java.lang.ClassLoader objects 1157 1158 class java_lang_ClassLoader : AllStatic { 1159 private: 1160 enum { 1161 hc_parent_offset = 0 1162 }; 1163 1164 static bool offsets_computed; 1165 static int parent_offset; 1166 static int parallelCapable_offset; 1167 1168 static void compute_offsets(); 1169 1170 public: 1171 static oop parent(oop loader); 1172 static bool isAncestor(oop loader, oop cl); 1173 1174 // Support for parallelCapable field 1175 static bool parallelCapable(oop the_class_mirror); 1176 1177 static bool is_trusted_loader(oop loader); 1178 1179 // Fix for 4474172 1180 static oop non_reflection_class_loader(oop loader); 1181 1182 // Testers 1183 static bool is_subclass(klassOop klass) { 1184 return Klass::cast(klass)->is_subclass_of(SystemDictionary::ClassLoader_klass()); 1185 } 1186 static bool is_instance(oop obj) { 1187 return obj != NULL && is_subclass(obj->klass()); 1188 } 1189 1190 // Debugging 1191 friend class JavaClasses; 1192 }; 1193 1194 1195 // Interface to java.lang.System objects 1196 1197 class java_lang_System : AllStatic { 1198 private: 1199 enum { 1200 hc_static_in_offset = 0, 1201 hc_static_out_offset = 1, 1202 hc_static_err_offset = 2, 1203 hc_static_security_offset = 3 1204 }; 1205 1206 static int static_in_offset; 1207 static int static_out_offset; 1208 static int static_err_offset; 1209 static int static_security_offset; 1210 1211 public: 1212 static int in_offset_in_bytes(); 1213 static int out_offset_in_bytes(); 1214 static int err_offset_in_bytes(); 1215 1216 static bool has_security_manager(); 1217 1218 // Debugging 1219 friend class JavaClasses; 1220 }; 1221 1222 1223 // Interface to java.lang.StackTraceElement objects 1224 1225 class java_lang_StackTraceElement: AllStatic { 1226 private: 1227 enum { 1228 hc_declaringClass_offset = 0, 1229 hc_methodName_offset = 1, 1230 hc_fileName_offset = 2, 1231 hc_lineNumber_offset = 3 1232 }; 1233 1234 static int declaringClass_offset; 1235 static int methodName_offset; 1236 static int fileName_offset; 1237 static int lineNumber_offset; 1238 1239 public: 1240 // Setters 1241 static void set_declaringClass(oop element, oop value); 1242 static void set_methodName(oop element, oop value); 1243 static void set_fileName(oop element, oop value); 1244 static void set_lineNumber(oop element, int value); 1245 1246 // Create an instance of StackTraceElement 1247 static oop create(methodHandle m, int bci, TRAPS); 1248 1249 // Debugging 1250 friend class JavaClasses; 1251 }; 1252 1253 1254 // Interface to java.lang.AssertionStatusDirectives objects 1255 1256 class java_lang_AssertionStatusDirectives: AllStatic { 1257 private: 1258 enum { 1259 hc_classes_offset, 1260 hc_classEnabled_offset, 1261 hc_packages_offset, 1262 hc_packageEnabled_offset, 1263 hc_deflt_offset 1264 }; 1265 1266 static int classes_offset; 1267 static int classEnabled_offset; 1268 static int packages_offset; 1269 static int packageEnabled_offset; 1270 static int deflt_offset; 1271 1272 public: 1273 // Setters 1274 static void set_classes(oop obj, oop val); 1275 static void set_classEnabled(oop obj, oop val); 1276 static void set_packages(oop obj, oop val); 1277 static void set_packageEnabled(oop obj, oop val); 1278 static void set_deflt(oop obj, bool val); 1279 // Debugging 1280 friend class JavaClasses; 1281 }; 1282 1283 1284 class java_nio_Buffer: AllStatic { 1285 private: 1286 static int _limit_offset; 1287 1288 public: 1289 static int limit_offset(); 1290 static void compute_offsets(); 1291 }; 1292 1293 class java_util_concurrent_locks_AbstractOwnableSynchronizer : AllStatic { 1294 private: 1295 static int _owner_offset; 1296 public: 1297 static void initialize(TRAPS); 1298 static oop get_owner_threadObj(oop obj); 1299 }; 1300 1301 // Use to declare fields that need to be injected into Java classes 1302 // for the JVM to use. The name_index and signature_index are 1303 // declared in vmSymbols. The may_be_java flag is used to declare 1304 // fields that might already exist in Java but should be injected if 1305 // they don't. Otherwise the field is unconditionally injected and 1306 // the JVM uses the injected one. This is to ensure that name 1307 // collisions don't occur. In general may_be_java should be false 1308 // unless there's a good reason. 1309 1310 class InjectedField { 1311 public: 1312 const SystemDictionary::WKID klass_id; 1313 const vmSymbols::SID name_index; 1314 const vmSymbols::SID signature_index; 1315 const bool may_be_java; 1316 1317 1318 klassOop klass() const { return SystemDictionary::well_known_klass(klass_id); } 1319 Symbol* name() const { return lookup_symbol(name_index); } 1320 Symbol* signature() const { return lookup_symbol(signature_index); } 1321 1322 int compute_offset(); 1323 1324 // Find the Symbol for this index 1325 static Symbol* lookup_symbol(int symbol_index) { 1326 return vmSymbols::symbol_at((vmSymbols::SID)symbol_index); 1327 } 1328 }; 1329 1330 #define DECLARE_INJECTED_FIELD_ENUM(klass, name, signature, may_be_java) \ 1331 klass##_##name##_enum, 1332 1333 #define ALL_INJECTED_FIELDS(macro) \ 1334 CLASS_INJECTED_FIELDS(macro) \ 1335 MEMBERNAME_INJECTED_FIELDS(macro) 1336 1337 // Interface to hard-coded offset checking 1338 1339 class JavaClasses : AllStatic { 1340 private: 1341 1342 static InjectedField _injected_fields[]; 1343 1344 static bool check_offset(const char *klass_name, int offset, const char *field_name, const char* field_sig) PRODUCT_RETURN0; 1345 static bool check_static_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) PRODUCT_RETURN0; 1346 static bool check_constant(const char *klass_name, int constant, const char *field_name, const char* field_sig) PRODUCT_RETURN0; 1347 1348 public: 1349 enum InjectedFieldID { 1350 ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD_ENUM) 1351 MAX_enum 1352 }; 1353 1354 static int compute_injected_offset(InjectedFieldID id); 1355 1356 static void compute_hard_coded_offsets(); 1357 static void compute_offsets(); 1358 static void check_offsets() PRODUCT_RETURN; 1359 1360 static InjectedField* get_injected(Symbol* class_name, int* field_count); 1361 }; 1362 1363 #undef DECLARE_INJECTED_FIELD_ENUM 1364 1365 #endif // SHARE_VM_CLASSFILE_JAVACLASSES_HPP