1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  31  *      compiler.jvmci.SecurityRestrictionsTest
  32  *      NO_SEC_MAN
  33  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  34  *      compiler.jvmci.SecurityRestrictionsTest
  35  *      NO_PERM
  36  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  37  *      compiler.jvmci.SecurityRestrictionsTest
  38  *      ALL_PERM
  39  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  40  *      compiler.jvmci.SecurityRestrictionsTest
  41  *      NO_JVMCI_ACCESS_PERM
  42  * @run main/othervm -XX:+UnlockExperimentalVMOptions 
  43  *      compiler.jvmci.SecurityRestrictionsTest
  44  *      NO_JVMCI
  45  */
  46 
  47 package compiler.jvmci;
  48 
  49 import jdk.vm.ci.hotspot.CompilerToVM;
  50 import jdk.test.lib.Utils;
  51 import java.lang.InternalError;
  52 import java.security.AccessControlException;
  53 import java.security.Permission;
  54 
  55 public class SecurityRestrictionsTest {
  56 
  57     public static void main(String[] args) {
  58         try {
  59             // to init Utils before call SecurityManager
  60             Class.forName(Utils.class.getName(), true,
  61                     Utils.class.getClassLoader());
  62         } catch (ClassNotFoundException e) {
  63             throw new Error("[TEST BUG]: jdk.test.lib.Utils not found", e);
  64         }
  65         try {
  66             TestCase mode = TestCase.valueOf(args[0]);
  67             mode.run();
  68         } catch (IllegalArgumentException e) {
  69             throw new Error("[TEST BUG]: Unknown mode " + args[0], e);
  70         }
  71     }
  72 
  73     private enum TestCase {
  74         NO_SEC_MAN,
  75         NO_JVMCI {
  76             @Override
  77             public Class<? extends Throwable> getExpectedException() {
  78                 return InternalError.class;
  79             }
  80         },
  81         ALL_PERM {
  82             @Override
  83             public SecurityManager getSecurityManager() {
  84                 return new SecurityManager() {
  85                     @Override
  86                     public void checkPermission(Permission perm) {
  87                     }
  88                 };
  89             }
  90         },
  91         NO_PERM {
  92             @Override
  93             public SecurityManager getSecurityManager() {
  94                 return new SecurityManager();
  95             }
  96 
  97             @Override
  98             public Class<? extends Throwable> getExpectedException() {
  99                 return AccessControlException.class;
 100             }
 101         },
 102         NO_JVMCI_ACCESS_PERM {
 103             @Override
 104             public SecurityManager getSecurityManager() {
 105                 return new SecurityManager() {
 106                     @Override
 107                     public void checkPermission(Permission perm) {
 108                         if (isJvmciPermission(perm)) {
 109                             super.checkPermission(perm);
 110                         }
 111                     }
 112 
 113                     @Override
 114                     public void checkPropertyAccess(String key) {
 115                         if (key.startsWith(JVMCI_PROP_START)) {
 116                             super.checkPropertyAccess(key);
 117                         }
 118                     }
 119                 };
 120             }
 121 
 122             private boolean isJvmciPermission(Permission perm) {
 123                 String name = perm.getName();
 124                 return perm instanceof RuntimePermission
 125                         && (JVMCI_SERVICES.equals(name)
 126                                 || name.startsWith(JVMCI_RT_PERM_START));
 127             }
 128 
 129             @Override
 130             public Class<? extends Throwable> getExpectedException() {
 131                 return AccessControlException.class;
 132             }
 133         };
 134 
 135         public void run() {
 136             System.setSecurityManager(getSecurityManager());
 137             Utils.runAndCheckException(
 138                     // to run CompilerToVM::<cinit> inside runAndCheckException
 139                     () -> new CompilerToVM(),
 140                     getExpectedException());
 141         }
 142 
 143         public SecurityManager getSecurityManager() {
 144             return null;
 145         }
 146 
 147         public Class<? extends Throwable> getExpectedException() {
 148             return null;
 149         }
 150 
 151         private static final String JVMCI_RT_PERM_START
 152                 = "accessClassInPackage.jdk.vm.ci";
 153         private static final String JVMCI_SERVICES = "jvmciServices";
 154         private static final String JVMCI_PROP_START = "jvmci.";
 155     }
 156 }