You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These intrinsics can affect the value of a place that may be read later using safe code. It is not immediate UB to write the value to the allocation space, but it would be to later use it with a type T where the value written is not valid.
E.g.:
use std::ptr;/// # Safety/// - Users must not read from `ptr` until a new valid value is written to itunsafefncopy_invalid_char(ptr:&mutchar){let invalid_value:u32 = 0xDE01;// SAFETY: This isunsafe{ ptr::copy(&invalid_value, ptr as*mut_as*mutu32,1)};}/// This checks that there is no UB if the user ensures that a valid character is written before next read.#[cfg_attr(kani, kani::proof)]fncheck_no_ub(){letmut c = 'a';// SAFETY: We will write to `c` before reading it againunsafe{copy_invalid_char(&mut c)};
c = 'b';assert_ne!(c,'a');}/// This checks that there is UB if a read exposes the invalid value.#[cfg_attr(kani, kani::proof)]fncheck_with_ub(){letmut c = 'a';// SAFETY: This is not safe and it will trigger UBunsafe{copy_invalid_char(&mut c)};assert_ne!(c,'a');}// Run this with MIRI#[cfg(miri)]fnmain(){check_no_ub();check_with_ub();}
The text was updated successfully, but these errors were encountered:
celinval
changed the title
Intrinsics: copy_nonoverlapping, copy.
Add support to validity checks in the presence of copy_nonoverlapping, copy.
Dec 5, 2024
These intrinsics can affect the value of a place that may be read later using safe code. It is not immediate UB to write the value to the allocation space, but it would be to later use it with a type T where the value written is not valid.
E.g.:
The text was updated successfully, but these errors were encountered: