7 ABI, FFI, and Compatibility

ABI AND FFIREVIEWED: 2026-10-21

Some other R implementations built a working interpreter and then struggled to run packages from CRAN, because native packages depend on R’s exported C API and its calling conventions, which cannot change without breaking those packages.

Thousands of CRAN packages contain compiled C, C++, or Fortran code: 5,569 of 25,474 had NeedsCompilation set to yes in tools::CRAN_package_db() on 2026-10-21. These packages depend on the R C API surface: the exported symbols, function signatures, calling conventions, public constants, the serialization format, and documented behavior. A compiled binary is frozen to that surface. If you change the calling convention of an exported function, rename a symbol, or alter a public constant, old binaries break. The layout of structures behind opaque pointers can change without any conforming package changing its source, although R then asks for packages to be rebuilt. The constraint lives in the surface, not in the implementation. Packages that reach past the API, as defining USE_RINTERNALS allowed before R 4.2.0, or that copy R’s internal headers, give that protection up and do break.

This constraint shapes the design of threaded-r and r-in-rust. Both change what sits behind the C API, and neither can change the API itself.

After this section

  • Define ABI and explain why binary compatibility is harder than language compatibility.
  • Describe what R’s C API exposes and why packages depend on it.
  • Identify which interpreter changes break native packages and which do not.
  • Explain the five architectural assumptions the C API makes and why they prevent free threading.
  • Say which parts of a runtime are frozen by shipped binaries and which are free to change.
  • Assess what an alternative R runtime must promise for real-world viability.

The pages

  1. What Is an ABI?. Binary interfaces, and why a shipped one is hard to change.

  2. How R Calls C, C++, and Fortran. Calling conventions, symbol registration, PROTECT/UNPROTECT, and object lifetimes across the boundary.

  3. The R C API and Thread Safety. The five architectural assumptions that make the C API incompatible with free threading.

  4. Dynamic Linking and Symbol Resolution. How compiled packages find R’s entry points and why symbol collisions are silent.

  5. Source, Binary, and Behavioral Compatibility. Three types of compatibility, how to break each, and what any alternative R must promise.

  6. External Libraries with Their Own Threads. BLAS, LAPACK, and OpenMP as part of the runtime. Thread count control and interaction with R’s threading model.

Before this section

Read R Runtime Internals first. You need to know what SEXPs are and how R executes code.

Where this leads

Multi-Interpreter Architecture shows how my work maintains SEXP compatibility across isolated interpreters. Every architectural choice in threaded-r and r-in-rust is constrained by CRAN compatibility.

The R Runtime Internals section covers what a SEXP is underneath, and why the representation stayed changeable.

Unsafe Rust at the R Boundary explains how r-in-rust wraps C code and manages lifetimes across the FFI. Source, Binary, and Behavioral Compatibility covers how systems evolve while maintaining backward compatibility.

Back to top