Skip to content

Out of range index error when bytecode generation with profiled interpreter

Summary

When using profiled_ghc flavoured GHC for a repl session for a big private project, I encountered this out of array index error when interpreting some modules:

ghci> :load (...)
[... of ...] Compiling ... ( src/....hs, interpreted )

<no location info>: error:
    Ix{Int}.index: Index (52) out of range ((0,39))

Fortunately, I could patch indexError to show the stack trace in profiled mode:

<no location info>: error:
    Ix{Int}.index: Index (52) out of range ((0,39))
CallStack (from HasCallStack):
  error, called at libraries/base/src/GHC/Ix.hs:169:5 in base:GHC.Ix
CallStack (from -prof):
  GHC.Stack.CCS.currentCallStack1 (<no location info>)
  GHC.Stack.CCS.currentCallStack (libraries/base/src/GHC/Stack/CCS.hsc:126:1-16)
  GHC.Exception.errorCallWithCallStackException (libraries/base/src/GHC/Exception.hs:102:1-31)
  GHC.Err.error (libraries/base/src/GHC/Err.hs:36:1-5)
  GHC.Ix.$w$sindexError (libraries/base/src/GHC/Ix.hs:168:1-10)
  GHC.StgToByteCode.lvl295 (<no location info>)
  GHC.StgToByteCode.schemeER_wrk (compiler/GHC/StgToByteCode.hs:(387,1)-(409,40))
  GHC.StgToByteCode.thenBc (compiler/GHC/StgToByteCode.hs:(2210,1)-(2214,17))
  GHC.StgToByteCode.$fApplicativeBcM7 (<no location info>)
  GHC.StgToByteCode.schemeR_wrk (compiler/GHC/StgToByteCode.hs:(358,1)-(382,60))
  GHC.StgToByteCode.schemeR (compiler/GHC/StgToByteCode.hs:(341,1)-(342,41))
  GHC.StgToByteCode.$wschemeR (compiler/GHC/StgToByteCode.hs:341:1-7)
  GHC.StgToByteCode.schemeTopBind (compiler/GHC/StgToByteCode.hs:(306,1)-(322,55))
  GHC.StgToByteCode.c (<no location info>)
  GHC.StgToByteCode.go11 (<no location info>)
  GHC.StgToByteCode.runBc (compiler/GHC/StgToByteCode.hs:(2206,1)-(2207,63))
  GHC.Utils.Error.$w$swithTiming' (<no location info>)
  GHC.StgToByteCode.byteCodeGen (compiler/GHC/StgToByteCode.hs:(105,1)-(150,38))
  GHC.StgToByteCode.byteCodeGen1 (<no location info>)
  GHC.Driver.Main.hscInteractive (compiler/GHC/Driver/Main.hs:(1963,1)-(2004,49))
  GHC.Driver.Main.generateByteCode2 (<no location info>)
  GHC.Driver.Main.generateByteCode (compiler/GHC/Driver/Main.hs:(2010,1)-(2020,32))
  GHC.Driver.Main.generateByteCode1 (<no location info>)
  GHC.Driver.Main.generateFreshByteCode (compiler/GHC/Driver/Main.hs:(2027,1)-(2031,17))
  GHC.Driver.Main.generateFreshByteCode1 (<no location info>)
  GHC.Driver.Pipeline.Execute.$wrunHscBackendPhase (<no location info>)
  GHC.Driver.Pipeline.Execute.runHscBackendPhase1 (<no location info>)
  GHC.Driver.Pipeline.Execute.runPhase1 (<no location info>)
  GHC.Driver.Pipeline.$w$shscGenBackendPipeline (<no location info>)
  GHC.Driver.Pipeline.fullPipeline2 (<no location info>)
  GHC.Driver.Pipeline.$w$shscPipeline (<no location info>)
  GHC.Driver.Pipeline.$wcompileOne' (<no location info>)
  GHC.Driver.Make.upsweep_mod (compiler/GHC/Driver/Make.hs:(1303,1)-(1315,12))
  GHC.Driver.Make.executeCompileNode.\ (compiler/GHC/Driver/Make.hs:(2518,81)-(2531,16))
  GHC.Driver.Make.withLoggerHsc.\ (compiler/GHC/Driver/Make.hs:(2481,35)-(2485,17))
  GHC.Driver.Make.runSeqPipelines.env.\ (compiler/GHC/Driver/Make.hs:2871:44-47)
  GHC.Driver.Make.runSeqPipelines.env (compiler/GHC/Driver/Make.hs:(2870,7)-(2875,21))
  GHC.Driver.Make.lvl83 (<no location info>)
  GHC.Driver.Make.withLoggerHsc (compiler/GHC/Driver/Make.hs:(2480,1)-(2485,17))
  GHC.Driver.Make.executeCompileNode (compiler/GHC/Driver/Make.hs:(2515,1)-(2540,35))
  GHC.Driver.Make.interpretBuildPlan.buildSingleModule.build_action (compiler/GHC/Driver/Make.hs:(1137,11)-(1161,42))
  GHC.Driver.Make.interpretBuildPlan.buildSingleModule (compiler/GHC/Driver/Make.hs:(1127,5)-(1167,49))
  GHC.Driver.Make.interpretBuildPlan.buildLoop (compiler/GHC/Driver/Make.hs:(1103,5)-(1121,50))
  GHC.Driver.Make.interpretBuildPlan (compiler/GHC/Driver/Make.hs:(1085,1)-(1240,73))
  GHC.Driver.Make.withLocalTmpFS.\ (compiler/GHC/Driver/Make.hs:2949:54-101)
  Control.Monad.Catch.$fMonadMaskMaybeT_$cgeneralBracket (libraries/exceptions/src/Control/Monad/Catch.hs:659:3-16)
  GHC.Driver.Make.withLocalTmpFS (compiler/GHC/Driver/Make.hs:(2938,1)-(2949,101))
  GHC.Driver.Make.runLoop.run_pipeline (compiler/GHC/Driver/Make.hs:2980:7-51)
  GHC.Driver.Make.runLoop.\ (compiler/GHC/Driver/Make.hs:(2972,30)-(2975,33))
  GHC.Driver.Make.runAllPipelines.spawn_actions.\.\ (compiler/GHC/Driver/Make.hs:2957:78-86)
  GHC.Driver.Make.runLoop (compiler/GHC/Driver/Make.hs:(2969,1)-(2980,51))
  GHC.Driver.Make.$srunLoop (<no location info>)
  GHC.Driver.Make.runAllPipelines.spawn_actions.\ (compiler/GHC/Driver/Make.hs:2957:55-96)
  GHC.Driver.Make.runAllPipelines.spawn_actions (compiler/GHC/Driver/Make.hs:(2956,7)-(2958,46))
  GHC.Driver.Make.runAllPipelines (compiler/GHC/Driver/Make.hs:(2953,1)-(2964,29))
  GHC.Driver.Make.runAllPipelines (<no location info>)
  GHC.Driver.Make.runSeqPipelines (compiler/GHC/Driver/Make.hs:(2869,1)-(2876,61))
  GHC.Driver.Make.runPipelines (compiler/GHC/Driver/Make.hs:(2862,1)-(2866,79))
  GHC.Driver.Make.upsweep (compiler/GHC/Driver/Make.hs:(1256,1)-(1272,42))
  Control.Monad.IO.Class.liftIO (libraries/base/src/Control/Monad/IO/Class.hs:66:5-25)
  GHCi.UI.Monad.$fApplicativeGHCi4 (<no location info>)
  GHC.Base.<*> (libraries/base/src/GHC/Base.hs:841:5-37)
  GHC.Driver.Make.load' (compiler/GHC/Driver/Make.hs:(690,1)-(781,31))
  GHC.Driver.Make.load' (compiler/GHC/Driver/Make.hs:690:1-5)
  GHCi.UI.Monad.$fMonadGHCi2 (<no location info>)
  GHC.Driver.Make.loadWithCache (compiler/GHC/Driver/Make.hs:(494,1)-(500,51))
  GHC.Driver.Make.loadWithCache (compiler/GHC/Driver/Make.hs:494:1-13)
  GHCi.UI.$strySuccess (<no location info>)
  GHCi.UI.lvl819 (<no location info>)
  Control.Monad.Catch.$w$cgeneralBracket1 (<no location info>)
  Control.Monad.Catch.$fMonadMaskIO1 (<no location info>)
  Control.Monad.Catch.$fMonadMaskReaderT1 (<no location info>)
  GHC.Driver.Monad.$fMonadMaskGhc1 (<no location info>)
  Control.Monad.Catch.generalBracket (libraries/exceptions/src/Control/Monad/Catch.hs:(342,3)-(350,15))
  Control.Monad.Catch.bracket (libraries/exceptions/src/Control/Monad/Catch.hs:890:1-7)
  Control.Monad.Catch.finally (libraries/exceptions/src/Control/Monad/Catch.hs:902:1-7)
  GHC.withCleanupSession (compiler/GHC.hs:(531,1)-(542,51))
  GHC.Driver.Monad.$fMonadGhc_$s$fMonadReaderT_$c>>= (<no location info>)
  GHC.Base.>>= (libraries/base/src/GHC/Base.hs:1031:5-55)
  GHC.Utils.Panic.withSignalHandlers (compiler/GHC/Utils/Panic.hs:241:1-18)
  GHC.runGhc (compiler/GHC.hs:(506,1)-(511,26))
  GHC.runGhc1 (<no location info>)
  Main.main2 (<no location info>)
  GHC.IO.catch1 (<no location info>)
  Control.Monad.Catch.$fMonadCatchIO1 (<no location info>)
  Control.Monad.Catch.catch (libraries/exceptions/src/Control/Monad/Catch.hs:201:3-66)
  GHC.defaultErrorHandler (compiler/GHC.hs:(443,1)-(478,7))
  GHC.$wdefaultErrorHandler (compiler/GHC.hs:443:1-19)
  Main.main1 (<no location info>)
  GHC.TopHandler.runMainIO1 (<no location info>)
  Main.main3 (<no location info>)
  Main.main (ghc/Main.hs:117:1-4)

so the bug is here: GHC.StgToByteCode.schemeER_wrk (compiler/GHC/StgToByteCode.hs:(387,1)-(409,40))

https://gitlab.haskell.org/wavewave/ghc/-/blob/T24435/compiler/GHC/StgToByteCode.hs?ref_type=heads#L405

tick_no is out of range.

Environment

Edited by Ian-Woo Kim
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information