Beispiel #1
0
 private string GuessName(Procedure_v1 userProc, Procedure proc = null)
 {
     if (userProc != null)
     {
         if (!string.IsNullOrEmpty(userProc.Name))
         {
             return(userProc.Name);
         }
         if (!string.IsNullOrEmpty(userProc.CSignature))
         {
             int i = userProc.CSignature.IndexOf('(');
             if (i > 0)
             {
                 var name = userProc.CSignature.Remove(i);
                 do
                 {
                     --i;
                 } while (i > 0 && (char.IsLetterOrDigit(name[i]) || name[i] == '_'));
                 return(name.Substring(i + 1));
             }
         }
     }
     if (proc != null)
     {
         return(proc.Name);
     }
     else
     {
         return(null);
     }
 }
Beispiel #2
0
 protected KeyValuePair <Address, Procedure>?EnsureUserProcedure(Procedure_v1 sp)
 {
     if (!Program.Architecture.TryParseAddress(sp.Address, out var addr))
     {
         return(null);
     }
     if (Program.Procedures.TryGetValue(addr, out var proc))
     {
         return(null); // Already scanned. Do nothing.
     }
     if (!sp.Decompile)
     {
         return(null);
     }
     proc = EnsureProcedure(addr, sp.Name);
     if (sp.Signature != null)
     {
         var sser = Program.CreateProcedureSerializer();
         proc.Signature = sser.Deserialize(sp.Signature, proc.Frame);
     }
     if (sp.Characteristics != null)
     {
         proc.Characteristics = sp.Characteristics;
     }
     return(new KeyValuePair <Address, Procedure>(addr, proc));
 }
Beispiel #3
0
        public override void DoIt()
        {
            var          dlgFactory = Services.RequireService <IDialogFactory>();
            var          uiSvc      = Services.RequireService <IDecompilerShellUiService>();
            Procedure_v1 sProc;

            if (!program.User.Procedures.TryGetValue(address, out sProc))
            {
                sProc = new Procedure_v1
                {
                    Name = procedure.Name
                }
            }
            ;
            //$REVIEW: use dialog factory.
            var i = new ProcedureDialogInteractor(program, sProc);

            using (ProcedureDialog dlg = i.CreateDialog())
            {
                if (DialogResult.OK == uiSvc.ShowModalDialog(dlg))
                {
                    i.ApplyChanges();
                    program.User.Procedures[address] = sProc;
                    procedure.Name = sProc.Name;
                }
            }
        }
    }
Beispiel #4
0
        public override void DoIt()
        {
            var          dlgFactory = Services.RequireService <IDialogFactory>();
            var          uiSvc      = Services.RequireService <IDecompilerShellUiService>();
            Procedure_v1 sProc;

            if (!program.User.Procedures.TryGetValue(address, out sProc))
            {
                sProc = new Procedure_v1
                {
                    Name = procedure.Name
                }
            }
            ;
            using (IProcedureDialog dlg = dlgFactory.CreateProcedureDialog(program, sProc))
            {
                if (DialogResult.OK == uiSvc.ShowModalDialog(dlg))
                {
                    dlg.ApplyChanges();
                    program.User.Procedures[address] = sProc;
                    if (procedure != null)
                    {
                        procedure.Name = sProc.Name;
                    }
                }
            }
        }
    }
Beispiel #5
0
        public void EnqueueUserProcedure(Procedure_v1 sp)
        {
            Address addr;

            if (!program.Architecture.TryParseAddress(sp.Address, out addr))
            {
                return;
            }
            Procedure proc;

            if (program.Procedures.TryGetValue(addr, out proc))
            {
                return; // Already scanned. Do nothing.
            }
            if (!sp.Decompile)
            {
                return;
            }
            proc = EnsureProcedure(addr, sp.Name);
            if (sp.Signature != null)
            {
                var sser = program.CreateProcedureSerializer();
                proc.Signature = sser.Deserialize(sp.Signature, proc.Frame);
            }
            if (sp.Characteristics != null)
            {
                proc.Characteristics = sp.Characteristics;
            }
            queue.Enqueue(PriorityEntryPoint, new ProcedureWorkItem(this, program, addr, sp.Name));
        }
Beispiel #6
0
        private bool TryGetNoDecompiledParsedProcedure(Address addr, out Procedure_v1 parsedProc)
        {
            if (!TryGetNoDecompiledProcedure(addr, out Procedure_v1 sProc))
            {
                parsedProc = null;
                return(false);
            }
            if (noDecompiledProcs.TryGetValue(addr, out parsedProc))
            {
                return(true);
            }
            parsedProc = new Procedure_v1()
            {
                Name = sProc.Name,
            };
            noDecompiledProcs[addr] = parsedProc;
            if (string.IsNullOrEmpty(sProc.CSignature))
            {
                Warn(addr, "The user-defined procedure at address {0} did not have a signature.", addr);
                return(true);
            }
            var usb      = new UserSignatureBuilder(Program);
            var procDecl = usb.ParseFunctionDeclaration(sProc.CSignature);

            if (procDecl == null)
            {
                Warn(addr, "The user-defined procedure signature at address {0} could not be parsed.", addr);
                return(true);
            }
            parsedProc.Signature = procDecl.Signature;
            return(true);
        }
        public void LoadProcedure(Procedure_v1 sp)
        {
            try
            {
                var sser      = new ProcedureSerializer(platform, this, this.defaultConvention);
                var signature = sser.Deserialize(sp.Signature, platform.Architecture.CreateFrame());
                library.Signatures[sp.Name] = signature;
                var mod = EnsureModule(this.moduleName, this.library);
                var svc = new SystemService
                {
                    ModuleName = mod.ModuleName,
                    Name       = sp.Name,
                    Signature  = signature,
                };
                mod.ServicesByName[sp.Name] = svc;    //$BUGBUG: catch dupes?

                if (sp.Ordinal != Procedure_v1.NoOrdinal)
                {
                    mod.ServicesByOrdinal[sp.Ordinal] = svc;
                }
            }
            catch (Exception ex)
            {
                Debug.Print("An error occurred when loading the signature of procedure {0}.", sp.Name);
                throw new ApplicationException(
                          string.Format("An error occurred when loading the signature of procedure {0}.", sp.Name),
                          ex);
            }
        }
Beispiel #8
0
 public ProcedureBase_v1?DeserializeSignature(Procedure_v1 userProc, Procedure proc)
 {
     if (!string.IsNullOrEmpty(userProc.CSignature))
     {
         return(ParseFunctionDeclaration(userProc.CSignature !));
     }
     return(null);
 }
Beispiel #9
0
 private void Given_Signature(Address addr, string procName, SerializedSignature ssig)
 {
     userSigs[addr] = new Procedure_v1
     {
         Address   = addr.ToString(),
         Name      = procName,
         Signature = ssig,
     };
 }
 public ProcedureDialogInteractor(IProcessorArchitecture arch, Procedure_v1 proc)
 {
     this.arch = arch;
     this.proc = proc;
     if (proc.Signature != null && proc.Signature.Arguments == null)
     {
         proc.Signature.Arguments = new Argument_v1[0];
     }
 }
Beispiel #11
0
 private bool TryGetNoDecompiledProcedure(Address addr, out Procedure_v1 sProc)
 {
     if (!Program.User.Procedures.TryGetValue(addr, out sProc) ||
         sProc.Decompile)
     {
         sProc = null;
         return(false);
     }
     return(true);
 }
Beispiel #12
0
        public Procedure Add(Procedure_v1 userProc, Action <ProcedureBuilder> testCodeBuilder)
        {
            var mock = new ProcedureBuilder(Program.Architecture, GuessName(userProc), null);

            mock.ProgramMock   = this;
            mock.LinearAddress = (uint)((procCount + 1) * 0x1000);
            testCodeBuilder(mock);
            Add(mock.Procedure, userProc);
            unresolvedProcedures.AddRange(mock.UnresolvedProcedures);
            return(mock.Procedure);
        }
Beispiel #13
0
        public Address EnqueueUserProcedure(Procedure_v1 sp)
        {
            var de = EnsureUserProcedure(sp);

            if (de == null)
            {
                return(null);
            }
            procQueue.Enqueue(PriorityEntryPoint, new ProcedureWorkItem(this, Program, de.Value.Key, sp.Name));
            return(de.Value.Key);
        }
Beispiel #14
0
 public void LoadProcedure(Procedure_v1 sp)
 {
     try
     {
         var sser      = new ProcedureSerializer(platform, this, this.defaultConvention ?? "");
         var signature = sser.Deserialize(sp.Signature, platform.Architecture.CreateFrame());
         if (sp.Name is null || signature is null)
         {
             return;
         }
         library.Signatures[sp.Name !] = signature;
Beispiel #15
0
        public void PpcPs_SerializeProcedure()
        {
            Procedure proc = new Procedure("foo", arch.CreateFrame());
            Address   addr = Address.Ptr32(0x12345);

            Given_ProcedureSerializer();
            Procedure_v1 sproc = ser.Serialize(proc, addr);

            Assert.AreEqual("foo", sproc.Name);
            Assert.AreEqual("00012345", sproc.Address);
        }
Beispiel #16
0
        public void VisitFunctionType(FunctionType ft)
        {
            var addr = rdr.Address;
            var up   = new Procedure_v1
            {
                Address   = addr.ToString(),
                Signature = ft.Signature
            };

            scanner.EnqueueUserProcedure(up);
        }
Beispiel #17
0
        public Address?EnqueueUserProcedure(IProcessorArchitecture arch, Procedure_v1 sp)
        {
            var de = EnsureUserProcedure(arch, sp);

            if (de == null)
            {
                return(null);
            }
            procQueue.Enqueue(PriorityEntryPoint, new ProcedureWorkItem(this, arch, de.Value.Key, sp.Name));
            return(de.Value.Key);
        }
        private void Given_ProcedureSignature(uint addr, string CSignature)
        {
            Given_ProcedureName(addr, "<unnamed>");

            var address = Address32.Ptr32(addr);
            var sProc   = new Procedure_v1()
            {
                CSignature = CSignature,
            };

            program.User.Procedures[address] = sProc;
        }
Beispiel #19
0
        public void Setup()
        {
            proc = new Procedure_v1();
            var arch     = new X86ArchitectureFlat32("x86-protected-32");
            var platform = new Win32Platform(null, arch);
            var program  = new Program
            {
                Architecture = arch,
                Platform     = platform
            };

            interactor = new ProcedureDialogInteractor(program, proc);
        }
Beispiel #20
0
        public void X86ps_SerializeProcedure()
        {
            Procedure proc = new Procedure(arch, "foo", arch.CreateFrame());
            Address   addr = Address.Ptr32(0x12345);

            Given_ProcedureSerializer("stdapi");
            mr.ReplayAll();

            Procedure_v1 sproc = ser.Serialize(proc, addr);

            Assert.AreEqual("foo", sproc.Name);
            Assert.AreEqual("00012345", sproc.Address);
        }
Beispiel #21
0
        public void Add(Procedure proc, Procedure_v1 userProc = null)
        {
            ++procCount;
            var addr = Address.Ptr32(procCount * 0x1000u);

            Program.Procedures[addr] = proc;
            Program.CallGraph.AddProcedure(proc);
            nameToProcedure[GuessName(userProc, proc)] = proc;
            if (userProc != null)
            {
                Program.User.Procedures[addr] = userProc;
            }
        }
Beispiel #22
0
 public Procedure_v1 EnsureUserProcedure(Address address, string name, bool decompile = true)
 {
     if (!User.Procedures.TryGetValue(address, out var up))
     {
         up = new Procedure_v1
         {
             Address   = address.ToString(),
             Name      = name,
             Decompile = decompile,
         };
         User.Procedures.Add(address, up);
     }
     return(up);
 }
Beispiel #23
0
        public void Setup()
        {
            proc = new Procedure_v1();
            var sc       = new ServiceContainer();
            var arch     = new X86ArchitectureFlat32(sc, "x86-protected-32", new Dictionary <string, object>());
            var platform = new Win32Platform(sc, arch);
            var program  = new Program
            {
                Architecture = arch,
                Platform     = platform
            };

            interactor = new ProcedureDialogInteractor(program, proc);
        }
Beispiel #24
0
        public void SudRead_v1()
        {
            Project_v1 proj = null;

            using (FileStream stm = new FileStream(FileUnitTester.MapTestPath("Core/SudRead.xml"), FileMode.Open))
            {
                XmlSerializer ser = SerializedLibrary.CreateSerializer_v1(typeof(Project_v1));
                proj = (Project_v1)ser.Deserialize(stm);
            }
            Assert.AreEqual("10003330", proj.Input.Address);
            Assert.AreEqual(2, proj.UserProcedures.Count);
            Procedure_v1 proc = (Procedure_v1)proj.UserProcedures[0];

            Assert.IsNull(proc.Signature.ReturnValue);
        }
        public void LoadProcedure(Procedure_v1 sp)
        {
            try
            {
                if (sp.Name is null)
                {
                    return;
                }
                if (sp.Characteristics != null)
                {
                    library.Characteristics[sp.Name] = sp.Characteristics;
                }
                var sser      = new ProcedureSerializer(platform, this, this.defaultConvention ?? "");
                var signature = sser.Deserialize(sp.Signature, platform.Architecture.CreateFrame());
                if (signature is null)
                {
                    return;
                }
                library.Signatures[sp.Name] = signature;
                if (platform.TryParseAddress(sp.Address, out var addr))
                {
                    this.library.Procedures[addr] = (sp.Name, signature);
                }
                else
                {
                    var mod = EnsureModule(this.moduleName, this.library);
                    var svc = new SystemService
                    {
                        ModuleName = mod.ModuleName,
                        Name       = sp.Name,
                        Signature  = signature,
                    };
                    mod.ServicesByName[sp.Name] = svc;    //$BUGBUG: catch dupes?

                    if (sp.Ordinal != Procedure_v1.NoOrdinal)
                    {
                        mod.ServicesByOrdinal[sp.Ordinal] = svc;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print("An error occurred when loading the signature of procedure {0}.", sp.Name);
                throw new ApplicationException(
                          string.Format("An error occurred when loading the signature of procedure {0}.", sp.Name),
                          ex);
            }
        }
Beispiel #26
0
        public void SlibReadOneProcedure()
        {
            XmlSerializer     ser = SerializedLibrary.CreateSerializer_v1(typeof(SerializedLibrary));
            SerializedLibrary lib;

            using (Stream stm = fsSvc.CreateFileStream(FileUnitTester.MapTestPath("Core/SlibOneProcedure.xml"), FileMode.Open))
            {
                lib = (SerializedLibrary)ser.Deserialize(stm);
            }
            Assert.AreEqual(1, lib.Procedures.Count);
            Procedure_v1 proc = (Procedure_v1)lib.Procedures[0];

            Assert.AreEqual("malloc", proc.Name);
            Assert.AreEqual(1, proc.Signature.Arguments.Length);
            Assert.AreEqual("int", proc.Signature.Arguments[0].Type.ToString());
        }
Beispiel #27
0
        public void PpcPs_SerializeProcedureWithSignature()
        {
            Procedure proc = new Procedure("foo", arch.CreateFrame())
            {
                Signature = new ProcedureSignature(
                    new Identifier("eax", PrimitiveType.Word32, arch.Registers[3]),
                    new Identifier[] {
                    new Identifier("arg00", PrimitiveType.Word32, arch.Registers[3])
                })
            };

            Address addr = Address.Ptr32(0x567A0C);

            Given_ProcedureSerializer();
            Procedure_v1 sproc = ser.Serialize(proc, addr);

            Assert.AreEqual("eax", sproc.Signature.ReturnValue.Name);
        }
        public void X86ps_ProcedureWithSignature()
        {
            Address   addr = Address.Ptr32(0x567A0C);
            Procedure proc = new Procedure(arch, "foo", addr, arch.CreateFrame())
            {
                Signature = new FunctionType(
                    new Identifier("eax", PrimitiveType.Word32, Registers.eax),
                    new Identifier[] {
                    new Identifier("arg00", PrimitiveType.Word32, new StackArgumentStorage(0, PrimitiveType.Word32))
                })
            };

            Given_ProcedureSerializer("stdapi");

            Procedure_v1 sproc = ser.Serialize(proc, addr);

            Assert.AreEqual("eax", sproc.Signature.ReturnValue.Name);
        }
Beispiel #29
0
 private void LoadProcedures(SerializedLibrary serializedLibrary)
 {
     if (serializedLibrary.Procedures != null)
     {
         foreach (object o in serializedLibrary.Procedures)
         {
             Procedure_v1 sp = o as Procedure_v1;
             if (sp != null)
             {
                 LoadProcedure(sp);
             }
             SerializedService svc = o as SerializedService;
             if (svc != null)
             {
                 LoadService(svc);
             }
         }
     }
 }
Beispiel #30
0
        public void SudWrite()
        {
            Project_v1 ud = new Project_v1();

            ud.Input.Address = "0x1000:0x0";
            ud.Output.DisassemblyFilename  = "foo.asm";
            ud.Output.IntermediateFilename = "foo.cod";
            Procedure_v1 proc = new Procedure_v1();

            proc.Name      = "foo";
            proc.Signature = new SerializedSignature
            {
                ReturnValue = new Argument_v1
                {
                    Kind = new Register_v1("eax")
                },
                Arguments = new Argument_v1[]
                {
                    new Argument_v1
                    {
                        Kind = new StackVariable_v1(),
                        Type = new PrimitiveType_v1(Domain.SignedInt, 4)
                    },
                    new Argument_v1
                    {
                        Kind = new StackVariable_v1(),
                        Type = new PrimitiveType_v1(Domain.SignedInt, 4)
                    }
                }
            };
            ud.UserProcedures.Add(proc);

            using (FileUnitTester fut = new FileUnitTester("Core/SudWrite.txt"))
            {
                var writer = new FilteringXmlWriter(fut.TextWriter);
                writer.Formatting = System.Xml.Formatting.Indented;
                XmlSerializer ser = SerializedLibrary.CreateSerializer_v1(typeof(Project_v1));
                ser.Serialize(writer, ud);
                fut.AssertFilesEqual();
            }
        }