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