Path: blob/master/src/hotspot/share/ci/ciField.cpp
40930 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "ci/ciField.hpp"26#include "ci/ciInstanceKlass.hpp"27#include "ci/ciSymbols.hpp"28#include "ci/ciUtilities.inline.hpp"29#include "classfile/javaClasses.hpp"30#include "classfile/vmClasses.hpp"31#include "gc/shared/collectedHeap.inline.hpp"32#include "interpreter/linkResolver.hpp"33#include "oops/klass.inline.hpp"34#include "oops/oop.inline.hpp"35#include "runtime/fieldDescriptor.inline.hpp"36#include "runtime/handles.inline.hpp"37#include "runtime/reflectionUtils.hpp"3839// ciField40//41// This class represents the result of a field lookup in the VM.42// The lookup may not succeed, in which case the information in43// the ciField will be incomplete.4445// The ciObjectFactory cannot create circular data structures in one query.46// To avoid vicious circularities, we initialize ciField::_type to NULL47// for reference types and derive it lazily from the ciField::_signature.48// Primitive types are eagerly initialized, and basic layout queries49// can succeed without initialization, using only the BasicType of the field.5051// Notes on bootstrapping and shared CI objects: A field is shared if and52// only if it is (a) non-static and (b) declared by a shared instance klass.53// This allows non-static field lists to be cached on shared types.54// Because the _type field is lazily initialized, however, there is a55// special restriction that a shared field cannot cache an unshared type.56// This puts a small performance penalty on shared fields with unshared57// types, such as StackTraceElement[] Throwable.stackTrace.58// (Throwable is shared because ClassCastException is shared, but59// StackTraceElement is not presently shared.)6061// It is not a vicious circularity for a ciField to recursively create62// the ciSymbols necessary to represent its name and signature.63// Therefore, these items are created eagerly, and the name and signature64// of a shared field are themselves shared symbols. This somewhat65// pollutes the set of shared CI objects: It grows from 50 to 93 items,66// with all of the additional 43 being uninteresting shared ciSymbols.67// This adds at most one step to the binary search, an amount which68// decreases for complex compilation tasks.6970// ------------------------------------------------------------------71// ciField::ciField72ciField::ciField(ciInstanceKlass* klass, int index) :73_known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {74ASSERT_IN_VM;75CompilerThread *THREAD = CompilerThread::current();7677assert(ciObjectFactory::is_initialized(), "not a shared field");7879assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constant-pool");8081constantPoolHandle cpool(THREAD, klass->get_instanceKlass()->constants());8283// Get the field's name, signature, and type.84Symbol* name = cpool->name_ref_at(index);85_name = ciEnv::current(THREAD)->get_symbol(name);8687int nt_index = cpool->name_and_type_ref_index_at(index);88int sig_index = cpool->signature_ref_index_at(nt_index);89Symbol* signature = cpool->symbol_at(sig_index);90_signature = ciEnv::current(THREAD)->get_symbol(signature);9192BasicType field_type = Signature::basic_type(signature);9394// If the field is a pointer type, get the klass of the95// field.96if (is_reference_type(field_type)) {97bool ignore;98// This is not really a class reference; the index always refers to the99// field's type signature, as a symbol. Linkage checks do not apply.100_type = ciEnv::current(THREAD)->get_klass_by_index(cpool, sig_index, ignore, klass);101} else {102_type = ciType::make(field_type);103}104105_name = (ciSymbol*)ciEnv::current(THREAD)->get_symbol(name);106107// 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.111int holder_index = cpool->klass_ref_index_at(index);112bool holder_is_accessible;113114ciKlass* generic_declared_holder = ciEnv::current(THREAD)->get_klass_by_index(cpool, holder_index,115holder_is_accessible,116klass);117118if (generic_declared_holder->is_array_klass()) {119// If the declared holder of the field is an array class, assume that120// the canonical holder of that field is java.lang.Object. Arrays121// do not have fields; java.lang.Object is the only supertype of an122// array type that can declare fields and is therefore the canonical123// holder of the array type.124//125// Furthermore, the compilers assume that java.lang.Object does not126// have any fields. Therefore, the field is not looked up. Instead,127// the method returns partial information that will trigger special128// handling in ciField::will_link and will result in a129// java.lang.NoSuchFieldError exception being thrown by the compiled130// code (the expected behavior in this case).131_holder = ciEnv::current(THREAD)->Object_klass();132_offset = -1;133_is_constant = false;134return;135}136137ciInstanceKlass* declared_holder = generic_declared_holder->as_instance_klass();138139// The declared holder of this field may not have been loaded.140// Bail out with partial field information.141if (!holder_is_accessible) {142// _type has already been set.143// The default values for _flags and _constant_value will suffice.144// We need values for _holder, _offset, and _is_constant,145_holder = declared_holder;146_offset = -1;147_is_constant = false;148return;149}150151InstanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass();152153// Perform the field lookup.154fieldDescriptor field_desc;155Klass* canonical_holder =156loaded_decl_holder->find_field(name, signature, &field_desc);157if (canonical_holder == NULL) {158// Field lookup failed. Will be detected by will_link.159_holder = declared_holder;160_offset = -1;161_is_constant = false;162return;163}164165// Access check based on declared_holder. canonical_holder should not be used166// to check access because it can erroneously succeed. If this check fails,167// propagate the declared holder to will_link() which in turn will bail out168// compilation for this field access.169bool can_access = Reflection::verify_member_access(klass->get_Klass(),170declared_holder->get_Klass(),171canonical_holder,172field_desc.access_flags(),173true, false, THREAD);174if (!can_access) {175_holder = declared_holder;176_offset = -1;177_is_constant = false;178// It's possible the access check failed due to a nestmate access check179// encountering an exception. We can't propagate the exception from here180// so we have to clear it. If the access check happens again in a different181// context then the exception will be thrown there.182if (HAS_PENDING_EXCEPTION) {183CLEAR_PENDING_EXCEPTION;184}185return;186}187188assert(canonical_holder == field_desc.field_holder(), "just checking");189initialize_from(&field_desc);190}191192ciField::ciField(fieldDescriptor *fd) :193_known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {194ASSERT_IN_VM;195196// Get the field's name, signature, and type.197ciEnv* env = CURRENT_ENV;198_name = env->get_symbol(fd->name());199_signature = env->get_symbol(fd->signature());200201BasicType field_type = fd->field_type();202203// If the field is a pointer type, get the klass of the204// field.205if (is_reference_type(field_type)) {206_type = NULL; // must call compute_type on first access207} else {208_type = ciType::make(field_type);209}210211initialize_from(fd);212213// Either (a) it is marked shared, or else (b) we are done bootstrapping.214assert(is_shared() || ciObjectFactory::is_initialized(),215"bootstrap classes must not create & cache unshared fields");216}217218static bool trust_final_non_static_fields(ciInstanceKlass* holder) {219if (holder == NULL)220return false;221if (holder->name() == ciSymbols::java_lang_System())222// Never trust strangely unstable finals: System.out, etc.223return false;224// Even if general trusting is disabled, trust system-built closures in these packages.225if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke") ||226holder->is_in_package("jdk/internal/foreign") || holder->is_in_package("jdk/incubator/foreign") ||227holder->is_in_package("jdk/internal/vm/vector") || holder->is_in_package("jdk/incubator/vector") ||228holder->is_in_package("java/lang"))229return true;230// Trust hidden classes. They are created via Lookup.defineHiddenClass and231// can't be serialized, so there is no hacking of finals going on with them.232if (holder->is_hidden())233return true;234// Trust final fields in all boxed classes235if (holder->is_box_klass())236return true;237// Trust final fields in records238if (holder->is_record())239return true;240// Trust final fields in String241if (holder->name() == ciSymbols::java_lang_String())242return true;243// Trust Atomic*FieldUpdaters: they are very important for performance, and make up one244// more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.245if (holder->name() == ciSymbols::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||246holder->name() == ciSymbols::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||247holder->name() == ciSymbols::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||248holder->name() == ciSymbols::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {249return true;250}251return TrustFinalNonStaticFields;252}253254void ciField::initialize_from(fieldDescriptor* fd) {255// Get the flags, offset, and canonical holder of the field.256_flags = ciFlags(fd->access_flags());257_offset = fd->offset();258Klass* field_holder = fd->field_holder();259assert(field_holder != NULL, "null field_holder");260_holder = CURRENT_ENV->get_instance_klass(field_holder);261262// Check to see if the field is constant.263Klass* k = _holder->get_Klass();264bool is_stable_field = FoldStableValues && is_stable();265if ((is_final() && !has_initialized_final_update()) || is_stable_field) {266if (is_static()) {267// This field just may be constant. The only case where it will268// not be constant is when the field is a *special* static & final field269// whose value may change. The three examples are java.lang.System.in,270// java.lang.System.out, and java.lang.System.err.271assert(vmClasses::System_klass() != NULL, "Check once per vm");272if (k == vmClasses::System_klass()) {273// Check offsets for case 2: System.in, System.out, or System.err274if (_offset == java_lang_System::in_offset() ||275_offset == java_lang_System::out_offset() ||276_offset == java_lang_System::err_offset()) {277_is_constant = false;278return;279}280}281_is_constant = true;282} else {283// An instance field can be constant if it's a final static field or if284// it's a final non-static field of a trusted class (classes in285// java.lang.invoke and sun.invoke packages and subpackages).286_is_constant = is_stable_field || trust_final_non_static_fields(_holder);287}288} else {289// For CallSite objects treat the target field as a compile time constant.290assert(vmClasses::CallSite_klass() != NULL, "should be already initialized");291if (k == vmClasses::CallSite_klass() &&292_offset == java_lang_invoke_CallSite::target_offset()) {293assert(!has_initialized_final_update(), "CallSite is not supposed to have writes to final fields outside initializers");294_is_constant = true;295} else {296// Non-final & non-stable fields are not constants.297_is_constant = false;298}299}300}301302// ------------------------------------------------------------------303// ciField::constant_value304// Get the constant value of a this static field.305ciConstant ciField::constant_value() {306assert(is_static() && is_constant(), "illegal call to constant_value()");307if (!_holder->is_initialized()) {308return ciConstant(); // Not initialized yet309}310if (_constant_value.basic_type() == T_ILLEGAL) {311// Static fields are placed in mirror objects.312VM_ENTRY_MARK;313ciInstance* mirror = CURRENT_ENV->get_instance(_holder->get_Klass()->java_mirror());314_constant_value = mirror->field_value_impl(type()->basic_type(), offset());315}316if (FoldStableValues && is_stable() && _constant_value.is_null_or_zero()) {317return ciConstant();318}319return _constant_value;320}321322// ------------------------------------------------------------------323// ciField::constant_value_of324// Get the constant value of non-static final field in the given object.325ciConstant ciField::constant_value_of(ciObject* object) {326assert(!is_static() && is_constant(), "only if field is non-static constant");327assert(object->is_instance(), "must be instance");328ciConstant field_value = object->as_instance()->field_value(this);329if (FoldStableValues && is_stable() && field_value.is_null_or_zero()) {330return ciConstant();331}332return field_value;333}334335// ------------------------------------------------------------------336// ciField::compute_type337//338// Lazily compute the type, if it is an instance klass.339ciType* ciField::compute_type() {340GUARDED_VM_ENTRY(return compute_type_impl();)341}342343ciType* ciField::compute_type_impl() {344ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);345if (!type->is_primitive_type() && is_shared()) {346// We must not cache a pointer to an unshared type, in a shared field.347bool type_is_also_shared = false;348if (type->is_type_array_klass()) {349type_is_also_shared = true; // int[] etc. are explicitly bootstrapped350} else if (type->is_instance_klass()) {351type_is_also_shared = type->as_instance_klass()->is_shared();352} else {353// Currently there is no 'shared' query for array types.354type_is_also_shared = !ciObjectFactory::is_initialized();355}356if (!type_is_also_shared)357return type; // Bummer.358}359_type = type;360return type;361}362363364// ------------------------------------------------------------------365// ciField::will_link366//367// Can a specific access to this field be made without causing368// link errors?369bool ciField::will_link(ciMethod* accessing_method,370Bytecodes::Code bc) {371VM_ENTRY_MARK;372assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||373bc == Bytecodes::_getfield || bc == Bytecodes::_putfield,374"unexpected bytecode");375376if (_offset == -1) {377// at creation we couldn't link to our holder so we need to378// maintain that stance, otherwise there's no safe way to use this379// ciField.380return false;381}382383// Check for static/nonstatic mismatch384bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);385if (is_static != this->is_static()) {386return false;387}388389// Get and put can have different accessibility rules390bool is_put = (bc == Bytecodes::_putfield || bc == Bytecodes::_putstatic);391if (is_put) {392if (_known_to_link_with_put == accessing_method) {393return true;394}395} else {396if (_known_to_link_with_get == accessing_method->holder()) {397return true;398}399}400401LinkInfo link_info(_holder->get_instanceKlass(),402_name->get_symbol(), _signature->get_symbol(),403methodHandle(THREAD, accessing_method->get_Method()));404fieldDescriptor result;405LinkResolver::resolve_field(result, link_info, bc, false, CHECK_AND_CLEAR_(false));406407// update the hit-cache, unless there is a problem with memory scoping:408if (accessing_method->holder()->is_shared() || !is_shared()) {409if (is_put) {410_known_to_link_with_put = accessing_method;411} else {412_known_to_link_with_get = accessing_method->holder();413}414}415416return true;417}418419bool ciField::is_call_site_target() {420ciInstanceKlass* callsite_klass = CURRENT_ENV->CallSite_klass();421if (callsite_klass == NULL)422return false;423return (holder()->is_subclass_of(callsite_klass) && (name() == ciSymbols::target_name()));424}425426bool ciField::is_autobox_cache() {427ciSymbol* klass_name = holder()->name();428return (name() == ciSymbols::cache_field_name() &&429holder()->uses_default_loader() &&430(klass_name == ciSymbols::java_lang_Character_CharacterCache() ||431klass_name == ciSymbols::java_lang_Byte_ByteCache() ||432klass_name == ciSymbols::java_lang_Short_ShortCache() ||433klass_name == ciSymbols::java_lang_Integer_IntegerCache() ||434klass_name == ciSymbols::java_lang_Long_LongCache()));435}436437// ------------------------------------------------------------------438// ciField::print439void ciField::print() {440tty->print("<ciField name=");441_holder->print_name();442tty->print(".");443_name->print_symbol();444tty->print(" signature=");445_signature->print_symbol();446tty->print(" offset=%d type=", _offset);447if (_type != NULL)448_type->print_name();449else450tty->print("(reference)");451tty->print(" flags=%04x", flags().as_int());452tty->print(" is_constant=%s", bool_to_str(_is_constant));453if (_is_constant && is_static()) {454tty->print(" constant_value=");455_constant_value.print();456}457tty->print(">");458}459460// ------------------------------------------------------------------461// ciField::print_name_on462//463// Print the name of this field464void ciField::print_name_on(outputStream* st) {465name()->print_symbol_on(st);466}467468469