Пример #1
0
        public void PersistentInterfaces()
        {
            var module  = ModuleDefinition.FromFile(typeof(InterfaceImplementations).Assembly.Location);
            var type    = module.TopLevelTypes.First(t => t.Name == nameof(InterfaceImplementations));
            var newType = RebuildAndLookup(type);

            Assert.Equal(new HashSet <string>(new[]
            {
                nameof(IInterface1), nameof(IInterface2),
            }), new HashSet <string>(newType.Interfaces.Select(i => i.Interface.Name)));
        }
Пример #2
0
        public void DetermineNestedPlatformDependentSize()
        {
            var module = ModuleDefinition.FromFile(typeof(MiscellaneousStructTest).Assembly.Location);
            var type   = (TypeDefinition)module.LookupMember(typeof(NestedPlatformDependentStruct).MetadataToken);

            var layout = type.GetImpliedMemoryLayout(IntPtr.Size == 4);

            Assert.Equal(IntPtr.Size == 4, layout.Is32Bit);
            Assert.True(layout.IsPlatformDependent);
            Assert.Equal((uint)Unsafe.SizeOf <NestedPlatformDependentStruct>(), layout.Size);
        }
Пример #3
0
        public void ReadMultipleProperties()
        {
            var module = ModuleDefinition.FromFile(typeof(MultipleProperties).Assembly.Location);
            var type   = module.TopLevelTypes.First(t => t.Name == nameof(MultipleProperties));

            Assert.Equal(new[]
            {
                nameof(MultipleProperties.ReadOnlyProperty), nameof(MultipleProperties.WriteOnlyProperty),
                nameof(MultipleProperties.ReadWriteProperty), "Item",
            }, type.Properties.Select(p => p.Name));
        }
Пример #4
0
        public void PersistentSingleEvent()
        {
            var module  = ModuleDefinition.FromFile(typeof(SingleEvent).Assembly.Location);
            var type    = module.TopLevelTypes.First(t => t.Name == nameof(SingleEvent));
            var newType = RebuildAndLookup(type);

            Assert.Equal(new[]
            {
                nameof(SingleEvent.SimpleEvent)
            }, newType.Events.Select(p => p.Name));
        }
        public void ReadTypesNested()
        {
            var module = ModuleDefinition.FromFile(typeof(TopLevelClass1).Assembly.Location);

            Assert.Equal(new HashSet <string>
            {
                "<Module>",
                nameof(TopLevelClass1),
                nameof(TopLevelClass2)
            }, module.TopLevelTypes.Select(t => t.Name));
        }
Пример #6
0
        public void ReadMethodOwner()
        {
            var module = ModuleDefinition.FromFile(typeof(GenericType <, ,>).Assembly.Location);
            var method = typeof(GenericType <, ,>).GetMethod("GenericMethodInGenericType");
            var token  = method.GetGenericArguments()[0].MetadataToken;

            var genericParameter = (GenericParameter)module.LookupMember(token);

            Assert.NotNull(genericParameter.Owner);
            Assert.Equal(method.MetadataToken, genericParameter.Owner.MetadataToken);
        }
Пример #7
0
        public void ResolveType()
        {
            var module = ModuleDefinition.FromFile(typeof(TopLevelClass1).Assembly.Location);

            var topLevelClass1 = new TypeReference(new AssemblyReference(module.Assembly),
                                                   typeof(TopLevelClass1).Namespace, typeof(TopLevelClass1).Name);

            var definition = _coreResolver.ResolveType(topLevelClass1);

            Assert.Equal((ITypeDefOrRef)topLevelClass1, definition, _comparer);
        }
Пример #8
0
        public void ReadMultipleFields()
        {
            var module = ModuleDefinition.FromFile(typeof(MultipleFields).Assembly.Location);
            var type   = module.TopLevelTypes.First(t => t.Name == nameof(MultipleFields));

            Assert.Equal(new[]
            {
                nameof(MultipleFields.IntField),
                nameof(MultipleFields.StringField),
                nameof(MultipleFields.TypeDefOrRefFieldType),
            }, type.Fields.Select(p => p.Name));
        }
Пример #9
0
        public void NewObjParameterLess()
        {
            var module = ModuleDefinition.FromFile(typeof(NewObjTest).Assembly.Location);
            var type   = (TypeDefinition)module.LookupMember(typeof(Constr).MetadataToken);
            var method = type.Methods.First(m => m.IsConstructor);

            var res = Dispatcher.Execute(ExecutionContext, new CilInstruction(CilOpCodes.Newobj, method));

            Assert.True(res.IsSuccess);
            Assert.Equal(1, ExecutionContext.ProgramState.Stack.Size);
            Assert.IsAssignableFrom <OValue>(ExecutionContext.ProgramState.Stack.Top);
        }
Пример #10
0
        public void ReadMultipleEvents()
        {
            var module = ModuleDefinition.FromFile(typeof(MultipleEvents).Assembly.Location);
            var type   = module.TopLevelTypes.First(t => t.Name == nameof(MultipleEvents));

            Assert.Equal(new[]
            {
                nameof(MultipleEvents.Event1),
                nameof(MultipleEvents.Event2),
                nameof(MultipleEvents.Event3),
            }, type.Events.Select(p => p.Name));
        }
Пример #11
0
        public void NormalMethodCallShouldBeUserDefined(TrileanValue purity)
        {
            _classifier.DefaultMethodCallPurity = purity;

            var module    = ModuleDefinition.FromFile(typeof(Math).Assembly.Location);
            var mathType  = (TypeDefinition)module.LookupMember(typeof(Math).MetadataToken);
            var sinMethod = mathType.Methods.First(m => m.Name == nameof(Math.Sin));

            var instruction = new CilInstruction(CilOpCodes.Call, sinMethod);

            Assert.Equal(purity, _classifier.IsPure(instruction));
        }
        public void ReadNormalMethod()
        {
            var module = ModuleDefinition.FromFile(typeof(SingleMethod).Assembly.Location);
            var method = (MethodDefinition)module.LookupMember(
                typeof(SingleMethod).GetMethod(nameof(SingleMethod.VoidParameterlessMethod)).MetadataToken);

            Assert.False(method.IsGetMethod);
            Assert.False(method.IsSetMethod);
            Assert.False(method.IsAddMethod);
            Assert.False(method.IsRemoveMethod);
            Assert.False(method.IsFireMethod);
        }
Пример #13
0
        public void LookupModuleReference()
        {
            var module = ModuleDefinition.FromFile(Path.Combine("Resources", "Manifest.exe"));
            var member = module.LookupMember(new MetadataToken(TableIndex.ModuleRef, 1));

            Assert.IsAssignableFrom <ModuleReference>(member);

            var moduleRef = (ModuleReference)member;

            Assert.Equal("MyModel.netmodule", moduleRef.Name);
            Assert.Same(module.ModuleReferences[0], moduleRef);
        }
Пример #14
0
        public void PersistentFieldSignature()
        {
            var module = ModuleDefinition.FromFile(typeof(SingleField).Assembly.Location);
            var field  = (FieldDefinition)module.LookupMember(
                typeof(SingleField).GetField(nameof(SingleField.IntField)).MetadataToken);

            field.Signature = FieldSignature.CreateInstance(module.CorLibTypeFactory.Byte);

            var newField = RebuildAndLookup(field);

            Assert.True(newField.Signature.FieldType.IsTypeOf("System", "Byte"), "Field type should be System.Byte");
        }
Пример #15
0
        public void BlacklistedMethodCallShouldBeConsideredImpure()
        {
            var module    = ModuleDefinition.FromFile(typeof(Math).Assembly.Location);
            var mathType  = (TypeDefinition)module.LookupMember(typeof(Stream).MetadataToken);
            var sinMethod = mathType.Methods.First(m => m.Name == nameof(Stream.Write));

            _classifier.KnownImpureMethods.Add(sinMethod);

            var instruction = new CilInstruction(CilOpCodes.Call, sinMethod);

            Assert.Equal(Trilean.False, _classifier.IsPure(instruction));
        }
Пример #16
0
        public void ReadWriteOnlyPropertySemantics()
        {
            var module   = ModuleDefinition.FromFile(typeof(MultipleProperties).Assembly.Location);
            var type     = module.TopLevelTypes.First(t => t.Name == nameof(MultipleProperties));
            var property = type.Properties.First(m => m.Name == nameof(MultipleProperties.WriteOnlyProperty));

            Assert.Single(property.Semantics);
            Assert.Equal(MethodSemanticsAttributes.Setter, property.Semantics[0].Attributes);
            Assert.Same(property, property.Semantics[0].Association);
            Assert.Equal("set_WriteOnlyProperty", property.Semantics[0].Method.Name);
            Assert.NotNull(property.SetMethod);
            Assert.Null(property.GetMethod);
        }
Пример #17
0
        public void ReadSingleGenericParameterConstraint()
        {
            var module = ModuleDefinition.FromFile(typeof(NonGenericType).Assembly.Location);
            var token  = typeof(NonGenericType)
                         .GetMethod(nameof(NonGenericType.GenericMethodWithConstraints))
                         .GetGenericArguments()[0]
                         .MetadataToken;

            var genericParameter = (GenericParameter)module.LookupMember(token);

            Assert.Single(genericParameter.Constraints);
            Assert.Equal(nameof(IFoo), genericParameter.Constraints[0].Constraint.Name);
        }
Пример #18
0
        public void ReadMethod()
        {
            var module = ModuleDefinition.FromFile(typeof(GenericsTestClass).Assembly.Location);
            var method = (MethodDefinition)module.LookupMember(typeof(GenericsTestClass)
                                                               .GetMethod(nameof(GenericsTestClass.MethodInstantiationFromGenericType)).MetadataToken);

            var call = method.CilMethodBody.Instructions.First(i => i.OpCode.Code == CilCode.Call);

            Assert.IsAssignableFrom <MethodSpecification>(call.Operand);

            var methodSpec = (MethodSpecification)call.Operand;

            Assert.Equal("GenericMethodInGenericType", methodSpec.Method.Name);
        }
Пример #19
0
        private static CustomAttribute GetCustomAttributeTestCase(string methodName, bool rebuild = false)
        {
            var module    = ModuleDefinition.FromFile(typeof(CustomAttributesTestClass).Assembly.Location);
            var type      = module.TopLevelTypes.First(t => t.Name == nameof(CustomAttributesTestClass));
            var method    = type.Methods.First(m => m.Name == methodName);
            var attribute = method.CustomAttributes
                            .First(c => c.Constructor.DeclaringType.Name == nameof(TestCaseAttribute));

            if (rebuild)
            {
                attribute = RebuildAndLookup(attribute);
            }
            return(attribute);
        }
Пример #20
0
        public void PersistentConstructor()
        {
            var module = ModuleDefinition.FromFile(typeof(CustomAttributesTestClass).Assembly.Location);

            using var stream = new MemoryStream();
            module.Write(stream);

            module = ModuleDefinition.FromReader(new ByteArrayReader(stream.ToArray()));

            var type = module.TopLevelTypes.First(t => t.Name == nameof(CustomAttributesTestClass));

            Assert.All(type.CustomAttributes, a =>
                       Assert.Equal(nameof(TestCaseAttribute), a.Constructor.DeclaringType.Name));
        }
Пример #21
0
        public void ReadFieldRva()
        {
            var module = ModuleDefinition.FromFile(typeof(InitialValues).Assembly.Location);
            var field  = module
                         .TopLevelTypes.First(t => t.Name == nameof(InitialValues))
                         .Fields.First(f => f.Name == nameof(InitialValues.ByteArray));

            var initializer = field.FindInitializerField();

            Assert.NotNull(initializer.FieldRva);
            Assert.IsAssignableFrom <IReadableSegment>(initializer.FieldRva);

            Assert.Equal(InitialValues.ByteArray, ((IReadableSegment)initializer.FieldRva).ToArray());
        }
Пример #22
0
 public static void DiscoverMethod(string[] args, Logger Log)
 {
     foreach (var Type in ModuleDefinition.FromFile(args[0]).GetAllTypes())
     {
         foreach (var Method in Type.Methods)
         {
             if (Method.Name == args[1] && Method.Parameters.Count == int.Parse(args[2]))
             {
                 DecMethod = Method;
                 Log.Info($"Decryption Method Found Params : {DecMethod.Resolve().Parameters.Count} :D");
             }
         }
     }
 }
Пример #23
0
 private static void ProcessExecutable(string inputFile)
 {
     Logger.Info($"Processing executable: {Path.GetFileName(inputFile)}");
     try
     {
         var module    = ModuleDefinition.FromFile(inputFile);
         var extractor = new ExtractorNew(module);
         extractor.Run();
     }
     catch (Exception e)
     {
         Logger.Error(e.Message);
     }
 }
Пример #24
0
        public void PersistentName()
        {
            const string newName = "NewName";

            var module = ModuleDefinition.FromFile(typeof(SingleField).Assembly.Location);
            var type   = module.TopLevelTypes.First(t => t.Name == nameof(SingleField));
            var field  = type.Fields[0];

            type.Fields[0].Name = newName;

            var newField = RebuildAndLookup(field);

            Assert.Equal(newName, newField.Name);
        }
Пример #25
0
        private static MethodDefinition LookupMethod(string methodName, bool rebuild)
        {
            var module = ModuleDefinition.FromFile(typeof(SecurityAttributes).Assembly.Location);

            if (rebuild)
            {
                var stream = new MemoryStream();
                module.Write(stream);
                module = ModuleDefinition.FromReader(ByteArrayDataSource.CreateReader(stream.ToArray()));
            }

            var type = module.TopLevelTypes.First(t => t.Name == nameof(SecurityAttributes));

            return(type.Methods.First(m => m.Name == methodName));
        }
Пример #26
0
        public void ReadMultipleMethods()
        {
            var module = ModuleDefinition.FromFile(typeof(MultipleMethods).Assembly.Location);
            var type   = module.TopLevelTypes.First(t => t.Name == nameof(MultipleMethods));

            Assert.Equal(new[]
            {
                ".ctor",
                nameof(MultipleMethods.VoidParameterlessMethod),
                nameof(MultipleMethods.IntParameterlessMethod),
                nameof(MultipleMethods.TypeDefOrRefParameterlessMethod),
                nameof(MultipleMethods.SingleParameterMethod),
                nameof(MultipleMethods.MultipleParameterMethod),
            }, type.Methods.Select(p => p.Name));
        }
Пример #27
0
        private void Verify(string fieldName, ICliValue newValue, IConcreteValue expectedValue)
        {
            var module = ModuleDefinition.FromFile(typeof(SimpleClass).Assembly.Location);
            var type   = (TypeDefinition)module.LookupMember(typeof(SimpleClass).MetadataToken);
            var field  = type.Fields.First(f => f.Name == fieldName);

            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var staticField = environment.StaticFieldFactory.Get(field);

            ExecutionContext.ProgramState.Stack.Push(newValue);
            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(CilOpCodes.Stsfld, field));

            Assert.True(result.IsSuccess);
            Assert.Equal(expectedValue, staticField.Value);
        }
Пример #28
0
        public void ReadMultipleGenericParameterConstraints()
        {
            var module = ModuleDefinition.FromFile(typeof(NonGenericType).Assembly.Location);
            var token  = typeof(NonGenericType)
                         .GetMethod(nameof(NonGenericType.GenericMethodWithConstraints))
                         .GetGenericArguments()[1]
                         .MetadataToken;

            var genericParameter = (GenericParameter)module.LookupMember(token);

            Assert.Equal(new[]
            {
                nameof(IFoo),
                nameof(IBar)
            }, genericParameter.Constraints.Select(c => c.Constraint.Name));
        }
Пример #29
0
        public void ReadGenericTypeInstantiation()
        {
            var module    = ModuleDefinition.FromFile(typeof(GenericsTestClass).Assembly.Location);
            var fieldType = module
                            .TopLevelTypes.First(t => t.Name == nameof(GenericsTestClass))
                            .Fields.First(f => f.Name == nameof(GenericsTestClass.GenericField))
                            .Signature.FieldType;

            Assert.IsAssignableFrom <GenericInstanceTypeSignature>(fieldType);
            var genericType = (GenericInstanceTypeSignature)fieldType;

            Assert.Equal("GenericType`3", genericType.GenericType.Name);
            Assert.Equal(new[]
            {
                "System.String", "System.Int32", "System.Object"
            }, genericType.TypeArguments.Select(a => a.FullName));
        }
Пример #30
0
        public void GetReadParametersInStaticContextShouldStartAtZeroIndex()
        {
            var module = ModuleDefinition.FromFile(typeof(TestClass).Assembly.Location);
            var type   = (TypeDefinition)module.LookupMember(typeof(TestClass).MetadataToken);
            var method = type.Methods
                         .First(m => m.Name == nameof(TestClass.StaticMethod));

            var architecture = new CilArchitecture(method.CilMethodBody);

            var readVariables = new IVariable[1];

            architecture.GetReadVariables(new CilInstruction(CilOpCodes.Ldarg_0), readVariables);

            Assert.Equal(new[] { method.Parameters[0] }, readVariables
                         .Cast <CilParameter>()
                         .Select(p => p.Parameter));
        }