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