Path: blob/master/test/hotspot/jtreg/runtime/Dictionary/CleanProtectionDomain.java
40948 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. 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*/2223/*24* @test25* @summary Verifies the creation and cleaup of entries in the Protection Domain Table26* @library /test/lib27* @modules java.base/jdk.internal.misc28* @build sun.hotspot.WhiteBox29* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox30* @run driver CleanProtectionDomain31*/3233import java.security.ProtectionDomain;34import jdk.test.lib.compiler.InMemoryJavaCompiler;35import jdk.internal.misc.Unsafe;36import static jdk.test.lib.Asserts.*;37import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.process.ProcessTools;39import sun.hotspot.WhiteBox;4041public class CleanProtectionDomain {4243public static void main(String args[]) throws Exception {44ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(45"-Xlog:protectiondomain+table=debug",46"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",47"-XX:+UnlockDiagnosticVMOptions",48"-XX:+WhiteBoxAPI",49"-Xbootclasspath/a:.",50"-Djava.security.manager=allow",51Test.class.getName());52OutputAnalyzer output = new OutputAnalyzer(pb.start());53output.shouldContain("protection domain added");54output.shouldContain("protection domain unlinked");55output.shouldHaveExitValue(0);56}5758static class Test {59public static void test() throws Exception {60TestClassLoader classloader = new TestClassLoader();61ProtectionDomain pd = new ProtectionDomain(null, null);62byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }");63Class<?> klass = classloader.defineClass("TestClass", klassbuf, pd);64}6566public static void main(String[] args) throws Exception {67WhiteBox wb = WhiteBox.getWhiteBox();68int removedCountOrig = wb.protectionDomainRemovedCount();69int removedCount;7071test();7273// Wait until ServiceThread cleans ProtectionDomain table.74// When the TestClassLoader is unloaded by GC, at least one75// ProtectionDomainCacheEntry will be eligible for removal.76int cnt = 0;77while (true) {78if (cnt++ % 30 == 0) {79System.gc();80}81removedCount = wb.protectionDomainRemovedCount();82if (removedCountOrig != removedCount) {83break;84}85Thread.sleep(100);86}87}8889private static class TestClassLoader extends ClassLoader {90public TestClassLoader() {91super();92}9394public Class<?> defineClass(String name, byte[] bytes, ProtectionDomain pd) {95return defineClass(name, bytes, 0, bytes.length, pd);96}97}98}99}100101102