src/share/vm/ci/ciField.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 8856
ac27a9c85bea
permissions
-rw-r--r--

Merge

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

mercurial