WIPIVERSE

libffi

libffi (Foreign Function Interface) is a portable library that provides a C programming language interface for calling natively compiled functions at run time, given information about the target function's signature, rather than at compile time. It also supports the reverse functionality: producing a pointer to a function that can accept and decode any combination of arguments defined at run time.

Overview

libffi abstracts over the differences in calling conventions across various processor–operating system combinations. Its C API is shared across all platform builds, allowing developers to dynamically load and invoke subroutines without knowing the target platform's calling convention in advance. In addition to regular functions, libffi also supports C-style variadic functions.

History

libffi was originally developed by Anthony Green and first released on October 7, 1996. It was inspired by the Gencall library from Silicon Graphics (SGI), which was created by Gianni Mariani for the purpose of calling functions by address and constructing call frames for the MIPS O32 calling convention. Green refined the concept, extended it to additional architectures and calling conventions, and released libffi as open-source software.

Technical Details

The library provides the lowest, machine-dependent layer of a fully featured foreign function interface. A higher-level layer must exist above libffi to handle type conversions for values passed between different languages. The core workflow involves:

  1. Creating an ffi_cif (Call InterFace) object that matches the signature of the target function.
  2. Using that object to invoke the function with provided arguments at run time.

Adoption

libffi is widely used as a bridging technology between compiled and interpreted language implementations, as well as for plug-in systems where function signatures are unknown at compile time. Notable users include:

  • Python – via the standard ctypes library in CPython
  • Haskell – via GHC (Glasgow Haskell Compiler) since late 2009
  • Java – OpenJDK (Zero-Assembly port), Dalvik (Android VM), Java Native Access (JNA), gcj
  • Ruby – via Fiddle (standard library) and Ruby-FFI
  • Common Lisp – via CFFI (C Foreign Function Interface)
  • Racket – for calling C code from Scheme
  • Mozilla – via js-ctypes for calling C functions from JavaScript
  • Node.js – via node-ffi addon
  • Perl – via FFI::Platypus and FFI::Raw
  • GNU Guile – since version 1.9.8
  • Mac OS X / iOS – via BridgeSupport and PyObjC, RubyCocoa, JSCocoa

Portability and License

libffi has been ported to a wide range of platforms, including Unix-like systems, Microsoft Windows, macOS, iOS, and bare-metal environments. It is written in C and assembly language and is released under the MIT License.

Current Status

The latest stable release is 3.7.1 (July 10, 2026). The project is hosted at sourceware.org/libffi/ with its source repository on GitHub.

Browse

More topics to explore

    Browse all articles