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
The reasons behind the API (and this PR) are following:
- reduce the lifetime of reference obtained from the pointer
Current pointer-to-ref conversion API (ptr_to_ref_[mut]) returns a
reference with a static lifetime. New API is parameterized by a lifetime,
which is not 'static.
- Ensure that IF the memory associated with the pointer was allocated a certain way,
the raw pointer will be converted to the corresponding Rust pointer primitive i.e. Box or Arc
(or reference if the pointer was not obtained from an explicit allocation).
However, this does not give us any guarantees about the origin of the pointer.
Consider following example:
// Rust
impl ArcFFI for Foo {}
fn extern "C" f1() -> *const Foo {
// a pointer to stack variable
// Also applies to some valid pointer obtained from the reference
// to the field of some already heap-allocated object.
// Decided to go with a stack variable to keep the example simple.
let foo = Foo;
&foo
}
fn extern "C" f2(foo: *const Foo) {
let foo = ArcFFI::cloned_from_ptr(foo);
}
// C
Foo *foo = f1();
f2(foo);
// Segfault.
// Even if f1() returned a valid pointer, that points to some
// heap-allocated memory. The pointer was not obtained from an Arc allocation.
// I.e., it was not obtained via Arc::into_raw().
To guarantee this, we need to introduce a special type for pointer that
would represent the pointer's properties. This will be done in a follow-up PR.
0 commit comments