Skip to content

Translate function pointers and lambdas#3

Open
lucic71 wants to merge 35 commits intoCpp2Rust:masterfrom
lucic71:fn-ptr
Open

Translate function pointers and lambdas#3
lucic71 wants to merge 35 commits intoCpp2Rust:masterfrom
lucic71:fn-ptr

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented Apr 13, 2026

This PR implements the following:

  1. translate function pointers and non-capturing lambdas as Option<fn> in unsafe and FnPtr in refcount
  2. expose translation rules function declarations to generated code to allow fn_ptr == translated_rule

Minor:

  • decreased build time of unit tests by compiling rules/libcc2rs only once, not per each unit-test
  • default integers/floats to 0_type insted of <type>::default() to allow initialization of global vars (static_local.cpp compiles now in unsafe)
  • allow nullptr conversions in T* -> void* and do memcpy in ErasedPtr using reinterpret_cast

Translation of function pointers

In unsafe, function pointers are modeled as Option. Casting between function pointers in unsafe is performed using std::mem::transmute, however there is no safe way to achieve the same operation in refcount.

This is solved using the new FnPtr. To allow casting between different function types, use a type erased Rc inside the new FnPtr. Equality of function pointers is achieved through comparing the address of the fn objects (safe operation).

The C standard allows converting function pointers between incompatible function types. UB is triggered only when the incompatible pointer is called. For this reason the new FnState implements 2 new concepts:

  1. casting adaptors (to allow argument casting between ABI compatible types)
  2. provenance stack, or cast history (to allow round-trip function pointer casts)

For 1., consider the following cast:

    int fn_taking_int_ptr(int *p);
    int (*fn_taking_void_ptr)(void*) = (int (*)(void*))fn_taking_int_ptr;

Calling fn_taking_int_ptr with an int* argument works because both int* and void* have the same size. To support this in Rust we need to create an int* -> void* adapter when casting from fn_taking_int_ptr to fn_taking_void_ptr:

    fn_taking_int_ptr.cast::<fn(AnyPtr) -> i32>(Some(
      (|a0: AnyPtr| -> i32 {
          fn_taking_int_ptr(a0.cast::<i32>().unwrap())
      }) as fn(AnyPtr) -> i32
    ))

The job of the adapter is to convert from AnyPtr to Ptr. Ptr::cast_fn is a new function that takes as type argument the type of the target function pointer and an optional adaptor. If cast_fn receives None, then there is no valid adaptor from source to target, matching the UB semantics of calling a function through an incompatible function
pointer:

    int add(int a, int b) { return a + b; }
    void (*wrong)(void) = (void (*)(void))add;
    wrong()

For 2., the provenance stack contains all casts performed on the pointer in the past. Compared to PtrKind::Reinterpreted, PtrKind::Fn has no backing byte storage through OriginalAlloc, so each cast must know its history in order to allow round-trip casts, such as:

    int (*)(int, int) -> void (*)(void) -> int (*)(int, int)
                      (1)               (2)

For this specific case, where both (1) and (2) create non-compatible adaptors (because of non-compatible arguments), we cannot recover a call to the original function after (1) is performed. For this to work, save a stack of provenance, and when (2) is perfomed, cast_fn recovers the original function pointer. See test_roundtrip in fn_ptr_cast.cpp.

A current limitation of this approach is that it only allows function pointer casts where the source is a direct declaration of a function. Accessing a function pointer through a member field for example, would create a capturing adapter which does not coerce in a fn inside Ptr.

Exposure of translation rules

To translate patterns such as fn_ptr == translated_rule, for example in fn1 == fread in fn_ptr_stdlib_compare.cpp, expose the translatoin rule for fread declared in rules/stdio/tgt_recount.rs. The translated code becomes:

fn1 == fn_ptr!(rules::stdio_tgt_refcount::f5, fn(AnyPtr, u64, u64, Ptr<::std::fs::File>) -> u64)

The f5 name is not ideal, but in a future PR we can rename all translation rules so that they match the names of the C++ constructs that they translate, for example, the above code can translate to:

fn1 == fn_ptr!(rules::stdio_tgt_refcount::fread, fn(AnyPtr, u64, u64, Ptr<::std::fs::File>) -> u64)

fn_ptr! is a new macro declared in libcc2rs/src/fn_ptr.rs, which is a syntactic sugar for:

FnPtr::new($f as $ty, $f as *const () as usize)

FnPtr::new receives a callable (the first argument) which needs to be converted from function item ($f) to function pointer ($f as $ty), and a stable address used for function pointer comparison.

@lucic71 lucic71 marked this pull request as draft April 13, 2026 20:35
@lucic71
Copy link
Copy Markdown
Contributor Author

lucic71 commented Apr 13, 2026

This is still a draft because not all unit tests pass:

  • function pointer casting
  • comparing local function pointer with stdlib function pointer. This is especially tricky for stdlib function pointers that have associated translation rules

@lucic71 lucic71 marked this pull request as ready for review April 15, 2026 14:49
lucic71 added 27 commits April 15, 2026 15:52
Non-capturing lambdas are translated as Option<fn>, same as free
functions, because they can be decayed to free functions.

Capturing lambdas remain Rc<dyn Fn>.
Previously function pointers were modeled as Option<fn>, but this is
problematic for function pointer casts. Option<fn> works well in unsafe
with std::mem::transmute, however there is no safe way to achieve the
same operation in refcount.

This is solved using the new Ptr<fn>. To allow casting between different
function types, use type erased Rc<dyn Any> inside the new PtrKind::Fn.
Equality of function pointers is achieved through implementing the
OriginalAlloc::address method.

The C standard allows converting function pointers between incompatible
function types. UB is triggered only when the incompatible pointer is
called. For this reason the new FnState implements 2 new concepts:
  1. casting adaptors (to allow argument casting between ABI compatible
    types)
  2. provenance stack (to allow round-trip function pointer casts)

For 1., consider the following cast:

        int fn_taking_int_ptr(int *p);
        int (*fn_taking_void_ptr)(void*) = (int (*)(void*))fn_taking_int_ptr;

Calling fn_taking_int_ptr with an int* argument works because both int*
and void* have the same size. To support this in Rust we need to create
an int* -> void* adapter when casting from fn_taking_int_ptr to
fn_taking_void_ptr:

        fn_taking_int_ptr.cast_fn::<fn(AnyPtr) -> i32>(Some(
          (|a0: AnyPtr| -> i32 {
              fn_taking_int_ptr(a0.cast::<i32>().unwrap())
          }) as fn(AnyPtr) -> i32
        ))

The job of the adapter is to convert from AnyPtr to Ptr<i32>.
Ptr::cast_fn is a new function that takes as type argument the type of
the target function pointer and an optional adaptor. If cast_fn receives
None, then there is no valid adaptor from source to target, matching the
UB semantics of calling a function through an incompatible function
pointer:

        int add(int a, int b) { return a + b; }
        void (*wrong)(void) = (void (*)(void))add;
        wrong()

For 2., the provenance stack contains all casts performed on the pointer
in the past. Compared to PtrKind::Reinterpreted, PtrKind::Fn has no
backing byte storage through OriginalAlloc, so each cast must know its
history in order to allow round-trip casts, such as:

        int (*)(int, int) -> void (*)(void) -> int (*)(int, int)
                          (1)               (2)

For this specific case, where both (1) and (2) create non-compatible
adaptors (because of non-compatible arguments), we cannot recover a call
to the original function after (1) is performed. For this to work, save
a stack of provenance, and when (2) is perfomed, cast_fn recovers the
original function pointer. See test_roundtrip in fn_ptr_cast.cpp.

A current limitation of this approach is that it only allows function
pointer casts where the source is a direct declaration of a function.
Accessing a function pointer through a member field for example, would
create a capturing adapter which does not coerce in a fn inside Ptr<fn>.
Also make the rules public so that generated code can reference them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant