Translate function pointers and lambdas#3
Open
lucic71 wants to merge 35 commits intoCpp2Rust:masterfrom
Open
Conversation
Contributor
Author
|
This is still a draft because not all unit tests pass:
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements the following:
Option<fn>in unsafe andFnPtrin refcountfn_ptr == translated_ruleMinor:
<type>::default()to allow initialization of global vars (static_local.cpp compiles now in unsafe)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:
For 1., consider the following cast:
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:
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:
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:
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 infn1 == freadin fn_ptr_stdlib_compare.cpp, expose the translatoin rule for fread declared in rules/stdio/tgt_recount.rs. The translated code becomes: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:
fn_ptr!is a new macro declared in libcc2rs/src/fn_ptr.rs, which is a syntactic sugar for: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.