Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

GC Crash in Thread Start on Release Mode Builds #4214

Closed

Description

@jkoritzinsky

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

  1. jkoritzinsky commented on Jul 21, 2017

    @jkoritzinsky
    MemberAuthor
  2. jkotas commented on Jul 22, 2017

    @jkotas
    Member

    I've attached the crash dump.

    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.

  3. jkoritzinsky commented on Jul 24, 2017

    @jkoritzinsky
    MemberAuthor

    I've attached the binary that generated the crash dump as well.

    Threading.zip

  4. janvorli commented on Jul 24, 2017

    @janvorli
    Member

    @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.

  5. janvorli commented on Jul 24, 2017

    @janvorli
    Member

    @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?

  6. janvorli commented on Jul 24, 2017

    @janvorli
    Member

    I've forgotten to say that I was unable to repro it locally yet - running 1000 iterations of the test in release build.

  7. jkoritzinsky commented on Jul 24, 2017

    @jkoritzinsky
    MemberAuthor

    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:

    sequential() {
    while read n
    do
    	"$@"
    done < <(seq 100)
    }
    sequential ./Threading

    If you need any more info, just let me know!

  8. jkoritzinsky commented on Jul 24, 2017

    @jkoritzinsky
    MemberAuthor

    Make sure you're testing the EnableClrThreadPool branch in my fork. That's where this issue repros

  9. janvorli commented on Jul 24, 2017

    @janvorli
    Member

    @jkoritzinsky Ah, I didn't know that! Ok, let me try again.

  10. janvorli commented on Jul 25, 2017

    @janvorli
    Member

    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.

  11. janvorli commented on Jul 25, 2017

    @janvorli
    Member

    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.

    (lldb) bt
    * thread #6: tid = 18324, 0x00007fb71d0a9414 libpthread.so.0`__pthread_cond_wait + 196 at pthread_cond_wait.S:185, stop reason = signal SIGSEGV
        frame #0: 0x00007fb71d0a9414 libpthread.so.0`__pthread_cond_wait + 196 at pthread_cond_wait.S:185
        frame #1: 0x000000000048f86b Threading`UnixEvent::Wait(this=0x0000000002108350, milliseconds=4294967295) + 155 at PalRedhawkUnix.cpp:321
        frame #2: 0x000000000048e456 Threading`::WaitForSingleObjectEx(handle=0x0000000002108340, milliseconds=4294967295, alertable=0) + 54 at PalRedhawkUnix.cpp:1098
        frame #3: 0x000000000048e48b Threading`::PalCompatibleWaitAny(alertable=0, timeout=4294967295, handleCount=1, pHandles=0x00000000021064b0, allowReentrantWait=0) + 43 at PalRedhawkUnix.cpp:1106
        frame #4: 0x00000000004a267f Threading`CLREventStatic::Wait(this=0x00000000021064b0, dwMilliseconds=4294967295, bAlertable=false, bAllowReentrantWait=false) + 159 at event.cpp:105
        frame #5: 0x0000000000430500 Threading`WKS::GCHeap::WaitUntilGCComplete(this=0x00000000021066c0, bConsiderGCStart=NO) + 112 at gcee.cpp:422
        frame #6: 0x000000000041433b Threading`RedhawkGCInterface::WaitForGCCompletion() + 27 at gcrhenv.cpp:309
        frame #7: 0x000000000042cf51 Threading`Thread::WaitForGC(this=0x00007fb6ea7fc5e8, pTransitionFrame=0x00007fb6ea7fbc98) + 49 at thread.cpp:85
        frame #8: 0x000000000042e0cd Threading`::RhpWaitForGC2(pFrame=0x00007fb6ea7fbc98) + 77 at thread.cpp:915
      * frame #9: 0x000000000053a4c2 Threading`S_P_CoreLib_System_Threading_ClrThreadPool_WorkerThread__WorkerThreadStart + 122 at ClrThreadPool.WorkerThread.cs:36
        frame #10: 0x00000000004ecec2 Threading`S_P_CoreLib_Internal_Runtime_Augments_RuntimeThread__StartThread + 182 at RuntimeThread.cs:477
        frame #11: 0x00000000004e6559 Threading`S_P_CoreLib_Internal_Runtime_Augments_RuntimeThread__ThreadEntryPoint + 25 at RuntimeThread.Unix.cs:143
        frame #12: 0x00007fb71d0a5182 libpthread.so.0`start_thread(arg=0x00007fb6ea7fc700) + 194 at pthread_create.c:312
        frame #13: 0x00007fb71cdd247d libc.so.6`__clone + 109 at clone.S:111
    

    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.

    (lldb) disass -a 0x000000000053a4ae
    Threading`S_P_CoreLib_System_Threading_ClrThreadPool_WorkerThread__WorkerThreadStart:
        0x53a448 <+0>:   pushq  %rbp
        0x53a449 <+1>:   pushq  %r15
        0x53a44b <+3>:   pushq  %r14
        0x53a44d <+5>:   pushq  %r13
        0x53a44f <+7>:   pushq  %r12
        0x53a451 <+9>:   pushq  %rbx
        0x53a452 <+10>:  subq   $0x78, %rsp
        0x53a456 <+14>:  leaq   0xa0(%rsp), %rbp
        0x53a45e <+22>:  leaq   -0x48(%rbp), %rdi
        0x53a462 <+26>:  movl   $0x8, %ecx
        0x53a467 <+31>:  xorl   %eax, %eax
        0x53a469 <+33>:  rep
        0x53a46a <+34>:  stosl  %eax, %es:(%rdi)
    Interruptible range 0 starts here
        0x53a46b <+35>:  callq  0x404dfc                  ; __GetThreadStaticBase_S_P_CoreLib_Internal_Runtime_Augments_RuntimeThread
        0x53a470 <+40>:  movq   0x8(%rax), %rdi
        0x53a474 <+44>:  testq  %rdi, %rdi
        0x53a477 <+47>:  jne    0x53a4e0                  ; <+152>
        0x53a479 <+49>:  xorl   %edi, %edi
        0x53a47b <+51>:  callq  0x4ca4bc                  ; S_P_CoreLib_Internal_Runtime_Augments_RuntimeThread__InitializeExistingThread
        0x53a480 <+56>:  jmp    0x53a4e0                  ; <+152>
        0x53a482 <+58>:  callq  0x54e990                  ; S_P_CoreLib_System_Threading_ClrThreadPool_WorkerThread__TakeActiveRequest
        0x53a487 <+63>:  testb  %al, %al
        0x53a489 <+65>:  je     0x53a4db                  ; <+147>
        0x53a48b <+67>:  callq  0x40669c                  ; __GetGCStaticBase_S_P_CoreLib_System_Threading_ClrThreadPool
        0x53a490 <+72>:  movq   %rax, %rbx
        0x53a493 <+75>:  movq   0x8(%rbx), %rdi
        0x53a497 <+79>:  addq   $0x98, %rdi
        0x53a49e <+86>:  movq   %rdi, -0x70(%rbp)
        0x53a4a2 <+90>:  leaq   -0x98(%rbp), %rdi
        0x53a4a9 <+97>:  callq  0x49fef4                  ; RhpPInvoke
    Slot 0 and 1 become live at offset 3 relative to the chunk start: 35+64+3 = 102
    Slot 0 is RAX, Slot 1 RBX 
        0x53a4ae <+102>: callq  0x4a51c0                  ; ::CoreLibNative_GetTickCount64() at pal_time.cpp:107
        0x53a4b3 <+107>: movq   %rax, %rbx
        0x53a4b6 <+110>: leaq   -0x98(%rbp), %rdi
        0x53a4bd <+117>: callq  0x49ff42                  ; RhpPInvokeReturn
        0x53a4c2 <+122>: movq   %rbx, %rax
        0x53a4c5 <+125>: movq   -0x70(%rbp), %rdi
        0x53a4c9 <+129>: movl   %eax, (%rdi)
        0x53a4cb <+131>: callq  0x54e9e8                  ; S_P_CoreLib_System_Threading_ThreadPoolWorkQueue__Dispatch
        0x53a4d0 <+136>: testb  %al, %al
        0x53a4d2 <+138>: je     0x53a4e0                  ; <+152>
        0x53a4d4 <+140>: callq  0x54ec04                  ; S_P_CoreLib_System_Threading_ClrThreadPool_WorkerThread__RemoveWorkingWorker
        0x53a4d9 <+145>: jmp    0x53a4e0                  ; <+152>
        0x53a4db <+147>: callq  0x54ec04                  ; S_P_CoreLib_System_Threading_ClrThreadPool_WorkerThread__RemoveWorkingWorker
        0x53a4e0 <+152>: callq  0x407f90                  ; __GetGCStaticBase_S_P_CoreLib_System_Threading_ClrThreadPool_WorkerThread
    Slot 0 live ends at offset 58 relative to the chunk start: 35+64+58=157
        0x53a4e5 <+157>: movq   0x8(%rax), %rax
    Slot 0 becomes alive again at offset 62 relative to the chunk start: 35+64+62=161
        0x53a4e9 <+161>: movq   0x8(%rax), %rbx
        0x53a4ed <+165>: callq  0x404dfc                  ; __GetThreadStaticBase_S_P_CoreLib_Internal_Runtime_Augments_RuntimeThread
  12. janvorli commented on Jul 25, 2017

    @janvorli
    Member

    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.

  13. janvorli commented on Jul 25, 2017

    @janvorli
    Member

    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).

  14. janvorli commented on Jul 26, 2017

    @janvorli
    Member

    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?

    35  interruptible
    40  +rax(interior)
    44  +rdi
    51  -rdi
    56  -rax(interior) +rax
    58  -rax
    72  +rax(interior)
    75  +rbx(interior)
    79  +rdi
    86  -rdi +rdi(interior)
    90  +rbp-70(interior)
    102 -rdi(interior) -rbx(interior) -rax(interior)
    117 +rdi(interior)
    122 -rdi(interior)
    129 +rdi(interior)
    131 -rbp-70(interior)
    136 -rdi(interior)
    157 +rax(interior)
    161 -rax(interior) +rax
    165 +rbx
    170 -rax +rax(interior)
    174 +r14
    186 -r14 -rax(interior) +rax
    189 -rax +r14
    200 +rax(interior)
    204 +r15
    210 +rdi
    215 -rdi -rax(interior)
    222 +rdi
    227 -r15 -rdi
    230 +rdi
    233 +rsi
    251 -r14 -rdi -rsi -rbx
    264 +rax(interior)
    267 +rbx(interior)
    271 +rdi
    278 +r14
    281 -rdi
    284 +rdi
    289 -rdi -rax(interior)
    296 +rdi
    301 -r14 -rdi
    306 +rdi
    310 -rdi +rdi(interior)
    313 -rdi(interior)
    352 +rdi(interior)
    359 -rdi(interior) +rdi
    402 -rdi +rdi(interior)
    431 -rdi(interior)
    466 +rax(interior)
    470 +rdi
    512 -rdi -rax(interior)
    531 +rdi
    545 -rdi -rbx(interior)
    555 not interruptible
    586 interruptible
    591 +rax(interior)
    594 +rbx(interior)
    598 +rdi
    612 -rdi -rbx(interior) -rax(interior)
    613 not interruptible
    
  15. janvorli commented on Jul 26, 2017

    @janvorli
    Member

    @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.

  16. jkotas commented on Jul 26, 2017

    @jkotas
    Member

    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.

    it seems the JIT should make sure that the register's livetime ends before the call to RhpPInvoke and not after it

    Agree.

  17. jkoritzinsky commented on Aug 1, 2017

    @jkoritzinsky
    MemberAuthor

    @janvorli @jkotas Is anyone looking at the JIT side of this currently?

  18. jkoritzinsky commented on Aug 1, 2017

    @jkoritzinsky
    MemberAuthor

    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.

  19. jkotas commented on Aug 1, 2017

    @jkotas
    Member

    @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_BEGIN call? It seems to terminate the GC tracking right after CORINFO_HELP_JIT_PINVOKE_BEGIN call - it is too late.

  20. sandreenko commented on Aug 1, 2017

    @sandreenko

    Yes, will it affect CoreCLR?

  21. jkotas commented on Aug 1, 2017

    @jkotas
    Member

    No - CORINFO_HELP_JIT_PINVOKE_BEGIN is CoreRT specific.

  22. jkoritzinsky commented on Aug 4, 2017

    @jkoritzinsky
    MemberAuthor

    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

  23. changed the title [-]GC Crash in Thread Start on Ubuntu Release[/-] [+]GC Crash in Thread Start on Release Mode Builds[/+] on Aug 9, 2017
  24. self-assigned this
    on Sep 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions