Path: blob/master/test/hotspot/jtreg/compiler/jvmci/SecurityRestrictionsTest.java
41149 views
/*1* Copyright (c) 2015, 2019, 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* @bug 813642126* @requires vm.jvmci27* @library /test/lib /28* @library common/patches29* @modules java.base/jdk.internal.misc30* @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot31* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper32* @run main/othervm -XX:+UnlockExperimentalVMOptions33* -XX:+EnableJVMCI34* compiler.jvmci.SecurityRestrictionsTest35* NO_SEC_MAN36* @run main/othervm -XX:+UnlockExperimentalVMOptions37* -XX:+EnableJVMCI38* -Djava.security.manager=allow39* compiler.jvmci.SecurityRestrictionsTest40* NO_PERM41* @run main/othervm -XX:+UnlockExperimentalVMOptions42* -XX:+EnableJVMCI43* -Djava.security.manager=allow44* compiler.jvmci.SecurityRestrictionsTest45* ALL_PERM46* @run main/othervm -XX:+UnlockExperimentalVMOptions47* -XX:+EnableJVMCI -XX:-UseJVMCICompiler48* -Djava.security.manager=allow49* compiler.jvmci.SecurityRestrictionsTest50* NO_JVMCI_ACCESS_PERM51* @run main/othervm -XX:+UnlockExperimentalVMOptions52* -XX:-EnableJVMCI -XX:-UseJVMCICompiler53* compiler.jvmci.SecurityRestrictionsTest54* NO_JVMCI55*/5657package compiler.jvmci;5859import jdk.test.lib.Utils;6061import java.security.AccessControlException;62import java.security.Permission;63import java.util.PropertyPermission;64import java.util.function.Consumer;6566public class SecurityRestrictionsTest {6768public static void main(String[] args) {69try {70// to init Utils before call SecurityManager71Class.forName(Utils.class.getName(), true,72Utils.class.getClassLoader());73} catch (ClassNotFoundException e) {74throw new Error("[TEST BUG]: jdk.test.lib.Utils not found", e);75}76try {77TestCase mode = TestCase.valueOf(args[0]);78mode.run();79} catch (IllegalArgumentException e) {80throw new Error("[TEST BUG]: Unknown mode " + args[0], e);81}82}8384private enum TestCase {85NO_SEC_MAN,86NO_JVMCI {87@Override88public Class<? extends Throwable> getExpectedException() {89return Error.class;90}91},92ALL_PERM {93@Override94public SecurityManager getSecurityManager() {95return new SecurityManager() {96@Override97public void checkPermission(Permission perm) {98}99};100}101},102NO_PERM {103@Override104public SecurityManager getSecurityManager() {105return new SecurityManager();106}107108@Override109public Class<? extends Throwable> getExpectedException() {110return AccessControlException.class;111}112},113NO_JVMCI_ACCESS_PERM {114@Override115public SecurityManager getSecurityManager() {116return new SecurityManager() {117@Override118public void checkPermission(Permission perm) {119if (isJvmciPermission(perm)) {120super.checkPermission(perm);121}122}123124@Override125public void checkPropertyAccess(String key) {126if (key.startsWith(JVMCI_PROP_START)) {127super.checkPropertyAccess(key);128}129}130};131}132133private boolean isJvmciPermission(Permission perm) {134String name = perm.getName();135boolean isJvmciRuntime = perm instanceof RuntimePermission136&& (JVMCI_SERVICES.equals(name)137|| name.startsWith(JVMCI_RT_PERM_START));138boolean isJvmciProperty = perm instanceof PropertyPermission139&& name.startsWith(JVMCI_PROP_START);140return isJvmciRuntime || isJvmciProperty;141}142143@Override144public Class<? extends Throwable> getExpectedException() {145return AccessControlException.class;146}147};148149public void run() {150System.setSecurityManager(getSecurityManager());151Consumer<Throwable> exceptionCheck = e -> {152if (e == null) {153if (getExpectedException() != null) {154String message = name() + ": Didn't get expected exception "155+ getExpectedException();156throw new AssertionError(message);157}158} else {159String message = name() + ": Got unexpected exception "160+ e.getClass().getSimpleName();161if (getExpectedException() == null){162throw new AssertionError(message, e);163}164165Throwable t = e;166while (t.getCause() != null) {167t = t.getCause();168}169if (!getExpectedException().isAssignableFrom(t.getClass())) {170message += " instead of " + getExpectedException()171.getSimpleName();172throw new AssertionError(message, e);173}174}175};176Utils.runAndCheckException(() -> {177// CompilerToVM::<cinit> provokes CompilerToVM::<init>178Class.forName("jdk.vm.ci.hotspot.CompilerToVMHelper");179}, exceptionCheck);180}181182public SecurityManager getSecurityManager() {183return null;184}185186public Class<? extends Throwable> getExpectedException() {187return null;188}189190private static final String JVMCI_RT_PERM_START191= "accessClassInPackage.jdk.vm.ci";192private static final String JVMCI_SERVICES = "jvmciServices";193private static final String JVMCI_PROP_START = "jvmci.";194195}196}197198199