1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/ci/ciField.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,409 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "ci/ciField.hpp" 1.30 +#include "ci/ciInstanceKlass.hpp" 1.31 +#include "ci/ciUtilities.hpp" 1.32 +#include "classfile/systemDictionary.hpp" 1.33 +#include "gc_interface/collectedHeap.inline.hpp" 1.34 +#include "interpreter/linkResolver.hpp" 1.35 +#include "memory/universe.inline.hpp" 1.36 +#include "oops/oop.inline.hpp" 1.37 +#include "oops/oop.inline2.hpp" 1.38 +#include "runtime/fieldDescriptor.hpp" 1.39 + 1.40 +// ciField 1.41 +// 1.42 +// This class represents the result of a field lookup in the VM. 1.43 +// The lookup may not succeed, in which case the information in 1.44 +// the ciField will be incomplete. 1.45 + 1.46 +// The ciObjectFactory cannot create circular data structures in one query. 1.47 +// To avoid vicious circularities, we initialize ciField::_type to NULL 1.48 +// for reference types and derive it lazily from the ciField::_signature. 1.49 +// Primitive types are eagerly initialized, and basic layout queries 1.50 +// can succeed without initialization, using only the BasicType of the field. 1.51 + 1.52 +// Notes on bootstrapping and shared CI objects: A field is shared if and 1.53 +// only if it is (a) non-static and (b) declared by a shared instance klass. 1.54 +// This allows non-static field lists to be cached on shared types. 1.55 +// Because the _type field is lazily initialized, however, there is a 1.56 +// special restriction that a shared field cannot cache an unshared type. 1.57 +// This puts a small performance penalty on shared fields with unshared 1.58 +// types, such as StackTraceElement[] Throwable.stackTrace. 1.59 +// (Throwable is shared because ClassCastException is shared, but 1.60 +// StackTraceElement is not presently shared.) 1.61 + 1.62 +// It is not a vicious circularity for a ciField to recursively create 1.63 +// the ciSymbols necessary to represent its name and signature. 1.64 +// Therefore, these items are created eagerly, and the name and signature 1.65 +// of a shared field are themselves shared symbols. This somewhat 1.66 +// pollutes the set of shared CI objects: It grows from 50 to 93 items, 1.67 +// with all of the additional 43 being uninteresting shared ciSymbols. 1.68 +// This adds at most one step to the binary search, an amount which 1.69 +// decreases for complex compilation tasks. 1.70 + 1.71 +// ------------------------------------------------------------------ 1.72 +// ciField::ciField 1.73 +ciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) { 1.74 + ASSERT_IN_VM; 1.75 + CompilerThread *thread = CompilerThread::current(); 1.76 + 1.77 + assert(ciObjectFactory::is_initialized(), "not a shared field"); 1.78 + 1.79 + assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool"); 1.80 + 1.81 + constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants()); 1.82 + 1.83 + // Get the field's name, signature, and type. 1.84 + Symbol* name = cpool->name_ref_at(index); 1.85 + _name = ciEnv::current(thread)->get_symbol(name); 1.86 + 1.87 + int nt_index = cpool->name_and_type_ref_index_at(index); 1.88 + int sig_index = cpool->signature_ref_index_at(nt_index); 1.89 + Symbol* signature = cpool->symbol_at(sig_index); 1.90 + _signature = ciEnv::current(thread)->get_symbol(signature); 1.91 + 1.92 + BasicType field_type = FieldType::basic_type(signature); 1.93 + 1.94 + // If the field is a pointer type, get the klass of the 1.95 + // field. 1.96 + if (field_type == T_OBJECT || field_type == T_ARRAY) { 1.97 + bool ignore; 1.98 + // This is not really a class reference; the index always refers to the 1.99 + // field's type signature, as a symbol. Linkage checks do not apply. 1.100 + _type = ciEnv::current(thread)->get_klass_by_index(cpool, sig_index, ignore, klass); 1.101 + } else { 1.102 + _type = ciType::make(field_type); 1.103 + } 1.104 + 1.105 + _name = (ciSymbol*)ciEnv::current(thread)->get_symbol(name); 1.106 + 1.107 + // Get the field's declared holder. 1.108 + // 1.109 + // Note: we actually create a ciInstanceKlass for this klass, 1.110 + // even though we may not need to. 1.111 + int holder_index = cpool->klass_ref_index_at(index); 1.112 + bool holder_is_accessible; 1.113 + ciInstanceKlass* declared_holder = 1.114 + ciEnv::current(thread)->get_klass_by_index(cpool, holder_index, 1.115 + holder_is_accessible, 1.116 + klass)->as_instance_klass(); 1.117 + 1.118 + // The declared holder of this field may not have been loaded. 1.119 + // Bail out with partial field information. 1.120 + if (!holder_is_accessible) { 1.121 + // _type has already been set. 1.122 + // The default values for _flags and _constant_value will suffice. 1.123 + // We need values for _holder, _offset, and _is_constant, 1.124 + _holder = declared_holder; 1.125 + _offset = -1; 1.126 + _is_constant = false; 1.127 + return; 1.128 + } 1.129 + 1.130 + InstanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass(); 1.131 + 1.132 + // Perform the field lookup. 1.133 + fieldDescriptor field_desc; 1.134 + Klass* canonical_holder = 1.135 + loaded_decl_holder->find_field(name, signature, &field_desc); 1.136 + if (canonical_holder == NULL) { 1.137 + // Field lookup failed. Will be detected by will_link. 1.138 + _holder = declared_holder; 1.139 + _offset = -1; 1.140 + _is_constant = false; 1.141 + return; 1.142 + } 1.143 + 1.144 + // Access check based on declared_holder. canonical_holder should not be used 1.145 + // to check access because it can erroneously succeed. If this check fails, 1.146 + // propagate the declared holder to will_link() which in turn will bail out 1.147 + // compilation for this field access. 1.148 + if (!Reflection::verify_field_access(klass->get_Klass(), declared_holder->get_Klass(), canonical_holder, field_desc.access_flags(), true)) { 1.149 + _holder = declared_holder; 1.150 + _offset = -1; 1.151 + _is_constant = false; 1.152 + return; 1.153 + } 1.154 + 1.155 + assert(canonical_holder == field_desc.field_holder(), "just checking"); 1.156 + initialize_from(&field_desc); 1.157 +} 1.158 + 1.159 +ciField::ciField(fieldDescriptor *fd): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) { 1.160 + ASSERT_IN_VM; 1.161 + 1.162 + // Get the field's name, signature, and type. 1.163 + ciEnv* env = CURRENT_ENV; 1.164 + _name = env->get_symbol(fd->name()); 1.165 + _signature = env->get_symbol(fd->signature()); 1.166 + 1.167 + BasicType field_type = fd->field_type(); 1.168 + 1.169 + // If the field is a pointer type, get the klass of the 1.170 + // field. 1.171 + if (field_type == T_OBJECT || field_type == T_ARRAY) { 1.172 + _type = NULL; // must call compute_type on first access 1.173 + } else { 1.174 + _type = ciType::make(field_type); 1.175 + } 1.176 + 1.177 + initialize_from(fd); 1.178 + 1.179 + // Either (a) it is marked shared, or else (b) we are done bootstrapping. 1.180 + assert(is_shared() || ciObjectFactory::is_initialized(), 1.181 + "bootstrap classes must not create & cache unshared fields"); 1.182 +} 1.183 + 1.184 +static bool trust_final_non_static_fields(ciInstanceKlass* holder) { 1.185 + if (holder == NULL) 1.186 + return false; 1.187 + if (holder->name() == ciSymbol::java_lang_System()) 1.188 + // Never trust strangely unstable finals: System.out, etc. 1.189 + return false; 1.190 + // Even if general trusting is disabled, trust system-built closures in these packages. 1.191 + if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke")) 1.192 + return true; 1.193 + return TrustFinalNonStaticFields; 1.194 +} 1.195 + 1.196 +void ciField::initialize_from(fieldDescriptor* fd) { 1.197 + // Get the flags, offset, and canonical holder of the field. 1.198 + _flags = ciFlags(fd->access_flags()); 1.199 + _offset = fd->offset(); 1.200 + _holder = CURRENT_ENV->get_instance_klass(fd->field_holder()); 1.201 + 1.202 + // Check to see if the field is constant. 1.203 + bool is_final = this->is_final(); 1.204 + bool is_stable = FoldStableValues && this->is_stable(); 1.205 + if (_holder->is_initialized() && (is_final || is_stable)) { 1.206 + if (!this->is_static()) { 1.207 + // A field can be constant if it's a final static field or if 1.208 + // it's a final non-static field of a trusted class (classes in 1.209 + // java.lang.invoke and sun.invoke packages and subpackages). 1.210 + if (is_stable || trust_final_non_static_fields(_holder)) { 1.211 + _is_constant = true; 1.212 + return; 1.213 + } 1.214 + _is_constant = false; 1.215 + return; 1.216 + } 1.217 + 1.218 + // This field just may be constant. The only cases where it will 1.219 + // not be constant are: 1.220 + // 1.221 + // 1. The field holds a non-perm-space oop. The field is, strictly 1.222 + // speaking, constant but we cannot embed non-perm-space oops into 1.223 + // generated code. For the time being we need to consider the 1.224 + // field to be not constant. 1.225 + // 2. The field is a *special* static&final field whose value 1.226 + // may change. The three examples are java.lang.System.in, 1.227 + // java.lang.System.out, and java.lang.System.err. 1.228 + 1.229 + KlassHandle k = _holder->get_Klass(); 1.230 + assert( SystemDictionary::System_klass() != NULL, "Check once per vm"); 1.231 + if( k() == SystemDictionary::System_klass() ) { 1.232 + // Check offsets for case 2: System.in, System.out, or System.err 1.233 + if( _offset == java_lang_System::in_offset_in_bytes() || 1.234 + _offset == java_lang_System::out_offset_in_bytes() || 1.235 + _offset == java_lang_System::err_offset_in_bytes() ) { 1.236 + _is_constant = false; 1.237 + return; 1.238 + } 1.239 + } 1.240 + 1.241 + Handle mirror = k->java_mirror(); 1.242 + 1.243 + switch(type()->basic_type()) { 1.244 + case T_BYTE: 1.245 + _constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset)); 1.246 + break; 1.247 + case T_CHAR: 1.248 + _constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset)); 1.249 + break; 1.250 + case T_SHORT: 1.251 + _constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset)); 1.252 + break; 1.253 + case T_BOOLEAN: 1.254 + _constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset)); 1.255 + break; 1.256 + case T_INT: 1.257 + _constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset)); 1.258 + break; 1.259 + case T_FLOAT: 1.260 + _constant_value = ciConstant(mirror->float_field(_offset)); 1.261 + break; 1.262 + case T_DOUBLE: 1.263 + _constant_value = ciConstant(mirror->double_field(_offset)); 1.264 + break; 1.265 + case T_LONG: 1.266 + _constant_value = ciConstant(mirror->long_field(_offset)); 1.267 + break; 1.268 + case T_OBJECT: 1.269 + case T_ARRAY: 1.270 + { 1.271 + oop o = mirror->obj_field(_offset); 1.272 + 1.273 + // A field will be "constant" if it is known always to be 1.274 + // a non-null reference to an instance of a particular class, 1.275 + // or to a particular array. This can happen even if the instance 1.276 + // or array is not perm. In such a case, an "unloaded" ciArray 1.277 + // or ciInstance is created. The compiler may be able to use 1.278 + // information about the object's class (which is exact) or length. 1.279 + 1.280 + if (o == NULL) { 1.281 + _constant_value = ciConstant(type()->basic_type(), ciNullObject::make()); 1.282 + } else { 1.283 + _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o)); 1.284 + assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning"); 1.285 + } 1.286 + } 1.287 + } 1.288 + if (is_stable && _constant_value.is_null_or_zero()) { 1.289 + // It is not a constant after all; treat it as uninitialized. 1.290 + _is_constant = false; 1.291 + } else { 1.292 + _is_constant = true; 1.293 + } 1.294 + } else { 1.295 + _is_constant = false; 1.296 + } 1.297 +} 1.298 + 1.299 +// ------------------------------------------------------------------ 1.300 +// ciField::compute_type 1.301 +// 1.302 +// Lazily compute the type, if it is an instance klass. 1.303 +ciType* ciField::compute_type() { 1.304 + GUARDED_VM_ENTRY(return compute_type_impl();) 1.305 +} 1.306 + 1.307 +ciType* ciField::compute_type_impl() { 1.308 + ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false); 1.309 + if (!type->is_primitive_type() && is_shared()) { 1.310 + // We must not cache a pointer to an unshared type, in a shared field. 1.311 + bool type_is_also_shared = false; 1.312 + if (type->is_type_array_klass()) { 1.313 + type_is_also_shared = true; // int[] etc. are explicitly bootstrapped 1.314 + } else if (type->is_instance_klass()) { 1.315 + type_is_also_shared = type->as_instance_klass()->is_shared(); 1.316 + } else { 1.317 + // Currently there is no 'shared' query for array types. 1.318 + type_is_also_shared = !ciObjectFactory::is_initialized(); 1.319 + } 1.320 + if (!type_is_also_shared) 1.321 + return type; // Bummer. 1.322 + } 1.323 + _type = type; 1.324 + return type; 1.325 +} 1.326 + 1.327 + 1.328 +// ------------------------------------------------------------------ 1.329 +// ciField::will_link 1.330 +// 1.331 +// Can a specific access to this field be made without causing 1.332 +// link errors? 1.333 +bool ciField::will_link(ciInstanceKlass* accessing_klass, 1.334 + Bytecodes::Code bc) { 1.335 + VM_ENTRY_MARK; 1.336 + assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic || 1.337 + bc == Bytecodes::_getfield || bc == Bytecodes::_putfield, 1.338 + "unexpected bytecode"); 1.339 + 1.340 + if (_offset == -1) { 1.341 + // at creation we couldn't link to our holder so we need to 1.342 + // maintain that stance, otherwise there's no safe way to use this 1.343 + // ciField. 1.344 + return false; 1.345 + } 1.346 + 1.347 + // Check for static/nonstatic mismatch 1.348 + bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic); 1.349 + if (is_static != this->is_static()) { 1.350 + return false; 1.351 + } 1.352 + 1.353 + // Get and put can have different accessibility rules 1.354 + bool is_put = (bc == Bytecodes::_putfield || bc == Bytecodes::_putstatic); 1.355 + if (is_put) { 1.356 + if (_known_to_link_with_put == accessing_klass) { 1.357 + return true; 1.358 + } 1.359 + } else { 1.360 + if (_known_to_link_with_get == accessing_klass) { 1.361 + return true; 1.362 + } 1.363 + } 1.364 + 1.365 + fieldDescriptor result; 1.366 + LinkResolver::resolve_field(result, _holder->get_instanceKlass(), 1.367 + _name->get_symbol(), _signature->get_symbol(), 1.368 + accessing_klass->get_Klass(), bc, true, false, 1.369 + KILL_COMPILE_ON_FATAL_(false)); 1.370 + 1.371 + // update the hit-cache, unless there is a problem with memory scoping: 1.372 + if (accessing_klass->is_shared() || !is_shared()) { 1.373 + if (is_put) { 1.374 + _known_to_link_with_put = accessing_klass; 1.375 + } else { 1.376 + _known_to_link_with_get = accessing_klass; 1.377 + } 1.378 + } 1.379 + 1.380 + return true; 1.381 +} 1.382 + 1.383 +// ------------------------------------------------------------------ 1.384 +// ciField::print 1.385 +void ciField::print() { 1.386 + tty->print("<ciField name="); 1.387 + _holder->print_name(); 1.388 + tty->print("."); 1.389 + _name->print_symbol(); 1.390 + tty->print(" signature="); 1.391 + _signature->print_symbol(); 1.392 + tty->print(" offset=%d type=", _offset); 1.393 + if (_type != NULL) 1.394 + _type->print_name(); 1.395 + else 1.396 + tty->print("(reference)"); 1.397 + tty->print(" flags=%04x", flags().as_int()); 1.398 + tty->print(" is_constant=%s", bool_to_str(_is_constant)); 1.399 + if (_is_constant && is_static()) { 1.400 + tty->print(" constant_value="); 1.401 + _constant_value.print(); 1.402 + } 1.403 + tty->print(">"); 1.404 +} 1.405 + 1.406 +// ------------------------------------------------------------------ 1.407 +// ciField::print_name_on 1.408 +// 1.409 +// Print the name of this field 1.410 +void ciField::print_name_on(outputStream* st) { 1.411 + name()->print_symbol_on(st); 1.412 +}