Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Felipe R. Monteiro <rms.felipe@gmail.com>
  • Loading branch information
celinval and feliperodri authored Jun 12, 2024
1 parent be3f574 commit c4b9c19
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion library/contracts/safety/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
// We add the configurations here to be checked.
println!("cargo:rustc-check-cfg=cfg(kani_host)");
}
}
1 change: 0 additions & 1 deletion library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,6 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
#[stable(feature = "volatile", since = "1.9.0")]
#[rustc_diagnostic_item = "ptr_write_volatile"]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
// What happens to the value that was replaced? No drop?
#[safety::requires(ub_checks::can_write(dst))]
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
// SAFETY: the caller must uphold the safety contract for `volatile_store`.
Expand Down
12 changes: 7 additions & 5 deletions library/core/src/ub_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,20 @@ pub use predicates::*;
/// At runtime, they are no-op, and always return true.
#[cfg(not(kani))]
mod predicates {
/// Check if a pointer can be dereferenced. I.e.:
/// * `src` must be valid for reads. See [crate::ptr] documentation.
/// * `src` must be properly aligned. Use read_unaligned if this is not the case.
/// * `src` must point to a properly initialized value of type T.
/// Checks if a pointer can be dereferenced, ensuring:
/// * `src` is valid for reads (see [`crate::ptr`] documentation).
/// * `src` is properly aligned (use `read_unaligned` if not).
/// * `src` points to a properly initialized value of type `T`.
///
/// [`crate::ptr`]: https://doc.rust-lang.org/std/ptr/index.html
pub fn can_dereference<T>(src: *const T) -> bool {
let _ = src;
true
}

/// Check if a pointer can be written to:
/// * `dst` must be valid for writes.
/// * `dst` must be properly aligned. Use [`write_unaligned`] if this is not the
/// * `dst` must be properly aligned. Use `write_unaligned` if this is not the
/// case.
pub fn can_write<T>(dst: *mut T) -> bool {
let _ = dst;
Expand All @@ -195,6 +196,7 @@ mod predicates {
true
}

/// Check if a pointer can be the target of unaligned writes.
/// * `dst` must be valid for writes.
pub fn can_write_unaligned<T>(dst: *mut T) -> bool {
let _ = dst;
Expand Down

0 comments on commit c4b9c19

Please sign in to comment.