Skip to content

Segfault on 8.10.7 using Coercible and Text

Summary

The following module (minimised from a closed-source application) crashes with a segfault when compiled under GHC 8.10.7, despite passing -dcore-lint. It seemingly works correctly on 9.0.1 and later. Thus this may be a known and fixed bug, but I couldn't find it on the issue tracker, and I figured a(nother) small test case could be helpful. I'd quite like to understand the circumstances under which this occurs; does it look familiar to anyone?

Steps to reproduce

The segfault is always reproducible on my system with this module, but is very sensitive to small details. For example, removing the unused NoP constructor, or using a string shorter than 4 characters in the first call to Text.pack, make the program print the correct result instead of SIGSEGV.

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# OPTIONS_GHC -dcore-lint -Wall #-}
{-# OPTIONS_GHC -O1 #-}

module Main where

import Data.Coerce
import qualified Data.Text as Text

data P k where
  IsP :: Coercible k Int => P k
  NoP :: P k

f :: P k -> k -> Text.Text
f p k =
    case p of
        IsP -> Text.pack "4444" <> Text.pack (show (coerce k :: Int))
        NoP -> undefined

main :: IO ()
main = print (f IsP (0 :: Int))
$ ghc-8.10.7 Segfault.hs 
[1 of 1] Compiling Main             ( Segfault.hs, Segfault.o )
Linking Segfault ...
$ ./Segfault 
Segmentation fault (core dumped)

Expected behavior

$ ghc-9.0.1 Segfault.hs 
[1 of 1] Compiling Main             ( Segfault.hs, Segfault.o )
Linking Segfault ...
$ ./Segfault 
"44440"

Environment

  • GHC version used: 8.10.7
  • Operating System: Linux
  • System Architecture: x86_64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information