Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic executables built by Stack on a macOS/AArch64 system segfault #6386

Closed
mouse07410 opened this issue Dec 17, 2023 · 16 comments
Closed

Comments

@mouse07410
Copy link

General summary/comments (optional)

Stack 2.13.1 fails to compile and link dynamic executables.

To be more specific, it compiles source .hs files to .o, while the linker expects .dyn_o - which causes "File not found error" and linker abort.

Why it matters
Statically linked on x86_64:

$ ll .stack-work/dist/x86_64-osx/ghc-9.6.3/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe
-rwxr-xr-x  1 ur20980  MITLL\Domain Users  1422136 Dec 17 00:04 .stack-work/dist/x86_64-osx/ghc-9.6.3/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe*
$

Statically linked on AARCH64:

$ ll /Users/ur20980/src/hsk-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin
total 22656
drwxr-xr-x  3 ur20980  staff        96 Dec 16 19:15 ./
drwxr-xr-x  6 ur20980  staff       192 Dec 16 19:15 ../
-rwxr-xr-x  1 ur20980  staff  11598672 Dec 16 19:15 hsk-stack-tst1-exe*

Dynamically linked (with Cabal) on x86_64:

$ ll /Users/ur20980/src/hsk-stack-tst1/dist-newstyle/build/x86_64-osx/ghc-9.8.1/hsk-stack-tst1-0.1.0.0/x/hsk-stack-tst1-exe/opt/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe
-rwxr-xr-x  1 ur20980  MITLL\Domain Users  86696 Dec 17 00:15 /Users/ur20980/src/hsk-stack-tst1/dist-newstyle/build/x86_64-osx/ghc-9.8.1/hsk-stack-tst1-0.1.0.0/x/hsk-stack-tst1-exe/opt/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe*
$ 

Dynamically linked (with Cabal) on AARCH64:

$ ll /Users/ur20980/src/hsk-stack-tst1/dist-newstyle/build/aarch64-osx/ghc-9.8.1/hsk-stack-tst1-0.1.0.0/x/hsk-stack-tst1-exe/opt/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe
-rwxr-xr-x  1 ur20980  staff  100160 Dec 16 19:16 /Users/ur20980/src/hsk-stack-tst1/dist-newstyle/build/aarch64-osx/ghc-9.8.1/hsk-stack-tst1-0.1.0.0/x/hsk-stack-tst1-exe/opt/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe*
$ 

Observe two orders of magnitude size increase against Cabal-built binaries.

Steps to reproduce

  1. Make sure -dynamic is present in ghc-options: in ~/.stack/config.yaml (see below)
  2. stack new hsk-stack-tst1
  3. cd hsk-stack-tst1
  4. stack build
  5. Observe the error.

stack.yaml:

# This file was automatically generated by 'stack init'
#
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
resolver:
  url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/0.yaml

packages:
- .

# Override default flag values for local packages and extra-deps
# flags: {}

Relevant part from ~/.stack/config.yaml:

ghc-options:
  # All packages
  "$locals": -Wall -dynamic
  "$targets": -Wall -dynamic
  #"$targets": -Werror -dynamic
  "$everything": -Wall -O2 -fPIC -dynamic -flink-rts -haddock
#apply-ghc-options: locals
#apply-ghc-options: targets
apply-ghc-options: everything

Expected

Here's what's happening when I remove -dynamic from the ghc-options in ~/.stack/config.yaml:

ghc-options:
  # All packages
  "$locals": -Wall #-dynamic
  "$targets": -Wall #-dynamic
  #"$targets": -Werror #-dynamic
  "$everything": -Wall -O2 -fPIC -flink-rts -haddock #-dynamic
#apply-ghc-options: locals
#apply-ghc-options: targets
apply-ghc-options: everything
$ stack build
Building all executables for hsk-stack-tst1 once. After a successful build of all of them, only specified
executables will be rebuilt.
hsk-stack-tst1> configure (lib + exe)
Configuring hsk-stack-tst1-0.1.0.0...
hsk-stack-tst1> build (lib + exe)
Preprocessing library for hsk-stack-tst1-0.1.0.0..
Building library for hsk-stack-tst1-0.1.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Lib.o, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Lib.dyn_o )
[2 of 2] Compiling Paths_hsk_stack_tst1 ( .stack-work/dist/x86_64-osx/ghc-9.6.3/build/autogen/Paths_hsk_stack_tst1.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Paths_hsk_stack_tst1.o, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Paths_hsk_stack_tst1.dyn_o )
ld: warning: -single_module is obsolete
Preprocessing executable 'hsk-stack-tst1-exe' for hsk-stack-tst1-0.1.0.0..
Building executable 'hsk-stack-tst1-exe' for hsk-stack-tst1-0.1.0.0..
[1 of 2] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe-tmp/Main.o )
[2 of 2] Compiling Paths_hsk_stack_tst1 ( .stack-work/dist/x86_64-osx/ghc-9.6.3/build/hsk-stack-tst1-exe/autogen/Paths_hsk_stack_tst1.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe-tmp/Paths_hsk_stack_tst1.o )
[3 of 3] Linking .stack-work/dist/x86_64-osx/ghc-9.6.3/build/hsk-stack-tst1-exe/hsk-stack-tst1-exe
ld: warning: ignoring duplicate libraries: '-lm'
hsk-stack-tst1> copy/register
Installing library in /Users/ur20980/src/hsk-stack-tst1/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/lib/x86_64-osx-ghc-9.6.3/hsk-stack-tst1-0.1.0.0-GDqSklOImIp2BXHmY6Yjrc
Installing executable hsk-stack-tst1-exe in /Users/ur20980/src/hsk-stack-tst1/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin
Registering library for hsk-stack-tst1-0.1.0.0..
$ stack run
someFunc
$

Actual

stack build --verbose
Version 2.13.1, Git revision 8102bb8afce90fc954f48efae38b87f37cabc988 x86_64 hpack-0.36.0
2023-12-16 23:50:11.535727: [debug] Checking for project config at: /Users/ur20980/src/hsk-stack-tst1/stack.yaml
2023-12-16 23:50:11.536519: [debug] Loading project config file stack.yaml
2023-12-16 23:50:11.538220: [debug] Use of Casa server enabled: (CasaRepoPrefix "https://casa.stackage.org", 1280).
2023-12-16 23:50:11.542154: [debug] (SQL) SELECT COUNT(*) FROM "last_performed" WHERE ("action"=?) AND ("timestamp">=?); [PersistInt64 1,PersistUTCTime 2023-12-16 04:50:11.5421 UTC]
2023-12-16 23:50:11.542840: [debug] Using package location completions from a lock file
2023-12-16 23:50:11.545221: [debug] Loaded snapshot from Pantry database.
2023-12-16 23:50:11.728913: [debug] Running Hpack on /Users/ur20980/src/hsk-stack-tst1/package.yaml
2023-12-16 23:50:11.731959: [debug] Hpack output unchanged in /Users/ur20980/src/hsk-stack-tst1/hsk-stack-tst1.cabal
2023-12-16 23:50:11.732223: [debug] Prefetching git repos: []
2023-12-16 23:50:11.732260: [debug] []
2023-12-16 23:50:11.734219: [debug] Asking for a supported GHC version
2023-12-16 23:50:11.734303: [debug] Getting system compiler version
2023-12-16 23:50:11.734941: [debug] (SQL) SELECT "id","actual_version","arch","ghc_path","ghc_size","ghc_modified","ghc_pkg_path","runghc_path","haddock_path","cabal_version","global_db","global_db_cache_size","global_db_cache_modified","info","global_dump" FROM "compiler_cache" WHERE "ghc_path"=?; [PersistText "/Users/ur20980/.ghcup/ghc/9.6.3/bin/ghc-9.6.3"]
2023-12-16 23:50:11.772132: [debug] Loaded compiler information from cache
2023-12-16 23:50:11.772248: [debug] Asking for a supported GHC version
2023-12-16 23:50:11.772288: [debug] Resolving package entries
2023-12-16 23:50:11.772328: [debug] Parsing the targets
2023-12-16 23:50:11.775389: [debug] Checking flags
2023-12-16 23:50:11.775441: [debug] SourceMap constructed
2023-12-16 23:50:11.782498: [debug] Starting to execute command inside EnvConfig
2023-12-16 23:50:11.784629: [debug] Finding out which packages are already installed
2023-12-16 23:50:11.784817: [debug] Run process: /Users/ur20980/.ghcup/ghc/9.6.3/bin/ghc-pkg-9.6.3 --global --no-user-package-db dump --expand-pkgroot
2023-12-16 23:50:11.853858: [debug] Process finished in 68976353000ms: /Users/ur20980/.ghcup/ghc/9.6.3/bin/ghc-pkg-9.6.3 --global --no-user-package-db dump --expand-pkgroot
2023-12-16 23:50:11.876025: [debug] Run process: /Users/ur20980/.ghcup/ghc/9.6.3/bin/ghc-pkg-9.6.3 --user --no-user-package-db --package-db /Users/ur20980/.stack/snapshots/x86_64-osx/f1d2b2c38a6385c2646d252ebac9faa449a1727e7927147ce1bfe277d309ffd2/9.6.3/pkgdb dump --expand-pkgroot
2023-12-16 23:50:11.932553: [debug] Process finished in 56438636000ms: /Users/ur20980/.ghcup/ghc/9.6.3/bin/ghc-pkg-9.6.3 --user --no-user-package-db --package-db /Users/ur20980/.stack/snapshots/x86_64-osx/f1d2b2c38a6385c2646d252ebac9faa449a1727e7927147ce1bfe277d309ffd2/9.6.3/pkgdb dump --expand-pkgroot
2023-12-16 23:50:11.932775: [debug] Run process: /Users/ur20980/.ghcup/ghc/9.6.3/bin/ghc-pkg-9.6.3 --user --no-user-package-db --package-db /Users/ur20980/src/hsk-stack-tst1/.stack-work/install/x86_64-osx/f1d2b2c38a6385c2646d252ebac9faa449a1727e7927147ce1bfe277d309ffd2/9.6.3/pkgdb dump --expand-pkgroot
2023-12-16 23:50:11.989090: [debug] Process finished in 56232736000ms: /Users/ur20980/.ghcup/ghc/9.6.3/bin/ghc-pkg-9.6.3 --user --no-user-package-db --package-db /Users/ur20980/src/hsk-stack-tst1/.stack-work/install/x86_64-osx/f1d2b2c38a6385c2646d252ebac9faa449a1727e7927147ce1bfe277d309ffd2/9.6.3/pkgdb dump --expand-pkgroot
2023-12-16 23:50:11.989399: [debug] Constructing the build plan
2023-12-16 23:50:11.990190: [debug] Checking if we are going to build multiple executables with the same name
2023-12-16 23:50:11.990252: [debug] Executing the build plan
2023-12-16 23:50:11.991557: [info] Building all executables for hsk-stack-tst1 once. After a successful build of all of them, only specified
executables will be rebuilt.
2023-12-16 23:50:11.991776: [debug] (SQL) SELECT "id","directory","type","pkg_src","active","path_env_var","haddock" FROM "config_cache" WHERE "directory"=? AND "type"=?; [PersistText "/Users/ur20980/src/hsk-stack-tst1/",PersistText "config"]
2023-12-16 23:50:11.992025: [debug] (SQL) SELECT "id", "config_cache_id", "index", "option" FROM "config_cache_dir_option" WHERE ("config_cache_id"=?) ORDER BY "index"; [PersistInt64 1]
2023-12-16 23:50:11.992162: [debug] (SQL) SELECT "id", "config_cache_id", "index", "option" FROM "config_cache_no_dir_option" WHERE ("config_cache_id"=?) ORDER BY "index"; [PersistInt64 1]
2023-12-16 23:50:11.992321: [debug] (SQL) SELECT "id", "config_cache_id", "ghc_pkg_id" FROM "config_cache_dep" WHERE ("config_cache_id"=?); [PersistInt64 1]
2023-12-16 23:50:11.992382: [debug] (SQL) SELECT "id", "config_cache_id", "component" FROM "config_cache_component" WHERE ("config_cache_id"=?); [PersistInt64 1]
2023-12-16 23:50:11.992763: [debug] Start: getPackageFiles /Users/ur20980/src/hsk-stack-tst1/hsk-stack-tst1.cabal
2023-12-16 23:50:11.995153: [debug] Finished in 2335410000ms: getPackageFiles /Users/ur20980/src/hsk-stack-tst1/hsk-stack-tst1.cabal
2023-12-16 23:50:11.996653: [info] hsk-stack-tst1> build (lib + exe)
2023-12-16 23:50:11.996838: [debug] Run process within /Users/ur20980/src/hsk-stack-tst1/: /Users/ur20980/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_6HauvNHV_3.10.1.0_ghc-9.6.3 --verbose=1 --builddir=.stack-work/dist/x86_64-osx/ghc-9.6.3 build lib:hsk-stack-tst1 exe:hsk-stack-tst1-exe --ghc-options " -fdiagnostics-color=always"
2023-12-16 23:50:12.078002: [info] Preprocessing library for hsk-stack-tst1-0.1.0.0..
2023-12-16 23:50:12.078153: [info] Building library for hsk-stack-tst1-0.1.0.0..
2023-12-16 23:50:12.158932: [warn] 
2023-12-16 23:50:12.159093: [warn] when making flags consistent: warning:
2023-12-16 23:50:12.159234: [warn]     -dynamic-too is ignored when using -dynamic
2023-12-16 23:50:12.445642: [warn] clang: error: no such file or directory: '.stack-work/dist/x86_64-osx/ghc-9.6.3/build/Lib.dyn_o'
2023-12-16 23:50:12.450531: [warn] clang: error: no such file or directory: '.stack-work/dist/x86_64-osx/ghc-9.6.3/build/Paths_hsk_stack_tst1.dyn_o'
2023-12-16 23:50:12.452756: [warn] `clang' failed in phase `Linker'. (Exit code: 1)
2023-12-16 23:50:12.476869: [debug] Checking for project config at: /Users/ur20980/src/hsk-stack-tst1/stack.yaml
2023-12-16 23:50:12.476973: [debug] Loading project config file stack.yaml
2023-12-16 23:50:12.478209: [debug] Use of Casa server enabled: (CasaRepoPrefix "https://casa.stackage.org", 1280).
2023-12-16 23:50:12.481483: [debug] (SQL) SELECT COUNT(*) FROM "last_performed" WHERE ("action"=?) AND ("timestamp">=?); [PersistInt64 1,PersistUTCTime 2023-12-16 04:50:12.481441 UTC]
2023-12-16 23:50:12.482877: [error] 
Error: [S-7282]
       Stack failed to execute the build plan.
       
       While executing the build plan, Stack encountered the error:
       
       [S-7011]
       While building package hsk-stack-tst1-0.1.0.0 (scroll up to its section to see the error) using:
       /Users/ur20980/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_6HauvNHV_3.10.1.0_ghc-9.6.3 --verbose=1 --builddir=.stack-work/dist/x86_64-osx/ghc-9.6.3 build lib:hsk-stack-tst1 exe:hsk-stack-tst1-exe --ghc-options " -fdiagnostics-color=always"
       Process exited with code: ExitFailure 1

Stack version

$ stack --version
Version 2.13.1, Git revision 8102bb8afce90fc954f48efae38b87f37cabc988 x86_64 hpack-0.36.0

Method of installation

  • Via GHCup

Platform

MacOS Sonoma 14.2, multiple versions of GHC (9.2.8, 9.4.8, 9.6.3, 9.8.1) - everything installed via GHCup.

@mpilgrem
Copy link
Member

@mouse07410, is this a regression compared to earlier versions of Stack? I am thinking of your issue #5199.

@mouse07410
Copy link
Author

mouse07410 commented Dec 17, 2023

Yes it is - because back then it built executables successfully, and the problem was installing dynamic libraries into the right places (which stack failed to do).

Now it can't even build them, creating .o when it should've created .dyn_o.

I haven't been using stack for years, but am trying to maintain a working setup just in case, so decided to test it - and reported the problem.

I'm pretty sure one of the earlier versions of stack could do least do stack run correctly.

@mpilgrem
Copy link
Member

@mouse07410, on WSL2 (Ubuntu 22.04.3 LTS), with a simple 'toy' one-package project foo (stack new foo), I tried stack.yaml:

resolver: lts-22.0
configure-options:
  $targets:
  - --enable-executable-dynamic

making use of https://cabal.readthedocs.io/en/3.4/cabal-project.html#cfg-flag---enable-executable-dynamic. That seemed to work - everything built and the executable was much smaller than the one built without specifying that Cabal configuration option.

@mouse07410
Copy link
Author

@mpilgrem very interesting. I copied your excerpt into stack.yaml of the toy project, and it builds fine - but crashes on execution:

$ stack build
h-stack-tst1-0.1.0.0: unregistering (local file changes: CHANGELOG.md README.md app/Main.hs
h-stack-tst1.cabal package.yaml src/Lib.hs)
Building all executables for h-stack-tst1 once. After a successful build of all of them, only specified
executables will be rebuilt.
h-stack-tst1> configure (lib + exe)
Configuring h-stack-tst1-0.1.0.0...
h-stack-tst1> build (lib + exe)
Preprocessing library for h-stack-tst1-0.1.0.0..
Building library for h-stack-tst1-0.1.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Lib.o, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Lib.dyn_o )
[2 of 2] Compiling Paths_h_stack_tst1 ( .stack-work/dist/aarch64-osx/ghc-9.6.3/build/autogen/Paths_h_stack_tst1.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Paths_h_stack_tst1.o, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Paths_h_stack_tst1.dyn_o )
ld: warning: -single_module is obsolete
Preprocessing executable 'h-stack-tst1-exe' for h-stack-tst1-0.1.0.0..
Building executable 'h-stack-tst1-exe' for h-stack-tst1-0.1.0.0..
[1 of 2] Compiling Main             ( app/Main.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/h-stack-tst1-exe-tmp/Main.dyn_o )
[2 of 2] Compiling Paths_h_stack_tst1 ( .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/autogen/Paths_h_stack_tst1.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/h-stack-tst1-exe-tmp/Paths_h_stack_tst1.dyn_o )
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/h-stack-tst1-exe
ld: warning: ignoring duplicate libraries: '-lm'
h-stack-tst1> copy/register
Installing library in /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/lib/aarch64-osx-ghc-9.6.3/h-stack-tst1-0.1.0.0-HLOUcWGFe3L9flc5gzPiOW
Installing executable h-stack-tst1-exe in /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin
Registering library for h-stack-tst1-0.1.0.0..
$ otool -L /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe 
/Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
	@rpath/libHSh-stack-tst1-0.1.0.0-HLOUcWGFe3L9flc5gzPiOW-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSbase-4.18.1.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-bignum-1.3-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-prim-0.10.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSrts-1.0.2_thr-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/opt/local/lib/libffi.8.dylib (compatibility version 10.0.0, current version 10.2.0)
$ stack run
zsh: segmentation fault  stack run
$ 

Attempt to debug gave this:

$ lldb /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe
(lldb) target create "/Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe"
Current executable set to '/Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe' (arm64).
(lldb) run
Process 28450 launched: '/Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe' (arm64)
Process 28450 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001007a3b1c libHSrts-1.0.2-ghc9.6.3.dylib`newCAF + 240
libHSrts-1.0.2-ghc9.6.3.dylib`newCAF:
->  0x1007a3b1c <+240>: ldr    w23, [x8]
    0x1007a3b20 <+244>: ldrb   w8, [x22, #0x50]
    0x1007a3b24 <+248>: cmp    w23, #0x0
    0x1007a3b28 <+252>: ccmp   w8, #0x0, #0x0, ne
Target 0: (h-stack-tst1-exe) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x00000001007a3b1c libHSrts-1.0.2-ghc9.6.3.dylib`newCAF + 240
    frame #1: 0x00000001007b213c libHSrts-1.0.2-ghc9.6.3.dylib`stg_unpack_cstring_info + 20
    frame #2: 0x00000001006d6164 libHSrts-1.0.2_thr-ghc9.6.3.dylib`schedule + 1184
(lldb) 

From the crash report:

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               h-stack-tst1-exe [28289]
Path:                  /Users/USER/*/h-stack-tst1-exe
Identifier:            h-stack-tst1-exe
Version:               ???
Code Type:             ARM-64 (Native)
Parent Process:        zsh [1334]
Responsible:           Terminal [1086]
User ID:               501

Date/Time:             2023-12-18 19:27:15.9589 -0500
OS Version:            macOS 14.2 (23C64)
Report Version:        12
Anonymous UUID:        161C054B-E964-CDD3-5EBC-5A9DBE3E2AE2

Sleep/Wake UUID:       FF67E68F-17CF-417E-959C-159423F82291

Time Awake Since Boot: 550000 seconds
Time Since Wake:       4957 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Codes:       0x0000000000000001, 0x0000000000000000

Termination Reason:    Namespace SIGNAL, Code 11 Segmentation fault: 11
Terminating Process:   exc handler [28289]

VM Region Info: 0 is not in any region.  Bytes before following region: 4378607616
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      104fc4000-104fd0000    [   48K] r-x/r-x SM=COW  ...tack-tst1-exe

Kernel Triage:
VM - (arg = 0x3) mach_vm_allocate_kernel failed within call to vm_map_enter


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libHSrts-1.0.2-ghc9.6.3.dylib 	       0x105767b1c newCAF + 240
1   libHSrts-1.0.2_thr-ghc9.6.3.dylib	       0x10569a164 schedule + 1184
2   libHSrts-1.0.2_thr-ghc9.6.3.dylib	       0x10569a164 schedule + 1184
3   dyld                          	       0x1890150e0 start + 2360

@mpilgrem
Copy link
Member

mpilgrem commented Dec 20, 2023

@mouse07410, I have access to a Mac mini M1 2020, with macOS 14.1/AArch64. I can't reproduce your problem; for me, macOS behaves the same way as Ubuntu and stack exec -- foo-exe or stack run behave as expected:

$ stack build
Building all executables for foo once. After a successful build of all of them, only specified executables will be rebuilt.
foo> configure (lib + exe)
Configuring foo-0.1.0.0...
foo> build (lib + exe) with ghc-9.6.3
Preprocessing library for foo-0.1.0.0..
Building library for foo-0.1.0.0..
[1 of 2] Compiling Lib
[2 of 2] Compiling Paths_foo
Preprocessing executable 'foo-exe' for foo-0.1.0.0..
Building executable 'foo-exe' for foo-0.1.0.0..
[1 of 2] Compiling Main
[2 of 2] Compiling Paths_foo
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.6.3/build/foo-exe/foo-exe
foo> copy/register
Installing library in /Users/mpilgrem/Haskell/foo/.stack-work/install/aarch64-osx/7d72371bc6590ebf409d9a2ea2b40a9b5985ad62eb97b33ee37b405eda5c07fc/9.6.3/lib/aarch64-osx-ghc-9.6.3/foo-0.1.0.0-30mdxUj6JUxGRkonHT0Pmf
Installing executable foo-exe in /Users/mpilgrem/Haskell/foo/.stack-work/install/aarch64-osx/7d72371bc6590ebf409d9a2ea2b40a9b5985ad62eb97b33ee37b405eda5c07fc/9.6.3/bin
Registering library for foo-0.1.0.0..

Compared to my experience, you are getting linker errors:

ld: warning: -single_module is obsolete
...
ld: warning: ignoring duplicate libraries: '-lm'

On my system, in case it is relevant:

$ which ld
/usr/bin/ld
$ ld -v
@(#)PROGRAM:ld  PROJECT:ld64-857.1
BUILD 23:13:29 May  7 2023
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.3, (clang-1403.0.22.14.1) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.3 (tapi-1403.0.5.1)

My otool output also differs from yours:

otool -L /Users/mpilgrem/Haskell/foo/.stack-work/install/aarch64-osx/7d72371bc6590ebf409d9a2ea2b40a9b5985ad62eb97b33ee37b405eda5c07fc/9.6.3/bin/foo
-exe
/Users/mpilgrem/Haskell/foo/.stack-work/install/aarch64-osx/7d72371bc6590ebf409d9a2ea2b40a9b5985ad62eb97b33ee37b405eda5c07fc/9.6.3/bin/foo-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.100.3)
	@rpath/libHSfoo-0.1.0.0-30mdxUj6JUxGRkonHT0Pmf-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSbase-4.18.1.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-prim-0.10.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSrts-1.0.2_thr-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)

Noticing that you have extra references to libiconv, I wondered if this was in any way relevant: #6182 (comment) - something to do with non-Apple libiconv being present on a system causing (other) problems. EDIT: I now think that is not an explanation. I have updated to macOS 14.2.1. Everything still works, as before, but now I have:

$ ld -v
@(#)PROGRAM:ld  PROJECT:dyld-1022.1
BUILD 13:21:42 Nov 10 2023
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h
will use ld-classic for: armv6 armv7 armv7s arm64_32 i386 armv6m armv7k armv7m armv7em
LTO support using: LLVM version 15.0.0 (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 15.0.0 (tapi-1500.0.12.8)
Library search paths:
Framework search paths: 

$ otool -L /Users/mpilgrem/Haskell/foo/.stack-work/install/aarch64-osx/7d72371bc6590ebf409d9a2ea2b40a9b5985ad62eb97b33ee37b405eda5c07fc/9.6.3/bin/foo-exe
/Users/mpilgrem/Haskell/foo/.stack-work/install/aarch64-osx/7d72371bc6590ebf409d9a2ea2b40a9b5985ad62eb97b33ee37b405eda5c07fc/9.6.3/bin/foo-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
	@rpath/libHSfoo-0.1.0.0-30mdxUj6JUxGRkonHT0Pmf-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSbase-4.18.1.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-bignum-1.3-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-prim-0.10.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSrts-1.0.2_thr-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/usr/lib/libffi.dylib (compatibility version 1.0.0, current version 32.0.0)

which looks very similar to your output, save for libffi:

your experience: /opt/local/lib/libffi.8.dylib (compatibility version 10.0.0, current version 10.2.0)
my experience: /usr/lib/libffi.dylib (compatibility version 1.0.0, current version 32.0.0)

EDIT2: Perhaps this past Stack issue is relevant: #1826

@mpilgrem mpilgrem changed the title Stack fails to build dynamic executables Dynamic executables built by Stack on a macOS/AArch64 system segfault Dec 20, 2023
@mouse07410
Copy link
Author

mouse07410 commented Dec 21, 2023

Interesting. Very interesting. I'm now at 14.2.1 too. Xcode-15.1.

Most of the packages (such as GCC-13, etc.) that don't come with the OS itself, are installed via MacPorts. Unfortunately, MacPorts has its own versions of ``libiconv, libcurl`, and (now, apparently) `libffi`. Thus, anything MacPorts-installed has to link against MacPorts versions of those libraries.

How, Haskell (GHC, Cabal, your Stack, etc.) binary distributions rely upon the MacOS (or, rather, XCode) versions of those libraries. Which means: in Haskell software that needs to use or depend on, e.g., OpenSSL, I have to link it with the above MacOS libraries first, then with the relevant MacPorts libraries - and then with the rest of the MacOS libraries. It's crazy - I know, but that's the only way I found for "it" to work.

After ensuring that /opt/local/lib/liconv/ directory contains the Xcode versions of those libraries, and adding libffi there, Stack sample worked correctly, except that it treated all the Haskell libraries as static:

$ stack run
Building all executables for h-tst5 once. After a successful build of all of them, only specified executables
will be rebuilt.
h-tst5> configure (lib + exe)
Configuring h-tst5-0.1.0.0...
h-tst5> build (lib + exe)
Preprocessing library for h-tst5-0.1.0.0..
Building library for h-tst5-0.1.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Lib.o, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Lib.dyn_o )
[2 of 2] Compiling Paths_h_tst5     ( .stack-work/dist/x86_64-osx/ghc-9.6.3/build/autogen/Paths_h_tst5.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Paths_h_tst5.o, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Paths_h_tst5.dyn_o )
ld: warning: -single_module is obsolete
Preprocessing executable 'h-tst5-exe' for h-tst5-0.1.0.0..
Building executable 'h-tst5-exe' for h-tst5-0.1.0.0..
[1 of 2] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe-tmp/Main.o )
[2 of 2] Compiling Paths_h_tst5     ( .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/autogen/Paths_h_tst5.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe-tmp/Paths_h_tst5.o )
[3 of 3] Linking .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe
ld: warning: ignoring duplicate libraries: '-lm'
h-tst5> copy/register
Installing library in /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/lib/x86_64-osx-ghc-9.6.3/h-tst5-0.1.0.0-GPS02nDvvbOBzEVV7Y3LLs
Installing executable h-tst5-exe in /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin
Registering library for h-tst5-0.1.0.0..
someFunc
$
$ ll /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe 
-rwxr-xr-x  1 ur20980  staff  1180136 Dec 21 16:16 /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe*
$ otool -L /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe 
/Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/usr/lib/libffi.dylib (compatibility version 1.0.0, current version 32.0.0)
	/opt/local/lib/libcharset.1.dylib (compatibility version 2.0.0, current version 2.0.0)
$ 

When I added

resolver: lts-22.0
configure-options:
  $targets:
  - --enable-executable-dynamic

to the project's stack.yaml, I got the crash again:

$ stack run
Warning: /Users/ur20980/.stack/config.yaml: Unrecognized field in ConfigMonoid: confgure-options
h-tst5-0.1.0.0: unregistering (flags changed from
["--extra-include-dirs=/opt/local/include","--extra-include-dirs=/usr/local/include","--extra-lib-dirs=/opt/local/lib/liconv","--extra-lib-dirs=/opt/local/lib","--extra-lib-dirs=/usr/local/lib"]
to
["--enable-executable-dynamic","--extra-include-dirs=/opt/local/include","--extra-include-dirs=/usr/local/include","--extra-lib-dirs=/opt/local/lib/liconv","--extra-lib-dirs=/opt/local/lib","--extra-lib-dirs=/usr/local/lib"])
h-tst5> configure (lib + exe)
Configuring h-tst5-0.1.0.0...
h-tst5> build (lib + exe)
Preprocessing library for h-tst5-0.1.0.0..
Building library for h-tst5-0.1.0.0..
ld: warning: -single_module is obsolete
Preprocessing executable 'h-tst5-exe' for h-tst5-0.1.0.0..
Building executable 'h-tst5-exe' for h-tst5-0.1.0.0..
[1 of 2] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe-tmp/Main.dyn_o )
[2 of 2] Compiling Paths_h_tst5     ( .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/autogen/Paths_h_tst5.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe-tmp/Paths_h_tst5.dyn_o )
[3 of 3] Linking .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe [Objects changed]
ld: warning: ignoring duplicate libraries: '-lm'
h-tst5> copy/register
Installing library in /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/lib/x86_64-osx-ghc-9.6.3/h-tst5-0.1.0.0-GPS02nDvvbOBzEVV7Y3LLs
Installing executable h-tst5-exe in /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin
Registering library for h-tst5-0.1.0.0..
Segmentation fault: 11
$ ll /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe 
-rwxr-xr-x  1 ur20980  staff  86624 Dec 21 16:29 /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe*
$ otool -L /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe
/Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/c8e1cf8670f6a55155dbf2920d71a6a9438fa88587bfbb063b1710febe6c77e9/9.6.3/bin/h-tst5-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
	@rpath/libHSh-tst5-0.1.0.0-GPS02nDvvbOBzEVV7Y3LLs-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSbase-4.18.1.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-bignum-1.3-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-prim-0.10.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSrts-1.0.2_thr-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/usr/lib/libffi.dylib (compatibility version 1.0.0, current version 32.0.0)
$ 

Here's the crash report:

Process:               h-tst5-exe [44734]
Path:                  /Users/USER/*/h-tst5-exe
Identifier:            h-tst5-exe
Version:               ???
Code Type:             X86-64 (Native)
Parent Process:        bash [1591]
Responsible:           Terminal [1081]
User ID:               501

Date/Time:             2023-12-21 16:38:29.9410 -0500
OS Version:            macOS 14.2.1 (23C71)
Report Version:        12
Bridge OS Version:     8.2 (21P2057)
Anonymous UUID:        BD844EB9-9C6F-867E-78EB-1ACDA55970A0


Time Awake Since Boot: 160000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Codes:       0x0000000000000001, 0x0000000000000000

Termination Reason:    Namespace SIGNAL, Code 11 Segmentation fault: 11
Terminating Process:   exc handler [44734]

VM Region Info: 0 is not in any region.  Bytes before following region: 4456939520
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      109a78000-109a84000    [   48K] r-x/r-x SM=COW  .../*/h-tst5-exe

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libHSrts-1.0.2-ghc9.6.3.dylib 	       0x10a2b3bf3 newCAF + 67

And a similar crash on M2 MacOS 14.2.1:

$ stack run
h-stack-tst1-0.1.0.0: unregistering (flags changed from
["--extra-include-dirs=/opt/local/include","--extra-include-dirs=/usr/local/include","--extra-lib-dirs=/opt/local/lib/liconv","--extra-lib-dirs=/opt/local/lib","--extra-lib-dirs=/usr/local/lib"]
to
["--enable-executable-dynamic","--extra-include-dirs=/opt/local/include","--extra-include-dirs=/usr/local/include","--extra-lib-dirs=/opt/local/lib/liconv","--extra-lib-dirs=/opt/local/lib","--extra-lib-dirs=/usr/local/lib"])
Building all executables for h-stack-tst1 once. After a successful build of all of them, only specified
executables will be rebuilt.
h-stack-tst1> configure (lib + exe)
Configuring h-stack-tst1-0.1.0.0...
h-stack-tst1> build (lib + exe)
Preprocessing library for h-stack-tst1-0.1.0.0..
Building library for h-stack-tst1-0.1.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Lib.o, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Lib.dyn_o )
[2 of 2] Compiling Paths_h_stack_tst1 ( .stack-work/dist/aarch64-osx/ghc-9.6.3/build/autogen/Paths_h_stack_tst1.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Paths_h_stack_tst1.o, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/Paths_h_stack_tst1.dyn_o )
ld: warning: -single_module is obsolete
Preprocessing executable 'h-stack-tst1-exe' for h-stack-tst1-0.1.0.0..
Building executable 'h-stack-tst1-exe' for h-stack-tst1-0.1.0.0..
[1 of 2] Compiling Main             ( app/Main.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/h-stack-tst1-exe-tmp/Main.dyn_o )
[2 of 2] Compiling Paths_h_stack_tst1 ( .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/autogen/Paths_h_stack_tst1.hs, .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/h-stack-tst1-exe-tmp/Paths_h_stack_tst1.dyn_o )
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/h-stack-tst1-exe
ld: warning: ignoring duplicate libraries: '-lm'
h-stack-tst1> copy/register
Installing library in /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/lib/aarch64-osx-ghc-9.6.3/h-stack-tst1-0.1.0.0-HLOUcWGFe3L9flc5gzPiOW
Installing executable h-stack-tst1-exe in /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin
Registering library for h-stack-tst1-0.1.0.0..
zsh: segmentation fault  stack run

with crash:

Process:               h-stack-tst1-exe [69347]
Path:                  /Users/USER/*/h-stack-tst1-exe
Identifier:            h-stack-tst1-exe
Version:               ???
Code Type:             ARM-64 (Native)
Parent Process:        zsh [3414]
Responsible:           Terminal [3195]
User ID:               501

Date/Time:             2023-12-21 17:00:24.6454 -0500
OS Version:            macOS 14.2.1 (23C71)
Report Version:        12
Anonymous UUID:        161C054B-E964-CDD3-5EBC-5A9DBE3E2AE2


Time Awake Since Boot: 160000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Codes:       0x0000000000000001, 0x0000000000000000

Termination Reason:    Namespace SIGNAL, Code 11 Segmentation fault: 11
Terminating Process:   exc handler [69347]

VM Region Info: 0 is not in any region.  Bytes before following region: 4341907456
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      102cc4000-102cd0000    [   48K] r-x/r-x SM=COW  ...tack-tst1-exe

Kernel Triage:
VM - (arg = 0x3) mach_vm_allocate_kernel failed within call to vm_map_enter


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libHSrts-1.0.2-ghc9.6.3.dylib 	       0x103467b1c newCAF + 240
1   libHSrts-1.0.2_thr-ghc9.6.3.dylib	       0x10339a164 schedule + 1184
2   libHSrts-1.0.2_thr-ghc9.6.3.dylib	       0x10339a164 schedule + 1184
3   dyld                          	       0x18b3e10e0 start + 2360

@mouse07410
Copy link
Author

Removing /opt/locall/lib/ altogether (because this toy project doesn't need any of those packages), still leads to crash:

$ stack run
h-stack-tst1-0.1.0.0: unregistering (flags changed from
["--extra-lib-dirs=/opt/local/lib/liconv","--extra-lib-dirs=/usr/local/lib"] to
["--extra-lib-dirs=/usr/local/lib"])
h-stack-tst1> configure (lib + exe)
Configuring h-stack-tst1-0.1.0.0...
h-stack-tst1> build (lib + exe)
Preprocessing library for h-stack-tst1-0.1.0.0..
Building library for h-stack-tst1-0.1.0.0..
ld: warning: -single_module is obsolete
Preprocessing executable 'h-stack-tst1-exe' for h-stack-tst1-0.1.0.0..
Building executable 'h-stack-tst1-exe' for h-stack-tst1-0.1.0.0..
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.6.3/build/h-stack-tst1-exe/h-stack-tst1-exe [Library changed]
ld: warning: ignoring duplicate libraries: '-lm'
h-stack-tst1> copy/register
Installing library in /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/lib/aarch64-osx-ghc-9.6.3/h-stack-tst1-0.1.0.0-HLOUcWGFe3L9flc5gzPiOW
Installing executable h-stack-tst1-exe in /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin
Registering library for h-stack-tst1-0.1.0.0..
zsh: segmentation fault  stack run
$ otool -L /Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe
/Users/ur20980/src/h-stack-tst1/.stack-work/install/aarch64-osx/89d39da80602756ab996a9b78727e7b07dd4e0adde41b5ea60d940c51b3160ca/9.6.3/bin/h-stack-tst1-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
	@rpath/libHSh-stack-tst1-0.1.0.0-HLOUcWGFe3L9flc5gzPiOW-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSbase-4.18.1.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-bignum-1.3-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-prim-0.10.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSrts-1.0.2_thr-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/usr/lib/libffi.dylib (compatibility version 1.0.0, current version 32.0.0)
$ 

@mpilgrem
Copy link
Member

For completeness, there are GHC issues which seems to have something to do with the ld warnings: https://gitlab.haskell.org/ghc/ghc/-/issues/24168 and https://gitlab.haskell.org/ghc/ghc/-/issues/24167.

@mouse07410
Copy link
Author

And, again, somehow Cabal doesn't seem to manifest this problem:

$ cabal run
Resolving dependencies...
Build profile: -w ghc-9.8.1 -O2
In order, the following will be built (use -v for more details):
 - h-tst5-0.1.0.0 (lib) (first run)
 - h-tst5-0.1.0.0 (exe:h-tst5-exe) (first run)
Configuring library for h-tst5-0.1.0.0..
Preprocessing library for h-tst5-0.1.0.0..
Building library for h-tst5-0.1.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/opt/build/Lib.o, /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/opt/build/Lib.dyn_o )
[2 of 2] Compiling Paths_h_tst5     ( /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/opt/build/autogen/Paths_h_tst5.hs, /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/opt/build/Paths_h_tst5.o, /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/opt/build/Paths_h_tst5.dyn_o )
ld: warning: -single_module is obsolete
Configuring executable 'h-tst5-exe' for h-tst5-0.1.0.0..
Preprocessing executable 'h-tst5-exe' for h-tst5-0.1.0.0..
Building executable 'h-tst5-exe' for h-tst5-0.1.0.0..
[1 of 2] Compiling Main             ( app/Main.hs, /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/h-tst5-exe-tmp/Main.dyn_o )
[2 of 2] Compiling Paths_h_tst5     ( /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/autogen/Paths_h_tst5.hs, /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/h-tst5-exe-tmp/Paths_h_tst5.dyn_o )
[3 of 3] Linking /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/h-tst5-exe
ld: warning: ignoring duplicate libraries: '-lm'
someFunc
$ otool -L /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/h-tst5-exe
/Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/h-tst5-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
	@rpath/libHSh-tst5-0.1.0.0-inplace-ghc9.8.1.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSbase-4.19.0.0-d6d2-ghc9.8.1.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-bignum-1.3-93be-ghc9.8.1.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-prim-0.11.0-5379-ghc9.8.1.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSrts-1.0.2_thr-ghc9.8.1.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/usr/lib/libffi.dylib (compatibility version 1.0.0, current version 32.0.0)
$ ll !!:$
ll /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/h-tst5-exe
-rwxr-xr-x  1 ur20980  staff  86672 Dec 21 17:52 /Users/ur20980/src/h-tst5/dist-newstyle/build/x86_64-osx/ghc-9.8.1/h-tst5-0.1.0.0/x/h-tst5-exe/opt/build/h-tst5-exe/h-tst5-exe*
$ 

Any idea what Stack is doing differently here???

@mpilgrem
Copy link
Member

@mouse07410, so we are comparing like for like, what does Cabal (the tool) do with GHC 9.6.3?

We can see the Cabal commands that Stack is relying on with stack --verbose build and (for full information) also the GHC commands that Cabal is, in turn, relying on with stack --verbose build --cabal-verbose. I'll see if I can boil that down to the essentials on my system ...

@mouse07410
Copy link
Author

Sure. Here's the log (output of stack --verbose build --cabal-verbose).
stack-verbose.log.txt

For comparison, output of cabal clean; cabal run --verbose (GHC set to 9.6.3, to ensure comparison of apples-to-apples).
cabal-verbose.log.txt

@mpilgrem
Copy link
Member

@mouse07410, I don't know if it has any significance, but I've spotted these differences in terms of the output from Stack:

  • in the configuration of Cabal, I am just passing --enable-executable-dynamic. You (somewhere - in your config.yaml perhaps?) are also passing --ghc-options -fPIC and --ghc-options -flink-rts. You are also passing --extra-include-dirs=/usr/local/include and --extra-lib-dirs=/usr/local/lib; I am not.

  • So, in the GHC --make of Lib, you are passing -fPIC and -flink-rts. I am not. You also have -I/usr/local/include.

  • So, in the GHC (implicit) --make of Lib.dyn_o, you are also passing -fPIC and -flink-rts. I am not. You also have -L/usr/local/lib.

  • The same is true in the GHC --make of app/Main.hs with -no-link, except that GHC is passed -fPIC in any event (so your passing of -fPIC is redundant).

  • The same is also true in the final GHC --make of app/Main.hs (without -no-link). Again GHC is passed -fPIC in any event.

@mouse07410
Copy link
Author

mouse07410 commented Dec 22, 2023

in the configuration of Cabal, I am just passing --enable-executable-dynamic.

I wonder - you're passing it through Stack, right?I tried to find a way to pass it via ~/.stack/config.yaml, but did not figure out how.

You (somewhere - in your config.yaml perhaps?) are also passing --ghc-options -fPIC and --ghc-options -flink-rts.

Yes I do. They don't seem to hurt, except for -flink-rts (see below).

You are also passing --extra-include-dirs=/usr/local/include and --extra-lib-dirs=/usr/local/lib; I am not.

True, I do... But the referred libraries seem to all be either in the Haskell/GHC land, or in the MacOS/Xcode land... They would be needed for real projects, but not for this toy one... I think the following proves that these are harmless.

And now, the solution, or at least pinpointing of the culprit. It is -flink-rts flag in ghc-options:.

Current ~/.stack/config.yaml:

ghc-options:
  # All packages
  "$locals": -Wall #-dynamic
  "$targets": -Wall #-dynamic
  #"$targets": -Werror -dynamic
  "$everything": -Wall -O2 -fPIC -haddock # -flink-rts
#apply-ghc-options: locals
#apply-ghc-options: targets
apply-ghc-options: everything

Current stack.yaml in the project dir:

resolver: lts-22.0
configure-options:
  $targets:
  - --enable-executable-dynamic

And the result:

$ stack clean
$ stack run
h-tst5-0.1.0.0: unregistering (local file changes: CHANGELOG.md README.md app/Main.hs h-tst5.cabal
package.yaml src/Lib.hs)
Building all executables for h-tst5 once. After a successful build of all of them, only specified executables
will be rebuilt.
h-tst5> configure (lib + exe)
Configuring h-tst5-0.1.0.0...
h-tst5> build (lib + exe)
Preprocessing library for h-tst5-0.1.0.0..
Building library for h-tst5-0.1.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Lib.o, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Lib.dyn_o )
[2 of 2] Compiling Paths_h_tst5     ( .stack-work/dist/x86_64-osx/ghc-9.6.3/build/autogen/Paths_h_tst5.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Paths_h_tst5.o, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/Paths_h_tst5.dyn_o )
ld: warning: -single_module is obsolete
Preprocessing executable 'h-tst5-exe' for h-tst5-0.1.0.0..
Building executable 'h-tst5-exe' for h-tst5-0.1.0.0..
[1 of 2] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe-tmp/Main.dyn_o )
[2 of 2] Compiling Paths_h_tst5     ( .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/autogen/Paths_h_tst5.hs, .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe-tmp/Paths_h_tst5.dyn_o )
[3 of 3] Linking .stack-work/dist/x86_64-osx/ghc-9.6.3/build/h-tst5-exe/h-tst5-exe
ld: warning: ignoring duplicate libraries: '-lm'
h-tst5> copy/register
Installing library in /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/b6a60e8de6a9e7872955e8d2d896ee75310c6f4ed57b155c817d7025910ff715/9.6.3/lib/x86_64-osx-ghc-9.6.3/h-tst5-0.1.0.0-GPS02nDvvbOBzEVV7Y3LLs
Installing executable h-tst5-exe in /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/b6a60e8de6a9e7872955e8d2d896ee75310c6f4ed57b155c817d7025910ff715/9.6.3/bin
Registering library for h-tst5-0.1.0.0..
someFunc
$ ll /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/b6a60e8de6a9e7872955e8d2d896ee75310c6f4ed57b155c817d7025910ff715/9.6.3/bin/h-tst5-exe 
-rwxr-xr-x  1 ur20980  staff  86624 Dec 22 16:41 /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/b6a60e8de6a9e7872955e8d2d896ee75310c6f4ed57b155c817d7025910ff715/9.6.3/bin/h-tst5-exe*
$ otool -L !!:$
otool -L /Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/b6a60e8de6a9e7872955e8d2d896ee75310c6f4ed57b155c817d7025910ff715/9.6.3/bin/h-tst5-exe
/Users/ur20980/src/h-tst5/.stack-work/install/x86_64-osx/b6a60e8de6a9e7872955e8d2d896ee75310c6f4ed57b155c817d7025910ff715/9.6.3/bin/h-tst5-exe:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
	@rpath/libHSh-tst5-0.1.0.0-GPS02nDvvbOBzEVV7Y3LLs-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSbase-4.18.1.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-bignum-1.3-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSghc-prim-0.10.0-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libHSrts-1.0.2_thr-ghc9.6.3.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/usr/lib/libffi.dylib (compatibility version 1.0.0, current version 32.0.0)
$ 

One definite problem discovered is: with ghc-options: -dynamic Stack still generates .o files instead of .dyn_o (as, e.g., Cabal does).

Another possible problem that I see - something's wrong with -flink-rts. You can see that @rpath/libHSrts-1.0.2_thr-ghc9.6.3.dylib gets linked regardless, but I wonder what that flag does that leads to SEGV.

Oh, and of course, the above works with and applies to GHC-9.8.1 too.

@mpilgrem
Copy link
Member

mpilgrem commented Dec 23, 2023

@mouse07410, you can specify non-project specific options in either a project's YAML configuration file (stack.yaml) or Stack's global YAML configuration file (config.yaml). So, the following should work in config.yaml too:

configure-options:
  $targets:
  - --enable-executable-dynamic

For completeness, I'll record here some of the flags/options that are being passed to GHC with --enable-executable-dynamic:

  • For the make of Lib: -static, -dynamic-too, -dynosuf dyn_o, -dynhisuf dyn_hi
  • For the (implied) make of Lib.dyn_o: -shared, -dynamic, -dynload deploy, -no-auto-link-packages
  • For the -no-link make of app/Main.hs: -dynamic, -fPIC, -osuf dyn_o, -hisuf dyn_hi
  • For the final make of app/Main.hs: as above, but also with -dynload deploy

I do not know if a user can replicate that complexity by bypassing Cabal (the library) and just passing flags directly from Stack to GHC (via Cabal (the library)).

Also for completeness, I'll post here my analysis of what cabal run is doing, when that analysis is complete. EDIT: cabal run is, essentially, doing what my configured version of stack build was doing: it passes --enable-shared and --enable-executable-dynamic to cabal configure. The --enabled-shared is redundant because it is anyway implied by --enable-executable-dynamic: https://cabal.readthedocs.io/en/stable/setup-commands.html#cmdoption-runhaskell-Setup.hs-configure-enable-executable-dynamic. Given essentially the same configuration, the calls to GHC are essentially the same.

@mpilgrem
Copy link
Member

I've added something to Stack's online documentation.

@mouse07410
Copy link
Author

Funny. I'm pretty sure I tried what you suggested earlier and it didn't work - but I tried it again now, and it worked perfectly. Perhaps I forgot to do stack clean between the tries...

Summary:

configure-options:
  $targets:
  - --enable-executable-dynamic

in ~/.stack/config.yaml works.

ghc-options:
  # All packages
  "$locals": -Wall #-dynamic
  "$targets": -Wall #-dynamic
  #"$targets": -Werror #-dynamic
  "$everything": -Wall -O2 -fPIC -haddock # -dynamic # -flink-rts
apply-ghc-options: everything

do not interfere with the above configure-options:. Probably only the -Wall -O2 flags are relevant, and -haddock for some.

Tested on MacOS Sonoma x86_64 and aarch64, with GHC-9.6.3 and 9.8.1.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants