Skip to content

Program with safe array operations triggers debug runtime assertion

I'll try to minimize later.

Main.hs:

{-# LANGUAGE ForeignFunctionInterface #-}
import Control.Monad
import Control.Monad.ST
import Data.Primitive.Array
import Data.Primitive.ByteArray
import Data.Primitive.SmallArray
import System.Environment
import System.Mem
import Foreign.C

data A arr = A !Int arr

enumSmallArray :: Int -> A (SmallArray Int)
enumSmallArray n = runST $ do
  arr <- newSmallArray n 0
  forM_ [1..n] $ \i ->
    writeSmallArray arr i i
  iarr <- freezeSmallArray arr 0 n
  return (A n iarr)

consumeSmallArray :: A (SmallArray a) -> a
consumeSmallArray (A n arr) = indexSmallArray arr (n - 1)

enumArray :: Int -> A (Array Int)
enumArray n = runST $ do
  arr <- newArray n 0
  forM_ [1..n] $ \i ->
    writeArray arr i i
  iarr <- freezeArray arr 0 n
  return (A n iarr)

consumeArray :: A (Array a) -> a
consumeArray (A n arr) = indexArray arr (n - 1)

foreign import ccall "printInt"
  printInt :: CInt -> IO ()

main :: IO ()
main = do
  n <- (\[s] -> read s) <$> getArgs
  ints <- forM [1..n] $ \i -> do
    let x = consumeSmallArray (enumSmallArray 12)
        y = consumeArray (enumArray i)
    case x+y of
      r -> when (i `mod` 5000 == 0) performMajorGC
        >> return r
  printInt (fromIntegral (sum ints))

print.c:

#include <stdio.h>

void printInt(int i) { printf("Result: %d\n", i); }

Compile:

$ ghc Main.hs print.c -debug
Linking Main ...

Run:

array_bug $ ./Main 1000 +RTS -DS
Main: internal error: ASSERTION FAILED: file rts/sm/Storage.c, line 960

    (GHC version 8.6.1 for x86_64_unknown_linux)
    Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug
zsh: abort (core dumped)  ./Main 1000 +RTS -DS

Tried with: 8.4.3, 8.6.1.

Trac metadata
Trac field Value
Version 8.6.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Runtime System
Test case
Differential revisions
BlockedBy
Related
Blocking
CC bgamari
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information