Path: blob/master/src/hotspot/share/gc/shenandoah/shenandoahAsserts.cpp
40957 views
/*1* Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"2526#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"3334void 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.3738ShenandoahHeap* heap = ShenandoahHeap::heap();39if (!heap->is_in(loc)) return;4041ShenandoahHeapRegion* r = heap->heap_region_containing(loc);42if (r != NULL && r->is_committed()) {43address start = MAX2((address) r->bottom(), (address) loc - 32);44address end = MIN2((address) r->end(), (address) loc + 128);45if (start >= end) return;4647stringStream ss;48os::print_hex_dump(&ss, start, end, 4);49msg.append("\n");50msg.append("Raw heap memory:\n%s", ss.as_string());51}52}5354void ShenandoahAsserts::print_obj(ShenandoahMessageBuffer& msg, oop obj) {55ShenandoahHeap* heap = ShenandoahHeap::heap();56ShenandoahHeapRegion *r = heap->heap_region_containing(obj);5758ResourceMark rm;59stringStream ss;60r->print_on(&ss);6162stringStream mw_ss;63obj->mark().print_on(&mw_ss);6465ShenandoahMarkingContext* const ctx = heap->marking_context();6667msg.append(" " PTR_FORMAT " - klass " PTR_FORMAT " %s\n", p2i(obj), p2i(obj->klass()), obj->klass()->external_name());68msg.append(" %3s allocated after mark start\n", ctx->allocated_after_mark_start(obj) ? "" : "not");69msg.append(" %3s after update watermark\n", cast_from_oop<HeapWord*>(obj) >= r->get_update_watermark() ? "" : "not");70msg.append(" %3s marked strong\n", ctx->is_marked_strong(obj) ? "" : "not");71msg.append(" %3s marked weak\n", ctx->is_marked_weak(obj) ? "" : "not");72msg.append(" %3s in collection set\n", heap->in_collection_set(obj) ? "" : "not");73msg.append(" mark:%s\n", mw_ss.as_string());74msg.append(" region: %s", ss.as_string());75}7677void ShenandoahAsserts::print_non_obj(ShenandoahMessageBuffer& msg, void* loc) {78ShenandoahHeap* heap = ShenandoahHeap::heap();79if (heap->is_in(loc)) {80msg.append(" inside Java heap\n");81ShenandoahHeapRegion *r = heap->heap_region_containing(loc);82stringStream ss;83r->print_on(&ss);8485msg.append(" %3s in collection set\n", heap->in_collection_set_loc(loc) ? "" : "not");86msg.append(" region: %s", ss.as_string());87} else {88msg.append(" outside of Java heap\n");89stringStream ss;90os::print_location(&ss, (intptr_t) loc, false);91msg.append(" %s", ss.as_string());92}93}9495void ShenandoahAsserts::print_obj_safe(ShenandoahMessageBuffer& msg, void* loc) {96ShenandoahHeap* heap = ShenandoahHeap::heap();97msg.append(" " PTR_FORMAT " - safe print, no details\n", p2i(loc));98if (heap->is_in(loc)) {99ShenandoahHeapRegion* r = heap->heap_region_containing(loc);100if (r != NULL) {101stringStream ss;102r->print_on(&ss);103msg.append(" region: %s", ss.as_string());104print_raw_memory(msg, loc);105}106}107}108109void ShenandoahAsserts::print_failure(SafeLevel level, oop obj, void* interior_loc, oop loc,110const char* phase, const char* label,111const char* file, int line) {112ShenandoahHeap* heap = ShenandoahHeap::heap();113ResourceMark rm;114115bool loc_in_heap = (loc != NULL && heap->is_in(loc));116117ShenandoahMessageBuffer msg("%s; %s\n\n", phase, label);118119msg.append("Referenced from:\n");120if (interior_loc != NULL) {121msg.append(" interior location: " PTR_FORMAT "\n", p2i(interior_loc));122if (loc_in_heap) {123print_obj(msg, loc);124} else {125print_non_obj(msg, interior_loc);126}127} else {128msg.append(" no interior location recorded (probably a plain heap scan, or detached oop)\n");129}130msg.append("\n");131132msg.append("Object:\n");133if (level >= _safe_oop) {134print_obj(msg, obj);135} else {136print_obj_safe(msg, obj);137}138msg.append("\n");139140if (level >= _safe_oop) {141oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);142msg.append("Forwardee:\n");143if (obj != fwd) {144if (level >= _safe_oop_fwd) {145print_obj(msg, fwd);146} else {147print_obj_safe(msg, fwd);148}149} else {150msg.append(" (the object itself)");151}152msg.append("\n");153}154155if (level >= _safe_oop_fwd) {156oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);157oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);158if (fwd != fwd2) {159msg.append("Second forwardee:\n");160print_obj_safe(msg, fwd2);161msg.append("\n");162}163}164165report_vm_error(file, line, msg.buffer());166}167168void ShenandoahAsserts::assert_in_heap(void* interior_loc, oop obj, const char *file, int line) {169ShenandoahHeap* heap = ShenandoahHeap::heap();170171if (!heap->is_in(obj)) {172print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_in_heap failed",173"oop must point to a heap address",174file, line);175}176}177178void ShenandoahAsserts::assert_in_heap_or_null(void* interior_loc, oop obj, const char *file, int line) {179ShenandoahHeap* heap = ShenandoahHeap::heap();180181if (obj != NULL && !heap->is_in(obj)) {182print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_in_heap_or_null failed",183"oop must point to a heap address",184file, line);185}186}187188void ShenandoahAsserts::assert_correct(void* interior_loc, oop obj, const char* file, int line) {189ShenandoahHeap* heap = ShenandoahHeap::heap();190191// Step 1. Check that obj is correct.192// After this step, it is safe to call heap_region_containing().193if (!heap->is_in(obj)) {194print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_correct failed",195"oop must point to a heap address",196file, line);197}198199Klass* obj_klass = obj->klass_or_null();200if (obj_klass == NULL) {201print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_correct failed",202"Object klass pointer should not be NULL",203file,line);204}205206if (!Metaspace::contains(obj_klass)) {207print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_correct failed",208"Object klass pointer must go to metaspace",209file,line);210}211212oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);213214if (obj != fwd) {215// When Full GC moves the objects, we cannot trust fwdptrs. If we got here, it means something216// tries fwdptr manipulation when Full GC is running. The only exception is using the fwdptr217// that still points to the object itself.218if (heap->is_full_gc_move_in_progress()) {219print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_correct failed",220"Non-trivial forwarding pointer during Full GC moves, probable bug.",221file, line);222}223224// Step 2. Check that forwardee is correct225if (!heap->is_in(fwd)) {226print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_correct failed",227"Forwardee must point to a heap address",228file, line);229}230231if (obj_klass != fwd->klass()) {232print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_correct failed",233"Forwardee klass disagrees with object class",234file, line);235}236237// Step 3. Check that forwardee points to correct region238if (heap->heap_region_index_containing(fwd) == heap->heap_region_index_containing(obj)) {239print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_correct failed",240"Non-trivial forwardee should in another region",241file, line);242}243244// Step 4. Check for multiple forwardings245oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);246if (fwd != fwd2) {247print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_correct failed",248"Multiple forwardings",249file, line);250}251}252}253254void ShenandoahAsserts::assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line) {255assert_correct(interior_loc, obj, file, line);256257ShenandoahHeap* heap = ShenandoahHeap::heap();258ShenandoahHeapRegion* r = heap->heap_region_containing(obj);259if (!r->is_active()) {260print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_in_correct_region failed",261"Object must reside in active region",262file, line);263}264265size_t alloc_size = obj->size();266if (alloc_size > ShenandoahHeapRegion::humongous_threshold_words()) {267size_t idx = r->index();268size_t num_regions = ShenandoahHeapRegion::required_regions(alloc_size * HeapWordSize);269for (size_t i = idx; i < idx + num_regions; i++) {270ShenandoahHeapRegion* chain_reg = heap->get_region(i);271if (i == idx && !chain_reg->is_humongous_start()) {272print_failure(_safe_unknown, obj, interior_loc, NULL, "Shenandoah assert_in_correct_region failed",273"Object must reside in humongous start",274file, line);275}276if (i != idx && !chain_reg->is_humongous_continuation()) {277print_failure(_safe_oop, obj, interior_loc, NULL, "Shenandoah assert_in_correct_region failed",278"Humongous continuation should be of proper size",279file, line);280}281}282}283}284285void ShenandoahAsserts::assert_forwarded(void* interior_loc, oop obj, const char* file, int line) {286assert_correct(interior_loc, obj, file, line);287oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);288289if (obj == fwd) {290print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_forwarded failed",291"Object should be forwarded",292file, line);293}294}295296void ShenandoahAsserts::assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line) {297assert_correct(interior_loc, obj, file, line);298oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);299300if (obj != fwd) {301print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_not_forwarded failed",302"Object should not be forwarded",303file, line);304}305}306307void ShenandoahAsserts::assert_marked(void *interior_loc, oop obj, const char *file, int line) {308assert_correct(interior_loc, obj, file, line);309310ShenandoahHeap* heap = ShenandoahHeap::heap();311if (!heap->marking_context()->is_marked(obj)) {312print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_marked failed",313"Object should be marked",314file, line);315}316}317318void ShenandoahAsserts::assert_in_cset(void* interior_loc, oop obj, const char* file, int line) {319assert_correct(interior_loc, obj, file, line);320321ShenandoahHeap* heap = ShenandoahHeap::heap();322if (!heap->in_collection_set(obj)) {323print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_in_cset failed",324"Object should be in collection set",325file, line);326}327}328329void ShenandoahAsserts::assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line) {330assert_correct(interior_loc, obj, file, line);331332ShenandoahHeap* heap = ShenandoahHeap::heap();333if (heap->in_collection_set(obj)) {334print_failure(_safe_all, obj, interior_loc, NULL, "Shenandoah assert_not_in_cset failed",335"Object should not be in collection set",336file, line);337}338}339340void ShenandoahAsserts::assert_not_in_cset_loc(void* interior_loc, const char* file, int line) {341ShenandoahHeap* heap = ShenandoahHeap::heap();342if (heap->in_collection_set_loc(interior_loc)) {343print_failure(_safe_unknown, NULL, interior_loc, NULL, "Shenandoah assert_not_in_cset_loc failed",344"Interior location should not be in collection set",345file, line);346}347}348349void ShenandoahAsserts::print_rp_failure(const char *label, BoolObjectClosure* actual,350const char *file, int line) {351ShenandoahMessageBuffer msg("%s\n", label);352msg.append(" Actual: " PTR_FORMAT "\n", p2i(actual));353report_vm_error(file, line, msg.buffer());354}355356void ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line) {357if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {358return;359}360361if (lock->owned_by_self()) {362return;363}364365ShenandoahMessageBuffer msg("Must ba at a Shenandoah safepoint or held %s lock", lock->name());366report_vm_error(file, line, msg.buffer());367}368369void ShenandoahAsserts::assert_heaplocked(const char* file, int line) {370ShenandoahHeap* heap = ShenandoahHeap::heap();371372if (heap->lock()->owned_by_self()) {373return;374}375376ShenandoahMessageBuffer msg("Heap lock must be owned by current thread");377report_vm_error(file, line, msg.buffer());378}379380void ShenandoahAsserts::assert_not_heaplocked(const char* file, int line) {381ShenandoahHeap* heap = ShenandoahHeap::heap();382383if (!heap->lock()->owned_by_self()) {384return;385}386387ShenandoahMessageBuffer msg("Heap lock must not be owned by current thread");388report_vm_error(file, line, msg.buffer());389}390391void ShenandoahAsserts::assert_heaplocked_or_safepoint(const char* file, int line) {392ShenandoahHeap* heap = ShenandoahHeap::heap();393394if (heap->lock()->owned_by_self()) {395return;396}397398if (ShenandoahSafepoint::is_at_shenandoah_safepoint() && Thread::current()->is_VM_thread()) {399return;400}401402ShenandoahMessageBuffer msg("Heap lock must be owned by current thread, or be at safepoint");403report_vm_error(file, line, msg.buffer());404}405406407