Example #1
0
        /* Extra tests for Iced branch patching. */
        private void BuildAddWithBranch(IMemoryAllocator alloc)
        {
            int wordSize = IntPtr.Size;

            string[] addFunction = new string[]
            {
                $"{_use32}",
                "jmp actualfunction",

                // Section of NOPs, iced must successfully manage to patch the jump over these.
                $"nop",
                $"nop",
                $"nop",
                $"nop",
                $"nop",
                $"nop",
                $"nop",
                $"nop",
                $"nop",
                $"nop",
                $"actualfunction:",
                $"mov {_eax}, [{_esp} + {wordSize * 1}]", // Left Parameter
                $"mov {_ecx}, [{_esp} + {wordSize * 2}]", // Right Parameter
                $"add {_eax}, {_ecx}",

                $"ret"
            };

            var result = _assembler.Assemble(addFunction);

            AddWithBranch = alloc.Allocate(result.Length);
            _memory.WriteRaw(AddWithBranch, result);
        }
Example #2
0
        /* Constructor and Destructor */

        public NativeCalculator(IMemoryAllocator alloc = null)
        {
            alloc ??= new ReloadedMemoryAllocator();
            BuildAdd(alloc);
            BuildSubtract(alloc);
            BuildDivide(alloc);
            BuildMultiply(alloc);
            BuildVTable();

            BuildAddWithBranch(alloc);
        }
Example #3
0
        public IbmSeries1Processor(IMemoryBlock memory, IMemoryAllocator registers)
        {
            Memory = memory;
            Writer = new MemoryWriter(Memory);
            Reader = new MemoryReader(Memory);

            var levelBlocks = new List <LevelBlock>();

            for (int level = 0; level < LevelCount; level++)
            {
                levelBlocks.Add(new LevelBlock(level));
            }

            Levels = levelBlocks;
        }
Example #4
0
 /// <summary>
 /// Reads a single .NET structure at the provided offset.
 /// </summary>
 /// <param name="self">The base pointer value to read from.</param>
 /// <param name="offset">The offset to start reading.</param>
 /// <param name="memoryAllocator">The memory allocator responsible for managing type layouts.</param>
 /// <param name="typeLayout">The type layout to read.</param>
 /// <returns>The read structure.</returns>
 /// <exception cref="ArgumentOutOfRangeException">
 /// Occurs when the offset does not fall within the memory range.
 /// </exception>
 public static IConcreteValue ReadStruct(this IPointerValue self, int offset, IMemoryAllocator memoryAllocator, TypeMemoryLayout typeLayout)
 {
     return(typeLayout.Type.ToTypeSignature().ElementType switch
     {
         ElementType.Boolean => self.ReadInteger8(offset),
         ElementType.Char => self.ReadInteger16(offset),
         ElementType.I1 => self.ReadInteger8(offset),
         ElementType.U1 => self.ReadInteger8(offset),
         ElementType.I2 => self.ReadInteger16(offset),
         ElementType.U2 => self.ReadInteger16(offset),
         ElementType.I4 => self.ReadInteger32(offset),
         ElementType.U4 => self.ReadInteger32(offset),
         ElementType.I8 => self.ReadInteger64(offset),
         ElementType.U8 => self.ReadInteger64(offset),
         ElementType.R4 => self.ReadFloat32(offset),
         ElementType.R8 => self.ReadFloat64(offset),
         ElementType.ValueType => self.ReadStructSlow(offset, memoryAllocator, typeLayout),
         ElementType.I => self.Is32Bit ? (IntegerValue)self.ReadInteger32(offset) : self.ReadInteger64(offset),
         ElementType.U => self.Is32Bit ? (IntegerValue)self.ReadInteger32(offset) : self.ReadInteger64(offset),
         ElementType.Enum => ReadEnumValue(self, offset, memoryAllocator, typeLayout),
         _ => new UnknownValue()
     });
Example #5
0
        private void BuildSubtract(IMemoryAllocator alloc)
        {
            int wordSize = IntPtr.Size;

            string[] subtractFunction = new string[]
            {
                $"{_use32}",
                $"push {_ebp}",
                $"mov {_ebp}, {_esp}",

                $"mov {_eax}, [{_ebp} + {wordSize * 2}]", // Left Parameter
                $"mov {_ecx}, [{_ebp} + {wordSize * 3}]", // Right Parameter
                $"sub {_eax}, {_ecx}",

                $"pop {_ebp}",
                $"ret"
            };

            var result = _assembler.Assemble(subtractFunction);

            Subtract = alloc.Allocate(result.Length);
            _memory.WriteRaw(Subtract, result);
        }
Example #6
0
        private void BuildMultiply(IMemoryAllocator alloc)
        {
            int wordSize = IntPtr.Size;

            string[] multiplyFunction = new string[]
            {
                $"{_use32}",
                $"push {_ebp}",
                $"mov {_ebp}, {_esp}",

                $"mov {_eax}, [{_ebp} + {wordSize * 2}]", // Left Parameter
                $"mov {_ecx}, [{_ebp} + {wordSize * 3}]", // Right Parameter
                $"imul {_eax}, {_ecx}",

                $"pop {_ebp}",
                $"ret"
            };

            var result = _assembler.Assemble(multiplyFunction);

            Multiply = alloc.Allocate(result.Length);
            _memory.WriteRaw(Multiply, result);
        }
Example #7
0
        private void BuildDivide(IMemoryAllocator alloc)
        {
            int wordSize = IntPtr.Size;

            string[] divideFunction = new string[]
            {
                $"{_use32}",
                $"push {_ebp}",
                $"mov {_ebp}, {_esp}",

                $"mov {_edx}, 0",                         // Ignore the upper bits for testing.
                $"mov {_eax}, [{_ebp} + {wordSize * 2}]", // Left Parameter
                $"mov {_ecx}, [{_ebp} + {wordSize * 3}]", // Right Parameter
                $"idiv {_ecx}",

                $"pop {_ebp}",
                $"ret"
            };

            var result = _assembler.Assemble(divideFunction);

            Divide = alloc.Allocate(result.Length);
            _memory.WriteRaw(Divide, result);
        }
Example #8
0
 public MemoryNode(IMemoryAllocator memoryAllocator)
 {
     _memoryAllocator = memoryAllocator;
 }
Example #9
0
 /// <summary>
 /// Creates a new low level emulated object.
 /// </summary>
 /// <param name="memoryAllocator">The object responsible for memory management in the virtual machine.</param>
 /// <param name="valueType">The type of the object.</param>
 /// <param name="contents">The raw contents of the object.</param>
 public LleObjectValue(IMemoryAllocator memoryAllocator, TypeSignature valueType, MemoryPointerValue contents)
 {
     Type             = valueType;
     _memoryAllocator = memoryAllocator ?? throw new ArgumentNullException(nameof(memoryAllocator));
     Contents         = contents ?? throw new ArgumentNullException(nameof(contents));
 }
Example #10
0
 public VMHeap(ILContext context, IMemoryAllocator allocator)
 {
     _context   = context;
     _allocator = allocator;
 }
Example #11
0
 /// <summary>
 /// Creates a new structure value.
 /// </summary>
 /// <param name="memoryAllocator">The object responsible for memory management in the virtual machine.</param>
 /// <param name="valueType">The type of the object.</param>
 /// <param name="contents">The raw contents of the structure.</param>
 public StructValue(IMemoryAllocator memoryAllocator, TypeSignature valueType, MemoryPointerValue contents)
     : base(memoryAllocator, valueType, contents)
 {
 }
 public SimpleMemoryPool(IMemoryAllocator allocator)
 {
     _allocator = allocator;
 }
Example #13
0
 public StaticMemoryAllocator(IMemoryAllocator allocator, long size)
 {
     Allocator = allocator;
     Size      = size;
 }
Example #14
0
 public static StaticMemoryAllocator ToStatic(this IMemoryAllocator allocator, long size)
 => new StaticMemoryAllocator(allocator, size);