javaClasses.cpp 144.0 KB
Newer Older
D
duke 已提交
1
/*
卓昂 已提交
2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25
#include "precompiled.hpp"
26
#include "classfile/altHashing.hpp"
27 28 29 30 31
#include "classfile/javaClasses.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/vmSymbols.hpp"
#include "code/debugInfo.hpp"
#include "code/pcDesc.hpp"
32
#include "compiler/compilerOracle.hpp"
33 34 35 36
#include "interpreter/interpreter.hpp"
#include "memory/oopFactory.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.inline.hpp"
37
#include "oops/fieldStreams.hpp"
38
#include "oops/instanceKlass.hpp"
39
#include "oops/instanceMirrorKlass.hpp"
40
#include "oops/klass.hpp"
41
#include "oops/method.hpp"
42
#include "oops/symbol.hpp"
43
#include "oops/typeArrayOop.hpp"
44
#include "prims/jvmtiRedefineClassesTrace.hpp"
45 46 47 48 49 50
#include "runtime/fieldDescriptor.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/safepoint.hpp"
51
#include "runtime/thread.inline.hpp"
52 53
#include "runtime/vframe.hpp"
#include "utilities/preserveException.hpp"
D
duke 已提交
54

55 56 57 58
#if INCLUDE_ALL_GCS
#include "gc_implementation/g1/g1TenantAllocationContext.hpp"
#endif  // INCLUDE_ALL_GCS

59 60
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java)    \
  klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum);

#define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java)           \
  { SystemDictionary::WK_KLASS_ENUM_NAME(klass), vmSymbols::VM_SYMBOL_ENUM_NAME(name##_name), vmSymbols::VM_SYMBOL_ENUM_NAME(signature), may_be_java },

InjectedField JavaClasses::_injected_fields[] = {
  ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
};

int JavaClasses::compute_injected_offset(InjectedFieldID id) {
  return _injected_fields[id].compute_offset();
}


InjectedField* JavaClasses::get_injected(Symbol* class_name, int* field_count) {
  *field_count = 0;

  vmSymbols::SID sid = vmSymbols::find_sid(class_name);
  if (sid == vmSymbols::NO_SID) {
    // Only well known classes can inject fields
    return NULL;
  }

  int count = 0;
  int start = -1;

#define LOOKUP_INJECTED_FIELD(klass, name, signature, may_be_java) \
  if (sid == vmSymbols::VM_SYMBOL_ENUM_NAME(klass)) {              \
    count++;                                                       \
    if (start == -1) start = klass##_##name##_enum;                \
  }
  ALL_INJECTED_FIELDS(LOOKUP_INJECTED_FIELD);
#undef LOOKUP_INJECTED_FIELD

  if (start != -1) {
    *field_count = count;
    return _injected_fields + start;
  }
  return NULL;
}


104
static bool find_field(InstanceKlass* ik,
105
                       Symbol* name_symbol, Symbol* signature_symbol,
106 107 108 109 110 111 112 113
                       fieldDescriptor* fd,
                       bool allow_super = false) {
  if (allow_super)
    return ik->find_field(name_symbol, signature_symbol, fd) != NULL;
  else
    return ik->find_local_field(name_symbol, signature_symbol, fd);
}

114 115 116
// Helpful routine for computing field offsets at run time rather than hardcoding them
static void
compute_offset(int &dest_offset,
117
               Klass* klass_oop, Symbol* name_symbol, Symbol* signature_symbol,
118
               bool allow_super = false) {
119
  fieldDescriptor fd;
120
  InstanceKlass* ik = InstanceKlass::cast(klass_oop);
121
  if (!find_field(ik, name_symbol, signature_symbol, &fd, allow_super)) {
122 123
    ResourceMark rm;
    tty->print_cr("Invalid layout of %s at %s", ik->external_name(), name_symbol->as_C_string());
124 125 126
#ifndef PRODUCT
    klass_oop->print();
    tty->print_cr("all fields:");
127
    for (AllFieldStream fs(InstanceKlass::cast(klass_oop)); !fs.done(); fs.next()) {
128 129 130
      tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
    }
#endif //PRODUCT
131
    vm_exit_during_initialization("Invalid layout of preloaded class: use -XX:+TraceClassLoading to see the origin of the problem class");
132 133
  }
  dest_offset = fd.offset();
D
duke 已提交
134 135 136
}

// Same as above but for "optional" offsets that might not be present in certain JDK versions
137 138
static void
compute_optional_offset(int& dest_offset,
139
                        Klass* klass_oop, Symbol* name_symbol, Symbol* signature_symbol,
140
                        bool allow_super = false) {
141
  fieldDescriptor fd;
142
  InstanceKlass* ik = InstanceKlass::cast(klass_oop);
143
  if (find_field(ik, name_symbol, signature_symbol, &fd, allow_super)) {
144 145
    dest_offset = fd.offset();
  }
D
duke 已提交
146 147
}

148

K
kvn 已提交
149 150 151 152 153 154 155 156 157 158
int java_lang_String::value_offset  = 0;
int java_lang_String::offset_offset = 0;
int java_lang_String::count_offset  = 0;
int java_lang_String::hash_offset   = 0;

bool java_lang_String::initialized  = false;

void java_lang_String::compute_offsets() {
  assert(!initialized, "offsets should be initialized only once");

159
  Klass* k = SystemDictionary::String_klass();
K
kvn 已提交
160 161 162 163 164 165 166 167
  compute_offset(value_offset,           k, vmSymbols::value_name(),  vmSymbols::char_array_signature());
  compute_optional_offset(offset_offset, k, vmSymbols::offset_name(), vmSymbols::int_signature());
  compute_optional_offset(count_offset,  k, vmSymbols::count_name(),  vmSymbols::int_signature());
  compute_optional_offset(hash_offset,   k, vmSymbols::hash_name(),   vmSymbols::int_signature());

  initialized = true;
}

168
Handle java_lang_String::basic_create(int length, TRAPS) {
K
kvn 已提交
169
  assert(initialized, "Must be initialized");
D
duke 已提交
170 171 172
  // Create the String object first, so there's a chance that the String
  // and the char array it points to end up in the same cache line.
  oop obj;
173
  obj = InstanceKlass::cast(SystemDictionary::String_klass())->allocate_instance(CHECK_NH);
D
duke 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

  // Create the char array.  The String object must be handlized here
  // because GC can happen as a result of the allocation attempt.
  Handle h_obj(THREAD, obj);
  typeArrayOop buffer;
    buffer = oopFactory::new_charArray(length, CHECK_NH);

  // Point the String at the char array
  obj = h_obj();
  set_value(obj, buffer);
  // No need to zero the offset, allocation zero'ed the entire String object
  assert(offset(obj) == 0, "initial String offset should be zero");
//set_offset(obj, 0);
  set_count(obj, length);

  return h_obj;
}

192 193
Handle java_lang_String::create_from_unicode(jchar* unicode, int length, TRAPS) {
  Handle h_obj = basic_create(length, CHECK_NH);
D
duke 已提交
194 195 196 197 198 199 200 201
  typeArrayOop buffer = value(h_obj());
  for (int index = 0; index < length; index++) {
    buffer->char_at_put(index, unicode[index]);
  }
  return h_obj;
}

oop java_lang_String::create_oop_from_unicode(jchar* unicode, int length, TRAPS) {
202
  Handle h_obj = create_from_unicode(unicode, length, CHECK_0);
D
duke 已提交
203 204 205 206 207 208 209 210
  return h_obj();
}

Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) {
  if (utf8_str == NULL) {
    return Handle();
  }
  int length = UTF8::unicode_length(utf8_str);
211
  Handle h_obj = basic_create(length, CHECK_NH);
D
duke 已提交
212 213 214 215 216 217 218 219 220 221 222
  if (length > 0) {
    UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
  }
  return h_obj;
}

oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) {
  Handle h_obj = create_from_str(utf8_str, CHECK_0);
  return h_obj();
}

223
Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) {
D
duke 已提交
224
  int length = UTF8::unicode_length((char*)symbol->bytes(), symbol->utf8_length());
225
  Handle h_obj = basic_create(length, CHECK_NH);
D
duke 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  if (length > 0) {
    UTF8::convert_to_unicode((char*)symbol->bytes(), value(h_obj())->char_at_addr(0), length);
  }
  return h_obj;
}

// Converts a C string to a Java String based on current encoding
Handle java_lang_String::create_from_platform_dependent_str(const char* str, TRAPS) {
  assert(str != NULL, "bad arguments");

  typedef jstring (*to_java_string_fn_t)(JNIEnv*, const char *);
  static to_java_string_fn_t _to_java_string_fn = NULL;

  if (_to_java_string_fn == NULL) {
    void *lib_handle = os::native_java_library();
241
    _to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "NewStringPlatform"));
D
duke 已提交
242 243 244 245 246 247 248 249 250
    if (_to_java_string_fn == NULL) {
      fatal("NewStringPlatform missing");
    }
  }

  jstring js = NULL;
  { JavaThread* thread = (JavaThread*)THREAD;
    assert(thread->is_Java_thread(), "must be java thread");
    HandleMark hm(thread);
251
    ThreadToNativeFromVM ttn(thread);
D
duke 已提交
252 253 254 255 256
    js = (_to_java_string_fn)(thread->jni_environment(), str);
  }
  return Handle(THREAD, JNIHandles::resolve(js));
}

257 258 259 260 261 262 263 264 265
// Converts a Java String to a native C string that can be used for
// native OS calls.
char* java_lang_String::as_platform_dependent_str(Handle java_string, TRAPS) {

  typedef char* (*to_platform_string_fn_t)(JNIEnv*, jstring, bool*);
  static to_platform_string_fn_t _to_platform_string_fn = NULL;

  if (_to_platform_string_fn == NULL) {
    void *lib_handle = os::native_java_library();
266
    _to_platform_string_fn = CAST_TO_FN_PTR(to_platform_string_fn_t, os::dll_lookup(lib_handle, "GetStringPlatformChars"));
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    if (_to_platform_string_fn == NULL) {
      fatal("GetStringPlatformChars missing");
    }
  }

  char *native_platform_string;
  { JavaThread* thread = (JavaThread*)THREAD;
    assert(thread->is_Java_thread(), "must be java thread");
    JNIEnv *env = thread->jni_environment();
    jstring js = (jstring) JNIHandles::make_local(env, java_string());
    bool is_copy;
    HandleMark hm(thread);
    ThreadToNativeFromVM ttn(thread);
    native_platform_string = (_to_platform_string_fn)(env, js, &is_copy);
    assert(is_copy == JNI_TRUE, "is_copy value changed");
    JNIHandles::destroy_local(js);
  }
  return native_platform_string;
}

D
duke 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
Handle java_lang_String::char_converter(Handle java_string, jchar from_char, jchar to_char, TRAPS) {
  oop          obj    = java_string();
  // Typical usage is to convert all '/' to '.' in string.
  typeArrayOop value  = java_lang_String::value(obj);
  int          offset = java_lang_String::offset(obj);
  int          length = java_lang_String::length(obj);

  // First check if any from_char exist
  int index; // Declared outside, used later
  for (index = 0; index < length; index++) {
    if (value->char_at(index + offset) == from_char) {
      break;
    }
  }
  if (index == length) {
    // No from_char, so do not copy.
    return java_string;
  }

  // Create new UNICODE buffer. Must handlize value because GC
  // may happen during String and char array creation.
  typeArrayHandle h_value(THREAD, value);
309
  Handle string = basic_create(length, CHECK_NH);
D
duke 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

  typeArrayOop from_buffer = h_value();
  typeArrayOop to_buffer   = java_lang_String::value(string());

  // Copy contents
  for (index = 0; index < length; index++) {
    jchar c = from_buffer->char_at(index + offset);
    if (c == from_char) {
      c = to_char;
    }
    to_buffer->char_at_put(index, c);
  }
  return string;
}

325
jchar* java_lang_String::as_unicode_string(oop java_string, int& length, TRAPS) {
D
duke 已提交
326 327 328 329
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
               length = java_lang_String::length(java_string);

330 331 332 333 334 335 336
  jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length);
  if (result != NULL) {
    for (int index = 0; index < length; index++) {
      result[index] = value->char_at(index + offset);
    }
  } else {
    THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string");
D
duke 已提交
337 338 339 340
  }
  return result;
}

341
unsigned int java_lang_String::hash_code(oop java_string) {
342
  int          length = java_lang_String::length(java_string);
343
  // Zero length string will hash to zero with String.hashCode() function.
344 345
  if (length == 0) return 0;

346 347
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
348
  return java_lang_String::hash_code(value->char_at_addr(offset), length);
349 350
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
char* java_lang_String::as_quoted_ascii(oop java_string) {
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);

  jchar* base = (length == 0) ? NULL : value->char_at_addr(offset);
  if (base == NULL) return NULL;

  int result_length = UNICODE::quoted_ascii_length(base, length) + 1;
  char* result = NEW_RESOURCE_ARRAY(char, result_length);
  UNICODE::as_quoted_ascii(base, length, result, result_length);
  assert(result_length >= length + 1, "must not be shorter");
  assert(result_length == (int)strlen(result) + 1, "must match");
  return result;
}

367
unsigned int java_lang_String::hash_string(oop java_string) {
368
  int          length = java_lang_String::length(java_string);
369 370 371 372
  // Zero length string doesn't hash necessarily hash to zero.
  if (length == 0) {
    return StringTable::hash_string(NULL, 0);
  }
373

374 375 376
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  return StringTable::hash_string(value->char_at_addr(offset), length);
377 378
}

379
Symbol* java_lang_String::as_symbol(Handle java_string, TRAPS) {
D
duke 已提交
380 381 382 383
  oop          obj    = java_string();
  typeArrayOop value  = java_lang_String::value(obj);
  int          offset = java_lang_String::offset(obj);
  int          length = java_lang_String::length(obj);
384
  jchar* base = (length == 0) ? NULL : value->char_at_addr(offset);
385 386
  Symbol* sym = SymbolTable::lookup_unicode(base, length, THREAD);
  return sym;
387
}
D
duke 已提交
388

389
Symbol* java_lang_String::as_symbol_or_null(oop java_string) {
390 391 392
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);
393
  jchar* base = (length == 0) ? NULL : value->char_at_addr(offset);
394
  return SymbolTable::probe_unicode(base, length);
D
duke 已提交
395 396
}

397

D
duke 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
int java_lang_String::utf8_length(oop java_string) {
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);
  jchar* position = (length == 0) ? NULL : value->char_at_addr(offset);
  return UNICODE::utf8_length(position, length);
}

char* java_lang_String::as_utf8_string(oop java_string) {
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);
  jchar* position = (length == 0) ? NULL : value->char_at_addr(offset);
  return UNICODE::as_utf8(position, length);
}

414 415 416 417 418 419 420 421
char* java_lang_String::as_utf8_string(oop java_string, char* buf, int buflen) {
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);
  jchar* position = (length == 0) ? NULL : value->char_at_addr(offset);
  return UNICODE::as_utf8(position, length, buf, buflen);
}

D
duke 已提交
422 423 424 425 426 427 428 429 430 431
char* java_lang_String::as_utf8_string(oop java_string, int start, int len) {
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);
  assert(start + len <= length, "just checking");
  jchar* position = value->char_at_addr(offset + start);
  return UNICODE::as_utf8(position, len);
}

bool java_lang_String::equals(oop java_string, jchar* chars, int len) {
432
  assert(java_string->klass() == SystemDictionary::String_klass(),
D
duke 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
         "must be java_string");
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);
  if (length != len) {
    return false;
  }
  for (int i = 0; i < len; i++) {
    if (value->char_at(i + offset) != chars[i]) {
      return false;
    }
  }
  return true;
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
bool java_lang_String::equals(oop str1, oop str2) {
  assert(str1->klass() == SystemDictionary::String_klass(),
         "must be java String");
  assert(str2->klass() == SystemDictionary::String_klass(),
         "must be java String");
  typeArrayOop value1  = java_lang_String::value(str1);
  int          offset1 = java_lang_String::offset(str1);
  int          length1 = java_lang_String::length(str1);
  typeArrayOop value2  = java_lang_String::value(str2);
  int          offset2 = java_lang_String::offset(str2);
  int          length2 = java_lang_String::length(str2);

  if (length1 != length2) {
    return false;
  }
  for (int i = 0; i < length1; i++) {
    if (value1->char_at(i + offset1) != value2->char_at(i + offset2)) {
      return false;
    }
  }
  return true;
}

471 472 473 474 475
void java_lang_String::print(oop java_string, outputStream* st) {
  assert(java_string->klass() == SystemDictionary::String_klass(), "must be java_string");
  typeArrayOop value  = java_lang_String::value(java_string);
  int          offset = java_lang_String::offset(java_string);
  int          length = java_lang_String::length(java_string);
D
duke 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

  int end = MIN2(length, 100);
  if (value == NULL) {
    // This can happen if, e.g., printing a String
    // object before its initializer has been called
    st->print_cr("NULL");
  } else {
    st->print("\"");
    for (int index = 0; index < length; index++) {
      st->print("%c", value->char_at(index + offset));
    }
    st->print("\"");
  }
}

491 492

static void initialize_static_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
  assert(mirror.not_null() && fd->is_static(), "just checking");
  if (fd->has_initial_value()) {
    BasicType t = fd->field_type();
    switch (t) {
      case T_BYTE:
        mirror()->byte_field_put(fd->offset(), fd->int_initial_value());
              break;
      case T_BOOLEAN:
        mirror()->bool_field_put(fd->offset(), fd->int_initial_value());
              break;
      case T_CHAR:
        mirror()->char_field_put(fd->offset(), fd->int_initial_value());
              break;
      case T_SHORT:
        mirror()->short_field_put(fd->offset(), fd->int_initial_value());
              break;
      case T_INT:
        mirror()->int_field_put(fd->offset(), fd->int_initial_value());
        break;
      case T_FLOAT:
        mirror()->float_field_put(fd->offset(), fd->float_initial_value());
        break;
      case T_DOUBLE:
        mirror()->double_field_put(fd->offset(), fd->double_initial_value());
        break;
      case T_LONG:
        mirror()->long_field_put(fd->offset(), fd->long_initial_value());
        break;
      case T_OBJECT:
        {
          #ifdef ASSERT
          TempNewSymbol sym = SymbolTable::new_symbol("Ljava/lang/String;", CHECK);
          assert(fd->signature() == sym, "just checking");
          #endif
          oop string = fd->string_initial_value(CHECK);
          mirror()->obj_field_put(fd->offset(), string);
        }
        break;
      default:
        THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
                  "Illegal ConstantValue attribute in class file");
    }
  }
}


void java_lang_Class::fixup_mirror(KlassHandle k, TRAPS) {
540
  assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
541

542 543
  // If the offset was read from the shared archive, it was fixed up already
  if (!k->is_shared()) {
544 545 546 547 548 549 550 551 552
    if (k->oop_is_instance()) {
      // During bootstrap, java.lang.Class wasn't loaded so static field
      // offsets were computed without the size added it.  Go back and
      // update all the static field offsets to included the size.
        for (JavaFieldStream fs(InstanceKlass::cast(k())); !fs.done(); fs.next()) {
        if (fs.access_flags().is_static()) {
          int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
          fs.set_offset(real_offset);
        }
553 554
      }
    }
555
  }
556
  create_mirror(k, Handle(NULL), Handle(NULL), CHECK);
557
}
D
duke 已提交
558

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
void java_lang_Class::initialize_mirror_fields(KlassHandle k,
                                               Handle mirror,
                                               Handle protection_domain,
                                               TRAPS) {
  // Allocate a simple java object for a lock.
  // This needs to be a java object because during class initialization
  // it can be held across a java call.
  typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
  set_init_lock(mirror(), r);

  // Set protection domain also
  set_protection_domain(mirror(), protection_domain());

  // Initialize static fields
  InstanceKlass::cast(k())->do_local_static_fields(&initialize_static_field, mirror, CHECK);
}

576 577
void java_lang_Class::create_mirror(KlassHandle k, Handle class_loader,
                                    Handle protection_domain, TRAPS) {
D
duke 已提交
578 579 580 581 582
  assert(k->java_mirror() == NULL, "should only assign mirror once");
  // Use this moment of initialization to cache modifier_flags also,
  // to support Class.getModifiers().  Instance classes recalculate
  // the cached flags after the class file is parsed, but before the
  // class is put into the system dictionary.
583
  int computed_modifiers = k->compute_modifier_flags(CHECK);
D
duke 已提交
584
  k->set_modifier_flags(computed_modifiers);
585 586 587
  // Class_klass has to be loaded because it is used to allocate
  // the mirror.
  if (SystemDictionary::Class_klass_loaded()) {
D
duke 已提交
588
    // Allocate mirror (java.lang.Class instance)
589 590 591 592 593 594
    Handle mirror = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);

    // Setup indirection from mirror->klass
    if (!k.is_null()) {
      java_lang_Class::set_klass(mirror(), k());
    }
595

596
    InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
597 598
    assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");

599 600
    java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));

D
duke 已提交
601
    // It might also have a component mirror.  This mirror must already exist.
602
    if (k->oop_is_array()) {
D
duke 已提交
603 604
      Handle comp_mirror;
      if (k->oop_is_typeArray()) {
605
        BasicType type = TypeArrayKlass::cast(k())->element_type();
D
duke 已提交
606
        comp_mirror = Universe::java_mirror(type);
607 608
      } else {
        assert(k->oop_is_objArray(), "Must be");
609
        Klass* element_klass = ObjArrayKlass::cast(k())->element_klass();
610
        assert(element_klass != NULL, "Must have an element klass");
611
        comp_mirror = element_klass->java_mirror();
D
duke 已提交
612
      }
613 614
      assert(comp_mirror.not_null(), "must have a mirror");

615
      // Two-way link between the array klass and its component mirror:
616
      ArrayKlass::cast(k())->set_component_mirror(comp_mirror());
617 618 619
      set_array_klass(comp_mirror(), k());
    } else {
      assert(k->oop_is_instance(), "Must be");
620

621 622 623 624 625 626 627 628 629 630
      initialize_mirror_fields(k, mirror, protection_domain, THREAD);
      if (HAS_PENDING_EXCEPTION) {
        // If any of the fields throws an exception like OOM remove the klass field
        // from the mirror so GC doesn't follow it after the klass has been deallocated.
        // This mirror looks like a primitive type, which logically it is because it
        // it represents no class.
        java_lang_Class::set_klass(mirror(), NULL);
        return;
      }
    }
631

632
    // set the classLoader field in the java_lang_Class instance
633
    assert(class_loader() == k->class_loader(), "should be same");
634 635
    set_class_loader(mirror(), class_loader());

636 637 638 639
    // Setup indirection from klass->mirror last
    // after any exceptions can happen during allocations.
    if (!k.is_null()) {
      k->set_java_mirror(mirror());
D
duke 已提交
640 641
    }
  } else {
642 643 644 645 646 647
    if (fixup_mirror_list() == NULL) {
      GrowableArray<Klass*>* list =
       new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
      set_fixup_mirror_list(list);
    }
    fixup_mirror_list()->push(k());
D
duke 已提交
648 649 650 651
  }
}


652
int  java_lang_Class::oop_size(oop java_class) {
653 654
  assert(_oop_size_offset != 0, "must be set");
  return java_class->int_field(_oop_size_offset);
655 656
}
void java_lang_Class::set_oop_size(oop java_class, int size) {
657 658
  assert(_oop_size_offset != 0, "must be set");
  java_class->int_field_put(_oop_size_offset, size);
659 660
}
int  java_lang_Class::static_oop_field_count(oop java_class) {
661 662
  assert(_static_oop_field_count_offset != 0, "must be set");
  return java_class->int_field(_static_oop_field_count_offset);
663 664
}
void java_lang_Class::set_static_oop_field_count(oop java_class, int size) {
665 666
  assert(_static_oop_field_count_offset != 0, "must be set");
  java_class->int_field_put(_static_oop_field_count_offset, size);
667 668
}

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
oop java_lang_Class::protection_domain(oop java_class) {
  assert(_protection_domain_offset != 0, "must be set");
  return java_class->obj_field(_protection_domain_offset);
}
void java_lang_Class::set_protection_domain(oop java_class, oop pd) {
  assert(_protection_domain_offset != 0, "must be set");
  java_class->obj_field_put(_protection_domain_offset, pd);
}

oop java_lang_Class::init_lock(oop java_class) {
  assert(_init_lock_offset != 0, "must be set");
  return java_class->obj_field(_init_lock_offset);
}
void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
  assert(_init_lock_offset != 0, "must be set");
  java_class->obj_field_put(_init_lock_offset, init_lock);
}

objArrayOop java_lang_Class::signers(oop java_class) {
  assert(_signers_offset != 0, "must be set");
  return (objArrayOop)java_class->obj_field(_signers_offset);
}
void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
  assert(_signers_offset != 0, "must be set");
  java_class->obj_field_put(_signers_offset, (oop)signers);
}


697 698 699 700 701 702 703 704 705 706 707 708
void java_lang_Class::set_class_loader(oop java_class, oop loader) {
  // jdk7 runs Queens in bootstrapping and jdk8-9 has no coordinated pushes yet.
  if (_class_loader_offset != 0) {
    java_class->obj_field_put(_class_loader_offset, loader);
  }
}

oop java_lang_Class::class_loader(oop java_class) {
  assert(_class_loader_offset != 0, "must be set");
  return java_class->obj_field(_class_loader_offset);
}

D
duke 已提交
709 710 711
oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
  // This should be improved by adding a field at the Java level or by
  // introducing a new VM klass (see comment in ClassFileParser)
712
  oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
D
duke 已提交
713
  if (type != T_VOID) {
714
    Klass* aklass = Universe::typeArrayKlassObj(type);
D
duke 已提交
715 716 717
    assert(aklass != NULL, "correct bootstrap");
    set_array_klass(java_class, aklass);
  }
718
#ifdef ASSERT
719
  InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
720 721
  assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
#endif
D
duke 已提交
722 723 724 725
  return java_class;
}


726
Klass* java_lang_Class::as_Klass(oop java_class) {
D
duke 已提交
727
  //%note memory_2
728
  assert(java_lang_Class::is_instance(java_class), "must be a Class object");
729
  Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
D
duke 已提交
730 731 732 733 734
  assert(k == NULL || k->is_klass(), "type check");
  return k;
}


735
void java_lang_Class::set_klass(oop java_class, Klass* klass) {
736
  assert(java_lang_Class::is_instance(java_class), "must be a Class object");
737
  java_class->metadata_field_put(_klass_offset, klass);
738 739 740
}


741 742
void java_lang_Class::print_signature(oop java_class, outputStream* st) {
  assert(java_lang_Class::is_instance(java_class), "must be a Class object");
743
  Symbol* name = NULL;
744 745 746 747
  bool is_instance = false;
  if (is_primitive(java_class)) {
    name = vmSymbols::type_signature(primitive_type(java_class));
  } else {
748
    Klass* k = as_Klass(java_class);
H
hseigel 已提交
749 750
    is_instance = k->oop_is_instance();
    name = k->name();
751 752 753 754 755 756 757 758 759 760
  }
  if (name == NULL) {
    st->print("<null>");
    return;
  }
  if (is_instance)  st->print("L");
  st->write((char*) name->base(), (int) name->utf8_length());
  if (is_instance)  st->print(";");
}

761
Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) {
762
  assert(java_lang_Class::is_instance(java_class), "must be a Class object");
763
  Symbol* name;
764
  if (is_primitive(java_class)) {
765 766 767 768 769
    name = vmSymbols::type_signature(primitive_type(java_class));
    // Because this can create a new symbol, the caller has to decrement
    // the refcount, so make adjustment here and below for symbols returned
    // that are not created or incremented due to a successful lookup.
    name->increment_refcount();
770
  } else {
771
    Klass* k = as_Klass(java_class);
H
hseigel 已提交
772 773
    if (!k->oop_is_instance()) {
      name = k->name();
774
      name->increment_refcount();
775 776
    } else {
      ResourceMark rm;
H
hseigel 已提交
777
      const char* sigstr = k->signature_name();
778
      int         siglen = (int) strlen(sigstr);
779 780 781 782 783
      if (!intern_if_not_found) {
        name = SymbolTable::probe(sigstr, siglen);
      } else {
        name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
      }
784 785
    }
  }
786
  return name;
787 788
}

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
// Returns the Java name for this Java mirror (Resource allocated)
// See Klass::external_name().
// For primitive type Java mirrors, its type name is returned.
const char* java_lang_Class::as_external_name(oop java_class) {
  assert(java_lang_Class::is_instance(java_class), "must be a Class object");
  const char* name = NULL;
  if (is_primitive(java_class)) {
    name = type2name(primitive_type(java_class));
  } else {
    name = as_Klass(java_class)->external_name();
  }
  if (name == NULL) {
    name = "<null>";
  }
  return name;
}
805

806 807
Klass* java_lang_Class::array_klass(oop java_class) {
  Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
H
hseigel 已提交
808
  assert(k == NULL || k->is_klass() && k->oop_is_array(), "should be array klass");
D
duke 已提交
809 810 811 812
  return k;
}


813
void java_lang_Class::set_array_klass(oop java_class, Klass* klass) {
H
hseigel 已提交
814
  assert(klass->is_klass() && klass->oop_is_array(), "should be array klass");
815
  java_class->metadata_field_put(_array_klass_offset, klass);
D
duke 已提交
816 817 818 819
}


bool java_lang_Class::is_primitive(oop java_class) {
820 821
  // should assert:
  //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
822 823 824 825 826
  bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);

#ifdef ASSERT
  if (is_primitive) {
    Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
827
    assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
828 829 830 831 832
        "Should be either the T_VOID primitive or a java primitive");
  }
#endif

  return is_primitive;
D
duke 已提交
833 834 835 836 837
}


BasicType java_lang_Class::primitive_type(oop java_class) {
  assert(java_lang_Class::is_primitive(java_class), "just checking");
838
  Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
D
duke 已提交
839 840 841
  BasicType type = T_VOID;
  if (ak != NULL) {
    // Note: create_basic_type_mirror above initializes ak to a non-null value.
842
    type = ArrayKlass::cast(ak)->element_type();
D
duke 已提交
843 844 845 846 847 848 849
  } else {
    assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
  }
  assert(Universe::java_mirror(type) == java_class, "must be consistent");
  return type;
}

850
BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) {
851 852 853 854 855 856 857
  assert(java_lang_Class::is_instance(java_class), "must be a Class object");
  if (is_primitive(java_class)) {
    if (reference_klass != NULL)
      (*reference_klass) = NULL;
    return primitive_type(java_class);
  } else {
    if (reference_klass != NULL)
858
      (*reference_klass) = as_Klass(java_class);
859 860 861 862
    return T_OBJECT;
  }
}

D
duke 已提交
863 864 865

oop java_lang_Class::primitive_mirror(BasicType t) {
  oop mirror = Universe::java_mirror(t);
866
  assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
D
duke 已提交
867 868 869 870 871 872 873 874 875 876 877
  assert(java_lang_Class::is_primitive(mirror), "must be primitive");
  return mirror;
}

bool java_lang_Class::offsets_computed = false;
int  java_lang_Class::classRedefinedCount_offset = -1;

void java_lang_Class::compute_offsets() {
  assert(!offsets_computed, "offsets should be initialized only once");
  offsets_computed = true;

878
  Klass* klass_oop = SystemDictionary::Class_klass();
D
duke 已提交
879 880
  // The classRedefinedCount field is only present starting in 1.5,
  // so don't go fatal.
881
  compute_optional_offset(classRedefinedCount_offset,
882
                          klass_oop, vmSymbols::classRedefinedCount_name(), vmSymbols::int_signature());
883

884 885 886 887 888 889
  // Needs to be optional because the old build runs Queens during bootstrapping
  // and jdk8-9 doesn't have coordinated pushes yet.
  compute_optional_offset(_class_loader_offset,
                 klass_oop, vmSymbols::classLoader_name(),
                 vmSymbols::classloader_signature());

890
  CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
D
duke 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
}

int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
  if (!JDK_Version::is_gte_jdk15x_version()
      || classRedefinedCount_offset == -1) {
    // The classRedefinedCount field is only present starting in 1.5.
    // If we don't have an offset for it then just return -1 as a marker.
    return -1;
  }

  return the_class_mirror->int_field(classRedefinedCount_offset);
}

void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
  if (!JDK_Version::is_gte_jdk15x_version()
      || classRedefinedCount_offset == -1) {
    // The classRedefinedCount field is only present starting in 1.5.
    // If we don't have an offset for it then nothing to set.
    return;
  }

  the_class_mirror->int_field_put(classRedefinedCount_offset, value);
}


// Note: JDK1.1 and before had a privateInfo_offset field which was used for the
//       platform thread structure, and a eetop offset which was used for thread
//       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
//       merged, so in the HotSpot VM we just use the eetop field for the thread
//       instead of the privateInfo_offset.
//
// Note: The stackSize field is only present starting in 1.4.

int java_lang_Thread::_name_offset = 0;
int java_lang_Thread::_group_offset = 0;
int java_lang_Thread::_contextClassLoader_offset = 0;
int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
int java_lang_Thread::_priority_offset = 0;
int java_lang_Thread::_eetop_offset = 0;
int java_lang_Thread::_daemon_offset = 0;
int java_lang_Thread::_stillborn_offset = 0;
int java_lang_Thread::_stackSize_offset = 0;
int java_lang_Thread::_tid_offset = 0;
int java_lang_Thread::_thread_status_offset = 0;
int java_lang_Thread::_park_blocker_offset = 0;
int java_lang_Thread::_park_event_offset = 0 ;
937
int java_lang_Thread::_inheritedTenantContainer_offset = 0 ;
D
duke 已提交
938 939 940 941 942


void java_lang_Thread::compute_offsets() {
  assert(_group_offset == 0, "offsets should be initialized only once");

943
  Klass* k = SystemDictionary::Thread_klass();
944
  compute_offset(_name_offset,      k, vmSymbols::name_name(),      vmSymbols::string_signature());
945 946 947 948 949 950 951
  compute_offset(_group_offset,     k, vmSymbols::group_name(),     vmSymbols::threadgroup_signature());
  compute_offset(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), vmSymbols::classloader_signature());
  compute_offset(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), vmSymbols::accesscontrolcontext_signature());
  compute_offset(_priority_offset,  k, vmSymbols::priority_name(),  vmSymbols::int_signature());
  compute_offset(_daemon_offset,    k, vmSymbols::daemon_name(),    vmSymbols::bool_signature());
  compute_offset(_eetop_offset,     k, vmSymbols::eetop_name(),     vmSymbols::long_signature());
  compute_offset(_stillborn_offset, k, vmSymbols::stillborn_name(), vmSymbols::bool_signature());
D
duke 已提交
952
  // The stackSize field is only present starting in 1.4, so don't go fatal.
953
  compute_optional_offset(_stackSize_offset, k, vmSymbols::stackSize_name(), vmSymbols::long_signature());
D
duke 已提交
954
  // The tid and thread_status fields are only present starting in 1.5, so don't go fatal.
955 956
  compute_optional_offset(_tid_offset, k, vmSymbols::thread_id_name(), vmSymbols::long_signature());
  compute_optional_offset(_thread_status_offset, k, vmSymbols::thread_status_name(), vmSymbols::int_signature());
D
duke 已提交
957
  // The parkBlocker field is only present starting in 1.6, so don't go fatal.
958 959
  compute_optional_offset(_park_blocker_offset, k, vmSymbols::park_blocker_name(), vmSymbols::object_signature());
  compute_optional_offset(_park_event_offset, k, vmSymbols::park_event_name(),
D
duke 已提交
960
 vmSymbols::long_signature());
961
  compute_offset(_inheritedTenantContainer_offset, k, vmSymbols::inheritedTenantContainer_name(), vmSymbols::tenantcontainer_signature());
D
duke 已提交
962 963 964 965
}


JavaThread* java_lang_Thread::thread(oop java_thread) {
966
  return (JavaThread*)java_thread->address_field(_eetop_offset);
D
duke 已提交
967 968 969 970
}


void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
971
  java_thread->address_field_put(_eetop_offset, (address)thread);
D
duke 已提交
972 973 974
}


975 976
oop java_lang_Thread::name(oop java_thread) {
  return java_thread->obj_field(_name_offset);
D
duke 已提交
977 978 979
}


980
void java_lang_Thread::set_name(oop java_thread, oop name) {
D
duke 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
  java_thread->obj_field_put(_name_offset, name);
}


ThreadPriority java_lang_Thread::priority(oop java_thread) {
  return (ThreadPriority)java_thread->int_field(_priority_offset);
}


void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
  java_thread->int_field_put(_priority_offset, priority);
}


oop java_lang_Thread::threadGroup(oop java_thread) {
  return java_thread->obj_field(_group_offset);
}


bool java_lang_Thread::is_stillborn(oop java_thread) {
  return java_thread->bool_field(_stillborn_offset) != 0;
}


// We never have reason to turn the stillborn bit off
void java_lang_Thread::set_stillborn(oop java_thread) {
  java_thread->bool_field_put(_stillborn_offset, true);
}


bool java_lang_Thread::is_alive(oop java_thread) {
  JavaThread* thr = java_lang_Thread::thread(java_thread);
  return (thr != NULL);
}


bool java_lang_Thread::is_daemon(oop java_thread) {
  return java_thread->bool_field(_daemon_offset) != 0;
}


void java_lang_Thread::set_daemon(oop java_thread) {
  java_thread->bool_field_put(_daemon_offset, true);
}

oop java_lang_Thread::context_class_loader(oop java_thread) {
  return java_thread->obj_field(_contextClassLoader_offset);
}

oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
  return java_thread->obj_field(_inheritedAccessControlContext_offset);
}

1034 1035 1036
oop java_lang_Thread::inherited_tenant_container(oop java_thread) {
  return java_thread->obj_field(_inheritedTenantContainer_offset);
}
D
duke 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

jlong java_lang_Thread::stackSize(oop java_thread) {
  // The stackSize field is only present starting in 1.4
  if (_stackSize_offset > 0) {
    assert(JDK_Version::is_gte_jdk14x_version(), "sanity check");
    return java_thread->long_field(_stackSize_offset);
  } else {
    return 0;
  }
}

// Write the thread status value to threadStatus field in java.lang.Thread java class.
void java_lang_Thread::set_thread_status(oop java_thread,
                                         java_lang_Thread::ThreadStatus status) {
  // The threadStatus is only present starting in 1.5
  if (_thread_status_offset > 0) {
    java_thread->int_field_put(_thread_status_offset, status);
  }
}

// Read thread status value from threadStatus field in java.lang.Thread java class.
java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
卓昂 已提交
1059
  assert((EnableJFR && Threads_lock->owned_by_self()) || Thread::current()->is_Watcher_thread() || Thread::current()->is_VM_thread() ||
D
duke 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
         JavaThread::current()->thread_state() == _thread_in_vm,
         "Java Thread is not running in vm");
  // The threadStatus is only present starting in 1.5
  if (_thread_status_offset > 0) {
    return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
  } else {
    // All we can easily figure out is if it is alive, but that is
    // enough info for a valid unknown status.
    // These aren't restricted to valid set ThreadStatus values, so
    // use JVMTI values and cast.
    JavaThread* thr = java_lang_Thread::thread(java_thread);
    if (thr == NULL) {
      // the thread hasn't run yet or is in the process of exiting
      return NEW;
    }
    return (java_lang_Thread::ThreadStatus)JVMTI_THREAD_STATE_ALIVE;
  }
}


jlong java_lang_Thread::thread_id(oop java_thread) {
  // The thread ID field is only present starting in 1.5
  if (_tid_offset > 0) {
    return java_thread->long_field(_tid_offset);
  } else {
    return 0;
  }
}

oop java_lang_Thread::park_blocker(oop java_thread) {
K
kamg 已提交
1090 1091
  assert(JDK_Version::current().supports_thread_park_blocker() &&
         _park_blocker_offset != 0, "Must support parkBlocker field");
D
duke 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

  if (_park_blocker_offset > 0) {
    return java_thread->obj_field(_park_blocker_offset);
  }

  return NULL;
}

jlong java_lang_Thread::park_event(oop java_thread) {
  if (_park_event_offset > 0) {
    return java_thread->long_field(_park_event_offset);
  }
  return 0;
}

bool java_lang_Thread::set_park_event(oop java_thread, jlong ptr) {
  if (_park_event_offset > 0) {
    java_thread->long_field_put(_park_event_offset, ptr);
    return true;
  }
  return false;
}


const char* java_lang_Thread::thread_status_name(oop java_thread) {
  assert(JDK_Version::is_gte_jdk15x_version() && _thread_status_offset != 0, "Must have thread status");
  ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
  switch (status) {
    case NEW                      : return "NEW";
    case RUNNABLE                 : return "RUNNABLE";
    case SLEEPING                 : return "TIMED_WAITING (sleeping)";
    case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
    case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
    case PARKED                   : return "WAITING (parking)";
    case PARKED_TIMED             : return "TIMED_WAITING (parking)";
    case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
    case TERMINATED               : return "TERMINATED";
    default                       : return "UNKNOWN";
  };
}
int java_lang_ThreadGroup::_parent_offset = 0;
int java_lang_ThreadGroup::_name_offset = 0;
int java_lang_ThreadGroup::_threads_offset = 0;
int java_lang_ThreadGroup::_groups_offset = 0;
int java_lang_ThreadGroup::_maxPriority_offset = 0;
int java_lang_ThreadGroup::_destroyed_offset = 0;
int java_lang_ThreadGroup::_daemon_offset = 0;
int java_lang_ThreadGroup::_vmAllowSuspension_offset = 0;
int java_lang_ThreadGroup::_nthreads_offset = 0;
int java_lang_ThreadGroup::_ngroups_offset = 0;

oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
  assert(java_thread_group->is_oop(), "thread group must be oop");
  return java_thread_group->obj_field(_parent_offset);
}

// ("name as oop" accessor is not necessary)

typeArrayOop java_lang_ThreadGroup::name(oop java_thread_group) {
  oop name = java_thread_group->obj_field(_name_offset);
  // ThreadGroup.name can be null
  return name == NULL ? (typeArrayOop)NULL : java_lang_String::value(name);
}

int java_lang_ThreadGroup::nthreads(oop java_thread_group) {
  assert(java_thread_group->is_oop(), "thread group must be oop");
  return java_thread_group->int_field(_nthreads_offset);
}

objArrayOop java_lang_ThreadGroup::threads(oop java_thread_group) {
  oop threads = java_thread_group->obj_field(_threads_offset);
  assert(threads != NULL, "threadgroups should have threads");
  assert(threads->is_objArray(), "just checking"); // Todo: Add better type checking code
  return objArrayOop(threads);
}

int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
  assert(java_thread_group->is_oop(), "thread group must be oop");
  return java_thread_group->int_field(_ngroups_offset);
}

objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
  oop groups = java_thread_group->obj_field(_groups_offset);
  assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
  return objArrayOop(groups);
}

ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
  assert(java_thread_group->is_oop(), "thread group must be oop");
  return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);
}

bool java_lang_ThreadGroup::is_destroyed(oop java_thread_group) {
  assert(java_thread_group->is_oop(), "thread group must be oop");
  return java_thread_group->bool_field(_destroyed_offset) != 0;
}

bool java_lang_ThreadGroup::is_daemon(oop java_thread_group) {
  assert(java_thread_group->is_oop(), "thread group must be oop");
  return java_thread_group->bool_field(_daemon_offset) != 0;
}

bool java_lang_ThreadGroup::is_vmAllowSuspension(oop java_thread_group) {
  assert(java_thread_group->is_oop(), "thread group must be oop");
  return java_thread_group->bool_field(_vmAllowSuspension_offset) != 0;
}

void java_lang_ThreadGroup::compute_offsets() {
  assert(_parent_offset == 0, "offsets should be initialized only once");

1202
  Klass* k = SystemDictionary::ThreadGroup_klass();
D
duke 已提交
1203

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
  compute_offset(_parent_offset,      k, vmSymbols::parent_name(),      vmSymbols::threadgroup_signature());
  compute_offset(_name_offset,        k, vmSymbols::name_name(),        vmSymbols::string_signature());
  compute_offset(_threads_offset,     k, vmSymbols::threads_name(),     vmSymbols::thread_array_signature());
  compute_offset(_groups_offset,      k, vmSymbols::groups_name(),      vmSymbols::threadgroup_array_signature());
  compute_offset(_maxPriority_offset, k, vmSymbols::maxPriority_name(), vmSymbols::int_signature());
  compute_offset(_destroyed_offset,   k, vmSymbols::destroyed_name(),   vmSymbols::bool_signature());
  compute_offset(_daemon_offset,      k, vmSymbols::daemon_name(),      vmSymbols::bool_signature());
  compute_offset(_vmAllowSuspension_offset, k, vmSymbols::vmAllowSuspension_name(), vmSymbols::bool_signature());
  compute_offset(_nthreads_offset,    k, vmSymbols::nthreads_name(),    vmSymbols::int_signature());
  compute_offset(_ngroups_offset,     k, vmSymbols::ngroups_name(),     vmSymbols::int_signature());
D
duke 已提交
1214 1215
}

1216
oop java_lang_Throwable::unassigned_stacktrace() {
1217
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::Throwable_klass());
1218 1219 1220 1221 1222 1223 1224 1225
  address addr = ik->static_field_addr(static_unassigned_stacktrace_offset);
  if (UseCompressedOops) {
    return oopDesc::load_decode_heap_oop((narrowOop *)addr);
  } else {
    return oopDesc::load_decode_heap_oop((oop*)addr);
  }
}

D
duke 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
oop java_lang_Throwable::backtrace(oop throwable) {
  return throwable->obj_field_acquire(backtrace_offset);
}


void java_lang_Throwable::set_backtrace(oop throwable, oop value) {
  throwable->release_obj_field_put(backtrace_offset, value);
}


oop java_lang_Throwable::message(oop throwable) {
  return throwable->obj_field(detailMessage_offset);
}


oop java_lang_Throwable::message(Handle throwable) {
  return throwable->obj_field(detailMessage_offset);
}


void java_lang_Throwable::set_message(oop throwable, oop value) {
  throwable->obj_field_put(detailMessage_offset, value);
}


1251 1252 1253 1254
void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
  throwable->obj_field_put(stackTrace_offset, st_element_array);
}

D
duke 已提交
1255 1256
void java_lang_Throwable::clear_stacktrace(oop throwable) {
  assert(JDK_Version::is_gte_jdk14x_version(), "should only be called in >= 1.4");
1257
  set_stacktrace(throwable, NULL);
D
duke 已提交
1258 1259 1260 1261 1262
}


void java_lang_Throwable::print(oop throwable, outputStream* st) {
  ResourceMark rm;
1263
  Klass* k = throwable->klass();
D
duke 已提交
1264
  assert(k != NULL, "just checking");
1265
  st->print("%s", InstanceKlass::cast(k)->external_name());
D
duke 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274
  oop msg = message(throwable);
  if (msg != NULL) {
    st->print(": %s", java_lang_String::as_utf8_string(msg));
  }
}


void java_lang_Throwable::print(Handle throwable, outputStream* st) {
  ResourceMark rm;
1275
  Klass* k = throwable->klass();
D
duke 已提交
1276
  assert(k != NULL, "just checking");
1277
  st->print("%s", InstanceKlass::cast(k)->external_name());
D
duke 已提交
1278 1279 1280 1281 1282 1283
  oop msg = message(throwable);
  if (msg != NULL) {
    st->print(": %s", java_lang_String::as_utf8_string(msg));
  }
}

1284 1285
// After this many redefines, the stack trace is unreliable.
const int MAX_VERSION = USHRT_MAX;
D
duke 已提交
1286

1287 1288 1289 1290 1291 1292
// Helper backtrace functions to store bci|version together.
static inline int merge_bci_and_version(int bci, int version) {
  // only store u2 for version, checking for overflow.
  if (version > USHRT_MAX || version < 0) version = MAX_VERSION;
  assert((jushort)bci == bci, "bci should be short");
  return build_int_from_shorts(version, bci);
D
duke 已提交
1293 1294
}

1295 1296
static inline int bci_at(unsigned int merged) {
  return extract_high_short_from_int(merged);
D
duke 已提交
1297
}
1298 1299
static inline int version_at(unsigned int merged) {
  return extract_low_short_from_int(merged);
D
duke 已提交
1300 1301
}

1302
static inline bool version_matches(Method* method, int version) {
1303 1304
  assert(version < MAX_VERSION, "version is too big");
  return method != NULL && (method->constants()->version() == version);
D
duke 已提交
1305 1306
}

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
static inline int get_line_number(Method* method, int bci) {
  int line_number = 0;
  if (method->is_native()) {
    // Negative value different from -1 below, enabling Java code in
    // class java.lang.StackTraceElement to distinguish "native" from
    // "no LineNumberTable".  JDK tests for -2.
    line_number = -2;
  } else {
    // Returns -1 if no LineNumberTable, and otherwise actual line number
    line_number = method->line_number_from_bci(bci);
    if (line_number == -1 && ShowHiddenFrames) {
      line_number = bci + 1000000;
D
duke 已提交
1319 1320
    }
  }
1321
  return line_number;
D
duke 已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330
}

// This class provides a simple wrapper over the internal structure of
// exception backtrace to insulate users of the backtrace from needing
// to know what it looks like.
class BacktraceBuilder: public StackObj {
 private:
  Handle          _backtrace;
  objArrayOop     _head;
1331
  typeArrayOop    _methods;
D
duke 已提交
1332
  typeArrayOop    _bcis;
1333
  objArrayOop     _mirrors;
1334
  typeArrayOop    _cprefs; // needed to insulate method name against redefinition
D
duke 已提交
1335 1336 1337 1338 1339 1340 1341
  int             _index;
  No_Safepoint_Verifier _nsv;

 public:

  enum {
    trace_methods_offset = java_lang_Throwable::trace_methods_offset,
1342
    trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
1343
    trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
1344
    trace_cprefs_offset  = java_lang_Throwable::trace_cprefs_offset,
D
duke 已提交
1345 1346 1347 1348 1349
    trace_next_offset    = java_lang_Throwable::trace_next_offset,
    trace_size           = java_lang_Throwable::trace_size,
    trace_chunk_size     = java_lang_Throwable::trace_chunk_size
  };

1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
  // get info out of chunks
  static typeArrayOop get_methods(objArrayHandle chunk) {
    typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
    assert(methods != NULL, "method array should be initialized in backtrace");
    return methods;
  }
  static typeArrayOop get_bcis(objArrayHandle chunk) {
    typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
    assert(bcis != NULL, "bci array should be initialized in backtrace");
    return bcis;
  }
  static objArrayOop get_mirrors(objArrayHandle chunk) {
    objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
    assert(mirrors != NULL, "mirror array should be initialized in backtrace");
    return mirrors;
  }
1366 1367 1368 1369 1370
  static typeArrayOop get_cprefs(objArrayHandle chunk) {
    typeArrayOop cprefs = typeArrayOop(chunk->obj_at(trace_cprefs_offset));
    assert(cprefs != NULL, "cprefs array should be initialized in backtrace");
    return cprefs;
  }
1371

D
duke 已提交
1372
  // constructor for new backtrace
1373
  BacktraceBuilder(TRAPS): _methods(NULL), _bcis(NULL), _head(NULL), _mirrors(NULL), _cprefs(NULL) {
D
duke 已提交
1374 1375 1376 1377 1378
    expand(CHECK);
    _backtrace = _head;
    _index = 0;
  }

1379 1380 1381 1382
  BacktraceBuilder(objArrayHandle backtrace) {
    _methods = get_methods(backtrace);
    _bcis = get_bcis(backtrace);
    _mirrors = get_mirrors(backtrace);
1383
    _cprefs = get_cprefs(backtrace);
1384 1385 1386 1387 1388 1389 1390 1391 1392
    assert(_methods->length() == _bcis->length() &&
           _methods->length() == _mirrors->length(),
           "method and source information arrays should match");

    // head is the preallocated backtrace
    _backtrace = _head = backtrace();
    _index = 0;
  }

D
duke 已提交
1393 1394 1395 1396 1397 1398 1399
  void expand(TRAPS) {
    objArrayHandle old_head(THREAD, _head);
    Pause_No_Safepoint_Verifier pnsv(&_nsv);

    objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
    objArrayHandle new_head(THREAD, head);

1400
    typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
1401
    typeArrayHandle new_methods(THREAD, methods);
D
duke 已提交
1402

1403
    typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
D
duke 已提交
1404 1405
    typeArrayHandle new_bcis(THREAD, bcis);

1406 1407 1408
    objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
    objArrayHandle new_mirrors(THREAD, mirrors);

1409 1410 1411
    typeArrayOop cprefs = oopFactory::new_shortArray(trace_chunk_size, CHECK);
    typeArrayHandle new_cprefs(THREAD, cprefs);

D
duke 已提交
1412 1413 1414 1415 1416
    if (!old_head.is_null()) {
      old_head->obj_at_put(trace_next_offset, new_head());
    }
    new_head->obj_at_put(trace_methods_offset, new_methods());
    new_head->obj_at_put(trace_bcis_offset, new_bcis());
1417
    new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
1418
    new_head->obj_at_put(trace_cprefs_offset, new_cprefs());
D
duke 已提交
1419 1420 1421

    _head    = new_head();
    _methods = new_methods();
1422
    _bcis = new_bcis();
1423
    _mirrors = new_mirrors();
1424
    _cprefs  = new_cprefs();
D
duke 已提交
1425 1426 1427 1428 1429 1430 1431
    _index = 0;
  }

  oop backtrace() {
    return _backtrace();
  }

1432
  inline void push(Method* method, int bci, TRAPS) {
1433 1434 1435 1436 1437
    // Smear the -1 bci to 0 since the array only holds unsigned
    // shorts.  The later line number lookup would just smear the -1
    // to a 0 even if it could be recorded.
    if (bci == SynchronizationEntryBCI) bci = 0;

D
duke 已提交
1438 1439 1440 1441 1442 1443
    if (_index >= trace_chunk_size) {
      methodHandle mhandle(THREAD, method);
      expand(CHECK);
      method = mhandle();
    }

1444
    _methods->short_at_put(_index, method->orig_method_idnum());
1445
    _bcis->int_at_put(_index, merge_bci_and_version(bci, method->constants()->version()));
1446
    _cprefs->short_at_put(_index, method->name_index());
1447 1448 1449 1450

    // We need to save the mirrors in the backtrace to keep the class
    // from being unloaded while we still have this stack trace.
    assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
1451
    _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
D
duke 已提交
1452 1453 1454
    _index++;
  }

1455 1456 1457 1458
};

// Print stack trace element to resource allocated buffer
char* java_lang_Throwable::print_stack_element_to_buffer(Handle mirror,
1459
                                  int method_id, int version, int bci, int cpref) {
1460 1461 1462 1463 1464 1465

  // Get strings and string lengths
  InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
  const char* klass_name  = holder->external_name();
  int buf_len = (int)strlen(klass_name);

1466
  Method* method = holder->method_with_orig_idnum(method_id, version);
1467

1468 1469 1470
  // The method can be NULL if the requested class version is gone
  Symbol* sym = (method != NULL) ? method->name() : holder->constants()->symbol_at(cpref);
  char* method_name = sym->as_C_string();
1471 1472
  buf_len += (int)strlen(method_name);

1473 1474 1475 1476
  // Use a specific ik version as a holder since the mirror might
  // refer to a version that is now obsolete and no longer accessible
  // via the previous versions list.
  holder = holder->get_klass_version(version);
1477
  char* source_file_name = NULL;
1478
  if (holder != NULL) {
1479 1480 1481 1482 1483
    Symbol* source = holder->source_file_name();
    if (source != NULL) {
      source_file_name = source->as_C_string();
      buf_len += (int)strlen(source_file_name);
    }
D
duke 已提交
1484 1485
  }

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
  // Allocate temporary buffer with extra space for formatting and line number
  char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64);

  // Print stack trace line in buffer
  sprintf(buf, "\tat %s.%s", klass_name, method_name);

  if (!version_matches(method, version)) {
    strcat(buf, "(Redefined)");
  } else {
    int line_number = get_line_number(method, bci);
    if (line_number == -2) {
      strcat(buf, "(Native Method)");
    } else {
      if (source_file_name != NULL && (line_number != -1)) {
        // Sourcename and linenumber
        sprintf(buf + (int)strlen(buf), "(%s:%d)", source_file_name, line_number);
      } else if (source_file_name != NULL) {
        // Just sourcename
        sprintf(buf + (int)strlen(buf), "(%s)", source_file_name);
      } else {
        // Neither sourcename nor linenumber
        sprintf(buf + (int)strlen(buf), "(Unknown Source)");
      }
      nmethod* nm = method->code();
      if (WizardMode && nm != NULL) {
        sprintf(buf + (int)strlen(buf), "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm);
      }
    }
D
duke 已提交
1514 1515
  }

1516 1517 1518 1519
  return buf;
}

void java_lang_Throwable::print_stack_element(outputStream *st, Handle mirror,
1520
                                              int method_id, int version, int bci, int cpref) {
1521
  ResourceMark rm;
1522
  char* buf = print_stack_element_to_buffer(mirror, method_id, version, bci, cpref);
1523 1524 1525 1526 1527
  st->print_cr("%s", buf);
}

void java_lang_Throwable::print_stack_element(outputStream *st, methodHandle method, int bci) {
  Handle mirror = method->method_holder()->java_mirror();
1528
  int method_id = method->orig_method_idnum();
1529
  int version = method->constants()->version();
1530 1531
  int cpref = method->name_index();
  print_stack_element(st, mirror, method_id, version, bci, cpref);
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
}

const char* java_lang_Throwable::no_stack_trace_message() {
  return "\t<<no stack trace available>>";
}


// Currently used only for exceptions occurring during startup
void java_lang_Throwable::print_stack_trace(oop throwable, outputStream* st) {
  Thread *THREAD = Thread::current();
  Handle h_throwable(THREAD, throwable);
  while (h_throwable.not_null()) {
    objArrayHandle result (THREAD, objArrayOop(backtrace(h_throwable())));
    if (result.is_null()) {
1546
      st->print_cr("%s", no_stack_trace_message());
1547 1548 1549 1550 1551 1552 1553 1554 1555
      return;
    }

    while (result.not_null()) {

      // Get method id, bci, version and mirror from chunk
      typeArrayHandle methods (THREAD, BacktraceBuilder::get_methods(result));
      typeArrayHandle bcis (THREAD, BacktraceBuilder::get_bcis(result));
      objArrayHandle mirrors (THREAD, BacktraceBuilder::get_mirrors(result));
1556
      typeArrayHandle cprefs (THREAD, BacktraceBuilder::get_cprefs(result));
1557 1558 1559 1560 1561 1562 1563 1564 1565

      int length = methods()->length();
      for (int index = 0; index < length; index++) {
        Handle mirror(THREAD, mirrors->obj_at(index));
        // NULL mirror means end of stack trace
        if (mirror.is_null()) goto handle_cause;
        int method = methods->short_at(index);
        int version = version_at(bcis->int_at(index));
        int bci = bci_at(bcis->int_at(index));
1566 1567
        int cpref = cprefs->short_at(index);
        print_stack_element(st, mirror, method, version, bci, cpref);
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
      }
      result = objArrayHandle(THREAD, objArrayOop(result->obj_at(trace_next_offset)));
    }
  handle_cause:
    {
      EXCEPTION_MARK;
      JavaValue cause(T_OBJECT);
      JavaCalls::call_virtual(&cause,
                              h_throwable,
                              KlassHandle(THREAD, h_throwable->klass()),
                              vmSymbols::getCause_name(),
                              vmSymbols::void_throwable_signature(),
                              THREAD);
      // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM.
      if (HAS_PENDING_EXCEPTION) {
        CLEAR_PENDING_EXCEPTION;
        h_throwable = Handle();
      } else {
        h_throwable = Handle(THREAD, (oop) cause.get_jobject());
        if (h_throwable.not_null()) {
          st->print("Caused by: ");
          print(h_throwable, st);
          st->cr();
        }
      }
    }
  }
}
D
duke 已提交
1596

1597
void java_lang_Throwable::fill_in_stack_trace(Handle throwable, methodHandle method, TRAPS) {
D
duke 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606
  if (!StackTraceInThrowable) return;
  ResourceMark rm(THREAD);

  // Start out by clearing the backtrace for this object, in case the VM
  // runs out of memory while allocating the stack trace
  set_backtrace(throwable(), NULL);
  if (JDK_Version::is_gte_jdk14x_version()) {
    // New since 1.4, clear lazily constructed Java level stacktrace if
    // refilling occurs
1607
    // This is unnecessary in 1.7+ but harmless
D
duke 已提交
1608 1609 1610 1611 1612 1613 1614
    clear_stacktrace(throwable());
  }

  int max_depth = MaxJavaStackTraceDepth;
  JavaThread* thread = (JavaThread*)THREAD;
  BacktraceBuilder bt(CHECK);

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
  // If there is no Java frame just return the method that was being called
  // with bci 0
  if (!thread->has_last_Java_frame()) {
    if (max_depth >= 1 && method() != NULL) {
      bt.push(method(), 0, CHECK);
      set_backtrace(throwable(), bt.backtrace());
    }
    return;
  }

D
duke 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
  // Instead of using vframe directly, this version of fill_in_stack_trace
  // basically handles everything by hand. This significantly improved the
  // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
  // See bug 6333838 for  more details.
  // The "ASSERT" here is to verify this method generates the exactly same stack
  // trace as utilizing vframe.
#ifdef ASSERT
  vframeStream st(thread);
  methodHandle st_method(THREAD, st.method());
#endif
  int total_count = 0;
  RegisterMap map(thread, false);
  int decode_offset = 0;
  nmethod* nm = NULL;
  bool skip_fillInStackTrace_check = false;
  bool skip_throwableInit_check = false;
1641
  bool skip_hidden = !ShowHiddenFrames;
D
duke 已提交
1642 1643

  for (frame fr = thread->last_frame(); max_depth != total_count;) {
1644
    Method* method = NULL;
D
duke 已提交
1645 1646 1647 1648 1649 1650
    int bci = 0;

    // Compiled java method case.
    if (decode_offset != 0) {
      DebugInfoReadStream stream(nm, decode_offset);
      decode_offset = stream.read_int();
1651
      method = (Method*)nm->metadata_at(stream.read_int());
1652
      bci = stream.read_bci();
D
duke 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
    } else {
      if (fr.is_first_frame()) break;
      address pc = fr.pc();
      if (fr.is_interpreted_frame()) {
        intptr_t bcx = fr.interpreter_frame_bcx();
        method = fr.interpreter_frame_method();
        bci =  fr.is_bci(bcx) ? bcx : method->bci_from((address)bcx);
        fr = fr.sender(&map);
      } else {
        CodeBlob* cb = fr.cb();
        // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
        // but non nmethod
        fr = fr.sender(&map);
        if (cb == NULL || !cb->is_nmethod()) {
          continue;
        }
        nm = (nmethod*)cb;
        if (nm->method()->is_native()) {
          method = nm->method();
          bci = 0;
        } else {
          PcDesc* pd = nm->pc_desc_at(pc);
          decode_offset = pd->scope_decode_offset();
          // if decode_offset is not equal to 0, it will execute the
          // "compiled java method case" at the beginning of the loop.
          continue;
        }
      }
    }
#ifdef ASSERT
1683 1684 1685 1686
    assert(st_method() == method && st.bci() == bci,
           "Wrong stack trace");
    st.next();
    // vframeStream::method isn't GC-safe so store off a copy
1687
    // of the Method* in case we GC.
1688 1689 1690
    if (!st.at_end()) {
      st_method = st.method();
    }
D
duke 已提交
1691
#endif
1692 1693 1694 1695 1696 1697

    // the format of the stacktrace will be:
    // - 1 or more fillInStackTrace frames for the exception class (skipped)
    // - 0 or more <init> methods for the exception class (skipped)
    // - rest of the stack

D
duke 已提交
1698
    if (!skip_fillInStackTrace_check) {
1699 1700 1701
      if ((method->name() == vmSymbols::fillInStackTrace_name() ||
           method->name() == vmSymbols::fillInStackTrace0_name()) &&
          throwable->is_a(method->method_holder())) {
D
duke 已提交
1702 1703
        continue;
      }
1704 1705 1706
      else {
        skip_fillInStackTrace_check = true; // gone past them all
      }
D
duke 已提交
1707 1708
    }
    if (!skip_throwableInit_check) {
1709 1710 1711 1712
      assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");

      // skip <init> methods of the exception class and superclasses
      // This is simlar to classic VM.
D
duke 已提交
1713 1714 1715 1716
      if (method->name() == vmSymbols::object_initializer_name() &&
          throwable->is_a(method->method_holder())) {
        continue;
      } else {
1717
        // there are none or we've seen them all - either way stop checking
D
duke 已提交
1718 1719 1720
        skip_throwableInit_check = true;
      }
    }
1721 1722 1723
    if (method->is_hidden()) {
      if (skip_hidden)  continue;
    }
D
duke 已提交
1724 1725 1726 1727 1728 1729 1730 1731
    bt.push(method, bci, CHECK);
    total_count++;
  }

  // Put completed stack trace into throwable object
  set_backtrace(throwable(), bt.backtrace());
}

1732
void java_lang_Throwable::fill_in_stack_trace(Handle throwable, methodHandle method) {
D
duke 已提交
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
  // No-op if stack trace is disabled
  if (!StackTraceInThrowable) {
    return;
  }

  // Disable stack traces for some preallocated out of memory errors
  if (!Universe::should_fill_in_stack_trace(throwable)) {
    return;
  }

  PRESERVE_EXCEPTION_MARK;

  JavaThread* thread = JavaThread::active();
1746
  fill_in_stack_trace(throwable, method, thread);
D
duke 已提交
1747 1748 1749 1750 1751 1752 1753 1754 1755
  // ignore exceptions thrown during stack trace filling
  CLEAR_PENDING_EXCEPTION;
}

void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
  // Allocate stack trace - backtrace is created but not filled in

  // No-op if stack trace is disabled
  if (!StackTraceInThrowable) return;
1756 1757
  BacktraceBuilder bt(CHECK);   // creates a backtrace
  set_backtrace(throwable(), bt.backtrace());
D
duke 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766
}


void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
  // Fill in stack trace into preallocated backtrace (no GC)

  // No-op if stack trace is disabled
  if (!StackTraceInThrowable) return;

1767
  assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check");
D
duke 已提交
1768

1769
  JavaThread* THREAD = JavaThread::current();
D
duke 已提交
1770

1771 1772
  objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
  assert(backtrace.not_null(), "backtrace should have been preallocated");
D
duke 已提交
1773

1774 1775
  ResourceMark rm(THREAD);
  vframeStream st(THREAD);
D
duke 已提交
1776

1777
  BacktraceBuilder bt(backtrace);
D
duke 已提交
1778 1779 1780 1781 1782

  // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
  // methods as preallocated errors aren't created by "java" code.

  // fill in as much stack trace as possible
1783
  typeArrayOop methods = BacktraceBuilder::get_methods(backtrace);
D
duke 已提交
1784 1785 1786 1787
  int max_chunks = MIN2(methods->length(), (int)MaxJavaStackTraceDepth);
  int chunk_count = 0;

  for (;!st.at_end(); st.next()) {
1788
    bt.push(st.method(), st.bci(), CHECK);
D
duke 已提交
1789 1790 1791 1792 1793
    chunk_count++;

    // Bail-out for deep stacks
    if (chunk_count >= max_chunks) break;
  }
1794 1795 1796 1797 1798 1799 1800 1801

  // For Java 7+ we support the Throwable immutability protocol defined for Java 7. This support
  // was missing in 7u0 so in 7u0 there is a workaround in the Throwable class. That workaround
  // can be removed in a JDK using this JVM version
  if (JDK_Version::is_gte_jdk17x_version()) {
      java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
      assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
  }
D
duke 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
}


int java_lang_Throwable::get_stack_trace_depth(oop throwable, TRAPS) {
  if (throwable == NULL) {
    THROW_0(vmSymbols::java_lang_NullPointerException());
  }
  objArrayOop chunk = objArrayOop(backtrace(throwable));
  int depth = 0;
  if (chunk != NULL) {
    // Iterate over chunks and count full ones
    while (true) {
      objArrayOop next = objArrayOop(chunk->obj_at(trace_next_offset));
      if (next == NULL) break;
      depth += trace_chunk_size;
      chunk = next;
    }
    assert(chunk != NULL && chunk->obj_at(trace_next_offset) == NULL, "sanity check");
1820 1821 1822 1823 1824 1825
    // Count element in remaining partial chunk.  NULL value for mirror
    // marks the end of the stack trace elements that are saved.
    objArrayOop mirrors = BacktraceBuilder::get_mirrors(chunk);
    assert(mirrors != NULL, "sanity check");
    for (int i = 0; i < mirrors->length(); i++) {
      if (mirrors->obj_at(i) == NULL) break;
D
duke 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
      depth++;
    }
  }
  return depth;
}


oop java_lang_Throwable::get_stack_trace_element(oop throwable, int index, TRAPS) {
  if (throwable == NULL) {
    THROW_0(vmSymbols::java_lang_NullPointerException());
  }
  if (index < 0) {
    THROW_(vmSymbols::java_lang_IndexOutOfBoundsException(), NULL);
  }
  // Compute how many chunks to skip and index into actual chunk
  objArrayOop chunk = objArrayOop(backtrace(throwable));
  int skip_chunks = index / trace_chunk_size;
  int chunk_index = index % trace_chunk_size;
  while (chunk != NULL && skip_chunks > 0) {
    chunk = objArrayOop(chunk->obj_at(trace_next_offset));
        skip_chunks--;
  }
  if (chunk == NULL) {
    THROW_(vmSymbols::java_lang_IndexOutOfBoundsException(), NULL);
  }
1851
  // Get method id, bci, version, mirror and cpref from chunk
1852 1853 1854
  typeArrayOop methods = BacktraceBuilder::get_methods(chunk);
  typeArrayOop bcis = BacktraceBuilder::get_bcis(chunk);
  objArrayOop mirrors = BacktraceBuilder::get_mirrors(chunk);
1855
  typeArrayOop cprefs = BacktraceBuilder::get_cprefs(chunk);
1856 1857 1858 1859 1860 1861

  assert(methods != NULL && bcis != NULL && mirrors != NULL, "sanity check");

  int method = methods->short_at(chunk_index);
  int version = version_at(bcis->int_at(chunk_index));
  int bci = bci_at(bcis->int_at(chunk_index));
1862
  int cpref = cprefs->short_at(chunk_index);
1863 1864
  Handle mirror(THREAD, mirrors->obj_at(chunk_index));

D
duke 已提交
1865
  // Chunk can be partial full
1866
  if (mirror.is_null()) {
D
duke 已提交
1867 1868
    THROW_(vmSymbols::java_lang_IndexOutOfBoundsException(), NULL);
  }
1869
  oop element = java_lang_StackTraceElement::create(mirror, method, version, bci, cpref, CHECK_0);
D
duke 已提交
1870 1871 1872
  return element;
}

1873
oop java_lang_StackTraceElement::create(Handle mirror, int method_id,
1874
                                        int version, int bci, int cpref, TRAPS) {
D
duke 已提交
1875
  // Allocate java.lang.StackTraceElement instance
1876
  Klass* k = SystemDictionary::StackTraceElement_klass();
1877
  assert(k != NULL, "must be loaded in 1.4+");
D
duke 已提交
1878 1879 1880 1881 1882 1883 1884 1885
  instanceKlassHandle ik (THREAD, k);
  if (ik->should_be_initialized()) {
    ik->initialize(CHECK_0);
  }

  Handle element = ik->allocate_instance_handle(CHECK_0);
  // Fill in class name
  ResourceMark rm(THREAD);
1886 1887
  InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
  const char* str = holder->external_name();
D
duke 已提交
1888 1889
  oop classname = StringTable::intern((char*) str, CHECK_0);
  java_lang_StackTraceElement::set_declaringClass(element(), classname);
1890

1891 1892 1893 1894
  Method* method = holder->method_with_orig_idnum(method_id, version);

  // The method can be NULL if the requested class version is gone
  Symbol* sym = (method != NULL) ? method->name() : holder->constants()->symbol_at(cpref);
1895 1896

  // Fill in method name
1897
  oop methodname = StringTable::intern(sym, CHECK_0);
D
duke 已提交
1898
  java_lang_StackTraceElement::set_methodName(element(), methodname);
1899 1900 1901 1902 1903

  if (!version_matches(method, version)) {
    // The method was redefined, accurate line number information isn't available
    java_lang_StackTraceElement::set_fileName(element(), NULL);
    java_lang_StackTraceElement::set_lineNumber(element(), -1);
D
duke 已提交
1904
  } else {
1905
    // Fill in source file name and line number.
1906 1907 1908 1909 1910
    // Use a specific ik version as a holder since the mirror might
    // refer to a version that is now obsolete and no longer accessible
    // via the previous versions list.
    holder = holder->get_klass_version(version);
    assert(holder != NULL, "sanity check");
1911 1912 1913 1914 1915
    Symbol* source = holder->source_file_name();
    if (ShowHiddenFrames && source == NULL)
      source = vmSymbols::unknown_class_name();
    oop filename = StringTable::intern(source, CHECK_0);
    java_lang_StackTraceElement::set_fileName(element(), filename);
D
duke 已提交
1916

1917 1918 1919
    int line_number = get_line_number(method, bci);
    java_lang_StackTraceElement::set_lineNumber(element(), line_number);
  }
D
duke 已提交
1920 1921 1922
  return element();
}

1923 1924
oop java_lang_StackTraceElement::create(methodHandle method, int bci, TRAPS) {
  Handle mirror (THREAD, method->method_holder()->java_mirror());
1925 1926 1927
  int method_id = method->orig_method_idnum();
  int cpref = method->name_index();
  return create(mirror, method_id, method->constants()->version(), bci, cpref, THREAD);
1928
}
D
duke 已提交
1929 1930

void java_lang_reflect_AccessibleObject::compute_offsets() {
1931
  Klass* k = SystemDictionary::reflect_AccessibleObject_klass();
1932
  compute_offset(override_offset, k, vmSymbols::override_name(), vmSymbols::bool_signature());
D
duke 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
}

jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return (jboolean) reflect->bool_field(override_offset);
}

void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  reflect->bool_field_put(override_offset, (int) value);
}

void java_lang_reflect_Method::compute_offsets() {
1946
  Klass* k = SystemDictionary::reflect_Method_klass();
1947 1948 1949 1950 1951 1952 1953
  compute_offset(clazz_offset,          k, vmSymbols::clazz_name(),          vmSymbols::class_signature());
  compute_offset(name_offset,           k, vmSymbols::name_name(),           vmSymbols::string_signature());
  compute_offset(returnType_offset,     k, vmSymbols::returnType_name(),     vmSymbols::class_signature());
  compute_offset(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), vmSymbols::class_array_signature());
  compute_offset(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), vmSymbols::class_array_signature());
  compute_offset(slot_offset,           k, vmSymbols::slot_name(),           vmSymbols::int_signature());
  compute_offset(modifiers_offset,      k, vmSymbols::modifiers_name(),      vmSymbols::int_signature());
D
duke 已提交
1954 1955 1956 1957 1958
  // The generic signature and annotations fields are only present in 1.5
  signature_offset = -1;
  annotations_offset = -1;
  parameter_annotations_offset = -1;
  annotation_default_offset = -1;
1959
  type_annotations_offset = -1;
1960 1961 1962 1963
  compute_optional_offset(signature_offset,             k, vmSymbols::signature_name(),             vmSymbols::string_signature());
  compute_optional_offset(annotations_offset,           k, vmSymbols::annotations_name(),           vmSymbols::byte_array_signature());
  compute_optional_offset(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), vmSymbols::byte_array_signature());
  compute_optional_offset(annotation_default_offset,    k, vmSymbols::annotation_default_name(),    vmSymbols::byte_array_signature());
1964
  compute_optional_offset(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      vmSymbols::byte_array_signature());
D
duke 已提交
1965 1966 1967 1968
}

Handle java_lang_reflect_Method::create(TRAPS) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
1969
  Klass* klass = SystemDictionary::reflect_Method_klass();
D
duke 已提交
1970 1971
  // This class is eagerly initialized during VM initialization, since we keep a refence
  // to one of the methods
1972
  assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized");
1973
  return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD);
D
duke 已提交
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
}

oop java_lang_reflect_Method::clazz(oop reflect) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return reflect->obj_field(clazz_offset);
}

void java_lang_reflect_Method::set_clazz(oop reflect, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
   reflect->obj_field_put(clazz_offset, value);
}

int java_lang_reflect_Method::slot(oop reflect) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return reflect->int_field(slot_offset);
}

void java_lang_reflect_Method::set_slot(oop reflect, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  reflect->int_field_put(slot_offset, value);
}

oop java_lang_reflect_Method::name(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return method->obj_field(name_offset);
}

void java_lang_reflect_Method::set_name(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  method->obj_field_put(name_offset, value);
}

oop java_lang_reflect_Method::return_type(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return method->obj_field(returnType_offset);
}

void java_lang_reflect_Method::set_return_type(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  method->obj_field_put(returnType_offset, value);
}

oop java_lang_reflect_Method::parameter_types(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return method->obj_field(parameterTypes_offset);
}

void java_lang_reflect_Method::set_parameter_types(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  method->obj_field_put(parameterTypes_offset, value);
}

oop java_lang_reflect_Method::exception_types(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return method->obj_field(exceptionTypes_offset);
}

void java_lang_reflect_Method::set_exception_types(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  method->obj_field_put(exceptionTypes_offset, value);
}

int java_lang_reflect_Method::modifiers(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return method->int_field(modifiers_offset);
}

void java_lang_reflect_Method::set_modifiers(oop method, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  method->int_field_put(modifiers_offset, value);
}

bool java_lang_reflect_Method::has_signature_field() {
  return (signature_offset >= 0);
}

oop java_lang_reflect_Method::signature(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_signature_field(), "signature field must be present");
  return method->obj_field(signature_offset);
}

void java_lang_reflect_Method::set_signature(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_signature_field(), "signature field must be present");
  method->obj_field_put(signature_offset, value);
}

bool java_lang_reflect_Method::has_annotations_field() {
  return (annotations_offset >= 0);
}

oop java_lang_reflect_Method::annotations(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotations_field(), "annotations field must be present");
  return method->obj_field(annotations_offset);
}

void java_lang_reflect_Method::set_annotations(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotations_field(), "annotations field must be present");
  method->obj_field_put(annotations_offset, value);
}

bool java_lang_reflect_Method::has_parameter_annotations_field() {
  return (parameter_annotations_offset >= 0);
}

oop java_lang_reflect_Method::parameter_annotations(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_parameter_annotations_field(), "parameter annotations field must be present");
  return method->obj_field(parameter_annotations_offset);
}

void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_parameter_annotations_field(), "parameter annotations field must be present");
  method->obj_field_put(parameter_annotations_offset, value);
}

bool java_lang_reflect_Method::has_annotation_default_field() {
  return (annotation_default_offset >= 0);
}

oop java_lang_reflect_Method::annotation_default(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotation_default_field(), "annotation default field must be present");
  return method->obj_field(annotation_default_offset);
}

void java_lang_reflect_Method::set_annotation_default(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotation_default_field(), "annotation default field must be present");
  method->obj_field_put(annotation_default_offset, value);
}

2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
bool java_lang_reflect_Method::has_type_annotations_field() {
  return (type_annotations_offset >= 0);
}

oop java_lang_reflect_Method::type_annotations(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_type_annotations_field(), "type_annotations field must be present");
  return method->obj_field(type_annotations_offset);
}

void java_lang_reflect_Method::set_type_annotations(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_type_annotations_field(), "type_annotations field must be present");
  method->obj_field_put(type_annotations_offset, value);
}

D
duke 已提交
2126
void java_lang_reflect_Constructor::compute_offsets() {
2127
  Klass* k = SystemDictionary::reflect_Constructor_klass();
2128 2129 2130 2131 2132
  compute_offset(clazz_offset,          k, vmSymbols::clazz_name(),          vmSymbols::class_signature());
  compute_offset(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), vmSymbols::class_array_signature());
  compute_offset(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), vmSymbols::class_array_signature());
  compute_offset(slot_offset,           k, vmSymbols::slot_name(),           vmSymbols::int_signature());
  compute_offset(modifiers_offset,      k, vmSymbols::modifiers_name(),      vmSymbols::int_signature());
D
duke 已提交
2133 2134 2135 2136
  // The generic signature and annotations fields are only present in 1.5
  signature_offset = -1;
  annotations_offset = -1;
  parameter_annotations_offset = -1;
2137
  type_annotations_offset = -1;
2138 2139 2140
  compute_optional_offset(signature_offset,             k, vmSymbols::signature_name(),             vmSymbols::string_signature());
  compute_optional_offset(annotations_offset,           k, vmSymbols::annotations_name(),           vmSymbols::byte_array_signature());
  compute_optional_offset(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), vmSymbols::byte_array_signature());
2141
  compute_optional_offset(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      vmSymbols::byte_array_signature());
D
duke 已提交
2142 2143 2144 2145
}

Handle java_lang_reflect_Constructor::create(TRAPS) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2146
  Symbol* name = vmSymbols::java_lang_reflect_Constructor();
2147
  Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
D
duke 已提交
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
  instanceKlassHandle klass (THREAD, k);
  // Ensure it is initialized
  klass->initialize(CHECK_NH);
  return klass->allocate_instance_handle(CHECK_NH);
}

oop java_lang_reflect_Constructor::clazz(oop reflect) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return reflect->obj_field(clazz_offset);
}

void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
   reflect->obj_field_put(clazz_offset, value);
}

oop java_lang_reflect_Constructor::parameter_types(oop constructor) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return constructor->obj_field(parameterTypes_offset);
}

void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  constructor->obj_field_put(parameterTypes_offset, value);
}

oop java_lang_reflect_Constructor::exception_types(oop constructor) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return constructor->obj_field(exceptionTypes_offset);
}

void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  constructor->obj_field_put(exceptionTypes_offset, value);
}

int java_lang_reflect_Constructor::slot(oop reflect) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return reflect->int_field(slot_offset);
}

void java_lang_reflect_Constructor::set_slot(oop reflect, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  reflect->int_field_put(slot_offset, value);
}

int java_lang_reflect_Constructor::modifiers(oop constructor) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return constructor->int_field(modifiers_offset);
}

void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  constructor->int_field_put(modifiers_offset, value);
}

bool java_lang_reflect_Constructor::has_signature_field() {
  return (signature_offset >= 0);
}

oop java_lang_reflect_Constructor::signature(oop constructor) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_signature_field(), "signature field must be present");
  return constructor->obj_field(signature_offset);
}

void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_signature_field(), "signature field must be present");
  constructor->obj_field_put(signature_offset, value);
}

bool java_lang_reflect_Constructor::has_annotations_field() {
  return (annotations_offset >= 0);
}

oop java_lang_reflect_Constructor::annotations(oop constructor) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotations_field(), "annotations field must be present");
  return constructor->obj_field(annotations_offset);
}

void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotations_field(), "annotations field must be present");
  constructor->obj_field_put(annotations_offset, value);
}

bool java_lang_reflect_Constructor::has_parameter_annotations_field() {
  return (parameter_annotations_offset >= 0);
}

oop java_lang_reflect_Constructor::parameter_annotations(oop method) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_parameter_annotations_field(), "parameter annotations field must be present");
  return method->obj_field(parameter_annotations_offset);
}

void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_parameter_annotations_field(), "parameter annotations field must be present");
  method->obj_field_put(parameter_annotations_offset, value);
}

2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
bool java_lang_reflect_Constructor::has_type_annotations_field() {
  return (type_annotations_offset >= 0);
}

oop java_lang_reflect_Constructor::type_annotations(oop constructor) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_type_annotations_field(), "type_annotations field must be present");
  return constructor->obj_field(type_annotations_offset);
}

void java_lang_reflect_Constructor::set_type_annotations(oop constructor, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_type_annotations_field(), "type_annotations field must be present");
  constructor->obj_field_put(type_annotations_offset, value);
}

D
duke 已提交
2268
void java_lang_reflect_Field::compute_offsets() {
2269
  Klass* k = SystemDictionary::reflect_Field_klass();
2270 2271 2272 2273 2274
  compute_offset(clazz_offset,     k, vmSymbols::clazz_name(),     vmSymbols::class_signature());
  compute_offset(name_offset,      k, vmSymbols::name_name(),      vmSymbols::string_signature());
  compute_offset(type_offset,      k, vmSymbols::type_name(),      vmSymbols::class_signature());
  compute_offset(slot_offset,      k, vmSymbols::slot_name(),      vmSymbols::int_signature());
  compute_offset(modifiers_offset, k, vmSymbols::modifiers_name(), vmSymbols::int_signature());
D
duke 已提交
2275 2276 2277
  // The generic signature and annotations fields are only present in 1.5
  signature_offset = -1;
  annotations_offset = -1;
2278
  type_annotations_offset = -1;
2279 2280
  compute_optional_offset(signature_offset, k, vmSymbols::signature_name(), vmSymbols::string_signature());
  compute_optional_offset(annotations_offset,  k, vmSymbols::annotations_name(),  vmSymbols::byte_array_signature());
2281
  compute_optional_offset(type_annotations_offset,  k, vmSymbols::type_annotations_name(),  vmSymbols::byte_array_signature());
D
duke 已提交
2282 2283 2284 2285
}

Handle java_lang_reflect_Field::create(TRAPS) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2286
  Symbol* name = vmSymbols::java_lang_reflect_Field();
2287
  Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
D
duke 已提交
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
  instanceKlassHandle klass (THREAD, k);
  // Ensure it is initialized
  klass->initialize(CHECK_NH);
  return klass->allocate_instance_handle(CHECK_NH);
}

oop java_lang_reflect_Field::clazz(oop reflect) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return reflect->obj_field(clazz_offset);
}

void java_lang_reflect_Field::set_clazz(oop reflect, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
   reflect->obj_field_put(clazz_offset, value);
}

oop java_lang_reflect_Field::name(oop field) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return field->obj_field(name_offset);
}

void java_lang_reflect_Field::set_name(oop field, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  field->obj_field_put(name_offset, value);
}

oop java_lang_reflect_Field::type(oop field) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return field->obj_field(type_offset);
}

void java_lang_reflect_Field::set_type(oop field, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  field->obj_field_put(type_offset, value);
}

int java_lang_reflect_Field::slot(oop reflect) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return reflect->int_field(slot_offset);
}

void java_lang_reflect_Field::set_slot(oop reflect, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  reflect->int_field_put(slot_offset, value);
}

int java_lang_reflect_Field::modifiers(oop field) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return field->int_field(modifiers_offset);
}

void java_lang_reflect_Field::set_modifiers(oop field, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  field->int_field_put(modifiers_offset, value);
}

bool java_lang_reflect_Field::has_signature_field() {
  return (signature_offset >= 0);
}

oop java_lang_reflect_Field::signature(oop field) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_signature_field(), "signature field must be present");
  return field->obj_field(signature_offset);
}

void java_lang_reflect_Field::set_signature(oop field, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_signature_field(), "signature field must be present");
  field->obj_field_put(signature_offset, value);
}

bool java_lang_reflect_Field::has_annotations_field() {
  return (annotations_offset >= 0);
}

oop java_lang_reflect_Field::annotations(oop field) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotations_field(), "annotations field must be present");
  return field->obj_field(annotations_offset);
}

void java_lang_reflect_Field::set_annotations(oop field, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_annotations_field(), "annotations field must be present");
  field->obj_field_put(annotations_offset, value);
}

2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
bool java_lang_reflect_Field::has_type_annotations_field() {
  return (type_annotations_offset >= 0);
}

oop java_lang_reflect_Field::type_annotations(oop field) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_type_annotations_field(), "type_annotations field must be present");
  return field->obj_field(type_annotations_offset);
}

void java_lang_reflect_Field::set_type_annotations(oop field, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  assert(has_type_annotations_field(), "type_annotations field must be present");
  field->obj_field_put(type_annotations_offset, value);
}
D
duke 已提交
2391 2392

void sun_reflect_ConstantPool::compute_offsets() {
2393
  Klass* k = SystemDictionary::reflect_ConstantPool_klass();
D
duke 已提交
2394 2395
  // This null test can be removed post beta
  if (k != NULL) {
2396 2397
    // The field is called ConstantPool* in the sun.reflect.ConstantPool class.
    compute_offset(_oop_offset, k, vmSymbols::ConstantPool_name(), vmSymbols::object_signature());
D
duke 已提交
2398 2399 2400
  }
}

2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
void java_lang_reflect_Parameter::compute_offsets() {
  Klass* k = SystemDictionary::reflect_Parameter_klass();
  if(NULL != k) {
    compute_offset(name_offset,        k, vmSymbols::name_name(),        vmSymbols::string_signature());
    compute_offset(modifiers_offset,   k, vmSymbols::modifiers_name(),   vmSymbols::int_signature());
    compute_offset(index_offset,       k, vmSymbols::index_name(),       vmSymbols::int_signature());
    compute_offset(executable_offset,  k, vmSymbols::executable_name(),  vmSymbols::executable_signature());
  }
}

Handle java_lang_reflect_Parameter::create(TRAPS) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  Symbol* name = vmSymbols::java_lang_reflect_Parameter();
  Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
  instanceKlassHandle klass (THREAD, k);
  // Ensure it is initialized
  klass->initialize(CHECK_NH);
  return klass->allocate_instance_handle(CHECK_NH);
}

oop java_lang_reflect_Parameter::name(oop param) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return param->obj_field(name_offset);
}

void java_lang_reflect_Parameter::set_name(oop param, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  param->obj_field_put(name_offset, value);
}

int java_lang_reflect_Parameter::modifiers(oop param) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return param->int_field(modifiers_offset);
}

void java_lang_reflect_Parameter::set_modifiers(oop param, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  param->int_field_put(modifiers_offset, value);
}

int java_lang_reflect_Parameter::index(oop param) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return param->int_field(index_offset);
}

void java_lang_reflect_Parameter::set_index(oop param, int value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  param->int_field_put(index_offset, value);
}

oop java_lang_reflect_Parameter::executable(oop param) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  return param->obj_field(executable_offset);
}

void java_lang_reflect_Parameter::set_executable(oop param, oop value) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
  param->obj_field_put(executable_offset, value);
}

D
duke 已提交
2461 2462 2463

Handle sun_reflect_ConstantPool::create(TRAPS) {
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2464
  Klass* k = SystemDictionary::reflect_ConstantPool_klass();
D
duke 已提交
2465 2466 2467 2468 2469 2470 2471
  instanceKlassHandle klass (THREAD, k);
  // Ensure it is initialized
  klass->initialize(CHECK_NH);
  return klass->allocate_instance_handle(CHECK_NH);
}


2472
void sun_reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) {
D
duke 已提交
2473
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2474 2475 2476
  oop mirror = value->pool_holder()->java_mirror();
  // Save the mirror to get back the constant pool.
  reflect->obj_field_put(_oop_offset, mirror);
D
duke 已提交
2477 2478
}

2479
ConstantPool* sun_reflect_ConstantPool::get_cp(oop reflect) {
D
duke 已提交
2480
  assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491

  oop mirror = reflect->obj_field(_oop_offset);
  Klass* k = java_lang_Class::as_Klass(mirror);
  assert(k->oop_is_instance(), "Must be");

  // Get the constant pool back from the klass.  Since class redefinition
  // merges the new constant pool into the old, this is essentially the
  // same constant pool as the original.  If constant pool merging is
  // no longer done in the future, this will have to change to save
  // the original.
  return InstanceKlass::cast(k)->constants();
D
duke 已提交
2492 2493 2494
}

void sun_reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() {
2495
  Klass* k = SystemDictionary::reflect_UnsafeStaticFieldAccessorImpl_klass();
D
duke 已提交
2496 2497
  // This null test can be removed post beta
  if (k != NULL) {
2498
    compute_offset(_base_offset, k,
D
duke 已提交
2499 2500 2501 2502
                   vmSymbols::base_name(), vmSymbols::object_signature());
  }
}

2503
oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
2504
  Klass* k = SystemDictionary::box_klass(type);
2505 2506 2507 2508
  if (k == NULL)  return NULL;
  instanceKlassHandle h (THREAD, k);
  if (!h->is_initialized())  h->initialize(CHECK_0);
  return h->allocate_instance(THREAD);
D
duke 已提交
2509 2510 2511 2512
}


oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
2513 2514
  oop box = initialize_and_allocate(type, CHECK_0);
  if (box == NULL)  return NULL;
D
duke 已提交
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
  switch (type) {
    case T_BOOLEAN:
      box->bool_field_put(value_offset, value->z);
      break;
    case T_CHAR:
      box->char_field_put(value_offset, value->c);
      break;
    case T_FLOAT:
      box->float_field_put(value_offset, value->f);
      break;
    case T_DOUBLE:
2526
      box->double_field_put(long_value_offset, value->d);
D
duke 已提交
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
      break;
    case T_BYTE:
      box->byte_field_put(value_offset, value->b);
      break;
    case T_SHORT:
      box->short_field_put(value_offset, value->s);
      break;
    case T_INT:
      box->int_field_put(value_offset, value->i);
      break;
    case T_LONG:
2538
      box->long_field_put(long_value_offset, value->j);
D
duke 已提交
2539 2540 2541 2542 2543 2544 2545 2546
      break;
    default:
      return NULL;
  }
  return box;
}


2547 2548 2549 2550 2551 2552 2553 2554 2555
BasicType java_lang_boxing_object::basic_type(oop box) {
  if (box == NULL)  return T_ILLEGAL;
  BasicType type = SystemDictionary::box_klass_type(box->klass());
  if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
    return T_ILLEGAL;
  return type;
}


D
duke 已提交
2556
BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
2557 2558 2559
  BasicType type = SystemDictionary::box_klass_type(box->klass());
  switch (type) {
  case T_BOOLEAN:
D
duke 已提交
2560
    value->z = box->bool_field(value_offset);
2561 2562
    break;
  case T_CHAR:
D
duke 已提交
2563
    value->c = box->char_field(value_offset);
2564 2565
    break;
  case T_FLOAT:
D
duke 已提交
2566
    value->f = box->float_field(value_offset);
2567 2568
    break;
  case T_DOUBLE:
2569
    value->d = box->double_field(long_value_offset);
2570 2571
    break;
  case T_BYTE:
D
duke 已提交
2572
    value->b = box->byte_field(value_offset);
2573 2574
    break;
  case T_SHORT:
D
duke 已提交
2575
    value->s = box->short_field(value_offset);
2576 2577
    break;
  case T_INT:
D
duke 已提交
2578
    value->i = box->int_field(value_offset);
2579 2580
    break;
  case T_LONG:
2581
    value->j = box->long_field(long_value_offset);
2582 2583 2584 2585 2586
    break;
  default:
    return T_ILLEGAL;
  } // end switch
  return type;
D
duke 已提交
2587 2588 2589 2590
}


BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
2591 2592 2593
  BasicType type = SystemDictionary::box_klass_type(box->klass());
  switch (type) {
  case T_BOOLEAN:
D
duke 已提交
2594
    box->bool_field_put(value_offset, value->z);
2595 2596
    break;
  case T_CHAR:
D
duke 已提交
2597
    box->char_field_put(value_offset, value->c);
2598 2599
    break;
  case T_FLOAT:
D
duke 已提交
2600
    box->float_field_put(value_offset, value->f);
2601 2602
    break;
  case T_DOUBLE:
2603
    box->double_field_put(long_value_offset, value->d);
2604 2605
    break;
  case T_BYTE:
D
duke 已提交
2606
    box->byte_field_put(value_offset, value->b);
2607 2608
    break;
  case T_SHORT:
D
duke 已提交
2609
    box->short_field_put(value_offset, value->s);
2610 2611
    break;
  case T_INT:
D
duke 已提交
2612
    box->int_field_put(value_offset, value->i);
2613 2614
    break;
  case T_LONG:
2615
    box->long_field_put(long_value_offset, value->j);
2616 2617 2618 2619 2620
    break;
  default:
    return T_ILLEGAL;
  } // end switch
  return type;
D
duke 已提交
2621 2622 2623
}


2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
  switch (type) {
  case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
  case T_CHAR:      st->print("%d", value->c);                      break;
  case T_BYTE:      st->print("%d", value->b);                      break;
  case T_SHORT:     st->print("%d", value->s);                      break;
  case T_INT:       st->print("%d", value->i);                      break;
  case T_LONG:      st->print(INT64_FORMAT, value->j);              break;
  case T_FLOAT:     st->print("%f", value->f);                      break;
  case T_DOUBLE:    st->print("%lf", value->d);                     break;
  default:          st->print("type %d?", type);                    break;
  }
}


D
duke 已提交
2639
// Support for java_lang_ref_Reference
2640 2641 2642 2643 2644 2645
HeapWord *java_lang_ref_Reference::pending_list_lock_addr() {
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::Reference_klass());
  address addr = ik->static_field_addr(static_lock_offset);
  return (HeapWord*) addr;
}

2646
oop java_lang_ref_Reference::pending_list_lock() {
2647
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::Reference_klass());
2648
  address addr = ik->static_field_addr(static_lock_offset);
2649 2650 2651 2652 2653
  if (UseCompressedOops) {
    return oopDesc::load_decode_heap_oop((narrowOop *)addr);
  } else {
    return oopDesc::load_decode_heap_oop((oop*)addr);
  }
D
duke 已提交
2654 2655
}

2656
HeapWord *java_lang_ref_Reference::pending_list_addr() {
2657
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::Reference_klass());
2658
  address addr = ik->static_field_addr(static_pending_offset);
2659 2660
  // XXX This might not be HeapWord aligned, almost rather be char *.
  return (HeapWord*)addr;
D
duke 已提交
2661 2662
}

2663 2664 2665 2666 2667 2668 2669
oop java_lang_ref_Reference::pending_list() {
  char *addr = (char *)pending_list_addr();
  if (UseCompressedOops) {
    return oopDesc::load_decode_heap_oop((narrowOop *)addr);
  } else {
    return oopDesc::load_decode_heap_oop((oop*)addr);
  }
D
duke 已提交
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
}


// Support for java_lang_ref_SoftReference

jlong java_lang_ref_SoftReference::timestamp(oop ref) {
  return ref->long_field(timestamp_offset);
}

jlong java_lang_ref_SoftReference::clock() {
2680
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::SoftReference_klass());
2681 2682
  jlong* offset = (jlong*)ik->static_field_addr(static_clock_offset);
  return *offset;
D
duke 已提交
2683 2684 2685
}

void java_lang_ref_SoftReference::set_clock(jlong value) {
2686
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::SoftReference_klass());
2687 2688
  jlong* offset = (jlong*)ik->static_field_addr(static_clock_offset);
  *offset = value;
D
duke 已提交
2689 2690
}

K
kbarrett 已提交
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
// Support for java_lang_ref_ReferenceQueue

oop java_lang_ref_ReferenceQueue::NULL_queue() {
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::ReferenceQueue_klass());
  oop mirror = ik->java_mirror();
  return mirror->obj_field(static_NULL_queue_offset);
}

oop java_lang_ref_ReferenceQueue::ENQUEUED_queue() {
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::ReferenceQueue_klass());
  oop mirror = ik->java_mirror();
  return mirror->obj_field(static_ENQUEUED_queue_offset);
}

void java_lang_ref_ReferenceQueue::compute_offsets() {
  Klass* k = SystemDictionary::ReferenceQueue_klass();
  compute_offset(static_NULL_queue_offset,
                 k,
                 vmSymbols::referencequeue_null_name(),
                 vmSymbols::referencequeue_signature());
  compute_offset(static_ENQUEUED_queue_offset,
                 k,
                 vmSymbols::referencequeue_enqueued_name(),
                 vmSymbols::referencequeue_signature());
}

2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
// Support for java_lang_invoke_DirectMethodHandle

int java_lang_invoke_DirectMethodHandle::_member_offset;

oop java_lang_invoke_DirectMethodHandle::member(oop dmh) {
  oop member_name = NULL;
  bool is_dmh = dmh->is_oop() && java_lang_invoke_DirectMethodHandle::is_instance(dmh);
  assert(is_dmh, "a DirectMethodHandle oop is expected");
  if (is_dmh) {
    member_name = dmh->obj_field(member_offset_in_bytes());
  }
  return member_name;
}

void java_lang_invoke_DirectMethodHandle::compute_offsets() {
  Klass* klass_oop = SystemDictionary::DirectMethodHandle_klass();
  if (klass_oop != NULL && EnableInvokeDynamic) {
    compute_offset(_member_offset, klass_oop, vmSymbols::member_name(), vmSymbols::java_lang_invoke_MemberName_signature());
  }
}
D
duke 已提交
2737

2738
// Support for java_lang_invoke_MethodHandle
2739

2740
int java_lang_invoke_MethodHandle::_type_offset;
2741
int java_lang_invoke_MethodHandle::_form_offset;
2742

2743 2744 2745 2746 2747
int java_lang_invoke_MemberName::_clazz_offset;
int java_lang_invoke_MemberName::_name_offset;
int java_lang_invoke_MemberName::_type_offset;
int java_lang_invoke_MemberName::_flags_offset;
int java_lang_invoke_MemberName::_vmtarget_offset;
2748
int java_lang_invoke_MemberName::_vmloader_offset;
2749
int java_lang_invoke_MemberName::_vmindex_offset;
2750

2751
int java_lang_invoke_LambdaForm::_vmentry_offset;
2752

2753
void java_lang_invoke_MethodHandle::compute_offsets() {
2754
  Klass* klass_oop = SystemDictionary::MethodHandle_klass();
2755
  if (klass_oop != NULL && EnableInvokeDynamic) {
2756 2757 2758 2759 2760
    compute_offset(_type_offset, klass_oop, vmSymbols::type_name(), vmSymbols::java_lang_invoke_MethodType_signature());
    compute_optional_offset(_form_offset, klass_oop, vmSymbols::form_name(), vmSymbols::java_lang_invoke_LambdaForm_signature());
    if (_form_offset == 0) {
      EnableInvokeDynamic = false;
    }
2761 2762 2763
  }
}

2764
void java_lang_invoke_MemberName::compute_offsets() {
2765
  Klass* klass_oop = SystemDictionary::MemberName_klass();
2766 2767 2768 2769 2770 2771
  if (klass_oop != NULL && EnableInvokeDynamic) {
    compute_offset(_clazz_offset,     klass_oop, vmSymbols::clazz_name(),     vmSymbols::class_signature());
    compute_offset(_name_offset,      klass_oop, vmSymbols::name_name(),      vmSymbols::string_signature());
    compute_offset(_type_offset,      klass_oop, vmSymbols::type_name(),      vmSymbols::object_signature());
    compute_offset(_flags_offset,     klass_oop, vmSymbols::flags_name(),     vmSymbols::int_signature());
    MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2772 2773 2774
  }
}

2775
void java_lang_invoke_LambdaForm::compute_offsets() {
2776
  Klass* klass_oop = SystemDictionary::LambdaForm_klass();
2777 2778
  if (klass_oop != NULL && EnableInvokeDynamic) {
    compute_offset(_vmentry_offset, klass_oop, vmSymbols::vmentry_name(), vmSymbols::java_lang_invoke_MemberName_signature());
2779 2780 2781
  }
}

2782
oop java_lang_invoke_MethodHandle::type(oop mh) {
2783 2784 2785
  return mh->obj_field(_type_offset);
}

2786
void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) {
2787 2788 2789
  mh->obj_field_put(_type_offset, mtype);
}

2790 2791 2792
oop java_lang_invoke_MethodHandle::form(oop mh) {
  assert(_form_offset != 0, "");
  return mh->obj_field(_form_offset);
2793 2794
}

2795 2796 2797
void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) {
  assert(_form_offset != 0, "");
  mh->obj_field_put(_form_offset, lform);
2798 2799 2800 2801
}

/// MemberName accessors

2802
oop java_lang_invoke_MemberName::clazz(oop mname) {
2803 2804 2805 2806
  assert(is_instance(mname), "wrong type");
  return mname->obj_field(_clazz_offset);
}

2807
void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) {
2808 2809 2810 2811
  assert(is_instance(mname), "wrong type");
  mname->obj_field_put(_clazz_offset, clazz);
}

2812
oop java_lang_invoke_MemberName::name(oop mname) {
2813 2814 2815 2816
  assert(is_instance(mname), "wrong type");
  return mname->obj_field(_name_offset);
}

2817
void java_lang_invoke_MemberName::set_name(oop mname, oop name) {
2818 2819 2820 2821
  assert(is_instance(mname), "wrong type");
  mname->obj_field_put(_name_offset, name);
}

2822
oop java_lang_invoke_MemberName::type(oop mname) {
2823 2824 2825 2826
  assert(is_instance(mname), "wrong type");
  return mname->obj_field(_type_offset);
}

2827
void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
2828 2829 2830 2831
  assert(is_instance(mname), "wrong type");
  mname->obj_field_put(_type_offset, type);
}

2832
int java_lang_invoke_MemberName::flags(oop mname) {
2833 2834 2835 2836
  assert(is_instance(mname), "wrong type");
  return mname->int_field(_flags_offset);
}

2837
void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
2838 2839 2840 2841
  assert(is_instance(mname), "wrong type");
  mname->int_field_put(_flags_offset, flags);
}

2842
Metadata* java_lang_invoke_MemberName::vmtarget(oop mname) {
2843
  assert(is_instance(mname), "wrong type");
2844
  return (Metadata*)mname->address_field(_vmtarget_offset);
2845 2846
}

2847 2848 2849 2850 2851
bool java_lang_invoke_MemberName::is_method(oop mname) {
  assert(is_instance(mname), "must be MemberName");
  return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
}

2852
void java_lang_invoke_MemberName::set_vmtarget(oop mname, Metadata* ref) {
2853
  assert(is_instance(mname), "wrong type");
2854
  // check the type of the vmtarget
2855
  oop dependency = NULL;
2856 2857 2858 2859 2860 2861 2862
  if (ref != NULL) {
    switch (flags(mname) & (MN_IS_METHOD |
                            MN_IS_CONSTRUCTOR |
                            MN_IS_FIELD)) {
    case MN_IS_METHOD:
    case MN_IS_CONSTRUCTOR:
      assert(ref->is_method(), "should be a method");
2863
      dependency = ((Method*)ref)->method_holder()->java_mirror();
2864 2865 2866
      break;
    case MN_IS_FIELD:
      assert(ref->is_klass(), "should be a class");
2867
      dependency = ((Klass*)ref)->java_mirror();
2868 2869 2870 2871 2872
      break;
    default:
      ShouldNotReachHere();
    }
  }
2873
  mname->address_field_put(_vmtarget_offset, (address)ref);
2874 2875 2876 2877
  // Add a reference to the loader (actually mirror because anonymous classes will not have
  // distinct loaders) to ensure the metadata is kept alive
  // This mirror may be different than the one in clazz field.
  mname->obj_field_put(_vmloader_offset, dependency);
2878 2879
}

2880
intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
2881
  assert(is_instance(mname), "wrong type");
2882
  return (intptr_t) mname->address_field(_vmindex_offset);
2883 2884
}

2885
void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
2886
  assert(is_instance(mname), "wrong type");
2887
  mname->address_field_put(_vmindex_offset, (address) index);
2888 2889
}

2890 2891 2892 2893 2894 2895 2896 2897 2898
bool java_lang_invoke_MemberName::equals(oop mn1, oop mn2) {
  if (mn1 == mn2) {
     return true;
  }
  return (vmtarget(mn1) == vmtarget(mn2) && flags(mn1) == flags(mn2) &&
          vmindex(mn1) == vmindex(mn2) &&
          clazz(mn1) == clazz(mn2));
}

2899 2900 2901
oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
  assert(is_instance(lform), "wrong type");
  return lform->obj_field(_vmentry_offset);
2902 2903 2904
}


2905
// Support for java_lang_invoke_MethodType
2906

2907 2908
int java_lang_invoke_MethodType::_rtype_offset;
int java_lang_invoke_MethodType::_ptypes_offset;
2909

2910
void java_lang_invoke_MethodType::compute_offsets() {
2911
  Klass* k = SystemDictionary::MethodType_klass();
2912 2913 2914 2915 2916 2917
  if (k != NULL) {
    compute_offset(_rtype_offset,  k, vmSymbols::rtype_name(),  vmSymbols::class_signature());
    compute_offset(_ptypes_offset, k, vmSymbols::ptypes_name(), vmSymbols::class_array_signature());
  }
}

2918
void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
2919 2920 2921 2922 2923 2924 2925 2926 2927
  st->print("(");
  objArrayOop pts = ptypes(mt);
  for (int i = 0, limit = pts->length(); i < limit; i++) {
    java_lang_Class::print_signature(pts->obj_at(i), st);
  }
  st->print(")");
  java_lang_Class::print_signature(rtype(mt), st);
}

2928
Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found, TRAPS) {
2929 2930 2931 2932 2933
  ResourceMark rm;
  stringStream buffer(128);
  print_signature(mt, &buffer);
  const char* sigstr =       buffer.base();
  int         siglen = (int) buffer.size();
2934 2935 2936 2937 2938 2939 2940
  Symbol *name;
  if (!intern_if_not_found) {
    name = SymbolTable::probe(sigstr, siglen);
  } else {
    name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
  }
  return name;
2941 2942
}

2943
bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
2944 2945
  if (mt1 == mt2)
    return true;
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
  if (rtype(mt1) != rtype(mt2))
    return false;
  if (ptype_count(mt1) != ptype_count(mt2))
    return false;
  for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
    if (ptype(mt1, i) != ptype(mt2, i))
      return false;
  }
  return true;
}

2957
oop java_lang_invoke_MethodType::rtype(oop mt) {
2958 2959 2960 2961
  assert(is_instance(mt), "must be a MethodType");
  return mt->obj_field(_rtype_offset);
}

2962
objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) {
2963 2964 2965 2966
  assert(is_instance(mt), "must be a MethodType");
  return (objArrayOop) mt->obj_field(_ptypes_offset);
}

2967
oop java_lang_invoke_MethodType::ptype(oop mt, int idx) {
2968 2969 2970
  return ptypes(mt)->obj_at(idx);
}

2971
int java_lang_invoke_MethodType::ptype_count(oop mt) {
2972 2973 2974
  return ptypes(mt)->length();
}

2975 2976 2977 2978 2979 2980 2981
int java_lang_invoke_MethodType::ptype_slot_count(oop mt) {
  objArrayOop pts = ptypes(mt);
  int count = pts->length();
  int slots = 0;
  for (int i = 0; i < count; i++) {
    BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i));
    slots += type2size[bt];
2982
  }
2983
  return slots;
2984 2985
}

2986 2987 2988
int java_lang_invoke_MethodType::rtype_slot_count(oop mt) {
  BasicType bt = java_lang_Class::as_BasicType(rtype(mt));
  return type2size[bt];
2989 2990
}

2991

2992
// Support for java_lang_invoke_CallSite
2993

2994
int java_lang_invoke_CallSite::_target_offset;
2995

2996
void java_lang_invoke_CallSite::compute_offsets() {
2997
  if (!EnableInvokeDynamic)  return;
2998
  Klass* k = SystemDictionary::CallSite_klass();
2999
  if (k != NULL) {
3000
    compute_offset(_target_offset, k, vmSymbols::target_name(), vmSymbols::java_lang_invoke_MethodHandle_signature());
3001 3002 3003
  }
}

3004

D
duke 已提交
3005 3006 3007 3008 3009
// Support for java_security_AccessControlContext

int java_security_AccessControlContext::_context_offset = 0;
int java_security_AccessControlContext::_privilegedContext_offset = 0;
int java_security_AccessControlContext::_isPrivileged_offset = 0;
M
mullan 已提交
3010
int java_security_AccessControlContext::_isAuthorized_offset = -1;
D
duke 已提交
3011 3012 3013 3014

void java_security_AccessControlContext::compute_offsets() {
  assert(_isPrivileged_offset == 0, "offsets should be initialized only once");
  fieldDescriptor fd;
3015
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::AccessControlContext_klass());
D
duke 已提交
3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030

  if (!ik->find_local_field(vmSymbols::context_name(), vmSymbols::protectiondomain_signature(), &fd)) {
    fatal("Invalid layout of java.security.AccessControlContext");
  }
  _context_offset = fd.offset();

  if (!ik->find_local_field(vmSymbols::privilegedContext_name(), vmSymbols::accesscontrolcontext_signature(), &fd)) {
    fatal("Invalid layout of java.security.AccessControlContext");
  }
  _privilegedContext_offset = fd.offset();

  if (!ik->find_local_field(vmSymbols::isPrivileged_name(), vmSymbols::bool_signature(), &fd)) {
    fatal("Invalid layout of java.security.AccessControlContext");
  }
  _isPrivileged_offset = fd.offset();
M
mullan 已提交
3031 3032 3033 3034 3035

  // The offset may not be present for bootstrapping with older JDK.
  if (ik->find_local_field(vmSymbols::isAuthorized_name(), vmSymbols::bool_signature(), &fd)) {
    _isAuthorized_offset = fd.offset();
  }
D
duke 已提交
3036 3037 3038
}


M
mullan 已提交
3039 3040 3041 3042 3043 3044
bool java_security_AccessControlContext::is_authorized(Handle context) {
  assert(context.not_null() && context->klass() == SystemDictionary::AccessControlContext_klass(), "Invalid type");
  assert(_isAuthorized_offset != -1, "should be set");
  return context->bool_field(_isAuthorized_offset) != 0;
}

D
duke 已提交
3045 3046 3047
oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) {
  assert(_isPrivileged_offset != 0, "offsets should have been initialized");
  // Ensure klass is initialized
3048
  InstanceKlass::cast(SystemDictionary::AccessControlContext_klass())->initialize(CHECK_0);
D
duke 已提交
3049
  // Allocate result
3050
  oop result = InstanceKlass::cast(SystemDictionary::AccessControlContext_klass())->allocate_instance(CHECK_0);
D
duke 已提交
3051 3052 3053 3054
  // Fill in values
  result->obj_field_put(_context_offset, context());
  result->obj_field_put(_privilegedContext_offset, privileged_context());
  result->bool_field_put(_isPrivileged_offset, isPrivileged);
3055 3056 3057 3058
  // whitelist AccessControlContexts created by the JVM if present
  if (_isAuthorized_offset != -1) {
    result->bool_field_put(_isAuthorized_offset, true);
  }
D
duke 已提交
3059 3060 3061 3062 3063
  return result;
}


// Support for java_lang_ClassLoader
3064

3065
bool java_lang_ClassLoader::offsets_computed = false;
3066
int  java_lang_ClassLoader::_loader_data_offset = -1;
3067 3068
int  java_lang_ClassLoader::parallelCapable_offset = -1;

3069 3070 3071 3072 3073 3074 3075 3076 3077
ClassLoaderData** java_lang_ClassLoader::loader_data_addr(oop loader) {
    assert(loader != NULL && loader->is_oop(), "loader must be oop");
    return (ClassLoaderData**) loader->address_field_addr(_loader_data_offset);
}

ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) {
  return *java_lang_ClassLoader::loader_data_addr(loader);
}

3078 3079 3080 3081 3082
void java_lang_ClassLoader::compute_offsets() {
  assert(!offsets_computed, "offsets should be initialized only once");
  offsets_computed = true;

  // The field indicating parallelCapable (parallelLockMap) is only present starting in 7,
3083
  Klass* k1 = SystemDictionary::ClassLoader_klass();
3084 3085
  compute_optional_offset(parallelCapable_offset,
    k1, vmSymbols::parallelCapable_name(), vmSymbols::concurrenthashmap_signature());
3086 3087

  CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3088
}
D
duke 已提交
3089 3090

oop java_lang_ClassLoader::parent(oop loader) {
3091
  assert(is_instance(loader), "loader must be oop");
D
duke 已提交
3092 3093 3094
  return loader->obj_field(parent_offset);
}

3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110
bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) {
  assert(is_instance(loader), "loader must be oop");
  assert(cl == NULL || is_instance(cl), "cl argument must be oop");
  oop acl = loader;
  debug_only(jint loop_count = 0);
  // This loop taken verbatim from ClassLoader.java:
  do {
    acl = parent(acl);
    if (cl == acl) {
      return true;
    }
    assert(++loop_count > 0, "loop_count overflow");
  } while (acl != NULL);
  return false;
}

D
duke 已提交
3111

3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
// For class loader classes, parallelCapable defined
// based on non-null field
// Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it
bool java_lang_ClassLoader::parallelCapable(oop class_loader) {
  if (!JDK_Version::is_gte_jdk17x_version()
     || parallelCapable_offset == -1) {
     // Default for backward compatibility is false
     return false;
  }
  return (class_loader->obj_field(parallelCapable_offset) != NULL);
}

D
duke 已提交
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
bool java_lang_ClassLoader::is_trusted_loader(oop loader) {
  // Fix for 4474172; see evaluation for more details
  loader = non_reflection_class_loader(loader);

  oop cl = SystemDictionary::java_system_loader();
  while(cl != NULL) {
    if (cl == loader) return true;
    cl = parent(cl);
  }
  return false;
}

oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) {
  if (loader != NULL) {
    // See whether this is one of the class loaders associated with
    // the generated bytecodes for reflection, and if so, "magically"
    // delegate to its parent to prevent class loading from occurring
    // in places where applications using reflection didn't expect it.
3142
    Klass* delegating_cl_class = SystemDictionary::reflect_DelegatingClassLoader_klass();
D
duke 已提交
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
    // This might be null in non-1.4 JDKs
    if (delegating_cl_class != NULL && loader->is_a(delegating_cl_class)) {
      return parent(loader);
    }
  }
  return loader;
}


// Support for java_lang_System
int java_lang_System::in_offset_in_bytes() {
3154
  return (InstanceMirrorKlass::offset_of_static_fields() + static_in_offset);
D
duke 已提交
3155 3156 3157 3158
}


int java_lang_System::out_offset_in_bytes() {
3159
  return (InstanceMirrorKlass::offset_of_static_fields() + static_out_offset);
D
duke 已提交
3160 3161 3162 3163
}


int java_lang_System::err_offset_in_bytes() {
3164
  return (InstanceMirrorKlass::offset_of_static_fields() + static_err_offset);
D
duke 已提交
3165 3166 3167
}


M
mullan 已提交
3168 3169 3170 3171 3172 3173 3174 3175 3176
bool java_lang_System::has_security_manager() {
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::System_klass());
  address addr = ik->static_field_addr(static_security_offset);
  if (UseCompressedOops) {
    return oopDesc::load_decode_heap_oop((narrowOop *)addr) != NULL;
  } else {
    return oopDesc::load_decode_heap_oop((oop*)addr) != NULL;
  }
}
D
duke 已提交
3177

3178 3179 3180 3181
int java_lang_Class::_klass_offset;
int java_lang_Class::_array_klass_offset;
int java_lang_Class::_oop_size_offset;
int java_lang_Class::_static_oop_field_count_offset;
3182
int java_lang_Class::_class_loader_offset;
3183 3184 3185
int java_lang_Class::_protection_domain_offset;
int java_lang_Class::_init_lock_offset;
int java_lang_Class::_signers_offset;
3186
GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
D
duke 已提交
3187 3188 3189 3190
int java_lang_Throwable::backtrace_offset;
int java_lang_Throwable::detailMessage_offset;
int java_lang_Throwable::cause_offset;
int java_lang_Throwable::stackTrace_offset;
3191
int java_lang_Throwable::static_unassigned_stacktrace_offset;
D
duke 已提交
3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
int java_lang_reflect_AccessibleObject::override_offset;
int java_lang_reflect_Method::clazz_offset;
int java_lang_reflect_Method::name_offset;
int java_lang_reflect_Method::returnType_offset;
int java_lang_reflect_Method::parameterTypes_offset;
int java_lang_reflect_Method::exceptionTypes_offset;
int java_lang_reflect_Method::slot_offset;
int java_lang_reflect_Method::modifiers_offset;
int java_lang_reflect_Method::signature_offset;
int java_lang_reflect_Method::annotations_offset;
int java_lang_reflect_Method::parameter_annotations_offset;
int java_lang_reflect_Method::annotation_default_offset;
3204
int java_lang_reflect_Method::type_annotations_offset;
D
duke 已提交
3205 3206 3207 3208 3209 3210 3211 3212
int java_lang_reflect_Constructor::clazz_offset;
int java_lang_reflect_Constructor::parameterTypes_offset;
int java_lang_reflect_Constructor::exceptionTypes_offset;
int java_lang_reflect_Constructor::slot_offset;
int java_lang_reflect_Constructor::modifiers_offset;
int java_lang_reflect_Constructor::signature_offset;
int java_lang_reflect_Constructor::annotations_offset;
int java_lang_reflect_Constructor::parameter_annotations_offset;
3213
int java_lang_reflect_Constructor::type_annotations_offset;
D
duke 已提交
3214 3215 3216 3217 3218 3219 3220
int java_lang_reflect_Field::clazz_offset;
int java_lang_reflect_Field::name_offset;
int java_lang_reflect_Field::type_offset;
int java_lang_reflect_Field::slot_offset;
int java_lang_reflect_Field::modifiers_offset;
int java_lang_reflect_Field::signature_offset;
int java_lang_reflect_Field::annotations_offset;
3221
int java_lang_reflect_Field::type_annotations_offset;
3222 3223 3224 3225
int java_lang_reflect_Parameter::name_offset;
int java_lang_reflect_Parameter::modifiers_offset;
int java_lang_reflect_Parameter::index_offset;
int java_lang_reflect_Parameter::executable_offset;
D
duke 已提交
3226
int java_lang_boxing_object::value_offset;
3227
int java_lang_boxing_object::long_value_offset;
D
duke 已提交
3228 3229 3230 3231 3232 3233 3234
int java_lang_ref_Reference::referent_offset;
int java_lang_ref_Reference::queue_offset;
int java_lang_ref_Reference::next_offset;
int java_lang_ref_Reference::discovered_offset;
int java_lang_ref_Reference::static_lock_offset;
int java_lang_ref_Reference::static_pending_offset;
int java_lang_ref_Reference::number_of_fake_oop_fields;
K
kbarrett 已提交
3235 3236
int java_lang_ref_ReferenceQueue::static_NULL_queue_offset;
int java_lang_ref_ReferenceQueue::static_ENQUEUED_queue_offset;
D
duke 已提交
3237 3238 3239 3240 3241 3242
int java_lang_ref_SoftReference::timestamp_offset;
int java_lang_ref_SoftReference::static_clock_offset;
int java_lang_ClassLoader::parent_offset;
int java_lang_System::static_in_offset;
int java_lang_System::static_out_offset;
int java_lang_System::static_err_offset;
M
mullan 已提交
3243
int java_lang_System::static_security_offset;
D
duke 已提交
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
int java_lang_StackTraceElement::declaringClass_offset;
int java_lang_StackTraceElement::methodName_offset;
int java_lang_StackTraceElement::fileName_offset;
int java_lang_StackTraceElement::lineNumber_offset;
int java_lang_AssertionStatusDirectives::classes_offset;
int java_lang_AssertionStatusDirectives::classEnabled_offset;
int java_lang_AssertionStatusDirectives::packages_offset;
int java_lang_AssertionStatusDirectives::packageEnabled_offset;
int java_lang_AssertionStatusDirectives::deflt_offset;
int java_nio_Buffer::_limit_offset;
3254 3255 3256 3257 3258 3259 3260
#if INCLUDE_ALL_GCS
int com_alibaba_tenant_TenantContainer::_allocation_context_offset;
int com_alibaba_tenant_TenantContainer::_tenant_id_offset;
int com_alibaba_tenant_TenantContainer::_tenant_state_offset;

int com_alibaba_tenant_TenantState::_static_state_offsets[com_alibaba_tenant_TenantState::TS_SIZE] = { 0 };
#endif // INCLUDE_ALL_GCS
D
duke 已提交
3261
int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset = 0;
3262
int sun_reflect_ConstantPool::_oop_offset;
D
duke 已提交
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
int sun_reflect_UnsafeStaticFieldAccessorImpl::_base_offset;


// Support for java_lang_StackTraceElement

void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
  element->obj_field_put(fileName_offset, value);
}

void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
  element->obj_field_put(declaringClass_offset, value);
}

void java_lang_StackTraceElement::set_methodName(oop element, oop value) {
  element->obj_field_put(methodName_offset, value);
}

void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
  element->int_field_put(lineNumber_offset, value);
}


// Support for java Assertions - java_lang_AssertionStatusDirectives.

void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
  o->obj_field_put(classes_offset, val);
}

void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
  o->obj_field_put(classEnabled_offset, val);
}

void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
  o->obj_field_put(packages_offset, val);
}

void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
  o->obj_field_put(packageEnabled_offset, val);
}

void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
  o->bool_field_put(deflt_offset, val);
}


// Support for intrinsification of java.nio.Buffer.checkIndex
int java_nio_Buffer::limit_offset() {
  return _limit_offset;
}


void java_nio_Buffer::compute_offsets() {
3315
  Klass* k = SystemDictionary::nio_Buffer_klass();
3316 3317
  assert(k != NULL, "must be loaded in 1.4+");
  compute_offset(_limit_offset, k, vmSymbols::limit_name(), vmSymbols::int_signature());
D
duke 已提交
3318 3319
}

3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
#if INCLUDE_ALL_GCS

// Support for com.alibaba.tenant.TenantContainer

void com_alibaba_tenant_TenantContainer::compute_offsets() {
  Klass* k = SystemDictionary::com_alibaba_tenant_TenantContainer_klass();
  assert(k != NULL, "Cannot find TenantContainer in current JDK");
  compute_offset(_tenant_id_offset, k, vmSymbols::tenant_id_address(), vmSymbols::long_signature());
  compute_offset(_allocation_context_offset, k, vmSymbols::allocation_context_address(), vmSymbols::long_signature());
  compute_offset(_tenant_state_offset, k, vmSymbols::state_name(), vmSymbols::com_alibaba_tenant_TenantState_signature());
}

jlong com_alibaba_tenant_TenantContainer::get_tenant_id(oop obj) {
  assert(obj != NULL, "TenantContainer object cannot be NULL");
  return obj->long_field(_tenant_id_offset);
}

G1TenantAllocationContext* com_alibaba_tenant_TenantContainer::get_tenant_allocation_context(oop obj) {
  assert(obj != NULL, "TenantContainer object cannot be NULL");
  return (G1TenantAllocationContext*)(obj->long_field(_allocation_context_offset));
}

void com_alibaba_tenant_TenantContainer::set_tenant_allocation_context(oop obj, G1TenantAllocationContext* context) {
  assert(obj != NULL, "TenantContainer object cannot be NULL");
  obj->long_field_put(_allocation_context_offset, (jlong)context);
}

bool com_alibaba_tenant_TenantContainer::is_dead(oop obj) {
  assert(obj != NULL, "TenantContainer object cannot be NULL");
  int state = com_alibaba_tenant_TenantState::state_of(obj);
  return state == com_alibaba_tenant_TenantState::TS_STOPPING
         || state == com_alibaba_tenant_TenantState::TS_DEAD;
}

oop com_alibaba_tenant_TenantContainer::get_tenant_state(oop obj) {
  assert(obj != NULL, "TenantContainer object cannot be NULL");
3356
  return obj->obj_field(_tenant_state_offset);
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
}

// Support for com.alibaba.tenant.TenantState

int com_alibaba_tenant_TenantState::state_of(oop tenant_obj) {
  assert(tenant_obj != NULL, "TenantContainer ");

  oop tenant_state = com_alibaba_tenant_TenantContainer::get_tenant_state(tenant_obj);
  InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::com_alibaba_tenant_TenantState_klass());

  for (int i = TS_STARTING; i < TS_SIZE; ++i) {
    assert(_static_state_offsets[i] == i * heapOopSize, "Must have been initialized");
    address addr = ik->static_field_addr(_static_state_offsets[i]);
    oop o = NULL;
    if (UseCompressedOops) {
      o = oopDesc::load_decode_heap_oop((narrowOop*)addr);
    } else {
      o = oopDesc::load_decode_heap_oop((oop*)addr);
    }
    assert(!oopDesc::is_null(o), "sanity");
    if (tenant_state == o) {
      return i;
    }
  }

  ShouldNotReachHere();
3383
  return -1;
3384 3385 3386 3387
}

#endif // INCLUDE_ALL_GCS

D
duke 已提交
3388 3389 3390 3391 3392
void java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(TRAPS) {
  if (_owner_offset != 0) return;

  assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later");
  SystemDictionary::load_abstract_ownable_synchronizer_klass(CHECK);
3393
  Klass* k = SystemDictionary::abstract_ownable_synchronizer_klass();
3394
  compute_offset(_owner_offset, k,
D
duke 已提交
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
                 vmSymbols::exclusive_owner_thread_name(), vmSymbols::thread_signature());
}

oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
  assert(_owner_offset != 0, "Must be initialized");
  return obj->obj_field(_owner_offset);
}

// Compute hard-coded offsets
// Invoked before SystemDictionary::initialize, so pre-loaded classes
// are not available to determine the offset_of_static_fields.
void JavaClasses::compute_hard_coded_offsets() {
3407
  const int x = heapOopSize;
3408
  const int header = instanceOopDesc::base_offset_in_bytes();
D
duke 已提交
3409 3410 3411 3412 3413 3414

  // Throwable Class
  java_lang_Throwable::backtrace_offset  = java_lang_Throwable::hc_backtrace_offset  * x + header;
  java_lang_Throwable::detailMessage_offset = java_lang_Throwable::hc_detailMessage_offset * x + header;
  java_lang_Throwable::cause_offset      = java_lang_Throwable::hc_cause_offset      * x + header;
  java_lang_Throwable::stackTrace_offset = java_lang_Throwable::hc_stackTrace_offset * x + header;
3415
  java_lang_Throwable::static_unassigned_stacktrace_offset = java_lang_Throwable::hc_static_unassigned_stacktrace_offset *  x;
D
duke 已提交
3416 3417

  // java_lang_boxing_object
3418 3419
  java_lang_boxing_object::value_offset = java_lang_boxing_object::hc_value_offset + header;
  java_lang_boxing_object::long_value_offset = align_size_up((java_lang_boxing_object::hc_value_offset + header), BytesPerLong);
D
duke 已提交
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432

  // java_lang_ref_Reference:
  java_lang_ref_Reference::referent_offset = java_lang_ref_Reference::hc_referent_offset * x + header;
  java_lang_ref_Reference::queue_offset = java_lang_ref_Reference::hc_queue_offset * x + header;
  java_lang_ref_Reference::next_offset  = java_lang_ref_Reference::hc_next_offset * x + header;
  java_lang_ref_Reference::discovered_offset  = java_lang_ref_Reference::hc_discovered_offset * x + header;
  java_lang_ref_Reference::static_lock_offset = java_lang_ref_Reference::hc_static_lock_offset *  x;
  java_lang_ref_Reference::static_pending_offset = java_lang_ref_Reference::hc_static_pending_offset * x;
  // Artificial fields for java_lang_ref_Reference
  // The first field is for the discovered field added in 1.4
  java_lang_ref_Reference::number_of_fake_oop_fields = 1;

  // java_lang_ref_SoftReference Class
3433
  java_lang_ref_SoftReference::timestamp_offset = align_size_up((java_lang_ref_SoftReference::hc_timestamp_offset * x + header), BytesPerLong);
D
duke 已提交
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443
  // Don't multiply static fields because they are always in wordSize units
  java_lang_ref_SoftReference::static_clock_offset = java_lang_ref_SoftReference::hc_static_clock_offset * x;

  // java_lang_ClassLoader
  java_lang_ClassLoader::parent_offset = java_lang_ClassLoader::hc_parent_offset * x + header;

  // java_lang_System
  java_lang_System::static_in_offset  = java_lang_System::hc_static_in_offset  * x;
  java_lang_System::static_out_offset = java_lang_System::hc_static_out_offset * x;
  java_lang_System::static_err_offset = java_lang_System::hc_static_err_offset * x;
M
mullan 已提交
3444
  java_lang_System::static_security_offset = java_lang_System::hc_static_security_offset * x;
D
duke 已提交
3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461

  // java_lang_StackTraceElement
  java_lang_StackTraceElement::declaringClass_offset = java_lang_StackTraceElement::hc_declaringClass_offset  * x + header;
  java_lang_StackTraceElement::methodName_offset = java_lang_StackTraceElement::hc_methodName_offset * x + header;
  java_lang_StackTraceElement::fileName_offset   = java_lang_StackTraceElement::hc_fileName_offset   * x + header;
  java_lang_StackTraceElement::lineNumber_offset = java_lang_StackTraceElement::hc_lineNumber_offset * x + header;
  java_lang_AssertionStatusDirectives::classes_offset = java_lang_AssertionStatusDirectives::hc_classes_offset * x + header;
  java_lang_AssertionStatusDirectives::classEnabled_offset = java_lang_AssertionStatusDirectives::hc_classEnabled_offset * x + header;
  java_lang_AssertionStatusDirectives::packages_offset = java_lang_AssertionStatusDirectives::hc_packages_offset * x + header;
  java_lang_AssertionStatusDirectives::packageEnabled_offset = java_lang_AssertionStatusDirectives::hc_packageEnabled_offset * x + header;
  java_lang_AssertionStatusDirectives::deflt_offset = java_lang_AssertionStatusDirectives::hc_deflt_offset * x + header;

}


// Compute non-hard-coded field offsets of all the classes in this file
void JavaClasses::compute_offsets() {
3462 3463
  // java_lang_Class::compute_offsets was called earlier in bootstrap
  java_lang_ClassLoader::compute_offsets();
D
duke 已提交
3464 3465
  java_lang_Thread::compute_offsets();
  java_lang_ThreadGroup::compute_offsets();
3466
  if (EnableInvokeDynamic) {
3467
    java_lang_invoke_MethodHandle::compute_offsets();
3468
    java_lang_invoke_DirectMethodHandle::compute_offsets();
3469
    java_lang_invoke_MemberName::compute_offsets();
3470
    java_lang_invoke_LambdaForm::compute_offsets();
3471 3472
    java_lang_invoke_MethodType::compute_offsets();
    java_lang_invoke_CallSite::compute_offsets();
3473
  }
D
duke 已提交
3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489
  java_security_AccessControlContext::compute_offsets();
  // Initialize reflection classes. The layouts of these classes
  // changed with the new reflection implementation in JDK 1.4, and
  // since the Universe doesn't know what JDK version it is until this
  // point we defer computation of these offsets until now.
  java_lang_reflect_AccessibleObject::compute_offsets();
  java_lang_reflect_Method::compute_offsets();
  java_lang_reflect_Constructor::compute_offsets();
  java_lang_reflect_Field::compute_offsets();
  if (JDK_Version::is_gte_jdk14x_version()) {
    java_nio_Buffer::compute_offsets();
  }
  if (JDK_Version::is_gte_jdk15x_version()) {
    sun_reflect_ConstantPool::compute_offsets();
    sun_reflect_UnsafeStaticFieldAccessorImpl::compute_offsets();
  }
3490 3491
  if (JDK_Version::is_jdk18x_version())
    java_lang_reflect_Parameter::compute_offsets();
3492

K
kbarrett 已提交
3493 3494
  java_lang_ref_ReferenceQueue::compute_offsets();

3495 3496
  // generated interpreter code wants to know about the offsets we just computed:
  AbstractAssembler::update_delayed_values();
3497 3498 3499 3500

  if(MultiTenant) {
    com_alibaba_tenant_TenantContainer::compute_offsets();
  }
D
duke 已提交
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510
}

#ifndef PRODUCT

// These functions exist to assert the validity of hard-coded field offsets to guard
// against changes in the class files

bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
  EXCEPTION_MARK;
  fieldDescriptor fd;
3511
  TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3512
  Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
D
duke 已提交
3513
  instanceKlassHandle h_klass (THREAD, k);
3514 3515 3516
  TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
  TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
  if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
D
duke 已提交
3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
    tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
    return false;
  }
  if (fd.is_static()) {
    tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
    return false;
  }
  if (fd.offset() == hardcoded_offset ) {
    return true;
  } else {
    tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.",
                  klass_name, field_name, hardcoded_offset, fd.offset());
    return false;
  }
}


bool JavaClasses::check_static_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
  EXCEPTION_MARK;
  fieldDescriptor fd;
3537
  TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3538
  Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
D
duke 已提交
3539
  instanceKlassHandle h_klass (THREAD, k);
3540 3541 3542
  TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
  TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
  if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
D
duke 已提交
3543 3544 3545 3546 3547 3548 3549
    tty->print_cr("Static field %s.%s not found", klass_name, field_name);
    return false;
  }
  if (!fd.is_static()) {
    tty->print_cr("Static field %s.%s appears to be nonstatic", klass_name, field_name);
    return false;
  }
3550
  if (fd.offset() == hardcoded_offset + InstanceMirrorKlass::offset_of_static_fields()) {
D
duke 已提交
3551 3552
    return true;
  } else {
3553
    tty->print_cr("Offset of static field %s.%s is hardcoded as %d but should really be %d.", klass_name, field_name, hardcoded_offset, fd.offset() - InstanceMirrorKlass::offset_of_static_fields());
D
duke 已提交
3554 3555 3556 3557 3558
    return false;
  }
}


3559 3560 3561
bool JavaClasses::check_constant(const char *klass_name, int hardcoded_constant, const char *field_name, const char* field_sig) {
  EXCEPTION_MARK;
  fieldDescriptor fd;
3562
  TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3563
  Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3564
  instanceKlassHandle h_klass (THREAD, k);
3565 3566 3567
  TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
  TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
  if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588
    tty->print_cr("Static field %s.%s not found", klass_name, field_name);
    return false;
  }
  if (!fd.is_static() || !fd.has_initial_value()) {
    tty->print_cr("Static field %s.%s appears to be non-constant", klass_name, field_name);
    return false;
  }
  if (!fd.initial_value_tag().is_int()) {
    tty->print_cr("Static field %s.%s is not an int", klass_name, field_name);
    return false;
  }
  jint field_value = fd.int_initial_value();
  if (field_value == hardcoded_constant) {
    return true;
  } else {
    tty->print_cr("Constant value of static field %s.%s is hardcoded as %d but should really be %d.", klass_name, field_name, hardcoded_constant, field_value);
    return false;
  }
}


D
duke 已提交
3589 3590 3591 3592
// Check the hard-coded field offsets of all the classes in this file

void JavaClasses::check_offsets() {
  bool valid = true;
3593
  HandleMark hm;
D
duke 已提交
3594 3595 3596 3597

#define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
  valid &= check_offset(klass_name, cpp_klass_name :: field_name ## _offset, #field_name, field_sig)

3598 3599 3600
#define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
  valid &= check_offset(klass_name, cpp_klass_name :: long_ ## field_name ## _offset, #field_name, field_sig)

D
duke 已提交
3601 3602 3603
#define CHECK_STATIC_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
  valid &= check_static_offset(klass_name, cpp_klass_name :: static_ ## field_name ## _offset, #field_name, field_sig)

3604 3605 3606
#define CHECK_CONSTANT(klass_name, cpp_klass_name, field_name, field_sig) \
  valid &= check_constant(klass_name, cpp_klass_name :: field_name, #field_name, field_sig)

D
duke 已提交
3607 3608 3609
  // java.lang.String

  CHECK_OFFSET("java/lang/String", java_lang_String, value, "[C");
K
kvn 已提交
3610 3611 3612 3613 3614 3615 3616
  if (java_lang_String::has_offset_field()) {
    CHECK_OFFSET("java/lang/String", java_lang_String, offset, "I");
    CHECK_OFFSET("java/lang/String", java_lang_String, count, "I");
  }
  if (java_lang_String::has_hash_field()) {
    CHECK_OFFSET("java/lang/String", java_lang_String, hash, "I");
  }
D
duke 已提交
3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635

  // java.lang.Class

  // Fake fields
  // CHECK_OFFSET("java/lang/Class", java_lang_Class, klass); // %%% this needs to be checked
  // CHECK_OFFSET("java/lang/Class", java_lang_Class, array_klass); // %%% this needs to be checked

  // java.lang.Throwable

  CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, backtrace, "Ljava/lang/Object;");
  CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, detailMessage, "Ljava/lang/String;");
  CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, cause, "Ljava/lang/Throwable;");
  CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, stackTrace, "[Ljava/lang/StackTraceElement;");

  // Boxed primitive objects (java_lang_boxing_object)

  CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
  CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
  CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
3636
  CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
D
duke 已提交
3637 3638 3639
  CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
  CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
  CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
3640
  CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
D
duke 已提交
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650

  // java.lang.ClassLoader

  CHECK_OFFSET("java/lang/ClassLoader", java_lang_ClassLoader, parent,      "Ljava/lang/ClassLoader;");

  // java.lang.System

  CHECK_STATIC_OFFSET("java/lang/System", java_lang_System,  in, "Ljava/io/InputStream;");
  CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, out, "Ljava/io/PrintStream;");
  CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, err, "Ljava/io/PrintStream;");
M
mullan 已提交
3651
  CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, security, "Ljava/lang/SecurityManager;");
D
duke 已提交
3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696

  // java.lang.StackTraceElement

  CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, declaringClass, "Ljava/lang/String;");
  CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, methodName, "Ljava/lang/String;");
  CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement,   fileName, "Ljava/lang/String;");
  CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, lineNumber, "I");

  // java.lang.ref.Reference

  CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;");
  CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;");
  CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;");
  // Fake field
  //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;");
  CHECK_STATIC_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, lock, "Ljava/lang/ref/Reference$Lock;");
  CHECK_STATIC_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, pending, "Ljava/lang/ref/Reference;");

  // java.lang.ref.SoftReference

  CHECK_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, timestamp, "J");
  CHECK_STATIC_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, clock, "J");

  // java.lang.AssertionStatusDirectives
  //
  // The CheckAssertionStatusDirectives boolean can be removed from here and
  // globals.hpp after the AssertionStatusDirectives class has been integrated
  // into merlin "for some time."  Without it, the vm will fail with early
  // merlin builds.

  if (CheckAssertionStatusDirectives && JDK_Version::is_gte_jdk14x_version()) {
    const char* nm = "java/lang/AssertionStatusDirectives";
    const char* sig = "[Ljava/lang/String;";
    CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classes, sig);
    CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classEnabled, "[Z");
    CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packages, sig);
    CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packageEnabled, "[Z");
    CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, deflt, "Z");
  }

  if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed");
}

#endif // PRODUCT

3697
int InjectedField::compute_offset() {
3698 3699
  Klass* klass_oop = klass();
  for (AllFieldStream fs(InstanceKlass::cast(klass_oop)); !fs.done(); fs.next()) {
3700 3701 3702 3703 3704 3705 3706 3707 3708
    if (!may_be_java && !fs.access_flags().is_internal()) {
      // Only look at injected fields
      continue;
    }
    if (fs.name() == name() && fs.signature() == signature()) {
      return fs.offset();
    }
  }
  ResourceMark rm;
3709
  tty->print_cr("Invalid layout of %s at %s/%s%s", InstanceKlass::cast(klass_oop)->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
3710 3711 3712
#ifndef PRODUCT
  klass_oop->print();
  tty->print_cr("all fields:");
3713
  for (AllFieldStream fs(InstanceKlass::cast(klass_oop)); !fs.done(); fs.next()) {
3714 3715 3716
    tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
  }
#endif //PRODUCT
3717
  vm_exit_during_initialization("Invalid layout of preloaded class: use -XX:+TraceClassLoading to see the origin of the problem class");
3718 3719 3720
  return -1;
}

D
duke 已提交
3721 3722 3723 3724 3725
void javaClasses_init() {
  JavaClasses::compute_offsets();
  JavaClasses::check_offsets();
  FilteredFieldsMap::initialize();  // must be done after computing offsets.
}