我在 .NET 2.0 C# 控制台项目中进行 VMWare 检测。检测代码在 C 中实现为 DLL 中的导出函数,该函数使用 P/Invoke 从 C# 代码调用。C# 代码编译为 x86 和 x64 为两个单独的可执行文件。C DLL 也为这两个平台编译。这是导出的函数:
PUBLIC Int32 __declspec(nothrow) WINAPI GetVMType ()
{
Int32 nVMWareType = 0;
try
{
if ( !IsInVMWare ( nVMWareType ) )
{
... // allocate memory, write data, etc.
}
}
catch ( ... )
{
nVMWareType = -1;
}
return ( nVMWareType );
} // <-- breakpoint happens here
我有以下代码使用内联汇编进行 VMWare 检测:
PRIVATE Bool IsInVMWare ( Int32& nType )
{
Bool bResult = false;
Int32 nVersion = -1;
nType = -1;
__try
{
#ifndef _WIN64 // 32-bit detection
__asm
{
push edx
push ecx
push ebx
mov eax, 'VMXh'
mov ebx, 0 // anything but 'VMXh'
mov ecx, 10 // get VMWare version
mov edx, 'VX' // port number
in eax, dx // read port
cmp ebx, 'VMXh' // is it a reply from VMWare?
je lblInVMWare
xor ecx, ecx // not in VMWare - clear return value
lblInVMWare:
mov [nVersion], ecx // vmware product type
pop ebx
pop ecx
pop edx
}
#else
nVersion = GetVMWareVersion (); // 64-bit detection
#endif
nType = nVersion;
if ( nType > 0 )
bResult = true;
}
__except ( EXCEPTION_EXECUTE_HANDLER )
{
bResult = false;
}
return ( bResult );
}
64 位检测代码GetVMWareVersion()
以汇编形式实现:
PUBLIC GetVMWareVersion
.CODE
ALIGN 8
GetVMWareVersion PROC
push rbp
mov rbp, rsp
push rdx
push rcx
push rbx
mov rax, 'VMXh'
mov rbx, 0 ; anything but 'VMXh'
mov rcx, 10 ; get VMWare version
mov rdx, 'VX' ; port number
in rax, dx ; read port
cmp ebx, 'VMXh' ; is it a reply from VMWare?
je $0@GetVMWareVersion
xor rax, rax ; not in VMWare - clear return value
jmp $1@GetVMWareVersion
$0@GetVMWareVersion:
mov rax, rcx ; VMWare product type
$1@GetVMWareVersion:
pop rbx
pop rcx
pop rdx
mov rsp, rbp
pop rbp
ret
GetVMWareVersion ENDP
END
32 位检测代码在非 VM Windows 7 上运行良好。当 64 位版本运行(相同环境)时,它会触发DebugBreak()
具有以下调用堆栈的调用:
KernelBase.dll!DebugBreak() + 0x2 bytes
[Frames below may be incorrect and/or missing, no symbols loaded for KernelBase.dll]
mscorwks.dll!PreBindAssembly() + 0x9ce69 bytes
mscorwks.dll!PreBindAssembly() + 0x9d28e bytes
mscorwks.dll!CreateApplicationContext() + 0x769d bytes
mscorwks.dll!StrongNameTokenFromPublicKey() + 0x64f8 bytes
mscorwks.dll!StrongNameTokenFromPublicKey() + 0x66ff bytes
mscorwks.dll!CreateApplicationContext() + 0x7f62 bytes
ntdll.dll!vsprintf_s() + 0x12b bytes
ntdll.dll!RtlUnwindEx() + 0x852 bytes
ntdll.dll!KiUserExceptionDispatcher() + 0x2e bytes
mscorwks.dll!IEE() + 0xd285 bytes
cccccccccccccccc()
0000000000d78180()
cccccccccccccccc()
当我在 Visual Studio 中调试 C DLL 时,我在输出窗口中也有这个:
First-chance exception at 0x000007feedbdfad1 (mscorwks.dll) in MyApp.exe:
0xC0000005: Access violation reading location 0xffffffffffffffff.
每隔一段时间(不总是)我也会在事件日志中得到这个:
.NET Runtime version 2.0.50727.5456 - Fatal Execution Engine Error
(000007FEEDB27916) (80131506)
我完全不知道为什么会发生这种情况。我做了很多搜索,但我找不到任何似乎适用于这个问题的东西。一些网站建议切换到 .NET 4.0,但这不是一个选择。
我分析了所有代码GetVMType ()
——它进行了一些内存分配并将数据写入动态分配的数组中,但该代码是正确的:所有内存都已释放,并且没有内存被错误地覆盖。
如果我修改 64 位汇编代码以跳过该in
指令,则不会触发断点。作为 32 位代码运行时,这不是问题。
调用 C# 程序有一个顶级异常处理程序和一个事件处理程序,UnhandledException
但是当这个问题发生时,应用程序只是退出 - 没有调用任何处理程序。
有谁知道这个设置有什么问题?in
我花了几个小时调试并试图查看发生了什么,但是当 P/Invoke 调用在该指令执行后返回时,.NET 中的某些东西似乎中断了。