Skip to content

Commit 17a0ee7

Browse files
committed
codecheck: Remove UB of ch8 usertests #140
1 parent 1aac88e commit 17a0ee7

File tree

10 files changed

+42
-37
lines changed

10 files changed

+42
-37
lines changed

user/src/bin/adder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate user_lib;
66
extern crate alloc;
77

88
use alloc::vec::Vec;
9+
use core::ptr::addr_of_mut;
910
use user_lib::{exit, get_time, thread_create, waittid};
1011

1112
static mut A: usize = 0;
@@ -14,7 +15,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
1415
static mut PER_THREAD: usize = 0;
1516

1617
unsafe fn critical_section(t: &mut usize) {
17-
let a = &mut A as *mut usize;
18+
let a = addr_of_mut!(A);
1819
let cur = a.read_volatile();
1920
for _ in 0..500 {
2021
*t = (*t) * (*t) % 10007;

user/src/bin/adder_atomic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern crate alloc;
77

88
use alloc::vec::Vec;
99
use core::sync::atomic::{AtomicBool, Ordering};
10+
use core::ptr::addr_of_mut;
1011
use user_lib::{exit, get_time, thread_create, waittid, yield_};
1112

1213
static mut A: usize = 0;
@@ -16,7 +17,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
1617
static mut PER_THREAD: usize = 0;
1718

1819
unsafe fn critical_section(t: &mut usize) {
19-
let a = &mut A as *mut usize;
20+
let a = addr_of_mut!(A);
2021
let cur = a.read_volatile();
2122
for _ in 0..500 {
2223
*t = (*t) * (*t) % 10007;

user/src/bin/adder_mutex_blocking.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate user_lib;
66
extern crate alloc;
77

88
use alloc::vec::Vec;
9+
use core::ptr::addr_of_mut;
910
use user_lib::{exit, get_time, thread_create, waittid};
1011
use user_lib::{mutex_blocking_create, mutex_lock, mutex_unlock};
1112

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

1718
unsafe fn critical_section(t: &mut usize) {
18-
let a = &mut A as *mut usize;
19+
let a = addr_of_mut!(A);
1920
let cur = a.read_volatile();
2021
for _ in 0..500 {
2122
*t = (*t) * (*t) % 10007;

user/src/bin/adder_mutex_spin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate user_lib;
66
extern crate alloc;
77

88
use alloc::vec::Vec;
9+
use core::ptr::addr_of_mut;
910
use user_lib::{exit, get_time, thread_create, waittid};
1011
use user_lib::{mutex_create, mutex_lock, mutex_unlock};
1112

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

1718
unsafe fn critical_section(t: &mut usize) {
18-
let a = &mut A as *mut usize;
19+
let a = addr_of_mut!(A);
1920
let cur = a.read_volatile();
2021
for _ in 0..500 {
2122
*t = (*t) * (*t) % 10007;

user/src/bin/adder_peterson_spin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
33
#![no_std]
44
#![no_main]
5-
#![feature(core_intrinsics)]
65

76
#[macro_use]
87
extern crate user_lib;
98
extern crate alloc;
109

1110
use alloc::vec::Vec;
11+
use core::ptr::{addr_of, addr_of_mut, read_volatile};
1212
use core::sync::atomic::{compiler_fence, Ordering};
1313
use user_lib::{exit, get_time, thread_create, waittid};
1414

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

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

4545
unsafe fn unlock(id: usize) {

user/src/bin/adder_peterson_yield.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
33
#![no_std]
44
#![no_main]
5-
#![feature(core_intrinsics)]
65

76
#[macro_use]
87
extern crate user_lib;
98
extern crate alloc;
109

1110
use alloc::vec::Vec;
12-
use core::sync::atomic::{compiler_fence, Ordering};
11+
use core::{
12+
ptr::addr_of_mut,
13+
sync::atomic::{compiler_fence, Ordering},
14+
};
1315
use user_lib::{exit, get_time, thread_create, waittid, yield_};
1416

1517
static mut A: usize = 0;
@@ -20,7 +22,7 @@ const THREAD_COUNT_DEFAULT: usize = 2;
2022
static mut PER_THREAD: usize = 0;
2123

2224
unsafe fn critical_section(t: &mut usize) {
23-
let a = &mut A as *mut usize;
25+
let a = addr_of_mut!(A);
2426
let cur = a.read_volatile();
2527
for _ in 0..500 {
2628
*t = (*t) * (*t) % 10007;

user/src/bin/adder_simple_spin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#![no_std]
22
#![no_main]
3-
#![feature(core_intrinsics)]
43

54
#[macro_use]
65
extern crate user_lib;
76
extern crate alloc;
87

98
use alloc::vec::Vec;
9+
use core::ptr::{addr_of, addr_of_mut, read_volatile};
1010
use user_lib::{exit, get_time, thread_create, waittid};
1111

1212
static mut A: usize = 0;
@@ -16,7 +16,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
1616
static mut PER_THREAD: usize = 0;
1717

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

2727
unsafe fn lock() {
28-
while vload!(&OCCUPIED) {}
28+
while read_volatile(addr_of!(OCCUPIED)) {}
2929
OCCUPIED = true;
3030
}
3131

user/src/bin/adder_simple_yield.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#![no_std]
22
#![no_main]
3-
#![feature(core_intrinsics)]
43

54
#[macro_use]
65
extern crate user_lib;
76
extern crate alloc;
87

98
use alloc::vec::Vec;
9+
use core::ptr::addr_of_mut;
1010
use user_lib::{exit, get_time, thread_create, waittid, yield_};
1111

1212
static mut A: usize = 0;
@@ -16,7 +16,7 @@ const THREAD_COUNT_DEFAULT: usize = 16;
1616
static mut PER_THREAD: usize = 0;
1717

1818
unsafe fn critical_section(t: &mut usize) {
19-
let a = &mut A as *mut usize;
19+
let a = addr_of_mut!(A);
2020
let cur = a.read_volatile();
2121
for _ in 0..500 {
2222
*t = (*t) * (*t) % 10007;

user/src/bin/eisenberg.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#![no_std]
22
#![no_main]
3-
#![feature(core_intrinsics)]
43

54
#[macro_use]
65
extern crate user_lib;
76
extern crate alloc;
87
extern crate core;
98

109
use alloc::vec::Vec;
11-
use core::sync::atomic::{AtomicUsize, Ordering};
10+
use core::{ptr::{addr_of, addr_of_mut, read_volatile, write_volatile}, sync::atomic::{AtomicUsize, Ordering}};
1211
use user_lib::{exit, sleep, thread_create, waittid};
1312

1413
const N: usize = 2;
@@ -38,19 +37,19 @@ fn critical_test_exit() {
3837
assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1);
3938
}
4039

41-
fn eisenberg_enter_critical(id: usize) {
40+
unsafe fn eisenberg_enter_critical(id: usize) {
4241
/* announce that we want to enter */
4342
loop {
4443
println!("Thread[{}] try enter", id);
45-
vstore!(&FLAG[id], FlagState::Want);
44+
write_volatile(addr_of_mut!(FLAG[id]), FlagState::Want);
4645
loop {
4746
/* check if any with higher priority is `Want` or `In` */
4847
let mut prior_thread: Option<usize> = None;
49-
let turn = vload!(&TURN);
48+
let turn = read_volatile(addr_of!(TURN));
5049
let ring_id = if id < turn { id + THREAD_NUM } else { id };
5150
// FLAG.iter() may lead to some errors, use for-loop instead
5251
for i in turn..ring_id {
53-
if vload!(&FLAG[i % THREAD_NUM]) != FlagState::Out {
52+
if read_volatile(addr_of!(FLAG[i % THREAD_NUM])) != FlagState::Out {
5453
prior_thread = Some(i % THREAD_NUM);
5554
break;
5655
}
@@ -66,13 +65,13 @@ fn eisenberg_enter_critical(id: usize) {
6665
sleep(1);
6766
}
6867
/* now tentatively claim the resource */
69-
vstore!(&FLAG[id], FlagState::In);
68+
write_volatile(addr_of_mut!(FLAG[id]), FlagState::In);
7069
/* enforce the order of `claim` and `conflict check`*/
7170
memory_fence!();
7271
/* check if anthor thread is also `In`, which imply a conflict*/
7372
let mut conflict = false;
7473
for i in 0..THREAD_NUM {
75-
if i != id && vload!(&FLAG[i]) == FlagState::In {
74+
if i != id && read_volatile(addr_of!(FLAG[i])) == FlagState::In {
7675
conflict = true;
7776
}
7877
}
@@ -83,28 +82,28 @@ fn eisenberg_enter_critical(id: usize) {
8382
/* no need to sleep */
8483
}
8584
/* clain the trun */
86-
vstore!(&TURN, id);
85+
write_volatile(addr_of_mut!(TURN), id);
8786
println!("Thread[{}] enter", id);
8887
}
8988

90-
fn eisenberg_exit_critical(id: usize) {
89+
unsafe fn eisenberg_exit_critical(id: usize) {
9190
/* find next one who wants to enter and give the turn to it*/
9291
let mut next = id;
9392
let ring_id = id + THREAD_NUM;
9493
for i in (id + 1)..ring_id {
9594
let idx = i % THREAD_NUM;
96-
if vload!(&FLAG[idx]) == FlagState::Want {
95+
if read_volatile(addr_of!(FLAG[idx])) == FlagState::Want {
9796
next = idx;
9897
break;
9998
}
10099
}
101-
vstore!(&TURN, next);
100+
write_volatile(addr_of_mut!(TURN), next);
102101
/* All done */
103-
vstore!(&FLAG[id], FlagState::Out);
102+
write_volatile(addr_of_mut!(FLAG[id]), FlagState::Out);
104103
println!("Thread[{}] exit, give turn to {}", id, next);
105104
}
106105

107-
pub fn thread_fn(id: usize) -> ! {
106+
pub unsafe fn thread_fn(id: usize) -> ! {
108107
println!("Thread[{}] init.", id);
109108
for _ in 0..N {
110109
eisenberg_enter_critical(id);

user/src/bin/peterson.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#![no_std]
22
#![no_main]
3-
#![feature(core_intrinsics)]
43

54
#[macro_use]
65
extern crate user_lib;
76
extern crate alloc;
87
extern crate core;
98

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

31-
fn peterson_enter_critical(id: usize, peer_id: usize) {
31+
unsafe fn peterson_enter_critical(id: usize, peer_id: usize) {
3232
// println!("Thread[{}] try enter", id);
33-
vstore!(&FLAG[id], true);
34-
vstore!(&TURN, peer_id);
33+
write_volatile(addr_of_mut!(FLAG[id]), true);
34+
write_volatile(addr_of_mut!(TURN), peer_id);
3535
memory_fence!();
36-
while vload!(&FLAG[peer_id]) && vload!(&TURN) == peer_id {
36+
while read_volatile(addr_of!(FLAG[peer_id])) && read_volatile(addr_of!(TURN)) == peer_id {
3737
// println!("Thread[{}] enter fail", id);
3838
sleep(1);
3939
// println!("Thread[{}] retry enter", id);
4040
}
4141
// println!("Thread[{}] enter", id);
4242
}
4343

44-
fn peterson_exit_critical(id: usize) {
45-
vstore!(&FLAG[id], false);
44+
unsafe fn peterson_exit_critical(id: usize) {
45+
write_volatile(addr_of_mut!(FLAG[id]), false);
4646
// println!("Thread[{}] exit", id);
4747
}
4848

49-
pub fn thread_fn(id: usize) -> ! {
49+
pub unsafe fn thread_fn(id: usize) -> ! {
5050
// println!("Thread[{}] init.", id);
5151
let peer_id: usize = id ^ 1;
5252
for iter in 0..N {

0 commit comments

Comments
 (0)