|
|
1
|
+{-# LANGUAGE BangPatterns #-}
|
|
|
2
|
+{-# LANGUAGE MagicHash #-}
|
|
|
3
|
+{-# LANGUAGE UnboxedTuples #-}
|
|
|
4
|
+
|
|
|
5
|
+-- Regression test for #27526 and #27123:
|
|
|
6
|
+-- This is an alternative regression test to T27123 that has the benefit of reproducing
|
|
|
7
|
+-- the issue even with the non threaded runtime.
|
|
|
8
|
+-- It has the downside of being LLM generated so the description of its mechanism is
|
|
|
9
|
+-- a bit rambling as a consequence.
|
|
|
10
|
+--
|
|
|
11
|
+-- LLM generated repro case starts below this line
|
|
|
12
|
+-------------------------------------------------
|
|
|
13
|
+-- Regression test for the profiling variant of stg_ap_0_fast (rts/Apply.cmm) entering a closure
|
|
|
14
|
+-- that is no longer a thunk.
|
|
|
15
|
+--
|
|
|
16
|
+-- The thunk case of stg_ap_0_fast performs STK_CHK_GEN() -- a GC point --
|
|
|
17
|
+-- before jumping to the closure's entry code. A GC there can replace the
|
|
|
18
|
+-- closure by its value: evacuate() short-circuits a BLACKHOLE whose
|
|
|
19
|
+-- indirectee is a tagged value, and every updated thunk is such a
|
|
|
20
|
+-- BLACKHOLE. If the value is a FUN, the (unfixed) code jumped into the
|
|
|
21
|
+-- function's entry code with R1 untagged and no arguments set up; profiled
|
|
|
22
|
+-- FUN entry code loads its CCS at a tag-adjusted offset from R1, so it
|
|
|
23
|
+-- reads a byte-shifted CCS pointer and enterFunCCS dereferences a
|
|
|
24
|
+-- non-canonical address: segfault.
|
|
|
25
|
+--
|
|
|
26
|
+-- This program arranges that deterministically, single-threaded, no timer:
|
|
|
27
|
+--
|
|
|
28
|
+-- * per iteration, h is an updatable thunk whose value is a dynamic
|
|
|
29
|
+-- arity-2 FUN; 'prime' evaluates it via an unknown call at shallow
|
|
|
30
|
+-- stack depth, updating it to a BLACKHOLE whose indirectee is the
|
|
|
31
|
+-- tag-2 FUN;
|
|
|
32
|
+-- * the BLACKHOLE is carried down an allocation-free descent through
|
|
|
33
|
+-- tiny stack chunks (+RTS -kc2k -kb256). The descent's own stack
|
|
|
34
|
+-- growth never garbage-collects (nothing arms CHECK_GC()), so the
|
|
|
35
|
+-- BLACKHOLE survives to the bottom;
|
|
|
36
|
+-- * at the bottom, 'bigAlloc' allocates one large (> the -AL8k large
|
|
|
37
|
+-- allocation limit) ByteArray#. That makes CHECK_GC() true, so the
|
|
|
38
|
+-- next *failing stack check* returns HeapOverflow and garbage-collects
|
|
|
39
|
+-- right away (stg_gc_noregs in rts/HeapStackCheck.cmm) instead of
|
|
|
40
|
+-- merely growing the stack;
|
|
|
41
|
+-- * 'forceIt' then seq's h, compiling to a call of stg_ap_0_fast with
|
|
|
42
|
+-- the BLACKHOLE as argument. When the stack check in the thunk case
|
|
|
43
|
+-- fails right there, the resulting GC short-circuits the BLACKHOLE to
|
|
|
44
|
+-- the FUN and the bug fires on resume;
|
|
|
45
|
+-- * the sweep of descent depths and frame-size mixes (chosen per level
|
|
|
46
|
+-- from the bits of a rotating pattern) makes the distance between the
|
|
|
47
|
+-- bottom and the chunk's stack limit take every value at word
|
|
|
48
|
+-- granularity, so the few-word failure window is hit many times;
|
|
|
49
|
+-- * a performMinorGC per iteration disarms CHECK_GC() again, keeping
|
|
|
50
|
+-- the next iteration's descent GC-free.
|
|
|
51
|
+--
|
|
|
52
|
+-- On an unfixed RTS this segfaults within the first few hundred
|
|
|
53
|
+-- iterations (observed with GHC 9.10/9.12/9.14 on x86-64); with the fix,
|
|
|
54
|
+-- stg_ap_0_fast re-dispatches on the closure type and returns the FUN
|
|
|
55
|
+-- like any other value.
|
|
|
56
|
+--
|
|
|
57
|
+-- The test must be compiled with -fprof-late (and not -fprof-auto): an
|
|
|
58
|
+-- SCC on the local bindings would change the shape of the thunks and the
|
|
|
59
|
+-- exact stack frames the window depends on. It is 64-bit only.
|
|
|
60
|
+
|
|
|
61
|
+module Main (main) where
|
|
|
62
|
+
|
|
|
63
|
+import GHC.Exts
|
|
|
64
|
+import System.Environment (getArgs)
|
|
|
65
|
+import System.Mem (performMinorGC)
|
|
|
66
|
+
|
|
|
67
|
+data Pair = Pair (Int -> Int -> Int) [Int]
|
|
|
68
|
+
|
|
|
69
|
+-- Evaluated constructor holding a dynamic arity-2 FUN (captures n).
|
|
|
70
|
+{-# NOINLINE mkPair #-}
|
|
|
71
|
+mkPair :: Int -> Pair
|
|
|
72
|
+mkPair n = Pair (\x y -> x + y + n) [n]
|
|
|
73
|
+
|
|
|
74
|
+-- Unknown call: evaluates (and thereby updates) the thunk, lazily.
|
|
|
75
|
+{-# NOINLINE prime #-}
|
|
|
76
|
+prime :: Bool -> (Int -> Int -> Int) -> Int
|
|
|
77
|
+prime b f = if b then f 3 4 else 0
|
|
|
78
|
+
|
|
|
79
|
+-- Statically-unknown eval via stg_ap_0_fast, lazily.
|
|
|
80
|
+{-# NOINLINE forceIt #-}
|
|
|
81
|
+forceIt :: Bool -> (Int -> Int -> Int) -> Int
|
|
|
82
|
+forceIt b h = if b then h `seq` 1 else 0
|
|
|
83
|
+
|
|
|
84
|
+-- One large allocation, arming CHECK_GC() until the next GC. The
|
|
|
85
|
+-- argument varies per call so this can never be floated out and shared.
|
|
|
86
|
+{-# NOINLINE bigAlloc #-}
|
|
|
87
|
+bigAlloc :: Int -> Int
|
|
|
88
|
+bigAlloc (I# k) =
|
|
|
89
|
+ case runRW# (\s -> case newByteArray# 9000# s of
|
|
|
90
|
+ (# s1, mba #) -> getSizeofMutableByteArray# mba s1) of
|
|
|
91
|
+ (# _, n #) -> I# (n +# k)
|
|
|
92
|
+
|
|
|
93
|
+-- The bottom of the descent: arm CHECK_GC(), then evaluate the BLACKHOLE
|
|
|
94
|
+-- via stg_ap_0_fast. Everything with deeper stack demands than the
|
|
|
95
|
+-- forceIt/stg_ap_0_fast path itself must stay out of this function, or
|
|
|
96
|
+-- its stack growth would swallow exactly the margins that reach the
|
|
|
97
|
+-- window. pat stays live across forceIt to deepen that frame a little,
|
|
|
98
|
+-- widening the window.
|
|
|
99
|
+{-# NOINLINE base #-}
|
|
|
100
|
+base :: Bool -> Int -> (Int -> Int -> Int) -> Int
|
|
|
101
|
+base b pat h =
|
|
|
102
|
+ let !g = bigAlloc pat -- CHECK_GC() true from here to next GC
|
|
|
103
|
+ !c = forceIt (g >= 0 && b) h -- stg_ap_0_fast(BLACKHOLE); the window.
|
|
|
104
|
+ -- c depends on g so the two calls
|
|
|
105
|
+ -- cannot be reordered.
|
|
|
106
|
+ in g + c + pat
|
|
|
107
|
+
|
|
|
108
|
+-- Descend to depth d, carrying the BLACKHOLE h. Each level pushes one of
|
|
|
109
|
+-- two continuation frames of different sizes, chosen by the bits of pat;
|
|
|
110
|
+-- across the sweep the popcount jitter covers stack offsets at word
|
|
|
111
|
+-- granularity where the depth alone only reaches multiples of one frame
|
|
|
112
|
+-- size. pat is rotated, not shifted, so the mix keeps varying below the
|
|
|
113
|
+-- last stack chunk crossing, however deep it happens. The descent must
|
|
|
114
|
+-- not allocate, so that no heap check runs between prime and forceIt.
|
|
|
115
|
+spinA :: Bool -> Int# -> Int# -> (Int -> Int -> Int) -> Int
|
|
|
116
|
+spinA b pat d h
|
|
|
117
|
+ | isTrue# (d <=# 0#) = base b (I# pat) h
|
|
|
118
|
+ | isTrue# ((pat `andI#` 1#) ==# 0#) = spinA b (rot pat) (d -# 1#) h + 1
|
|
|
119
|
+ | otherwise = spinB b (rot pat) (d -# 1#) h (I# (d *# 17#))
|
|
|
120
|
+
|
|
|
121
|
+spinB :: Bool -> Int# -> Int# -> (Int -> Int -> Int) -> Int -> Int
|
|
|
122
|
+spinB b pat d h x = spinA b pat d h + (x `div` 1000003)
|
|
|
123
|
+
|
|
|
124
|
+rot :: Int# -> Int#
|
|
|
125
|
+rot w = uncheckedIShiftRL# w 1# `orI#` uncheckedIShiftL# w 63#
|
|
|
126
|
+{-# INLINE rot #-}
|
|
|
127
|
+
|
|
|
128
|
+-- One iteration: build and prime the thunk at shallow stack depth, then
|
|
|
129
|
+-- descend and evaluate it at the bottom; finally disarm CHECK_GC() so
|
|
|
130
|
+-- the next iteration's descent stays GC-free.
|
|
|
131
|
+{-# NOINLINE iter #-}
|
|
|
132
|
+iter :: Bool -> Int -> IO Int
|
|
|
133
|
+iter b i@(I# i#) = do
|
|
|
134
|
+ let p = mkPair i
|
|
|
135
|
+ h = case p of Pair f _ -> f -- updatable thunk, value = tag-2 FUN
|
|
|
136
|
+ !a = prime b h -- h := BLACKHOLE -> FUN (tag 2)
|
|
|
137
|
+ !r = spinA (a >= 0 && b) -- depends on a: descend only after
|
|
|
138
|
+ i# (150# +# (i# `andI#` 511#)) h -- priming
|
|
|
139
|
+ performMinorGC
|
|
|
140
|
+ pure (a + r)
|
|
|
141
|
+
|
|
|
142
|
+main :: IO ()
|
|
|
143
|
+main = do
|
|
|
144
|
+ args <- getArgs
|
|
|
145
|
+ let iters = case args of
|
|
|
146
|
+ a:_ -> read a
|
|
|
147
|
+ _ -> 20000
|
|
|
148
|
+ go !acc i
|
|
|
149
|
+ | i >= iters = pure acc
|
|
|
150
|
+ | otherwise = do
|
|
|
151
|
+ v <- iter (iters > 0) i
|
|
|
152
|
+ go (acc + v) (i + 1)
|
|
|
153
|
+ r <- go 0 0
|
|
|
154
|
+ print r |