Skip to content

Commit

Permalink
Add read_volatile to volatile test
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Nov 18, 2024
1 parent f14161e commit c71dbfd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
2 changes: 1 addition & 1 deletion libgccjit.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29901846ff610daab8a80436cfe36e93b4b5aa1e
50d1270fd6409407f38b982e606df1dba4bf58ed
32 changes: 11 additions & 21 deletions tests/run/volatile2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
// Run-time:
// status: 0

#![no_std]
#![feature(core_intrinsics, start)]

#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
core::intrinsics::abort();
}

mod libc {
#[link(name = "c")]
extern "C" {
Expand Down Expand Up @@ -44,8 +36,7 @@ static mut COUNT: u32 = 0;
static mut STORAGE: *mut u8 = core::ptr::null_mut();
const PAGE_SIZE: usize = 1 << 15;

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
fn main() {
unsafe {
// Register a segfault handler
libc::sigaction(
Expand All @@ -67,8 +58,7 @@ fn main(_argc: isize, _argv: *const *const u8) -> isize {
0,
).cast();
if STORAGE == libc::MAP_FAILED {
libc::puts(b"error: mmap failed\0".as_ptr());
return 1;
panic!("error: mmap failed");
}

let p_count = (&mut COUNT) as *mut u32;
Expand All @@ -81,21 +71,21 @@ fn main(_argc: isize, _argv: *const *const u8) -> isize {
STORAGE.add(PAGE_SIZE).write_volatile(1);
STORAGE.add(0).write_volatile(1);
STORAGE.add(PAGE_SIZE).write_volatile(1);
STORAGE.add(0).read_volatile();
STORAGE.add(PAGE_SIZE).read_volatile();
STORAGE.add(0).write_volatile(1);
STORAGE.add(PAGE_SIZE).write_volatile(1);

// The segfault handler should have been called for every
// `write_volatile` in `STORAGE`. If the compiler ignores volatility,
// some of these writes will be combined, causing a different number of
// segfaults.
// The segfault handler should have been called for every `write_volatile` and
// `read_volatile` in `STORAGE`. If the compiler ignores volatility, some of these writes
// will be combined, causing a different number of segfaults.
//
// This `p_count` read is done by a volatile read. If the compiler
// ignores volatility, the compiler will speculate that `*p_count` is
// unchanged and remove this check, failing the test.
if p_count.read_volatile() != 6 {
libc::puts(b"error: segfault count mismatch\0".as_ptr());
return 1;
if p_count.read_volatile() != 10 {
panic!("error: segfault count mismatch");
}

0
}
}

Expand Down

0 comments on commit c71dbfd

Please sign in to comment.