[Git][ghc/ghc][wip/T19146] 2 commits: rts: Don't sanity-check StgTSO.global_link

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Tue May 2 19:38:30 UTC 2023



Ben Gamari pushed to branch wip/T19146 at Glasgow Haskell Compiler / GHC


Commits:
b5382516 by Ben Gamari at 2023-05-02T15:38:25-04:00
rts: Don't sanity-check StgTSO.global_link

See Note [Avoid dangling global_link pointers].

Fixes #19146.

- - - - -
d2bcf347 by Ben Gamari at 2023-05-02T15:38:25-04:00
rts: Introduce printGlobalThreads

- - - - -


4 changed files:

- rts/Threads.c
- rts/Threads.h
- rts/sm/Evac.c
- rts/sm/Sanity.c


Changes:

=====================================
rts/Threads.c
=====================================
@@ -1008,6 +1008,20 @@ printAllThreads(void)
   }
 }
 
+void
+printGlobalThreads(void)
+{
+  for (uint32_t g = 0; g < RtsFlags.GcFlags.generations; g++) {
+    debugBelch("\ngen %d\n", g);
+    for (StgTSO *t = generations[g].threads; t != END_TSO_QUEUE; t = t->global_link) {
+      debugBelch("thread %p (id=%lu)\n", t, t->id);
+    }
+    for (StgTSO *t = generations[g].old_threads; t != END_TSO_QUEUE; t = t->global_link) {
+      debugBelch("thread %p (id=%lu) (old)\n", t, t->id);
+    }
+  }
+}
+
 // useful from gdb
 void
 printThreadQueue(StgTSO *t)


=====================================
rts/Threads.h
=====================================
@@ -43,6 +43,7 @@ W_   threadStackUnderflow (Capability *cap, StgTSO *tso);
 bool performTryPutMVar(Capability *cap, StgMVar *mvar, StgClosure *value);
 
 #if defined(DEBUG)
+void printGlobalThreads(void);
 void printThreadBlockage (StgTSO *tso);
 void printThreadStatus (StgTSO *t);
 void printAllThreads (void);


=====================================
rts/sm/Evac.c
=====================================
@@ -1030,8 +1030,10 @@ loop:
       return;
 
   case TSO:
+    {
       copy(p,info,q,sizeofW(StgTSO),gen_no);
       return;
+    }
 
   case STACK:
     {


=====================================
rts/sm/Sanity.c
=====================================
@@ -737,6 +737,27 @@ checkSTACK (StgStack *stack)
     checkStackChunk(sp, stack_end);
 }
 
+/*
+ * Note [Avoid dangling global_link pointers]
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * TSOs are a bit odd in that they have a global_link pointer field
+ * which is not scavenged by the GC. This field is used to track the
+ * generations[_].[old_]threads lists and is ultimately updated by
+ * MarkWeak.c:tidyThreadList.
+
+ * Typically the fact that this field is not scavenged is fine as all reachable
+ * TSOs on the heap are guaranteed to be on some generation's thread list and
+ * therefore will be scavenged by tidyThreadList. However, the sanity checker
+ * poses a bit of a challenge here as it walks heap blocks directly and
+ * therefore may encounter TSOs which aren't reachable via the heap.
+ * For this reason, checkTSO does not check global_link. Instead, we only do
+ * so in checkGlobalTSOList, which by definition will only look at
+ * threads which are reachable via a thread list (and therefore must have won
+ * the forwarding-pointer race).
+ *
+ * See #19146.
+ */
+
 void
 checkTSO(StgTSO *tso)
 {
@@ -761,9 +782,11 @@ checkTSO(StgTSO *tso)
     ASSERT(LOOKS_LIKE_CLOSURE_PTR(tso->bq));
     ASSERT(LOOKS_LIKE_CLOSURE_PTR(tso->blocked_exceptions));
     ASSERT(LOOKS_LIKE_CLOSURE_PTR(tso->stackobj));
-    ASSERT(LOOKS_LIKE_CLOSURE_PTR(tso->global_link) &&
-            (tso->global_link == END_TSO_QUEUE ||
-             get_itbl((StgClosure*)tso->global_link)->type == TSO));
+
+    // This assertion is sadly not viable. See Note [Sanity-checking global_link].
+    //ASSERT(LOOKS_LIKE_CLOSURE_PTR(tso->global_link) &&
+    //        (tso->global_link == END_TSO_QUEUE ||
+    //         get_itbl((StgClosure*)tso->global_link)->type == TSO));
 
     if (tso->label) {
         ASSERT(LOOKS_LIKE_CLOSURE_PTR(tso->label));



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/708c5eba210deb951b1ab130a84fd67ebb76318d...d2bcf3472a825bafc11e90b5e5f8b8d40cd6bc67

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/708c5eba210deb951b1ab130a84fd67ebb76318d...d2bcf3472a825bafc11e90b5e5f8b8d40cd6bc67
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230502/f777fcd7/attachment-0001.html>


More information about the ghc-commits mailing list