Skip to content

Static GHCi can segfault when accessing .bss section in C

When an object file is statically linked, GHCi can return junk or segfault when trying to access data defined in .bss section via foreign call.

watashi % ~/gao/ghc/inplace/bin/ghc-stage2 --info | grep Dynamic
 ,("Dynamic by default","NO")
 ,("GHC Dynamic","NO")
watashi % cat bss.c
int read_bss(int i) {
  static int bss[1 << 20];
  return bss[i];
}
watashi % ~/gao/ghc/inplace/bin/ghc-stage2 --interactive test.o
GHCi, version 8.7.20180920: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/watashi/.ghci
Prelude> :m + Foreign Foreign.C
Prelude Foreign Foreign.C> foreign import ccall unsafe "read_bss" read_bss :: Int -> IO Int
Prelude Foreign Foreign.C> read_bss 0
4294059519
Prelude Foreign Foreign.C> read_bss 1
65535
Prelude Foreign Foreign.C> mapM (read_bss . bit) [0 .. 19]
zsh: segmentation fault (core dumped)  ~/gao/ghc/inplace/bin/ghc-stage2 --interactive test.o
Trac metadata
Trac field Value
Version 8.6.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component GHCi
Test case
Differential revisions
BlockedBy
Related
Blocking
CC simonmar, watashi
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information