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
transform.Run(function, context); 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 if (function.Children.Count != 1)
// normally return;
// leave IL_0000(await(callvirt<> n__0(ldobj xxHandler(ldloca this), ldobj System.Net.Http.HttpRequestMessage(ldloca request), ldobj System.Threading.CancellationToken(ldloca cancellationToken)))) var blockContainer = function.Children[0];
// would be decompiled to if (blockContainer.OpCode != OpCode.BlockContainer)
// return await((DelegatingHandler)this).SendAsync(request, cancellationToken); return;
// this changes it to if (blockContainer.Children.Count != 1)
// return await base.SendAsync(request, cancellationToken); return;
if (thisArg.OpCode == OpCode.LdObj && thisArg.Children.Count > 0 && thisArg.Children[0].OpCode == OpCode.LdLoca) { var block = blockContainer.Children[0];
thisArg = new LdLoc(((LdLoca)thisArg.Children[0]).Variable); 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 newInst.Arguments.Clear();
for (int i = 1; i < inst.Arguments.Count; i++) {
newInst.Arguments.Add(inst.Arguments.ElementAtOrDefault(i).Clone());
}
inst.ReplaceWith(newInst);
}
}
}
}
// Checks if the method is a proxy method by checking if it only contains a call instruction and if it has special compiler attributes. ILInstruction thisArg = inst.Arguments[0];
private class ProxyMethodVisitor : ILVisitor
{
ILTransformContext context;
int invalidInstructions = 0;
Call currentCall;
public Call GetCalledMethod(ILFunction function, ILTransformContext context) // special handling for first argument (this) - the underlying issue may be somewhere else
{ // normally
this.context = context; // leave IL_0000(await(callvirt<> n__0(ldobj xxHandler(ldloca this), ldobj System.Net.Http.HttpRequestMessage(ldloca request), ldobj System.Threading.CancellationToken(ldloca cancellationToken))))
function.AcceptVisitor(this); // would be decompiled to
if (invalidInstructions == 0) { // return await((DelegatingHandler)this).SendAsync(request, cancellationToken);
return currentCall; // this changes it to
} // return await base.SendAsync(request, cancellationToken);
return null; 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) newInst.Arguments.Add(thisArg);
{
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);
}
}
protected internal override void VisitCall(Call inst) // add everything except first argument
{ for (int i = 1; i < inst.Arguments.Count; i++) {
if (currentCall == null) { newInst.Arguments.Add(inst.Arguments[i]);
currentCall = inst; }
} else { inst.ReplaceWith(newInst);
invalidInstructions++; // more than one call in the function
} }
} }
} }

Loading…
Cancel
Save