Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port generator to use AsmResolver #124

Merged
merged 36 commits into from
Sep 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b86d87e
Port generator to use AsmResolver
ds5678 May 13, 2024
c3bfab4
Improve method unstripping
ds5678 May 13, 2024
c8107f1
Rewrite mscorlib 2 to mscorlib 4
ds5678 May 13, 2024
8107c35
Formatting
ds5678 May 13, 2024
102c431
Use full name for importing a corlib reference
ds5678 May 13, 2024
4e5a2a7
Handle some nullable warnings
ds5678 May 13, 2024
f6228ad
StartsWith extension method for Utf8String
ds5678 May 13, 2024
98aa95c
Switch opcode unstripping
ds5678 May 13, 2024
332d5ff
Don't unstrip methods that use array opcodes
ds5678 May 13, 2024
837e532
Improve unstripping support for newarr:
ds5678 May 13, 2024
1757f0c
Address format workflow errors
ds5678 Jun 29, 2024
19b2295
AsmResolver 6
ds5678 Jul 1, 2024
06b123b
Replace SetSemanticMethods with new set accessors from AsmResolver 6
ds5678 Jul 1, 2024
00b7302
Implement HasOverrides
ds5678 Jul 1, 2024
5b30890
Nullable annotations
ds5678 Jul 1, 2024
4c1e51e
Copy generic parameter constraints when creating params methods
ds5678 Jul 1, 2024
15eaeab
Remove CecilAdapter
ds5678 Jul 1, 2024
5e58d9e
Unstrip ldlen
ds5678 Jul 1, 2024
676db2a
Resolve a consistency issue where Cecil considers nint and nuint prim…
ds5678 Jul 12, 2024
c9d5b20
Fix resolution issues
ds5678 Jul 12, 2024
831061f
Null coallescence in UnstripTranslator
ds5678 Jul 12, 2024
0dd9899
Resolve some issues with MethodSignature not marked as generic
ds5678 Jul 12, 2024
5a615d5
Resolve more GenericParameterCount issues
ds5678 Jul 12, 2024
331cee4
Fix an issue with naming for method semantics
ds5678 Jul 12, 2024
a9ab5e9
Fix consistency issue with name unmangling for generic parameters
ds5678 Jul 12, 2024
5f2ecd1
Fix property signatures
ds5678 Jul 12, 2024
81c665a
Formatting
ds5678 Jul 12, 2024
9623127
Remove HasOverrides check
ds5678 Jul 13, 2024
9d361a3
Address issues
ds5678 Jul 16, 2024
a3b6f74
Optimize macros
ds5678 Jul 16, 2024
0f91436
Assembly reference improvements
ds5678 Jul 17, 2024
3b396f4
Null HashValue and PublicKeyOrToken
ds5678 Aug 13, 2024
fdb4f24
Fix array return
ds5678 Aug 16, 2024
3b760a2
Cast to Utf8String
ds5678 Aug 18, 2024
7643307
More null forgiving while unstripping
ds5678 Aug 19, 2024
c985a2f
Fix resolution bug for resolving constraints on generic parameters in…
ds5678 Sep 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve method unstripping
* Reuse primitive operands
* Support Parameter operands
* Map local variables
* Map instruction labels
  • Loading branch information
ds5678 committed Jul 7, 2024

Verified

This commit was signed with the committer’s verified signature.
rpearce Robert Pearce
commit c3bfab41a0a26fd2ced37cd043ee9155540e8f61
31 changes: 11 additions & 20 deletions Il2CppInterop.Generator/CecilAdapter.cs
Original file line number Diff line number Diff line change
@@ -38,33 +38,24 @@ public static void AddLoadArgument(this CilInstructionCollection instructions, i
instructions.Add(CilOpCodes.Ldarg_3);
break;
default:
{
var method = instructions.Owner.Owner;
var parameterIndex = method.IsStatic ? argumentIndex : argumentIndex - 1;
instructions.Add(CilOpCodes.Ldarg, method.Parameters[parameterIndex]);
}
instructions.Add(CilOpCodes.Ldarg, instructions.GetArgument(argumentIndex));
break;
}
}

public static void AddLoadArgumentAddress(this CilInstructionCollection instructions, int argumentIndex)
{
var method = instructions.Owner.Owner;
int parameterIndex;
if (method.IsStatic)
{
parameterIndex = argumentIndex;
}
else
{
if (argumentIndex == 0)
{
throw new NotSupportedException("Cannot load address of 'this' argument.");
}
parameterIndex = argumentIndex - 1;
}
instructions.Add(CilOpCodes.Ldarga, instructions.GetArgument(argumentIndex));
}

instructions.Add(CilOpCodes.Ldarga, method.Parameters[parameterIndex]);
private static Parameter GetArgument(this CilInstructionCollection instructions, int argumentIndex)
{
var method = instructions.Owner.Owner;
return method.IsStatic
? method.Parameters[argumentIndex]
: argumentIndex == 0
? method.Parameters.ThisParameter!
: method.Parameters[argumentIndex - 1];
}

public static bool HasGenericParameters(this TypeDefinition type) => type.GenericParameters.Count > 0;
8 changes: 6 additions & 2 deletions Il2CppInterop.Generator/Passes/Pass89GenerateForwarders.cs
Original file line number Diff line number Diff line change
@@ -28,7 +28,9 @@ public static void DoPass(RewriteGlobalContext context)
continue;

var exportedType = new ExportedType(null, mainModuleType.Namespace, mainModuleType.Name)
{ Attributes = TypeAttributes.Forwarder };
{
Attributes = TypeAttributes.Forwarder
};
targetModule.ExportedTypes.Add(exportedType);

AddNestedTypes(mainModuleType, exportedType, targetModule);
@@ -45,7 +47,9 @@ private static void AddNestedTypes(TypeDefinition mainModuleType, ExportedType i

var nestedImport = targetModule.DefaultImporter.ImportType(nested);
var nestedExport = new ExportedType(importedType, nestedImport.Namespace, nestedImport.Name)
{ Attributes = TypeAttributes.Forwarder };
{
Attributes = TypeAttributes.Forwarder
};

targetModule.ExportedTypes.Add(nestedExport);

117 changes: 92 additions & 25 deletions Il2CppInterop.Generator/Utils/UnstripTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using AsmResolver;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Collections;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.PE.DotNet.Cil;
using Il2CppInterop.Generator.Contexts;
@@ -12,50 +14,70 @@ public static class UnstripTranslator
public static bool TranslateMethod(MethodDefinition original, MethodDefinition target,
TypeRewriteContext typeRewriteContext, RuntimeAssemblyReferences imports)
{
if (original.CilMethodBody is null) return true;
if (original.CilMethodBody is null)
return true;

target.CilMethodBody = new(target);

var globalContext = typeRewriteContext.AssemblyContext.GlobalContext;
Dictionary<CilLocalVariable, CilLocalVariable> localVariableMap = new();
foreach (var variableDefinition in original.CilMethodBody.LocalVariables)
{
var variableType =
Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, variableDefinition.VariableType,
imports);
if (variableType == null) return false;
target.CilMethodBody.LocalVariables.Add(new CilLocalVariable(variableType));
if (variableType == null)
return false;
var newVariableDefinition = new CilLocalVariable(variableType);
target.CilMethodBody.LocalVariables.Add(newVariableDefinition);
localVariableMap.Add(variableDefinition, newVariableDefinition);
}

List<KeyValuePair<CilInstructionLabel, CilInstructionLabel>> labelMap = new();
Dictionary<CilInstruction, CilInstruction> instructionMap = new();

var targetBuilder = target.CilMethodBody.Instructions;
foreach (var bodyInstruction in original.CilMethodBody.Instructions)
if (bodyInstruction.OpCode.OperandType == CilOperandType.InlineField)
{
if (bodyInstruction.Operand is null)
{
var fieldArg = (IFieldDescriptor?)bodyInstruction.Operand;
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode);
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.OpCode.OperandType == CilOperandType.InlineField)
{
var fieldArg = (IFieldDescriptor)bodyInstruction.Operand;
var fieldDeclarer =
Pass80UnstripMethods.ResolveTypeInNewAssembliesRaw(globalContext, fieldArg.DeclaringType.ToTypeSignature(), imports);
if (fieldDeclarer == null) return false;
if (fieldDeclarer == null)
return false;
var newField = fieldDeclarer.Resolve().Fields.SingleOrDefault(it => it.Name == fieldArg.Name);
if (newField != null)
{
targetBuilder.Add(bodyInstruction.OpCode, imports.Module.DefaultImporter.ImportField(newField));
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, imports.Module.DefaultImporter.ImportField(newField));
instructionMap.Add(bodyInstruction, newInstruction);
}
else
{
if (bodyInstruction.OpCode == OpCodes.Ldfld || bodyInstruction.OpCode == OpCodes.Ldsfld)
{
var getterMethod = fieldDeclarer.Resolve().Properties
.SingleOrDefault(it => it.Name == fieldArg.Name)?.GetMethod;
if (getterMethod == null) return false;
if (getterMethod == null)
return false;

targetBuilder.Add(OpCodes.Call, imports.Module.DefaultImporter.ImportMethod(getterMethod));
var newInstruction = targetBuilder.Add(OpCodes.Call, imports.Module.DefaultImporter.ImportMethod(getterMethod));
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.OpCode == OpCodes.Stfld || bodyInstruction.OpCode == OpCodes.Stsfld)
{
var setterMethod = fieldDeclarer.Resolve().Properties
.SingleOrDefault(it => it.Name == fieldArg.Name)?.SetMethod;
if (setterMethod == null) return false;
if (setterMethod == null)
return false;

targetBuilder.Add(OpCodes.Call, imports.Module.DefaultImporter.ImportMethod(setterMethod));
var newInstruction = targetBuilder.Add(OpCodes.Call, imports.Module.DefaultImporter.ImportMethod(setterMethod));
instructionMap.Add(bodyInstruction, newInstruction);
}
else
{
@@ -68,24 +90,28 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
var methodArg = (IMethodDescriptor)bodyInstruction.Operand;
var methodDeclarer =
Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, methodArg.DeclaringType.ToTypeSignature(), imports);
if (methodDeclarer == null) return false; // todo: generic methods
if (methodDeclarer == null)
return false; // todo: generic methods

var newReturnType =
Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, methodArg.Signature.ReturnType, imports);
if (newReturnType == null) return false;
if (newReturnType == null)
return false;

var newMethodSignature = CecilAdapter.CreateMethodSignature(!methodArg.Signature.HasThis, newReturnType);
var newMethod = new MemberReference(methodDeclarer.ToTypeDefOrRef(), methodArg.Name, newMethodSignature);
foreach (var methodArgParameter in methodArg.Signature.ParameterTypes)
{
var newParamType = Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext,
methodArgParameter, imports);
if (newParamType == null) return false;
if (newParamType == null)
return false;

newMethodSignature.ParameterTypes.Add(newParamType);
}

targetBuilder.Add(bodyInstruction.OpCode, imports.Module.DefaultImporter.ImportMethod(newMethod));
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, imports.Module.DefaultImporter.ImportMethod(newMethod));
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.OpCode.OperandType == CilOperandType.InlineType)
{
@@ -96,7 +122,8 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
{
var newTypeOwner =
Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, original.DeclaringType?.ToTypeSignature(), imports)?.Resolve();
if (newTypeOwner == null) return false;
if (newTypeOwner == null)
return false;
targetType = newTypeOwner.GenericParameters.Single(it => it.Name == targetType.Name).ToTypeSignature();
}
else
@@ -107,30 +134,35 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
else
{
targetType = Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, targetType, imports);
if (targetType == null) return false;
if (targetType == null)
return false;
}

if (bodyInstruction.OpCode == OpCodes.Castclass && !targetType.IsValueType)
{
targetBuilder.Add(OpCodes.Call,
var newInstruction = targetBuilder.Add(OpCodes.Call,
imports.Module.DefaultImporter.ImportMethod(imports.Il2CppObjectBase_Cast.Value.MakeGenericInstanceMethod(targetType)));
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.OpCode == OpCodes.Isinst && !targetType.IsValueType)
{
targetBuilder.Add(OpCodes.Call,
var newInstruction = targetBuilder.Add(OpCodes.Call,
imports.Module.DefaultImporter.ImportMethod(imports.Il2CppObjectBase_TryCast.Value.MakeGenericInstanceMethod(targetType)));
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.OpCode == OpCodes.Newarr && !targetType.IsValueType)
{
targetBuilder.Add(OpCodes.Conv_I8);
var newInstruction = targetBuilder.Add(OpCodes.Conv_I8);

var il2cppTypeArray = imports.Il2CppReferenceArray.MakeGenericInstanceType(targetType).ToTypeDefOrRef();
targetBuilder.Add(OpCodes.Newobj, imports.Module.DefaultImporter.ImportMethod(
CecilAdapter.CreateInstanceMethodReference(".ctor", imports.Module.Void(), il2cppTypeArray, imports.Module.Long())));
instructionMap.Add(bodyInstruction, newInstruction);
}
else
{
targetBuilder.Add(bodyInstruction.OpCode, targetType.ToTypeDefOrRef());
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, targetType.ToTypeDefOrRef());
instructionMap.Add(bodyInstruction, newInstruction);
}
}
else if (bodyInstruction.OpCode.OperandType == CilOperandType.InlineSig)
@@ -163,20 +195,48 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
else
{
targetTok = Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, targetTok, imports);
if (targetTok == null) return false;
if (targetTok == null)
return false;
}

targetBuilder.Add(OpCodes.Call,
var newInstruction = targetBuilder.Add(OpCodes.Call,
imports.Module.DefaultImporter.ImportMethod(imports.Il2CppSystemRuntimeTypeHandleGetRuntimeTypeHandle.Value.MakeGenericInstanceMethod(targetTok)));
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.Operand is null)
else if (bodyInstruction.Operand is string or Utf8String
|| bodyInstruction.Operand.GetType().IsPrimitive)
{
targetBuilder.Add(bodyInstruction.OpCode);
var newInstruction = new CilInstruction(bodyInstruction.OpCode, bodyInstruction.Operand);
targetBuilder.Add(newInstruction);
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.Operand is Parameter parameter)
{
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, target.Parameters.GetBySignatureIndex(parameter.MethodSignatureIndex));
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.Operand is CilLocalVariable localVariable)
{
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, localVariableMap[localVariable]);
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.Operand is CilInstructionLabel label)
{
var newLabel = new CilInstructionLabel();
labelMap.Add(new(label, newLabel));
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, newLabel);
instructionMap.Add(bodyInstruction, newInstruction);
}
else
{
return false;
}
}

foreach ((var oldLabel, var newLabel) in labelMap)
{
newLabel.Instruction = instructionMap[oldLabel.Instruction!];
}

return true;
}
@@ -192,4 +252,11 @@ public static void ReplaceBodyWithException(MethodDefinition newMethod, RuntimeA
processor.Add(OpCodes.Throw);
processor.Add(OpCodes.Ret);
}

//Required for deconstruction on net472
private static void Deconstruct(this KeyValuePair<CilInstructionLabel, CilInstructionLabel> pair, out CilInstructionLabel key, out CilInstructionLabel value)
{
key = pair.Key;
value = pair.Value;
}
}