Exemplo n.º 1
0
 private BlockWorkitem CreateWorkItem(Address addr, ProcessorState state)
 {
     return new BlockWorkitem(
         scanner, 
         prog,
         state,
         addr);
 }
Exemplo n.º 2
0
 public IEnumerable<RtlInstructionCluster> CreateRewriter(ImageReader rdr, ProcessorState state, Frame frame, IRewriterHost host)
 {
     var linAddr = rdr.Address.ToLinear();
     RtlTrace trace;
     if (!rewriters.Traces.TryGetValue(rdr.Address, out trace))
         NUnit.Framework.Assert.Fail(string.Format("Unexpected request for a rewriter at address {0}", rdr.Address));
     return trace;
 }
Exemplo n.º 3
0
 public override SystemService FindService(int vector, ProcessorState state)
 {
     if (TypeLibs.Length == 0)
         return null;
     SystemService svc;
     this.TypeLibs[0].ServicesByVector.TryGetValue(vector&0xFFFF , out svc);
     return svc;
 }
Exemplo n.º 4
0
		public override SystemService FindService(int vector, ProcessorState state)
		{
			foreach (SystemService svc in realModeServices)
			{
				if (svc.SyscallInfo.Matches(vector, state))
					return svc;
			}
			return null;
		}
Exemplo n.º 5
0
 public void Setup()
 {
     arch = new IntelArchitecture(ProcessorMode.Protected32);
     m = new ProcedureBuilder();
     state = arch.CreateProcessorState();
     expSimp = new ExpressionSimplifier(
             new IntelArchitecture(ProcessorMode.Protected32).CreateProcessorState());
     SCZO = m.Frame.EnsureFlagGroup((uint)(FlagM.SF | FlagM.CF | FlagM.ZF | FlagM.OF), "SCZO", PrimitiveType.Byte);
     host = new BackwalkerHost();
 }
Exemplo n.º 6
0
		public EntryPoint(Address addr, string name, ProcessorState state)
		{
            if (addr == null)
                throw new ArgumentNullException("addr");
            if (state == null)
				throw new ArgumentNullException("state");
			this.Address = addr;
			this.Name = name;
			this.ProcessorState = state;
		}
Exemplo n.º 7
0
 public List<Address> Build(Address addrTable, Address addrFrom, ProcessorState state)
 {
     bw = new Backwalker(this, null, null);
     if (bw == null)
         return null;
     List<BackwalkOperation> operations = bw.BackWalk(null);
     if (operations == null)
         return PostError("Unable to determine limit", addrFrom, addrTable);
     return BuildAux(bw, addrFrom, state);
 }
Exemplo n.º 8
0
 public override SystemService FindService(int vector, ProcessorState state)
 {
     EnsureTypeLibraries();
     return this.typelibs
         .Where(t => t.ServicesByVector != null && t.ServicesByVector.Count > 0)
         .SelectMany(t => t.ServicesByVector)
         .Where(svc => svc.Value.SyscallInfo.Matches(vector, state))
         .Select(svc => svc.Value)
         .FirstOrDefault();
 }
Exemplo n.º 9
0
 protected Address ReadSegmentedCodeAddress(int byteSize, ImageReader rdr, ProcessorState state)
 {
     if (byteSize == PrimitiveType.Word16.Size)
     {
         return Address.SegPtr(state.GetRegister(Registers.cs).ToUInt16(), rdr.ReadLeUInt16());
     }
     else
     {
         ushort off = rdr.ReadLeUInt16();
         ushort seg = rdr.ReadLeUInt16();
         return Address.SegPtr(seg, off);
     }
 }
Exemplo n.º 10
0
 public override SystemService FindService(RtlInstruction rtl, ProcessorState state)
 {
     if (!a6Pattern.Match(rtl))
         return null;
     var reg = ((Identifier) a6Pattern.CapturedExpressions("addrReg")).Storage as RegisterStorage;
     var offset = ((Constant) a6Pattern.CapturedExpressions("offset")).ToInt32();
     if (reg != Registers.a6)
         return null;
     if (funcs == null)
         funcs = LoadFuncs();
     SystemService svc;
     return funcs.TryGetValue(offset, out svc) ? svc : null;
 }
Exemplo n.º 11
0
 public override SystemService FindService(int vector, ProcessorState state)
 {
     switch (vector)
     {
     // http://www.riscos.com/support/developers/prm/errors.html#89849
     case 0x2B:
         return new SystemService
         {
             Name = "OS_GenerateError",
             Characteristics = new ProcedureCharacteristics {
                 Terminates = true,
             },
             Signature = new ProcedureSignature(null,
                 new Identifier("r0", PrimitiveType.Pointer32, A32Registers.r0))
         };
     }
     throw new NotSupportedException(string.Format("Unknown RiscOS vector &{0:X}.", vector)); 
 }
Exemplo n.º 12
0
        public List<Address> BuildAux(Backwalker bw, Address addrFrom, ProcessorState state)
        {
            int limit = 0;
            int[] permutation = null;
            foreach (BackwalkOperation op in bw.Operations)
            {
                BackwalkError err = op as BackwalkError;
                if (err != null)
                {
                    return PostError(err.ErrorMessage, addrFrom, bw.VectorAddress);
                }
                var deref = op as BackwalkDereference;
                if (deref != null)
                {
                    permutation = BuildMapping(deref, limit);
                }
                limit = op.Apply(limit);
            }
            if (limit == 0)
                return PostError("Unable to determine limit", addrFrom, bw.VectorAddress);

            return BuildTable(bw.VectorAddress, limit, permutation, bw.Stride, state);
        }
Exemplo n.º 13
0
 public override BlockWorkitem CreateBlockWorkItem(Address addrStart, Procedure proc, ProcessorState state)
 {
     Test_LastBlockWorkitem = base.CreateBlockWorkItem(addrStart, proc, state);
     return Test_LastBlockWorkitem;
 }
Exemplo n.º 14
0
 public abstract Address ReadCodeAddress(int byteSize, ImageReader rdr, ProcessorState state);
Exemplo n.º 15
0
 public IEnumerable<RtlInstructionCluster> CreateRewriter(ImageReader rdr, ProcessorState state, Frame frame, IRewriterHost host)
 {
     return new Pdp11Rewriter(this, new Pdp11Disassembler(rdr, this), frame);
 }
Exemplo n.º 16
0
        /// <summary>
        /// Performs a scan of the blocks that constitute a procedure named <paramref name="procedureName"/>
        /// </summary>
        /// <param name="addr">Address of the code from which we will start scanning.</param>
        /// <param name="procedureName"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ProcedureBase ScanProcedure(Address addr, string procedureName, ProcessorState state)
        {
            TerminateAnyBlockAt(addr);
            var trampoline = GetTrampoline(addr);
            if (trampoline != null)
                return trampoline;
            var imp = GetImportedProcedure(addr, addr);
            if (imp != null)
                return imp;
            Procedure proc = EnsureProcedure(addr, procedureName);
            if (visitedProcs.Contains(proc))
                return proc;

            visitedProcs.Add(proc);
            Debug.WriteLineIf(trace.TraceInfo, string.Format("Scanning procedure at {0}", addr));

            //$REFACTOR: make the stack explicit?
            var oldQueue = queue;
            queue = new PriorityQueue<WorkItem>();
            var st = state.Clone();
            st.OnProcedureEntered();
            var sp = proc.Frame.EnsureRegister(program.Architecture.StackRegister);
            st.SetValue(sp, proc.Frame.FramePointer);
            var block = EnqueueJumpTarget(addr, addr, proc, st);
            proc.ControlGraph.AddEdge(proc.EntryBlock, block);
            ProcessQueue();
            queue = oldQueue;

            // Add <stackpointer> := fp explicitly to the starting block.
            proc.EntryBlock.Succ[0].Statements.Insert(0, addr.ToLinear(), new Assignment(sp, proc.Frame.FramePointer));
            return proc;
        }
Exemplo n.º 17
0
        public Block EnqueueJumpTarget(Address addrSrc, Address addrDest, Procedure proc, ProcessorState state)
        {
            Procedure procDest;
            Block block = FindExactBlock(addrDest);
            if (block == null)
            {
                // Target wasn't a block before. Make sure it exists.
                block = FindContainingBlock(addrDest);
                if (block != null)
                {
                    block = SplitBlock(block, addrDest);
                }
                else
                {
                    block = AddBlock(addrDest, proc, GenerateBlockName(addrDest));
                }

                if (proc == block.Procedure)
                {
                    // Easy case: split a block in our own procedure.
                    var wi = CreateBlockWorkItem(addrDest, proc, state);
                    queue.Enqueue(PriorityJumpTarget, wi);
                }
                else if (IsBlockLinearProcedureExit(block))
                {
                    block = CloneBlockIntoOtherProcedure(block, proc);
                }
                else
                {
                    // We just created a block in a foreign procedure. 
                    blocks.Remove(addrDest);
                    block.Procedure.RemoveBlock(block);
                    procDest = (Procedure) ScanProcedure(addrDest, null, state);
                    var blockThunk = CreateCallRetThunk(addrSrc, proc, procDest);
                    var wi = CreatePromoteWorkItem(addrDest, block, procDest);
                    queue.Enqueue(PriorityBlockPromote, wi);
                    block = blockThunk;
                }
            }
            else if (block.Procedure != proc)
            {
                // Jumped to a block with a different procedure than the current one.
                // Was the jump to the entry of an existing procedure?
                if (program.Procedures.TryGetValue(addrDest, out procDest))
                {
                    if (procDest == proc)
                    {
                        proc.Signature.StackDelta = block.Procedure.Signature.StackDelta;
                        proc.Signature.FpuStackDelta = block.Procedure.Signature.FpuStackDelta;
                        var wi = CreatePromoteWorkItem(addrDest, block, procDest);
                        queue.Enqueue(PriorityBlockPromote, wi);
                    }
                    else
                    {
                        // We jumped to the entry of a different procedure.
                        block = CreateCallRetThunk(addrSrc, proc, procDest);
                    }
                }
                else
                {
                    // Jumped into the middle of another procedure. Is it worth promoting the destination block
                    // to a new procedure?
                    if (IsBlockLinearProcedureExit(block))
                    {
                        // No, just clone the block into the new procedure.
                        block = CloneBlockIntoOtherProcedure(block, proc);
                    }
                    else
                    {
                        // We jumped into a pre-existing block of another procedure which was hairy enough
                        // that we need to promote the block to a new procedure.
                        procDest = EnsureProcedure(addrDest, null);
                        var blockNew = CreateCallRetThunk(addrSrc, proc, procDest);
                        procDest.ControlGraph.AddEdge(procDest.EntryBlock, block);
                        var wi = CreatePromoteWorkItem(addrDest, block, procDest);
                        queue.Enqueue(PriorityBlockPromote, wi);
                        return blockNew;
                    }
                }
            }
            return block;
        }
Exemplo n.º 18
0
		public EntryPoint(Address addr, ProcessorState state) : this(addr, null, state)
		{
		}
Exemplo n.º 19
0
 public ProcessorState(ProcessorState orig)
 {
     this.linearDerived = new Dictionary<RegisterStorage, Expression>(orig.linearDerived);
     this.stackState = new SortedList<int, Expression>(orig.stackState);
     this.ErrorListener = this.ErrorListener;
 }
Exemplo n.º 20
0
 public override SystemService FindService(int vector, ProcessorState state)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 21
0
 public override SystemService FindService(int vector, ProcessorState state)
 {
     if (int3svc.SyscallInfo.Matches(vector, state))
         return int3svc;
     throw new NotImplementedException("INT services are not supported by " + this.GetType().Name);
 }
Exemplo n.º 22
0
 /// <summary>
 /// Creates a work item which will process code starting at the address
 /// <paramref name="addrStart"/>. The resulting block will belong to 
 /// the procedure <paramref name="proc"/>.
 /// </summary>
 /// <param name="addrStart"></param>
 /// <param name="proc"></param>
 /// <param name="stateOnEntry"></param>
 /// <returns></returns>
 public virtual BlockWorkitem CreateBlockWorkItem(
     Address addrStart,
     Procedure proc, 
     ProcessorState stateOnEntry)
 {
     return new BlockWorkitem(
         this,
         program,
         stateOnEntry,
         addrStart);
 }
Exemplo n.º 23
0
 public IEnumerable<RtlInstructionCluster> GetTrace(Address addrStart, ProcessorState state, Frame frame)
 {
     return program.Architecture.CreateRewriter(
             CreateReader(addrStart),
             state,
             frame,
             this);
 }
Exemplo n.º 24
0
 public IEnumerable<RtlInstructionCluster> CreateRewriter(ImageReader rdr, ProcessorState state, Frame frame, IRewriterHost host)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 25
0
        public void EnqueueVectorTable(Address addrFrom, Address addrTable, PrimitiveType elemSize, ushort segBase, bool calltable, Procedure proc, ProcessorState state)
        {
            ImageMapVectorTable table;
            if (vectors.TryGetValue(addrTable, out table))
                return;

            table = new ImageMapVectorTable(addrTable, calltable);
            var wi = new VectorWorkItem(this, program, table, proc);
            wi.State = state.Clone();
            wi.Stride = elemSize;
            wi.SegBase = segBase;
            wi.Table = table;
            wi.AddrFrom = addrFrom;

            imageMap.AddItem(addrTable, table);
            vectors[addrTable] = table;
            queue.Enqueue(PriorityVector, wi);
        }
Exemplo n.º 26
0
 public override Address ReadCodeAddress(int byteSize, ImageReader rdr, ProcessorState state)
 {
     return ReadSegmentedCodeAddress(byteSize, rdr, state);
 }
Exemplo n.º 27
0
 public IEnumerable<RtlInstructionCluster> CreateRewriter(ImageReader rdr, ProcessorState state, Frame frame, IRewriterHost host)
 {
     return new Z80Rewriter(this, rdr, state, frame, host);
 }
Exemplo n.º 28
0
 public override Address ReadCodeAddress(int byteSize, ImageReader rdr, ProcessorState state)
 {
     return Address.Ptr64(rdr.ReadLeUInt64());
 }
Exemplo n.º 29
0
 public Address ReadCodeAddress(int size, ImageReader rdr, ProcessorState state)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 30
0
        private void BuildTest(IntelArchitecture arch, Address addr, Platform platform, Action<X86Assembler> m)
        {
            this.arch = new IntelArchitecture(ProcessorMode.Protected32);
            proc = new Procedure("test", arch.CreateFrame());
            block = proc.AddBlock("testblock");
            this.state = arch.CreateProcessorState();
            var asm = new X86Assembler(arch, addr, new List<EntryPoint>());
            scanner = repository.StrictMock<IScanner>();
            m(asm);
            lr = asm.GetImage();
            host = new RewriterHost(asm.ImportReferences,
                new Dictionary<string, ProcedureSignature>
                {
                {
                    "GetDC", 
                    new ProcedureSignature(
                        new Identifier("", new Pointer(VoidType.Instance, 4), new RegisterStorage("eax", 0, PrimitiveType.Word32)),
                        new Identifier("arg", 
                            new TypeReference(
                                "HWND",
                                new Pointer(VoidType.Instance, 4)),
                            new StackArgumentStorage(0, new TypeReference(
                                "HWND",
                                new Pointer(VoidType.Instance, 4)))))
                                {
                                    StackDelta = 4,
}
                }
              });
            var rw = arch.CreateRewriter(lr.Image.CreateLeReader(addr), this.state, proc.Frame, host);
            var prog = new Program
            {
                Architecture = arch,
                Image = lr.Image,
                ImageMap = lr.ImageMap,
                Platform = platform,
            };
            using (repository.Record())
            {
                scanner.Stub(x => x.FindContainingBlock(Arg<Address>.Is.Anything)).Return(block);
                scanner.Stub(x => x.GetTrace(null, null, null)).IgnoreArguments().Return(rw);
            }
            wi = new BlockWorkitem(scanner, prog, state, addr);
        }