1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!-- FFI docs as a chapter -->
6 Foreign function interface (FFI)
9 <para>GHC (mostly) conforms to the Haskell Foreign Function Interface,
10 whose definition is part of the Haskell Report on <ulink url="http://www.haskell.org/"><literal>http://www.haskell.org/</literal></ulink>.</para>
12 <para>FFI support is enabled by default, but can be enabled or disabled explicitly with the <option>-XForeignFunctionInterface</option><indexterm><primary><option>-XForeignFunctionInterface</option></primary>
13 </indexterm> flag.</para>
15 <para>GHC implements a number of GHC-specific extensions to the FFI
16 Addendum. These extensions are described in <xref linkend="ffi-ghcexts" />, but please note that programs using
17 these features are not portable. Hence, these features should be
18 avoided where possible.</para>
20 <para>The FFI libraries are documented in the accompanying library
21 documentation; see for example the
22 <ulink url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Foreign</literal></ulink> module.</para>
24 <sect1 id="ffi-ghcexts">
25 <title>GHC extensions to the FFI Addendum</title>
27 <para>The FFI features that are described in this section are specific to
28 GHC. Your code will not be portable to other compilers if you use them.</para>
31 <title>Unboxed types</title>
33 <para>The following unboxed types may be used as basic foreign types
34 (see FFI Addendum, Section 3.2): <literal>Int#</literal>,
35 <literal>Word#</literal>, <literal>Char#</literal>,
36 <literal>Float#</literal>, <literal>Double#</literal>,
37 <literal>Addr#</literal>, <literal>StablePtr# a</literal>,
38 <literal>MutableByteArray#</literal>, <literal>ForeignObj#</literal>,
39 and <literal>ByteArray#</literal>.</para>
42 <sect2 id="ffi-newtype-io">
43 <title>Newtype wrapping of the IO monad</title>
44 <para>The FFI spec requires the IO monad to appear in various places,
45 but it can sometimes be convenient to wrap the IO monad in a
46 <literal>newtype</literal>, thus:
48 newtype MyIO a = MIO (IO a)
50 (A reason for doing so might be to prevent the programmer from
51 calling arbitrary IO procedures in some part of the program.)
53 <para>The Haskell FFI already specifies that arguments and results of
54 foreign imports and exports will be automatically unwrapped if they are
55 newtypes (Section 3.2 of the FFI addendum). GHC extends the FFI by automatically unwrapping any newtypes that
56 wrap the IO monad itself.
57 More precisely, wherever the FFI specification requires an IO type, GHC will
58 accept any newtype-wrapping of an IO type. For example, these declarations are
61 foreign import foo :: Int -> MyIO Int
62 foreign import "dynamic" baz :: (Int -> MyIO Int) -> CInt -> MyIO Int
68 <title>Primitive imports</title>
70 GHC extends the FFI with an additional calling convention
71 <literal>prim</literal>, e.g.:
73 foreign import prim "foo" foo :: ByteArray# -> (# Int#, Int# #)
75 This is used to import functions written in Cmm code that follow an
76 internal GHC calling convention. This feature is not intended for
77 use outside of the core libraries that come with GHC. For more
78 details see the GHC developer wiki.
82 <sect2 id="ffi-interruptible">
83 <title>Interruptible foreign calls</title>
85 This concerns the interaction of foreign calls
86 with <literal>Control.Concurrent.throwTo</literal>.
87 Normally when the target of a <literal>throwTo</literal> is
88 involved in a foreign call, the exception is not raised
89 until the call returns, and in the meantime the caller is
90 blocked. This can result in unresponsiveness, which is
91 particularly undesirable in the case of user interrupt
92 (e.g. Control-C). The default behaviour when a Control-C
93 signal is received (<literal>SIGINT</literal> on Unix) is to raise
94 the <literal>UserInterrupt</literal> exception in the main
95 thread; if the main thread is blocked in a foreign call at
96 the time, then the program will not respond to the user
101 The problem is that it is not possible in general to
102 interrupt a foreign call safely. However, GHC does provide
103 a way to interrupt blocking system calls which works for
104 most system calls on both Unix and Windows. A foreign call
105 can be annotated with <literal>interruptible</literal> instead
106 of <literal>safe</literal> or <literal>unsafe</literal>:
109 foreign import ccall interruptible
110 "sleep" :: CUint -> IO CUint
113 <literal>interruptble</literal> behaves exactly as
114 <literal>safe</literal>, except that when
115 a <literal>throwTo</literal> is directed at a thread in an
116 interruptible foreign call, an OS-specific mechanism will be
117 used to attempt to cause the foreign call to return:
121 <term>Unix systems</term>
124 The thread making the foreign call is sent
125 a <literal>SIGPIPE</literal> signal
126 using <literal>pthread_kill()</literal>. This is
127 usually enough to cause a blocking system call to
128 return with <literal>EINTR</literal> (GHC by default
129 installs an empty signal handler
130 for <literal>SIGPIPE</literal>, to override the
131 default behaviour which is to terminate the process
137 <term>Windows systems</term>
140 [Vista and later only] The RTS calls the Win32
141 function <literal>CancelSynchronousIO</literal>,
142 which will cause a blocking I/O operation to return
144 error <literal>ERROR_OPERATION_ABORTED</literal>.
150 If the system call is successfully interrupted, it will
151 return to Haskell whereupon the exception can be raised. Be
152 especially careful when
153 using <literal>interruptible</literal> that the caller of
154 the foreign function is prepared to deal with the
155 consequences of the call being interrupted; on Unix it is
156 good practice to check for <literal>EINTR</literal> always,
157 but on Windows it is not typically necessary to
158 handle <literal>ERROR_OPERATION_ABORTED</literal>.
164 <title>Using the FFI with GHC</title>
166 <para>The following sections also give some hints and tips on the
167 use of the foreign function interface in GHC.</para>
169 <sect2 id="foreign-export-ghc">
170 <title>Using <literal>foreign export</literal> and <literal>foreign import ccall "wrapper"</literal> with GHC</title>
172 <indexterm><primary><literal>foreign export
173 </literal></primary><secondary>with GHC</secondary>
176 <para>When GHC compiles a module (say <filename>M.hs</filename>)
177 which uses <literal>foreign export</literal> or
178 <literal>foreign import "wrapper"</literal>, it generates two
179 additional files, <filename>M_stub.c</filename> and
180 <filename>M_stub.h</filename>. GHC will automatically compile
181 <filename>M_stub.c</filename> to generate
182 <filename>M_stub.o</filename> at the same time.</para>
184 <para>For a plain <literal>foreign export</literal>, the file
185 <filename>M_stub.h</filename> contains a C prototype for the
186 foreign exported function, and <filename>M_stub.c</filename>
187 contains its definition. For example, if we compile the
188 following module:</para>
193 foreign export ccall foo :: Int -> IO Int
196 foo n = return (length (f n))
200 f n = n:(f (n-1))</programlisting>
202 <para>Then <filename>Foo_stub.h</filename> will contain
203 something like this:</para>
207 extern HsInt foo(HsInt a0);</programlisting>
209 <para>and <filename>Foo_stub.c</filename> contains the
210 compiler-generated definition of <literal>foo()</literal>. To
211 invoke <literal>foo()</literal> from C, just <literal>#include
212 "Foo_stub.h"</literal> and call <literal>foo()</literal>.</para>
214 <para>The <filename>foo_stub.c</filename> and
215 <filename>foo_stub.h</filename> files can be redirected using the
216 <option>-stubdir</option> option; see <xref linkend="options-output"
219 <para>When linking the program, remember to include
220 <filename>M_stub.o</filename> in the final link command line, or
221 you'll get link errors for the missing function(s) (this isn't
222 necessary when building your program with <literal>ghc
223 ––make</literal>, as GHC will automatically link in the
224 correct bits).</para>
226 <sect3 id="using-own-main">
227 <title>Using your own <literal>main()</literal></title>
229 <para>Normally, GHC's runtime system provides a
230 <literal>main()</literal>, which arranges to invoke
231 <literal>Main.main</literal> in the Haskell program. However,
232 you might want to link some Haskell code into a program which
233 has a main function written in another language, say C. In
234 order to do this, you have to initialize the Haskell runtime
235 system explicitly.</para>
237 <para>Let's take the example from above, and invoke it from a
238 standalone C program. Here's the C code:</para>
241 #include <stdio.h>
244 #ifdef __GLASGOW_HASKELL__
245 #include "foo_stub.h"
248 #ifdef __GLASGOW_HASKELL__
249 extern void __stginit_Foo ( void );
252 int main(int argc, char *argv[])
256 hs_init(&argc, &argv);
257 #ifdef __GLASGOW_HASKELL__
258 hs_add_root(__stginit_Foo);
261 for (i = 0; i < 5; i++) {
262 printf("%d\n", foo(2500));
269 <para>We've surrounded the GHC-specific bits with
270 <literal>#ifdef __GLASGOW_HASKELL__</literal>; the rest of the
271 code should be portable across Haskell implementations that
272 support the FFI standard.</para>
274 <para>The call to <literal>hs_init()</literal>
275 initializes GHC's runtime system. Do NOT try to invoke any
276 Haskell functions before calling
277 <literal>hs_init()</literal>: bad things will
278 undoubtedly happen.</para>
280 <para>We pass references to <literal>argc</literal> and
281 <literal>argv</literal> to <literal>hs_init()</literal>
282 so that it can separate out any arguments for the RTS
283 (i.e. those arguments between
284 <literal>+RTS...-RTS</literal>).</para>
287 <function>hs_add_root</function><indexterm><primary><function>hs_add_root</function></primary>
288 </indexterm>, a GHC-specific interface which is required to
289 initialise the Haskell modules in the program. The argument
290 to <function>hs_add_root</function> should be the name of the
291 initialization function for the "root" module in your program
292 - in other words, the module which directly or indirectly
293 imports all the other Haskell modules in the program. In a
294 standalone Haskell program the root module is normally
295 <literal>Main</literal>, but when you are using Haskell code
296 from a library it may not be. If your program has multiple
297 root modules, then you can call
298 <function>hs_add_root</function> multiple times, one for each
299 root. The name of the initialization function for module
300 <replaceable>M</replaceable> is
301 <literal>__stginit_<replaceable>M</replaceable></literal>, and
302 it may be declared as an external function symbol as in the
303 code above. Note that the symbol name should be transformed
304 according to the Z-encoding:</para>
307 <tgroup cols="2" align="left" colsep="1" rowsep="1">
310 <entry>Character</entry>
311 <entry>Replacement</entry>
316 <entry><literal>.</literal></entry>
317 <entry><literal>zd</literal></entry>
320 <entry><literal>_</literal></entry>
321 <entry><literal>zu</literal></entry>
324 <entry><literal>`</literal></entry>
325 <entry><literal>zq</literal></entry>
328 <entry><literal>Z</literal></entry>
329 <entry><literal>ZZ</literal></entry>
332 <entry><literal>z</literal></entry>
333 <entry><literal>zz</literal></entry>
339 <para>After we've finished invoking our Haskell functions, we
340 can call <literal>hs_exit()</literal>, which terminates the
343 <para>There can be multiple calls to
344 <literal>hs_init()</literal>, but each one should be matched
345 by one (and only one) call to
346 <literal>hs_exit()</literal><footnote><para>The outermost
347 <literal>hs_exit()</literal> will actually de-initialise the
348 system. NOTE that currently GHC's runtime cannot reliably
349 re-initialise after this has happened,
350 see <xref linkend="ffi-divergence" />.</para>
353 <para>NOTE: when linking the final program, it is normally
354 easiest to do the link using GHC, although this isn't
355 essential. If you do use GHC, then don't forget the flag
356 <option>-no-hs-main</option><indexterm><primary><option>-no-hs-main</option></primary>
357 </indexterm>, otherwise GHC will try to link
358 to the <literal>Main</literal> Haskell module.</para>
361 <sect3 id="ffi-library">
362 <title>Making a Haskell library that can be called from foreign
365 <para>The scenario here is much like in <xref linkend="using-own-main"
366 />, except that the aim is not to link a complete program, but to
367 make a library from Haskell code that can be deployed in the same
368 way that you would deploy a library of C code.</para>
370 <para>The main requirement here is that the runtime needs to be
371 initialized before any Haskell code can be called, so your library
372 should provide initialisation and deinitialisation entry points,
373 implemented in C or C++. For example:</para>
376 HsBool mylib_init(void){
380 // Initialize Haskell runtime
381 hs_init(&argc, &argv);
383 // Tell Haskell about all root modules
384 hs_add_root(__stginit_Foo);
386 // do any other initialization here and
387 // return false if there was a problem
391 void mylib_end(void){
396 <para>The initialisation routine, <literal>mylib_init</literal>, calls
397 <literal>hs_init()</literal> and <literal>hs_add_root()</literal> as
398 normal to initialise the Haskell runtime, and the corresponding
399 deinitialisation function <literal>mylib_end()</literal> calls
400 <literal>hs_exit()</literal> to shut down the runtime.</para>
405 <sect2 id="glasgow-foreign-headers">
406 <title>Using header files</title>
408 <indexterm><primary>C calls, function headers</primary></indexterm>
410 <para>C functions are normally declared using prototypes in a C
411 header file. Earlier versions of GHC (6.8.3 and
412 earlier) <literal>#include</literal>d the header file in
413 the C source file generated from the Haskell code, and the C
414 compiler could therefore check that the C function being
415 called via the FFI was being called at the right type.</para>
417 <para>GHC no longer includes external header files when
418 compiling via C, so this checking is not performed. The
419 change was made for compatibility with the native code backend
420 (<literal>-fasm</literal>) and to comply strictly with the FFI
421 specification, which requires that FFI calls are not subject
422 to macro expansion and other CPP conversions that may be
423 applied when using C header files. This approach also
424 simplifies the inlining of foreign calls across module and
425 package boundaries: there's no need for the header file to be
426 available when compiling an inlined version of a foreign call,
427 so the compiler is free to inline foreign calls in any
430 <para>The <literal>-#include</literal> option is now
431 deprecated, and the <literal>include-files</literal> field
432 in a Cabal package specification is ignored.</para>
437 <title>Memory Allocation</title>
439 <para>The FFI libraries provide several ways to allocate memory
440 for use with the FFI, and it isn't always clear which way is the
441 best. This decision may be affected by how efficient a
442 particular kind of allocation is on a given compiler/platform,
443 so this section aims to shed some light on how the different
444 kinds of allocation perform with GHC.</para>
448 <term><literal>alloca</literal> and friends</term>
450 <para>Useful for short-term allocation when the allocation
451 is intended to scope over a given <literal>IO</literal>
452 computation. This kind of allocation is commonly used
453 when marshalling data to and from FFI functions.</para>
455 <para>In GHC, <literal>alloca</literal> is implemented
456 using <literal>MutableByteArray#</literal>, so allocation
457 and deallocation are fast: much faster than C's
458 <literal>malloc/free</literal>, but not quite as fast as
459 stack allocation in C. Use <literal>alloca</literal>
460 whenever you can.</para>
465 <term><literal>mallocForeignPtr</literal></term>
467 <para>Useful for longer-term allocation which requires
468 garbage collection. If you intend to store the pointer to
469 the memory in a foreign data structure, then
470 <literal>mallocForeignPtr</literal> is
471 <emphasis>not</emphasis> a good choice, however.</para>
473 <para>In GHC, <literal>mallocForeignPtr</literal> is also
474 implemented using <literal>MutableByteArray#</literal>.
475 Although the memory is pointed to by a
476 <literal>ForeignPtr</literal>, there are no actual
477 finalizers involved (unless you add one with
478 <literal>addForeignPtrFinalizer</literal>), and the
479 deallocation is done using GC, so
480 <literal>mallocForeignPtr</literal> is normally very
486 <term><literal>malloc/free</literal></term>
488 <para>If all else fails, then you need to resort to
489 <literal>Foreign.malloc</literal> and
490 <literal>Foreign.free</literal>. These are just wrappers
491 around the C functions of the same name, and their
492 efficiency will depend ultimately on the implementations
493 of these functions in your platform's C library. We
494 usually find <literal>malloc</literal> and
495 <literal>free</literal> to be significantly slower than
496 the other forms of allocation above.</para>
501 <term><literal>Foreign.Marshal.Pool</literal></term>
503 <para>Pools are currently implemented using
504 <literal>malloc/free</literal>, so while they might be a
505 more convenient way to structure your memory allocation
506 than using one of the other forms of allocation, they
507 won't be any more efficient. We do plan to provide an
508 improved-performance implementation of Pools in the
509 future, however.</para>
515 <sect2 id="ffi-threads">
516 <title>Multi-threading and the FFI</title>
518 <para>In order to use the FFI in a multi-threaded setting, you must
519 use the <option>-threaded</option> option
520 (see <xref linkend="options-linker" />).</para>
523 <title>Foreign imports and multi-threading</title>
525 <para>When you call a <literal>foreign import</literal>ed
526 function that is annotated as <literal>safe</literal> (the
527 default), and the program was linked
528 using <option>-threaded</option>, then the call will run
529 concurrently with other running Haskell threads. If the
530 program was linked without <option>-threaded</option>,
531 then the other Haskell threads will be blocked until the
534 <para>This means that if you need to make a foreign call to
535 a function that takes a long time or blocks indefinitely,
536 then you should mark it <literal>safe</literal> and
537 use <option>-threaded</option>. Some library functions
538 make such calls internally; their documentation should
539 indicate when this is the case.</para>
541 <para>If you are making foreign calls from multiple Haskell
542 threads and using <option>-threaded</option>, make sure that
543 the foreign code you are calling is thread-safe. In
544 particularly, some GUI libraries are not thread-safe and
545 require that the caller only invokes GUI methods from a
546 single thread. If this is the case, you may need to
547 restrict your GUI operations to a single Haskell thread,
548 and possibly also use a bound thread (see
549 <xref linkend="haskell-threads-and-os-threads" />).</para>
551 <para>Note that foreign calls made by different Haskell
552 threads may execute in <emphasis>parallel</emphasis>, even
553 when the <literal>+RTS -N</literal> flag is not being used
554 (<xref linkend="parallel-options" />). The <literal>+RTS
555 -N</literal> flag controls parallel execution of Haskell
556 threads, but there may be an arbitrary number of foreign
557 calls in progress at any one time, regardless of
558 the <literal>+RTS -N</literal> value.</para>
560 <para>If a call is annotated as <literal>interruptible</literal>
561 and the program was multithreaded, the call may be
562 interrupted in the event that the Haskell thread receives an
563 exception. The mechanism by which the interrupt occurs
564 is platform dependent, but is intended to cause blocking
565 system calls to return immediately with an interrupted error
566 code. The underlying operating system thread is not to be
567 destroyed. See <xref linkend="ffi-interruptible"/> for more details.</para>
570 <sect3 id="haskell-threads-and-os-threads">
571 <title>The relationship between Haskell threads and OS
574 <para>Normally there is no fixed relationship between Haskell
575 threads and OS threads. This means that when you make a
576 foreign call, that call may take place in an unspecified OS
577 thread. Furthermore, there is no guarantee that multiple
578 calls made by one Haskell thread will be made by the same OS
581 <para>This usually isn't a problem, and it allows the GHC
582 runtime system to make efficient use of OS thread resources.
583 However, there are cases where it is useful to have more
584 control over which OS thread is used, for example when
585 calling foreign code that makes use of thread-local state.
586 For cases like this, we provide <emphasis>bound
587 threads</emphasis>, which are Haskell threads tied to a
588 particular OS thread. For information on bound threads, see
590 for the <ulink url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>
595 <title>Foreign exports and multi-threading</title>
597 <para>When the program is linked
598 with <option>-threaded</option>, then you may
599 invoke <literal>foreign export</literal>ed functions from
600 multiple OS threads concurrently. The runtime system must
601 be initialised as usual by
602 calling <literal>hs_init()</literal>
603 and <literal>hs_add_root</literal>, and these calls must
604 complete before invoking any <literal>foreign
605 export</literal>ed functions.</para>
609 <title>On the use of <literal>hs_exit()</literal></title>
611 <para><literal>hs_exit()</literal> normally causes the termination of
612 any running Haskell threads in the system, and when
613 <literal>hs_exit()</literal> returns, there will be no more Haskell
614 threads running. The runtime will then shut down the system in an
615 orderly way, generating profiling
616 output and statistics if necessary, and freeing all the memory it
619 <para>It isn't always possible to terminate a Haskell thread forcibly:
620 for example, the thread might be currently executing a foreign call,
621 and we have no way to force the foreign call to complete. What's
622 more, the runtime must
623 assume that in the worst case the Haskell code and runtime are about
624 to be removed from memory (e.g. if this is a <link linkend="win32-dlls">Windows DLL</link>,
625 <literal>hs_exit()</literal> is normally called before unloading the
626 DLL). So <literal>hs_exit()</literal> <emphasis>must</emphasis> wait
627 until all outstanding foreign calls return before it can return
630 <para>The upshot of this is that if you have Haskell threads that are
631 blocked in foreign calls, then <literal>hs_exit()</literal> may hang
632 (or possibly busy-wait) until the calls return. Therefore it's a
633 good idea to make sure you don't have any such threads in the system
634 when calling <literal>hs_exit()</literal>. This includes any threads
635 doing I/O, because I/O may (or may not, depending on the
636 type of I/O and the platform) be implemented using blocking foreign
639 <para>The GHC runtime treats program exit as a special case, to avoid
640 the need to wait for blocked threads when a standalone
641 executable exits. Since the program and all its threads are about to
642 terminate at the same time that the code is removed from memory, it
643 isn't necessary to ensure that the threads have exited first.
644 (Unofficially, if you want to use this fast and loose version of
645 <literal>hs_exit()</literal>, then call
646 <literal>shutdownHaskellAndExit()</literal> instead).</para>
650 <sect2 id="ffi-floating-point">
651 <title>Floating point and the FFI</title>
654 On POSIX systems, the <literal>fenv.h</literal> header
655 provides operations for inspecting and modifying the state of
656 the floating point unit. In particular, the rounding mode
657 used by floating point operations can be changed, and the
658 exception flags can be tested.
662 In Haskell, floating-point operations have pure types, and the
663 evaluation order is unspecified. So strictly speaking, since
664 the <literal>fenv.h</literal> functions let you change the
665 results of, or observe the effects of floating point
666 operations, use of <literal>fenv.h</literal> renders the
667 behaviour of floating-point operations anywhere in the program
672 Having said that, we <emphasis>can</emphasis> document exactly
673 what GHC does with respect to the floating point state, so
674 that if you really need to use <literal>fenv.h</literal> then
675 you can do so with full knowledge of the pitfalls:
679 GHC completely ignores the floating-point
680 environment, the runtime neither modifies nor reads it.
685 The floating-point environment is not saved over a
686 normal thread context-switch. So if you modify the
687 floating-point state in one thread, those changes may be
688 visible in other threads. Furthermore, testing the
689 exception state is not reliable, because a context
690 switch may change it. If you need to modify or test the
691 floating point state and use threads, then you must use
693 (<literal>Control.Concurrent.forkOS</literal>), because
694 a bound thread has its own OS thread, and OS threads do
695 save and restore the floating-point state.
700 It is safe to modify the floating-point unit state
701 temporarily during a foreign call, because foreign calls
702 are never pre-empted by GHC.
712 ;;; Local Variables: ***
713 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***