Tue, 13 Dec 2016 14:37:04 -0500
8168699: Validate special case invocations
Reviewed-by: kevinw, vlivanov
duke@435 | 1 | /* |
coleenp@8739 | 2 | * Copyright (c) 1999, 2016, 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/ciInstance.hpp" |
stefank@2314 | 28 | #include "ci/ciInstanceKlass.hpp" |
stefank@2314 | 29 | #include "ci/ciUtilities.hpp" |
stefank@2314 | 30 | #include "classfile/systemDictionary.hpp" |
stefank@2314 | 31 | #include "memory/allocation.hpp" |
stefank@2314 | 32 | #include "memory/allocation.inline.hpp" |
stefank@2314 | 33 | #include "oops/oop.inline.hpp" |
never@3137 | 34 | #include "oops/fieldStreams.hpp" |
stefank@2314 | 35 | #include "runtime/fieldDescriptor.hpp" |
duke@435 | 36 | |
duke@435 | 37 | // ciInstanceKlass |
duke@435 | 38 | // |
coleenp@4037 | 39 | // This class represents a Klass* in the HotSpot virtual machine |
coleenp@4037 | 40 | // whose Klass part in an InstanceKlass. |
duke@435 | 41 | |
duke@435 | 42 | // ------------------------------------------------------------------ |
duke@435 | 43 | // ciInstanceKlass::ciInstanceKlass |
duke@435 | 44 | // |
duke@435 | 45 | // Loaded instance klass. |
kvn@479 | 46 | ciInstanceKlass::ciInstanceKlass(KlassHandle h_k) : |
kvn@479 | 47 | ciKlass(h_k), _non_static_fields(NULL) |
kvn@479 | 48 | { |
duke@435 | 49 | assert(get_Klass()->oop_is_instance(), "wrong type"); |
never@2551 | 50 | assert(get_instanceKlass()->is_loaded(), "must be at least loaded"); |
coleenp@4037 | 51 | InstanceKlass* ik = get_instanceKlass(); |
duke@435 | 52 | |
duke@435 | 53 | AccessFlags access_flags = ik->access_flags(); |
duke@435 | 54 | _flags = ciFlags(access_flags); |
duke@435 | 55 | _has_finalizer = access_flags.has_finalizer(); |
duke@435 | 56 | _has_subklass = ik->subklass() != NULL; |
coleenp@3368 | 57 | _init_state = ik->init_state(); |
duke@435 | 58 | _nonstatic_field_size = ik->nonstatic_field_size(); |
coleenp@548 | 59 | _has_nonstatic_fields = ik->has_nonstatic_fields(); |
iveresov@6050 | 60 | _has_default_methods = ik->has_default_methods(); |
coleenp@8739 | 61 | _is_anonymous = ik->is_anonymous(); |
duke@435 | 62 | _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields: |
duke@435 | 63 | |
jiangli@3701 | 64 | _implementor = NULL; // we will fill these lazily |
duke@435 | 65 | |
duke@435 | 66 | Thread *thread = Thread::current(); |
duke@435 | 67 | if (ciObjectFactory::is_initialized()) { |
duke@435 | 68 | _loader = JNIHandles::make_local(thread, ik->class_loader()); |
duke@435 | 69 | _protection_domain = JNIHandles::make_local(thread, |
duke@435 | 70 | ik->protection_domain()); |
duke@435 | 71 | _is_shared = false; |
duke@435 | 72 | } else { |
duke@435 | 73 | Handle h_loader(thread, ik->class_loader()); |
duke@435 | 74 | Handle h_protection_domain(thread, ik->protection_domain()); |
duke@435 | 75 | _loader = JNIHandles::make_global(h_loader); |
duke@435 | 76 | _protection_domain = JNIHandles::make_global(h_protection_domain); |
duke@435 | 77 | _is_shared = true; |
duke@435 | 78 | } |
duke@435 | 79 | |
duke@435 | 80 | // Lazy fields get filled in only upon request. |
duke@435 | 81 | _super = NULL; |
duke@435 | 82 | _java_mirror = NULL; |
duke@435 | 83 | |
duke@435 | 84 | if (is_shared()) { |
never@1577 | 85 | if (h_k() != SystemDictionary::Object_klass()) { |
duke@435 | 86 | super(); |
duke@435 | 87 | } |
duke@435 | 88 | //compute_nonstatic_fields(); // done outside of constructor |
duke@435 | 89 | } |
duke@435 | 90 | |
duke@435 | 91 | _field_cache = NULL; |
duke@435 | 92 | } |
duke@435 | 93 | |
duke@435 | 94 | // Version for unloaded classes: |
duke@435 | 95 | ciInstanceKlass::ciInstanceKlass(ciSymbol* name, |
duke@435 | 96 | jobject loader, jobject protection_domain) |
coleenp@4037 | 97 | : ciKlass(name, T_OBJECT) |
duke@435 | 98 | { |
duke@435 | 99 | assert(name->byte_at(0) != '[', "not an instance klass"); |
coleenp@4037 | 100 | _init_state = (InstanceKlass::ClassState)0; |
duke@435 | 101 | _nonstatic_field_size = -1; |
coleenp@548 | 102 | _has_nonstatic_fields = false; |
duke@435 | 103 | _nonstatic_fields = NULL; |
coleenp@8739 | 104 | _is_anonymous = false; |
duke@435 | 105 | _loader = loader; |
duke@435 | 106 | _protection_domain = protection_domain; |
duke@435 | 107 | _is_shared = false; |
duke@435 | 108 | _super = NULL; |
duke@435 | 109 | _java_mirror = NULL; |
duke@435 | 110 | _field_cache = NULL; |
duke@435 | 111 | } |
duke@435 | 112 | |
duke@435 | 113 | |
duke@435 | 114 | |
duke@435 | 115 | // ------------------------------------------------------------------ |
duke@435 | 116 | // ciInstanceKlass::compute_shared_is_initialized |
never@2000 | 117 | void ciInstanceKlass::compute_shared_init_state() { |
duke@435 | 118 | GUARDED_VM_ENTRY( |
coleenp@4037 | 119 | InstanceKlass* ik = get_instanceKlass(); |
coleenp@3368 | 120 | _init_state = ik->init_state(); |
duke@435 | 121 | ) |
duke@435 | 122 | } |
duke@435 | 123 | |
duke@435 | 124 | // ------------------------------------------------------------------ |
duke@435 | 125 | // ciInstanceKlass::compute_shared_has_subklass |
duke@435 | 126 | bool ciInstanceKlass::compute_shared_has_subklass() { |
duke@435 | 127 | GUARDED_VM_ENTRY( |
coleenp@4037 | 128 | InstanceKlass* ik = get_instanceKlass(); |
duke@435 | 129 | _has_subklass = ik->subklass() != NULL; |
duke@435 | 130 | return _has_subklass; |
duke@435 | 131 | ) |
duke@435 | 132 | } |
duke@435 | 133 | |
duke@435 | 134 | // ------------------------------------------------------------------ |
duke@435 | 135 | // ciInstanceKlass::loader |
duke@435 | 136 | oop ciInstanceKlass::loader() { |
duke@435 | 137 | ASSERT_IN_VM; |
duke@435 | 138 | return JNIHandles::resolve(_loader); |
duke@435 | 139 | } |
duke@435 | 140 | |
duke@435 | 141 | // ------------------------------------------------------------------ |
duke@435 | 142 | // ciInstanceKlass::loader_handle |
duke@435 | 143 | jobject ciInstanceKlass::loader_handle() { |
duke@435 | 144 | return _loader; |
duke@435 | 145 | } |
duke@435 | 146 | |
duke@435 | 147 | // ------------------------------------------------------------------ |
duke@435 | 148 | // ciInstanceKlass::protection_domain |
duke@435 | 149 | oop ciInstanceKlass::protection_domain() { |
duke@435 | 150 | ASSERT_IN_VM; |
duke@435 | 151 | return JNIHandles::resolve(_protection_domain); |
duke@435 | 152 | } |
duke@435 | 153 | |
duke@435 | 154 | // ------------------------------------------------------------------ |
duke@435 | 155 | // ciInstanceKlass::protection_domain_handle |
duke@435 | 156 | jobject ciInstanceKlass::protection_domain_handle() { |
duke@435 | 157 | return _protection_domain; |
duke@435 | 158 | } |
duke@435 | 159 | |
duke@435 | 160 | // ------------------------------------------------------------------ |
duke@435 | 161 | // ciInstanceKlass::field_cache |
duke@435 | 162 | // |
duke@435 | 163 | // Get the field cache associated with this klass. |
duke@435 | 164 | ciConstantPoolCache* ciInstanceKlass::field_cache() { |
duke@435 | 165 | if (is_shared()) { |
duke@435 | 166 | return NULL; |
duke@435 | 167 | } |
duke@435 | 168 | if (_field_cache == NULL) { |
duke@435 | 169 | assert(!is_java_lang_Object(), "Object has no fields"); |
duke@435 | 170 | Arena* arena = CURRENT_ENV->arena(); |
duke@435 | 171 | _field_cache = new (arena) ciConstantPoolCache(arena, 5); |
duke@435 | 172 | } |
duke@435 | 173 | return _field_cache; |
duke@435 | 174 | } |
duke@435 | 175 | |
duke@435 | 176 | // ------------------------------------------------------------------ |
duke@435 | 177 | // ciInstanceKlass::get_canonical_holder |
duke@435 | 178 | // |
duke@435 | 179 | ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) { |
duke@435 | 180 | #ifdef ASSERT |
duke@435 | 181 | if (!(offset >= 0 && offset < layout_helper())) { |
duke@435 | 182 | tty->print("*** get_canonical_holder(%d) on ", offset); |
duke@435 | 183 | this->print(); |
duke@435 | 184 | tty->print_cr(" ***"); |
duke@435 | 185 | }; |
duke@435 | 186 | assert(offset >= 0 && offset < layout_helper(), "offset must be tame"); |
duke@435 | 187 | #endif |
duke@435 | 188 | |
coleenp@548 | 189 | if (offset < instanceOopDesc::base_offset_in_bytes()) { |
duke@435 | 190 | // All header offsets belong properly to java/lang/Object. |
duke@435 | 191 | return CURRENT_ENV->Object_klass(); |
duke@435 | 192 | } |
duke@435 | 193 | |
duke@435 | 194 | ciInstanceKlass* self = this; |
duke@435 | 195 | for (;;) { |
duke@435 | 196 | assert(self->is_loaded(), "must be loaded to have size"); |
duke@435 | 197 | ciInstanceKlass* super = self->super(); |
coleenp@548 | 198 | if (super == NULL || super->nof_nonstatic_fields() == 0 || |
coleenp@548 | 199 | !super->contains_field_offset(offset)) { |
duke@435 | 200 | return self; |
duke@435 | 201 | } else { |
duke@435 | 202 | self = super; // return super->get_canonical_holder(offset) |
duke@435 | 203 | } |
duke@435 | 204 | } |
duke@435 | 205 | } |
duke@435 | 206 | |
duke@435 | 207 | // ------------------------------------------------------------------ |
duke@435 | 208 | // ciInstanceKlass::is_java_lang_Object |
duke@435 | 209 | // |
duke@435 | 210 | // Is this klass java.lang.Object? |
coleenp@4037 | 211 | bool ciInstanceKlass::is_java_lang_Object() const { |
duke@435 | 212 | return equals(CURRENT_ENV->Object_klass()); |
duke@435 | 213 | } |
duke@435 | 214 | |
duke@435 | 215 | // ------------------------------------------------------------------ |
duke@435 | 216 | // ciInstanceKlass::uses_default_loader |
kvn@5110 | 217 | bool ciInstanceKlass::uses_default_loader() const { |
twisti@1573 | 218 | // Note: We do not need to resolve the handle or enter the VM |
twisti@1573 | 219 | // in order to test null-ness. |
twisti@1573 | 220 | return _loader == NULL; |
twisti@1573 | 221 | } |
twisti@1573 | 222 | |
twisti@1573 | 223 | // ------------------------------------------------------------------ |
kvn@5110 | 224 | |
kvn@5110 | 225 | /** |
kvn@5110 | 226 | * Return basic type of boxed value for box klass or T_OBJECT if not. |
kvn@5110 | 227 | */ |
kvn@5110 | 228 | BasicType ciInstanceKlass::box_klass_type() const { |
kvn@5110 | 229 | if (uses_default_loader() && is_loaded()) { |
kvn@5110 | 230 | return SystemDictionary::box_klass_type(get_Klass()); |
kvn@5110 | 231 | } else { |
kvn@5110 | 232 | return T_OBJECT; |
kvn@5110 | 233 | } |
kvn@5110 | 234 | } |
kvn@5110 | 235 | |
kvn@5110 | 236 | /** |
kvn@5110 | 237 | * Is this boxing klass? |
kvn@5110 | 238 | */ |
kvn@5110 | 239 | bool ciInstanceKlass::is_box_klass() const { |
kvn@5110 | 240 | return is_java_primitive(box_klass_type()); |
kvn@5110 | 241 | } |
kvn@5110 | 242 | |
kvn@5110 | 243 | /** |
kvn@5110 | 244 | * Is this boxed value offset? |
kvn@5110 | 245 | */ |
kvn@5110 | 246 | bool ciInstanceKlass::is_boxed_value_offset(int offset) const { |
kvn@5110 | 247 | BasicType bt = box_klass_type(); |
kvn@5110 | 248 | return is_java_primitive(bt) && |
kvn@5110 | 249 | (offset == java_lang_boxing_object::value_offset_in_bytes(bt)); |
kvn@5110 | 250 | } |
kvn@5110 | 251 | |
kvn@5110 | 252 | // ------------------------------------------------------------------ |
twisti@1573 | 253 | // ciInstanceKlass::is_in_package |
twisti@1573 | 254 | // |
twisti@1573 | 255 | // Is this klass in the given package? |
twisti@1573 | 256 | bool ciInstanceKlass::is_in_package(const char* packagename, int len) { |
twisti@1573 | 257 | // To avoid class loader mischief, this test always rejects application classes. |
twisti@1573 | 258 | if (!uses_default_loader()) |
twisti@1573 | 259 | return false; |
twisti@1573 | 260 | GUARDED_VM_ENTRY( |
twisti@1573 | 261 | return is_in_package_impl(packagename, len); |
twisti@1573 | 262 | ) |
twisti@1573 | 263 | } |
twisti@1573 | 264 | |
twisti@1573 | 265 | bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) { |
twisti@1573 | 266 | ASSERT_IN_VM; |
twisti@1573 | 267 | |
twisti@1573 | 268 | // If packagename contains trailing '/' exclude it from the |
twisti@1573 | 269 | // prefix-test since we test for it explicitly. |
twisti@1573 | 270 | if (packagename[len - 1] == '/') |
twisti@1573 | 271 | len--; |
twisti@1573 | 272 | |
twisti@1573 | 273 | if (!name()->starts_with(packagename, len)) |
twisti@1573 | 274 | return false; |
twisti@1573 | 275 | |
twisti@1573 | 276 | // Test if the class name is something like "java/lang". |
twisti@1573 | 277 | if ((len + 1) > name()->utf8_length()) |
twisti@1573 | 278 | return false; |
twisti@1573 | 279 | |
twisti@1573 | 280 | // Test for trailing '/' |
twisti@1573 | 281 | if ((char) name()->byte_at(len) != '/') |
twisti@1573 | 282 | return false; |
twisti@1573 | 283 | |
twisti@1573 | 284 | // Make sure it's not actually in a subpackage: |
twisti@1573 | 285 | if (name()->index_of_at(len+1, "/", 1) >= 0) |
twisti@1573 | 286 | return false; |
twisti@1573 | 287 | |
twisti@1573 | 288 | return true; |
duke@435 | 289 | } |
duke@435 | 290 | |
duke@435 | 291 | // ------------------------------------------------------------------ |
duke@435 | 292 | // ciInstanceKlass::print_impl |
duke@435 | 293 | // |
duke@435 | 294 | // Implementation of the print method. |
duke@435 | 295 | void ciInstanceKlass::print_impl(outputStream* st) { |
duke@435 | 296 | ciKlass::print_impl(st); |
drchase@6680 | 297 | GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i((address)loader()));) |
duke@435 | 298 | if (is_loaded()) { |
duke@435 | 299 | st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=", |
duke@435 | 300 | bool_to_str(is_initialized()), |
duke@435 | 301 | bool_to_str(has_finalizer()), |
duke@435 | 302 | bool_to_str(has_subklass()), |
duke@435 | 303 | layout_helper()); |
duke@435 | 304 | |
duke@435 | 305 | _flags.print_klass_flags(); |
duke@435 | 306 | |
duke@435 | 307 | if (_super) { |
duke@435 | 308 | st->print(" super="); |
duke@435 | 309 | _super->print_name(); |
duke@435 | 310 | } |
duke@435 | 311 | if (_java_mirror) { |
duke@435 | 312 | st->print(" mirror=PRESENT"); |
duke@435 | 313 | } |
duke@435 | 314 | } else { |
duke@435 | 315 | st->print(" loaded=false"); |
duke@435 | 316 | } |
duke@435 | 317 | } |
duke@435 | 318 | |
duke@435 | 319 | // ------------------------------------------------------------------ |
duke@435 | 320 | // ciInstanceKlass::super |
duke@435 | 321 | // |
duke@435 | 322 | // Get the superklass of this klass. |
duke@435 | 323 | ciInstanceKlass* ciInstanceKlass::super() { |
duke@435 | 324 | assert(is_loaded(), "must be loaded"); |
duke@435 | 325 | if (_super == NULL && !is_java_lang_Object()) { |
duke@435 | 326 | GUARDED_VM_ENTRY( |
coleenp@4037 | 327 | Klass* super_klass = get_instanceKlass()->super(); |
coleenp@4037 | 328 | _super = CURRENT_ENV->get_instance_klass(super_klass); |
duke@435 | 329 | ) |
duke@435 | 330 | } |
duke@435 | 331 | return _super; |
duke@435 | 332 | } |
duke@435 | 333 | |
duke@435 | 334 | // ------------------------------------------------------------------ |
duke@435 | 335 | // ciInstanceKlass::java_mirror |
duke@435 | 336 | // |
duke@435 | 337 | // Get the instance of java.lang.Class corresponding to this klass. |
jrose@1959 | 338 | // Cache it on this->_java_mirror. |
duke@435 | 339 | ciInstance* ciInstanceKlass::java_mirror() { |
never@2658 | 340 | if (is_shared()) { |
never@2658 | 341 | return ciKlass::java_mirror(); |
never@2658 | 342 | } |
duke@435 | 343 | if (_java_mirror == NULL) { |
jrose@1959 | 344 | _java_mirror = ciKlass::java_mirror(); |
duke@435 | 345 | } |
duke@435 | 346 | return _java_mirror; |
duke@435 | 347 | } |
duke@435 | 348 | |
duke@435 | 349 | // ------------------------------------------------------------------ |
duke@435 | 350 | // ciInstanceKlass::unique_concrete_subklass |
duke@435 | 351 | ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() { |
duke@435 | 352 | if (!is_loaded()) return NULL; // No change if class is not loaded |
duke@435 | 353 | if (!is_abstract()) return NULL; // Only applies to abstract classes. |
duke@435 | 354 | if (!has_subklass()) return NULL; // Must have at least one subklass. |
duke@435 | 355 | VM_ENTRY_MARK; |
coleenp@4037 | 356 | InstanceKlass* ik = get_instanceKlass(); |
duke@435 | 357 | Klass* up = ik->up_cast_abstract(); |
coleenp@4037 | 358 | assert(up->oop_is_instance(), "must be InstanceKlass"); |
duke@435 | 359 | if (ik == up) { |
duke@435 | 360 | return NULL; |
duke@435 | 361 | } |
coleenp@4037 | 362 | return CURRENT_THREAD_ENV->get_instance_klass(up); |
duke@435 | 363 | } |
duke@435 | 364 | |
duke@435 | 365 | // ------------------------------------------------------------------ |
duke@435 | 366 | // ciInstanceKlass::has_finalizable_subclass |
duke@435 | 367 | bool ciInstanceKlass::has_finalizable_subclass() { |
duke@435 | 368 | if (!is_loaded()) return true; |
duke@435 | 369 | VM_ENTRY_MARK; |
duke@435 | 370 | return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL; |
duke@435 | 371 | } |
duke@435 | 372 | |
duke@435 | 373 | // ------------------------------------------------------------------ |
duke@435 | 374 | // ciInstanceKlass::get_field_by_offset |
duke@435 | 375 | ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) { |
duke@435 | 376 | if (!is_static) { |
duke@435 | 377 | for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) { |
duke@435 | 378 | ciField* field = _nonstatic_fields->at(i); |
duke@435 | 379 | int field_off = field->offset_in_bytes(); |
duke@435 | 380 | if (field_off == field_offset) |
duke@435 | 381 | return field; |
duke@435 | 382 | if (field_off > field_offset) |
duke@435 | 383 | break; |
duke@435 | 384 | // could do binary search or check bins, but probably not worth it |
duke@435 | 385 | } |
duke@435 | 386 | return NULL; |
duke@435 | 387 | } |
duke@435 | 388 | VM_ENTRY_MARK; |
coleenp@4037 | 389 | InstanceKlass* k = get_instanceKlass(); |
duke@435 | 390 | fieldDescriptor fd; |
duke@435 | 391 | if (!k->find_field_from_offset(field_offset, is_static, &fd)) { |
duke@435 | 392 | return NULL; |
duke@435 | 393 | } |
duke@435 | 394 | ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd); |
duke@435 | 395 | return field; |
duke@435 | 396 | } |
duke@435 | 397 | |
kvn@479 | 398 | // ------------------------------------------------------------------ |
never@1515 | 399 | // ciInstanceKlass::get_field_by_name |
never@1515 | 400 | ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) { |
never@1515 | 401 | VM_ENTRY_MARK; |
coleenp@4037 | 402 | InstanceKlass* k = get_instanceKlass(); |
never@1515 | 403 | fieldDescriptor fd; |
coleenp@4037 | 404 | Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd); |
never@1515 | 405 | if (def == NULL) { |
never@1515 | 406 | return NULL; |
never@1515 | 407 | } |
never@1515 | 408 | ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd); |
never@1515 | 409 | return field; |
never@1515 | 410 | } |
never@1515 | 411 | |
never@1515 | 412 | // ------------------------------------------------------------------ |
kvn@479 | 413 | // ciInstanceKlass::non_static_fields. |
kvn@479 | 414 | |
kvn@479 | 415 | class NonStaticFieldFiller: public FieldClosure { |
kvn@479 | 416 | GrowableArray<ciField*>* _arr; |
kvn@479 | 417 | ciEnv* _curEnv; |
kvn@479 | 418 | public: |
kvn@479 | 419 | NonStaticFieldFiller(ciEnv* curEnv, GrowableArray<ciField*>* arr) : |
kvn@479 | 420 | _curEnv(curEnv), _arr(arr) |
kvn@479 | 421 | {} |
kvn@479 | 422 | void do_field(fieldDescriptor* fd) { |
kvn@479 | 423 | ciField* field = new (_curEnv->arena()) ciField(fd); |
kvn@479 | 424 | _arr->append(field); |
kvn@479 | 425 | } |
kvn@479 | 426 | }; |
kvn@479 | 427 | |
kvn@479 | 428 | GrowableArray<ciField*>* ciInstanceKlass::non_static_fields() { |
kvn@479 | 429 | if (_non_static_fields == NULL) { |
kvn@479 | 430 | VM_ENTRY_MARK; |
kvn@479 | 431 | ciEnv* curEnv = ciEnv::current(); |
coleenp@4037 | 432 | InstanceKlass* ik = get_instanceKlass(); |
never@3137 | 433 | int max_n_fields = ik->java_fields_count(); |
kvn@479 | 434 | |
kvn@2040 | 435 | Arena* arena = curEnv->arena(); |
kvn@479 | 436 | _non_static_fields = |
kvn@2040 | 437 | new (arena) GrowableArray<ciField*>(arena, max_n_fields, 0, NULL); |
kvn@479 | 438 | NonStaticFieldFiller filler(curEnv, _non_static_fields); |
kvn@479 | 439 | ik->do_nonstatic_fields(&filler); |
kvn@479 | 440 | } |
kvn@479 | 441 | return _non_static_fields; |
kvn@479 | 442 | } |
kvn@479 | 443 | |
duke@435 | 444 | static int sort_field_by_offset(ciField** a, ciField** b) { |
duke@435 | 445 | return (*a)->offset_in_bytes() - (*b)->offset_in_bytes(); |
duke@435 | 446 | // (no worries about 32-bit overflow...) |
duke@435 | 447 | } |
duke@435 | 448 | |
duke@435 | 449 | // ------------------------------------------------------------------ |
duke@435 | 450 | // ciInstanceKlass::compute_nonstatic_fields |
duke@435 | 451 | int ciInstanceKlass::compute_nonstatic_fields() { |
duke@435 | 452 | assert(is_loaded(), "must be loaded"); |
duke@435 | 453 | |
duke@435 | 454 | if (_nonstatic_fields != NULL) |
duke@435 | 455 | return _nonstatic_fields->length(); |
duke@435 | 456 | |
coleenp@548 | 457 | if (!has_nonstatic_fields()) { |
duke@435 | 458 | Arena* arena = CURRENT_ENV->arena(); |
duke@435 | 459 | _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL); |
duke@435 | 460 | return 0; |
duke@435 | 461 | } |
duke@435 | 462 | assert(!is_java_lang_Object(), "bootstrap OK"); |
duke@435 | 463 | |
coleenp@548 | 464 | // Size in bytes of my fields, including inherited fields. |
kvn@600 | 465 | int fsize = nonstatic_field_size() * heapOopSize; |
coleenp@548 | 466 | |
duke@435 | 467 | ciInstanceKlass* super = this->super(); |
duke@435 | 468 | GrowableArray<ciField*>* super_fields = NULL; |
coleenp@548 | 469 | if (super != NULL && super->has_nonstatic_fields()) { |
kvn@600 | 470 | int super_fsize = super->nonstatic_field_size() * heapOopSize; |
coleenp@548 | 471 | int super_flen = super->nof_nonstatic_fields(); |
duke@435 | 472 | super_fields = super->_nonstatic_fields; |
duke@435 | 473 | assert(super_flen == 0 || super_fields != NULL, "first get nof_fields"); |
coleenp@548 | 474 | // See if I am no larger than my super; if so, I can use his fields. |
coleenp@548 | 475 | if (fsize == super_fsize) { |
coleenp@548 | 476 | _nonstatic_fields = super_fields; |
coleenp@548 | 477 | return super_fields->length(); |
coleenp@548 | 478 | } |
duke@435 | 479 | } |
duke@435 | 480 | |
duke@435 | 481 | GrowableArray<ciField*>* fields = NULL; |
duke@435 | 482 | GUARDED_VM_ENTRY({ |
duke@435 | 483 | fields = compute_nonstatic_fields_impl(super_fields); |
duke@435 | 484 | }); |
duke@435 | 485 | |
duke@435 | 486 | if (fields == NULL) { |
duke@435 | 487 | // This can happen if this class (java.lang.Class) has invisible fields. |
duke@435 | 488 | _nonstatic_fields = super_fields; |
duke@435 | 489 | return super_fields->length(); |
duke@435 | 490 | } |
duke@435 | 491 | |
duke@435 | 492 | int flen = fields->length(); |
duke@435 | 493 | |
duke@435 | 494 | // Now sort them by offset, ascending. |
duke@435 | 495 | // (In principle, they could mix with superclass fields.) |
duke@435 | 496 | fields->sort(sort_field_by_offset); |
duke@435 | 497 | _nonstatic_fields = fields; |
duke@435 | 498 | return flen; |
duke@435 | 499 | } |
duke@435 | 500 | |
duke@435 | 501 | GrowableArray<ciField*>* |
duke@435 | 502 | ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>* |
duke@435 | 503 | super_fields) { |
duke@435 | 504 | ASSERT_IN_VM; |
duke@435 | 505 | Arena* arena = CURRENT_ENV->arena(); |
duke@435 | 506 | int flen = 0; |
duke@435 | 507 | GrowableArray<ciField*>* fields = NULL; |
coleenp@4037 | 508 | InstanceKlass* k = get_instanceKlass(); |
never@3137 | 509 | for (JavaFieldStream fs(k); !fs.done(); fs.next()) { |
never@3137 | 510 | if (fs.access_flags().is_static()) continue; |
never@3137 | 511 | flen += 1; |
never@3137 | 512 | } |
duke@435 | 513 | |
never@3137 | 514 | // allocate the array: |
never@3137 | 515 | if (flen == 0) { |
never@3137 | 516 | return NULL; // return nothing if none are locally declared |
never@3137 | 517 | } |
never@3137 | 518 | if (super_fields != NULL) { |
never@3137 | 519 | flen += super_fields->length(); |
never@3137 | 520 | } |
never@3137 | 521 | fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL); |
never@3137 | 522 | if (super_fields != NULL) { |
never@3137 | 523 | fields->appendAll(super_fields); |
never@3137 | 524 | } |
never@3137 | 525 | |
never@3137 | 526 | for (JavaFieldStream fs(k); !fs.done(); fs.next()) { |
never@3137 | 527 | if (fs.access_flags().is_static()) continue; |
drchase@5732 | 528 | fieldDescriptor& fd = fs.field_descriptor(); |
never@3137 | 529 | ciField* field = new (arena) ciField(&fd); |
never@3137 | 530 | fields->append(field); |
duke@435 | 531 | } |
duke@435 | 532 | assert(fields->length() == flen, "sanity"); |
duke@435 | 533 | return fields; |
duke@435 | 534 | } |
duke@435 | 535 | |
duke@435 | 536 | // ------------------------------------------------------------------ |
duke@435 | 537 | // ciInstanceKlass::find_method |
duke@435 | 538 | // |
duke@435 | 539 | // Find a method in this klass. |
duke@435 | 540 | ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) { |
duke@435 | 541 | VM_ENTRY_MARK; |
coleenp@4037 | 542 | InstanceKlass* k = get_instanceKlass(); |
coleenp@2497 | 543 | Symbol* name_sym = name->get_symbol(); |
coleenp@2497 | 544 | Symbol* sig_sym= signature->get_symbol(); |
duke@435 | 545 | |
coleenp@4037 | 546 | Method* m = k->find_method(name_sym, sig_sym); |
duke@435 | 547 | if (m == NULL) return NULL; |
duke@435 | 548 | |
coleenp@4037 | 549 | return CURRENT_THREAD_ENV->get_method(m); |
duke@435 | 550 | } |
duke@435 | 551 | |
duke@435 | 552 | // ------------------------------------------------------------------ |
duke@435 | 553 | // ciInstanceKlass::is_leaf_type |
duke@435 | 554 | bool ciInstanceKlass::is_leaf_type() { |
duke@435 | 555 | assert(is_loaded(), "must be loaded"); |
duke@435 | 556 | if (is_shared()) { |
duke@435 | 557 | return is_final(); // approximately correct |
duke@435 | 558 | } else { |
jiangli@3701 | 559 | return !_has_subklass && (nof_implementors() == 0); |
duke@435 | 560 | } |
duke@435 | 561 | } |
duke@435 | 562 | |
duke@435 | 563 | // ------------------------------------------------------------------ |
duke@435 | 564 | // ciInstanceKlass::implementor |
duke@435 | 565 | // |
duke@435 | 566 | // Report an implementor of this interface. |
duke@435 | 567 | // Note that there are various races here, since my copy |
duke@435 | 568 | // of _nof_implementors might be out of date with respect |
coleenp@4037 | 569 | // to results returned by InstanceKlass::implementor. |
duke@435 | 570 | // This is OK, since any dependencies we decide to assert |
duke@435 | 571 | // will be checked later under the Compile_lock. |
jiangli@3701 | 572 | ciInstanceKlass* ciInstanceKlass::implementor() { |
jiangli@3701 | 573 | ciInstanceKlass* impl = _implementor; |
duke@435 | 574 | if (impl == NULL) { |
duke@435 | 575 | // Go into the VM to fetch the implementor. |
duke@435 | 576 | { |
duke@435 | 577 | VM_ENTRY_MARK; |
coleenp@4037 | 578 | Klass* k = get_instanceKlass()->implementor(); |
duke@435 | 579 | if (k != NULL) { |
coleenp@4037 | 580 | if (k == get_instanceKlass()) { |
jiangli@3701 | 581 | // More than one implementors. Use 'this' in this case. |
jiangli@3701 | 582 | impl = this; |
jiangli@3701 | 583 | } else { |
coleenp@4037 | 584 | impl = CURRENT_THREAD_ENV->get_instance_klass(k); |
jiangli@3701 | 585 | } |
duke@435 | 586 | } |
duke@435 | 587 | } |
duke@435 | 588 | // Memoize this result. |
duke@435 | 589 | if (!is_shared()) { |
jiangli@3701 | 590 | _implementor = impl; |
duke@435 | 591 | } |
duke@435 | 592 | } |
duke@435 | 593 | return impl; |
duke@435 | 594 | } |
minqi@4267 | 595 | |
coleenp@8739 | 596 | ciInstanceKlass* ciInstanceKlass::host_klass() { |
coleenp@8739 | 597 | assert(is_loaded(), "must be loaded"); |
coleenp@8739 | 598 | if (is_anonymous()) { |
coleenp@8739 | 599 | VM_ENTRY_MARK |
coleenp@8739 | 600 | Klass* host_klass = get_instanceKlass()->host_klass(); |
coleenp@8739 | 601 | return CURRENT_ENV->get_instance_klass(host_klass); |
coleenp@8739 | 602 | } |
coleenp@8739 | 603 | return NULL; |
coleenp@8739 | 604 | } |
coleenp@8739 | 605 | |
minqi@4267 | 606 | // Utility class for printing of the contents of the static fields for |
minqi@4267 | 607 | // use by compilation replay. It only prints out the information that |
minqi@4267 | 608 | // could be consumed by the compiler, so for primitive types it prints |
minqi@4267 | 609 | // out the actual value. For Strings it's the actual string value. |
minqi@4267 | 610 | // For array types it it's first level array size since that's the |
minqi@4267 | 611 | // only value which statically unchangeable. For all other reference |
minqi@4267 | 612 | // types it simply prints out the dynamic type. |
minqi@4267 | 613 | |
minqi@4267 | 614 | class StaticFinalFieldPrinter : public FieldClosure { |
minqi@4267 | 615 | outputStream* _out; |
minqi@4267 | 616 | const char* _holder; |
minqi@4267 | 617 | public: |
minqi@4267 | 618 | StaticFinalFieldPrinter(outputStream* out, const char* holder) : |
minqi@4267 | 619 | _out(out), |
minqi@4267 | 620 | _holder(holder) { |
minqi@4267 | 621 | } |
minqi@4267 | 622 | void do_field(fieldDescriptor* fd) { |
minqi@4267 | 623 | if (fd->is_final() && !fd->has_initial_value()) { |
vlivanov@4531 | 624 | ResourceMark rm; |
minqi@4267 | 625 | oop mirror = fd->field_holder()->java_mirror(); |
minqi@4267 | 626 | _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii()); |
minqi@4267 | 627 | switch (fd->field_type()) { |
minqi@4267 | 628 | case T_BYTE: _out->print_cr("%d", mirror->byte_field(fd->offset())); break; |
minqi@4267 | 629 | case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset())); break; |
minqi@4267 | 630 | case T_SHORT: _out->print_cr("%d", mirror->short_field(fd->offset())); break; |
minqi@4267 | 631 | case T_CHAR: _out->print_cr("%d", mirror->char_field(fd->offset())); break; |
minqi@4267 | 632 | case T_INT: _out->print_cr("%d", mirror->int_field(fd->offset())); break; |
drchase@6680 | 633 | case T_LONG: _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break; |
minqi@4267 | 634 | case T_FLOAT: { |
minqi@4267 | 635 | float f = mirror->float_field(fd->offset()); |
minqi@4267 | 636 | _out->print_cr("%d", *(int*)&f); |
minqi@4267 | 637 | break; |
minqi@4267 | 638 | } |
minqi@4267 | 639 | case T_DOUBLE: { |
minqi@4267 | 640 | double d = mirror->double_field(fd->offset()); |
drchase@6680 | 641 | _out->print_cr(INT64_FORMAT, *(int64_t*)&d); |
minqi@4267 | 642 | break; |
minqi@4267 | 643 | } |
minqi@4267 | 644 | case T_ARRAY: { |
minqi@4267 | 645 | oop value = mirror->obj_field_acquire(fd->offset()); |
minqi@4267 | 646 | if (value == NULL) { |
minqi@4267 | 647 | _out->print_cr("null"); |
minqi@4267 | 648 | } else { |
minqi@4267 | 649 | typeArrayOop ta = (typeArrayOop)value; |
minqi@4267 | 650 | _out->print("%d", ta->length()); |
minqi@4267 | 651 | if (value->is_objArray()) { |
minqi@4267 | 652 | objArrayOop oa = (objArrayOop)value; |
minqi@4267 | 653 | const char* klass_name = value->klass()->name()->as_quoted_ascii(); |
minqi@4267 | 654 | _out->print(" %s", klass_name); |
minqi@4267 | 655 | } |
minqi@4267 | 656 | _out->cr(); |
minqi@4267 | 657 | } |
minqi@4267 | 658 | break; |
minqi@4267 | 659 | } |
minqi@4267 | 660 | case T_OBJECT: { |
minqi@4267 | 661 | oop value = mirror->obj_field_acquire(fd->offset()); |
minqi@4267 | 662 | if (value == NULL) { |
minqi@4267 | 663 | _out->print_cr("null"); |
minqi@4267 | 664 | } else if (value->is_instance()) { |
minqi@4267 | 665 | if (value->is_a(SystemDictionary::String_klass())) { |
minqi@4267 | 666 | _out->print("\""); |
minqi@4267 | 667 | _out->print_raw(java_lang_String::as_quoted_ascii(value)); |
minqi@4267 | 668 | _out->print_cr("\""); |
minqi@4267 | 669 | } else { |
minqi@4267 | 670 | const char* klass_name = value->klass()->name()->as_quoted_ascii(); |
drchase@6680 | 671 | _out->print_cr("%s", klass_name); |
minqi@4267 | 672 | } |
minqi@4267 | 673 | } else { |
minqi@4267 | 674 | ShouldNotReachHere(); |
minqi@4267 | 675 | } |
minqi@4267 | 676 | break; |
minqi@4267 | 677 | } |
minqi@4267 | 678 | default: |
minqi@4267 | 679 | ShouldNotReachHere(); |
minqi@4267 | 680 | } |
minqi@4267 | 681 | } |
minqi@4267 | 682 | } |
minqi@4267 | 683 | }; |
minqi@4267 | 684 | |
minqi@4267 | 685 | |
minqi@4267 | 686 | void ciInstanceKlass::dump_replay_data(outputStream* out) { |
vlivanov@4531 | 687 | ResourceMark rm; |
vlivanov@4531 | 688 | |
minqi@4267 | 689 | InstanceKlass* ik = get_instanceKlass(); |
minqi@4267 | 690 | ConstantPool* cp = ik->constants(); |
minqi@4267 | 691 | |
minqi@4267 | 692 | // Try to record related loaded classes |
minqi@4267 | 693 | Klass* sub = ik->subklass(); |
minqi@4267 | 694 | while (sub != NULL) { |
minqi@4267 | 695 | if (sub->oop_is_instance()) { |
minqi@4267 | 696 | out->print_cr("instanceKlass %s", sub->name()->as_quoted_ascii()); |
minqi@4267 | 697 | } |
minqi@4267 | 698 | sub = sub->next_sibling(); |
minqi@4267 | 699 | } |
minqi@4267 | 700 | |
minqi@4267 | 701 | // Dump out the state of the constant pool tags. During replay the |
minqi@4267 | 702 | // tags will be validated for things which shouldn't change and |
minqi@4267 | 703 | // classes will be resolved if the tags indicate that they were |
minqi@4267 | 704 | // resolved at compile time. |
minqi@4267 | 705 | out->print("ciInstanceKlass %s %d %d %d", ik->name()->as_quoted_ascii(), |
minqi@4267 | 706 | is_linked(), is_initialized(), cp->length()); |
minqi@4267 | 707 | for (int index = 1; index < cp->length(); index++) { |
minqi@4267 | 708 | out->print(" %d", cp->tags()->at(index)); |
minqi@4267 | 709 | } |
minqi@4267 | 710 | out->cr(); |
minqi@4267 | 711 | if (is_initialized()) { |
minqi@4267 | 712 | // Dump out the static final fields in case the compilation relies |
minqi@4267 | 713 | // on their value for correct replay. |
minqi@4267 | 714 | StaticFinalFieldPrinter sffp(out, ik->name()->as_quoted_ascii()); |
minqi@4267 | 715 | ik->do_local_static_fields(&sffp); |
minqi@4267 | 716 | } |
minqi@4267 | 717 | } |