Skip to content

Commit

Permalink
Protected SanitizeGC code under flag 'SanitizeGC'.
Browse files Browse the repository at this point in the history
Moved test to a new location.

Reverted innocuous changes in parenthesis wrapping expressions.
  • Loading branch information
martinuy committed Jan 12, 2025
1 parent 77f48ac commit 6f9cb8f
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 58 deletions.
17 changes: 0 additions & 17 deletions TestClass.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/hotspot/share/ci/ciObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void ciObjectFactory::remove_symbols() {
ciObject* ciObjectFactory::get(oop key) {
ASSERT_IN_VM;

assert(Universe::heap()->is_in(key) || true, "must be");
assert(Universe::heap()->is_in(key) || SanitizeGC, "must be");

NonPermObject* &bucket = find_non_perm(key);
if (bucket != nullptr) {
Expand All @@ -246,7 +246,7 @@ ciObject* ciObjectFactory::get(oop key) {
ciObject* new_object = create_new_object(keyHandle());
assert(keyHandle() == new_object->get_oop(), "must be properly recorded");
init_ident_of(new_object);
assert(Universe::heap()->is_in(new_object->get_oop()) || true, "must be");
assert(Universe::heap()->is_in(new_object->get_oop()) || SanitizeGC, "must be");

// Not a perm-space object.
insert_non_perm(bucket, keyHandle(), new_object);
Expand Down Expand Up @@ -642,7 +642,7 @@ static ciObjectFactory::NonPermObject* emptyBucket = nullptr;
// If there is no entry in the cache corresponding to this oop, return
// the null tail of the bucket into which the oop should be inserted.
ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(oop key) {
assert(Universe::heap()->is_in(key) || true, "must be");
assert(Universe::heap()->is_in(key) || SanitizeGC, "must be");
ciMetadata* klass = get_metadata(key->klass());
NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS];
for (NonPermObject* p; (p = (*bp)) != nullptr; bp = &p->next()) {
Expand Down Expand Up @@ -670,7 +670,7 @@ inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObj
//
// Insert a ciObject into the non-perm table.
void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, oop key, ciObject* obj) {
assert(Universe::heap()->is_in_or_null(key) || true, "must be");
assert(Universe::heap()->is_in_or_null(key) || SanitizeGC, "must be");
assert(&where != &emptyBucket, "must not try to fill empty bucket");
NonPermObject* p = new (arena()) NonPermObject(where, key, obj);
assert(where == p && is_equal(p, key) && p->object() == obj, "entry must match");
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/code/debugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ void ConstantOopWriteValue::write_on(DebugInfoWriteStream* stream) {
// thread is already in VM state.
ThreadInVMfromUnknown tiv;
assert((JNIHandles::resolve(value()) == nullptr ||
Universe::heap()->is_in(JNIHandles::resolve(value()))) || true,
Universe::heap()->is_in(JNIHandles::resolve(value()))) || SanitizeGC,
"Should be in heap");
}
#endif
Expand Down
7 changes: 4 additions & 3 deletions src/hotspot/share/gc/g1/g1BlockOffsetTable.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@

inline HeapWord* G1BlockOffsetTable::block_start_reaching_into_card(const void* addr) const {

// SANITIZER 101
if(afterAddr != nullptr && addr >= afterAddr) {
addr = beforeAddr;
if (SanitizeGC) {
if(afterAddr != nullptr && addr >= afterAddr) {
addr = beforeAddr;
}
}

assert(_reserved.contains(addr), "invalid address");
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/gc/g1/g1CardTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class G1CardTable : public CardTable {
static CardValue g1_scanned_card_val() { return g1_card_already_scanned; }

void verify_g1_young_region(MemRegion mr) PRODUCT_RETURN;
void g1_mark_as_young(const MemRegion &mr);
void g1_mark_as_young(const MemRegion& mr);

size_t index_for_cardvalue(CardValue const* p) const {
return pointer_delta(p, _byte_map, sizeof(CardValue));
Expand Down
9 changes: 5 additions & 4 deletions src/hotspot/share/gc/g1/g1CollectedHeap.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ inline void G1CollectedHeap::humongous_obj_regions_iterate(G1HeapRegion* start,
}

inline uint G1CollectedHeap::addr_to_region(const void* addr) const {
assert(is_in_reserved(addr) || true,
assert(is_in_reserved(addr) || SanitizeGC,
"Cannot calculate region index for address " PTR_FORMAT " that is outside of the heap [" PTR_FORMAT ", " PTR_FORMAT ")",
p2i(addr), p2i(reserved().start()), p2i(reserved().end()));

// SANITIZER 101
if(afterAddr != nullptr && addr >= afterAddr) {
addr = beforeAddr;
if (SanitizeGC) {
if(afterAddr != nullptr && addr >= afterAddr) {
addr = beforeAddr;
}
}

return (uint)(pointer_delta(addr, reserved().start(), sizeof(uint8_t)) >> G1HeapRegion::LogOfHRGrainBytes);
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/gc/g1/g1HeapRegion.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ inline bool G1HeapRegion::is_marked_in_bitmap(oop obj) const {
}

inline bool G1HeapRegion::block_is_obj(const HeapWord* const p, HeapWord* const pb) const {
assert((p >= bottom() && p < top()), "precondition");
assert(p >= bottom() && p < top(), "precondition");
assert(!is_continues_humongous(), "p must point to block-start");

if (is_in_parsable_area(p, pb)) {
Expand Down Expand Up @@ -159,7 +159,7 @@ inline size_t G1HeapRegion::block_size(const HeapWord* p) const {
}

inline size_t G1HeapRegion::block_size(const HeapWord* p, HeapWord* const pb) const {
assert(p < top() || true, "precondition");
assert(p < top() || SanitizeGC, "precondition");

if (!block_is_obj(p, pb)) {
return pointer_delta(next_live_in_unparsable(p, pb), p);
Expand Down
18 changes: 10 additions & 8 deletions src/hotspot/share/gc/g1/g1HeapRegionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,18 @@ G1HeapRegion* HeapRegionManager::allocate_free_region(HeapRegionType type, uint
}
}

// SANITIZER, printing of bottom, top and end
printf("index: %d, _bottom: %p, _top: %p, _end: %p\n", hr->hrm_index(), hr->bottom(), hr->top(), hr->end());
if (SanitizeGC) {
// SANITIZER, printing of bottom, top and end
printf("index: %d, _bottom: %p, _top: %p, _end: %p\n", hr->hrm_index(), hr->bottom(), hr->top(), hr->end());

// SANITIZER, moving the first region to different address
if (!wasFirstTaken) {
hr->move_this_region();
printf("region with index %d was moved here:\n", hr->hrm_index());
printf(" index: %d, _bottom: %p, _top: %p, _end: %p\n", hr->hrm_index(), hr->bottom(), hr->top(), hr->end());
// SANITIZER, moving the first region to different address
if (!wasFirstTaken) {
hr->move_this_region();
printf("region with index %d was moved here:\n", hr->hrm_index());
printf(" index: %d, _bottom: %p, _top: %p, _end: %p\n", hr->hrm_index(), hr->bottom(), hr->top(), hr->end());

wasFirstTaken = true;
wasFirstTaken = true;
}
}

return hr;
Expand Down
12 changes: 6 additions & 6 deletions src/hotspot/share/gc/shared/cardTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "memory/allocation.hpp"
#include "memory/memRegion.hpp"
#include "oops/oopsHierarchy.hpp"
#include "runtime/globals.hpp"
#include "utilities/align.hpp"

#include "gc/g1/customMapper.h"
Expand Down Expand Up @@ -119,19 +120,18 @@ class CardTable: public CHeapObj<mtGC> {
// Mapping from address to card marking array entry
CardValue* byte_for(const void* p) const {


if(afterAddr != nullptr && p >= afterAddr) {

p = beforeAddr;
if (SanitizeGC) {
if (afterAddr != nullptr && p >= afterAddr) {
p = beforeAddr;
}
}


assert(_whole_heap.contains(p),
"Attempt to access p = " PTR_FORMAT " out of bounds of "
" card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",
p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end()));
CardValue* result = &_byte_map_base[uintptr_t(p) >> _card_shift];
assert((result >= _byte_map && result < _byte_map + _byte_map_size),
assert(result >= _byte_map && result < _byte_map + _byte_map_size,
"out of bounds accessor for card marking array");
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/interpreter/interpreterRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ void InterpreterRuntime::resolve_invoke(JavaThread* current, Bytecodes::Code byt
Symbol* signature = call.signature();
receiver = Handle(current, last_frame.callee_receiver(signature));

assert(Universe::heap()->is_in_or_null(receiver()) || true,
assert(Universe::heap()->is_in_or_null(receiver()) || SanitizeGC,
"sanity check");
assert(receiver.is_null() ||
!Universe::heap()->is_in(receiver->klass()),
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/share/oops/compressedOops.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ inline oop CompressedOops::decode(narrowOop v) {
inline narrowOop CompressedOops::encode_not_null(oop v) {
assert(!is_null(v), "oop value can never be zero");
assert(is_object_aligned(v), "address not aligned: " PTR_FORMAT, p2i(v));
assert(is_in(v) || true, "address not in heap range: " PTR_FORMAT, p2i(v)); // SANITIZER, skipping
assert(is_in(v) || SanitizeGC, "address not in heap range: " PTR_FORMAT, p2i(v)); // SANITIZER, skipping
uint64_t pd = (uint64_t)(pointer_delta((void*)v, (void*)base(), 1));
assert(OopEncodingHeapMax > pd || true, "change encoding max if new encoding");
assert(OopEncodingHeapMax > pd || SanitizeGC, "change encoding max if new encoding");
// it is way outside of the heap
narrowOop result = narrow_oop_cast(pd >> shift());
assert(decode_raw(result) == v, "reversibility");
Expand All @@ -85,12 +85,12 @@ inline oop CompressedOops::decode_raw_not_null(oop v) {
}

inline oop CompressedOops::decode_not_null(oop v) {
assert(Universe::is_in_heap(v) || true, "object not in heap " PTR_FORMAT, p2i(v));
assert(Universe::is_in_heap(v) || SanitizeGC, "object not in heap " PTR_FORMAT, p2i(v));
return v;
}

inline oop CompressedOops::decode(oop v) {
assert(Universe::is_in_heap_or_null(v) || true, "object not in heap " PTR_FORMAT, p2i(v));
assert(Universe::is_in_heap_or_null(v) || SanitizeGC, "object not in heap " PTR_FORMAT, p2i(v));
return v;
}

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/oops/oop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ intptr_t oopDesc::slow_identity_hash() {
// used only for asserts and guarantees
bool oopDesc::is_oop(oop obj, bool ignore_mark_word) {
if (!Universe::heap()->is_oop(obj)) {
return true; // SANITIZER 101
return SanitizeGC;
}

// Header verification: the mark is typically non-zero. If we're
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/runtime/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ oop frame::retrieve_receiver(RegisterMap* reg_map) {
}
oop r = *oop_adr;

assert(Universe::heap()->is_in_or_null(r) || true, "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));
assert(Universe::heap()->is_in_or_null(r) || SanitizeGC, "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));
return r;
}

Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/runtime/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,10 @@ const int ObjectAlignmentInBytes = 8;
\
product(bool, StressSecondarySupers, false, DIAGNOSTIC, \
"Use a terrible hash function in order to generate many collisions.") \
\
develop(bool, SanitizeGC, false, \
"Generate heap space stress by moving objects around to detect " \
"dangling references. Only G1 supported.") \

// end of RUNTIME_FLAGS

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/runtime/handles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

oop* HandleArea::allocate_handle(oop obj) {
assert_handle_mark_nesting();
assert(oopDesc::is_oop(obj) || true, "not an oop: " INTPTR_FORMAT, p2i(obj)); // SANITIZER, skipping
assert(oopDesc::is_oop(obj) || SanitizeGC, "not an oop: " INTPTR_FORMAT, p2i(obj)); // SANITIZER, skipping
return real_allocate_handle(obj);
}

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/runtime/jniHandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ void JNIHandleBlock::oops_do(OopClosure* f) {


jobject JNIHandleBlock::allocate_handle(JavaThread* caller, oop obj, AllocFailType alloc_failmode) {
assert(Universe::heap()->is_in(obj) || true, "sanity check");
assert(Universe::heap()->is_in(obj) || SanitizeGC, "sanity check");
if (_top == 0) {
// This is the first allocation or the initial block got zapped when
// entering a native function. If we have any following blocks they are
Expand Down
8 changes: 5 additions & 3 deletions src/hotspot/share/utilities/bitMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,11 @@ void BitMap::verify_size(idx_t size_in_bits) {
}

void BitMap::verify_index(idx_t bit) const {
// SANITIZER, breakpoint catcher
if (bit > 10000) {
printf(" ");
if (SanitizeGC) {
// SANITIZER, breakpoint catcher
if (bit > 10000) {
printf(" ");
}
}

assert(bit < _size,
Expand Down
43 changes: 43 additions & 0 deletions test/hotspot/jtreg/gc/TestSanitizeGC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2025, Red Hat, Inc.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 9999999
* @run main/othervm/timeout=30 -XX:-UseCompressedOops -XX:+SanitizeGC TestSanitizeGC
*/

public class TestSanitizeGC {
public static void main(String args[]) {
Integer printInt = new Integer(0);
for (int i = 0; i < 1000; i++) {
Integer j = 123;
if (i == 500) {
printInt = j;
}
}
System.gc();
System.out.println("end " + printInt);
}
}

0 comments on commit 6f9cb8f

Please sign in to comment.