Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 3d326d7d by Sylvain Henry at 2026-10-18T18:16:09-04:00 JS: fix stack corruption in h$ap_gen_fast's pushAllRegs (#27624) The loop added in 9e46990976 ("JS: support more than 128 registers (#26558)") pushed register N to stack[sp - N - 2] while the switch it falls through into, and every reader of the h$ap_gen frame, uses stack[sp - (N - 2)]. Registers >= R32 were therefore written 4 slots too low: the slots the readers consult were left stale and the frame below was silently corrupted. Fix the loop to use the same slot formula as the switch, and add a regression test: an unknown call to a thunk with 35 argument register slots, which goes through the generic apply. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> - - - - - 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: ===================================== changelog.d/27624 ===================================== @@ -0,0 +1,8 @@ +section: js-backend +synopsis: Fix stack corruption in the generic apply with 31 or more argument registers. + ``h$ap_gen_fast`` wrote registers above R31 to wrong stack slots (off by 4), + corrupting the stack frame below and reloading stale values for the + affected arguments. This affected unknown calls to thunks with 31 or more + argument register slots. +issues: #27624 +mrs: !16614 ===================================== compiler/GHC/StgToJS/Apply.hs ===================================== @@ -774,8 +774,9 @@ genericFastApply s = , postIncrS regs -- copy high registers with a loop , WhileStat False (regs .>. toJExpr max_low_reg) $ mconcat - -- rN stored in stack[sp - N - 2] so that r2 is stored in stack[sp], etc. - [ stack .! (sp - regs - 2) |= highReg_expr regs + -- rN stored in stack[sp - (N - 2)] so that r2 is stored in stack[sp], etc. + -- (same layout as pushReg below and as the readers in h$ap_gen; see #27624) + [ stack .! (sp - (regs - 2)) |= highReg_expr regs , postDecrS regs ] , SwitchStat regs (map pushReg low_regs) mempty ===================================== testsuite/tests/codeGen/should_run/T27624.hs ===================================== @@ -0,0 +1,39 @@ +-- Regression test for #27624: the JS backend's h$ap_gen_fast wrote +-- registers >= R32 to wrong stack slots (off by 4), corrupting the stack. +-- +-- The call below is an unknown call with 35 argument register slots and a +-- thunk callee, so it goes through the generic apply (h$ap_gen_fast on the +-- JS backend). Compile with -O0 to keep `g` a thunk and the call unknown. +module Main (main) where + +import System.Environment (getArgs) + +type F = Int -> Int -> Int -> Int -> Int -> Int -> Int + -> Int -> Int -> Int -> Int -> Int -> Int -> Int + -> Int -> Int -> Int -> Int -> Int -> Int -> Int + -> Int -> Int -> Int -> Int -> Int -> Int -> Int + -> Int -> Int -> Int -> Int -> Int -> Int -> Int + -> Int + +{-# NOINLINE add35 #-} +add35 :: F +add35 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 + a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 a33 a34 a35 + = a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15+a16+a17+a18 + +a19+a20+a21+a22+a23+a24+a25+a26+a27+a28+a29+a30+a31+a32+a33+a34+a35 + +{-# NOINLINE sub35 #-} +sub35 :: F +sub35 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 + a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 a33 a34 a35 + = a1-a2-a3-a4-a5-a6-a7-a8-a9-a10-a11-a12-a13-a14-a15-a16-a17-a18 + -a19-a20-a21-a22-a23-a24-a25-a26-a27-a28-a29-a30-a31-a32-a33-a34-a35 + +main :: IO () +main = do + args <- getArgs + -- g is a let-bound thunk of function type; the 35-argument application + -- below is an unknown call with no specialized apply function. + let g = if null args then add35 else sub35 + print (g 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 + 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35) ===================================== testsuite/tests/codeGen/should_run/T27624.stdout ===================================== @@ -0,0 +1 @@ +630 ===================================== testsuite/tests/codeGen/should_run/all.T ===================================== @@ -259,6 +259,7 @@ test('CCallConv', [req_c], compile_and_run, ['CCallConv_c.c']) test('T25364', normal, compile_and_run, ['']) test('T26061', normal, compile_and_run, ['']) test('T26537', compile_timeout_multiplier(1.5), compile_and_run, ['-O2 -fregs-graph']) +test('T27624', normal, compile_and_run, ['-O0']) test('T24016', normal, compile_and_run, ['-O1 -fPIC']) test('T27046', [ req_cmm View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/3d326d7d28ee5d51c9ae42ae1baf1c5a... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/3d326d7d28ee5d51c9ae42ae1baf1c5a... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help