예제 #1
0
        public void SetBit(int index, TrileanValue expected)
        {
            var value = new Integer32Value(0);

            value.SetBit(index, expected);
            Assert.Equal(expected, value.GetBit(index));
        }
예제 #2
0
        /// <inheritdoc />
        public void WriteInteger32(int offset, Integer32Value value)
        {
            AssertOffsetValidity(offset, sizeof(uint));

            BinaryPrimitives.WriteUInt32LittleEndian(_memory.Span.Slice(offset, 4), value.U32);
            BinaryPrimitives.WriteUInt32LittleEndian(_knownBitMask.Span.Slice(offset, 4), value.Mask);
        }
예제 #3
0
        public void IsGreaterThan(string a, string b, bool signed, TrileanValue expected)
        {
            var value1 = new Integer32Value(a);
            var value2 = new Integer32Value(b);

            Assert.Equal(expected, value1.IsGreaterThan(value2, signed));
        }
예제 #4
0
        public void IsEqualTo(string a, string b, TrileanValue expected)
        {
            var value1 = new Integer32Value(a);
            var value2 = new Integer32Value(b);

            Assert.Equal(expected, value1.IsEqualTo(value2));
        }
예제 #5
0
        public void ReadStaticIntField()
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var fieldValue  = new Integer32Value(0x12345678);

            Verify(nameof(SimpleClass.StaticIntField), fieldValue, new I4Value(fieldValue.I32));
        }
예제 #6
0
 /// <inheritdoc />
 public void WriteInteger32(int offset, Integer32Value value)
 {
     if (IsKnown)
     {
         ReferencedMemory.WriteInteger32(CurrentOffset + offset, value);
     }
 }
예제 #7
0
 /// <inheritdoc />
 public void WriteInteger32(int offset, Integer32Value value)
 {
     if (IsKnown)
     {
         BasePointer.WriteInteger32(CurrentOffset + offset, value);
     }
 }
예제 #8
0
        public void Not(string input, string expected)
        {
            var value1 = new Integer32Value(input);

            value1.Not();

            Assert.Equal(new Integer32Value(expected), value1);
        }
예제 #9
0
        public void RemainderWithUnknownNumbersShouldNotResultInInfiniteLoop()
        {
            var value1 = new Integer32Value("11111111");
            var value2 = new Integer32Value(0, 0);

            value1.Remainder(value2);
            Assert.False(value1.IsKnown);
        }
예제 #10
0
        public void Add(string a, string b, string expected)
        {
            var value1 = new Integer32Value(a);
            var value2 = new Integer32Value(b);

            value1.Add(value2);

            Assert.Equal(new Integer32Value(expected), value1);
        }
예제 #11
0
        public void KnownValueSignUnsignedSame()
        {
            var value = new Integer32Value(0x12345678);

            Assert.True(value.IsKnown);
            Assert.Equal(Integer32Value.FullyKnownMask, value.Mask);
            Assert.Equal(0x12345678u, value.U32);
            Assert.Equal(0x12345678, value.I32);
        }
예제 #12
0
        public void ReadInteger64()
        {
            var ptrValue = new MemoryBlockValue(8);

            var value = new Integer32Value("00001111????0011001100??00??0101");

            ptrValue.WriteInteger32(0, value);
            Assert.Equal(value, ptrValue.ReadInteger32(0));
        }
예제 #13
0
        /// <summary>
        /// When the value is fully known, gets the raw integer value stored in this native integer as an int64.
        /// </summary>
        /// <returns>The integer, sign extended to a 64 bit integer.</returns>
        /// <exception cref="InvalidOperationException"></exception>
        public long ToKnownI64()
        {
            if (IsKnown)
            {
                throw new InvalidOperationException("Value is not fully known.");
            }

            return(_value switch
            {
                Integer32Value int32 => int32.I32,
                Integer64Value int64 => int64.I64,
                _ => throw new ArgumentOutOfRangeException()
            });
예제 #14
0
        public void WriteInteger32ShouldBeLittleEndian()
        {
            var ptrValue = new MemoryBlockValue(4);

            var value = new Integer32Value("00001111" + "????0011" + "001100??" + "00??0101");

            ptrValue.WriteInteger32(0, value);

            Assert.Equal(new Integer8Value("00??0101"), ptrValue.ReadInteger8(0));
            Assert.Equal(new Integer8Value("001100??"), ptrValue.ReadInteger8(1));
            Assert.Equal(new Integer8Value("????0011"), ptrValue.ReadInteger8(2));
            Assert.Equal(new Integer8Value("00001111"), ptrValue.ReadInteger8(3));
        }
예제 #15
0
        public void ReadInteger64()
        {
            using var memoryOwner = MemoryPool <byte> .Shared.Rent(8);

            using var bitmaskOwner = MemoryPool <byte> .Shared.Rent(8);

            var ptrValue = new MemoryPointerValue(memoryOwner.Memory, bitmaskOwner.Memory, true);

            var value = new Integer32Value("00001111????0011001100??00??0101");

            ptrValue.WriteInteger32(0, value);
            Assert.Equal(value, ptrValue.ReadInteger32(0));
        }
예제 #16
0
        public void ShallowCopyShouldReferenceSameArray()
        {
            var array = new ArrayValue(new IConcreteValue[]
            {
                new Integer32Value(0), new Integer32Value(1), new Integer32Value(2),
            });

            var copy = (ArrayValue)array.Copy();

            Assert.NotSame(array, copy);
            Assert.Equal((IEnumerable <IConcreteValue>)array, copy);

            copy[0] = new Integer32Value(1234);
            Assert.Equal(new Integer32Value(1234), array[0]);
        }
예제 #17
0
        public void WriteInteger32ShouldBeLittleEndian()
        {
            using var memoryOwner = MemoryPool <byte> .Shared.Rent(4);

            using var bitmaskOwner = MemoryPool <byte> .Shared.Rent(4);

            var ptrValue = new MemoryPointerValue(memoryOwner.Memory, bitmaskOwner.Memory, true);

            var value = new Integer32Value("00001111" + "????0011" + "001100??" + "00??0101");

            ptrValue.WriteInteger32(0, value);

            Assert.Equal(new Integer8Value("00??0101"), ptrValue.ReadInteger8(0));
            Assert.Equal(new Integer8Value("001100??"), ptrValue.ReadInteger8(1));
            Assert.Equal(new Integer8Value("????0011"), ptrValue.ReadInteger8(2));
            Assert.Equal(new Integer8Value("00001111"), ptrValue.ReadInteger8(3));
        }
예제 #18
0
        public void ReadStaticIntField()
        {
            var fieldValue = new Integer32Value(0x12345678);

            SetAndVerify(nameof(SimpleClass.StaticIntField), fieldValue, new I4Value(fieldValue.I32));
        }
예제 #19
0
        public void ReadFromInt32EnumShouldResultInI4()
        {
            var fieldValue = new Integer32Value(1);

            SetAndVerify(nameof(SimpleClass.StaticInt32Enum), fieldValue, new I4Value(fieldValue.I32));
        }
예제 #20
0
 /// <inheritdoc />
 public void WriteInteger32(int offset, Integer32Value value) => Contents.WriteInteger32(offset, value);
예제 #21
0
        public void IsNonZero(string input, TrileanValue expected)
        {
            var value = new Integer32Value(input);

            Assert.Equal(expected, value.IsNonZero);
        }
예제 #22
0
        public override void Execute(Context context, IEnumerable <MetadataMember> targets)
        {
            if (!context.IsReflectionCorlibSafe)
            {
                return;
            }
            foreach (var Method in targets.OfType <MethodDefinition>().Where(m => m.CilMethodBody is not null))
            {
                var Instructions = Method.CilMethodBody.Instructions;
                Method.CilMethodBody.ConstructSymbolicFlowGraph(out var DFG); /* Dfg Constructing. */
                for (int x = 0; x < Instructions.Count; x++)
                {
                    var Instr = Instructions[x];   /* Some People Make It Callvirt To Deafet Public tools :p */
                    if ((Instr.IsCode(CilCode.Call) || Instr.IsCode(CilCode.Callvirt)) &&
                        Instr.IsFromNS("System", "Math") && DFG.Nodes.Contains(Instr.Offset))
                    {
                        var CallNode = DFG.Nodes[Instr.Offset]
                                       .GetOrderedDependencies(DependencyCollectionFlags.IncludeStackDependencies)
                                       .ToList();

                        if (CallNode.Any(x => x.Contents.OpCode.OperandType == CilOperandType.InlineMethod))
                        {
                            continue;
                        }

                        var vm = new CilVirtualMachine(Method.CilMethodBody,
                                                       context.Module.Is32Module());
                        var ex = new CilExecutionContext(vm, vm.CurrentState, default);

                        foreach (var Dep in CallNode)
                        {
                            var CInstr = Dep.Contents;
                            vm.Dispatcher.Execute(ex, CInstr);
                            CInstr.Nop();
                        }
                        var ISlot = new object[((IMethodDescriptor)Instr.Operand)
                                               .Signature.GetTotalParameterCount()];
                        for (int i = 0; i < ISlot.Length; i++)
                        {
                            var Value = vm.CurrentState.Stack.Pop();
                            ISlot[i] = Value switch {
                                I4Value I4 => I4.I32,
                                I8Value I8 => I8.I64,
                                FValue F => F.F64,
                                Float32Value F32 => F32.F32,
                                Float64Value F64 => F64.F64,
                                Integer16Value I6 => I6.I16,
                                Integer32Value I32 => I32.I32,
                                Integer64Value I64 => I64.I64,
                                        _ => throw new NotSupportedException(nameof(Value))
                            };
                        }
                        var InvocationValue = context.ReflectionCorlib.ResolveMethod(((IMethodDescriptor)Instr.Operand).MetadataToken.ToInt32())
                                              .Invoke(null, ISlot);
                        Instr.OpCode = ((IMethodDescriptor)Instr.Operand).Signature.ReturnType.ElementType switch {
                            ElementType.I4 => CilOpCodes.Ldc_I4,
                            ElementType.I8 => CilOpCodes.Ldc_I8,
                            ElementType.R4 => CilOpCodes.Ldc_R4,
                            ElementType.R8 => CilOpCodes.Ldc_R8,
                            _ => CilOpCodes.Ldc_I4,
                        };
                        Instr.Operand = InvocationValue;
                    }
                }
            }
        }
    }