IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
AccessController.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2013 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26using System.Diagnostics;
27
28using IKVM.Runtime;
29
31{
32
33 static class AccessController
34 {
35
42 public static object getStackAccessControlContext(global::java.security.AccessControlContext context, global::ikvm.@internal.CallerID callerID)
43 {
44#if FIRST_PASS
45 throw new NotImplementedException();
46#else
47 var array = new List<global::java.security.ProtectionDomain>();
48 var is_privileged = GetProtectionDomains(array, callerID, new StackTrace(1));
49 if (array.Count == 0)
50 if (is_privileged && context == null)
51 return null;
52
53 return CreateAccessControlContext(array, is_privileged, context);
54#endif
55 }
56
57#if !FIRST_PASS
58
59 static bool GetProtectionDomains(List<global::java.security.ProtectionDomain> array, global::ikvm.@internal.CallerID callerID, StackTrace stack)
60 {
61 // first we have to skip all AccessController related frames, because we can be called from a doPrivileged implementation (not the privileged action)
62 // in which case we should ignore the doPrivileged frame
63 int skip = 0;
64 for (; skip < stack.FrameCount; skip++)
65 {
66 Type type = stack.GetFrame(skip).GetMethod().DeclaringType;
67 if (type != typeof(AccessController) && type != typeof(global::java.security.AccessController))
68 {
69 break;
70 }
71 }
72 global::java.security.ProtectionDomain previous_protection_domain = null;
73 for (int i = skip; i < stack.FrameCount; i++)
74 {
75 bool is_privileged = false;
76 global::java.security.ProtectionDomain protection_domain;
77 var method = stack.GetFrame(i).GetMethod();
78 if (method.DeclaringType == typeof(global::java.security.AccessController) && method.Name == "doPrivileged")
79 {
80 is_privileged = true;
81 global::java.lang.Class caller = callerID.getCallerClass();
82 protection_domain = caller == null ? null : IKVM.Java.Externs.java.lang.Class.getProtectionDomain0(caller);
83 }
84 else if (IKVM.Java.Externs.sun.reflect.Reflection.IsHideFromStackWalk(method))
85 {
86 continue;
87 }
88 else
89 {
90 protection_domain = GetProtectionDomainFromType(method.DeclaringType);
91 }
92
93 if (previous_protection_domain != protection_domain && protection_domain != null)
94 {
95 previous_protection_domain = protection_domain;
96 array.Add(protection_domain);
97 }
98
99 if (is_privileged)
100 {
101 return true;
102 }
103 }
104
105 return false;
106 }
107
108 static object CreateAccessControlContext(List<global::java.security.ProtectionDomain> context, bool is_privileged, global::java.security.AccessControlContext privileged_context)
109 {
110 var acc = new global::java.security.AccessControlContext(context == null || context.Count == 0 ? null : context.ToArray(), is_privileged);
111 acc._privilegedContext(privileged_context);
112 return acc;
113 }
114
115 static global::java.security.ProtectionDomain GetProtectionDomainFromType(Type type)
116 {
117 if (type == null || type.Assembly == typeof(object).Assembly || type.Assembly == typeof(AccessController).Assembly || type.Assembly == typeof(global::java.lang.Thread).Assembly)
118 return null;
119
120 var tw = JVM.Context.ClassLoaderFactory.GetJavaTypeFromType(type);
121 if (tw != null)
122 return IKVM.Java.Externs.java.lang.Class.getProtectionDomain0(tw.ClassObject);
123
124 return null;
125 }
126
127#endif
128
133 public static object getInheritedAccessControlContext()
134 {
135#if FIRST_PASS
136 throw new NotImplementedException();
137#else
138 var inheritedAccessControlContext = global::java.lang.Thread.currentThread().inheritedAccessControlContext;
139 var acc = inheritedAccessControlContext as global::java.security.AccessControlContext;
140 if (acc != null)
141 return acc;
142
143 var lc = inheritedAccessControlContext as global::java.security.AccessController.LazyContext;
144 if (lc == null)
145 return null;
146
147 var list = new List<global::java.security.ProtectionDomain>();
148 while (lc != null)
149 {
150 if (GetProtectionDomains(list, lc.callerID, lc.stackTrace))
151 return CreateAccessControlContext(list, true, lc.context);
152
153 lc = lc.parent;
154 }
155
156 return CreateAccessControlContext(list, false, null);
157#endif
158 }
159
160 }
161
162}
IKVM.Reflection.Type Type
IKVM.Reflection.Assembly Assembly
Implements the native methods for 'java.lang.Class'.
Definition Class.cs:37
static global::java.security.ProtectionDomain getProtectionDomain0(global::java.lang.Class thisClass)
Definition Class.cs:389
static object getStackAccessControlContext(global::java.security.AccessControlContext context, global::ikvm. @internal.CallerID callerID)
Implements the native method 'getStackAccessControlContext'.
static object getInheritedAccessControlContext()
Implements the native method 'getInheritedAccessControlContext'.
Provides the implementations of the native methods in global::sun.reflect.Reflection.
Definition Reflection.cs:40
Main state of the running JVM.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.