[Haskell-cafe] ccall vs capi

Oleg Grenrus oleg.grenrus at iki.fi
Mon Jun 14 07:44:10 UTC 2021


https://downloads.haskell.org/ghc/9.0.1/docs/html/users_guide/exts/ffi.html?highlight=capiffi#extension-CApiFFI

I see very little reason to not use CApiFFI if your project doesn't need
to work on old GHCs.

It also checks that foreign imports have compatible types - very nice
feature.

- Oleg

On 14.6.2021 0.47, Brandon Allbery wrote:
> capi uses the C compiler to make the call, requiring ghc to write out
> and compile a C stub file for each import. This guarantees C
> compatibility and also works with cpp-defined symbols.
>
> ccall attempts to emulate what the C compiler does with generated
> assembly language, but may get it wrong on Darwin; I recall Moritz
> Angermann saying someone would have to dump out the generated assembly
> from both cases to see what's different and what needs to change for
> ccall to work on Darwin.
>
> Possible differences include not zeroing the upper parts of registers
> with 32-bit values in them and not treating other registers properly
> (for example, getting caller-saved registers wrong). It's also
> conceivable that there's a cpp macro involved normally and there's a
> hidden bug in a fallback libSystem version of a function. Someone will
> have to examine the generated assembly as above to see what's actually
> wrong.
>
> On Sun, Jun 13, 2021 at 5:21 PM Julian Ospald <hasufell at hasufell.de
> <mailto:hasufell at hasufell.de>> wrote:
>
>     Hi,
>
>     recently during a ghcup release, we stumbled over yet another `ccall`
>     bug on Darwin: https://github.com/biegunka/terminal-size/pull/16
>
>     This took Ben some time to debug (thanks again) and it isn't always
>     clear with such
>     bugs whether something is wrong with GHC/RTS/GC or a library, so this
>     can be a time-sink.
>
>     It's also not the first time it happened:
>     * https://github.com/haskell/unix/pull/110#issuecomment-613384845
>     *
>     https://github.com/hasufell/hpath/commit/74d686547e67da3eeae327c84c63f83833205890
>
>     I can't say that I fully understood what the exact difference here is
>     and why it seems to matter much more on Darwin than on other
>     systems. So
>     I think it would be helpful if we could come up with:
>
>     1. a clear description of the differences of ccall vs capi
>     2. an explanation why ccall can lead to terrible bugs on darwin
>     3. a guideline of when using one over the other is appropriate
>
>     Not everyone deals with ffi on a regular basis, so I hope the experts
>     here can shed some light!
>
>     Cheers,
>     Julian
>     _______________________________________________
>     Haskell-Cafe mailing list
>     To (un)subscribe, modify options or view archives go to:
>     http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>     Only members subscribed via the mailman list are allowed to post.
>
>
>
> -- 
> brandon s allbery kf8nh
> allbery.b at gmail.com <mailto:allbery.b at gmail.com>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.


More information about the Haskell-Cafe mailing list