Repository navigation
GC Crash in Thread Start on Release Mode Builds #4214
Description
There is an intermittent GC crash in garbage collection on Ubuntu during thread startup. #4124 exposes this issue. For every 100 runs of the Threading test in Ubuntu built in Release mode on #4124, between 2 and 6 will crash with this error. I've attached the crash dump.
Here is a callstack from one of the crashes:
#0 0x000000000041366d in GcEnumObject(Object**, unsigned int, void (*)(Object**, ScanContext*, unsigned int), ScanContext*) ()
#1 0x000000000041247e in EnumGcRefsCallback(void*, void**, unsigned int) ()
#2 0x0000000000461f3f in GcInfoDecoder::EnumerateLiveSlots(REGDISPLAY*, bool, unsigned int, void (*)(void*, void**, unsigned int), void*) ()
#3 0x0000000000463b71 in UnixNativeCodeManager::EnumGcRefs(MethodInfo*, void*, REGDISPLAY*, GCEnumContext*) ()
#4 0x000000000041243b in RedhawkGCInterface::EnumGcRefs(ICodeManager*, MethodInfo*, void*, REGDISPLAY*, void*, void*) ()
#5 0x000000000041e617 in Thread::GcScanRootsWorker(void*, void*, StackFrameIterator&) ()
#6 0x000000000041e480 in Thread::GcScanRoots(void*, void*) ()
#7 0x0000000000413492 in GCToEEInterface::GcScanRoots(void (*)(Object**, ScanContext*, unsigned int), int, int, ScanContext*) ()
#8 0x0000000000432c5e in WKS::gc_heap::mark_phase(int, unsigned int) ()
#9 0x00000000004304d0 in WKS::gc_heap::gc1() ()
#10 0x00000000004390fc in WKS::gc_heap::garbage_collect(int) ()
#11 0x000000000042d287 in WKS::GCHeap::GarbageCollectGeneration(unsigned int, gc_reason) ()
#12 0x000000000042ee27 in WKS::gc_heap::try_allocate_more_space(alloc_context*, unsigned long, int) ()
#13 0x000000000045180d in WKS::GCHeap::Alloc(gc_alloc_context*, unsigned long, unsigned int) ()
#14 0x00000000004122f6 in RhpGcAlloc ()
#15 0x00000000004677e4 in RhpNewObject ()
#16 0x0000000000476470 in S_P_CoreLib_System_Runtime_RuntimeExports__RhNewObject ()
#17 0x0000000000487e9c in S_P_CoreLib_Internal_Runtime_ThreadStatics__AllocateThreadStaticStorageForType ()
#18 0x0000000000483840 in S_P_CoreLib_Internal_Runtime_ThreadStatics__GetThreadStaticBaseForType ()
#19 0x00000000004b2304 in S_P_CoreLib_Internal_Runtime_Augments_RuntimeThread__StartThread ()
#20 0x00000000004ab9fd in S_P_CoreLib_Internal_Runtime_Augments_RuntimeThread__ThreadEntryPoint ()
#21 0x00007fd717b66184 in start_thread (arg=0x7fd6f5ffb700) at pthread_create.c:312
#22 0x00007fd717892ffd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
I've traced the issue to src/Native/Runtime/coreclr/gcinfodecoder.cpp.
GCInfoDecoder::ReportRegisterToGC (called inline in ReportSlotToGC, which is in turn inlined into EnumerateLiveSlots), calls GetRegisterSlot. Sometimes, GetRegisterSlot returns null on non-Windows systems. This happens to be one of those times apparently. CoreCLR has a code block below to handle this situation, but it is ifdefd out with flags that we do not use on CoreRT. If the null value makes it to the call to the callback pCallBack, then the system crashes, so we need to add a fallback like CoreCLR.
The machinery it uses to support its fallback (a pCurrentContext member on the REGDISPLAY struct), is not in CoreRT, so the fix isn't a simple ifdef change.
Activity
jkoritzinsky commentedon Jul 21, 2017
@kouvel @sergiy-k
jkotas commentedon Jul 22, 2017
Can you please include the binary w/ symbols as well? The crash dump is impossible to debug without it.
cc @janvorli Looks like dwarf unwind info bug.
The piece of info to look at is
pRegisterSet->IP, and then look at what method and instruction is at this IP.jkoritzinsky commentedon Jul 24, 2017
I've attached the binary that generated the crash dump as well.
Threading.zip
janvorli commentedon Jul 24, 2017
@jkoritzinsky I'll take a look. I guess @jkotas is right with his suspection that it is a dwarf unwind issue. Either in the info or in my changes to the unwinder code.
janvorli commentedon Jul 24, 2017
@jkoritzinsky I cannot get anything useful from the dump. There are no locals anywhere. So I am trying to repro the issue locally. I am running it on 8 proc machine with 24GB of RAM. So I was wondering if your machine was more limited and that would trigger the issue more often somehow. Can you please share your machine config with me?
janvorli commentedon Jul 24, 2017
I've forgotten to say that I was unable to repro it locally yet - running 1000 iterations of the test in release build.
jkoritzinsky commentedon Jul 24, 2017
Yeah I can't get locals either from it (or any of the dumps that have been generated).
I'm running the test on an Ubuntu 14.04 VM with 8 virtual processors and 8GB of RAM assigned to it.
I've been using this script to run the test:
If you need any more info, just let me know!
jkoritzinsky commentedon Jul 24, 2017
Make sure you're testing the EnableClrThreadPool branch in my fork. That's where this issue repros
janvorli commentedon Jul 24, 2017
@jkoritzinsky Ah, I didn't know that! Ok, let me try again.
janvorli commentedon Jul 25, 2017
Ok, I was able to repro it, fortunately even with added -g and modified -O3 to -O0 in the CMakeLists.txt. So I now have all locals etc. I'll look into the details tomorrow.
janvorli commentedon Jul 25, 2017
So it looks like the problem is in the GCInfo we generate for S_P_CoreLib_System_Threading_ClrThreadPool_WorkerThread__WorkerThreadStart.
The call stack of the thread we are walking when the crash occurs is as follows. The frame 9 is the one where we fail. It is the last managed frame on the stack, so its registers etc come from the transition frame.
The disass of the relevant part of the function at frame 9 is below. I have annotated it with info that was decoded by the GCInfo decoder when I was stepping through the slot reporting code right before the failure.
We fail when we are trying to report slot 1 that represents register RBX.
According to the GCInfo, the lifetime of that register extends over the RhpPInvoke call that fills in the transition frame.But the RhpPInvoke comment says: "the codegenerator must ensure that there are no live GC references in callee saved registers." So that rule is broken here.Also, notice that the liveness ranges starts and ends look weird - they point into the middle of instructions and also the RAX liveness range seems incorrect. For example, it covers instruction at offset +63, but at that point, the RAX clearly doesn't contain a reference.janvorli commentedon Jul 25, 2017
Hmm, I got the lifetime offsets wrong, they are encoded as relative to 64 byte chunk, not the whole interruptible range. So they actually look correct, I'll updated the disass annotation above.
janvorli commentedon Jul 25, 2017
Now the question is why does RBX get reported for code offset 101 (the code offset from the IP in the transition frame is 102 and we initialize GcInfoDecoder with that value adjusted by -1).
janvorli commentedon Jul 26, 2017
So, I have have fed the unwind info into a hacked GCDump helper in CoreCLR repo to get the complete liveness list. The result is pasted below. The location 102 which is the RIP stored in the transition frame is the location where the liveness of all the register slots ends, so the code generator seems to be ensuring that none of the callee saved registers contain live references at that point.
So thinking about why the registers are reported, it seems that the problem is that we initiate the GcInfoDecoder with codeOffset - 1. That moves the offset back to the area where the registers are still alive. The comment on the codeOffset - 1 says: // TODO: Is this adjustment correct?, which makes my suspicion even stronger. Although it sounds strange that we are not hitting GC reporting issues left and right if that's really wrong.
@jkotas, do you remember why you had to use codeOffset - 1 instead of just the codeOffset?
janvorli commentedon Jul 26, 2017
@jkotas ah, I've just realized that the -1 augmentation of the codeOffset is to make sure that when we report at caller site, the registers that become dead right after the call are still reported, since their lifetime should end after the call completes, right?
In that case, it seems the JIT should make sure that the register's livetime ends before the call to RhpPInvoke and not after it.
jkotas commentedon Jul 26, 2017
The decrement is trying to match https://github.com/dotnet/coreclr/blob/master/src/vm/eetwain.cpp#L4988 in CoreCLR. I think I have added it to fix some crashes - e.g. BasicThreading test crashes without it on Windows x64 release right now.
Agree.
jkoritzinsky commentedon Aug 1, 2017
@janvorli @jkotas Is anyone looking at the JIT side of this currently?
jkoritzinsky commentedon Aug 1, 2017
I found a relatively consistent repro of this issue. I put it in a gist at https://gist.github.com/jkoritzinsky/322b770bd4a6bbf12bb9f0d4399a5f1f.
It still requires being built on Ubuntu Release using the EnableClrThreadPool branch.
jkotas commentedon Aug 1, 2017
@sandreenko Could you please look into what it would take to teach RyuJIT to terminate tracking of all GC references before
CORINFO_HELP_JIT_PINVOKE_BEGINcall? It seems to terminate the GC tracking right afterCORINFO_HELP_JIT_PINVOKE_BEGINcall - it is too late.sandreenko commentedon Aug 1, 2017
Yes, will it affect CoreCLR?
jkotas commentedon Aug 1, 2017
No -
CORINFO_HELP_JIT_PINVOKE_BEGINis CoreRT specific.jkoritzinsky commentedon Aug 4, 2017
I just had this repro on Windows x64 in a Release build. I've attached the executable, pdb, and dump for you to take a look at.
Windows x64 Repro.zip
[-]GC Crash in Thread Start on Ubuntu Release[/-][+]GC Crash in Thread Start on Release Mode Builds[/+]