test/hotspot/jtreg/runtime/Dictionary/CleanProtectionDomain.java
author mchung
Thu, 04 Oct 2018 08:45:21 -0700
changeset 52015 821bfc24d750
parent 51700 0fa33d4e721e
child 52705 53a407ab6d22
permissions -rw-r--r--
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass Reviewed-by: alanb, egahlin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51610
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     1
/*
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     4
 *
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     8
 *
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    13
 * accompanied this code).
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    14
 *
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    18
 *
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    21
 * questions.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    22
 */
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    23
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    24
/*
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    25
 * @test
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    26
 * @summary Verifies the creation and cleaup of entries in the Protection Domain Table
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    27
 * @library /test/lib
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    28
 * @modules java.base/jdk.internal.misc
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    29
 * @build sun.hotspot.WhiteBox
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    30
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    31
 * @run main CleanProtectionDomain
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    32
 */
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    33
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    34
import java.security.ProtectionDomain;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    35
import jdk.test.lib.compiler.InMemoryJavaCompiler;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    36
import jdk.internal.misc.Unsafe;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    37
import static jdk.test.lib.Asserts.*;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    38
import jdk.test.lib.process.OutputAnalyzer;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    39
import jdk.test.lib.process.ProcessTools;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    40
import sun.hotspot.WhiteBox;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    41
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    42
public class CleanProtectionDomain {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    43
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    44
  public static void main(String args[]) throws Exception {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    45
    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    46
                                  "-Xlog:protectiondomain+table=debug",
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    47
                                  "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
51700
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    48
                                  "-XX:+UnlockDiagnosticVMOptions",
51610
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    49
                                  "-XX:+WhiteBoxAPI",
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    50
                                  "-Xbootclasspath/a:.",
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    51
                                  Test.class.getName());
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    52
    OutputAnalyzer output = new OutputAnalyzer(pb.start());
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    53
    output.shouldContain("protection domain added");
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    54
    output.shouldContain("protection domain unlinked");
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    55
    output.shouldHaveExitValue(0);
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    56
  }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    57
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    58
  static class Test {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    59
    public static void test() throws Exception {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    60
      TestClassLoader classloader = new TestClassLoader();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    61
      ProtectionDomain pd = new ProtectionDomain(null, null);
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    62
      byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }");
52015
821bfc24d750 8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents: 51700
diff changeset
    63
      Class<?> klass = classloader.defineClass("TestClass", klassbuf, pd);
51610
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    64
    }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    65
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    66
    public static void main(String[] args) throws Exception {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    67
      WhiteBox wb = WhiteBox.getWhiteBox();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    68
      int removedCountOrig =  wb.protectionDomainRemovedCount();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    69
      int removedCount;
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    70
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    71
      test();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    72
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    73
      // Wait until ServiceThread cleans ProtectionDomain table.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    74
      // When the TestClassLoader is unloaded by GC, at least one
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    75
      // ProtectionDomainCacheEntry will be eligible for removal.
51700
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    76
      int cnt = 0;
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    77
      while (true) {
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    78
        if (cnt++ % 30 == 0) {
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    79
          System.gc();
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    80
        }
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    81
        removedCount = wb.resolvedMethodRemovedCount();
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    82
        if (removedCountOrig != removedCount) {
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    83
          break;
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    84
        }
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    85
        Thread.sleep(100);
0fa33d4e721e 8210300: runtime/MemberName/MemberNameLeak.java fails with RuntimeException
pchilanomate
parents: 51610
diff changeset
    86
      }
51610
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    87
    }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    88
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    89
    private static class TestClassLoader extends ClassLoader {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    90
      public TestClassLoader() {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    91
        super();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    92
      }
52015
821bfc24d750 8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents: 51700
diff changeset
    93
821bfc24d750 8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents: 51700
diff changeset
    94
      public Class<?> defineClass(String name, byte[] bytes, ProtectionDomain pd) {
821bfc24d750 8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents: 51700
diff changeset
    95
        return defineClass(name, bytes, 0, bytes.length, pd);
821bfc24d750 8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents: 51700
diff changeset
    96
      }
51610
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    97
    }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    98
  }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    99
}