Opened 15 months ago

Last modified 15 months ago

#9278 new bug

GHCi crash: selector _ for message _ does not match selector known to Objective C runtime

Reported by: mietek Owned by:
Priority: normal Milestone:
Component: GHCi Version: 7.6.3
Keywords: crash, dynamic linking Cc: hvr, chak@…
Operating System: MacOS X Architecture: x86_64 (amd64)
Type of failure: GHCi crash Test Case:
Blocked By: Blocking:
Related Tickets: #9277 Differential Rev(s):


Given an Objective-C object file referencing a symbol available in a system framework, interactive GHCi 7.6.3 crashes in an odd fashion.

Start with an Objective-C source file, defining a function to be used via the FFI:

$ cat >foo.m <<EOF
#import <Foundation/Foundation.h>

BOOL is_main_thread()
  return [NSThread isMainThread];
$ cat >Main.hs <<EOF
module Main where

foreign import ccall "is_main_thread" isMainThread :: IO Bool

main :: IO ()
main = do
  mt <- isMainThread
  print mt

Non-interactive GHC 7.6.3 works as expected:

$ clang -c -o foo.o foo.m
$ ghc -framework Foundation -o foo foo.o Main.hs
[1 of 1] Compiling Main             ( Main.hs, Main.o )
Linking foo ...
$ ./foo

Interactive GHCi 7.6.3 crashes:

$ ghci -framework Foundation foo.o Main.hs
GHCi, version 7.6.3:  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading object (static) foo.o ... done
Loading object (framework) Foundation ... done
final link ... done
Ok, modules loaded: Main.
> main
2014-07-07 12:28:21.811 ghc[37395:1103] *** NSForwarding: warning: selector (0x10ddef328) for message 'isMainThread' does not match selector known to Objective C runtime (0x7fff8ea15881)-- abort
2014-07-07 12:28:21.813 ghc[37395:1103] +[NSThread isMainThread]: unrecognized selector sent to class 0x7fff75535280
2014-07-07 12:28:21.814 ghc[37395:1103] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSThread isMainThread]: unrecognized selector sent to class 0x7fff75535280'
*** First throw call stack:
	0   CoreFoundation                      0x00007fff8bb4825c __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x00007fff85029e75 objc_exception_throw + 43
	2   CoreFoundation                      0x00007fff8bb4b02d +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
	3   CoreFoundation                      0x00007fff8baa6322 ___forwarding___ + 1010
	4   CoreFoundation                      0x00007fff8baa5ea8 _CF_forwarding_prep_0 + 120
	5   ???                                 0x000000010ddef31a 0x0 + 4527682330
	6   ???                                 0x000000010ddf02f5 0x0 + 4527686389
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

Change History (3)

comment:1 Changed 15 months ago by mietek

  • Summary changed from GHCi crash: to GHCi crash: selector _ for message _ does not match selector known to Objective C runtime

comment:2 Changed 15 months ago by mietek

There is a similar issue in GHC 7.8.2: #9277.

Last edited 15 months ago by mietek (previous) (diff)

comment:3 Changed 15 months ago by chak

  • Cc chak@… added

AFAIK, the Objective-C runtime executes some initialisation code to set up Objective-C classes. The system linker produces executables that ensure that this initialisation is performed before any application code is being run. GHC's runtime doesn't know anything about that, though, and will just load the Objective-C binary code without performing the required runtime initialisation.

Based on that, I believe, the error message you are seeing is a consequence of the selectors for the classes not being registered in the data structures used by objc_msgSend().

Here is a description of the internals of the ObjC runtime:

Note: See TracTickets for help on using tickets.