Browse Source

Better check if the call can be replaced. Don't clone arguments.

pull/903/head
mohe2015 8 years ago
parent
commit
5b3adbd4fd
  1. 119
      ICSharpCode.Decompiler/IL/Transforms/ProxyCallReplacer.cs

119
ICSharpCode.Decompiler/IL/Transforms/ProxyCallReplacer.cs

@ -52,84 +52,65 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -52,84 +52,65 @@ namespace ICSharpCode.Decompiler.IL.Transforms
transform.Run(function, context);
}
}
Call currentCall = new ProxyMethodVisitor().GetCalledMethod(function, context);
if (currentCall != null) {
Call newInst = (Call)currentCall.Clone();
// check if original arguments are only correct ldloc calls
for (int i = 0; i < currentCall.Arguments.Count; i++) {
var originalArg = currentCall.Arguments.ElementAtOrDefault(i);
if (originalArg.OpCode != OpCode.LdLoc ||
originalArg.Children.Count != 0 ||
((LdLoc)originalArg).Variable.Kind != VariableKind.Parameter ||
((LdLoc)originalArg).Variable.Index != i - 1) {
return;
}
}
newInst.Arguments.Clear();
ILInstruction thisArg = inst.Arguments.ElementAtOrDefault(0).Clone();
// special handling for first argument (this) - the underlying issue may be somewhere else
// normally
// leave IL_0000(await(callvirt<> n__0(ldobj xxHandler(ldloca this), ldobj System.Net.Http.HttpRequestMessage(ldloca request), ldobj System.Threading.CancellationToken(ldloca cancellationToken))))
// would be decompiled to
// return await((DelegatingHandler)this).SendAsync(request, cancellationToken);
// this changes it to
// return await base.SendAsync(request, cancellationToken);
if (thisArg.OpCode == OpCode.LdObj && thisArg.Children.Count > 0 && thisArg.Children[0].OpCode == OpCode.LdLoca) {
thisArg = new LdLoc(((LdLoca)thisArg.Children[0]).Variable);
if (function.Children.Count != 1)
return;
var blockContainer = function.Children[0];
if (blockContainer.OpCode != OpCode.BlockContainer)
return;
if (blockContainer.Children.Count != 1)
return;
var block = blockContainer.Children[0];
if (block.OpCode != OpCode.Block)
return;
if (block.Children.Count > 2)
return;
if (block.Children.Count == 2 && block.Children[1].OpCode != OpCode.Nop)
return;
var leave = block.Children[0];
if (leave.OpCode != OpCode.Leave)
return;
if (leave.Children.Count != 1)
return;
Call call = leave.Children[0] as Call;
if (call == null)
return;
// check if original arguments are only correct ldloc calls
for (int i = 0; i < call.Arguments.Count; i++) {
var originalArg = call.Arguments[i];
if (originalArg.OpCode != OpCode.LdLoc ||
originalArg.Children.Count != 0 ||
((LdLoc)originalArg).Variable.Kind != VariableKind.Parameter ||
((LdLoc)originalArg).Variable.Index != i - 1) {
return;
}
}
newInst.Arguments.Add(thisArg);
Call newInst = (Call)call.Clone();
// add everything except first argument
for (int i = 1; i < inst.Arguments.Count; i++) {
newInst.Arguments.Add(inst.Arguments.ElementAtOrDefault(i).Clone());
}
inst.ReplaceWith(newInst);
}
}
}
}
newInst.Arguments.Clear();
// Checks if the method is a proxy method by checking if it only contains a call instruction and if it has special compiler attributes.
private class ProxyMethodVisitor : ILVisitor
{
ILTransformContext context;
int invalidInstructions = 0;
Call currentCall;
ILInstruction thisArg = inst.Arguments[0];
public Call GetCalledMethod(ILFunction function, ILTransformContext context)
{
this.context = context;
function.AcceptVisitor(this);
if (invalidInstructions == 0) {
return currentCall;
}
return null;
}
// special handling for first argument (this) - the underlying issue may be somewhere else
// normally
// leave IL_0000(await(callvirt<> n__0(ldobj xxHandler(ldloca this), ldobj System.Net.Http.HttpRequestMessage(ldloca request), ldobj System.Threading.CancellationToken(ldloca cancellationToken))))
// would be decompiled to
// return await((DelegatingHandler)this).SendAsync(request, cancellationToken);
// this changes it to
// return await base.SendAsync(request, cancellationToken);
if (thisArg.OpCode == OpCode.LdObj && thisArg.Children.Count > 0 && thisArg.Children[0].OpCode == OpCode.LdLoca) {
thisArg = new LdLoc(((LdLoca)thisArg.Children[0]).Variable);
}
protected override void Default(ILInstruction inst)
{
if (inst.OpCode != OpCode.ILFunction &&
inst.OpCode != OpCode.BlockContainer &&
inst.OpCode != OpCode.Block &&
inst.OpCode != OpCode.Leave &&
inst.OpCode != OpCode.Nop) {
invalidInstructions++;
}
foreach (var child in inst.Children) {
child.AcceptVisitor(this);
}
}
newInst.Arguments.Add(thisArg);
protected internal override void VisitCall(Call inst)
{
if (currentCall == null) {
currentCall = inst;
} else {
invalidInstructions++; // more than one call in the function
// add everything except first argument
for (int i = 1; i < inst.Arguments.Count; i++) {
newInst.Arguments.Add(inst.Arguments[i]);
}
inst.ReplaceWith(newInst);
}
}
}

Loading…
Cancel
Save