Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8278602: CDS dynamic dump may access unloaded classes
Reviewed-by: coleenp, ccheung
- Loading branch information
Showing
8 changed files
with
265 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#ifndef SHARED_CDS_DUMPTIMESHAREDCLASSINFO_INLINE_HPP | ||
#define SHARED_CDS_DUMPTIMESHAREDCLASSINFO_INLINE_HPP | ||
|
||
#include "cds/dumpTimeClassInfo.hpp" | ||
#include "classfile/systemDictionaryShared.hpp" | ||
#include "classfile/classLoaderData.inline.hpp" | ||
#include "oops/instanceKlass.hpp" | ||
#include "oops/klass.inline.hpp" | ||
#include "runtime/safepoint.hpp" | ||
|
||
#if INCLUDE_CDS | ||
|
||
// For safety, only iterate over a class if it loader is alive. | ||
// IterationHelper and DumpTimeSharedClassTable::iterate | ||
// must be used only inside a safepoint, where the value of | ||
// k->is_loader_alive() will not change. | ||
template<class ITER> | ||
class DumpTimeSharedClassTable::IterationHelper { | ||
ITER* _iter; | ||
public: | ||
IterationHelper(ITER* iter) { | ||
_iter = iter; | ||
} | ||
bool do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { | ||
assert(SafepointSynchronize::is_at_safepoint(), "invariant"); | ||
assert_lock_strong(DumpTimeTable_lock); | ||
if (k->is_loader_alive()) { | ||
bool result = _iter->do_entry(k, info); | ||
assert(k->is_loader_alive(), "must not change"); | ||
return result; | ||
} else { | ||
if (!SystemDictionaryShared::is_excluded_class(k)) { | ||
SystemDictionaryShared::warn_excluded(k, "Class loader not alive"); | ||
SystemDictionaryShared::set_excluded_locked(k); | ||
} | ||
return true; | ||
} | ||
} | ||
}; | ||
|
||
template<class ITER> | ||
void DumpTimeSharedClassTable::iterate(ITER* iter) const { | ||
IterationHelper<ITER> helper(iter); | ||
DumpTimeSharedClassTableBaseType::iterate(&helper); | ||
} | ||
|
||
#endif // INCLUDE_CDS | ||
|
||
#endif // SHARED_CDS_DUMPTIMESHAREDCLASSINFO_INLINE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/LotsUnloadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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 | ||
* @bug 8278602 | ||
* @summary Lots of classes being unloaded while we try to dump a dynamic archive | ||
* @requires vm.cds | ||
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds | ||
* /test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes | ||
* @build sun.hotspot.WhiteBox | ||
* @build LotsUnloadApp | ||
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox | ||
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar LotsUnloadApp.jar LotsUnloadApp DefinedAsHiddenKlass | ||
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. LotsUnloadTest | ||
*/ | ||
|
||
// Note: for https://bugs.openjdk.java.net/browse/JDK-8278602, this test case does NOT | ||
// reliably reproduce the problem. Reproduction requires patching ZGC. Please see | ||
// the bug report for instructions. | ||
// | ||
// This test case is included so that it may find a similar bug under stress conditions | ||
// in the CI runs. | ||
import jdk.test.lib.helpers.ClassFileInstaller; | ||
|
||
public class LotsUnloadTest extends DynamicArchiveTestBase { | ||
public static void main(String[] args) throws Exception { | ||
runTest(LotsUnloadTest::test); | ||
} | ||
|
||
static void test() throws Exception { | ||
String topArchiveName = getNewArchiveName(); | ||
String appJar = ClassFileInstaller.getJarPath("LotsUnloadApp.jar"); | ||
String mainClass = "LotsUnloadApp"; | ||
String logging; | ||
|
||
if (Boolean.getBoolean("verbose.LotsUnloadTest")) { | ||
// class+unload logs may change GC timing and cause the bug to be | ||
// less reproducible. | ||
logging = "-Xlog:cds,class+unload"; | ||
} else { | ||
logging = "-Xlog:cds"; | ||
} | ||
|
||
dump(topArchiveName, | ||
logging, | ||
"-Xmx64m", "-Xms32m", | ||
"-cp", appJar, mainClass) | ||
.assertNormalExit(output -> { | ||
output.shouldHaveExitValue(0); | ||
}); | ||
|
||
run(topArchiveName, | ||
logging, | ||
"-Xmx64m", "-Xms32m", | ||
"-cp", appJar, mainClass) | ||
.assertNormalExit(output -> { | ||
output.shouldHaveExitValue(0); | ||
}); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes/LotsUnloadApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
import java.lang.invoke.MethodType; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodHandles.Lookup; | ||
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; | ||
|
||
public class LotsUnloadApp implements Runnable { | ||
static byte[] classdata; | ||
static int exitAfterNumClasses = 1024; | ||
|
||
public static void main(String args[]) throws Throwable { | ||
String resname = DefinedAsHiddenKlass.class.getName() + ".class"; | ||
classdata = LotsUnloadApp.class.getClassLoader().getResourceAsStream(resname).readAllBytes(); | ||
|
||
int numThreads = 4; | ||
try { | ||
numThreads = Integer.parseInt(args[0]); | ||
} catch (Throwable t) {} | ||
|
||
try { | ||
exitAfterNumClasses = Integer.parseInt(args[1]); | ||
} catch (Throwable t) {} | ||
|
||
for (int i = 0; i < numThreads; i++) { | ||
Thread t = new Thread(new LotsUnloadApp()); | ||
t.start(); | ||
} | ||
} | ||
|
||
public void run() { | ||
while (true) { | ||
try { | ||
Lookup lookup = MethodHandles.lookup(); | ||
Class<?> cl = lookup.defineHiddenClass(classdata, false, NESTMATE).lookupClass(); | ||
cl.newInstance(); | ||
add(); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
static int n; | ||
static synchronized void add() { | ||
n++; | ||
if (n >= exitAfterNumClasses) { | ||
System.exit(0); | ||
} | ||
} | ||
} | ||
|
||
class DefinedAsHiddenKlass { | ||
// ZGC region size is always a multiple of 2MB on x64. | ||
// Make this slightly smaller than that. | ||
static byte[] array = new byte[2 * 1024 * 1024 - 8 * 1024]; | ||
static String x; | ||
public DefinedAsHiddenKlass() { | ||
// This will generate some lambda forms hidden classes for string concat. | ||
x = "array size is " + array.length + " bytes "; | ||
} | ||
} |
09cf5f1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Issues