Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

7 changed files:

Changes:

  • changelog.d/fix-threadlabel-segfault-27618
    1
    +section: rts
    
    2
    +synopsis: Fix a segfault that could occur when querying the label of an
    
    3
    +  unlabeled thread.
    
    4
    +issues: #27618
    
    5
    +mrs: !16468
    
    6
    +description: {
    
    7
    +  The ``threadLabel#`` primop returned a null pointer in the ``ByteArray#``
    
    8
    +  field of its result for threads that have no label. This could lead to a
    
    9
    +  crash during garbage collection.
    
    10
    +}

  • rts/PrimOps.cmm
    ... ... @@ -51,6 +51,7 @@ import CLOSURE stg_AP_info;
    51 51
     import CLOSURE stg_ARR_WORDS_info;
    
    52 52
     import CLOSURE stg_BCO_info;
    
    53 53
     import CLOSURE stg_C_FINALIZER_LIST_info;
    
    54
    +import CLOSURE stg_DEAD_SLOT_closure;
    
    54 55
     import CLOSURE stg_DEAD_WEAK_info;
    
    55 56
     import CLOSURE stg_END_STM_WATCH_QUEUE_closure;
    
    56 57
     import CLOSURE stg_END_TSO_QUEUE_closure;
    
    ... ... @@ -1160,7 +1161,7 @@ stg_threadLabelzh ( gcptr tso )
    1160 1161
         W_ r;
    
    1161 1162
         r = StgTSO_label(tso);
    
    1162 1163
         if (r == 0) {
    
    1163
    -        return (0, 0);
    
    1164
    +        return (0, stg_DEAD_SLOT_closure);
    
    1164 1165
         } else {
    
    1165 1166
             return (1, r);
    
    1166 1167
         }
    

  • rts/StgMiscClosures.cmm
    ... ... @@ -726,6 +726,18 @@ INFO_TABLE_CONSTR(stg_NO_FINALIZER,0,0,0,CONSTR_NOCAF,"NO_FINALIZER","NO_FINALIZ
    726 726
     
    
    727 727
     CLOSURE(stg_NO_FINALIZER_closure,stg_NO_FINALIZER);
    
    728 728
     
    
    729
    +/* ----------------------------------------------------------------------------
    
    730
    +   DEAD_SLOT
    
    731
    +
    
    732
    +   A static nullary constructor for dead pointer-typed result slots
    
    733
    +   that must still hold a valid closure for the GC (see #27618).
    
    734
    +   ------------------------------------------------------------------------- */
    
    735
    +
    
    736
    +INFO_TABLE_CONSTR(stg_DEAD_SLOT,0,0,0,CONSTR_NOCAF,"DEAD_SLOT","DEAD_SLOT")
    
    737
    +{ ccall pbarf("DEAD_SLOT object (%p) entered!", R1 "ptr") never returns; }
    
    738
    +
    
    739
    +CLOSURE(stg_DEAD_SLOT_closure,stg_DEAD_SLOT);
    
    740
    +
    
    729 741
     /* ----------------------------------------------------------------------------
    
    730 742
        Stable Names are unlifted too.
    
    731 743
        ------------------------------------------------------------------------- */
    

  • rts/include/stg/MiscClosures.h
    ... ... @@ -200,6 +200,7 @@ RTS_ENTRY(stg_SRT_16);
    200 200
     
    
    201 201
     RTS_CLOSURE(stg_END_TSO_QUEUE_closure);
    
    202 202
     RTS_CLOSURE(stg_NO_FINALIZER_closure);
    
    203
    +RTS_CLOSURE(stg_DEAD_SLOT_closure);
    
    203 204
     RTS_CLOSURE(stg_dummy_ret_closure);
    
    204 205
     RTS_CLOSURE(stg_forceIO_closure);
    
    205 206
     RTS_CLOSURE(stg_CLOSURE_TABLE_NULL_closure);
    
    ... ... @@ -211,6 +212,7 @@ RTS_CLOSURE(stg_END_STM_CHUNK_LIST_closure);
    211 212
     RTS_CLOSURE(stg_NO_TREC_closure);
    
    212 213
     
    
    213 214
     RTS_ENTRY(stg_NO_FINALIZER);
    
    215
    +RTS_ENTRY(stg_DEAD_SLOT);
    
    214 216
     
    
    215 217
     #if IN_STG_CODE
    
    216 218
     extern StgWordArray stg_CHARLIKE_closure;
    

  • testsuite/tests/rts/T27618.hs
    1
    +{-# LANGUAGE NumericUnderscores #-}
    
    2
    +
    
    3
    +-- Test for a major GC segfault triggered by threadLabel# returning NULL in
    
    4
    +-- a GC pointer field on unlabeled threads. Run with -N4 -A32k.
    
    5
    +
    
    6
    +module Main (main) where
    
    7
    +
    
    8
    +import Control.Concurrent
    
    9
    +import Control.Monad
    
    10
    +import Data.IORef
    
    11
    +import GHC.Conc.Sync (threadLabel)
    
    12
    +import System.Mem (performMajorGC)
    
    13
    +
    
    14
    +{-# NOINLINE queryLabel #-}
    
    15
    +queryLabel :: ThreadId -> IO (Maybe String)
    
    16
    +queryLabel = threadLabel
    
    17
    +
    
    18
    +main :: IO ()
    
    19
    +main = do
    
    20
    +  stop <- newIORef False
    
    21
    +  _ <- forkIO $ forever performMajorGC
    
    22
    +  targets <- replicateM 8 $ forkIO $ forever (threadDelay 1_000_000)
    
    23
    +  dones <- forM [1 :: Int .. 8] $ \_ -> do
    
    24
    +    done <- newEmptyMVar
    
    25
    +    _ <- forkIO $ do
    
    26
    +      let loop = do
    
    27
    +            s <- readIORef stop
    
    28
    +            unless s $ do
    
    29
    +              forM_ targets $ \t -> do
    
    30
    +                r <- queryLabel t
    
    31
    +                r `seq` pure ()
    
    32
    +              loop
    
    33
    +      loop
    
    34
    +      putMVar done ()
    
    35
    +    pure done
    
    36
    +  threadDelay 2_000_000
    
    37
    +  writeIORef stop True
    
    38
    +  mapM_ takeMVar dones
    
    39
    +  putStrLn "done"

  • testsuite/tests/rts/T27618.stdout
    1
    +done

  • testsuite/tests/rts/all.T
    ... ... @@ -741,3 +741,7 @@ test('T27477',
    741 741
          ],
    
    742 742
          compile_and_run, ['-O2'])
    
    743 743
     
    
    744
    +test('T27618',
    
    745
    +     [req_target_smp, omit_ghci],
    
    746
    +     compile_and_run, ['-O2 -threaded -with-rtsopts "-N4 -A32k"'])
    
    747
    +