Example #1
0
        public static object DynamicMultianewarray(string clazz, int[] lengths, [email protected] callerId)
        {
            Profiler.Count("DynamicMultianewarray");
            TypeWrapper wrapper = LoadTypeWrapper(clazz, callerId);

            return(multianewarray(wrapper.TypeAsArrayType.TypeHandle, lengths));
        }
Example #2
0
        private static TypeWrapper LoadTypeWrapper(string clazz, [email protected] callerId)
        {
#if FIRST_PASS
            return(null);
#else
            try
            {
                TypeWrapper           context = TypeWrapper.FromClass(callerId.getCallerClass());
                TypeWrapper           wrapper = ClassLoaderWrapper.FromCallerID(callerId).LoadClassByDottedName(clazz);
                java.lang.ClassLoader loader  = callerId.getCallerClassLoader();
                if (loader != null)
                {
                    loader.checkPackageAccess(wrapper.ClassObject, callerId.getCallerClass().pd);
                }
                if (!wrapper.IsAccessibleFrom(context))
                {
                    throw new java.lang.IllegalAccessError("Try to access class " + wrapper.Name + " from class " + context.Name);
                }
                wrapper.Finish();
                return(wrapper);
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
        // this method is used by IKVM.Runtime.JNI
        internal static ClassLoaderWrapper FromCallerID([email protected] callerID)
        {
#if FIRST_PASS
            return(null);
#else
            return(GetClassLoaderWrapper(callerID.getCallerClassLoader()));
#endif
        }
Example #4
0
        public static java.lang.Class DynamicClassLiteral(string clazz, [email protected] callerId)
        {
#if FIRST_PASS
            return(null);
#else
            Profiler.Count("DynamicClassLiteral");
            return(LoadTypeWrapper(clazz, callerId).ClassObject);
#endif
        }
Example #5
0
        public static void DynamicNewCheckOnly(string clazz, [email protected] callerId)
        {
            Profiler.Count("DynamicNewCheckOnly");
            TypeWrapper wrapper = LoadTypeWrapper(clazz, callerId);

            if (wrapper.IsAbstract)
            {
                throw new java.lang.InstantiationError(clazz);
            }
            wrapper.RunClassInit();
        }
Example #6
0
        public static object DynamicNewarray(int length, string clazz, [email protected] callerId)
        {
            Profiler.Count("DynamicNewarray");
            if (length < 0)
            {
                throw new java.lang.NegativeArraySizeException();
            }
            TypeWrapper wrapper = LoadTypeWrapper(clazz, callerId);

            return(Array.CreateInstance(wrapper.TypeAsArrayType, length));
        }
Example #7
0
 public static object DynamicCast(object obj, string clazz, [email protected] callerId)
 {
     Profiler.Count("DynamicCast");
     // NOTE it's important that we don't try to load the class if obj == null
     // (to be compatible with Sun)
     if (obj != null && !DynamicInstanceOf(obj, clazz, callerId))
     {
         throw new java.lang.ClassCastException(NativeCode.ikvm.runtime.Util.GetTypeWrapperFromObject(obj).Name);
     }
     return(obj);
 }
Example #8
0
        public static bool DynamicInstanceOf(object obj, string clazz, [email protected] callerId)
        {
            Profiler.Count("DynamicInstanceOf");
            // NOTE it's important that we don't try to load the class if obj == null
            // (to be compatible with Sun)
            if (obj == null)
            {
                return(false);
            }
            TypeWrapper wrapper = LoadTypeWrapper(clazz, callerId);

            return(wrapper.IsInstance(obj));
        }
Example #9
0
        public static object DynamicGetstatic(string name, string sig, string clazz, [email protected] callerID)
        {
            Profiler.Count("DynamicGetstatic");
            FieldWrapper fw = GetFieldWrapper(null, clazz, name, sig, true, callerID);

            java.lang.reflect.Field field = (java.lang.reflect.Field)fw.ToField(false);
            object val = field.get(null, callerID);

            if (fw.FieldTypeWrapper.IsPrimitive)
            {
                val = JVM.Unbox(val);
            }
            return(val);
        }
Example #10
0
        private static TypeWrapper LoadTypeWrapper(string clazz, [email protected] callerId)
        {
#if FIRST_PASS
            return(null);
#else
            try
            {
                TypeWrapper context = TypeWrapper.FromClass(callerId.getCallerClass());
#if WINRT
                RuntimeReflectionHelper.Instance.Mark(clazz);
#endif
                System.Diagnostics.Debug.WriteLine("Trying to load " + clazz);
                TypeWrapper wrapper = null;
                try {
                    wrapper = ClassLoaderWrapper.FromCallerID(callerId).LoadClassByDottedName(clazz);
                } catch (NotImplementedException nie)
                {
#if WINRT
                    RuntimeReflectionHelper.Instance.Mark(clazz);
#endif
                    System.Diagnostics.Debug.WriteLine("Not implemented " + clazz);
                    throw nie;
                }

                java.lang.ClassLoader loader = callerId.getCallerClassLoader();
                if (loader != null)
                {
                    loader.checkPackageAccess(wrapper.ClassObject, callerId.getCallerClass().pd);
                }
                if (!wrapper.IsAccessibleFrom(context))
                {
                    throw new java.lang.IllegalAccessError("Try to access class " + wrapper.Name + " from class " + context.Name);
                }
                wrapper.Finish();
                return(wrapper);
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
Example #11
0
    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);
    }
Example #12
0
        private static java.lang.invoke.MethodHandle DynamicLoadMethodHandleImpl(int kind, string clazz, string name, string sig, [email protected] callerID)
        {
#if FIRST_PASS
            return(null);
#else
            java.lang.invoke.MethodHandles.Lookup lookup = new java.lang.invoke.MethodHandles.Lookup(callerID.getCallerClass(),
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PUBLIC |
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PRIVATE |
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PROTECTED |
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PACKAGE,
                                                                                                     true);
            java.lang.Class refc = LoadTypeWrapper(clazz, callerID).ClassObject;
            try
            {
                switch ((ClassFile.RefKind)kind)
                {
                case ClassFile.RefKind.getStatic:
                case ClassFile.RefKind.putStatic:
                case ClassFile.RefKind.getField:
                case ClassFile.RefKind.putField:
                    java.lang.Class type = ClassLoaderWrapper.FromCallerID(callerID).FieldTypeWrapperFromSig(sig).ClassObject;
                    switch ((ClassFile.RefKind)kind)
                    {
                    case ClassFile.RefKind.getStatic:
                        return(lookup.findStaticGetter(refc, name, type));

                    case ClassFile.RefKind.putStatic:
                        return(lookup.findStaticSetter(refc, name, type));

                    case ClassFile.RefKind.getField:
                        return(lookup.findGetter(refc, name, type));

                    case ClassFile.RefKind.putField:
                        return(lookup.findSetter(refc, name, type));

                    default:
                        throw new InvalidOperationException();
                    }

                default:
                    java.lang.invoke.MethodType mt = null;
                    DynamicLoadMethodType(ref mt, sig, callerID);
                    switch ((ClassFile.RefKind)kind)
                    {
                    case ClassFile.RefKind.invokeInterface:
                        return(lookup.findVirtual(refc, name, mt));

                    case ClassFile.RefKind.invokeSpecial:
                        return(lookup.findSpecial(refc, name, mt, callerID.getCallerClass()));

                    case ClassFile.RefKind.invokeStatic:
                        return(lookup.findStatic(refc, name, mt));

                    case ClassFile.RefKind.invokeVirtual:
                        return(lookup.findVirtual(refc, name, mt));

                    case ClassFile.RefKind.newInvokeSpecial:
                        return(lookup.findConstructor(refc, mt));

                    default:
                        throw new InvalidOperationException();
                    }
                }
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
            catch (java.lang.ReflectiveOperationException x)
            {
                throw new java.lang.IncompatibleClassChangeError().initCause(x);
            }
#endif
        }
Example #13
0
    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);
    }
Example #14
0
        public static void DynamicLinkIndyCallSite <T>(ref IndyCallSite <T> site, java.lang.invoke.CallSite cs, Exception x, string signature, [email protected] callerID)
            where T : class             // Delegate
        {
#if !FIRST_PASS
            // when a CallSite is first constructed, it doesn't call MethodHandleNatives.setCallSiteTargetNormal(),
            // so we have to check if we need to initialize it here (i.e. attach an IndyCallSite<T> to it)
            if (cs != null)
            {
                if (cs.ics == null)
                {
                    Java_java_lang_invoke_MethodHandleNatives.InitializeCallSite(cs);
                }
                lock (cs.ics)
                {
                    cs.ics.SetTarget(cs.target);
                }
            }
            java.lang.invoke.MethodType typeCache = null;
            IndyCallSite <T>            ics;
            if (x != null || cs == null || cs.type() != DynamicLoadMethodType(ref typeCache, signature, callerID))
            {
                x = MapException <Exception>(x ?? (cs == null
                                        ? (Exception) new java.lang.ClassCastException("bootstrap method failed to produce a CallSite")
                                        : new java.lang.invoke.WrongMethodTypeException()), MapFlags.None);
                java.lang.invoke.MethodType   type = LoadMethodType <T>();
                java.lang.invoke.MethodHandle exc  = x is java.lang.BootstrapMethodError
                                        ? java.lang.invoke.MethodHandles.constant(typeof(java.lang.BootstrapMethodError), x)
                                        : java.lang.invoke.MethodHandles.publicLookup().findConstructor(typeof(java.lang.BootstrapMethodError), java.lang.invoke.MethodType.methodType(typeof(void), typeof(string), typeof(Exception)))
                                                     .bindTo("call site initialization exception").bindTo(x);
                ics = new IndyCallSite <T>();
                ((IIndyCallSite)ics).SetTarget(
                    java.lang.invoke.MethodHandles.dropArguments(
                        java.lang.invoke.MethodHandles.foldArguments(
                            java.lang.invoke.MethodHandles.throwException(type.returnType(), typeof(java.lang.BootstrapMethodError)),
                            exc),
                        0, type.parameterArray()));
            }
            else
            {
                ics = new IndyCallSite <T>();
                ((IIndyCallSite)ics).SetTarget(cs.dynamicInvoker().asType(LoadMethodType <T>()));
            }
            IndyCallSite <T> curr = site;
            if (curr.IsBootstrap)
            {
                Interlocked.CompareExchange(ref site, ics, curr);
            }
#endif
        }
Example #15
0
        public static object DynamicInvokevirtual(object obj, RuntimeTypeHandle type, string clazz, string name, string sig, object[] args, [email protected] callerID)
        {
            Profiler.Count("DynamicInvokevirtual");
            MethodWrapper mw = GetMethodWrapper(obj, type, clazz, name, sig, false);

            java.lang.reflect.Method m = (java.lang.reflect.Method)mw.ToMethodOrConstructor(false);
            object val = m.invoke(obj, BoxArgs(mw, args), callerID);

            if (mw.ReturnType.IsPrimitive && mw.ReturnType != PrimitiveTypeWrapper.VOID)
            {
                val = JVM.Unbox(val);
            }
            return(val);
        }
Example #16
0
    public static object getStackAccessControlContext(java.security.AccessControlContext context, [email protected] callerID)
    {
#if FIRST_PASS
        return(null);
#else
#if !WINRT
        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));
#else
        throw new NotImplementedException();
#endif
#endif
    }
Example #17
0
        private static FieldWrapper GetFieldWrapper(object thisObj, string clazz, string name, string sig, bool isStatic, [email protected] callerId)
        {
            TypeWrapper  caller  = TypeWrapper.FromClass(callerId.getCallerClass());
            TypeWrapper  wrapper = LoadTypeWrapper(clazz, callerId);
            FieldWrapper field   = wrapper.GetFieldWrapper(name, sig);

            if (field == null)
            {
                throw new java.lang.NoSuchFieldError(clazz + "." + name);
            }
            // TODO check loader constraints
            if (field.IsStatic != isStatic)
            {
                throw new java.lang.IncompatibleClassChangeError(clazz + "." + name);
            }
            TypeWrapper objType = null;

            if (thisObj != null)
            {
                objType = ClassLoaderWrapper.GetWrapperFromType(thisObj.GetType());
            }
            if (field.IsAccessibleFrom(wrapper, caller, objType))
            {
                return(field);
            }
            throw new java.lang.IllegalAccessError(field.DeclaringType.Name + "." + name);
        }
Example #18
0
        public static object DynamicInvokeSpecialNew(RuntimeTypeHandle type, string clazz, string name, string sig, object[] args, [email protected] callerID)
        {
            Profiler.Count("DynamicInvokeSpecialNew");
            MethodWrapper mw = GetMethodWrapper(null, type, clazz, name, sig, false);

            java.lang.reflect.Constructor cons = (java.lang.reflect.Constructor)mw.ToMethodOrConstructor(false);
            return(cons.newInstance(BoxArgs(mw, args), callerID));
        }
Example #19
0
			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;
			}
Example #20
0
        public static T DynamicBinderMemberLookup <T>(int kind, string clazz, string name, string sig, [email protected] callerID)
            where T : class             /* delegate */
        {
#if FIRST_PASS
            return(null);
#else
            try
            {
                java.lang.invoke.MethodHandle mh = DynamicLoadMethodHandleImpl(kind, clazz, name, sig, callerID);
                return(GetDelegateForInvokeExact <T>(java.lang.invoke.MethodHandles.explicitCastArguments(mh, MethodHandleUtil.GetDelegateMethodType(typeof(T)))));
            }
            catch (java.lang.IncompatibleClassChangeError x)
            {
                if (x.getCause() is java.lang.NoSuchMethodException)
                {
                    throw new java.lang.NoSuchMethodError(x.getCause().Message);
                }
                if (x.getCause() is java.lang.NoSuchFieldException)
                {
                    throw new java.lang.NoSuchFieldError(x.getCause().Message);
                }
                if (x.getCause() is java.lang.IllegalAccessException)
                {
                    throw new java.lang.IllegalAccessError(x.getCause().Message);
                }
                throw;
            }
#endif
        }
Example #21
0
 public static java.lang.invoke.MethodHandle DynamicLoadMethodHandle(ref java.lang.invoke.MethodHandle cache, int kind, string clazz, string name, string sig, [email protected] callerID)
 {
     if (cache == null)
     {
         Interlocked.CompareExchange(ref cache, DynamicLoadMethodHandleImpl(kind, clazz, name, sig, callerID), null);
     }
     return(cache);
 }
Example #22
0
 public static java.lang.invoke.MethodType DynamicLoadMethodType(ref java.lang.invoke.MethodType cache, string sig, [email protected] callerID)
 {
     if (cache == null)
     {
         DynamicLoadMethodTypeImpl(ref cache, sig, callerID);
     }
     return(cache);
 }
Example #23
0
 public static java.lang.Class DynamicClassLiteral(string clazz, [email protected] callerId)
 {
     Profiler.Count("DynamicClassLiteral");
     return(LoadTypeWrapper(clazz, callerId).ClassObject);
 }
Example #24
0
        private static void DynamicLoadMethodTypeImpl(ref java.lang.invoke.MethodType cache, string sig, [email protected] callerID)
        {
#if !FIRST_PASS
            try
            {
                ClassLoaderWrapper loader = ClassLoaderWrapper.FromCallerID(callerID);
                TypeWrapper[]      args   = loader.ArgTypeWrapperListFromSig(sig, LoadMode.LoadOrThrow);
                java.lang.Class[]  ptypes = new java.lang.Class[args.Length];
                for (int i = 0; i < ptypes.Length; i++)
                {
                    ptypes[i] = args[i].ClassObject;
                }
                Interlocked.CompareExchange(ref cache, java.lang.invoke.MethodType.methodType(loader.RetTypeWrapperFromSig(sig, LoadMode.LoadOrThrow).ClassObject, ptypes), null);
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
Example #25
0
        public static object DynamicGetfield(object obj, string name, string sig, RuntimeTypeHandle type, string clazz, [email protected] callerID)
        {
            Profiler.Count("DynamicGetfield");
            FieldWrapper fw = GetFieldWrapper(ClassLoaderWrapper.GetWrapperFromType(obj.GetType()), type, clazz, name, sig, false);

            java.lang.reflect.Field field = (java.lang.reflect.Field)fw.ToField(false);
            object val = field.get(obj, callerID);

            if (fw.FieldTypeWrapper.IsPrimitive)
            {
                val = JVM.Unbox(val);
            }
            return(val);
        }
Example #26
0
        private static java.lang.invoke.MethodHandle DynamicLoadMethodHandleImpl(int kind, string clazz, string name, string sig, [email protected] callerID)
        {
#if FIRST_PASS
            return(null);
#else
            java.lang.Class refc = LoadTypeWrapper(clazz, callerID).ClassObject;
            try
            {
                switch ((ClassFile.RefKind)kind)
                {
                case ClassFile.RefKind.getStatic:
                case ClassFile.RefKind.putStatic:
                case ClassFile.RefKind.getField:
                case ClassFile.RefKind.putField:
                    java.lang.Class type = ClassLoaderWrapper.FromCallerID(callerID).FieldTypeWrapperFromSig(sig, LoadMode.LoadOrThrow).ClassObject;
                    return(java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(callerID.getCallerClass(), kind, refc, name, type));

                default:
                    java.lang.invoke.MethodType mt = null;
                    DynamicLoadMethodType(ref mt, sig, callerID);
                    // HACK linkMethodHandleConstant is broken for MethodHandle.invoke[Exact]
                    if (kind == (int)ClassFile.RefKind.invokeVirtual && refc == CoreClasses.java.lang.invoke.MethodHandle.Wrapper.ClassObject)
                    {
                        switch (name)
                        {
                        case "invokeExact":
                            return(java.lang.invoke.MethodHandles.exactInvoker(mt));

                        case "invoke":
                            return(java.lang.invoke.MethodHandles.invoker(mt));
                        }
                    }
                    return(java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(callerID.getCallerClass(), kind, refc, name, mt));
                }
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
Example #27
0
    public static java.lang.Class defineClass(object thisUnsafe, string name, byte[] buf, int offset, int length, [email protected] callerID)
    {
#if FIRST_PASS
        return(null);
#else
        return(defineClass(thisUnsafe, name, buf, offset, length, callerID.getCallerClassLoader(), callerID.getCallerClass().pd));
#endif
    }
Example #28
0
				internal FrameState([email protected] callerID, int localRefSlot, int localRefIndex)
				{
					this.callerID = callerID;
					this.localRefSlot = localRefSlot;
					this.localRefIndex = localRefIndex;
				}
Example #29
0
        public static void DynamicPutstatic(object val, string name, string sig, RuntimeTypeHandle type, string clazz, [email protected] callerID)
        {
            Profiler.Count("DynamicPutstatic");
            FieldWrapper fw = GetFieldWrapper(null, type, clazz, name, sig, true);

            if (fw.IsFinal)
            {
                throw new java.lang.IllegalAccessError("Field " + fw.DeclaringType.Name + "." + fw.Name + " is final");
            }
            java.lang.reflect.Field field = (java.lang.reflect.Field)fw.ToField(false);
            if (fw.FieldTypeWrapper.IsPrimitive)
            {
                val = JVM.Box(val);
            }
            field.set(null, val, callerID);
        }
Example #30
0
			internal Exception Leave(FrameState prev)
			{
				// on the current (.NET 2.0 SP2) x86 JIT an explicit for loop is faster than Array.Clear() up to about 100 elements
				for (int i = 0; i < localRefIndex; i++)
				{
					active[i] = null;
				}
				while (--localRefSlot != prev.localRefSlot)
				{
					if (localRefs[localRefSlot] != null)
					{
						if (localRefs[localRefSlot].Length == LOCAL_REF_MAX_BUCKET_SIZE)
						{
							// if the bucket is totally allocated, we're assuming a leaky method so we throw the bucket away
							localRefs[localRefSlot] = null;
						}
						else
						{
							Array.Clear(localRefs[localRefSlot], 0, localRefs[localRefSlot].Length);
						}
					}
				}
				active = localRefs[localRefSlot];
				this.localRefIndex = prev.localRefIndex;
				this.callerID = prev.callerID;
				Exception x = pendingException;
				pendingException = null;
				return x;
			}
Example #31
0
 internal DynamicMethodBuilder(Type owner, string name, MethodType type, [email protected] callerID)
     : this(name, type, null, callerID, null, owner)
 {
 }