You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>.
0 commit comments