Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
-
3d326d7d
by Sylvain Henry at 2026-10-18T18:16:09-04:00
5 changed files:
- + changelog.d/27624
- compiler/GHC/StgToJS/Apply.hs
- + testsuite/tests/codeGen/should_run/T27624.hs
- + testsuite/tests/codeGen/should_run/T27624.stdout
- testsuite/tests/codeGen/should_run/all.T
Changes:
| 1 | +section: js-backend
|
|
| 2 | +synopsis: Fix stack corruption in the generic apply with 31 or more argument registers.
|
|
| 3 | + ``h$ap_gen_fast`` wrote registers above R31 to wrong stack slots (off by 4),
|
|
| 4 | + corrupting the stack frame below and reloading stale values for the
|
|
| 5 | + affected arguments. This affected unknown calls to thunks with 31 or more
|
|
| 6 | + argument register slots.
|
|
| 7 | +issues: #27624
|
|
| 8 | +mrs: !16614 |
| ... | ... | @@ -774,8 +774,9 @@ genericFastApply s = |
| 774 | 774 | , postIncrS regs
|
| 775 | 775 | -- copy high registers with a loop
|
| 776 | 776 | , WhileStat False (regs .>. toJExpr max_low_reg) $ mconcat
|
| 777 | - -- rN stored in stack[sp - N - 2] so that r2 is stored in stack[sp], etc.
|
|
| 778 | - [ stack .! (sp - regs - 2) |= highReg_expr regs
|
|
| 777 | + -- rN stored in stack[sp - (N - 2)] so that r2 is stored in stack[sp], etc.
|
|
| 778 | + -- (same layout as pushReg below and as the readers in h$ap_gen; see #27624)
|
|
| 779 | + [ stack .! (sp - (regs - 2)) |= highReg_expr regs
|
|
| 779 | 780 | , postDecrS regs
|
| 780 | 781 | ]
|
| 781 | 782 | , SwitchStat regs (map pushReg low_regs) mempty
|
| 1 | +-- Regression test for #27624: the JS backend's h$ap_gen_fast wrote
|
|
| 2 | +-- registers >= R32 to wrong stack slots (off by 4), corrupting the stack.
|
|
| 3 | +--
|
|
| 4 | +-- The call below is an unknown call with 35 argument register slots and a
|
|
| 5 | +-- thunk callee, so it goes through the generic apply (h$ap_gen_fast on the
|
|
| 6 | +-- JS backend). Compile with -O0 to keep `g` a thunk and the call unknown.
|
|
| 7 | +module Main (main) where
|
|
| 8 | + |
|
| 9 | +import System.Environment (getArgs)
|
|
| 10 | + |
|
| 11 | +type F = Int -> Int -> Int -> Int -> Int -> Int -> Int
|
|
| 12 | + -> Int -> Int -> Int -> Int -> Int -> Int -> Int
|
|
| 13 | + -> Int -> Int -> Int -> Int -> Int -> Int -> Int
|
|
| 14 | + -> Int -> Int -> Int -> Int -> Int -> Int -> Int
|
|
| 15 | + -> Int -> Int -> Int -> Int -> Int -> Int -> Int
|
|
| 16 | + -> Int
|
|
| 17 | + |
|
| 18 | +{-# NOINLINE add35 #-}
|
|
| 19 | +add35 :: F
|
|
| 20 | +add35 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18
|
|
| 21 | + a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 a33 a34 a35
|
|
| 22 | + = a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15+a16+a17+a18
|
|
| 23 | + +a19+a20+a21+a22+a23+a24+a25+a26+a27+a28+a29+a30+a31+a32+a33+a34+a35
|
|
| 24 | + |
|
| 25 | +{-# NOINLINE sub35 #-}
|
|
| 26 | +sub35 :: F
|
|
| 27 | +sub35 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18
|
|
| 28 | + a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 a33 a34 a35
|
|
| 29 | + = a1-a2-a3-a4-a5-a6-a7-a8-a9-a10-a11-a12-a13-a14-a15-a16-a17-a18
|
|
| 30 | + -a19-a20-a21-a22-a23-a24-a25-a26-a27-a28-a29-a30-a31-a32-a33-a34-a35
|
|
| 31 | + |
|
| 32 | +main :: IO ()
|
|
| 33 | +main = do
|
|
| 34 | + args <- getArgs
|
|
| 35 | + -- g is a let-bound thunk of function type; the 35-argument application
|
|
| 36 | + -- below is an unknown call with no specialized apply function.
|
|
| 37 | + let g = if null args then add35 else sub35
|
|
| 38 | + print (g 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
|
| 39 | + 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35) |
| 1 | +630 |
| ... | ... | @@ -259,6 +259,7 @@ test('CCallConv', [req_c], compile_and_run, ['CCallConv_c.c']) |
| 259 | 259 | test('T25364', normal, compile_and_run, [''])
|
| 260 | 260 | test('T26061', normal, compile_and_run, [''])
|
| 261 | 261 | test('T26537', compile_timeout_multiplier(1.5), compile_and_run, ['-O2 -fregs-graph'])
|
| 262 | +test('T27624', normal, compile_and_run, ['-O0'])
|
|
| 262 | 263 | test('T24016', normal, compile_and_run, ['-O1 -fPIC'])
|
| 263 | 264 | test('T27046',
|
| 264 | 265 | [ req_cmm
|