< prev index next >

src/hotspot/share/ci/ciField.cpp

Print this page


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


  50 // This allows non-static field lists to be cached on shared types.
  51 // Because the _type field is lazily initialized, however, there is a
  52 // special restriction that a shared field cannot cache an unshared type.
  53 // This puts a small performance penalty on shared fields with unshared
  54 // types, such as StackTraceElement[] Throwable.stackTrace.
  55 // (Throwable is shared because ClassCastException is shared, but
  56 // StackTraceElement is not presently shared.)
  57 
  58 // It is not a vicious circularity for a ciField to recursively create
  59 // the ciSymbols necessary to represent its name and signature.
  60 // Therefore, these items are created eagerly, and the name and signature
  61 // of a shared field are themselves shared symbols.  This somewhat
  62 // pollutes the set of shared CI objects:  It grows from 50 to 93 items,
  63 // with all of the additional 43 being uninteresting shared ciSymbols.
  64 // This adds at most one step to the binary search, an amount which
  65 // decreases for complex compilation tasks.
  66 
  67 // ------------------------------------------------------------------
  68 // ciField::ciField
  69 ciField::ciField(ciInstanceKlass* klass, int index) :
  70     _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
  71   ASSERT_IN_VM;
  72   CompilerThread *THREAD = CompilerThread::current();
  73 
  74   assert(ciObjectFactory::is_initialized(), "not a shared field");
  75 
  76   assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constant-pool");
  77 
  78   constantPoolHandle cpool(THREAD, klass->get_instanceKlass()->constants());
  79 
  80   // Get the field's name, signature, and type.
  81   Symbol* name  = cpool->name_ref_at(index);
  82   _name = ciEnv::current(THREAD)->get_symbol(name);
  83 
  84   int nt_index = cpool->name_and_type_ref_index_at(index);
  85   int sig_index = cpool->signature_ref_index_at(nt_index);
  86   Symbol* signature = cpool->symbol_at(sig_index);
  87   _signature = ciEnv::current(THREAD)->get_symbol(signature);
  88 
  89   BasicType field_type = FieldType::basic_type(signature);
  90 
  91   // If the field is a pointer type, get the klass of the
  92   // field.
  93   if (field_type == T_OBJECT || field_type == T_ARRAY) {
  94     bool ignore;
  95     // This is not really a class reference; the index always refers to the
  96     // field's type signature, as a symbol.  Linkage checks do not apply.
  97     _type = ciEnv::current(THREAD)->get_klass_by_index(cpool, sig_index, ignore, klass);
  98   } else {
  99     _type = ciType::make(field_type);
 100   }
 101 
 102   _name = (ciSymbol*)ciEnv::current(THREAD)->get_symbol(name);
 103 



 104   // Get the field's declared holder.
 105   //
 106   // Note: we actually create a ciInstanceKlass for this klass,
 107   // even though we may not need to.
 108   int holder_index = cpool->klass_ref_index_at(index);
 109   bool holder_is_accessible;
 110 
 111   ciKlass* generic_declared_holder = ciEnv::current(THREAD)->get_klass_by_index(cpool, holder_index,
 112                                                                                 holder_is_accessible,
 113                                                                                 klass);
 114 
 115   if (generic_declared_holder->is_array_klass()) {
 116     // If the declared holder of the field is an array class, assume that
 117     // the canonical holder of that field is java.lang.Object. Arrays
 118     // do not have fields; java.lang.Object is the only supertype of an
 119     // array type that can declare fields and is therefore the canonical
 120     // holder of the array type.
 121     //
 122     // Furthermore, the compilers assume that java.lang.Object does not
 123     // have any fields. Therefore, the field is not looked up. Instead,


 182     return;
 183   }
 184 
 185   assert(canonical_holder == field_desc.field_holder(), "just checking");
 186   initialize_from(&field_desc);
 187 }
 188 
 189 ciField::ciField(fieldDescriptor *fd) :
 190     _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
 191   ASSERT_IN_VM;
 192 
 193   // Get the field's name, signature, and type.
 194   ciEnv* env = CURRENT_ENV;
 195   _name = env->get_symbol(fd->name());
 196   _signature = env->get_symbol(fd->signature());
 197 
 198   BasicType field_type = fd->field_type();
 199 
 200   // If the field is a pointer type, get the klass of the
 201   // field.
 202   if (field_type == T_OBJECT || field_type == T_ARRAY) {
 203     _type = NULL;  // must call compute_type on first access
 204   } else {
 205     _type = ciType::make(field_type);
 206   }
 207 
 208   initialize_from(fd);
 209 
 210   // Either (a) it is marked shared, or else (b) we are done bootstrapping.
 211   assert(is_shared() || ciObjectFactory::is_initialized(),
 212          "bootstrap classes must not create & cache unshared fields");
 213 }
 214 























 215 static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
 216   if (holder == NULL)
 217     return false;
 218   if (holder->name() == ciSymbol::java_lang_System())
 219     // Never trust strangely unstable finals:  System.out, etc.
 220     return false;
 221   // Even if general trusting is disabled, trust system-built closures in these packages.
 222   if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
 223     return true;
 224   // Trust VM unsafe anonymous classes. They are private API (jdk.internal.misc.Unsafe)
 225   // and can't be serialized, so there is no hacking of finals going on with them.
 226   if (holder->is_unsafe_anonymous())
 227     return true;
 228   // Trust final fields in all boxed classes
 229   if (holder->is_box_klass())
 230     return true;
 231   // Trust final fields in String
 232   if (holder->name() == ciSymbol::java_lang_String())
 233     return true;
 234   // Trust Atomic*FieldUpdaters: they are very important for performance, and make up one
 235   // more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.
 236   if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
 237       holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||
 238       holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||
 239       holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {
 240     return true;
 241   }
 242   return TrustFinalNonStaticFields;
 243 }
 244 
 245 void ciField::initialize_from(fieldDescriptor* fd) {
 246   // Get the flags, offset, and canonical holder of the field.
 247   _flags = ciFlags(fd->access_flags());
 248   _offset = fd->offset();
 249   Klass* field_holder = fd->field_holder();
 250   assert(field_holder != NULL, "null field_holder");
 251   _holder = CURRENT_ENV->get_instance_klass(field_holder);


 252 
 253   // Check to see if the field is constant.
 254   Klass* k = _holder->get_Klass();
 255   bool is_stable_field = FoldStableValues && is_stable();
 256   if ((is_final() && !has_initialized_final_update()) || is_stable_field) {
 257     if (is_static()) {
 258       // This field just may be constant.  The only case where it will
 259       // not be constant is when the field is a *special* static & final field
 260       // whose value may change.  The three examples are java.lang.System.in,
 261       // java.lang.System.out, and java.lang.System.err.
 262       assert(SystemDictionary::System_klass() != NULL, "Check once per vm");
 263       if (k == SystemDictionary::System_klass()) {
 264         // Check offsets for case 2: System.in, System.out, or System.err
 265         if( _offset == java_lang_System::in_offset_in_bytes()  ||
 266             _offset == java_lang_System::out_offset_in_bytes() ||
 267             _offset == java_lang_System::err_offset_in_bytes() ) {
 268           _is_constant = false;
 269           return;
 270         }
 271       }


 344       // Currently there is no 'shared' query for array types.
 345       type_is_also_shared = !ciObjectFactory::is_initialized();
 346     }
 347     if (!type_is_also_shared)
 348       return type;              // Bummer.
 349   }
 350   _type = type;
 351   return type;
 352 }
 353 
 354 
 355 // ------------------------------------------------------------------
 356 // ciField::will_link
 357 //
 358 // Can a specific access to this field be made without causing
 359 // link errors?
 360 bool ciField::will_link(ciMethod* accessing_method,
 361                         Bytecodes::Code bc) {
 362   VM_ENTRY_MARK;
 363   assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
 364          bc == Bytecodes::_getfield  || bc == Bytecodes::_putfield,
 365          "unexpected bytecode");
 366 
 367   if (_offset == -1) {
 368     // at creation we couldn't link to our holder so we need to
 369     // maintain that stance, otherwise there's no safe way to use this
 370     // ciField.
 371     return false;
 372   }
 373 
 374   // Check for static/nonstatic mismatch
 375   bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
 376   if (is_static != this->is_static()) {
 377     return false;
 378   }
 379 
 380   // Get and put can have different accessibility rules
 381   bool is_put    = (bc == Bytecodes::_putfield  || bc == Bytecodes::_putstatic);
 382   if (is_put) {
 383     if (_known_to_link_with_put == accessing_method) {
 384       return true;
 385     }


 410 // ------------------------------------------------------------------
 411 // ciField::print
 412 void ciField::print() {
 413   tty->print("<ciField name=");
 414   _holder->print_name();
 415   tty->print(".");
 416   _name->print_symbol();
 417   tty->print(" signature=");
 418   _signature->print_symbol();
 419   tty->print(" offset=%d type=", _offset);
 420   if (_type != NULL)
 421     _type->print_name();
 422   else
 423     tty->print("(reference)");
 424   tty->print(" flags=%04x", flags().as_int());
 425   tty->print(" is_constant=%s", bool_to_str(_is_constant));
 426   if (_is_constant && is_static()) {
 427     tty->print(" constant_value=");
 428     _constant_value.print();
 429   }


 430   tty->print(">");
 431 }
 432 
 433 // ------------------------------------------------------------------
 434 // ciField::print_name_on
 435 //
 436 // Print the name of this field
 437 void ciField::print_name_on(outputStream* st) {
 438   name()->print_symbol_on(st);
 439 }
   1 /*
   2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


  50 // This allows non-static field lists to be cached on shared types.
  51 // Because the _type field is lazily initialized, however, there is a
  52 // special restriction that a shared field cannot cache an unshared type.
  53 // This puts a small performance penalty on shared fields with unshared
  54 // types, such as StackTraceElement[] Throwable.stackTrace.
  55 // (Throwable is shared because ClassCastException is shared, but
  56 // StackTraceElement is not presently shared.)
  57 
  58 // It is not a vicious circularity for a ciField to recursively create
  59 // the ciSymbols necessary to represent its name and signature.
  60 // Therefore, these items are created eagerly, and the name and signature
  61 // of a shared field are themselves shared symbols.  This somewhat
  62 // pollutes the set of shared CI objects:  It grows from 50 to 93 items,
  63 // with all of the additional 43 being uninteresting shared ciSymbols.
  64 // This adds at most one step to the binary search, an amount which
  65 // decreases for complex compilation tasks.
  66 
  67 // ------------------------------------------------------------------
  68 // ciField::ciField
  69 ciField::ciField(ciInstanceKlass* klass, int index) :
  70   _is_flattened(false), _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
  71   ASSERT_IN_VM;
  72   CompilerThread *THREAD = CompilerThread::current();
  73 
  74   assert(ciObjectFactory::is_initialized(), "not a shared field");
  75 
  76   assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constant-pool");
  77 
  78   constantPoolHandle cpool(THREAD, klass->get_instanceKlass()->constants());
  79 
  80   // Get the field's name, signature, and type.
  81   Symbol* name  = cpool->name_ref_at(index);
  82   _name = ciEnv::current(THREAD)->get_symbol(name);
  83 
  84   int nt_index = cpool->name_and_type_ref_index_at(index);
  85   int sig_index = cpool->signature_ref_index_at(nt_index);
  86   Symbol* signature = cpool->symbol_at(sig_index);
  87   _signature = ciEnv::current(THREAD)->get_symbol(signature);
  88 
  89   BasicType field_type = FieldType::basic_type(signature);
  90 
  91   // If the field is a pointer type, get the klass of the
  92   // field.
  93   if (field_type == T_OBJECT || field_type == T_ARRAY || field_type == T_VALUETYPE) {
  94     bool ignore;
  95     // This is not really a class reference; the index always refers to the
  96     // field's type signature, as a symbol.  Linkage checks do not apply.
  97     _type = ciEnv::current(THREAD)->get_klass_by_index(cpool, sig_index, ignore, klass);
  98   } else {
  99     _type = ciType::make(field_type);
 100   }
 101 
 102   _name = (ciSymbol*)ciEnv::current(THREAD)->get_symbol(name);
 103 
 104   // this is needed if the field class is not yet loaded.
 105   _is_flattenable = _signature->is_Q_signature();
 106 
 107   // Get the field's declared holder.
 108   //
 109   // Note: we actually create a ciInstanceKlass for this klass,
 110   // even though we may not need to.
 111   int holder_index = cpool->klass_ref_index_at(index);
 112   bool holder_is_accessible;
 113 
 114   ciKlass* generic_declared_holder = ciEnv::current(THREAD)->get_klass_by_index(cpool, holder_index,
 115                                                                                 holder_is_accessible,
 116                                                                                 klass);
 117 
 118   if (generic_declared_holder->is_array_klass()) {
 119     // If the declared holder of the field is an array class, assume that
 120     // the canonical holder of that field is java.lang.Object. Arrays
 121     // do not have fields; java.lang.Object is the only supertype of an
 122     // array type that can declare fields and is therefore the canonical
 123     // holder of the array type.
 124     //
 125     // Furthermore, the compilers assume that java.lang.Object does not
 126     // have any fields. Therefore, the field is not looked up. Instead,


 185     return;
 186   }
 187 
 188   assert(canonical_holder == field_desc.field_holder(), "just checking");
 189   initialize_from(&field_desc);
 190 }
 191 
 192 ciField::ciField(fieldDescriptor *fd) :
 193     _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
 194   ASSERT_IN_VM;
 195 
 196   // Get the field's name, signature, and type.
 197   ciEnv* env = CURRENT_ENV;
 198   _name = env->get_symbol(fd->name());
 199   _signature = env->get_symbol(fd->signature());
 200 
 201   BasicType field_type = fd->field_type();
 202 
 203   // If the field is a pointer type, get the klass of the
 204   // field.
 205   if (field_type == T_OBJECT || field_type == T_ARRAY || field_type == T_VALUETYPE) {
 206     _type = NULL;  // must call compute_type on first access
 207   } else {
 208     _type = ciType::make(field_type);
 209   }
 210 
 211   initialize_from(fd);
 212 
 213   // Either (a) it is marked shared, or else (b) we are done bootstrapping.
 214   assert(is_shared() || ciObjectFactory::is_initialized(),
 215          "bootstrap classes must not create & cache unshared fields");
 216 }
 217 
 218 // Special copy constructor used to flatten value type fields by
 219 // copying the fields of the value type to a new holder klass.
 220 ciField::ciField(ciField* field, ciInstanceKlass* holder, int offset, bool is_final) {
 221   assert(field->holder()->is_valuetype(), "should only be used for value type field flattening");
 222   // Set the is_final flag
 223   jint final = is_final ? JVM_ACC_FINAL : ~JVM_ACC_FINAL;
 224   AccessFlags flags(field->flags().as_int() & final);
 225   _flags = ciFlags(flags);
 226   _holder = holder;
 227   _offset = offset;
 228   // Copy remaining fields
 229   _name = field->_name;
 230   _signature = field->_signature;
 231   _type = field->_type;
 232   _is_constant = field->_is_constant;
 233   _known_to_link_with_put = field->_known_to_link_with_put;
 234   _known_to_link_with_get = field->_known_to_link_with_get;
 235   _constant_value = field->_constant_value;
 236   assert(!field->is_flattened(), "field must not be flattened");
 237   _is_flattened = false;
 238   _is_flattenable = field->is_flattenable();
 239 }
 240 
 241 static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
 242   if (holder == NULL)
 243     return false;
 244   if (holder->name() == ciSymbol::java_lang_System())
 245     // Never trust strangely unstable finals:  System.out, etc.
 246     return false;
 247   // Even if general trusting is disabled, trust system-built closures in these packages.
 248   if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
 249     return true;
 250   // Trust VM unsafe anonymous classes. They are private API (jdk.internal.misc.Unsafe)
 251   // and can't be serialized, so there is no hacking of finals going on with them.
 252   if (holder->is_unsafe_anonymous())
 253     return true;
 254   // Trust final fields in all boxed classes
 255   if (holder->is_box_klass())
 256     return true;
 257   // Trust final fields in String
 258   if (holder->name() == ciSymbol::java_lang_String())
 259     return true;
 260   // Trust Atomic*FieldUpdaters: they are very important for performance, and make up one
 261   // more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.
 262   if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
 263       holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||
 264       holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||
 265       holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {
 266     return true;
 267   }
 268   return TrustFinalNonStaticFields;
 269 }
 270 
 271 void ciField::initialize_from(fieldDescriptor* fd) {
 272   // Get the flags, offset, and canonical holder of the field.
 273   _flags = ciFlags(fd->access_flags());
 274   _offset = fd->offset();
 275   Klass* field_holder = fd->field_holder();
 276   assert(field_holder != NULL, "null field_holder");
 277   _holder = CURRENT_ENV->get_instance_klass(field_holder);
 278   _is_flattened = fd->is_flattened();
 279   _is_flattenable = fd->is_flattenable();
 280 
 281   // Check to see if the field is constant.
 282   Klass* k = _holder->get_Klass();
 283   bool is_stable_field = FoldStableValues && is_stable();
 284   if ((is_final() && !has_initialized_final_update()) || is_stable_field) {
 285     if (is_static()) {
 286       // This field just may be constant.  The only case where it will
 287       // not be constant is when the field is a *special* static & final field
 288       // whose value may change.  The three examples are java.lang.System.in,
 289       // java.lang.System.out, and java.lang.System.err.
 290       assert(SystemDictionary::System_klass() != NULL, "Check once per vm");
 291       if (k == SystemDictionary::System_klass()) {
 292         // Check offsets for case 2: System.in, System.out, or System.err
 293         if( _offset == java_lang_System::in_offset_in_bytes()  ||
 294             _offset == java_lang_System::out_offset_in_bytes() ||
 295             _offset == java_lang_System::err_offset_in_bytes() ) {
 296           _is_constant = false;
 297           return;
 298         }
 299       }


 372       // Currently there is no 'shared' query for array types.
 373       type_is_also_shared = !ciObjectFactory::is_initialized();
 374     }
 375     if (!type_is_also_shared)
 376       return type;              // Bummer.
 377   }
 378   _type = type;
 379   return type;
 380 }
 381 
 382 
 383 // ------------------------------------------------------------------
 384 // ciField::will_link
 385 //
 386 // Can a specific access to this field be made without causing
 387 // link errors?
 388 bool ciField::will_link(ciMethod* accessing_method,
 389                         Bytecodes::Code bc) {
 390   VM_ENTRY_MARK;
 391   assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
 392          bc == Bytecodes::_getfield  || bc == Bytecodes::_putfield  ||
 393          bc == Bytecodes::_withfield, "unexpected bytecode");
 394 
 395   if (_offset == -1) {
 396     // at creation we couldn't link to our holder so we need to
 397     // maintain that stance, otherwise there's no safe way to use this
 398     // ciField.
 399     return false;
 400   }
 401 
 402   // Check for static/nonstatic mismatch
 403   bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
 404   if (is_static != this->is_static()) {
 405     return false;
 406   }
 407 
 408   // Get and put can have different accessibility rules
 409   bool is_put    = (bc == Bytecodes::_putfield  || bc == Bytecodes::_putstatic);
 410   if (is_put) {
 411     if (_known_to_link_with_put == accessing_method) {
 412       return true;
 413     }


 438 // ------------------------------------------------------------------
 439 // ciField::print
 440 void ciField::print() {
 441   tty->print("<ciField name=");
 442   _holder->print_name();
 443   tty->print(".");
 444   _name->print_symbol();
 445   tty->print(" signature=");
 446   _signature->print_symbol();
 447   tty->print(" offset=%d type=", _offset);
 448   if (_type != NULL)
 449     _type->print_name();
 450   else
 451     tty->print("(reference)");
 452   tty->print(" flags=%04x", flags().as_int());
 453   tty->print(" is_constant=%s", bool_to_str(_is_constant));
 454   if (_is_constant && is_static()) {
 455     tty->print(" constant_value=");
 456     _constant_value.print();
 457   }
 458   tty->print(" is_flattenable=%s", bool_to_str(_is_flattenable));
 459   tty->print(" is_flattened=%s", bool_to_str(_is_flattened));
 460   tty->print(">");
 461 }
 462 
 463 // ------------------------------------------------------------------
 464 // ciField::print_name_on
 465 //
 466 // Print the name of this field
 467 void ciField::print_name_on(outputStream* st) {
 468   name()->print_symbol_on(st);
 469 }
< prev index next >