private static bool GetProtectionDomains(List<java.security.ProtectionDomain> array, [email protected] callerID, StackTrace stack) { // first we have to skip all AccessController related frames, because we can be called from a doPrivileged implementation (not the privileged action) // in which case we should ignore the doPrivileged frame int skip = 0; for (; skip < stack.FrameCount; skip++) { Type type = stack.GetFrame(skip).GetMethod().DeclaringType; if (type != typeof(Java_java_security_AccessController) && type != typeof(java.security.AccessController)) { break; } } java.security.ProtectionDomain previous_protection_domain = null; for (int i = skip; i < stack.FrameCount; i++) { bool is_privileged = false; java.security.ProtectionDomain protection_domain; MethodBase method = stack.GetFrame(i).GetMethod(); if (method.DeclaringType == typeof(java.security.AccessController) && method.Name == "doPrivileged") { is_privileged = true; java.lang.Class caller = callerID.getCallerClass(); protection_domain = caller == null ? null : Java_java_lang_Class.getProtectionDomain0(caller); } else if (Java_sun_reflect_Reflection.IsHideFromStackWalk(method)) { continue; } else { protection_domain = GetProtectionDomainFromType(method.DeclaringType); } if (previous_protection_domain != protection_domain && protection_domain != null) { previous_protection_domain = protection_domain; array.Add(protection_domain); } if (is_privileged) { return true; } } return false; }
public static object getStackAccessControlContext(java.security.AccessControlContext context, [email protected] callerID) { #if FIRST_PASS return null; #else List<java.security.ProtectionDomain> array = new List<java.security.ProtectionDomain>(); bool is_privileged = GetProtectionDomains(array, callerID, new StackTrace(1)); if (array.Count == 0) { if (is_privileged && context == null) { return null; } } return CreateAccessControlContext(array, is_privileged, context); #endif }
internal InvokeArgsProcessor(MethodWrapper mw, MethodBase method, object original_obj, object[] original_args, [email protected] callerID) { TypeWrapper[] argTypes = mw.GetParameters(); if(!mw.IsStatic && method.IsStatic && mw.Name != "<init>") { // we've been redirected to a static method, so we have to copy the 'obj' into the args object[] nargs = new object[original_args.Length + 1]; nargs[0] = original_obj; original_args.CopyTo(nargs, 1); this.obj = null; this.args = nargs; for(int i = 0; i < argTypes.Length; i++) { if(!argTypes[i].IsUnloadable && argTypes[i].IsGhost) { object v = Activator.CreateInstance(argTypes[i].TypeAsSignatureType); argTypes[i].GhostRefField.SetValue(v, args[i + 1]); args[i + 1] = v; } } } else { this.obj = original_obj; this.args = original_args; for(int i = 0; i < argTypes.Length; i++) { if(!argTypes[i].IsUnloadable && argTypes[i].IsGhost) { if(this.args == original_args) { this.args = (object[])args.Clone(); } object v = Activator.CreateInstance(argTypes[i].TypeAsSignatureType); argTypes[i].GhostRefField.SetValue(v, args[i]); this.args[i] = v; } } } if(mw.HasCallerID) { object[] nargs = new object[args.Length + 1]; Array.Copy(args, nargs, args.Length); nargs[args.Length] = callerID; args = nargs; } }
public static IntPtr GetFuncPtr([email protected] callerID, string clazz, string name, string sig) { ClassLoaderWrapper loader = ClassLoaderWrapper.FromCallerID(callerID); int sp = 0; for(int i = 1; sig[i] != ')'; i++) { switch(sig[i]) { case '[': sp += IntPtr.Size; while(sig[++i] == '['); if(sig[i] == 'L') { while(sig[++i] != ';'); } break; case 'L': sp += IntPtr.Size; while(sig[++i] != ';'); break; case 'J': case 'D': sp += 8; break; case 'F': case 'I': case 'C': case 'Z': case 'S': case 'B': sp += 4; break; default: Debug.Assert(false); break; } } string mangledClass = JniMangle(clazz); string mangledName = JniMangle(name); string mangledSig = JniMangle(sig.Substring(1, sig.IndexOf(')') - 1)); string shortMethodName = String.Format("Java_{0}_{1}", mangledClass, mangledName); string longMethodName = String.Format("Java_{0}_{1}__{2}", mangledClass, mangledName, mangledSig); Tracer.Info(Tracer.Jni, "Linking native method: {0}.{1}{2}, class loader = {3}, short = {4}, long = {5}, args = {6}", clazz, name, sig, loader, shortMethodName, longMethodName, sp + 2 * IntPtr.Size); lock(JniHelper.JniLock) { foreach(IntPtr p in loader.GetNativeLibraries()) { IntPtr pfunc = JniHelper.ikvm_GetProcAddress(p, shortMethodName, sp + 2 * IntPtr.Size); if(pfunc != IntPtr.Zero) { Tracer.Info(Tracer.Jni, "Native method {0}.{1}{2} found in library 0x{3:X} (short)", clazz, name, sig, p.ToInt64()); return pfunc; } pfunc = JniHelper.ikvm_GetProcAddress(p, longMethodName, sp + 2 * IntPtr.Size); if(pfunc != IntPtr.Zero) { Tracer.Info(Tracer.Jni, "Native method {0}.{1}{2} found in library 0x{3:X} (long)", clazz, name, sig, p.ToInt64()); return pfunc; } } } string msg = string.Format("{0}.{1}{2}", clazz, name, sig); Tracer.Error(Tracer.Jni, "UnsatisfiedLinkError: {0}", msg); throw new java.lang.UnsatisfiedLinkError(msg); }
public IntPtr Enter([email protected] callerID) { env = TlsHack.ManagedJNIEnv; if(env == null) { env = JNIEnv.CreateJNIEnv()->GetManagedJNIEnv(); } prevFrameState = env.Enter(callerID); return (IntPtr)(void*)env.pJNIEnv; }
internal FrameState Enter([email protected] newCallerID) { FrameState prev = new FrameState(callerID, localRefSlot, localRefIndex); this.callerID = newCallerID; localRefSlot++; if (localRefSlot >= localRefs.Length) { object[][] tmp = new object[localRefs.Length * 2][]; Array.Copy(localRefs, 0, tmp, 0, localRefs.Length); localRefs = tmp; } localRefIndex = 0; active = localRefs[localRefSlot]; if (active == null) { active = localRefs[localRefSlot] = new object[LOCAL_REF_INITIAL_BUCKET_SIZE]; } return prev; }
internal FrameState([email protected] callerID, int localRefSlot, int localRefIndex) { this.callerID = callerID; this.localRefSlot = localRefSlot; this.localRefIndex = localRefIndex; }
/// <summary> /// Set an EventRequest received from debugger. /// </summary> /// <param name="eventRequest">the new EventRequest</param> internal void AddEventRequest(ikvm.debugger.requests.EventRequest eventRequest) { switch (eventRequest.EventKind) { case EventKind.THREAD_START: threadStartEventRequest = eventRequest; break; } }
object ICustomInvoke.Invoke(object obj, object[] args, [email protected] callerID) { FieldWrapper[] values = this.DeclaringType.GetFields(); for (int i = 0; i < values.Length; i++) { if (values[i].Name.Equals(args[0])) { return ((EnumFieldWrapper)values[i]).GetValue(); } } throw new java.lang.IllegalArgumentException("" + args[0]); }
object ICustomInvoke.Invoke(object obj, object[] args, [email protected] callerID) { FieldWrapper[] values = this.DeclaringType.GetFields(); object[] array = (object[])Array.CreateInstance(this.DeclaringType.TypeAsArrayType, values.Length); for (int i = 0; i < values.Length; i++) { array[i] = ((EnumFieldWrapper)values[i]).GetValue(); } return array; }
object ICustomInvoke.Invoke(object obj, object[] args, [email protected] callerID) { // a DynamicOnlyMethodWrapper is an interface method, but now that we've been called on an actual object instance, // we can resolve to a real method and call that instead TypeWrapper tw = TypeWrapper.FromClass(NativeCode.ikvm.runtime.Util.getClassFromObject(obj)); MethodWrapper mw = tw.GetMethodWrapper(this.Name, this.Signature, true); if (mw == null) { throw new java.lang.AbstractMethodError(tw.Name + "." + this.Name + this.Signature); } java.lang.reflect.Method m = (java.lang.reflect.Method)mw.ToMethodOrConstructor(true); m.@override = true; return m.invoke(obj, args, callerID); }
object ICustomInvoke.Invoke(object obj, object[] args, [email protected] callerID) { return method.Invoke(obj, args); }
private static bool GetProtectionDomains(List<java.security.ProtectionDomain> array, [email protected] callerID, StackTrace stack) { java.security.ProtectionDomain previous_protection_domain = null; for (int i = 0; i < stack.FrameCount; i++) { bool is_privileged = false; java.security.ProtectionDomain protection_domain; MethodBase method = stack.GetFrame(i).GetMethod(); if (method.DeclaringType == typeof(java.security.AccessController) && method.Name == "doPrivileged") { is_privileged = true; java.lang.Class caller = callerID.getCallerClass(); protection_domain = caller == null ? null : Java_java_lang_Class.getProtectionDomain0(caller); } else { protection_domain = GetProtectionDomainFromType(method.DeclaringType); } if (previous_protection_domain != protection_domain && protection_domain != null) { previous_protection_domain = protection_domain; array.Add(protection_domain); } if (is_privileged) { return true; } } return false; }