Esempio n. 1
0
 public ValueTypeFieldValueLocation(DmdType type, ValueLocation containingLocation, StructMirror structMirror, FieldInfoMirror field)
 {
     Type = type ?? throw new ArgumentNullException(nameof(type));
     this.containingLocation = containingLocation ?? throw new ArgumentNullException(nameof(containingLocation));
     this.field = field ?? throw new ArgumentNullException(nameof(field));
     structType = structMirror.Type;
     Debug.Assert(!field.IsStatic);
     Debug.Assert(field.DeclaringType == structType);
     Debug.Assert(containingLocation.Load() is StructMirror);
     valueIndex = GetValueIndex(field);
     if ((uint)valueIndex >= (uint)structMirror.Fields.Length)
     {
         throw new InvalidOperationException();
     }
 }
Esempio n. 2
0
        private void RegisterType(TypeMirror typeMirror)
        {
            if (!_types.ContainsKey(typeMirror.FullName))
            {
                _types.Add(typeMirror.FullName, new TypeSummary
                {
                    TypeMirror = typeMirror,
                });

                string typeName = typeMirror.Name;
                if (!string.IsNullOrEmpty(typeMirror.Namespace))
                {
                    typeName = typeMirror.Namespace + "." + typeMirror.Name;
                }
                logger.Trace("Loaded and registered Type: " + typeName);
            }
        }
        public PropertyValueReference(EvaluationContext ctx, PropertyInfoMirror property, object obj, TypeMirror declaringType, MethodMirror getter, Value[] indexerArgs) : base(ctx)
        {
            this.declaringType = declaringType;
            this.indexerArgs   = indexerArgs;
            this.property      = property;
            this.getter        = getter;
            this.obj           = obj;

            var objectMirror = obj as ObjectMirror;

            if (objectMirror != null)
            {
                EnsureContextHasDomain(objectMirror.Domain);
            }

            flags = GetFlags(property, getter);
        }
        T BuildAttribute <T> (CustomAttributeDataMirror attr)
        {
            List <object> args = new List <object> ();

            foreach (CustomAttributeTypedArgumentMirror arg in attr.ConstructorArguments)
            {
                object val = arg.Value;
                if (val is TypeMirror)
                {
                    // The debugger attributes that take a type as parameter of the constructor have
                    // a corresponding constructor overload that takes a type name. We'll use that
                    // constructor because we can't load target types in the debugger process.
                    // So what we do here is convert the Type to a String.
                    TypeMirror tm = (TypeMirror)val;
                    val = tm.FullName + ", " + tm.Assembly.ManifestModule.Name;
                }
                else if (val is EnumMirror)
                {
                    EnumMirror em = (EnumMirror)val;
                    val = em.Value;
                }
                args.Add(val);
            }
            Type   type = typeof(T);
            object at   = Activator.CreateInstance(type, args.ToArray());

            foreach (CustomAttributeNamedArgumentMirror arg in attr.NamedArguments)
            {
                object val     = arg.TypedValue.Value;
                string postFix = "";
                if (arg.TypedValue.ArgumentType == typeof(Type))
                {
                    postFix = "TypeName";
                }
                if (arg.Field != null)
                {
                    type.GetField(arg.Field.Name + postFix).SetValue(at, val);
                }
                else if (arg.Property != null)
                {
                    type.GetProperty(arg.Property.Name + postFix).SetValue(at, val, null);
                }
            }
            return((T)at);
        }
        protected Value InvokeStaticMethod([NotNull] TypeMirror type, string methodName, params Value[] parameters)
        {
            var result = Adaptor.Invocator.InvokeStaticMethod(Context, type, methodName, parameters);

            if (result == null)
            {
                var args = new string[2 + parameters.Length];
                args[0] = type.FullName;
                args[1] = methodName;
                Array.Copy(parameters, 0, args, 2, parameters.Length);
                // ReSharper disable FormatStringProblem
                myLogger.Warn("InvokeStaticMethod returned null for {0}.{1}({2})", args);
                // ReSharper restore FormatStringProblem
                return(null);
            }

            return(result.Result);
        }
        public PropertyValueReference(EvaluationContext ctx, PropertyInfoMirror property, object obj, TypeMirror declaringType, MethodMirror getter, Value[] indexerArgs) : base(ctx)
        {
            this.property      = property;
            this.obj           = obj;
            this.declaringType = declaringType;
            this.indexerArgs   = indexerArgs;

            flags = ObjectValueFlags.Property;
            if (property.GetSetMethod(true) == null)
            {
                flags |= ObjectValueFlags.ReadOnly;
            }

            if (getter.IsStatic)
            {
                flags |= ObjectValueFlags.Global;
            }
            if (getter.IsPublic)
            {
                flags |= ObjectValueFlags.Public;
            }
            else if (getter.IsPrivate)
            {
                flags |= ObjectValueFlags.Private;
            }
            else if (getter.IsFamily)
            {
                flags |= ObjectValueFlags.Protected;
            }
            else if (getter.IsFamilyAndAssembly)
            {
                flags |= ObjectValueFlags.Internal;
            }
            else if (getter.IsFamilyOrAssembly)
            {
                flags |= ObjectValueFlags.InternalProtected;
            }

            if (property.DeclaringType.IsValueType)
            {
                flags |= ObjectValueFlags.ReadOnly;                 // Setting property values on structs is not supported by sdb
            }
        }
Esempio n. 7
0
        void InitializeBreakpoints(TypeMirror monoType)
        {
            debuggerThread.VerifyAccess();
            Debug.Assert(monoType != null);
            if (monoType == null)
            {
                return;
            }
            var module = TryGetModuleCore_NoCreate(monoType.Module);

            if (module == null)
            {
                return;
            }

            var state = module.GetOrCreateData <TypeLoadBreakpointState>();

            state.OnTypeLoaded(monoType);
        }
        private string GetComponentName([CanBeNull] TypeMirror objectNamesType, Value componentValue)
        {
            if (objectNamesType != null)
            {
                try
                {
                    var result = InvokeStaticMethod(objectNamesType, "GetInspectorTitle", componentValue);
                    if (result is StringMirror stringMirror)
                    {
                        return(stringMirror.Value);
                    }
                }
                catch (Exception e)
                {
                    ourLogger.Error(e, "Unable to fetch object names for {0}", componentValue);
                }
            }

            return(componentValue.Type.Name);
        }
        protected override ValueReference GetMember(EvaluationContext ctx, object t, object co, string name)
        {
            TypeMirror type = (TypeMirror)t;

            while (type != null)
            {
                FieldInfoMirror field = type.GetField(name);
                if (field != null)
                {
                    return(new FieldValueReference(ctx, field, co, type));
                }
                PropertyInfoMirror prop = type.GetProperty(name);
                if (prop != null)
                {
                    return(new PropertyValueReference(ctx, prop, co, type, null));
                }
                type = type.BaseType;
            }
            return(null);
        }
Esempio n. 10
0
        MethodMirror GetMethodCore(DmdMethodBase method, MonoTypeLoader monoTypeLoader)
        {
            MethodMirror monoMethod;

            var mi = method as DmdMethodInfo;

            if ((object)mi != null && mi.IsConstructedGenericMethod)
            {
                if (toMonoMethod.TryGetValue(method, out monoMethod))
                {
                    return(monoMethod);
                }
                if (!engine.MonoVirtualMachine.Version.AtLeast(2, 24))
                {
                    throw new InvalidOperationException();
                }
                monoMethod = TryGetMethodCore2(mi.GetGenericMethodDefinition(), monoTypeLoader);
                if (monoMethod != null)
                {
                    var genArgs     = mi.GetGenericArguments();
                    var monoGenArgs = new TypeMirror[genArgs.Count];
                    for (int i = 0; i < monoGenArgs.Length; i++)
                    {
                        monoGenArgs[i] = MonoDebugTypeCreator.GetType(engine, genArgs[i], monoTypeLoader);
                    }
                    monoMethod           = monoMethod.MakeGenericMethod(monoGenArgs);
                    toMonoMethod[method] = monoMethod;
                    return(monoMethod);
                }
            }
            else
            {
                monoMethod = TryGetMethodCore2(method, monoTypeLoader);
                if (monoMethod != null)
                {
                    return(monoMethod);
                }
            }

            throw new InvalidOperationException();
        }
Esempio n. 11
0
 MethodMirror?GetCreateInstance(TypeMirror arrayType)
 {
     foreach (var method in arrayType.GetMethods())
     {
         if (method.Name != nameof(Array.CreateInstance))
         {
             continue;
         }
         var ps = method.GetParameters();
         if (ps.Length != 2)
         {
             continue;
         }
         if (ps[0].ParameterType.FullName != "System.Type" || ps[1].ParameterType.FullName != "System.Int32")
         {
             continue;
         }
         return(method);
     }
     return(null);
 }
        private static List <KeyValuePair <string, Mirror> > GetChildren(TypeMirror type, Guid filterGuid)
        {
            var children = new List <KeyValuePair <string, Mirror> >();

            do
            {
                if (filterGuid != EnumOnlyPropertiesFilter)
                {
                    children.AddRange(type.GetFields().Select(x => new KeyValuePair <string, Mirror>(x.Name, x)));
                }
                if (filterGuid != EnumOnlyFieldsFilter)
                {
                    children.AddRange(type.GetProperties().Select(x => new KeyValuePair <string, Mirror>(x.Name, x)));
                }

                type = type.BaseType;
            } while (type != null);

            children = children.Where(x => x.Key != null && x.Key.Length > 0 && x.Key[0] != '<').OrderBy(x => x.Key).ToList();
            return(children);
        }
        public Value RuntimeInvoke(MethodMirror method, object target, Value[] values)
        {
            if (values != null)
            {
                // Some arguments may need to be boxed
                ParameterInfoMirror[] mparams = method.GetParameters();
                if (mparams.Length != values.Length)
                {
                    throw new EvaluatorException("Invalid number of arguments when calling: " + method.Name);
                }

                for (int n = 0; n < mparams.Length; n++)
                {
                    TypeMirror tm = mparams [n].ParameterType;
                    if (tm.IsValueType || tm.IsPrimitive)
                    {
                        continue;
                    }
                    object     type          = Adapter.GetValueType(this, values [n]);
                    TypeMirror argTypeMirror = type as TypeMirror;
                    Type       argType       = type as Type;
                    if ((argTypeMirror != null && (argTypeMirror.IsValueType || argTypeMirror.IsPrimitive)) || (argType != null && (argType.IsValueType || argType.IsPrimitive)))
                    {
                        // A value type being assigned to a parameter which is not a value type. The value has to be boxed.
                        try {
                            values [n] = Thread.Domain.CreateBoxedValue(values [n]);
                        } catch (NotSupportedException) {
                            // This runtime doesn't support creating boxed values
                            throw new EvaluatorException("This runtime does not support creating boxed values.");
                        }
                    }
                }
            }

            MethodCall mc = new MethodCall(this, method, target, values);

            Adapter.AsyncExecute(mc, Options.EvaluationTimeout);
            return(mc.ReturnValue);
        }
        Location GetLocFromType(TypeMirror type, string file, int line)
        {
            Location target_loc = null;

            foreach (MethodMirror m in type.GetMethods())
            {
                foreach (Location l in m.Locations)
                {
                    if (System.IO.Path.GetFileName(l.SourceFile) == file && l.LineNumber == line)
                    {
                        target_loc = l;
                        break;
                    }
                }
                if (target_loc != null)
                {
                    break;
                }
            }

            return(target_loc);
        }
Esempio n. 15
0
        TypeMirror TryResolveType(TypeMirror monoType, DmdType realType)
        {
            var fullName = realType.FullName;

            if (fullName == null && realType.IsGenericType)
            {
                fullName = realType.GetGenericTypeDefinition().FullName;
            }
            if (string.IsNullOrEmpty(fullName))
            {
                return(null);
            }
            // This fails if fullName is a generic instantiated type and at least one generic argument
            // is a type in another assembly, eg. List<MyType>.
            var result = monoType.Module.Assembly.GetType(fullName);

            if (result != null)
            {
                return(result);
            }
            return(monoTypeLoader?.Load(monoType.Assembly, fullName));
        }
Esempio n. 16
0
        private Stream <CompilationMessage> ValidateInjectedTypes(VariableElement field)
        {
            TypeMirror fieldType = field.asType();

            if (InjectsAllowedType(fieldType))
            {
                return(Stream.empty());
            }

            if (InjectsRestrictedType(fieldType))
            {
                if (_ignoresWarnings)
                {
                    return(Stream.empty());
                }

//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                return(Stream.of(new ContextFieldWarning(field, "@%s usage warning: found unsupported restricted type <%s> on %s.\n" + "The procedure will not load unless declared via the configuration option 'dbms.security.procedures.unrestricted'.\n" + "You can ignore this warning by passing the option -A%s to the Java compiler", typeof(Context).FullName, fieldType.ToString(), FieldFullName(field), IGNORE_CONTEXT_WARNINGS_OPTION)));
            }

//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
            return(Stream.of(new ContextFieldError(field, "@%s usage error: found unknown type <%s> on field %s, expected one of: %s", typeof(Context).FullName, fieldType.ToString(), FieldFullName(field), JoinTypes(_supportedTypes))));
        }
        public override object CreateValue(EvaluationContext ctx, object type, params object[] args)
        {
            ctx.AssertTargetInvokeAllowed();

            SoftEvaluationContext cx = (SoftEvaluationContext)ctx;
            TypeMirror            t  = (TypeMirror)type;

            TypeMirror[] types = new TypeMirror [args.Length];
            for (int n = 0; n < args.Length; n++)
            {
                types [n] = ToTypeMirror(ctx, GetValueType(ctx, args [n]));
            }

            Value[] values = new Value[args.Length];
            for (int n = 0; n < args.Length; n++)
            {
                values[n] = (Value)args [n];
            }

            MethodMirror ctor = OverloadResolve(cx, ".ctor", t, types, true, true, true);

            return(t.NewInstance(cx.Thread, ctor, values));
        }
Esempio n. 18
0
        private Stream <CompilationMessage> ValidateReturnType(ExecutableElement method)
        {
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getCanonicalName method:
            string streamClassName = typeof(Stream).FullName;

            TypeMirror streamType       = _typeUtils.erasure(_elementUtils.getTypeElement(streamClassName).asType());
            TypeMirror returnType       = method.ReturnType;
            TypeMirror erasedReturnType = _typeUtils.erasure(returnType);

            TypeMirror voidType = _typeUtils.getNoType(TypeKind.VOID);

            if (_typeUtils.isSameType(returnType, voidType))
            {
                return(Stream.empty());
            }

            if (!_typeUtils.isSubtype(erasedReturnType, streamType))
            {
                return(Stream.of(new ReturnTypeError(method, "Return type of %s#%s must be %s", method.EnclosingElement.SimpleName, method.SimpleName, streamClassName)));
            }

            return(_recordVisitor.visit(returnType));
        }
Esempio n. 19
0
        static FieldInfoMirror[] GetFields(TypeMirror monoType, int length)
        {
            var fields = new FieldInfoMirror[length];
            int w      = 0;

            foreach (var f in monoType.GetFields())
            {
                if (f.IsStatic || f.IsLiteral)
                {
                    continue;
                }
                if (w >= fields.Length)
                {
                    return(null);
                }
                fields[w++] = f;
            }
            if (w != length)
            {
                return(null);
            }
            return(fields);
        }
 public FieldValueReference(EvaluationContext ctx, FieldInfoMirror field, object obj, TypeMirror declaringType, string vname, ObjectValueFlags vflags) : base(ctx)
 {
     this.field         = field;
     this.obj           = obj;
     this.declaringType = declaringType;
     this.vname         = vname;
     flags = vflags;
     if (field.IsStatic)
     {
         flags   |= ObjectValueFlags.Global;
         this.obj = null;
     }
     if (field.IsPublic)
     {
         flags |= ObjectValueFlags.Public;
     }
     else if (field.IsPrivate)
     {
         flags |= ObjectValueFlags.Private;
     }
     else if (field.IsFamily)
     {
         flags |= ObjectValueFlags.Protected;
     }
     else if (field.IsFamilyAndAssembly)
     {
         flags |= ObjectValueFlags.Internal;
     }
     else if (field.IsFamilyOrAssembly)
     {
         flags |= ObjectValueFlags.InternalProtected;
     }
     if (obj is PrimitiveValue)
     {
         flags |= ObjectValueFlags.ReadOnly;
     }
 }
        public override object ForceLoadType(EvaluationContext gctx, string typeName)
        {
            // Shortcut to avoid a target invoke in case the type is already loaded
            object t = GetType(gctx, typeName);

            if (t != null)
            {
                return(t);
            }

            SoftEvaluationContext ctx = (SoftEvaluationContext)gctx;

            if (!ctx.Options.AllowTargetInvoke)
            {
                return(null);
            }
            TypeMirror tm    = (TypeMirror)ctx.Thread.Type.GetTypeObject().Type;
            TypeMirror stype = ctx.Session.GetType("System.String");

            if (stype == null)
            {
                // If the string type is not loaded, we need to get it in another way
                StringMirror ss = ctx.Thread.Domain.CreateString("");
                stype = ss.Type;
            }
            TypeMirror[] ats = new TypeMirror[] { stype };
            MethodMirror met = OverloadResolve(ctx, "GetType", tm, ats, false, true, true);

            try {
                tm.InvokeMethod(ctx.Thread, met, new Value[] { (Value)CreateValue(ctx, typeName) });
            } catch {
                return(null);
            } finally {
                ctx.Session.StackVersion++;
            }
            return(GetType(ctx, typeName));
        }
 string GetInfo()
 {
     try {
         TypeMirror type = null;
         if (obj is ObjectMirror)
         {
             type = ((ObjectMirror)obj).Type;
         }
         else if (obj is TypeMirror)
         {
             type = (TypeMirror)obj;
         }
         else if (obj is StructMirror)
         {
             type = ((StructMirror)obj).Type;
         }
         return(string.Format("method {0} on object {1}",
                              function == null? "[null]" : function.FullName,
                              type == null? "[null]" : type.FullName));
     } catch (Exception ex) {
         LoggingService.LogError("Error getting info for SDB MethodCall", ex);
         return("");
     }
 }
        public override object RuntimeInvoke(EvaluationContext gctx, object targetType, object target, string methodName, object[] argTypes, object[] argValues)
        {
            SoftEvaluationContext ctx = (SoftEvaluationContext)gctx;

            ctx.AssertTargetInvokeAllowed();

            TypeMirror type = target != null ? ((ObjectMirror)target).Type : (TypeMirror)targetType;

            TypeMirror[] types = new TypeMirror [argTypes.Length];
            for (int n = 0; n < argTypes.Length; n++)
            {
                types [n] = ToTypeMirror(ctx, argTypes [n]);
            }

            Value[] values = new Value[argValues.Length];
            for (int n = 0; n < argValues.Length; n++)
            {
                values[n] = (Value)argValues [n];
            }

            MethodMirror method = OverloadResolve(ctx, methodName, type, types, target != null, target == null, true);

            return(ctx.RuntimeInvoke(method, target ?? targetType, values));
        }
Esempio n. 24
0
 public bool TryGetType(DmdType type, out TypeMirror monoType) => toMonoType.TryGetValue(type, out monoType);
 public FieldValueReference(EvaluationContext ctx, FieldInfoMirror field, object obj, TypeMirror declaringType)
     : this(ctx, field, obj, declaringType, null, ObjectValueFlags.Field)
 {
 }
Esempio n. 26
0
 private bool?Validate(TypeMirror typeMirror)
 {
     return(_allowedTypesValidator.test(typeMirror));
 }
Esempio n. 27
0
 static bool IsValueTypeOrPrimitive(TypeMirror type)
 {
     return(type != null && (type.IsValueType || type.IsPrimitive));
 }
Esempio n. 28
0
        public FieldValueReference(EvaluationContext ctx, FieldInfoMirror field, object obj, TypeMirror declaringType, string vname, ObjectValueFlags vflags, FieldReferenceBatch batch = null) : base(ctx)
        {
            this.field         = field;
            this.obj           = obj;
            this.declaringType = declaringType;
            this.vname         = vname;
            this.batch         = batch;
            flags = vflags;

            if (field.IsStatic)
            {
                this.obj = null;
            }

            var objectMirror = obj as ObjectMirror;

            if (objectMirror != null)
            {
                EnsureContextHasDomain(objectMirror.Domain);
            }

            flags |= GetFlags(field);

            if (obj is PrimitiveValue)
            {
                flags |= ObjectValueFlags.ReadOnly;
            }
        }
Esempio n. 29
0
 public bool TryGetType(TypeMirror monoType, out DmdType type) => toReflectionType.TryGetValue(monoType, out type);
Esempio n. 30
0
 public void Add(TypeMirror monoType, DmdType reflectionType)
 {
     toReflectionType[monoType] = reflectionType;
     toMonoType[reflectionType] = monoType;
 }