|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2021, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.core.jdk; |
| 26 | + |
| 27 | +import com.oracle.svm.core.threadlocal.FastThreadLocalFactory; |
| 28 | +import com.oracle.svm.core.threadlocal.FastThreadLocalObject; |
| 29 | +import com.oracle.svm.core.util.VMError; |
| 30 | +import com.oracle.svm.util.ReflectionUtil; |
| 31 | +import org.graalvm.compiler.serviceprovider.JavaVersionUtil; |
| 32 | + |
| 33 | +import java.security.AccessControlContext; |
| 34 | +import java.security.PrivilegedActionException; |
| 35 | +import java.security.ProtectionDomain; |
| 36 | +import java.util.ArrayDeque; |
| 37 | +import java.util.Objects; |
| 38 | + |
| 39 | +/** |
| 40 | + * Stack for storing AccessControlContexts. Used in conjunction with |
| 41 | + * {@code StackAccessControlContextVisitor}. |
| 42 | + */ |
| 43 | +class PrivilegedStack { |
| 44 | + |
| 45 | + public static class StackElement { |
| 46 | + protected AccessControlContext context; |
| 47 | + protected Class<?> caller; |
| 48 | + |
| 49 | + StackElement(AccessControlContext context, Class<?> caller) { |
| 50 | + this.context = context; |
| 51 | + this.caller = caller; |
| 52 | + } |
| 53 | + |
| 54 | + public AccessControlContext getContext() { |
| 55 | + return context; |
| 56 | + } |
| 57 | + |
| 58 | + public Class<?> getCaller() { |
| 59 | + return caller; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /* Local AccessControlContext stack */ |
| 64 | + private static final FastThreadLocalObject<ArrayDeque<StackElement>> stack; |
| 65 | + |
| 66 | + static { |
| 67 | + |
| 68 | + @SuppressWarnings("unchecked") |
| 69 | + Class<ArrayDeque<StackElement>> cls = (Class<ArrayDeque<StackElement>>) (Object) ArrayDeque.class; |
| 70 | + stack = FastThreadLocalFactory.createObject(cls, "AccessControlContextStack"); |
| 71 | + } |
| 72 | + |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + private static ArrayDeque<StackElement> getStack() { |
| 75 | + ensureInitialized(); |
| 76 | + return stack.get(); |
| 77 | + } |
| 78 | + |
| 79 | + private static void ensureInitialized() { |
| 80 | + if (stack.get() == null) { |
| 81 | + ArrayDeque<StackElement> tmp = new ArrayDeque<>(); |
| 82 | + stack.set(tmp); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static void push(AccessControlContext context, Class<?> caller) { |
| 87 | + getStack().push(new StackElement(context, caller)); |
| 88 | + } |
| 89 | + |
| 90 | + public static void pop() { |
| 91 | + getStack().pop(); |
| 92 | + } |
| 93 | + |
| 94 | + public static AccessControlContext peekContext() { |
| 95 | + return Objects.requireNonNull(getStack().peek()).getContext(); |
| 96 | + } |
| 97 | + |
| 98 | + public static Class<?> peekCaller() { |
| 99 | + return Objects.requireNonNull(getStack().peek()).getCaller(); |
| 100 | + } |
| 101 | + |
| 102 | + public static int length() { |
| 103 | + return getStack().size(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +@InternalVMMethod |
| 108 | +@SuppressWarnings({"unused"}) |
| 109 | +public class AccessControllerUtil { |
| 110 | + |
| 111 | + /** |
| 112 | + * Instance that is used to mark contexts that were disallowed in |
| 113 | + * {@code AccessControlContextReplacerFeature.replaceAccessControlContext()} If this marker is |
| 114 | + * passed to {@code AccessController.doPrivileged()} a runtime error will be thrown. |
| 115 | + */ |
| 116 | + public static final AccessControlContext DISALLOWED_CONTEXT_MARKER; |
| 117 | + |
| 118 | + static { |
| 119 | + try { |
| 120 | + DISALLOWED_CONTEXT_MARKER = ReflectionUtil.lookupConstructor(AccessControlContext.class, ProtectionDomain[].class, boolean.class).newInstance(new ProtectionDomain[0], true); |
| 121 | + } catch (ReflectiveOperationException ex) { |
| 122 | + throw VMError.shouldNotReachHere(ex); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + static Throwable wrapCheckedException(Throwable ex) { |
| 127 | + if (ex instanceof Exception && !(ex instanceof RuntimeException)) { |
| 128 | + return new PrivilegedActionException((Exception) ex); |
| 129 | + } else { |
| 130 | + return ex; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + static Throwable wrapCheckedExceptionForPrivilegedAction(Throwable ex) { |
| 135 | + if (JavaVersionUtil.JAVA_SPEC <= 11) { |
| 136 | + return wrapCheckedException(ex); |
| 137 | + } |
| 138 | + return ex; |
| 139 | + } |
| 140 | +} |
0 commit comments