在Windows中签出coreclr的代码库,通过build unixmscorlib命令编译出mscorlib.dll。
然后在Mac OS X中使用这个mscorlib.dll运行.NET程序时,出现如下的错误:
Assert failure (unable to format) /git/dotnet/coreclr/src/vm/binder.cpp Consistency check failed: Managed object size does not match unmanaged object size man: 0x18, unman: 0x20, Name: System.Runtime.InteropServices.CriticalHandle FAILED: size == expectedsize **** MessageBox invoked, title 'Assert failure (unable to format)' **** Consistency check failed: Managed object size does not match unmanaged object size man: 0x18, unman: 0x20, Name: System.Runtime.InteropServices.CriticalHandle FAILED: size == expectedsize ******** Assert failure (unable to format) /git/dotnet/coreclr/src/vm/binder.cpp pFD != NULL **** MessageBox invoked, title 'Assert failure (unable to format)' **** pFD != NULL ******** ExecuteAssembly failed - status: 0x80004005
请问如何解决这个问题?
当时用的是release版的mscorlib.dll,后来改用debug版的mscorlib.dll,问题就消失了。
字节对齐?
IntPtr.Size == 4?
问题的确与字节对齐有关
在Mac OS X上,IntPtr.Size的值是8。
@dudu: x64 的。 man: 0x18, unman: 0x20,前者是 24(3),后者是 32(4),所以不知道这 size 是表示的指针类型,还是结构体大小。
@Launcher: 出现错误的代码位置:
pMT = ClassLoader::LoadTypeByNameThrowing(GetModule()->GetAssembly(), p->classNameSpace, p->className).AsMethodTable(); if (p->expectedClassSize == sizeof(NoClass)) continue; // hidden size of the type that participates in the alignment calculation DWORD hiddenSize = pMT->IsValueType() ? sizeof(MethodTable*) : 0; DWORD size = pMT->GetBaseSize() - (sizeof(ObjHeader)+hiddenSize); DWORD expectedsize = (DWORD)ALIGN_UP(p->expectedClassSize + (sizeof(ObjHeader) + hiddenSize), DATA_ALIGNMENT) - (sizeof(ObjHeader) + hiddenSize); CONSISTENCY_CHECK_MSGF(size == expectedsize, ("Managed object size does not match unmanaged object size\n" "man: 0x%x, unman: 0x%x, Name: %s\n", size, expectedsize, pMT->GetDebugClassName()));
代码文件:https://github.com/dotnet/coreclr/blob/master/src/vm/binder.cpp
@dudu: 比较下 release 和 deubg 的 DATA_ALIGNMENT 值是否一致。