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

Implement ParamDef copy for the code injection #366

Merged
merged 6 commits into from
Jul 2, 2021
Merged
Changes from all commits
Commits
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
102 changes: 59 additions & 43 deletions Confuser.Core/Helpers/InjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,64 +99,80 @@ static void CopyMethodDef(MethodDef methodDef, InjectContext ctx) {

newMethodDef.Signature = ctx.Importer.Import(methodDef.Signature);
newMethodDef.Parameters.UpdateParameterTypes();

foreach (var paramDef in methodDef.ParamDefs)
newMethodDef.ParamDefs.Add(new ParamDefUser(paramDef.Name, paramDef.Sequence, paramDef.Attributes));

if (methodDef.ImplMap != null)
newMethodDef.ImplMap = new ImplMapUser(new ModuleRefUser(ctx.TargetModule, methodDef.ImplMap.Module.Name), methodDef.ImplMap.Name, methodDef.ImplMap.Attributes);

foreach (CustomAttribute ca in methodDef.CustomAttributes)
newMethodDef.CustomAttributes.Add(new CustomAttribute((ICustomAttributeType)ctx.Importer.Import(ca.Constructor)));

if (methodDef.HasBody) {
newMethodDef.Body = new CilBody(methodDef.Body.InitLocals, new List<Instruction>(), new List<ExceptionHandler>(), new List<Local>());
newMethodDef.Body.MaxStack = methodDef.Body.MaxStack;

var bodyMap = new Dictionary<object, object>();

foreach (Local local in methodDef.Body.Variables) {
var newLocal = new Local(ctx.Importer.Import(local.Type));
newMethodDef.Body.Variables.Add(newLocal);
newLocal.Name = local.Name;

bodyMap[local] = newLocal;
}

foreach (Instruction instr in methodDef.Body.Instructions) {
var newInstr = new Instruction(instr.OpCode, instr.Operand);
newInstr.SequencePoint = instr.SequencePoint;
if (methodDef.HasBody)
CopyMethodBody(methodDef, ctx, newMethodDef);
}

static void CopyMethodBody(MethodDef methodDef, InjectContext ctx, MethodDef newMethodDef)
{
newMethodDef.Body = new CilBody(methodDef.Body.InitLocals, new List<Instruction>(),
new List<ExceptionHandler>(), new List<Local>()) {MaxStack = methodDef.Body.MaxStack};

if (newInstr.Operand is IType)
newInstr.Operand = ctx.Importer.Import((IType)newInstr.Operand);
var bodyMap = new Dictionary<object, object>();

else if (newInstr.Operand is IMethod)
newInstr.Operand = ctx.Importer.Import((IMethod)newInstr.Operand);
foreach (Local local in methodDef.Body.Variables)
{
var newLocal = new Local(ctx.Importer.Import(local.Type));
newMethodDef.Body.Variables.Add(newLocal);
newLocal.Name = local.Name;

else if (newInstr.Operand is IField)
newInstr.Operand = ctx.Importer.Import((IField)newInstr.Operand);
bodyMap[local] = newLocal;
}

newMethodDef.Body.Instructions.Add(newInstr);
bodyMap[instr] = newInstr;
foreach (Instruction instr in methodDef.Body.Instructions)
{
var newInstr = new Instruction(instr.OpCode, instr.Operand)
{
SequencePoint = instr.SequencePoint
};

switch (newInstr.Operand)
{
case IType type:
newInstr.Operand = ctx.Importer.Import(type);
break;
case IMethod method:
newInstr.Operand = ctx.Importer.Import(method);
break;
case IField field:
newInstr.Operand = ctx.Importer.Import(field);
break;
}

foreach (Instruction instr in newMethodDef.Body.Instructions) {
if (instr.Operand != null && bodyMap.ContainsKey(instr.Operand))
instr.Operand = bodyMap[instr.Operand];

else if (instr.Operand is Instruction[])
instr.Operand = ((Instruction[])instr.Operand).Select(target => (Instruction)bodyMap[target]).ToArray();
}
newMethodDef.Body.Instructions.Add(newInstr);
bodyMap[instr] = newInstr;
}

foreach (ExceptionHandler eh in methodDef.Body.ExceptionHandlers)
newMethodDef.Body.ExceptionHandlers.Add(new ExceptionHandler(eh.HandlerType) {
CatchType = eh.CatchType == null ? null : (ITypeDefOrRef)ctx.Importer.Import(eh.CatchType),
TryStart = (Instruction)bodyMap[eh.TryStart],
TryEnd = (Instruction)bodyMap[eh.TryEnd],
HandlerStart = (Instruction)bodyMap[eh.HandlerStart],
HandlerEnd = (Instruction)bodyMap[eh.HandlerEnd],
FilterStart = eh.FilterStart == null ? null : (Instruction)bodyMap[eh.FilterStart]
});

newMethodDef.Body.SimplifyMacros(newMethodDef.Parameters);
foreach (Instruction instr in newMethodDef.Body.Instructions)
{
if (instr.Operand != null && bodyMap.ContainsKey(instr.Operand))
instr.Operand = bodyMap[instr.Operand];
else if (instr.Operand is Instruction[] instructions)
instr.Operand = instructions.Select(target => (Instruction) bodyMap[target]).ToArray();
}

foreach (ExceptionHandler eh in methodDef.Body.ExceptionHandlers)
newMethodDef.Body.ExceptionHandlers.Add(new ExceptionHandler(eh.HandlerType)
{
CatchType = eh.CatchType == null ? null : ctx.Importer.Import(eh.CatchType),
TryStart = (Instruction) bodyMap[eh.TryStart],
TryEnd = (Instruction) bodyMap[eh.TryEnd],
HandlerStart = (Instruction) bodyMap[eh.HandlerStart],
HandlerEnd = (Instruction) bodyMap[eh.HandlerEnd],
FilterStart = eh.FilterStart == null ? null : (Instruction) bodyMap[eh.FilterStart]
});

newMethodDef.Body.SimplifyMacros(newMethodDef.Parameters);
}

/// <summary>
Expand Down