foreign export + unsafeCoerce# = non-termination
Summary
On aarch64 macOS I have a module which foreign exports a function which uses unsafeCoerce#
within.
Running this Haskell function from an external main (e.g. a C main) doesn't terminate, the function looks like it deadlocks and never returns. On x86_64 linux it does terminate and return.
Steps to reproduce
main.c:
#include <HsFFI.h>
#include "X_stub.h"
int main(void) {
hs_init(NULL, NULL);
bad();
hs_exit();
return 0;
}
X.hs:
{-# LANGUAGE MagicHash #-}
module X where
import GHC.Exts
n = 5
bad = return (Ptr (unsafeCoerce# n))
foreign export ccall bad :: IO (Ptr ())
Then
ghc -c X.hs
ghc -o main -no-hs-main main.c X.o
./main
Expected behavior
The program should terminate. I don't see why interpreting whatever as an Addr# could cause the program to deadlock. This behaviour on aarch64 macOS is also different from what @bgamari reported happened in Linux x86_64 where the program terminates.
Environment
- GHC version used: 9.8.2
Optional:
- Operating System: macOS
- System Architecture: M2/aarch64