Skip to content

Commit 8ce45d8

Browse files
authored
Rollup merge of rust-lang#69651 - Mark-Simulacrum:black-box-marker, r=eddyb
Try to ensure usize marker does not get merged This follows up on [this conversation](rust-lang#69209 (comment)). However, I'm not confident this is quite correct, so feedback is appreciated, as always.
2 parents c31b704 + a9259fb commit 8ce45d8

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/libcore/fmt/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,18 @@ pub struct ArgumentV1<'a> {
264264
// could have been miscompiled. In practice, we never call as_usize on non-usize
265265
// containing data (as a matter of static generation of the formatting
266266
// arguments), so this is merely an additional check.
267+
//
268+
// We primarily want to ensure that the function pointer at `USIZE_MARKER` has
269+
// an address corresponding *only* to functions that also take `&usize` as their
270+
// first argument. The read_volatile here ensures that we can safely ready out a
271+
// usize from the passed reference and that this address does not point at a
272+
// non-usize taking function.
267273
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
268-
static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = |_, _| loop {};
274+
static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = |ptr, _| {
275+
// SAFETY: ptr is a reference
276+
let _v: usize = unsafe { crate::ptr::read_volatile(ptr) };
277+
loop {}
278+
};
269279

270280
impl<'a> ArgumentV1<'a> {
271281
#[doc(hidden)]

0 commit comments

Comments
 (0)