Skip to content

Commit

Permalink
Flesh out the LibCpp2Il Api quite heavily
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam committed Sep 7, 2020
1 parent 84838e9 commit ed51fa1
Show file tree
Hide file tree
Showing 36 changed files with 1,119 additions and 578 deletions.
10 changes: 5 additions & 5 deletions Cpp2IL.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cpp2IL", "Cpp2IL\Cpp2IL.csproj", "{E8BA3E8E-3CDC-4562-AE61-CA48ECA270DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibCpp2IL", "LibCpp2IL\LibCpp2IL.csproj", "{24E00021-D8E3-4CD7-9324-091EAC3FF48A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibCpp2IL", "LibCpp2IL\LibCpp2IL.csproj", "{7C9601B4-B53B-48CD-866F-DB908B3BF54D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -14,9 +14,9 @@ Global
{E8BA3E8E-3CDC-4562-AE61-CA48ECA270DE}.Release|Any CPU.Build.0 = Release|Any CPU
{E8BA3E8E-3CDC-4562-AE61-CA48ECA270DE}.Debug|Any CPU.ActiveCfg = Debug|x64
{E8BA3E8E-3CDC-4562-AE61-CA48ECA270DE}.Debug|Any CPU.Build.0 = Debug|x64
{24E00021-D8E3-4CD7-9324-091EAC3FF48A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24E00021-D8E3-4CD7-9324-091EAC3FF48A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24E00021-D8E3-4CD7-9324-091EAC3FF48A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24E00021-D8E3-4CD7-9324-091EAC3FF48A}.Release|Any CPU.Build.0 = Release|Any CPU
{7C9601B4-B53B-48CD-866F-DB908B3BF54D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C9601B4-B53B-48CD-866F-DB908B3BF54D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C9601B4-B53B-48CD-866F-DB908B3BF54D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C9601B4-B53B-48CD-866F-DB908B3BF54D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
74 changes: 37 additions & 37 deletions Cpp2IL/Analysis/ASMAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,13 @@ private void CheckForTwoOpInstruction(Instruction instruction)
//Have a global here.
switch (global.IdentifierType)
{
case GlobalIdentifier.Type.TYPE:
case GlobalIdentifier.Type.TYPEREF:
_analysis.Actions.Add(new GlobalTypeRefToConstantAction(_analysis, instruction));
break;
case GlobalIdentifier.Type.METHOD:
case GlobalIdentifier.Type.METHODREF:
_analysis.Actions.Add(new GlobalMethodRefToConstantAction(_analysis, instruction));
break;
case GlobalIdentifier.Type.FIELD:
case GlobalIdentifier.Type.FIELDREF:
//needed?
break;
case GlobalIdentifier.Type.LITERAL:
Expand Down Expand Up @@ -782,8 +782,8 @@ private void HandleFunctionCall(MethodDefinition target, bool processReturnType,
var global = GetGlobalInReg(possibility);
if (global.HasValue)
{
args.Add($"'{global.Value.Name}' (LITERAL type System.String) as {parameter.Name} in register {possibility}");
paramNames.Add($"'{global.Value.Name}'");
args.Add($"'{global.Value.Value}' (LITERAL type System.String) as {parameter.Name} in register {possibility}");
paramNames.Add($"'{global.Value.Value}'");

if (!parameter.ParameterType.IsAssignableFrom(StringReference))
{
Expand Down Expand Up @@ -853,10 +853,10 @@ private void HandleFunctionCall(MethodDefinition target, bool processReturnType,
foreach (var possibility in possibilities)
{
_registerContents.TryGetValue(possibility, out var potentialGlob);
if (potentialGlob is GlobalIdentifier g && g.Offset != 0 && g.IdentifierType == GlobalIdentifier.Type.METHOD)
if (potentialGlob is GlobalIdentifier g && g.Offset != 0 && g.IdentifierType == GlobalIdentifier.Type.METHODREF)
{
_typeDump.Append($" ; - generic method def located, is {g.Name}");
var genericParams = g.Name.Substring(g.Name.LastIndexOf("<", StringComparison.Ordinal) + 1);
_typeDump.Append($" ; - generic method def located, is {g.Value}");
var genericParams = g.Value.Substring(g.Value.LastIndexOf("<", StringComparison.Ordinal) + 1);
genericParams = genericParams.Remove(genericParams.Length - 1);

var genericCount = genericParams.Split(',').Length;
Expand Down Expand Up @@ -955,8 +955,8 @@ private List<string> DetectPotentialLoops(List<Instruction> instructions)
if (glob2.IdentifierType == GlobalIdentifier.Type.LITERAL)
{
objectType = StringReference;
objectName = $"'{glob2.Name}'";
constant = glob2.Name;
objectName = $"'{glob2.Value}'";
constant = glob2.Value;
break;
}
}
Expand Down Expand Up @@ -1021,13 +1021,13 @@ private List<string> DetectPotentialLoops(List<Instruction> instructions)
{
if (glob.IdentifierType == GlobalIdentifier.Type.LITERAL)
{
objectName = $"\"{glob.Name}\"";
objectName = $"\"{glob.Value}\"";
objectType = StringReference;
constant = glob.Name;
constant = glob.Value;
}
else
{
objectName = $"global_{glob.IdentifierType}_{glob.Name}";
objectName = $"global_{glob.IdentifierType}_{glob.Value}";
objectType = LongReference;
constant = glob;
}
Expand Down Expand Up @@ -1113,7 +1113,7 @@ private List<string> DetectPotentialLoops(List<Instruction> instructions)


//Ok we have a global, resolve it
var (type, _) = Utils.TryLookupTypeDefByName(global.Name);
var (type, _) = Utils.TryLookupTypeDefByName(global.Value);
if (type == null) return null;

var fields = type.Fields.Where(f => f.IsStatic).ToList();
Expand Down Expand Up @@ -1989,18 +1989,18 @@ private void CheckForMoveIntoRegister(Instruction instruction)
_typeDump.Append($"; - Read on memory location 0x{addr:X}");
if (SharedState.GlobalsByOffset.TryGetValue(addr, out var glob))
{
_typeDump.Append($" - this is global value {glob.Name} of type {glob.IdentifierType}");
_registerAliases[destReg] = $"global_{glob.IdentifierType}_{glob.Name}";
_typeDump.Append($" - this is global value {glob.Value} of type {glob.IdentifierType}");
_registerAliases[destReg] = $"global_{glob.IdentifierType}_{glob.Value}";
_registerContents[destReg] = glob;
switch (glob.IdentifierType)
{
case GlobalIdentifier.Type.TYPE:
_registerTypes[destReg] = Utils.TryLookupTypeDefByName(glob.Name).Item1;
case GlobalIdentifier.Type.TYPEREF:
_registerTypes[destReg] = Utils.TryLookupTypeDefByName(glob.Value).Item1;
break;
case GlobalIdentifier.Type.METHOD:
case GlobalIdentifier.Type.METHODREF:
_registerTypes.TryRemove(destReg, out _);
break;
case GlobalIdentifier.Type.FIELD:
case GlobalIdentifier.Type.FIELDREF:
_registerTypes.TryRemove(destReg, out _);
break;
case GlobalIdentifier.Type.LITERAL:
Expand Down Expand Up @@ -2199,10 +2199,10 @@ private void CheckForCallAddress(Instruction instruction)
if (g is GlobalIdentifier glob)
{
//Check it's valid (which it should be?)
if (glob.Offset != 0 && glob.IdentifierType == GlobalIdentifier.Type.TYPE)
if (glob.Offset != 0 && glob.IdentifierType == GlobalIdentifier.Type.TYPEREF)
{
//Look up type
var (definedType, genericParams) = Utils.TryLookupTypeDefByName(glob.Name);
var (definedType, genericParams) = Utils.TryLookupTypeDefByName(glob.Value);

if (definedType != null)
{
Expand All @@ -2219,7 +2219,7 @@ private void CheckForCallAddress(Instruction instruction)
}
else
{
_methodFunctionality.Append($"{Utils.Repeat("\t", _blockDepth + 2)}Creates an instance of (unresolved) type {glob.Name}\n");
_methodFunctionality.Append($"{Utils.Repeat("\t", _blockDepth + 2)}Creates an instance of (unresolved) type {glob.Value}\n");
success = true;
}
}
Expand Down Expand Up @@ -2255,10 +2255,10 @@ private void CheckForCallAddress(Instruction instruction)
if (match.Success)
{
Enum.TryParse<GlobalIdentifier.Type>(match.Groups[1].Value, out var type);
var global = SharedState.Globals.Find(g => g.Name == match.Groups[2].Value && g.IdentifierType == type);
var global = SharedState.Globals.Find(g => g.Value == match.Groups[2].Value && g.IdentifierType == type);
if (global.Offset != 0)
{
var (definedType, genericParams) = Utils.TryLookupTypeDefByName(global.Name.Replace("[]", ""));
var (definedType, genericParams) = Utils.TryLookupTypeDefByName(global.Value.Replace("[]", ""));

if (definedType != null)
{
Expand All @@ -2270,8 +2270,8 @@ private void CheckForCallAddress(Instruction instruction)
}
else
{
_typeDump.Append($" - got expected type name {global.Name} for array but could not resolve to an actual type");
_methodFunctionality.Append($"{Utils.Repeat("\t", _blockDepth + 2)}Creates an array of (unresolved) type {global.Name} and size {arraySize}\n");
_typeDump.Append($" - got expected type name {global.Value} for array but could not resolve to an actual type");
_methodFunctionality.Append($"{Utils.Repeat("\t", _blockDepth + 2)}Creates an array of (unresolved) type {global.Value} and size {arraySize}\n");
TaintMethod(TaintReason.FAILED_TYPE_RESOLVE);
success = true;
}
Expand Down Expand Up @@ -2352,10 +2352,10 @@ private void CheckForCallAddress(Instruction instruction)
}
}

if (g is GlobalIdentifier glob && glob.Offset != 0 && glob.IdentifierType == GlobalIdentifier.Type.TYPE)
if (g is GlobalIdentifier glob && glob.Offset != 0 && glob.IdentifierType == GlobalIdentifier.Type.TYPEREF)
{
var destType = Utils.TryLookupTypeDefByName(glob.Name).Item1;
_typeDump.Append($" - Boxes the primitive value {castTarget} to {destType?.FullName} (resolved from {glob.Name})");
var destType = Utils.TryLookupTypeDefByName(glob.Value).Item1;
_typeDump.Append($" - Boxes the primitive value {castTarget} to {destType?.FullName} (resolved from {glob.Value})");
_registerAliases["rax"] = castTarget;
if (destType != null)
_registerTypes["rax"] = destType;
Expand All @@ -2376,10 +2376,10 @@ private void CheckForCallAddress(Instruction instruction)
//Try to directly resolve a destination type constant (as a global) in rdx
_registerContents.TryGetValue("rdx", out var t);

if (t is GlobalIdentifier typeGlobal && typeGlobal.IdentifierType == GlobalIdentifier.Type.TYPE)
if (t is GlobalIdentifier typeGlobal && typeGlobal.IdentifierType == GlobalIdentifier.Type.TYPEREF)
{
//got one? look it up and re-set T to the type def.
(t, _) = Utils.TryLookupTypeDefByName(typeGlobal.Name);
(t, _) = Utils.TryLookupTypeDefByName(typeGlobal.Value);
}

object castTarget;
Expand All @@ -2393,7 +2393,7 @@ private void CheckForCallAddress(Instruction instruction)
_registerContents.TryGetValue("rcx", out var g);
if (g is GlobalIdentifier glob)
{
castTarget = glob.Name;
castTarget = glob.Value;
globalIdentifier = glob;
}
else
Expand Down Expand Up @@ -2425,11 +2425,11 @@ private void CheckForCallAddress(Instruction instruction)

if (globalIdentifier.Offset == 0 || globalIdentifier.IdentifierType != GlobalIdentifier.Type.LITERAL)
{
_psuedoCode.Append(globalIdentifier.Name ?? castTarget);
_psuedoCode.Append(globalIdentifier.Value ?? castTarget);
}
else
{
_psuedoCode.Append($"'{globalIdentifier.Name}'");
_psuedoCode.Append($"'{globalIdentifier.Value}'");
}

_psuedoCode.Append("\n");
Expand Down Expand Up @@ -2948,7 +2948,7 @@ private void CheckForReturn(Instruction instruction)
if (returnConstant != null)
{
if (returnConstant is GlobalIdentifier glob && glob.IdentifierType == GlobalIdentifier.Type.LITERAL)
returnConstant = glob.Name;
returnConstant = glob.Value;

if (returnConstant is string)
returnConstant = $"\"{returnConstant}\"";
Expand Down Expand Up @@ -2979,7 +2979,7 @@ private void CheckForReturn(Instruction instruction)
if (match.Success)
{
Enum.TryParse<GlobalIdentifier.Type>(match.Groups[1].Value, out var type);
var global = SharedState.Globals.Find(g2 => g2.Name == match.Groups[2].Value && g2.IdentifierType == type);
var global = SharedState.Globals.Find(g2 => g2.Value == match.Groups[2].Value && g2.IdentifierType == type);
if (global.Offset != 0)
{
return global;
Expand Down
1 change: 1 addition & 0 deletions Cpp2IL/Analysis/Actions/GlobalMethodRefToConstantAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using LibCpp2IL;
using Mono.Cecil;
using SharpDisasm;

Expand Down
1 change: 1 addition & 0 deletions Cpp2IL/Analysis/Actions/GlobalStringRefToConstantAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cpp2IL.Analysis.ResultModels;
using LibCpp2IL;
using Mono.Cecil;
using SharpDisasm;

Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL/Analysis/Actions/GlobalTypeRefToConstantAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public GlobalTypeRefToConstantAction(MethodAnalysis context, Instruction instruc
{
var globalAddress = context.MethodStart + LibCpp2ILUtils.GetOffsetFromMemoryAccess(instruction, instruction.Operands[1]);
GlobalRead = SharedState.GlobalsByOffset[globalAddress];
var (type, genericParams) = Utils.TryLookupTypeDefByName(GlobalRead.Name);
var (type, genericParams) = Utils.TryLookupTypeDefByName(GlobalRead.Value);
ResolvedType = type;

if (ResolvedType == null) return;
Expand Down
Loading

0 comments on commit ed51fa1

Please sign in to comment.