ICastable interop test failure under x86
Simply run Interop\ICastable\Castable test under x86 RyuJIT using the .cmd file after setting CORE_ROOT - this is what I'm getting:
Assert failure(PID 22548 [0x00005814], Thread: 7668 [0x1df4]): Consistency check failed: AV in clr at
this callstack:
------
CORECLR! JIT_IsInstanceOfInterface + 0x8 (0x0ff51b68)
MSCORLIB.NI! 29'::__Body::Run'::5'::__Body::Run + 0x1BF (0x1031a77f)
CORECLR!RunMain'::29'::__Body::Run + 0xA0 (0x1031a9f0)
-----
.AV on tid=0x1df4 (7668), cxr=0065CC94, exr=0065CC44
FAILED: false
CORECLR! CHECK::Trigger + 0x30A (0x0fe923ca)
CORECLR! CLRVectoredExceptionHandlerPhase3 + 0x28E (0x10089d8e)
CORECLR! CLRVectoredExceptionHandlerPhase2 + 0x87 (0x10089917)
CORECLR! CLRVectoredExceptionHandler + 0x1DC (0x1008986c)
CORECLR! CLRVectoredExceptionHandlerShim + 0x15B (0x1008a04b)
NTDLL! RtlLoadString + 0x217 (0x7711fe07)
NTDLL! RtlUnwind + 0x1B6 (0x77124136)
NTDLL! KiUserExceptionDispatcher + 0xF (0x77138e7f)
MSCORLIB.NI! <no symbol> + 0x0 (0x091e70ca)
MSCORLIB.NI! <no symbol> + 0x0 (0x091e6e71)
File: d:\github\coreclr\src\vm\excep.cpp Line: 8141
Image: D:\GitHub\coreclr\bin\tests\Windows_NT.x86.Debug\Tests\Core_Root\CoreRun.exe
I can confirm this is a GC hole in x86 RyuJIT.
The problem happens in this stack:
mscorlib!System.String.FormatHelper(System.IFormatProvider, System.String, System.ParamsArray)+0x36
mscorlib!System.String.Format(System.String, System.Object)+0x35
Castable!Castable.IsInstanceOfInterface(System.RuntimeTypeHandle, System.Exception ByRef)+0x5d
coreclr!CallDescrWorkerInternal+0x34
coreclr!CallDescrWorker+0x127
coreclr!CallDescrWorkerWithHandler+0x205
coreclr!DispatchCallSimple+0x1a2
coreclr!ObjIsInstanceOf+0x3aa
coreclr!JITutil_IsInstanceOfAny+0x23f
coreclr!JITutil_IsInstanceOfInterface+0x116
Castable!Program.Main()+0x264
coreclr!CallDescrWorkerInternal+0x34
coreclr!CallDescrWorker+0x127
coreclr!CallDescrWorkerWithHandler+0x205
coreclr!MethodDescCallSite::CallTargetWorker+0xaa7
coreclr!MethodDescCallSite::Call_RetArgSlot+0xc8
coreclr!``RunMain'::29'::__Body::Run'::5'::__Body::Run+0x1bf
coreclr!RunMain'::29'::__Body::Run+0xa0
coreclr!RunMain+0x15a
coreclr!Assembly::ExecuteMainMethod+0x3ca
Inside FormatHelper, ParamsArray struct (which is a 4-object field) is located in [ebp+8] ~ [ebp+14h], as indicated from caller code "Format(String, Object)" (and the prolog of the callee, of course):
23495aa5 50 push eax 23495aa6 ff75f0 push dword ptr [ebp-10h] 23495aa9 53 push ebx 23495aaa 57 push edi 23495aab 8bd6 mov edx,esi 23495aad 33c9 xor ecx,ecx 23495aaf ff1520690122 call dword ptr ds:[22016920h]
However, these locals are not being tracked:
0:000> !gcinfo 23495bb4 entry point 23495b80 Normal JIT generated code GC info 22e66778 Pointer table: 48 | [EBP+08H] an untracked local 44 | [EBP+0CH] an untracked local 44 | [EBP+10H] an untracked local 44 | [EBP+14H] an untracked local A1 60 | 0021 call [ EDI ESI ] argMask=00 A6 40 | 0047 call [ ESI ] argMask=00 A1 60 | 0068 call [ EDI ESI ] argMask=00 AB 40 | 0093 call [ ESI ] argMask=00 2A | 009D call [ ESI ] argMask=00 FF |
This is incorrect and results in a GC hole - leading to one of the field in paramsArray struct being incorrect and then eventually crashing in JIT_IsInstanceOfInterface.
@BruceForstall @LLITCHEV Who is the best person to look at this? If this only happens in x86 RyuJIT, this probably don't need to be fixed for RTM (it's just that I don't know where to pull the legacy JIT, so I don't know how to confirm that).
How do you reproduce this? There's a terminology issue here: I assume this is with the x86 "legacy_backend" JIT that is built out of the JIT source tree. In the JIT team, we talk about the "RyuJIT x86" compiler as a new, non-default, code path that we are just bringing up. Nonetheless, it sounds like it is NOT the JIT32 x86 compiler that we are shipping.
Nonetheless, we should have an issue opened for it and investigate it, as it might affect our "RyuJIT x86" bring-up.
I'm not familiar with the x86 GC encoding/dump, but marking something "untracked" should mean it is treated as a GC object for the entire function. "untracked" means that we won't generate per-instruction GC transition information. For non-GC objects, we won't report them at all.
@BruceForstall Ah, I see. I haven't debugged GC holes in a while so the exact meaning of untracked local has escaped me. This would make sense. So I debugged deeper and it looks like a case of bad codegen under x86 RyuJIT that is related to ICastable (yes, I'm running with the JIT in CoreCLR repo, not the legacy_backend JIT from the JIT source tree, not the one that we are shipping with), and not really a GC hole as I expected earlier.
The ICastable interface is a interesting feature: it allows object to implement ICastable and override casting behavior. The redirection code is implemented in ObjIsInstanceOf, and would call into ICastable.IsInstanceOfInterface('this', RuntimeTypeHandle typehandle, out Exception ex) before the cast would've failed without ICastable.
In this particular case, the test code has the following line in Castable.IsInstanceOfInterface:
Console.WriteLine("....", Type.GetTypeFromHandle(typeHandle))
And it is translated to the following code:
80 237d0c64 8d4508 lea eax,[ebp+8] 80 237d0c67 ff30 push dword ptr [eax] 80 237d0c69 e872f53f36 call coreclr!RuntimeTypeHandle::GetTypeFromHandle (59bd01e0)
This is incorrect because the RuntimeTypeHandle is in edx, not ebp+8. ebp+8 is the out exception argument. We end up blowing up later using the out exception location as OBJECTREF.
Here is the callstack:
Castable!Castable.IsInstanceOfInterface(System.RuntimeTypeHandle, System.Exception ByRef)+0x44
coreclr!CallDescrWorkerInternal+0x34
coreclr!CallDescrWorker+0x127
coreclr!CallDescrWorkerWithHandler+0x205
coreclr!DispatchCallSimple+0x1a2
coreclr!ObjIsInstanceOf+0x3aa
coreclr!JITutil_IsInstanceOfAny+0x23f
coreclr!JITutil_IsInstanceOfInterface+0x116
Castable!Program.Main()+0x264
coreclr!CallDescrWorkerInternal+0x34
coreclr!CallDescrWorker+0x127
coreclr!CallDescrWorkerWithHandler+0x205
coreclr!MethodDescCallSite::CallTargetWorker+0xaa7
coreclr!MethodDescCallSite::Call_RetArgSlot+0xc8
coreclr!``RunMain'::29'::__Body::Run'::5'::__Body::Run+0x1bf
coreclr!RunMain'::29'::__Body::Run+0xa0
coreclr!RunMain+0x15a
coreclr!Assembly::ExecuteMainMethod+0x3ca
coreclr!CorHost2::ExecuteAssembly+0x419
corerun!TryRun+0xa8b
@BruceForstall I've added repro instruction in the issue description in the beginning. Simply run Interop\ICastable\Castable test using the .cmd file after setting CORE_ROOT under x86. It should repro every single time.
@RussKeldorph Can you triage this?
FYI, this repros when using the non-open-source JIT32 x86 JIT as well.
For context: the VM has special code that generates the call to the JIT'ed Castable.IsInstanceOfInterface function. This is in jithelpers.cpp, function ObjIsInstanceOf(), under ifdef FEATURE_ICASTABLE:
// If type implements ICastable interface we give it a chance to tell us if it can be casted
// to a given type.
else if (toTypeHnd.IsInterface() && fromTypeHnd.GetMethodTable()->IsICastable())
{
// Make actuall call to obj.IsInstanceOfInterface(interfaceTypeObj, out exception)
OBJECTREF exception = NULL;
GCPROTECT_BEGIN(exception);
MethodTable *pFromTypeMT = fromTypeHnd.GetMethodTable();
MethodDesc *pIsInstanceOfMD = pFromTypeMT->GetMethodDescForInterfaceMethod(MscorlibBinder::GetMethod(METHOD__ICASTABLE__ISINSTANCEOF)); //GC triggers
OBJECTREF managedType = toTypeHnd.GetManagedClassObject(); //GC triggers
PREPARE_NONVIRTUAL_CALLSITE_USING_METHODDESC(pIsInstanceOfMD);
DECLARE_ARGHOLDER_ARRAY(args, 3);
args[ARGNUM_0] = OBJECTREF_TO_ARGHOLDER(obj);
args[ARGNUM_1] = OBJECTREF_TO_ARGHOLDER(managedType);
args[ARGNUM_2] = PTR_TO_ARGHOLDER(&exception);
CALL_MANAGED_METHOD(fCast, BOOL, args);
INDEBUG(managedType = NULL); // managedType isn't protected during the call
if (!fCast && throwCastException && exception != NULL)
{
RealCOMPlusThrow(exception);
}
GCPROTECT_END(); //exception
}
The first argument is the 'this' pointer, passed in ECX. The second argument is the System.RuntimeTypeHandle. It is being passed as a REF pointer. However, System.RuntimeTypeHandle is a struct type that wraps a single System.RuntimeType object pointer. This JIT sees this as a struct, and assumes (via the calling convention rules) that it is passed on the stack, at EBP+8. The third argument is a byref pointer, which the gets passed in EDX.
The bug is that the ICASTABLE code above is not passing the 2nd argument properly. On x64, it doesn't matter, because an 8-byte (pointer-sized) struct gets passed in registers.
You can create a simple function with the same signature and call it via a normal IL (JIT-generated) mechanism, and see code work:
[MethodImplAttribute(MethodImplOptions.NoInlining)]
bool Foo(System.RuntimeTypeHandle a, out System.Exception ex)
@BruceForstall Thanks! This is very helpful. I've made the fix to avoid passing structs and pass System.RuntimeType instead - making things a lot easier :)