test/hotspot/jtreg/runtime/Dictionary/CleanProtectionDomain.java
author pchilanomate
Fri, 31 Aug 2018 10:22:04 -0400
changeset 51610 cdef4df6b0e7
child 51700 0fa33d4e721e
permissions -rw-r--r--
8206424: Use locking for cleaning ProtectionDomainTable Summary: ServiceThread is now in charge of cleaning ProtectionDomainTable entries Reviewed-by: coleenp, iklam
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",
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    48
                                  "-XX:+WhiteBoxAPI",
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    49
                                  "-Xbootclasspath/a:.",
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    50
                                  Test.class.getName());
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    51
    OutputAnalyzer output = new OutputAnalyzer(pb.start());
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    52
    output.shouldContain("protection domain added");
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    53
    output.shouldContain("protection domain unlinked");
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    54
    output.shouldHaveExitValue(0);
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    55
  }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    56
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    57
  static class Test {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    58
    public static void test() throws Exception {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    59
      Unsafe unsafe = Unsafe.getUnsafe();
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 { }");
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    63
      Class klass = unsafe.defineClass(null, klassbuf, 0, klassbuf.length, classloader, pd);
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
      System.gc();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    74
      // Wait until ServiceThread cleans ProtectionDomain table.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    75
      // When the TestClassLoader is unloaded by GC, at least one
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    76
      // ProtectionDomainCacheEntry will be eligible for removal.
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    77
      do {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    78
        removedCount = wb.protectionDomainRemovedCount();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    79
      } while (removedCountOrig == removedCount);
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    80
    }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    81
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    82
    private static class TestClassLoader extends ClassLoader {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    83
      public TestClassLoader() {
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    84
        super();
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    85
      }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    86
    }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    87
  }
cdef4df6b0e7 8206424: Use locking for cleaning ProtectionDomainTable
pchilanomate
parents:
diff changeset
    88
}