Skip to content

-fPIC on Windows causes problems depending on number of constructors in a type

Summary

The manual states that -fPIC is a no-op on Windows: https://downloads.haskell.org/ghc/latest/docs/users_guide/phases.html#ghc-flag--fPIC. It turns out that actually passing the flag has observable effect manifesting as an unexplained executable crash. The crash seems to only occur when underlying program uses a datatype with 6 constructors. Decreasing it to 5 makes program work with -fPIC.

Steps to reproduce

test.cabal

cabal-version: 3.6

name:
  test
version:
  0.1

build-type:
  Simple

flag pic
  description:
    -fPIC
  default:
    False
  manual:
    True

executable test
  main-is:
    Test.hs
  build-depends:
    , base
    , optparse-applicative
  ghc-options:
    -main-is Test
  if flag(pic)
    ghc-options:
      -fPIC

Test.hs

module Test (main) where

import Options.Applicative
import System.IO

data Command
  = Command1
  | Command2
  | Command3
  | Command4
  | Command5
  | Command6 -- Commenting this line works with -fPIC, uncommenting leads to a crash.
  deriving (Show)

parseCommand :: Parser Command
parseCommand = pure Command2

main :: IO ()
main = do
  putStrLn' "1"
  cmd <- customExecParser (prefs mempty) $ info parseCommand mempty
  putStrLn' "2"
  putStrLn' $ "cmd = " ++ show cmd
  pure ()

putStrLn' :: String -> IO ()
putStrLn' msg = putStrLn msg *> hFlush stdout

Successful run:

$ cabal run -c 'test -pic' test
Resolving dependencies...
Build profile: -w ghc-9.6.2 -O1
In order, the following will be built (use -v for more details):
 - test-0.1 (exe:test) (configuration changed)
Configuring executable 'test' for test-0.1..
Preprocessing executable 'test' for test-0.1..
Building executable 'test' for test-0.1..
[1 of 1] Compiling Test [Flags changed]
[2 of 2] Linking c:\\home\\ghc-windows-bug\\dist-newstyle\\build\\x86_64-windows\\ghc-9.6.2\\test-0.1\\x\\test\\build\\test\\test.exe [Objects changed]
1
2
cmd = Command2

Failing run:

cabal run -c 'test +pic' test
Resolving dependencies...
Build profile: -w ghc-9.6.2 -O1
In order, the following will be built (use -v for more details):
 - test-0.1 (exe:test) (configuration changed)
Configuring executable 'test' for test-0.1..
Preprocessing executable 'test' for test-0.1..
Building executable 'test' for test-0.1..
[1 of 1] Compiling Test [Flags changed]
[2 of 2] Linking c:\\home\\ghc-windows-bug\\dist-newstyle\\build\\x86_64-windows\\ghc-9.6.2\\test-0.1\\x\\test\\build\\test\\test.exe [Objects changed]
1
2

Access violation in generated code when executing data at 0x23f961770

 Attempting to reconstruct a stack trace...

   Frame	Code address
 * 0x2cd980	0x23f961770
 * 0x2cd988	0x1
 * 0x2cd990	0x4b4002cda68
 * 0x2cd998	0x31790

Expected behavior

Absence of unexpected crashes.

Environment

  • GHC version used:

9.6.2, 9.8.0.alpha4

Optional:

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