Skip to content

Commit

Permalink
codecheck: Remove UB of ch8 usertests #140
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfcyx committed Jan 21, 2024
1 parent a4aebfe commit b89c12c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 39 deletions.
3 changes: 2 additions & 1 deletion user/src/bin/adder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::ptr::addr_of_mut;
use user_lib::{exit, get_time, thread_create, waittid};

static mut A: usize = 0;
Expand All @@ -14,7 +15,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand Down
3 changes: 2 additions & 1 deletion user/src/bin/adder_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::ptr::addr_of_mut;
use core::sync::atomic::{AtomicBool, Ordering};
use user_lib::{exit, get_time, thread_create, waittid, yield_};

Expand All @@ -16,7 +17,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand Down
3 changes: 2 additions & 1 deletion user/src/bin/adder_mutex_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::ptr::addr_of_mut;
use user_lib::{exit, get_time, thread_create, waittid};
use user_lib::{mutex_blocking_create, mutex_lock, mutex_unlock};

Expand All @@ -15,7 +16,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand Down
3 changes: 2 additions & 1 deletion user/src/bin/adder_mutex_spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::ptr::addr_of_mut;
use user_lib::{exit, get_time, thread_create, waittid};
use user_lib::{mutex_create, mutex_lock, mutex_unlock};

Expand All @@ -15,7 +16,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand Down
6 changes: 3 additions & 3 deletions user/src/bin/adder_peterson_spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#![no_std]
#![no_main]
#![feature(core_intrinsics)]

#[macro_use]
extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::ptr::{addr_of, addr_of_mut, read_volatile};
use core::sync::atomic::{compiler_fence, Ordering};
use user_lib::{exit, get_time, thread_create, waittid};

Expand All @@ -20,7 +20,7 @@ const THREAD_COUNT_DEFAULT: usize = 2;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand All @@ -39,7 +39,7 @@ unsafe fn lock(id: usize) {
// Otherwise the compiler will assume that they will never
// be changed on this thread. Thus, they will be accessed
// only once!
while vload!(&FLAG[j]) && vload!(&TURN) == j {}
while read_volatile(addr_of!(FLAG[j])) && read_volatile(addr_of!(TURN)) == j {}
}

unsafe fn unlock(id: usize) {
Expand Down
8 changes: 5 additions & 3 deletions user/src/bin/adder_peterson_yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#![no_std]
#![no_main]
#![feature(core_intrinsics)]

#[macro_use]
extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::sync::atomic::{compiler_fence, Ordering};
use core::{
ptr::addr_of_mut,
sync::atomic::{compiler_fence, Ordering},
};
use user_lib::{exit, get_time, thread_create, waittid, yield_};

static mut A: usize = 0;
Expand All @@ -20,7 +22,7 @@ const THREAD_COUNT_DEFAULT: usize = 2;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand Down
6 changes: 3 additions & 3 deletions user/src/bin/adder_simple_spin.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![no_std]
#![no_main]
#![feature(core_intrinsics)]

#[macro_use]
extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::ptr::{addr_of, addr_of_mut, read_volatile};
use user_lib::{exit, get_time, thread_create, waittid};

static mut A: usize = 0;
Expand All @@ -16,7 +16,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand All @@ -25,7 +25,7 @@ unsafe fn critical_section(t: &mut usize) {
}

unsafe fn lock() {
while vload!(&OCCUPIED) {}
while read_volatile(addr_of!(OCCUPIED)) {}
OCCUPIED = true;
}

Expand Down
4 changes: 2 additions & 2 deletions user/src/bin/adder_simple_yield.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![no_std]
#![no_main]
#![feature(core_intrinsics)]

#[macro_use]
extern crate user_lib;
extern crate alloc;

use alloc::vec::Vec;
use core::ptr::addr_of_mut;
use user_lib::{exit, get_time, thread_create, waittid, yield_};

static mut A: usize = 0;
Expand All @@ -16,7 +16,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
static mut PER_THREAD: usize = 0;

unsafe fn critical_section(t: &mut usize) {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..500 {
*t = (*t) * (*t) % 10007;
Expand Down
30 changes: 16 additions & 14 deletions user/src/bin/eisenberg.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#![no_std]
#![no_main]
#![feature(core_intrinsics)]

#[macro_use]
extern crate user_lib;
extern crate alloc;
extern crate core;

use alloc::vec::Vec;
use core::sync::atomic::{AtomicUsize, Ordering};
use core::{
ptr::{addr_of, addr_of_mut, read_volatile, write_volatile},
sync::atomic::{AtomicUsize, Ordering},
};
use user_lib::{exit, sleep, thread_create, waittid};

const N: usize = 2;
Expand Down Expand Up @@ -38,19 +40,19 @@ fn critical_test_exit() {
assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1);
}

fn eisenberg_enter_critical(id: usize) {
unsafe fn eisenberg_enter_critical(id: usize) {
/* announce that we want to enter */
loop {
println!("Thread[{}] try enter", id);
vstore!(&FLAG[id], FlagState::Want);
write_volatile(addr_of_mut!(FLAG[id]), FlagState::Want);
loop {
/* check if any with higher priority is `Want` or `In` */
let mut prior_thread: Option<usize> = None;
let turn = vload!(&TURN);
let turn = read_volatile(addr_of!(TURN));
let ring_id = if id < turn { id + THREAD_NUM } else { id };
// FLAG.iter() may lead to some errors, use for-loop instead
for i in turn..ring_id {
if vload!(&FLAG[i % THREAD_NUM]) != FlagState::Out {
if read_volatile(addr_of!(FLAG[i % THREAD_NUM])) != FlagState::Out {
prior_thread = Some(i % THREAD_NUM);
break;
}
Expand All @@ -66,13 +68,13 @@ fn eisenberg_enter_critical(id: usize) {
sleep(1);
}
/* now tentatively claim the resource */
vstore!(&FLAG[id], FlagState::In);
write_volatile(addr_of_mut!(FLAG[id]), FlagState::In);
/* enforce the order of `claim` and `conflict check`*/
memory_fence!();
/* check if anthor thread is also `In`, which imply a conflict*/
let mut conflict = false;
for i in 0..THREAD_NUM {
if i != id && vload!(&FLAG[i]) == FlagState::In {
if i != id && read_volatile(addr_of!(FLAG[i])) == FlagState::In {
conflict = true;
}
}
Expand All @@ -83,28 +85,28 @@ fn eisenberg_enter_critical(id: usize) {
/* no need to sleep */
}
/* clain the trun */
vstore!(&TURN, id);
write_volatile(addr_of_mut!(TURN), id);
println!("Thread[{}] enter", id);
}

fn eisenberg_exit_critical(id: usize) {
unsafe fn eisenberg_exit_critical(id: usize) {
/* find next one who wants to enter and give the turn to it*/
let mut next = id;
let ring_id = id + THREAD_NUM;
for i in (id + 1)..ring_id {
let idx = i % THREAD_NUM;
if vload!(&FLAG[idx]) == FlagState::Want {
if read_volatile(addr_of!(FLAG[idx])) == FlagState::Want {
next = idx;
break;
}
}
vstore!(&TURN, next);
write_volatile(addr_of_mut!(TURN), next);
/* All done */
vstore!(&FLAG[id], FlagState::Out);
write_volatile(addr_of_mut!(FLAG[id]), FlagState::Out);
println!("Thread[{}] exit, give turn to {}", id, next);
}

pub fn thread_fn(id: usize) -> ! {
pub unsafe fn thread_fn(id: usize) -> ! {
println!("Thread[{}] init.", id);
for _ in 0..N {
eisenberg_enter_critical(id);
Expand Down
16 changes: 8 additions & 8 deletions user/src/bin/peterson.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![no_std]
#![no_main]
#![feature(core_intrinsics)]

#[macro_use]
extern crate user_lib;
extern crate alloc;
extern crate core;

use alloc::vec::Vec;
use core::ptr::{addr_of, addr_of_mut, read_volatile, write_volatile};
use core::sync::atomic::{AtomicUsize, Ordering};
use user_lib::{exit, sleep, thread_create, waittid};
const N: usize = 1000;
Expand All @@ -28,25 +28,25 @@ fn critical_test_exit() {
assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1);
}

fn peterson_enter_critical(id: usize, peer_id: usize) {
unsafe fn peterson_enter_critical(id: usize, peer_id: usize) {
// println!("Thread[{}] try enter", id);
vstore!(&FLAG[id], true);
vstore!(&TURN, peer_id);
write_volatile(addr_of_mut!(FLAG[id]), true);
write_volatile(addr_of_mut!(TURN), peer_id);
memory_fence!();
while vload!(&FLAG[peer_id]) && vload!(&TURN) == peer_id {
while read_volatile(addr_of!(FLAG[peer_id])) && read_volatile(addr_of!(TURN)) == peer_id {
// println!("Thread[{}] enter fail", id);
sleep(1);
// println!("Thread[{}] retry enter", id);
}
// println!("Thread[{}] enter", id);
}

fn peterson_exit_critical(id: usize) {
vstore!(&FLAG[id], false);
unsafe fn peterson_exit_critical(id: usize) {
write_volatile(addr_of_mut!(FLAG[id]), false);
// println!("Thread[{}] exit", id);
}

pub fn thread_fn(id: usize) -> ! {
pub unsafe fn thread_fn(id: usize) -> ! {
// println!("Thread[{}] init.", id);
let peer_id: usize = id ^ 1;
for iter in 0..N {
Expand Down
3 changes: 2 additions & 1 deletion user/src/bin/race_adder_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate alloc;

use crate::alloc::string::ToString;
use alloc::vec::Vec;
use core::ptr::addr_of_mut;
use user_lib::{exit, get_time, thread_create, waittid};

static mut A: usize = 0;
Expand All @@ -16,7 +17,7 @@ const THREAD_COUNT: usize = 16;
unsafe fn f(count: usize) -> ! {
let mut t = 2usize;
for _ in 0..PER_THREAD {
let a = &mut A as *mut usize;
let a = addr_of_mut!(A);
let cur = a.read_volatile();
for _ in 0..count {
t = t * t % 10007;
Expand Down
1 change: 0 additions & 1 deletion user/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![feature(linkage)]
#![feature(panic_info_message)]
#![feature(alloc_error_handler)]
#![feature(core_intrinsics)]

#[macro_use]
pub mod console;
Expand Down

0 comments on commit b89c12c

Please sign in to comment.