Skip to content

Commit 7ff8662

Browse files
fee1-deadRalfJungoli-obk
committed
add const_make_global; err for const_allocate ptrs if didn't call
Co-Authored-By: Ralf Jung <[email protected]> Co-Authored-By: Oli Scherer <[email protected]>
1 parent 9d16539 commit 7ff8662

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

alloc/src/boxed/thin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use core::error::Error;
66
use core::fmt::{self, Debug, Display, Formatter};
77
#[cfg(not(no_global_oom_handling))]
8-
use core::intrinsics::const_allocate;
8+
use core::intrinsics::{const_allocate, const_make_global};
99
use core::marker::PhantomData;
1010
#[cfg(not(no_global_oom_handling))]
1111
use core::marker::Unsize;
@@ -340,9 +340,10 @@ impl<H> WithHeader<H> {
340340
alloc.add(metadata_offset).cast();
341341
// SAFETY: `*metadata_ptr` is within the allocation.
342342
metadata_ptr.write(ptr::metadata::<Dyn>(ptr::dangling::<T>() as *const Dyn));
343-
343+
// SAFETY: valid heap allocation
344+
const_make_global(alloc);
344345
// SAFETY: we have just written the metadata.
345-
&*(metadata_ptr)
346+
&*metadata_ptr
346347
}
347348
};
348349

core/src/intrinsics/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,6 +2533,15 @@ pub const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize)
25332533
// Runtime NOP
25342534
}
25352535

2536+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2537+
#[rustc_nounwind]
2538+
#[rustc_intrinsic]
2539+
#[miri::intrinsic_fallback_is_spec]
2540+
pub const unsafe fn const_make_global(ptr: *mut u8) -> *const u8 {
2541+
// const eval overrides this function; at runtime, it is a NOP.
2542+
ptr
2543+
}
2544+
25362545
/// Returns whether we should perform contract-checking at runtime.
25372546
///
25382547
/// This is meant to be similar to the ub_checks intrinsic, in terms

0 commit comments

Comments
 (0)