Thu, 07 Apr 2011 09:53:20 -0700
7009266: G1: assert(obj->is_oop_or_null(true )) failed: Error
Summary: A referent object that is only weakly reachable at the start of concurrent marking but is re-attached to the strongly reachable object graph during marking may not be marked as live. This can cause the reference object to be processed prematurely and leave dangling pointers to the referent object. Implement a read barrier for the java.lang.ref.Reference::referent field by intrinsifying the Reference.get() method, and intercepting accesses though JNI, reflection, and Unsafe, so that when a non-null referent object is read it is also logged in an SATB buffer.
Reviewed-by: kvn, iveresov, never, tonyp, dholmes
1 /*
2 * Copyright (c) 1997, 2011, 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 *
23 */
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "gc_implementation/shared/markSweep.inline.hpp"
28 #include "gc_interface/collectedHeap.inline.hpp"
29 #include "memory/oopFactory.hpp"
30 #include "memory/permGen.hpp"
31 #include "memory/universe.inline.hpp"
32 #include "oops/constantPoolKlass.hpp"
33 #include "oops/constantPoolOop.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "oops/oop.inline2.hpp"
36 #include "oops/symbol.hpp"
37 #include "runtime/handles.inline.hpp"
38 #ifdef TARGET_OS_FAMILY_linux
39 # include "thread_linux.inline.hpp"
40 #endif
41 #ifdef TARGET_OS_FAMILY_solaris
42 # include "thread_solaris.inline.hpp"
43 #endif
44 #ifdef TARGET_OS_FAMILY_windows
45 # include "thread_windows.inline.hpp"
46 #endif
47 #ifndef SERIALGC
48 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
49 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
50 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
51 #include "memory/cardTableRS.hpp"
52 #include "oops/oop.pcgc.inline.hpp"
53 #endif
55 constantPoolOop constantPoolKlass::allocate(int length, bool is_conc_safe, TRAPS) {
56 int size = constantPoolOopDesc::object_size(length);
57 KlassHandle klass (THREAD, as_klassOop());
58 assert(klass()->is_oop(), "Can't be null, else handlizing of c below won't work");
59 constantPoolHandle pool;
60 {
61 constantPoolOop c =
62 (constantPoolOop)CollectedHeap::permanent_obj_allocate(klass, size, CHECK_NULL);
63 assert(c->klass_or_null() != NULL, "Handlizing below won't work");
64 pool = constantPoolHandle(THREAD, c);
65 }
67 pool->set_length(length);
68 pool->set_tags(NULL);
69 pool->set_cache(NULL);
70 pool->set_operands(NULL);
71 pool->set_pool_holder(NULL);
72 pool->set_flags(0);
73 // only set to non-zero if constant pool is merged by RedefineClasses
74 pool->set_orig_length(0);
75 // if constant pool may change during RedefineClasses, it is created
76 // unsafe for GC concurrent processing.
77 pool->set_is_conc_safe(is_conc_safe);
78 // all fields are initialized; needed for GC
80 // Note: because we may be in this "conc_unsafe" state when allocating
81 // t_oop below, which may in turn cause a GC, it is imperative that our
82 // size be correct, consistent and henceforth stable, at this stage.
83 assert(pool->is_oop() && pool->is_parsable(), "Else size() below is unreliable");
84 assert(size == pool->size(), "size() is wrong");
86 // initialize tag array
87 typeArrayOop t_oop = oopFactory::new_permanent_byteArray(length, CHECK_NULL);
88 typeArrayHandle tags (THREAD, t_oop);
89 for (int index = 0; index < length; index++) {
90 tags()->byte_at_put(index, JVM_CONSTANT_Invalid);
91 }
92 pool->set_tags(tags());
94 // Check that our size was stable at its old value.
95 assert(size == pool->size(), "size() changed");
96 return pool();
97 }
99 klassOop constantPoolKlass::create_klass(TRAPS) {
100 constantPoolKlass o;
101 KlassHandle h_this_klass(THREAD, Universe::klassKlassObj());
102 KlassHandle k = base_create_klass(h_this_klass, header_size(), o.vtbl_value(), CHECK_NULL);
103 // Make sure size calculation is right
104 assert(k()->size() == align_object_size(header_size()), "wrong size for object");
105 java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror
106 return k();
107 }
109 int constantPoolKlass::oop_size(oop obj) const {
110 assert(obj->is_constantPool(), "must be constantPool");
111 return constantPoolOop(obj)->object_size();
112 }
115 void constantPoolKlass::oop_follow_contents(oop obj) {
116 assert (obj->is_constantPool(), "obj must be constant pool");
117 constantPoolOop cp = (constantPoolOop) obj;
118 // Performance tweak: We skip iterating over the klass pointer since we
119 // know that Universe::constantPoolKlassObj never moves.
121 // If the tags array is null we are in the middle of allocating this constant pool
122 if (cp->tags() != NULL) {
123 // gc of constant pool contents
124 oop* base = (oop*)cp->base();
125 for (int i = 0; i < cp->length(); i++) {
126 if (cp->is_pointer_entry(i)) {
127 if (*base != NULL) MarkSweep::mark_and_push(base);
128 }
129 base++;
130 }
131 // gc of constant pool instance variables
132 MarkSweep::mark_and_push(cp->tags_addr());
133 MarkSweep::mark_and_push(cp->cache_addr());
134 MarkSweep::mark_and_push(cp->operands_addr());
135 MarkSweep::mark_and_push(cp->pool_holder_addr());
136 }
137 }
139 #ifndef SERIALGC
140 void constantPoolKlass::oop_follow_contents(ParCompactionManager* cm,
141 oop obj) {
142 assert (obj->is_constantPool(), "obj must be constant pool");
143 constantPoolOop cp = (constantPoolOop) obj;
144 // Performance tweak: We skip iterating over the klass pointer since we
145 // know that Universe::constantPoolKlassObj never moves.
147 // If the tags array is null we are in the middle of allocating this constant
148 // pool.
149 if (cp->tags() != NULL) {
150 // gc of constant pool contents
151 oop* base = (oop*)cp->base();
152 for (int i = 0; i < cp->length(); i++) {
153 if (cp->is_pointer_entry(i)) {
154 if (*base != NULL) PSParallelCompact::mark_and_push(cm, base);
155 }
156 base++;
157 }
158 // gc of constant pool instance variables
159 PSParallelCompact::mark_and_push(cm, cp->tags_addr());
160 PSParallelCompact::mark_and_push(cm, cp->cache_addr());
161 PSParallelCompact::mark_and_push(cm, cp->operands_addr());
162 PSParallelCompact::mark_and_push(cm, cp->pool_holder_addr());
163 }
164 }
165 #endif // SERIALGC
168 int constantPoolKlass::oop_adjust_pointers(oop obj) {
169 assert (obj->is_constantPool(), "obj must be constant pool");
170 constantPoolOop cp = (constantPoolOop) obj;
171 // Get size before changing pointers.
172 // Don't call size() or oop_size() since that is a virtual call.
173 int size = cp->object_size();
174 // Performance tweak: We skip iterating over the klass pointer since we
175 // know that Universe::constantPoolKlassObj never moves.
177 // If the tags array is null we are in the middle of allocating this constant
178 // pool.
179 if (cp->tags() != NULL) {
180 oop* base = (oop*)cp->base();
181 for (int i = 0; i< cp->length(); i++) {
182 if (cp->is_pointer_entry(i)) {
183 MarkSweep::adjust_pointer(base);
184 }
185 base++;
186 }
187 }
188 MarkSweep::adjust_pointer(cp->tags_addr());
189 MarkSweep::adjust_pointer(cp->cache_addr());
190 MarkSweep::adjust_pointer(cp->operands_addr());
191 MarkSweep::adjust_pointer(cp->pool_holder_addr());
192 return size;
193 }
196 int constantPoolKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
197 assert (obj->is_constantPool(), "obj must be constant pool");
198 // Performance tweak: We skip iterating over the klass pointer since we
199 // know that Universe::constantPoolKlassObj never moves.
200 constantPoolOop cp = (constantPoolOop) obj;
201 // Get size before changing pointers.
202 // Don't call size() or oop_size() since that is a virtual call.
203 int size = cp->object_size();
205 // If the tags array is null we are in the middle of allocating this constant
206 // pool.
207 if (cp->tags() != NULL) {
208 oop* base = (oop*)cp->base();
209 for (int i = 0; i < cp->length(); i++) {
210 if (cp->is_pointer_entry(i)) {
211 blk->do_oop(base);
212 }
213 base++;
214 }
215 }
216 blk->do_oop(cp->tags_addr());
217 blk->do_oop(cp->cache_addr());
218 blk->do_oop(cp->operands_addr());
219 blk->do_oop(cp->pool_holder_addr());
220 return size;
221 }
224 int constantPoolKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
225 assert (obj->is_constantPool(), "obj must be constant pool");
226 // Performance tweak: We skip iterating over the klass pointer since we
227 // know that Universe::constantPoolKlassObj never moves.
228 constantPoolOop cp = (constantPoolOop) obj;
229 // Get size before changing pointers.
230 // Don't call size() or oop_size() since that is a virtual call.
231 int size = cp->object_size();
233 // If the tags array is null we are in the middle of allocating this constant
234 // pool.
235 if (cp->tags() != NULL) {
236 oop* base = (oop*)cp->base();
237 for (int i = 0; i < cp->length(); i++) {
238 if (mr.contains(base)) {
239 if (cp->is_pointer_entry(i)) {
240 blk->do_oop(base);
241 }
242 }
243 base++;
244 }
245 }
246 oop* addr;
247 addr = cp->tags_addr();
248 if (mr.contains(addr)) blk->do_oop(addr);
249 addr = cp->cache_addr();
250 if (mr.contains(addr)) blk->do_oop(addr);
251 addr = cp->operands_addr();
252 if (mr.contains(addr)) blk->do_oop(addr);
253 addr = cp->pool_holder_addr();
254 if (mr.contains(addr)) blk->do_oop(addr);
255 return size;
256 }
258 bool constantPoolKlass::oop_is_conc_safe(oop obj) const {
259 assert(obj->is_constantPool(), "must be constantPool");
260 return constantPoolOop(obj)->is_conc_safe();
261 }
263 #ifndef SERIALGC
264 int constantPoolKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
265 assert (obj->is_constantPool(), "obj must be constant pool");
266 constantPoolOop cp = (constantPoolOop) obj;
268 // If the tags array is null we are in the middle of allocating this constant
269 // pool.
270 if (cp->tags() != NULL) {
271 oop* base = (oop*)cp->base();
272 for (int i = 0; i < cp->length(); ++i, ++base) {
273 if (cp->is_pointer_entry(i)) {
274 PSParallelCompact::adjust_pointer(base);
275 }
276 }
277 }
278 PSParallelCompact::adjust_pointer(cp->tags_addr());
279 PSParallelCompact::adjust_pointer(cp->cache_addr());
280 PSParallelCompact::adjust_pointer(cp->operands_addr());
281 PSParallelCompact::adjust_pointer(cp->pool_holder_addr());
282 return cp->object_size();
283 }
285 void constantPoolKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
286 assert(obj->is_constantPool(), "should be constant pool");
287 constantPoolOop cp = (constantPoolOop) obj;
288 if (cp->tags() != NULL &&
289 (!JavaObjectsInPerm || (AnonymousClasses && cp->has_pseudo_string()))) {
290 for (int i = 1; i < cp->length(); ++i) {
291 if (cp->tag_at(i).is_string()) {
292 oop* base = cp->obj_at_addr_raw(i);
293 if (PSScavenge::should_scavenge(base)) {
294 pm->claim_or_forward_depth(base);
295 }
296 }
297 }
298 }
299 }
300 #endif // SERIALGC
302 // Printing
304 void constantPoolKlass::oop_print_on(oop obj, outputStream* st) {
305 EXCEPTION_MARK;
306 oop anObj;
307 assert(obj->is_constantPool(), "must be constantPool");
308 Klass::oop_print_on(obj, st);
309 constantPoolOop cp = constantPoolOop(obj);
310 if (cp->flags() != 0) {
311 st->print(" - flags: 0x%x", cp->flags());
312 if (cp->has_pseudo_string()) st->print(" has_pseudo_string");
313 if (cp->has_invokedynamic()) st->print(" has_invokedynamic");
314 st->cr();
315 }
316 st->print_cr(" - cache: " INTPTR_FORMAT, cp->cache());
318 for (int index = 1; index < cp->length(); index++) { // Index 0 is unused
319 st->print(" - %3d : ", index);
320 cp->tag_at(index).print_on(st);
321 st->print(" : ");
322 switch (cp->tag_at(index).value()) {
323 case JVM_CONSTANT_Class :
324 { anObj = cp->klass_at(index, CATCH);
325 anObj->print_value_on(st);
326 st->print(" {0x%lx}", (address)anObj);
327 }
328 break;
329 case JVM_CONSTANT_Fieldref :
330 case JVM_CONSTANT_Methodref :
331 case JVM_CONSTANT_InterfaceMethodref :
332 st->print("klass_index=%d", cp->uncached_klass_ref_index_at(index));
333 st->print(" name_and_type_index=%d", cp->uncached_name_and_type_ref_index_at(index));
334 break;
335 case JVM_CONSTANT_UnresolvedString :
336 case JVM_CONSTANT_String :
337 if (cp->is_pseudo_string_at(index)) {
338 anObj = cp->pseudo_string_at(index);
339 } else {
340 anObj = cp->string_at(index, CATCH);
341 }
342 anObj->print_value_on(st);
343 st->print(" {0x%lx}", (address)anObj);
344 break;
345 case JVM_CONSTANT_Integer :
346 st->print("%d", cp->int_at(index));
347 break;
348 case JVM_CONSTANT_Float :
349 st->print("%f", cp->float_at(index));
350 break;
351 case JVM_CONSTANT_Long :
352 st->print_jlong(cp->long_at(index));
353 index++; // Skip entry following eigth-byte constant
354 break;
355 case JVM_CONSTANT_Double :
356 st->print("%lf", cp->double_at(index));
357 index++; // Skip entry following eigth-byte constant
358 break;
359 case JVM_CONSTANT_NameAndType :
360 st->print("name_index=%d", cp->name_ref_index_at(index));
361 st->print(" signature_index=%d", cp->signature_ref_index_at(index));
362 break;
363 case JVM_CONSTANT_Utf8 :
364 cp->symbol_at(index)->print_value_on(st);
365 break;
366 case JVM_CONSTANT_UnresolvedClass : // fall-through
367 case JVM_CONSTANT_UnresolvedClassInError: {
368 // unresolved_klass_at requires lock or safe world.
369 CPSlot entry = cp->slot_at(index);
370 if (entry.is_oop()) {
371 entry.get_oop()->print_value_on(st);
372 } else {
373 entry.get_symbol()->print_value_on(st);
374 }
375 }
376 break;
377 case JVM_CONSTANT_MethodHandle :
378 st->print("ref_kind=%d", cp->method_handle_ref_kind_at(index));
379 st->print(" ref_index=%d", cp->method_handle_index_at(index));
380 break;
381 case JVM_CONSTANT_MethodType :
382 st->print("signature_index=%d", cp->method_type_index_at(index));
383 break;
384 case JVM_CONSTANT_InvokeDynamicTrans :
385 case JVM_CONSTANT_InvokeDynamic :
386 {
387 st->print("bootstrap_method_index=%d", cp->invoke_dynamic_bootstrap_method_ref_index_at(index));
388 st->print(" name_and_type_index=%d", cp->invoke_dynamic_name_and_type_ref_index_at(index));
389 int argc = cp->invoke_dynamic_argument_count_at(index);
390 if (argc > 0) {
391 for (int arg_i = 0; arg_i < argc; arg_i++) {
392 int arg = cp->invoke_dynamic_argument_index_at(index, arg_i);
393 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
394 }
395 st->print("}");
396 }
397 }
398 break;
399 default:
400 ShouldNotReachHere();
401 break;
402 }
403 st->cr();
404 }
405 st->cr();
406 }
408 void constantPoolKlass::oop_print_value_on(oop obj, outputStream* st) {
409 assert(obj->is_constantPool(), "must be constantPool");
410 constantPoolOop cp = constantPoolOop(obj);
411 st->print("constant pool [%d]", cp->length());
412 if (cp->has_pseudo_string()) st->print("/pseudo_string");
413 if (cp->has_invokedynamic()) st->print("/invokedynamic");
414 if (cp->operands() != NULL) st->print("/operands[%d]", cp->operands()->length());
415 cp->print_address_on(st);
416 st->print(" for ");
417 cp->pool_holder()->print_value_on(st);
418 if (cp->cache() != NULL) {
419 st->print(" cache=" PTR_FORMAT, cp->cache());
420 }
421 }
423 const char* constantPoolKlass::internal_name() const {
424 return "{constant pool}";
425 }
427 // Verification
429 void constantPoolKlass::oop_verify_on(oop obj, outputStream* st) {
430 Klass::oop_verify_on(obj, st);
431 guarantee(obj->is_constantPool(), "object must be constant pool");
432 constantPoolOop cp = constantPoolOop(obj);
433 guarantee(cp->is_perm(), "should be in permspace");
434 if (!cp->partially_loaded()) {
435 for (int i = 0; i< cp->length(); i++) {
436 CPSlot entry = cp->slot_at(i);
437 if (cp->tag_at(i).is_klass()) {
438 if (entry.is_oop()) {
439 guarantee(entry.get_oop()->is_perm(), "should be in permspace");
440 guarantee(entry.get_oop()->is_klass(), "should be klass");
441 }
442 }
443 if (cp->tag_at(i).is_unresolved_klass()) {
444 if (entry.is_oop()) {
445 guarantee(entry.get_oop()->is_perm(), "should be in permspace");
446 guarantee(entry.get_oop()->is_klass(), "should be klass");
447 }
448 }
449 if (cp->tag_at(i).is_symbol()) {
450 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
451 }
452 if (cp->tag_at(i).is_unresolved_string()) {
453 if (entry.is_oop()) {
454 guarantee(entry.get_oop()->is_perm(), "should be in permspace");
455 guarantee(entry.get_oop()->is_instance(), "should be instance");
456 }
457 else {
458 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
459 }
460 }
461 if (cp->tag_at(i).is_string()) {
462 if (!cp->has_pseudo_string()) {
463 if (entry.is_oop()) {
464 guarantee(!JavaObjectsInPerm || entry.get_oop()->is_perm(),
465 "should be in permspace");
466 guarantee(entry.get_oop()->is_instance(), "should be instance");
467 }
468 } else {
469 // can be non-perm, can be non-instance (array)
470 }
471 }
472 // FIXME: verify JSR 292 tags JVM_CONSTANT_MethodHandle, etc.
473 }
474 guarantee(cp->tags()->is_perm(), "should be in permspace");
475 guarantee(cp->tags()->is_typeArray(), "should be type array");
476 if (cp->cache() != NULL) {
477 // Note: cache() can be NULL before a class is completely setup or
478 // in temporary constant pools used during constant pool merging
479 guarantee(cp->cache()->is_perm(), "should be in permspace");
480 guarantee(cp->cache()->is_constantPoolCache(), "should be constant pool cache");
481 }
482 if (cp->operands() != NULL) {
483 guarantee(cp->operands()->is_perm(), "should be in permspace");
484 guarantee(cp->operands()->is_typeArray(), "should be type array");
485 }
486 if (cp->pool_holder() != NULL) {
487 // Note: pool_holder() can be NULL in temporary constant pools
488 // used during constant pool merging
489 guarantee(cp->pool_holder()->is_perm(), "should be in permspace");
490 guarantee(cp->pool_holder()->is_klass(), "should be klass");
491 }
492 }
493 }
495 bool constantPoolKlass::oop_partially_loaded(oop obj) const {
496 assert(obj->is_constantPool(), "object must be constant pool");
497 constantPoolOop cp = constantPoolOop(obj);
498 return cp->tags() == NULL || cp->pool_holder() == (klassOop) cp; // Check whether pool holder points to self
499 }
502 void constantPoolKlass::oop_set_partially_loaded(oop obj) {
503 assert(obj->is_constantPool(), "object must be constant pool");
504 constantPoolOop cp = constantPoolOop(obj);
505 assert(cp->pool_holder() == NULL, "just checking");
506 cp->set_pool_holder((klassOop) cp); // Temporarily set pool holder to point to self
507 }
509 #ifndef PRODUCT
510 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
511 void constantPoolKlass::preload_and_initialize_all_classes(oop obj, TRAPS) {
512 guarantee(obj->is_constantPool(), "object must be constant pool");
513 constantPoolHandle cp(THREAD, (constantPoolOop)obj);
514 guarantee(!cp->partially_loaded(), "must be fully loaded");
516 for (int i = 0; i< cp->length(); i++) {
517 if (cp->tag_at(i).is_unresolved_klass()) {
518 // This will force loading of the class
519 klassOop klass = cp->klass_at(i, CHECK);
520 if (klass->is_instance()) {
521 // Force initialization of class
522 instanceKlass::cast(klass)->initialize(CHECK);
523 }
524 }
525 }
526 }
528 #endif