Skip to content

Commit

Permalink
Migrate new analysis engine over to use ICED
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam committed Sep 11, 2020
1 parent 38fab4d commit babbd93
Show file tree
Hide file tree
Showing 43 changed files with 434 additions and 148 deletions.
215 changes: 144 additions & 71 deletions Cpp2IL/Analysis/ASMAnalyzer.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/AllocateArrayAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Cpp2IL.Analysis.ResultModels;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
4 changes: 2 additions & 2 deletions Cpp2IL/Analysis/Actions/AllocateInstanceAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Cpp2IL.Analysis.ResultModels;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down Expand Up @@ -34,7 +34,7 @@ public override string ToPsuedoCode()

public override string ToTextSummary()
{
return $"Allocates an instance of type {TypeCreated} and stores it as {LocalReturned?.Name} in rax.\n";
return $"[!] Allocates an instance of type {TypeCreated} and stores it as {LocalReturned?.Name} in rax.\n";
}
}
}
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/ArrayOffsetToLocalAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/BaseAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/BoxValueAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Cpp2IL.Analysis.ResultModels;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/CallBailOutAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/CallInitClassAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/CallInitMethodAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
8 changes: 4 additions & 4 deletions Cpp2IL/Analysis/Actions/CallManagedFunctionAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using LibCpp2IL;
using LibCpp2IL.Metadata;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down Expand Up @@ -36,15 +36,15 @@ private bool CheckParameters(Il2CppMethodDefinition method, MethodAnalysis conte
}
}

if (actualArgs.Any(a => a != null))
if (actualArgs.Any(a => a != null && !context.IsEmptyRegArg(a)))
return false; //Left over args - it's probably not this one

return true;
}

public CallManagedFunctionAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
var jumpTarget = Utils.GetJumpTarget(instruction, context.MethodStart + instruction.PC);
var jumpTarget = instruction.NearBranchTarget;
var objectMethodBeingCalledOn = context.GetLocalInReg("rcx");
var listOfCallableMethods = LibCpp2IlMain.GetManagedMethodImplementationsAtAddress(jumpTarget);

Expand Down Expand Up @@ -84,7 +84,7 @@ public override Mono.Cecil.Cil.Instruction[] ToILInstructions()

public override string ToTextSummary()
{
return $"Calls managed method {target?.FullName}\n";
return $"[!] Calls managed method {target?.FullName}\n";
}
}
}
45 changes: 45 additions & 0 deletions Cpp2IL/Analysis/Actions/CallManagedFunctionInRegAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Cpp2IL.Analysis.ResultModels;
using Iced.Intel;
using Mono.Cecil;

namespace Cpp2IL.Analysis.Actions
{
public class CallManagedFunctionInRegAction : BaseAction
{
private MethodDefinition _targetMethod;
private LocalDefinition? _instanceCalledOn;

public CallManagedFunctionInRegAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
var regName = Utils.GetRegisterNameNew(instruction.MemoryBase);
var operand = context.GetConstantInReg(regName);
_targetMethod = (MethodDefinition) operand.Value;

if (!_targetMethod.IsStatic)
{
_instanceCalledOn = context.GetLocalInReg("rcx");
if (_instanceCalledOn == null)
{
var cons = context.GetConstantInReg("rcx");
if (cons?.Value is NewSafeCastResult castResult)
_instanceCalledOn = castResult.original;
}
}
}

public override Mono.Cecil.Cil.Instruction[] ToILInstructions()
{
throw new System.NotImplementedException();
}

public override string? ToPsuedoCode()
{
throw new System.NotImplementedException();
}

public override string ToTextSummary()
{
return $"[!] Calls method {_targetMethod.FullName} from a register, on instance {_instanceCalledOn} if applicable\n";
}
}
}
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/CallNativeMethodFailureAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
12 changes: 6 additions & 6 deletions Cpp2IL/Analysis/Actions/CallVirtualMethodAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Cpp2IL.Analysis.ResultModels;
using LibCpp2IL;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand All @@ -15,14 +15,14 @@ public class CallVirtualMethodAction : BaseAction

public CallVirtualMethodAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
var inReg = context.GetOperandInRegister(Utils.GetRegisterName(instruction.Operands[0]));
var inReg = context.GetOperandInRegister(Utils.GetRegisterNameNew(instruction.MemoryBase));

if (!(inReg is ConstantDefinition cons) || !(cons.Value is Il2CppClassIdentifier klass)) return;

var classReadFrom = klass.backingType;
var readOffset = Utils.GetOperandMemoryOffset(instruction.Operands[0]);
Called = Utils.GetMethodFromReadKlassOffset(readOffset);

var readOffset = instruction.MemoryDisplacement;
Called = Utils.GetMethodFromReadKlassOffset((int) readOffset);

if (Called == null) return;

Expand All @@ -41,7 +41,7 @@ public override string ToPsuedoCode()

public override string ToTextSummary()
{
return $"Calls virtual function {Called?.FullName} on instance {CalledOn} with {Arguments.Count} arguments\n";
return $"[!] Calls virtual function {Called?.FullName} on instance {CalledOn} with {Arguments.Count} arguments\n";
}
}
}
8 changes: 4 additions & 4 deletions Cpp2IL/Analysis/Actions/ClassPointerLoadAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;
using SharpDisasm.Udis86;

namespace Cpp2IL.Analysis.Actions
Expand All @@ -13,11 +13,11 @@ public class ClassPointerLoadAction : BaseAction

public ClassPointerLoadAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
destReg = Utils.GetRegisterName(instruction.Operands[0]);
if(instruction.Operands[0].Base == ud_type.UD_R_RSP)
destReg = Utils.GetRegisterNameNew(instruction.Op0Register);
if(instruction.Op0Register == Register.RSP)
Console.WriteLine("WARNING: CLASS POINTER LOAD DEST IS STACK.");

var sourceReg = Utils.GetRegisterName(instruction.Operands[1]);
var sourceReg = Utils.GetRegisterNameNew(instruction.MemoryBase);
var inReg = context.GetOperandInRegister(sourceReg);
localCopiedFrom = inReg is LocalDefinition local ? local : inReg is ConstantDefinition cons && cons.Value is NewSafeCastResult result ? result.original : null;
if (localCopiedFrom == null) return;
Expand Down
5 changes: 3 additions & 2 deletions Cpp2IL/Analysis/Actions/ClearRegAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand All @@ -9,7 +9,8 @@ public class ClearRegAction : BaseAction

public ClearRegAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
regCleared = Utils.GetRegisterName(instruction.Operands[0]);
regCleared = Utils.GetRegisterNameNew(instruction.Op0Register);
context.ZeroRegister(regCleared);
}

public override Mono.Cecil.Cil.Instruction[] ToILInstructions()
Expand Down
8 changes: 4 additions & 4 deletions Cpp2IL/Analysis/Actions/ComparisonAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand All @@ -10,8 +10,8 @@ public class ComparisonAction : BaseAction

public ComparisonAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
var r0 = Utils.GetRegisterName(instruction.Operands[0]);
var r1 = Utils.GetRegisterName(instruction.Operands[1]);
var r0 = Utils.GetRegisterNameNew(instruction.Op0Register);
var r1 = Utils.GetRegisterNameNew(instruction.Op1Register);

if (r0 != "rsp")
ArgumentOne = context.GetOperandInRegister(r0);
Expand All @@ -31,7 +31,7 @@ public override string ToPsuedoCode()

public override string ToTextSummary()
{
return $"Compares {ArgumentOne} and {ArgumentTwo}";
return $"[!] Compares {ArgumentOne} and {ArgumentTwo}";
}
}
}
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/ConditionalJumpAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/ConstantToFieldAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Cpp2IL.Analysis.ResultModels;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/ConstantToRegAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/ConstantToStackAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/FieldToLocalAction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Cpp2IL.Analysis.ResultModels;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;

namespace Cpp2IL.Analysis.Actions
{
Expand Down
6 changes: 3 additions & 3 deletions Cpp2IL/Analysis/Actions/GlobalMethodRefToConstantAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using LibCpp2IL;
using LibCpp2IL.Metadata;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;
using SharpDisasm.Udis86;

namespace Cpp2IL.Analysis.Actions
Expand All @@ -17,7 +17,7 @@ public class GlobalMethodRefToConstantAction : BaseAction

public GlobalMethodRefToConstantAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
var globalAddress = context.MethodStart + Utils.GetOffsetFromMemoryAccess(instruction, instruction.Operands[1]);
var globalAddress = instruction.GetRipBasedInstructionMemoryAddress();
MethodData = LibCpp2IlMain.GetMethodDefinitionByGlobalAddress(globalAddress);
var (type, genericParams) = Utils.TryLookupTypeDefByName(MethodData!.DeclaringType.FullName);

Expand All @@ -31,7 +31,7 @@ public GlobalMethodRefToConstantAction(MethodAnalysis context, Instruction instr

if (ResolvedMethod == null) return;

var destReg = instruction.Operands[0].Type == ud_type.UD_OP_REG ? Utils.GetRegisterName(instruction.Operands[0]) : null;
var destReg = instruction.Op0Kind == OpKind.Register ? Utils.GetRegisterNameNew(instruction.Op0Register) : null;
var name = ResolvedMethod.Name;

ConstantWritten = context.MakeConstant(typeof(MethodDefinition), ResolvedMethod, name, destReg);
Expand Down
6 changes: 3 additions & 3 deletions Cpp2IL/Analysis/Actions/GlobalStringRefToConstantAction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Cpp2IL.Analysis.ResultModels;
using LibCpp2IL;
using Mono.Cecil;
using SharpDisasm;
using Iced.Intel;
using SharpDisasm.Udis86;

namespace Cpp2IL.Analysis.Actions
Expand All @@ -13,12 +13,12 @@ public class GlobalStringRefToConstantAction : BaseAction

public GlobalStringRefToConstantAction(MethodAnalysis context, Instruction instruction) : base(context, instruction)
{
var globalAddress = context.MethodStart + Utils.GetOffsetFromMemoryAccess(instruction, instruction.Operands[1]);
var globalAddress = instruction.GetRipBasedInstructionMemoryAddress();
ResolvedString = LibCpp2IlMain.GetLiteralByAddress(globalAddress);

if (ResolvedString == null) return;

var destReg = instruction.Operands[0].Type == ud_type.UD_OP_REG ? Utils.GetRegisterName(instruction.Operands[0]) : null;
var destReg = instruction.Op0Kind == OpKind.Register ? Utils.GetRegisterNameNew(instruction.Op0Register) : null;

ConstantWritten = context.MakeConstant(typeof(string), ResolvedString, null, destReg);
}
Expand Down
Loading

0 comments on commit babbd93

Please sign in to comment.