Skip to content

Commit 46cff54

Browse files
committed
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.
1 parent 0d7d9e0 commit 46cff54

File tree

5 files changed

+71
-73
lines changed

5 files changed

+71
-73
lines changed

macro/src/expand.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,12 +1332,12 @@ fn expand_unique_ptr(
13321332
let new_method = if can_construct_from_value {
13331333
Some(quote! {
13341334
#[doc(hidden)]
1335-
fn __new(value: Self) -> *mut ::std::ffi::c_void {
1335+
fn __new(value: Self) -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
13361336
extern "C" {
13371337
#[link_name = #link_uninit]
1338-
fn __uninit(this: *mut *mut ::std::ffi::c_void) -> *mut ::std::ffi::c_void;
1338+
fn __uninit(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::std::ffi::c_void;
13391339
}
1340-
let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
1340+
let mut repr = ::std::mem::MaybeUninit::uninit();
13411341
unsafe { __uninit(&mut repr).cast::<#ident #ty_generics>().write(value) }
13421342
repr
13431343
}
@@ -1357,47 +1357,47 @@ fn expand_unique_ptr(
13571357
f.write_str(#name)
13581358
}
13591359
#[doc(hidden)]
1360-
fn __null() -> *mut ::std::ffi::c_void {
1360+
fn __null() -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
13611361
extern "C" {
13621362
#[link_name = #link_null]
1363-
fn __null(this: *mut *mut ::std::ffi::c_void);
1363+
fn __null(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
13641364
}
1365-
let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
1365+
let mut repr = ::std::mem::MaybeUninit::uninit();
13661366
unsafe { __null(&mut repr) }
13671367
repr
13681368
}
13691369
#new_method
13701370
#[doc(hidden)]
1371-
unsafe fn __raw(raw: *mut Self) -> *mut ::std::ffi::c_void {
1371+
unsafe fn __raw(raw: *mut Self) -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
13721372
extern "C" {
13731373
#[link_name = #link_raw]
1374-
fn __raw(this: *mut *mut ::std::ffi::c_void, raw: *mut ::std::ffi::c_void);
1374+
fn __raw(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>, raw: *mut ::std::ffi::c_void);
13751375
}
1376-
let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
1376+
let mut repr = ::std::mem::MaybeUninit::uninit();
13771377
__raw(&mut repr, raw.cast());
13781378
repr
13791379
}
13801380
#[doc(hidden)]
1381-
unsafe fn __get(repr: *mut ::std::ffi::c_void) -> *const Self {
1381+
unsafe fn __get(repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const Self {
13821382
extern "C" {
13831383
#[link_name = #link_get]
1384-
fn __get(this: *const *mut ::std::ffi::c_void) -> *const ::std::ffi::c_void;
1384+
fn __get(this: *const ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const ::std::ffi::c_void;
13851385
}
13861386
__get(&repr).cast()
13871387
}
13881388
#[doc(hidden)]
1389-
unsafe fn __release(mut repr: *mut ::std::ffi::c_void) -> *mut Self {
1389+
unsafe fn __release(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut Self {
13901390
extern "C" {
13911391
#[link_name = #link_release]
1392-
fn __release(this: *mut *mut ::std::ffi::c_void) -> *mut ::std::ffi::c_void;
1392+
fn __release(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::std::ffi::c_void;
13931393
}
13941394
__release(&mut repr).cast()
13951395
}
13961396
#[doc(hidden)]
1397-
unsafe fn __drop(mut repr: *mut ::std::ffi::c_void) {
1397+
unsafe fn __drop(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) {
13981398
extern "C" {
13991399
#[link_name = #link_drop]
1400-
fn __drop(this: *mut *mut ::std::ffi::c_void);
1400+
fn __drop(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
14011401
}
14021402
__drop(&mut repr);
14031403
}
@@ -1644,46 +1644,46 @@ fn expand_cxx_vector(
16441644
}
16451645
#by_value_methods
16461646
#[doc(hidden)]
1647-
fn __unique_ptr_null() -> *mut ::std::ffi::c_void {
1647+
fn __unique_ptr_null() -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
16481648
extern "C" {
16491649
#[link_name = #link_unique_ptr_null]
1650-
fn __unique_ptr_null(this: *mut *mut ::std::ffi::c_void);
1650+
fn __unique_ptr_null(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
16511651
}
1652-
let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
1652+
let mut repr = ::std::mem::MaybeUninit::uninit();
16531653
unsafe { __unique_ptr_null(&mut repr) }
16541654
repr
16551655
}
16561656
#[doc(hidden)]
1657-
unsafe fn __unique_ptr_raw(raw: *mut ::cxx::CxxVector<Self>) -> *mut ::std::ffi::c_void {
1657+
unsafe fn __unique_ptr_raw(raw: *mut ::cxx::CxxVector<Self>) -> ::std::mem::MaybeUninit<*mut ::std::ffi::c_void> {
16581658
extern "C" {
16591659
#[link_name = #link_unique_ptr_raw]
1660-
fn __unique_ptr_raw #impl_generics(this: *mut *mut ::std::ffi::c_void, raw: *mut ::cxx::CxxVector<#elem #ty_generics>);
1660+
fn __unique_ptr_raw #impl_generics(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>, raw: *mut ::cxx::CxxVector<#elem #ty_generics>);
16611661
}
1662-
let mut repr = ::std::ptr::null_mut::<::std::ffi::c_void>();
1662+
let mut repr = ::std::mem::MaybeUninit::uninit();
16631663
__unique_ptr_raw(&mut repr, raw);
16641664
repr
16651665
}
16661666
#[doc(hidden)]
1667-
unsafe fn __unique_ptr_get(repr: *mut ::std::ffi::c_void) -> *const ::cxx::CxxVector<Self> {
1667+
unsafe fn __unique_ptr_get(repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const ::cxx::CxxVector<Self> {
16681668
extern "C" {
16691669
#[link_name = #link_unique_ptr_get]
1670-
fn __unique_ptr_get #impl_generics(this: *const *mut ::std::ffi::c_void) -> *const ::cxx::CxxVector<#elem #ty_generics>;
1670+
fn __unique_ptr_get #impl_generics(this: *const ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *const ::cxx::CxxVector<#elem #ty_generics>;
16711671
}
16721672
__unique_ptr_get(&repr)
16731673
}
16741674
#[doc(hidden)]
1675-
unsafe fn __unique_ptr_release(mut repr: *mut ::std::ffi::c_void) -> *mut ::cxx::CxxVector<Self> {
1675+
unsafe fn __unique_ptr_release(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::cxx::CxxVector<Self> {
16761676
extern "C" {
16771677
#[link_name = #link_unique_ptr_release]
1678-
fn __unique_ptr_release #impl_generics(this: *mut *mut ::std::ffi::c_void) -> *mut ::cxx::CxxVector<#elem #ty_generics>;
1678+
fn __unique_ptr_release #impl_generics(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) -> *mut ::cxx::CxxVector<#elem #ty_generics>;
16791679
}
16801680
__unique_ptr_release(&mut repr)
16811681
}
16821682
#[doc(hidden)]
1683-
unsafe fn __unique_ptr_drop(mut repr: *mut ::std::ffi::c_void) {
1683+
unsafe fn __unique_ptr_drop(mut repr: ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>) {
16841684
extern "C" {
16851685
#[link_name = #link_unique_ptr_drop]
1686-
fn __unique_ptr_drop(this: *mut *mut ::std::ffi::c_void);
1686+
fn __unique_ptr_drop(this: *mut ::std::mem::MaybeUninit<*mut ::std::ffi::c_void>);
16871687
}
16881688
__unique_ptr_drop(&mut repr);
16891689
}

src/cxx_vector.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::iter::FusedIterator;
1010
use core::marker::{PhantomData, PhantomPinned};
1111
use core::mem::{self, ManuallyDrop, MaybeUninit};
1212
use core::pin::Pin;
13-
use core::ptr;
1413
use core::slice;
1514

1615
/// Binding to C++ `std::vector<T, std::allocator<T>>`.
@@ -351,15 +350,15 @@ pub unsafe trait VectorElement: Sized {
351350
unreachable!()
352351
}
353352
#[doc(hidden)]
354-
fn __unique_ptr_null() -> *mut c_void;
353+
fn __unique_ptr_null() -> MaybeUninit<*mut c_void>;
355354
#[doc(hidden)]
356-
unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void;
355+
unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> MaybeUninit<*mut c_void>;
357356
#[doc(hidden)]
358-
unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>;
357+
unsafe fn __unique_ptr_get(repr: MaybeUninit<*mut c_void>) -> *const CxxVector<Self>;
359358
#[doc(hidden)]
360-
unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>;
359+
unsafe fn __unique_ptr_release(repr: MaybeUninit<*mut c_void>) -> *mut CxxVector<Self>;
361360
#[doc(hidden)]
362-
unsafe fn __unique_ptr_drop(repr: *mut c_void);
361+
unsafe fn __unique_ptr_drop(repr: MaybeUninit<*mut c_void>);
363362
}
364363

365364
macro_rules! vector_element_by_value_methods {
@@ -420,55 +419,55 @@ macro_rules! impl_vector_element {
420419
}
421420
vector_element_by_value_methods!($kind, $segment, $ty);
422421
#[doc(hidden)]
423-
fn __unique_ptr_null() -> *mut c_void {
422+
fn __unique_ptr_null() -> MaybeUninit<*mut c_void> {
424423
extern "C" {
425424
attr! {
426425
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$null")]
427-
fn __unique_ptr_null(this: *mut *mut c_void);
426+
fn __unique_ptr_null(this: *mut MaybeUninit<*mut c_void>);
428427
}
429428
}
430-
let mut repr = ptr::null_mut::<c_void>();
429+
let mut repr = MaybeUninit::uninit();
431430
unsafe { __unique_ptr_null(&mut repr) }
432431
repr
433432
}
434433
#[doc(hidden)]
435-
unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void {
434+
unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> MaybeUninit<*mut c_void> {
436435
extern "C" {
437436
attr! {
438437
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$raw")]
439-
fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>);
438+
fn __unique_ptr_raw(this: *mut MaybeUninit<*mut c_void>, raw: *mut CxxVector<$ty>);
440439
}
441440
}
442-
let mut repr = ptr::null_mut::<c_void>();
441+
let mut repr = MaybeUninit::uninit();
443442
__unique_ptr_raw(&mut repr, raw);
444443
repr
445444
}
446445
#[doc(hidden)]
447-
unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> {
446+
unsafe fn __unique_ptr_get(repr: MaybeUninit<*mut c_void>) -> *const CxxVector<Self> {
448447
extern "C" {
449448
attr! {
450449
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$get")]
451-
fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>;
450+
fn __unique_ptr_get(this: *const MaybeUninit<*mut c_void>) -> *const CxxVector<$ty>;
452451
}
453452
}
454453
__unique_ptr_get(&repr)
455454
}
456455
#[doc(hidden)]
457-
unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> {
456+
unsafe fn __unique_ptr_release(mut repr: MaybeUninit<*mut c_void>) -> *mut CxxVector<Self> {
458457
extern "C" {
459458
attr! {
460459
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$release")]
461-
fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>;
460+
fn __unique_ptr_release(this: *mut MaybeUninit<*mut c_void>) -> *mut CxxVector<$ty>;
462461
}
463462
}
464463
__unique_ptr_release(&mut repr)
465464
}
466465
#[doc(hidden)]
467-
unsafe fn __unique_ptr_drop(mut repr: *mut c_void) {
466+
unsafe fn __unique_ptr_drop(mut repr: MaybeUninit<*mut c_void>) {
468467
extern "C" {
469468
attr! {
470469
#[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$drop")]
471-
fn __unique_ptr_drop(this: *mut *mut c_void);
470+
fn __unique_ptr_drop(this: *mut MaybeUninit<*mut c_void>);
472471
}
473472
}
474473
__unique_ptr_drop(&mut repr);

src/shared_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct SharedPtr<T>
1515
where
1616
T: SharedPtrTarget,
1717
{
18-
repr: [*mut c_void; 2],
18+
repr: [MaybeUninit<*mut c_void>; 2],
1919
ty: PhantomData<T>,
2020
}
2121

src/unique_ptr.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ use crate::ExternType;
66
use core::ffi::c_void;
77
use core::fmt::{self, Debug, Display};
88
use core::marker::PhantomData;
9-
use core::mem;
9+
use core::mem::{self, MaybeUninit};
1010
use core::ops::{Deref, DerefMut};
1111
use core::pin::Pin;
12-
use core::ptr;
1312

1413
/// Binding to C++ `std::unique_ptr<T, std::default_delete<T>>`.
1514
#[repr(C)]
1615
pub struct UniquePtr<T>
1716
where
1817
T: UniquePtrTarget,
1918
{
20-
repr: *mut c_void,
19+
repr: MaybeUninit<*mut c_void>,
2120
ty: PhantomData<T>,
2221
}
2322

@@ -207,9 +206,9 @@ pub unsafe trait UniquePtrTarget {
207206
#[doc(hidden)]
208207
fn __typename(f: &mut fmt::Formatter) -> fmt::Result;
209208
#[doc(hidden)]
210-
fn __null() -> *mut c_void;
209+
fn __null() -> MaybeUninit<*mut c_void>;
211210
#[doc(hidden)]
212-
fn __new(value: Self) -> *mut c_void
211+
fn __new(value: Self) -> MaybeUninit<*mut c_void>
213212
where
214213
Self: Sized,
215214
{
@@ -219,26 +218,26 @@ pub unsafe trait UniquePtrTarget {
219218
unreachable!()
220219
}
221220
#[doc(hidden)]
222-
unsafe fn __raw(raw: *mut Self) -> *mut c_void;
221+
unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void>;
223222
#[doc(hidden)]
224-
unsafe fn __get(repr: *mut c_void) -> *const Self;
223+
unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self;
225224
#[doc(hidden)]
226-
unsafe fn __release(repr: *mut c_void) -> *mut Self;
225+
unsafe fn __release(repr: MaybeUninit<*mut c_void>) -> *mut Self;
227226
#[doc(hidden)]
228-
unsafe fn __drop(repr: *mut c_void);
227+
unsafe fn __drop(repr: MaybeUninit<*mut c_void>);
229228
}
230229

231230
extern "C" {
232231
#[link_name = "cxxbridge1$unique_ptr$std$string$null"]
233-
fn unique_ptr_std_string_null(this: *mut *mut c_void);
232+
fn unique_ptr_std_string_null(this: *mut MaybeUninit<*mut c_void>);
234233
#[link_name = "cxxbridge1$unique_ptr$std$string$raw"]
235-
fn unique_ptr_std_string_raw(this: *mut *mut c_void, raw: *mut CxxString);
234+
fn unique_ptr_std_string_raw(this: *mut MaybeUninit<*mut c_void>, raw: *mut CxxString);
236235
#[link_name = "cxxbridge1$unique_ptr$std$string$get"]
237-
fn unique_ptr_std_string_get(this: *const *mut c_void) -> *const CxxString;
236+
fn unique_ptr_std_string_get(this: *const MaybeUninit<*mut c_void>) -> *const CxxString;
238237
#[link_name = "cxxbridge1$unique_ptr$std$string$release"]
239-
fn unique_ptr_std_string_release(this: *mut *mut c_void) -> *mut CxxString;
238+
fn unique_ptr_std_string_release(this: *mut MaybeUninit<*mut c_void>) -> *mut CxxString;
240239
#[link_name = "cxxbridge1$unique_ptr$std$string$drop"]
241-
fn unique_ptr_std_string_drop(this: *mut *mut c_void);
240+
fn unique_ptr_std_string_drop(this: *mut MaybeUninit<*mut c_void>);
242241
}
243242

244243
unsafe impl UniquePtrTarget for CxxString {
@@ -247,29 +246,29 @@ unsafe impl UniquePtrTarget for CxxString {
247246
f.write_str("CxxString")
248247
}
249248
#[doc(hidden)]
250-
fn __null() -> *mut c_void {
251-
let mut repr = ptr::null_mut::<c_void>();
249+
fn __null() -> MaybeUninit<*mut c_void> {
250+
let mut repr = MaybeUninit::uninit();
252251
unsafe {
253252
unique_ptr_std_string_null(&mut repr);
254253
}
255254
repr
256255
}
257256
#[doc(hidden)]
258-
unsafe fn __raw(raw: *mut Self) -> *mut c_void {
259-
let mut repr = ptr::null_mut::<c_void>();
257+
unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
258+
let mut repr = MaybeUninit::uninit();
260259
unique_ptr_std_string_raw(&mut repr, raw);
261260
repr
262261
}
263262
#[doc(hidden)]
264-
unsafe fn __get(repr: *mut c_void) -> *const Self {
263+
unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
265264
unique_ptr_std_string_get(&repr)
266265
}
267266
#[doc(hidden)]
268-
unsafe fn __release(mut repr: *mut c_void) -> *mut Self {
267+
unsafe fn __release(mut repr: MaybeUninit<*mut c_void>) -> *mut Self {
269268
unique_ptr_std_string_release(&mut repr)
270269
}
271270
#[doc(hidden)]
272-
unsafe fn __drop(mut repr: *mut c_void) {
271+
unsafe fn __drop(mut repr: MaybeUninit<*mut c_void>) {
273272
unique_ptr_std_string_drop(&mut repr);
274273
}
275274
}
@@ -283,23 +282,23 @@ where
283282
write!(f, "CxxVector<{}>", display(T::__typename))
284283
}
285284
#[doc(hidden)]
286-
fn __null() -> *mut c_void {
285+
fn __null() -> MaybeUninit<*mut c_void> {
287286
T::__unique_ptr_null()
288287
}
289288
#[doc(hidden)]
290-
unsafe fn __raw(raw: *mut Self) -> *mut c_void {
289+
unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
291290
T::__unique_ptr_raw(raw)
292291
}
293292
#[doc(hidden)]
294-
unsafe fn __get(repr: *mut c_void) -> *const Self {
293+
unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
295294
T::__unique_ptr_get(repr)
296295
}
297296
#[doc(hidden)]
298-
unsafe fn __release(repr: *mut c_void) -> *mut Self {
297+
unsafe fn __release(repr: MaybeUninit<*mut c_void>) -> *mut Self {
299298
T::__unique_ptr_release(repr)
300299
}
301300
#[doc(hidden)]
302-
unsafe fn __drop(repr: *mut c_void) {
301+
unsafe fn __drop(repr: MaybeUninit<*mut c_void>) {
303302
T::__unique_ptr_drop(repr);
304303
}
305304
}

src/weak_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct WeakPtr<T>
1616
where
1717
T: WeakPtrTarget,
1818
{
19-
repr: [*mut c_void; 2],
19+
repr: [MaybeUninit<*mut c_void>; 2],
2020
ty: PhantomData<T>,
2121
}
2222

0 commit comments

Comments
 (0)