SecurityRestrictionsTest.java revision 12158:0fe2815ffa74
11590Srgrimes/*
21590Srgrimes * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41590Srgrimes *
51590Srgrimes * This code is free software; you can redistribute it and/or modify it
61590Srgrimes * under the terms of the GNU General Public License version 2 only, as
71590Srgrimes * published by the Free Software Foundation.
81590Srgrimes *
91590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121590Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131590Srgrimes * accompanied this code).
141590Srgrimes *
151590Srgrimes * You should have received a copy of the GNU General Public License version
161590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181590Srgrimes *
191590Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201590Srgrimes * or visit www.oracle.com if you need additional information or have any
211590Srgrimes * questions.
221590Srgrimes */
231590Srgrimes
241590Srgrimes/**
251590Srgrimes * @test
261590Srgrimes * @bug 8136421
271590Srgrimes * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
281590Srgrimes * @library /test/lib /
291590Srgrimes * @library common/patches
301590Srgrimes * @modules java.base/jdk.internal.misc
311590Srgrimes * @modules jdk.vm.ci/jdk.vm.ci.hotspot
321590Srgrimes * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
331590Srgrimes * @run main/othervm -XX:+UnlockExperimentalVMOptions
341590Srgrimes *      -XX:+EnableJVMCI
351590Srgrimes *      compiler.jvmci.SecurityRestrictionsTest
361590Srgrimes *      NO_SEC_MAN
3762833Swsanchez * @run main/othervm -XX:+UnlockExperimentalVMOptions
3862833Swsanchez *      -XX:+EnableJVMCI
391590Srgrimes *      compiler.jvmci.SecurityRestrictionsTest
401590Srgrimes *      NO_PERM
4162833Swsanchez * @run main/othervm -XX:+UnlockExperimentalVMOptions
4294587Sobrien *      -XX:+EnableJVMCI
431590Srgrimes *      compiler.jvmci.SecurityRestrictionsTest
44141104Sharti *      ALL_PERM
45141104Sharti * @run main/othervm -XX:+UnlockExperimentalVMOptions
46141104Sharti *      -XX:+EnableJVMCI
471590Srgrimes *      compiler.jvmci.SecurityRestrictionsTest
48141133Sharti *      NO_JVMCI_ACCESS_PERM
49141104Sharti * @run main/othervm -XX:+UnlockExperimentalVMOptions
50141104Sharti *      -XX:-EnableJVMCI
51141104Sharti *      compiler.jvmci.SecurityRestrictionsTest
52141104Sharti *      NO_JVMCI
53141104Sharti */
545814Sjkh
555814Sjkhpackage compiler.jvmci;
565814Sjkh
575814Sjkhimport jdk.test.lib.Utils;
585814Sjkh
595814Sjkhimport java.security.AccessControlException;
605814Sjkhimport java.security.Permission;
615814Sjkhimport java.util.PropertyPermission;
625814Sjkhimport java.util.function.Consumer;
63104696Sjmallett
645814Sjkhpublic class SecurityRestrictionsTest {
655814Sjkh
66138232Sharti    public static void main(String[] args) {
67138264Sharti        try {
685814Sjkh            // to init Utils before call SecurityManager
695814Sjkh            Class.forName(Utils.class.getName(), true,
705814Sjkh                    Utils.class.getClassLoader());
711590Srgrimes        } catch (ClassNotFoundException e) {
721590Srgrimes            throw new Error("[TEST BUG]: jdk.test.lib.Utils not found", e);
73138547Sharti        }
741590Srgrimes        try {
751590Srgrimes            TestCase mode = TestCase.valueOf(args[0]);
761590Srgrimes            mode.run();
771590Srgrimes        } catch (IllegalArgumentException e) {
781590Srgrimes            throw new Error("[TEST BUG]: Unknown mode " + args[0], e);
79138547Sharti        }
801590Srgrimes    }
8194584Sobrien
8294584Sobrien    private enum TestCase {
831590Srgrimes        NO_SEC_MAN,
841590Srgrimes        NO_JVMCI {
8594638Sobrien            @Override
8694638Sobrien            public Class<? extends Throwable> getExpectedException() {
871590Srgrimes                return InternalError.class;
881590Srgrimes            }
89138264Sharti        },
901590Srgrimes        ALL_PERM {
911590Srgrimes            @Override
9294638Sobrien            public SecurityManager getSecurityManager() {
931590Srgrimes                return new SecurityManager() {
941590Srgrimes                    @Override
9594638Sobrien                    public void checkPermission(Permission perm) {
9694638Sobrien                    }
9794638Sobrien                };
9894638Sobrien            }
9994638Sobrien        },
10094638Sobrien        NO_PERM {
1011590Srgrimes            @Override
1021590Srgrimes            public SecurityManager getSecurityManager() {
10394638Sobrien                return new SecurityManager();
10494638Sobrien            }
1051590Srgrimes
106138232Sharti            @Override
1071590Srgrimes            public Class<? extends Throwable> getExpectedException() {
1081590Srgrimes                return AccessControlException.class;
1091590Srgrimes            }
1101590Srgrimes        },
1111590Srgrimes        NO_JVMCI_ACCESS_PERM {
1121590Srgrimes            @Override
1131590Srgrimes            public SecurityManager getSecurityManager() {
1141590Srgrimes                return new SecurityManager() {
1151590Srgrimes                    @Override
1161590Srgrimes                    public void checkPermission(Permission perm) {
1171590Srgrimes                        if (isJvmciPermission(perm)) {
1181590Srgrimes                            super.checkPermission(perm);
1191590Srgrimes                        }
120104696Sjmallett                    }
1211590Srgrimes
12294584Sobrien                    @Override
12394584Sobrien                    public void checkPropertyAccess(String key) {
1241590Srgrimes                        if (key.startsWith(JVMCI_PROP_START)) {
1251590Srgrimes                            super.checkPropertyAccess(key);
1261590Srgrimes                        }
1271590Srgrimes                    }
1281590Srgrimes                };
1291590Srgrimes            }
1301590Srgrimes
1315814Sjkh            private boolean isJvmciPermission(Permission perm) {
1325814Sjkh                String name = perm.getName();
1335814Sjkh                boolean isJvmciRuntime = perm instanceof RuntimePermission
1345814Sjkh                        && (JVMCI_SERVICES.equals(name)
1355814Sjkh                            || name.startsWith(JVMCI_RT_PERM_START));
1361590Srgrimes                boolean isJvmciProperty = perm instanceof PropertyPermission
1371590Srgrimes                        && name.startsWith(JVMCI_PROP_START);
1381590Srgrimes                return isJvmciRuntime || isJvmciProperty;
1391590Srgrimes            }
1401590Srgrimes
1411590Srgrimes            @Override
1421590Srgrimes            public Class<? extends Throwable> getExpectedException() {
1435814Sjkh                return AccessControlException.class;
1441590Srgrimes            }
1451590Srgrimes        };
1461590Srgrimes
14749938Shoek        public void run() {
148124966Sru            System.setSecurityManager(getSecurityManager());
1491590Srgrimes            Consumer<Throwable> exceptionCheck = e -> {
150124966Sru                if (e == null) {
151124966Sru                    if (getExpectedException() != null) {
152124966Sru                        String message = name() + ": Didn't get expected exception "
153124966Sru                                + getExpectedException();
154124840Sru                        throw new AssertionError(message);
155138264Sharti                    }
156124840Sru                } else {
157124840Sru                    String message = name() + ": Got unexpected exception "
158124840Sru                            + e.getClass().getSimpleName();
1591590Srgrimes                    if (getExpectedException() == null){
1601590Srgrimes                        throw new AssertionError(message, e);
1615814Sjkh                    }
1621590Srgrimes
1631590Srgrimes                    Throwable t = e;
1641590Srgrimes                    while (t.getCause() != null) {
1651590Srgrimes                        t = t.getCause();
1661590Srgrimes                    }
1671590Srgrimes                    if (!getExpectedException().isAssignableFrom(t.getClass())) {
1681590Srgrimes                        message += " instead of " + getExpectedException()
1691590Srgrimes                                .getSimpleName();
1701590Srgrimes                        throw new AssertionError(message, e);
1711590Srgrimes                    }
1725814Sjkh                }
1735814Sjkh            };
1745814Sjkh            Utils.runAndCheckException(() -> {
1751590Srgrimes                try {
1761590Srgrimes                    // CompilerToVM::<cinit> provokes CompilerToVM::<init>
1771590Srgrimes                    Class.forName("jdk.vm.ci.hotspot.CompilerToVMHelper");
178138264Sharti                } catch (ClassNotFoundException e) {
17918730Ssteve                    throw new Error("TESTBUG : " + e, e);
1801590Srgrimes                }
1811590Srgrimes            }, exceptionCheck);
182138264Sharti        }
1831590Srgrimes
1841590Srgrimes        public SecurityManager getSecurityManager() {
1851590Srgrimes            return null;
1861590Srgrimes        }
1875814Sjkh
1885814Sjkh        public Class<? extends Throwable> getExpectedException() {
1895814Sjkh            return null;
1905814Sjkh        }
1915814Sjkh
1925814Sjkh        private static final String JVMCI_RT_PERM_START
1935814Sjkh                = "accessClassInPackage.jdk.vm.ci";
1948874Srgrimes        private static final String JVMCI_SERVICES = "jvmciServices";
1951590Srgrimes        private static final String JVMCI_PROP_START = "jvmci.";
1961590Srgrimes
1971590Srgrimes    }
1981590Srgrimes}
1991590Srgrimes