【发布时间】:2017-10-23 01:58:55
【问题描述】:
我正在尝试在运行时换出方法的内容,以便对遗留代码进行单元测试。我一直在处理这些 SO 答案;
- Dynamically replace the contents of a C# method?
- How to replace the pointer to the overridden (virtual) method in the pointer of my method? (Release x64 and x86)
这是我目前所拥有的完整代码示例。
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Foo.Bar {
public interface IFoo {
string Apple();
}
public class Bar {
protected virtual object One() {
return null;
}
protected virtual object Two() {
return null;
}
protected virtual object Three() {
return null;
}
/* Uncomment this to generate a null reference */
//protected virtual object Four() {
// return null;
//}
}
public class Foo : Bar, IFoo {
public string Apple() {
return "Apple";
}
public string Orange() {
return "Orange";
}
/* Uncommenting this fixes the null reference */
//public override int GetHashCode() {
// throw new NotImplementedException();
//}
public void ReplaceMethod(Delegate targetMethod, Delegate replacementMethod) {
MethodInfo methodToReplace = targetMethod.Method;
MethodInfo methodToInject = replacementMethod.Method;
RuntimeHelpers.PrepareMethod(methodToReplace.MethodHandle);
RuntimeHelpers.PrepareMethod(methodToInject.MethodHandle);
if (methodToReplace.IsVirtual)
ReplaceVirtualInner(methodToReplace, methodToInject);
else
ReplaceStandard(methodToReplace, methodToInject);
}
private void ReplaceStandard(MethodInfo methodToReplace, MethodInfo methodToInject) {
IntPtr targetPtr = methodToInject.MethodHandle.Value;
IntPtr replacePtr = methodToReplace.MethodHandle.Value;
unsafe
{
if (IntPtr.Size == 4) {
int* inj = (int*)replacePtr.ToPointer() + 2;
int* tar = (int*)targetPtr.ToPointer() + 2;
if (Debugger.IsAttached) {
byte* injInst = (byte*)*inj;
byte* tarInst = (byte*)*tar;
int* injSrc = (int*)(injInst + 1);
int* tarSrc = (int*)(tarInst + 1);
*tarSrc = (((int)injInst + 5) + *injSrc) - ((int)tarInst + 5);
}
else {
*tar = *inj;
}
}
else {
long* inj = (long*)replacePtr.ToPointer() + 1;
long* tar = (long*)targetPtr.ToPointer() + 1;
if (Debugger.IsAttached) {
byte* injInst = (byte*)*inj;
byte* tarInst = (byte*)*tar;
long* injSrc = (long*)(injInst + 1);
long* tarSrc = (long*)(tarInst + 1);
*tarSrc = (((long)injInst + 5) + *injSrc) - ((long)tarInst + 5);
}
else {
*tar = *inj;
}
}
}
}
private void ReplaceVirtualInner(MethodInfo methodToReplace, MethodInfo methodToInject) {
unsafe
{
UInt64* methodDesc = (UInt64*)(methodToReplace.MethodHandle.Value.ToPointer());
int index = (int)(((*methodDesc) >> 32) & 0xFF);
if (IntPtr.Size == 4) {
uint* classStart = (uint*)methodToReplace.DeclaringType.TypeHandle.Value.ToPointer();
classStart += 10;
classStart = (uint*)*classStart;
uint* tar = classStart + index;
uint* inj = (uint*)methodToInject.MethodHandle.Value.ToPointer() + 2;
if (Debugger.IsAttached) {
byte* injInst = (byte*)*inj;
byte* tarInst = (byte*)*tar;
uint* injSrc = (uint*)(injInst + 1);
uint* tarSrc = (uint*)(tarInst + 1);
*tarSrc = (((uint)injInst + 5) + *injSrc) - ((uint)tarInst + 5);
}
else {
*tar = *inj;
}
}
else {
ulong* classStart = (ulong*)methodToReplace.DeclaringType.TypeHandle.Value.ToPointer();
classStart += 8;
classStart = (ulong*)*classStart;
ulong* tar = classStart + index;
ulong* inj = (ulong*)methodToInject.MethodHandle.Value.ToPointer() + 1;
if (Debugger.IsAttached) {
byte* injInst = (byte*)*inj;
byte* tarInst = (byte*)*tar;
ulong* injSrc = (ulong*)(injInst + 1);
ulong* tarSrc = (ulong*)(tarInst + 1);
*tarSrc = (((ulong)injInst + 5) + *injSrc) - ((ulong)tarInst + 5);
}
else {
*tar = *inj;
}
}
}
}
}
}
用法;
Foo.Bar.Foo foo = new Foo.Bar.Foo();
foo.ReplaceMethod(
((Func<string>)foo.Apple),
((Func<string>)foo.Orange)
);
var result = foo.Apple(); // this is "Orange" :)
我对上面的代码有一个广泛的理解,本质上它是定位目标方法的地址并更改值,使其指向不同的内存位置。还有一些额外的 voodoo 来适应调试器添加的内存缓冲区。
虚拟方法的处理方式不同,我不完全理解,尤其是;
uint* classStart = (uint*)methodToReplace.DeclaringType.TypeHandle.Value.ToPointer();
classStart += 10; /* why 10?? */
classStart = (uint*)*classStart;
代码有效,但是,事情变得奇怪了;如果基类(目标方法的声明类型)有超过 3 个 virtual 方法,实现了一个接口并且没有覆盖任何方法,则抛出 NullReferenceException。
请问,有人可以解释发生了什么并帮助我更深入地了解代码吗?
【问题讨论】:
-
既然您这样做是为了进行单元测试,我会研究 Microsoft Fakes 框架。为您做同样的事情,而无需操作指针。此外,AFAIC 您要替换的方法的大小必须与您要替换的方法的内存大小相匹配。
-
@zaitsman 看起来很有用,不幸的是它似乎需要 Visual Studio Enterprise,是时候拿出乞讨的碗了
标签: c# unit-testing pointers virtual unsafe