src/share/vm/ci/ciField.cpp

Wed, 31 Jan 2018 19:24:57 -0500

author
dbuck
date
Wed, 31 Jan 2018 19:24:57 -0500
changeset 9289
427b2fb1944f
parent 8664
00cbb581da94
child 8856
ac27a9c85bea
permissions
-rw-r--r--

8189170: Add option to disable stack overflow checking in primordial thread for use with JNI_CreateJavaJVM
Reviewed-by: dcubed

duke@435 1 /*
drchase@5732 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "ci/ciField.hpp"
stefank@2314 27 #include "ci/ciInstanceKlass.hpp"
stefank@2314 28 #include "ci/ciUtilities.hpp"
stefank@2314 29 #include "classfile/systemDictionary.hpp"
stefank@2314 30 #include "gc_interface/collectedHeap.inline.hpp"
stefank@2314 31 #include "interpreter/linkResolver.hpp"
stefank@2314 32 #include "memory/universe.inline.hpp"
stefank@2314 33 #include "oops/oop.inline.hpp"
stefank@2314 34 #include "oops/oop.inline2.hpp"
stefank@2314 35 #include "runtime/fieldDescriptor.hpp"
duke@435 36
duke@435 37 // ciField
duke@435 38 //
duke@435 39 // This class represents the result of a field lookup in the VM.
duke@435 40 // The lookup may not succeed, in which case the information in
duke@435 41 // the ciField will be incomplete.
duke@435 42
duke@435 43 // The ciObjectFactory cannot create circular data structures in one query.
duke@435 44 // To avoid vicious circularities, we initialize ciField::_type to NULL
duke@435 45 // for reference types and derive it lazily from the ciField::_signature.
duke@435 46 // Primitive types are eagerly initialized, and basic layout queries
duke@435 47 // can succeed without initialization, using only the BasicType of the field.
duke@435 48
duke@435 49 // Notes on bootstrapping and shared CI objects: A field is shared if and
duke@435 50 // only if it is (a) non-static and (b) declared by a shared instance klass.
duke@435 51 // This allows non-static field lists to be cached on shared types.
duke@435 52 // Because the _type field is lazily initialized, however, there is a
duke@435 53 // special restriction that a shared field cannot cache an unshared type.
duke@435 54 // This puts a small performance penalty on shared fields with unshared
duke@435 55 // types, such as StackTraceElement[] Throwable.stackTrace.
duke@435 56 // (Throwable is shared because ClassCastException is shared, but
duke@435 57 // StackTraceElement is not presently shared.)
duke@435 58
duke@435 59 // It is not a vicious circularity for a ciField to recursively create
duke@435 60 // the ciSymbols necessary to represent its name and signature.
duke@435 61 // Therefore, these items are created eagerly, and the name and signature
duke@435 62 // of a shared field are themselves shared symbols. This somewhat
duke@435 63 // pollutes the set of shared CI objects: It grows from 50 to 93 items,
duke@435 64 // with all of the additional 43 being uninteresting shared ciSymbols.
duke@435 65 // This adds at most one step to the binary search, an amount which
duke@435 66 // decreases for complex compilation tasks.
duke@435 67
duke@435 68 // ------------------------------------------------------------------
duke@435 69 // ciField::ciField
never@3854 70 ciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
duke@435 71 ASSERT_IN_VM;
duke@435 72 CompilerThread *thread = CompilerThread::current();
duke@435 73
duke@435 74 assert(ciObjectFactory::is_initialized(), "not a shared field");
duke@435 75
duke@435 76 assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool");
duke@435 77
duke@435 78 constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());
duke@435 79
duke@435 80 // Get the field's name, signature, and type.
coleenp@2497 81 Symbol* name = cpool->name_ref_at(index);
coleenp@2497 82 _name = ciEnv::current(thread)->get_symbol(name);
duke@435 83
duke@435 84 int nt_index = cpool->name_and_type_ref_index_at(index);
duke@435 85 int sig_index = cpool->signature_ref_index_at(nt_index);
coleenp@2497 86 Symbol* signature = cpool->symbol_at(sig_index);
coleenp@2497 87 _signature = ciEnv::current(thread)->get_symbol(signature);
duke@435 88
coleenp@2497 89 BasicType field_type = FieldType::basic_type(signature);
duke@435 90
duke@435 91 // If the field is a pointer type, get the klass of the
duke@435 92 // field.
duke@435 93 if (field_type == T_OBJECT || field_type == T_ARRAY) {
duke@435 94 bool ignore;
duke@435 95 // This is not really a class reference; the index always refers to the
duke@435 96 // field's type signature, as a symbol. Linkage checks do not apply.
twisti@1573 97 _type = ciEnv::current(thread)->get_klass_by_index(cpool, sig_index, ignore, klass);
duke@435 98 } else {
duke@435 99 _type = ciType::make(field_type);
duke@435 100 }
duke@435 101
coleenp@2497 102 _name = (ciSymbol*)ciEnv::current(thread)->get_symbol(name);
duke@435 103
duke@435 104 // Get the field's declared holder.
duke@435 105 //
duke@435 106 // Note: we actually create a ciInstanceKlass for this klass,
duke@435 107 // even though we may not need to.
duke@435 108 int holder_index = cpool->klass_ref_index_at(index);
duke@435 109 bool holder_is_accessible;
duke@435 110 ciInstanceKlass* declared_holder =
twisti@1573 111 ciEnv::current(thread)->get_klass_by_index(cpool, holder_index,
twisti@1573 112 holder_is_accessible,
twisti@1573 113 klass)->as_instance_klass();
duke@435 114
duke@435 115 // The declared holder of this field may not have been loaded.
duke@435 116 // Bail out with partial field information.
duke@435 117 if (!holder_is_accessible) {
drchase@5732 118 // _type has already been set.
duke@435 119 // The default values for _flags and _constant_value will suffice.
duke@435 120 // We need values for _holder, _offset, and _is_constant,
duke@435 121 _holder = declared_holder;
duke@435 122 _offset = -1;
duke@435 123 _is_constant = false;
duke@435 124 return;
duke@435 125 }
duke@435 126
coleenp@4037 127 InstanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass();
duke@435 128
duke@435 129 // Perform the field lookup.
duke@435 130 fieldDescriptor field_desc;
coleenp@4037 131 Klass* canonical_holder =
coleenp@2497 132 loaded_decl_holder->find_field(name, signature, &field_desc);
duke@435 133 if (canonical_holder == NULL) {
duke@435 134 // Field lookup failed. Will be detected by will_link.
duke@435 135 _holder = declared_holder;
duke@435 136 _offset = -1;
duke@435 137 _is_constant = false;
duke@435 138 return;
duke@435 139 }
duke@435 140
goetz@6812 141 // Access check based on declared_holder. canonical_holder should not be used
goetz@6812 142 // to check access because it can erroneously succeed. If this check fails,
goetz@6812 143 // propagate the declared holder to will_link() which in turn will bail out
goetz@6812 144 // compilation for this field access.
goetz@6812 145 if (!Reflection::verify_field_access(klass->get_Klass(), declared_holder->get_Klass(), canonical_holder, field_desc.access_flags(), true)) {
goetz@6812 146 _holder = declared_holder;
goetz@6812 147 _offset = -1;
goetz@6812 148 _is_constant = false;
goetz@6812 149 return;
goetz@6812 150 }
goetz@6812 151
duke@435 152 assert(canonical_holder == field_desc.field_holder(), "just checking");
duke@435 153 initialize_from(&field_desc);
duke@435 154 }
duke@435 155
never@3854 156 ciField::ciField(fieldDescriptor *fd): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
duke@435 157 ASSERT_IN_VM;
duke@435 158
duke@435 159 // Get the field's name, signature, and type.
duke@435 160 ciEnv* env = CURRENT_ENV;
coleenp@2497 161 _name = env->get_symbol(fd->name());
coleenp@2497 162 _signature = env->get_symbol(fd->signature());
duke@435 163
duke@435 164 BasicType field_type = fd->field_type();
duke@435 165
duke@435 166 // If the field is a pointer type, get the klass of the
duke@435 167 // field.
duke@435 168 if (field_type == T_OBJECT || field_type == T_ARRAY) {
duke@435 169 _type = NULL; // must call compute_type on first access
duke@435 170 } else {
duke@435 171 _type = ciType::make(field_type);
duke@435 172 }
duke@435 173
duke@435 174 initialize_from(fd);
duke@435 175
duke@435 176 // Either (a) it is marked shared, or else (b) we are done bootstrapping.
duke@435 177 assert(is_shared() || ciObjectFactory::is_initialized(),
duke@435 178 "bootstrap classes must not create & cache unshared fields");
duke@435 179 }
duke@435 180
jrose@1608 181 static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
jrose@1608 182 if (holder == NULL)
jrose@1608 183 return false;
jrose@1608 184 if (holder->name() == ciSymbol::java_lang_System())
jrose@1608 185 // Never trust strangely unstable finals: System.out, etc.
jrose@1608 186 return false;
jrose@1608 187 // Even if general trusting is disabled, trust system-built closures in these packages.
jrose@2639 188 if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
jrose@1608 189 return true;
shade@8176 190 // Trust Atomic*FieldUpdaters: they are very important for performance, and make up one
shade@8176 191 // more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.
shade@8176 192 if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
shade@8176 193 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||
shade@8176 194 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||
shade@8176 195 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {
shade@8176 196 return true;
shade@8176 197 }
jrose@1608 198 return TrustFinalNonStaticFields;
jrose@1608 199 }
jrose@1608 200
duke@435 201 void ciField::initialize_from(fieldDescriptor* fd) {
duke@435 202 // Get the flags, offset, and canonical holder of the field.
duke@435 203 _flags = ciFlags(fd->access_flags());
duke@435 204 _offset = fd->offset();
coleenp@4037 205 _holder = CURRENT_ENV->get_instance_klass(fd->field_holder());
duke@435 206
duke@435 207 // Check to see if the field is constant.
vlivanov@5658 208 bool is_final = this->is_final();
vlivanov@5658 209 bool is_stable = FoldStableValues && this->is_stable();
zmajo@8664 210 if (_holder->is_initialized() && ((is_final && !has_initialized_final_update()) || is_stable)) {
twisti@1573 211 if (!this->is_static()) {
jrose@2639 212 // A field can be constant if it's a final static field or if
jrose@2639 213 // it's a final non-static field of a trusted class (classes in
jrose@2639 214 // java.lang.invoke and sun.invoke packages and subpackages).
vlivanov@5658 215 if (is_stable || trust_final_non_static_fields(_holder)) {
twisti@1573 216 _is_constant = true;
twisti@1573 217 return;
twisti@1573 218 }
twisti@1573 219 _is_constant = false;
twisti@1573 220 return;
twisti@1573 221 }
twisti@1573 222
duke@435 223 // This field just may be constant. The only cases where it will
duke@435 224 // not be constant are:
duke@435 225 //
duke@435 226 // 1. The field holds a non-perm-space oop. The field is, strictly
duke@435 227 // speaking, constant but we cannot embed non-perm-space oops into
duke@435 228 // generated code. For the time being we need to consider the
duke@435 229 // field to be not constant.
duke@435 230 // 2. The field is a *special* static&final field whose value
duke@435 231 // may change. The three examples are java.lang.System.in,
duke@435 232 // java.lang.System.out, and java.lang.System.err.
duke@435 233
coleenp@4037 234 KlassHandle k = _holder->get_Klass();
never@1577 235 assert( SystemDictionary::System_klass() != NULL, "Check once per vm");
never@2568 236 if( k() == SystemDictionary::System_klass() ) {
duke@435 237 // Check offsets for case 2: System.in, System.out, or System.err
duke@435 238 if( _offset == java_lang_System::in_offset_in_bytes() ||
duke@435 239 _offset == java_lang_System::out_offset_in_bytes() ||
duke@435 240 _offset == java_lang_System::err_offset_in_bytes() ) {
duke@435 241 _is_constant = false;
duke@435 242 return;
duke@435 243 }
duke@435 244 }
duke@435 245
never@2658 246 Handle mirror = k->java_mirror();
never@2658 247
duke@435 248 switch(type()->basic_type()) {
duke@435 249 case T_BYTE:
never@2658 250 _constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset));
duke@435 251 break;
duke@435 252 case T_CHAR:
never@2658 253 _constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset));
duke@435 254 break;
duke@435 255 case T_SHORT:
never@2658 256 _constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset));
duke@435 257 break;
duke@435 258 case T_BOOLEAN:
never@2658 259 _constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset));
duke@435 260 break;
duke@435 261 case T_INT:
never@2658 262 _constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset));
duke@435 263 break;
duke@435 264 case T_FLOAT:
never@2658 265 _constant_value = ciConstant(mirror->float_field(_offset));
duke@435 266 break;
duke@435 267 case T_DOUBLE:
never@2658 268 _constant_value = ciConstant(mirror->double_field(_offset));
duke@435 269 break;
duke@435 270 case T_LONG:
never@2658 271 _constant_value = ciConstant(mirror->long_field(_offset));
duke@435 272 break;
duke@435 273 case T_OBJECT:
duke@435 274 case T_ARRAY:
duke@435 275 {
never@2658 276 oop o = mirror->obj_field(_offset);
duke@435 277
duke@435 278 // A field will be "constant" if it is known always to be
duke@435 279 // a non-null reference to an instance of a particular class,
duke@435 280 // or to a particular array. This can happen even if the instance
duke@435 281 // or array is not perm. In such a case, an "unloaded" ciArray
duke@435 282 // or ciInstance is created. The compiler may be able to use
duke@435 283 // information about the object's class (which is exact) or length.
duke@435 284
duke@435 285 if (o == NULL) {
duke@435 286 _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
duke@435 287 } else {
duke@435 288 _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
duke@435 289 assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
duke@435 290 }
duke@435 291 }
duke@435 292 }
vlivanov@5658 293 if (is_stable && _constant_value.is_null_or_zero()) {
vlivanov@5658 294 // It is not a constant after all; treat it as uninitialized.
vlivanov@5658 295 _is_constant = false;
vlivanov@5658 296 } else {
vlivanov@5658 297 _is_constant = true;
vlivanov@5658 298 }
duke@435 299 } else {
duke@435 300 _is_constant = false;
duke@435 301 }
duke@435 302 }
duke@435 303
duke@435 304 // ------------------------------------------------------------------
duke@435 305 // ciField::compute_type
duke@435 306 //
duke@435 307 // Lazily compute the type, if it is an instance klass.
duke@435 308 ciType* ciField::compute_type() {
duke@435 309 GUARDED_VM_ENTRY(return compute_type_impl();)
duke@435 310 }
duke@435 311
duke@435 312 ciType* ciField::compute_type_impl() {
jrose@2982 313 ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);
duke@435 314 if (!type->is_primitive_type() && is_shared()) {
duke@435 315 // We must not cache a pointer to an unshared type, in a shared field.
duke@435 316 bool type_is_also_shared = false;
duke@435 317 if (type->is_type_array_klass()) {
duke@435 318 type_is_also_shared = true; // int[] etc. are explicitly bootstrapped
duke@435 319 } else if (type->is_instance_klass()) {
duke@435 320 type_is_also_shared = type->as_instance_klass()->is_shared();
duke@435 321 } else {
duke@435 322 // Currently there is no 'shared' query for array types.
duke@435 323 type_is_also_shared = !ciObjectFactory::is_initialized();
duke@435 324 }
duke@435 325 if (!type_is_also_shared)
duke@435 326 return type; // Bummer.
duke@435 327 }
duke@435 328 _type = type;
duke@435 329 return type;
duke@435 330 }
duke@435 331
duke@435 332
duke@435 333 // ------------------------------------------------------------------
duke@435 334 // ciField::will_link
duke@435 335 //
duke@435 336 // Can a specific access to this field be made without causing
duke@435 337 // link errors?
duke@435 338 bool ciField::will_link(ciInstanceKlass* accessing_klass,
duke@435 339 Bytecodes::Code bc) {
duke@435 340 VM_ENTRY_MARK;
never@3854 341 assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
never@3854 342 bc == Bytecodes::_getfield || bc == Bytecodes::_putfield,
never@3854 343 "unexpected bytecode");
never@3854 344
duke@435 345 if (_offset == -1) {
duke@435 346 // at creation we couldn't link to our holder so we need to
duke@435 347 // maintain that stance, otherwise there's no safe way to use this
duke@435 348 // ciField.
duke@435 349 return false;
duke@435 350 }
duke@435 351
never@3854 352 // Check for static/nonstatic mismatch
never@3854 353 bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
never@3854 354 if (is_static != this->is_static()) {
never@3854 355 return false;
never@3854 356 }
never@3854 357
never@3854 358 // Get and put can have different accessibility rules
never@3854 359 bool is_put = (bc == Bytecodes::_putfield || bc == Bytecodes::_putstatic);
never@3854 360 if (is_put) {
never@3854 361 if (_known_to_link_with_put == accessing_klass) {
never@3854 362 return true;
never@3854 363 }
never@3856 364 } else {
never@3854 365 if (_known_to_link_with_get == accessing_klass) {
never@3854 366 return true;
never@3854 367 }
duke@435 368 }
duke@435 369
drchase@5732 370 fieldDescriptor result;
drchase@5732 371 LinkResolver::resolve_field(result, _holder->get_instanceKlass(),
drchase@5732 372 _name->get_symbol(), _signature->get_symbol(),
drchase@5732 373 accessing_klass->get_Klass(), bc, true, false,
drchase@5732 374 KILL_COMPILE_ON_FATAL_(false));
duke@435 375
duke@435 376 // update the hit-cache, unless there is a problem with memory scoping:
never@3854 377 if (accessing_klass->is_shared() || !is_shared()) {
never@3854 378 if (is_put) {
never@3854 379 _known_to_link_with_put = accessing_klass;
never@3854 380 } else {
never@3854 381 _known_to_link_with_get = accessing_klass;
never@3854 382 }
never@3854 383 }
duke@435 384
duke@435 385 return true;
duke@435 386 }
duke@435 387
duke@435 388 // ------------------------------------------------------------------
duke@435 389 // ciField::print
duke@435 390 void ciField::print() {
roland@4357 391 tty->print("<ciField name=");
duke@435 392 _holder->print_name();
duke@435 393 tty->print(".");
duke@435 394 _name->print_symbol();
roland@4357 395 tty->print(" signature=");
roland@4357 396 _signature->print_symbol();
duke@435 397 tty->print(" offset=%d type=", _offset);
vlivanov@5658 398 if (_type != NULL)
vlivanov@5658 399 _type->print_name();
vlivanov@5658 400 else
vlivanov@5658 401 tty->print("(reference)");
vlivanov@5658 402 tty->print(" flags=%04x", flags().as_int());
duke@435 403 tty->print(" is_constant=%s", bool_to_str(_is_constant));
kvn@2037 404 if (_is_constant && is_static()) {
duke@435 405 tty->print(" constant_value=");
duke@435 406 _constant_value.print();
duke@435 407 }
duke@435 408 tty->print(">");
duke@435 409 }
duke@435 410
duke@435 411 // ------------------------------------------------------------------
duke@435 412 // ciField::print_name_on
duke@435 413 //
duke@435 414 // Print the name of this field
duke@435 415 void ciField::print_name_on(outputStream* st) {
duke@435 416 name()->print_symbol_on(st);
duke@435 417 }

mercurial