Skip to content

Segfault regression in GHC 9.2 due to CPR

Summary

A simple executable available at https://github.com/Bodigrim/ghc92-segfault runs fine, when compiled with GHC 9.0.1, but fails with a segfault, when compiled with GHC 9.2.1: https://github.com/Bodigrim/ghc92-segfault/runs/4071678266?check_suite_focus=true#step:4:31

Steps to reproduce

git clone https://github.com/Bodigrim/ghc92-segfault 
cd ghc92-segfault 
cabal run -w ghc-9.2 segfault 

fails with "Segmentation fault (core dumped) cabal run segfault".

segfault.cabal:

cabal-version: 1.16
name: segfault
version: 0.1
build-type: Simple

executable segfault
  main-is: Main.hs
  other-modules: Types, Class
  build-depends: base, vector
  default-language: Haskell2010

Types.hs:

module Types where

import qualified Data.Vector as V

data Value = Unit Double | Number Int
  deriving (Show)

mkArray :: [Value] -> Value
mkArray = mkVector . V.fromList

mkVector :: V.Vector Value -> Value
mkVector vs = case traverse extractNumber vs of
  Nothing -> Unit 43.5
  Just{}  -> Number 42

extractNumber :: Value -> Maybe Int
extractNumber (Number i) = Just i
extractNumber _ = Nothing

Class.hs:

module Class where

import Types

class ToValue a where
  toValue :: a -> Value

instance ToValue Value where
  toValue = id

instance ToValue a => ToValue [a] where
  toValue = mkArray . map toValue

Main.hs:

module Main where

import Types
import Class

main :: IO ()
main = print $ toValue [Number 1]

Certain variations of this code do not crash, but return an incorrect runtime result instead, so labelling as both.

Expected behavior

I expect the executable to run without segfaults.

Environment

  • GHC version used: 9.2.1
Edited by Ben Gamari
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information