Avoid asserting to llvm that C++ ptr type bits are fully occupied
For example if hypothetically any of std::unique_ptr or std::shared_ptr
or std::weak_ptr contained any padding bits in their representation,
manipulating any of them by value in Rust as if they were fully
initialized pointers would be UB.
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index ce02c43..bc3f000 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -1332,12 +1332,12 @@
let new_method = if can_construct_from_value {
Some(quote! {
#[doc(hidden)]
- fn __new(value: Self) -> *mut ::std::ffi::c_void {
+ fn __new(value: Self) -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
extern "C" {
#[link_name = #link_uninit]
- fn __uninit(this: *mut *mut ::std::ffi::c_void) -> *mut ::std::ffi::c_void;
+ fn __uninit(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::std::ffi::c_void;
}
- let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
+ let mut repr = ::std::mem::MaybeUninit::uninit();
unsafe { __uninit(&mut repr).cast::<#ident #ty_generics>().write(value) }
repr
}
@@ -1357,47 +1357,47 @@
f.write_str(#name)
}
#[doc(hidden)]
- fn __null() -> *mut ::std::ffi::c_void {
+ fn __null() -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
extern "C" {
#[link_name = #link_null]
- fn __null(this: *mut *mut ::std::ffi::c_void);
+ fn __null(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
}
- let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
+ let mut repr = ::std::mem::MaybeUninit::uninit();
unsafe { __null(&mut repr) }
repr
}
#new_method
#[doc(hidden)]
- unsafe fn __raw(raw: *mut Self) -> *mut ::std::ffi::c_void {
+ unsafe fn __raw(raw: *mut Self) -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
extern "C" {
#[link_name = #link_raw]
- fn __raw(this: *mut *mut ::std::ffi::c_void, raw: *mut ::std::ffi::c_void);
+ fn __raw(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>, raw: *mut ::std::ffi::c_void);
}
- let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
+ let mut repr = ::std::mem::MaybeUninit::uninit();
__raw(&mut repr, raw.cast());
repr
}
#[doc(hidden)]
- unsafe fn __get(repr: *mut ::std::ffi::c_void) -> *const Self {
+ unsafe fn __get(repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const Self {
extern "C" {
#[link_name = #link_get]
- fn __get(this: *const *mut ::std::ffi::c_void) -> *const ::std::ffi::c_void;
+ fn __get(this: *const ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const ::std::ffi::c_void;
}
__get(&repr).cast()
}
#[doc(hidden)]
- unsafe fn __release(mut repr: *mut ::std::ffi::c_void) -> *mut Self {
+ unsafe fn __release(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut Self {
extern "C" {
#[link_name = #link_release]
- fn __release(this: *mut *mut ::std::ffi::c_void) -> *mut ::std::ffi::c_void;
+ fn __release(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::std::ffi::c_void;
}
__release(&mut repr).cast()
}
#[doc(hidden)]
- unsafe fn __drop(mut repr: *mut ::std::ffi::c_void) {
+ unsafe fn __drop(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) {
extern "C" {
#[link_name = #link_drop]
- fn __drop(this: *mut *mut ::std::ffi::c_void);
+ fn __drop(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
}
__drop(&mut repr);
}
@@ -1644,46 +1644,46 @@
}
#by_value_methods
#[doc(hidden)]
- fn __unique_ptr_null() -> *mut ::std::ffi::c_void {
+ fn __unique_ptr_null() -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
extern "C" {
#[link_name = #link_unique_ptr_null]
- fn __unique_ptr_null(this: *mut *mut ::std::ffi::c_void);
+ fn __unique_ptr_null(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
}
- let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
+ let mut repr = ::std::mem::MaybeUninit::uninit();
unsafe { __unique_ptr_null(&mut repr) }
repr
}
#[doc(hidden)]
- unsafe fn __unique_ptr_raw(raw: *mut ::cxx::CxxVector<Self>) -> *mut ::std::ffi::c_void {
+ unsafe fn __unique_ptr_raw(raw: *mut ::cxx::CxxVector<Self>) -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
extern "C" {
#[link_name = #link_unique_ptr_raw]
- fn __unique_ptr_raw #impl_generics(this: *mut *mut ::std::ffi::c_void, raw: *mut ::cxx::CxxVector<#elem #ty_generics>);
+ fn __unique_ptr_raw #impl_generics(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>, raw: *mut ::cxx::CxxVector<#elem #ty_generics>);
}
- let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
+ let mut repr = ::std::mem::MaybeUninit::uninit();
__unique_ptr_raw(&mut repr, raw);
repr
}
#[doc(hidden)]
- unsafe fn __unique_ptr_get(repr: *mut ::std::ffi::c_void) -> *const ::cxx::CxxVector<Self> {
+ unsafe fn __unique_ptr_get(repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const ::cxx::CxxVector<Self> {
extern "C" {
#[link_name = #link_unique_ptr_get]
- fn __unique_ptr_get #impl_generics(this: *const *mut ::std::ffi::c_void) -> *const ::cxx::CxxVector<#elem #ty_generics>;
+ fn __unique_ptr_get #impl_generics(this: *const ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const ::cxx::CxxVector<#elem #ty_generics>;
}
__unique_ptr_get(&repr)
}
#[doc(hidden)]
- unsafe fn __unique_ptr_release(mut repr: *mut ::std::ffi::c_void) -> *mut ::cxx::CxxVector<Self> {
+ unsafe fn __unique_ptr_release(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::cxx::CxxVector<Self> {
extern "C" {
#[link_name = #link_unique_ptr_release]
- fn __unique_ptr_release #impl_generics(this: *mut *mut ::std::ffi::c_void) -> *mut ::cxx::CxxVector<#elem #ty_generics>;
+ fn __unique_ptr_release #impl_generics(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::cxx::CxxVector<#elem #ty_generics>;
}
__unique_ptr_release(&mut repr)
}
#[doc(hidden)]
- unsafe fn __unique_ptr_drop(mut repr: *mut ::std::ffi::c_void) {
+ unsafe fn __unique_ptr_drop(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) {
extern "C" {
#[link_name = #link_unique_ptr_drop]
- fn __unique_ptr_drop(this: *mut *mut ::std::ffi::c_void);
+ fn __unique_ptr_drop(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
}
__unique_ptr_drop(&mut repr);
}
diff --git a/src/cxx_vector.rs b/src/cxx_vector.rs
index 23cbcc1..87a985e 100644
--- a/src/cxx_vector.rs
+++ b/src/cxx_vector.rs
@@ -10,7 +10,6 @@
use core::marker::{PhantomData, PhantomPinned};
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::pin::Pin;
-use core::ptr;
use core::slice;
/// Binding to C++ `std::vector<T, std::allocator<T>>`.
@@ -351,15 +350,15 @@
unreachable!()
}
#[doc(hidden)]
- fn __unique_ptr_null() -> *mut c_void;
+ fn __unique_ptr_null() -> MaybeUninit<*mut c_void>;
#[doc(hidden)]
- unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void;
+ unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> MaybeUninit<*mut c_void>;
#[doc(hidden)]
- unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>;
+ unsafe fn __unique_ptr_get(repr: MaybeUninit<*mut c_void>) -> *const CxxVector<Self>;
#[doc(hidden)]
- unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>;
+ unsafe fn __unique_ptr_release(repr: MaybeUninit<*mut c_void>) -> *mut CxxVector<Self>;
#[doc(hidden)]
- unsafe fn __unique_ptr_drop(repr: *mut c_void);
+ unsafe fn __unique_ptr_drop(repr: MaybeUninit<*mut c_void>);
}
macro_rules! vector_element_by_value_methods {
@@ -420,55 +419,55 @@
}
vector_element_by_value_methods!($kind, $segment, $ty);
#[doc(hidden)]
- fn __unique_ptr_null() -> *mut c_void {
+ fn __unique_ptr_null() -> MaybeUninit<*mut c_void> {
extern "C" {
attr! {
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$null")]
- fn __unique_ptr_null(this: *mut *mut c_void);
+ fn __unique_ptr_null(this: *mut MaybeUninit<*mut c_void>);
}
}
- let mut repr = ptr::null_mut::<c_void>();
+ let mut repr = MaybeUninit::uninit();
unsafe { __unique_ptr_null(&mut repr) }
repr
}
#[doc(hidden)]
- unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void {
+ unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> MaybeUninit<*mut c_void> {
extern "C" {
attr! {
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$raw")]
- fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>);
+ fn __unique_ptr_raw(this: *mut MaybeUninit<*mut c_void>, raw: *mut CxxVector<$ty>);
}
}
- let mut repr = ptr::null_mut::<c_void>();
+ let mut repr = MaybeUninit::uninit();
__unique_ptr_raw(&mut repr, raw);
repr
}
#[doc(hidden)]
- unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> {
+ unsafe fn __unique_ptr_get(repr: MaybeUninit<*mut c_void>) -> *const CxxVector<Self> {
extern "C" {
attr! {
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$get")]
- fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>;
+ fn __unique_ptr_get(this: *const MaybeUninit<*mut c_void>) -> *const CxxVector<$ty>;
}
}
__unique_ptr_get(&repr)
}
#[doc(hidden)]
- unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> {
+ unsafe fn __unique_ptr_release(mut repr: MaybeUninit<*mut c_void>) -> *mut CxxVector<Self> {
extern "C" {
attr! {
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$release")]
- fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>;
+ fn __unique_ptr_release(this: *mut MaybeUninit<*mut c_void>) -> *mut CxxVector<$ty>;
}
}
__unique_ptr_release(&mut repr)
}
#[doc(hidden)]
- unsafe fn __unique_ptr_drop(mut repr: *mut c_void) {
+ unsafe fn __unique_ptr_drop(mut repr: MaybeUninit<*mut c_void>) {
extern "C" {
attr! {
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$drop")]
- fn __unique_ptr_drop(this: *mut *mut c_void);
+ fn __unique_ptr_drop(this: *mut MaybeUninit<*mut c_void>);
}
}
__unique_ptr_drop(&mut repr);
diff --git a/src/shared_ptr.rs b/src/shared_ptr.rs
index 66b988b..20c7e0f 100644
--- a/src/shared_ptr.rs
+++ b/src/shared_ptr.rs
@@ -15,7 +15,7 @@
where
T: SharedPtrTarget,
{
- repr: [*mut c_void; 2],
+ repr: [MaybeUninit<*mut c_void>; 2],
ty: PhantomData<T>,
}
diff --git a/src/unique_ptr.rs b/src/unique_ptr.rs
index 836f467..d9ac442 100644
--- a/src/unique_ptr.rs
+++ b/src/unique_ptr.rs
@@ -6,10 +6,9 @@
use core::ffi::c_void;
use core::fmt::{self, Debug, Display};
use core::marker::PhantomData;
-use core::mem;
+use core::mem::{self, MaybeUninit};
use core::ops::{Deref, DerefMut};
use core::pin::Pin;
-use core::ptr;
/// Binding to C++ `std::unique_ptr<T, std::default_delete<T>>`.
#[repr(C)]
@@ -17,7 +16,7 @@
where
T: UniquePtrTarget,
{
- repr: *mut c_void,
+ repr: MaybeUninit<*mut c_void>,
ty: PhantomData<T>,
}
@@ -207,9 +206,9 @@
#[doc(hidden)]
fn __typename(f: &mut fmt::Formatter) -> fmt::Result;
#[doc(hidden)]
- fn __null() -> *mut c_void;
+ fn __null() -> MaybeUninit<*mut c_void>;
#[doc(hidden)]
- fn __new(value: Self) -> *mut c_void
+ fn __new(value: Self) -> MaybeUninit<*mut c_void>
where
Self: Sized,
{
@@ -219,26 +218,26 @@
unreachable!()
}
#[doc(hidden)]
- unsafe fn __raw(raw: *mut Self) -> *mut c_void;
+ unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void>;
#[doc(hidden)]
- unsafe fn __get(repr: *mut c_void) -> *const Self;
+ unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self;
#[doc(hidden)]
- unsafe fn __release(repr: *mut c_void) -> *mut Self;
+ unsafe fn __release(repr: MaybeUninit<*mut c_void>) -> *mut Self;
#[doc(hidden)]
- unsafe fn __drop(repr: *mut c_void);
+ unsafe fn __drop(repr: MaybeUninit<*mut c_void>);
}
extern "C" {
#[link_name = "cxxbridge1$unique_ptr$std$string$null"]
- fn unique_ptr_std_string_null(this: *mut *mut c_void);
+ fn unique_ptr_std_string_null(this: *mut MaybeUninit<*mut c_void>);
#[link_name = "cxxbridge1$unique_ptr$std$string$raw"]
- fn unique_ptr_std_string_raw(this: *mut *mut c_void, raw: *mut CxxString);
+ fn unique_ptr_std_string_raw(this: *mut MaybeUninit<*mut c_void>, raw: *mut CxxString);
#[link_name = "cxxbridge1$unique_ptr$std$string$get"]
- fn unique_ptr_std_string_get(this: *const *mut c_void) -> *const CxxString;
+ fn unique_ptr_std_string_get(this: *const MaybeUninit<*mut c_void>) -> *const CxxString;
#[link_name = "cxxbridge1$unique_ptr$std$string$release"]
- fn unique_ptr_std_string_release(this: *mut *mut c_void) -> *mut CxxString;
+ fn unique_ptr_std_string_release(this: *mut MaybeUninit<*mut c_void>) -> *mut CxxString;
#[link_name = "cxxbridge1$unique_ptr$std$string$drop"]
- fn unique_ptr_std_string_drop(this: *mut *mut c_void);
+ fn unique_ptr_std_string_drop(this: *mut MaybeUninit<*mut c_void>);
}
unsafe impl UniquePtrTarget for CxxString {
@@ -247,29 +246,29 @@
f.write_str("CxxString")
}
#[doc(hidden)]
- fn __null() -> *mut c_void {
- let mut repr = ptr::null_mut::<c_void>();
+ fn __null() -> MaybeUninit<*mut c_void> {
+ let mut repr = MaybeUninit::uninit();
unsafe {
unique_ptr_std_string_null(&mut repr);
}
repr
}
#[doc(hidden)]
- unsafe fn __raw(raw: *mut Self) -> *mut c_void {
- let mut repr = ptr::null_mut::<c_void>();
+ unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
+ let mut repr = MaybeUninit::uninit();
unique_ptr_std_string_raw(&mut repr, raw);
repr
}
#[doc(hidden)]
- unsafe fn __get(repr: *mut c_void) -> *const Self {
+ unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
unique_ptr_std_string_get(&repr)
}
#[doc(hidden)]
- unsafe fn __release(mut repr: *mut c_void) -> *mut Self {
+ unsafe fn __release(mut repr: MaybeUninit<*mut c_void>) -> *mut Self {
unique_ptr_std_string_release(&mut repr)
}
#[doc(hidden)]
- unsafe fn __drop(mut repr: *mut c_void) {
+ unsafe fn __drop(mut repr: MaybeUninit<*mut c_void>) {
unique_ptr_std_string_drop(&mut repr);
}
}
@@ -283,23 +282,23 @@
write!(f, "CxxVector<{}>", display(T::__typename))
}
#[doc(hidden)]
- fn __null() -> *mut c_void {
+ fn __null() -> MaybeUninit<*mut c_void> {
T::__unique_ptr_null()
}
#[doc(hidden)]
- unsafe fn __raw(raw: *mut Self) -> *mut c_void {
+ unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
T::__unique_ptr_raw(raw)
}
#[doc(hidden)]
- unsafe fn __get(repr: *mut c_void) -> *const Self {
+ unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
T::__unique_ptr_get(repr)
}
#[doc(hidden)]
- unsafe fn __release(repr: *mut c_void) -> *mut Self {
+ unsafe fn __release(repr: MaybeUninit<*mut c_void>) -> *mut Self {
T::__unique_ptr_release(repr)
}
#[doc(hidden)]
- unsafe fn __drop(repr: *mut c_void) {
+ unsafe fn __drop(repr: MaybeUninit<*mut c_void>) {
T::__unique_ptr_drop(repr);
}
}
diff --git a/src/weak_ptr.rs b/src/weak_ptr.rs
index e29d30e..2c06d36 100644
--- a/src/weak_ptr.rs
+++ b/src/weak_ptr.rs
@@ -16,7 +16,7 @@
where
T: WeakPtrTarget,
{
- repr: [*mut c_void; 2],
+ repr: [MaybeUninit<*mut c_void>; 2],
ty: PhantomData<T>,
}