Beispiel #1
0
        public void CallVirtOverrideParameterizedMethod()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var stack       = ExecutionContext.ProgramState.Stack;

            // Create instance of the derived type.
            var objectTypeDef = (TypeDefinition)_type.Module.LookupMember(typeof(DerivedSimpleClass).MetadataToken);
            var objectTypeSig = objectTypeDef.ToTypeSignature();

            var objectRef = environment.ValueFactory.CreateObject(objectTypeSig, true);

            // Push object.
            stack.Push(environment.CliMarshaller.ToCliValue(objectRef, objectTypeSig));
            stack.Push(new I4Value(123));
            stack.Push(OValue.Null(environment.Is32Bit));

            // Invoke base method using virtual dispatch.
            var method = _type.Methods.First(m => m.Name == nameof(SimpleClass.VirtualParameterizedInstanceMethod));
            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(CilOpCodes.Callvirt, method));

            Assert.True(result.IsSuccess);
            Assert.Equal(
                objectTypeDef.Methods.First(m => m.Name == method.Name),
                ((HookedMethodInvoker)environment.MethodInvoker).LastInvokedMethod);
        }
Beispiel #2
0
        public void UndocumentedWriteStaticFromNullReferenceShouldNotThrow()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();

            VerifyUndocumentedWriteStatic(
                OValue.Null(environment.Is32Bit),
                new I4Value(0x12345678),
                new Integer32Value(0x12345678));
        }
Beispiel #3
0
        public void UndocumentedReadStaticFromNullReferenceShouldNotThrow()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();

            VerifyUndocumentedLoadStatic(
                nameof(SimpleClass.StaticIntField),
                OValue.Null(environment.Is32Bit),
                new Integer32Value(0x12345678),
                new I4Value(0x12345678));
        }
Beispiel #4
0
        public void LdLenOnNullShouldThrowNullReferenceException()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var stack       = ExecutionContext.ProgramState.Stack;

            stack.Push(OValue.Null(environment.Is32Bit));

            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(CilOpCodes.Ldlen));

            Assert.False(result.IsSuccess);
            Assert.IsAssignableFrom <NullReferenceException>(result.Exception);
        }
Beispiel #5
0
        public void CallVirtOnNullReferenceShouldThrow()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var stack       = ExecutionContext.ProgramState.Stack;

            stack.Push(OValue.Null(environment.Is32Bit));

            var method = _type.Methods.First(m => m.Name == nameof(SimpleClass.InstanceMethod));
            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(CilOpCodes.Callvirt, method));

            Assert.False(result.IsSuccess);
            Assert.IsAssignableFrom <NullReferenceException>(result.Exception);
        }
Beispiel #6
0
        public void ObjectWithNullComparison()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var marshaller  = environment.CliMarshaller;

            var stack = ExecutionContext.ProgramState.Stack;

            var stringValue = environment.ValueFactory.GetStringValue("Hello, world!");

            stack.Push(marshaller.ToCliValue(stringValue, environment.Module.CorLibTypeFactory.String));
            stack.Push(OValue.Null(environment.Is32Bit));

            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(CilOpCodes.Cgt_Un));

            Assert.True(result.IsSuccess);
            Assert.Equal(Trilean.True, ((I4Value)stack.Top).IsNonZero);
        }
Beispiel #7
0
        public void ReadFromNullReferenceShouldThrow()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var stack       = ExecutionContext.ProgramState.Stack;

            // Look up relevant metadata.
            var simpleClassType = LookupTestType(typeof(SimpleClass));
            var intField        = simpleClassType.Fields.First(f => f.Name == nameof(SimpleClass.IntField));

            // Push null.
            stack.Push(OValue.Null(environment.Is32Bit));

            // Test.
            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(CilOpCodes.Ldfld, intField));

            Assert.False(result.IsSuccess);
            Assert.IsAssignableFrom <NullReferenceException>(result.Exception);
        }
Beispiel #8
0
        public void DifferentObjectsOnStack()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var marshaller  = environment.CliMarshaller;

            var instruction = new CilInstruction(CilOpCodes.Beq, new CilOffsetLabel(0x1234));
            var stringValue = environment.ValueFactory.GetStringValue("Hello, World!");

            var stack = ExecutionContext.ProgramState.Stack;

            stack.Push(marshaller.ToCliValue(stringValue, environment.Module.CorLibTypeFactory.String));
            stack.Push(OValue.Null(environment.Is32Bit));

            var result = Dispatcher.Execute(ExecutionContext, instruction);

            Assert.True(result.IsSuccess);
            Assert.Equal(0, stack.Size);
            Assert.Equal(instruction.Offset + instruction.Size, ExecutionContext.ProgramState.ProgramCounter);
        }