Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8271073: Improve testing with VM option VerifyArchivedFields
Reviewed-by: ccheung, minqi
  • Loading branch information
iklam committed Sep 16, 2021
1 parent bc48a0a commit b982904
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
29 changes: 18 additions & 11 deletions src/hotspot/share/cds/heapShared.cpp
Expand Up @@ -674,23 +674,28 @@ void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) {
}

static void verify_the_heap(Klass* k, const char* which) {
if (VerifyArchivedFields) {
if (VerifyArchivedFields > 0) {
ResourceMark rm;
log_info(cds, heap)("Verify heap %s initializing static field(s) in %s",
which, k->external_name());

VM_Verify verify_op;
VMThread::execute(&verify_op);

if (!FLAG_IS_DEFAULT(VerifyArchivedFields)) {
// If VerifyArchivedFields has a non-default value (e.g., specified on the command-line), do
// more expensive checks.
if (is_init_completed()) {
FlagSetting fs1(VerifyBeforeGC, true);
FlagSetting fs2(VerifyDuringGC, true);
FlagSetting fs3(VerifyAfterGC, true);
Universe::heap()->collect(GCCause::_java_lang_system_gc);
}
if (VerifyArchivedFields > 1 && is_init_completed()) {
// At this time, the oop->klass() of some archived objects in the heap may not
// have been loaded into the system dictionary yet. Nevertheless, oop->klass() should
// have enough information (object size, oop maps, etc) so that a GC can be safely
// performed.
//
// -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage
// to check for GC safety.
log_info(cds, heap)("Trigger GC %s initializing static field(s) in %s",
which, k->external_name());
FlagSetting fs1(VerifyBeforeGC, true);
FlagSetting fs2(VerifyDuringGC, true);
FlagSetting fs3(VerifyAfterGC, true);
Universe::heap()->collect(GCCause::_java_lang_system_gc);
}
}
}
Expand Down Expand Up @@ -1707,10 +1712,12 @@ class VerifyLoadedHeapEmbeddedPointers: public BasicOopIterateClosure {
};

void HeapShared::verify_loaded_heap() {
if (!VerifyArchivedFields || !is_loaded()) {
if (VerifyArchivedFields <= 0 || !is_loaded()) {
return;
}

log_info(cds, heap)("Verify all oops and pointers in loaded heap");

ResourceMark rm;
ResourceHashtable<uintptr_t, bool> table;
VerifyLoadedHeapEmbeddedPointers verifier(&table);
Expand Down
8 changes: 6 additions & 2 deletions src/hotspot/share/gc/shared/gc_globals.hpp
Expand Up @@ -523,8 +523,12 @@
product(bool, VerifyDuringGC, false, DIAGNOSTIC, \
"Verify memory system during GC (between phases)") \
\
product(bool, VerifyArchivedFields, trueInDebug, DIAGNOSTIC, \
"Verify memory when archived oop fields are loaded from CDS)") \
product(int, VerifyArchivedFields, 0, DIAGNOSTIC, \
"Verify memory when archived oop fields are loaded from CDS; " \
"0: No check; " \
"1: Basic verification with VM_Verify (no side effects); " \
"2: Detailed verification by forcing a GC (with side effects)") \
range(0, 2) \
\
product(ccstrlist, VerifyGCType, "", DIAGNOSTIC, \
"GC type(s) to verify when Verify*GC is enabled." \
Expand Down
2 changes: 2 additions & 0 deletions test/hotspot/jtreg/runtime/cds/appcds/TestCommon.java
Expand Up @@ -409,6 +409,8 @@ public static OutputAnalyzer runWithArchive(AppCDSOptions opts)
cmd.add(opts.appJar);
}

CDSTestUtils.addVerifyArchivedFields(cmd);

for (String s : opts.suffix) cmd.add(s);

if (RUN_WITH_JFR) {
Expand Down
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* 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
* @summary run with -XX:VerifyArchivedFields=2 for more expensive verification
* of the archived heap objects.
* @requires vm.cds.write.archived.java.heap
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
* @build Hello
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar Hello.jar Hello
* @run driver VerifyArchivedFields
*/

import jdk.test.lib.helpers.ClassFileInstaller;

public class VerifyArchivedFields {
// Note: -XX:VerifyArchivedFields=2 will force a GC every time when
// HeapShared::initialize_from_archived_subgraph(Klass* k, ...) is called. This ensures
// that it's safe to do a GC even when the oop->klass() of some archived heap objects
// are not yet loaded into the system dictionary.
public static void main(String[] args) throws Exception {
TestCommon.test(ClassFileInstaller.getJarPath("Hello.jar"),
TestCommon.list("Hello"),
"-XX:+UnlockDiagnosticVMOptions",
"-XX:VerifyArchivedFields=2",
"-Xlog:cds=debug",
"-Xlog:cds+heap=debug",
"-Xlog:gc*=debug",
"Hello");
}
}
7 changes: 7 additions & 0 deletions test/lib/jdk/test/lib/cds/CDSTestUtils.java
Expand Up @@ -399,6 +399,12 @@ public static OutputAnalyzer runWithArchive(String... cliPrefix)
.addPrefix(cliPrefix) );
}

// Enable basic verification (VerifyArchivedFields=1, no side effects) for all CDS
// tests to make sure the archived heap objects are mapped/loaded properly.
public static void addVerifyArchivedFields(ArrayList<String> cmd) {
cmd.add("-XX:+UnlockDiagnosticVMOptions");
cmd.add("-XX:VerifyArchivedFields=1");
}

// Execute JVM with CDS archive, specify CDSOptions
public static OutputAnalyzer runWithArchive(CDSOptions opts)
Expand All @@ -413,6 +419,7 @@ public static OutputAnalyzer runWithArchive(CDSOptions opts)
opts.archiveName = getDefaultArchiveName();
cmd.add("-XX:SharedArchiveFile=" + opts.archiveName);
}
addVerifyArchivedFields(cmd);

if (opts.useVersion)
cmd.add("-version");
Expand Down

1 comment on commit b982904

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.