1 /*
  2  * Copyright (c) 2018, 2020, Red Hat, Inc. 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  */
 24 
 25 
 26 #include "gc/shenandoah/shenandoahAsserts.hpp"
 27 #include "gc/shenandoah/shenandoahForwarding.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
 30 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 31 #include "gc/shenandoah/shenandoahUtils.hpp"
 32 #include "memory/resourceArea.hpp"
 33 
 34 void print_raw_memory(ShenandoahMessageBuffer &msg, void* loc) {
 35   // Be extra safe. Only access data that is guaranteed to be safe:
 36   // should be in heap, in known committed region, within that region.
 37 
 38   ShenandoahHeap* heap = ShenandoahHeap::heap();
 39   if (!heap->is_in_reserved(loc)) return;
 40 
 41   ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
 42   if (r != nullptr && r->is_committed()) {
 43     address start = MAX2((address) r->bottom(), (address) loc - 32);
 44     address end   = MIN2((address) r->end(),    (address) loc + 128);
 45     if (start >= end) return;
 46 
 47     stringStream ss;
 48     os::print_hex_dump(&ss, start, end, 4);
 49     msg.append("\n");
 50     msg.append("Raw heap memory:\n%s", ss.freeze());
 51   }
 52 }
 53 
 54 void ShenandoahAsserts::print_obj(ShenandoahMessageBuffer& msg, oop obj) {
 55   ShenandoahHeap* heap = ShenandoahHeap::heap();
 56   ShenandoahHeapRegion *r = heap->heap_region_containing(obj);
 57 
 58   ResourceMark rm;
 59   stringStream ss;
 60   r->print_on(&ss);
 61 
 62   stringStream mw_ss;
 63   obj->mark().print_on(&mw_ss);
 64 
 65   ShenandoahMarkingContext* const ctx = heap->marking_context();
 66 
 67   msg.append("  " PTR_FORMAT " - klass " PTR_FORMAT " %s\n", p2i(obj), p2i(obj->klass()), obj->klass()->external_name());
 68   msg.append("    %3s allocated after mark start\n", ctx->allocated_after_mark_start(obj) ? "" : "not");
 69   msg.append("    %3s after update watermark\n",     cast_from_oop<HeapWord*>(obj) >= r->get_update_watermark() ? "" : "not");
 70   msg.append("    %3s marked strong\n",              ctx->is_marked_strong(obj) ? "" : "not");
 71   msg.append("    %3s marked weak\n",                ctx->is_marked_weak(obj) ? "" : "not");
 72   msg.append("    %3s in collection set\n",          heap->in_collection_set(obj) ? "" : "not");
 73   if (heap->mode()->is_generational() && !obj->is_forwarded()) {
 74     msg.append("  age: %d\n", obj->age());
 75   }
 76   msg.append("  mark:%s\n", mw_ss.freeze());
 77   msg.append("  region: %s", ss.freeze());
 78 }
 79 
 80 void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) {
 81   ShenandoahHeap* heap = ShenandoahHeap::heap();
 82   if (heap->is_in_reserved(loc)) {
 83     msg.append("  inside Java heap\n");
 84     ShenandoahHeapRegion *r = heap->heap_region_containing(loc);
 85     stringStream ss;
 86     r->print_on(&ss);
 87 
 88     msg.append("    %3s in collection set\n",    heap->in_collection_set_loc(loc) ? "" : "not");
 89     msg.append("  region: %s", ss.freeze());
 90   } else {
 91     msg.append("  outside of Java heap\n");
 92     stringStream ss;
 93     os::print_location(&ss, (intptr_t) loc, false);
 94     msg.append("  %s", ss.freeze());
 95   }
 96 }
 97 
 98 void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) {
 99   ShenandoahHeap* heap = ShenandoahHeap::heap();
100   msg.append("  " PTR_FORMAT " - safe print, no details\n", p2i(loc));
101   if (heap->is_in_reserved(loc)) {
102     ShenandoahHeapRegion* r = heap->heap_region_containing(loc);
103     if (r != nullptr) {
104       stringStream ss;
105       r->print_on(&ss);
106       msg.append("  region: %s", ss.freeze());
107       print_raw_memory(msg, loc);
108     }
109   }
110 }
111 
112 void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_loc, oop loc,
113                                        const char* phase, const char* label,
114                                        const char* file, int line) {
115   ShenandoahHeap* heap = ShenandoahHeap::heap();
116   ResourceMark rm;
117 
118   bool loc_in_heap = (loc != nullptr && heap->is_in_reserved(loc));
119 
120   ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label);
121 
122   msg.append("Referenced from:\n");
123   if (interior_loc != nullptr) {
124     msg.append("  interior location: " PTR_FORMAT "\n", p2i(interior_loc));
125     if (loc_in_heap) {
126       print_obj(msg, loc);
127     } else {
128       print_non_obj(msg, interior_loc);
129     }
130   } else {
131     msg.append("  no interior location recorded (probably a plain heap scan, or detached oop)\n");
132   }
133   msg.append("\n");
134 
135   msg.append("Object:\n");
136   if (level >= _safe_oop) {
137     print_obj(msg, obj);
138   } else {
139     print_obj_safe(msg, obj);
140   }
141   msg.append("\n");
142 
143   if (level >= _safe_oop) {
144     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
145     msg.append("Forwardee:\n");
146     if (obj != fwd) {
147       if (level >= _safe_oop_fwd) {
148         print_obj(msg, fwd);
149       } else {
150         print_obj_safe(msg, fwd);
151       }
152     } else {
153       msg.append("  (the object itself)");
154     }
155     msg.append("\n");
156   }
157 
158   if (level >= _safe_oop_fwd) {
159     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
160     oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
161     if (fwd != fwd2) {
162       msg.append("Second forwardee:\n");
163       print_obj_safe(msg, fwd2);
164       msg.append("\n");
165     }
166   }
167 
168   report_vm_error(file, line, msg.buffer());
169 }
170 
171 void ShenandoahAsserts::assert_in_heap_bounds(void* interior_loc, oop obj, const char *file, int line) {
172   ShenandoahHeap* heap = ShenandoahHeap::heap();
173 
174   if (!heap->is_in_reserved(obj)) {
175     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds failed",
176                   "oop must be in heap bounds",
177                   file, line);
178   }
179 }
180 
181 void ShenandoahAsserts::assert_in_heap_bounds_or_null(void* interior_loc, oop obj, const char *file, int line) {
182   ShenandoahHeap* heap = ShenandoahHeap::heap();
183 
184   if (obj != nullptr && !heap->is_in_reserved(obj)) {
185     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_heap_bounds_or_null failed",
186                   "oop must be in heap bounds",
187                   file, line);
188   }
189 }
190 
191 void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char* file, int line) {
192   ShenandoahHeap* heap = ShenandoahHeap::heap();
193 
194   // Step 1. Check that obj is correct.
195   // After this step, it is safe to call heap_region_containing().
196   if (!heap->is_in_reserved(obj)) {
197     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
198                   "oop must be in heap bounds",
199                   file, line);
200   }
201 
202   Klass* obj_klass = ShenandoahForwarding::klass(obj);
203   if (obj_klass == nullptr) {
204     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
205                   "Object klass pointer should not be null",
206                   file,line);
207   }
208 
209   if (!Metaspace::contains(obj_klass)) {
210     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
211                   "Object klass pointer must go to metaspace",
212                   file,line);
213   }
214 
215   if (!heap->is_in(obj)) {
216     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
217                   "Object should be in active region area",
218                   file, line);
219   }
220 
221   oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
222 
223   if (obj != fwd) {
224     // When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something
225     // tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr
226     // that still points to the object itself.
227     if (heap->is_full_gc_move_in_progress()) {
228       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
229                     "Non-trivial forwarding pointer during Full GC moves, probable bug.",
230                     file, line);
231     }
232 
233     // Step 2. Check that forwardee is correct
234     if (!heap->is_in_reserved(fwd)) {
235       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
236                     "Forwardee must be in heap bounds",
237                     file, line);
238     }
239 
240     if (obj_klass != ShenandoahForwarding::klass(fwd)) {
241       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
242                     "Forwardee klass disagrees with object class",
243                     file, line);
244     }
245 
246     // Step 3. Check that forwardee points to correct region
247     if (!heap->is_in(fwd)) {
248       print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
249                     "Forwardee should be in active region area",
250                     file, line);
251     }
252 
253     if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) {
254       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
255                     "Non-trivial forwardee should be in another region",
256                     file, line);
257     }
258 
259     // Step 4. Check for multiple forwardings
260     oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
261     if (fwd != fwd2) {
262       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
263                     "Multiple forwardings",
264                     file, line);
265     }
266   }
267 
268   // Do additional checks for special objects: their fields can hold metadata as well.
269   // We want to check class loading/unloading did not corrupt them.
270 
271   if (Universe::is_fully_initialized() && (obj_klass == vmClasses::Class_klass())) {
272     Metadata* klass = obj->metadata_field(java_lang_Class::klass_offset());
273     if (klass != nullptr && !Metaspace::contains(klass)) {
274       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
275                     "Instance class mirror should point to Metaspace",
276                     file, line);
277     }
278 
279     Metadata* array_klass = obj->metadata_field(java_lang_Class::array_klass_offset());
280     if (array_klass != nullptr && !Metaspace::contains(array_klass)) {
281       print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_correct failed",
282                     "Array class mirror should point to Metaspace",
283                     file, line);
284     }
285   }
286 }
287 
288 void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) {
289   assert_correct(interior_loc, obj, file, line);
290 
291   ShenandoahHeap* heap = ShenandoahHeap::heap();
292   ShenandoahHeapRegion* r = heap->heap_region_containing(obj);
293   if (!r->is_active()) {
294     print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
295                   "Object must reside in active region",
296                   file, line);
297   }
298 
299   size_t alloc_size = obj->size();
300   HeapWord* obj_end = cast_from_oop<HeapWord*>(obj) + alloc_size;
301 
302   if (ShenandoahHeapRegion::requires_humongous(alloc_size)) {
303     size_t idx = r->index();
304     size_t end_idx = heap->heap_region_index_containing(obj_end - 1);
305     for (size_t i = idx; i < end_idx; i++) {
306       ShenandoahHeapRegion* chain_reg = heap->get_region(i);
307       if (i == idx && !chain_reg->is_humongous_start()) {
308         print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
309                       "Object must reside in humongous start",
310                       file, line);
311       }
312       if (i != idx && !chain_reg->is_humongous_continuation()) {
313         print_failure(_safe_oop, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
314                       "Humongous continuation should be of proper size",
315                       file, line);
316       }
317     }
318   } else {
319     if (obj_end > r->top()) {
320       print_failure(_safe_unknown, obj, interior_loc, nullptr, "Shenandoah assert_in_correct_region failed",
321                     "Object end should be within the active area of the region",
322                     file, line);
323     }
324   }
325 }
326 
327 void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) {
328   assert_correct(interior_loc, obj, file, line);
329   oop fwd =   ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
330 
331   if (obj == fwd) {
332     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_forwarded failed",
333                   "Object should be forwarded",
334                   file, line);
335   }
336 }
337 
338 void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) {
339   assert_correct(interior_loc, obj, file, line);
340   oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
341 
342   if (obj != fwd) {
343     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_forwarded failed",
344                   "Object should not be forwarded",
345                   file, line);
346   }
347 }
348 
349 void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) {
350   assert_correct(interior_loc, obj, file, line);
351 
352   ShenandoahHeap* heap = ShenandoahHeap::heap();
353   if (!heap->marking_context()->is_marked(obj)) {
354     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked failed",
355                   "Object should be marked",
356                   file, line);
357   }
358 }
359 
360 void ShenandoahAsserts::assert_marked_weak(void *interior_loc, oop obj, const char *file, int line) {
361   assert_correct(interior_loc, obj, file, line);
362 
363   ShenandoahHeap* heap = ShenandoahHeap::heap();
364   if (!heap->marking_context()->is_marked_weak(obj)) {
365     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_weak failed",
366                   "Object should be marked weakly",
367                   file, line);
368   }
369 }
370 
371 void ShenandoahAsserts::assert_marked_strong(void *interior_loc, oop obj, const char *file, int line) {
372   assert_correct(interior_loc, obj, file, line);
373 
374   ShenandoahHeap* heap = ShenandoahHeap::heap();
375   if (!heap->marking_context()->is_marked_strong(obj)) {
376     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_marked_strong failed",
377                   "Object should be marked strongly",
378                   file, line);
379   }
380 }
381 
382 void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) {
383   assert_correct(interior_loc, obj, file, line);
384 
385   ShenandoahHeap* heap = ShenandoahHeap::heap();
386   if (!heap->in_collection_set(obj)) {
387     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_in_cset failed",
388                   "Object should be in collection set",
389                   file, line);
390   }
391 }
392 
393 void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) {
394   assert_correct(interior_loc, obj, file, line);
395 
396   ShenandoahHeap* heap = ShenandoahHeap::heap();
397   if (heap->in_collection_set(obj)) {
398     print_failure(_safe_all, obj, interior_loc, nullptr, "Shenandoah assert_not_in_cset failed",
399                   "Object should not be in collection set",
400                   file, line);
401   }
402 }
403 
404 void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) {
405   ShenandoahHeap* heap = ShenandoahHeap::heap();
406   if (heap->in_collection_set_loc(interior_loc)) {
407     print_failure(_safe_unknown, nullptr, interior_loc, nullptr, "Shenandoah assert_not_in_cset_loc failed",
408                   "Interior location should not be in collection set",
409                   file, line);
410   }
411 }
412 
413 void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual,
414                                          const char *file, int line) {
415   ShenandoahMessageBuffer msg("%s\n", label);
416   msg.append(" Actual:                  " PTR_FORMAT "\n", p2i(actual));
417   report_vm_error(file, line, msg.buffer());
418 }
419 
420 void ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line) {
421   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
422     return;
423   }
424 
425   if (lock->owned_by_self()) {
426     return;
427   }
428 
429   ShenandoahMessageBuffer msg("Must be at a Shenandoah safepoint or held %s lock", lock->name());
430   report_vm_error(file, line, msg.buffer());
431 }
432 
433 void ShenandoahAsserts::assert_heaplocked(const char* file, int line) {
434   ShenandoahHeap* heap = ShenandoahHeap::heap();
435 
436   if (heap->lock()->owned_by_self()) {
437     return;
438   }
439 
440   ShenandoahMessageBuffer msg("Heap lock must be owned by current thread");
441   report_vm_error(file, line, msg.buffer());
442 }
443 
444 void ShenandoahAsserts::assert_not_heaplocked(const char* file, int line) {
445   ShenandoahHeap* heap = ShenandoahHeap::heap();
446 
447   if (!heap->lock()->owned_by_self()) {
448     return;
449   }
450 
451   ShenandoahMessageBuffer msg("Heap lock must not be owned by current thread");
452   report_vm_error(file, line, msg.buffer());
453 }
454 
455 void ShenandoahAsserts::assert_heaplocked_or_safepoint(const char* file, int line) {
456   ShenandoahHeap* heap = ShenandoahHeap::heap();
457 
458   if (heap->lock()->owned_by_self()) {
459     return;
460   }
461 
462   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
463     return;
464   }
465 
466   ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint");
467   report_vm_error(file, line, msg.buffer());
468 }
469 
470 void ShenandoahAsserts::assert_generational(const char* file, int line) {
471   if (ShenandoahHeap::heap()->mode()->is_generational()) {
472     return;
473   }
474 
475   ShenandoahMessageBuffer msg("Must be in generational mode");
476   report_vm_error(file, line, msg.buffer());
477 }
478 
479 void ShenandoahAsserts::assert_control_or_vm_thread_at_safepoint(bool at_safepoint, const char* file, int line) {
480   Thread* thr = Thread::current();
481   if (thr == ShenandoahHeap::heap()->control_thread()) {
482     return;
483   }
484   if (thr->is_VM_thread()) {
485     if (!at_safepoint) {
486       return;
487     } else if (SafepointSynchronize::is_at_safepoint()) {
488       return;
489     }
490   }
491 
492   ShenandoahMessageBuffer msg("Must be either control thread, or vm thread");
493   if (at_safepoint) {
494     msg.append(" at a safepoint");
495   }
496   report_vm_error(file, line, msg.buffer());
497 }
498 
499 void ShenandoahAsserts::assert_generations_reconciled(const char* file, int line) {
500   if (!SafepointSynchronize::is_at_safepoint()) {
501     return;
502   }
503 
504   ShenandoahHeap* heap = ShenandoahHeap::heap();
505   ShenandoahGeneration* ggen = heap->gc_generation();
506   ShenandoahGeneration* agen = heap->active_generation();
507   if (agen == ggen) {
508     return;
509   }
510 
511   ShenandoahMessageBuffer msg("Active(%d) & GC(%d) Generations aren't reconciled", agen->type(), ggen->type());
512   report_vm_error(file, line, msg.buffer());
513 }