From c8c82a0bca8db4dc119536a41112ba8c81f727a5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 20 Nov 2022 11:01:00 +0100 Subject: [PATCH] make align_offset always work on no-provenance pointers --- src/tools/miri/src/shims/mod.rs | 6 ++++++ src/tools/miri/tests/pass/align_offset_symbolic.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/tools/miri/src/shims/mod.rs b/src/tools/miri/src/shims/mod.rs index dcb99a27668..b6efad6b5ee 100644 --- a/src/tools/miri/src/shims/mod.rs +++ b/src/tools/miri/src/shims/mod.rs @@ -89,6 +89,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { } let ptr = this.read_pointer(ptr_op)?; + // If this carries no provenance, treat it like an integer. + if ptr.provenance.is_none() { + // Use actual implementation. + return Ok(false); + } + if let Ok((alloc_id, _offset, _)) = this.ptr_try_get_alloc_id(ptr) { // Only do anything if we can identify the allocation this goes to. let (_size, cur_align, _kind) = this.get_alloc_info(alloc_id); diff --git a/src/tools/miri/tests/pass/align_offset_symbolic.rs b/src/tools/miri/tests/pass/align_offset_symbolic.rs index b3e57338363..4e1584b8387 100644 --- a/src/tools/miri/tests/pass/align_offset_symbolic.rs +++ b/src/tools/miri/tests/pass/align_offset_symbolic.rs @@ -1,4 +1,7 @@ //@compile-flags: -Zmiri-symbolic-alignment-check +#![feature(strict_provenance)] + +use std::ptr; fn test_align_offset() { let d = Box::new([0u32; 4]); @@ -16,6 +19,9 @@ fn test_align_offset() { assert_eq!(raw.wrapping_offset(2).align_offset(2), 0); assert_eq!(raw.wrapping_offset(2).align_offset(4), 2); assert_eq!(raw.wrapping_offset(2).align_offset(8), usize::MAX); // requested alignment higher than allocation alignment + + let p = ptr::invalid::<()>(1); + assert_eq!(p.align_offset(1), 0); } fn test_align_to() {