@@ -10,21 +10,29 @@ fn main() {
10
10
test_race ( ) ;
11
11
}
12
12
13
+ fn read_bytes < const N : usize > ( fd : i32 , buf : & mut [ u8 ; N ] ) -> i32 {
14
+ let res: i32 =
15
+ unsafe { libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , N as libc:: size_t ) . try_into ( ) . unwrap ( ) } ;
16
+ return res;
17
+ }
18
+
19
+ fn write_bytes < const N : usize > ( fd : i32 , data : [ u8 ; N ] ) -> i32 {
20
+ let res: i32 =
21
+ unsafe { libc:: write ( fd, data. as_ptr ( ) as * const libc:: c_void , N ) . try_into ( ) . unwrap ( ) } ;
22
+ return res;
23
+ }
24
+
13
25
fn test_read_write ( ) {
14
26
let flags = libc:: EFD_NONBLOCK | libc:: EFD_CLOEXEC ;
15
27
let fd = unsafe { libc:: eventfd ( 0 , flags) } ;
16
28
let sized_8_data: [ u8 ; 8 ] = 1_u64 . to_ne_bytes ( ) ;
17
29
// Write 1 to the counter.
18
- let res: i64 = unsafe {
19
- libc:: write ( fd, sized_8_data. as_ptr ( ) as * const libc:: c_void , 8 ) . try_into ( ) . unwrap ( )
20
- } ;
30
+ let res = write_bytes ( fd, sized_8_data) ;
21
31
assert_eq ! ( res, 8 ) ;
22
32
23
33
// Read 1 from the counter.
24
34
let mut buf: [ u8 ; 8 ] = [ 0 ; 8 ] ;
25
- let res: i32 = unsafe {
26
- libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
27
- } ;
35
+ let res = read_bytes ( fd, & mut buf) ;
28
36
// Read returns number of bytes has been read, which is always 8.
29
37
assert_eq ! ( res, 8 ) ;
30
38
// Check the value of counter read.
@@ -34,51 +42,40 @@ fn test_read_write() {
34
42
// After read, the counter is currently 0, read counter 0 should fail with return
35
43
// value -1.
36
44
let mut buf: [ u8 ; 8 ] = [ 0 ; 8 ] ;
37
- let res: i32 = unsafe {
38
- libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
39
- } ;
45
+ let res = read_bytes ( fd, & mut buf) ;
40
46
assert_eq ! ( res, -1 ) ;
41
47
42
48
// Write with supplied buffer that > 8 bytes should be allowed.
43
49
let sized_9_data: [ u8 ; 9 ] ;
44
50
if cfg ! ( target_endian = "big" ) {
45
- // Adjust the data based on the endianess of host system.
51
+ // Adjust the data based on the endianness of host system.
46
52
sized_9_data = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ;
47
53
} else {
48
54
sized_9_data = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
49
55
}
50
- let res: i64 = unsafe {
51
- libc:: write ( fd, sized_9_data. as_ptr ( ) as * const libc:: c_void , 8 ) . try_into ( ) . unwrap ( )
52
- } ;
56
+ let res = write_bytes ( fd, sized_9_data) ;
53
57
assert_eq ! ( res, 8 ) ;
54
58
55
59
// Read with supplied buffer that < 8 bytes should fail with return
56
60
// value -1.
57
61
let mut buf: [ u8 ; 7 ] = [ 1 ; 7 ] ;
58
- let res: i32 = unsafe {
59
- libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
60
- } ;
62
+ let res = read_bytes ( fd, & mut buf) ;
61
63
assert_eq ! ( res, -1 ) ;
62
64
63
65
// Write with supplied buffer that < 8 bytes should fail with return
64
66
// value -1.
65
- let res: i64 = unsafe {
66
- libc:: write ( fd, sized_8_data[ 0 ..7 ] . as_ptr ( ) as * const libc:: c_void , 7 ) . try_into ( ) . unwrap ( )
67
- } ;
67
+ let size_7_data: [ u8 ; 7 ] = [ 1 ; 7 ] ;
68
+ let res = write_bytes ( fd, size_7_data) ;
68
69
assert_eq ! ( res, -1 ) ;
69
70
70
71
// Read with supplied buffer > 8 bytes should be allowed.
71
72
let mut buf: [ u8 ; 9 ] = [ 1 ; 9 ] ;
72
- let res: i32 = unsafe {
73
- libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
74
- } ;
73
+ let res = read_bytes ( fd, & mut buf) ;
75
74
assert_eq ! ( res, 8 ) ;
76
75
77
76
// Write u64::MAX should fail.
78
77
let u64_max_bytes: [ u8 ; 8 ] = [ 255 ; 8 ] ;
79
- let res: i64 = unsafe {
80
- libc:: write ( fd, u64_max_bytes. as_ptr ( ) as * const libc:: c_void , 8 ) . try_into ( ) . unwrap ( )
81
- } ;
78
+ let res = write_bytes ( fd, u64_max_bytes) ;
82
79
assert_eq ! ( res, -1 ) ;
83
80
}
84
81
@@ -88,9 +85,7 @@ fn test_race() {
88
85
let fd = unsafe { libc:: eventfd ( 0 , flags) } ;
89
86
let thread1 = thread:: spawn ( move || {
90
87
let mut buf: [ u8 ; 8 ] = [ 0 ; 8 ] ;
91
- let res: i32 = unsafe {
92
- libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
93
- } ;
88
+ let res = read_bytes ( fd, & mut buf) ;
94
89
// read returns number of bytes has been read, which is always 8.
95
90
assert_eq ! ( res, 8 ) ;
96
91
let counter = u64:: from_ne_bytes ( buf) ;
@@ -99,8 +94,7 @@ fn test_race() {
99
94
} ) ;
100
95
unsafe { VAL = 1 } ;
101
96
let data: [ u8 ; 8 ] = 1_u64 . to_ne_bytes ( ) ;
102
- let res: i64 =
103
- unsafe { libc:: write ( fd, data. as_ptr ( ) as * const libc:: c_void , 8 ) . try_into ( ) . unwrap ( ) } ;
97
+ let res = write_bytes ( fd, data) ;
104
98
// write returns number of bytes written, which is always 8.
105
99
assert_eq ! ( res, 8 ) ;
106
100
thread:: yield_now ( ) ;
0 commit comments