Skip to content

Commit b4ab820

Browse files
committed
epoll test cleanup
1 parent 34aec7c commit b4ab820

File tree

1 file changed

+58
-118
lines changed

1 file changed

+58
-118
lines changed

src/tools/miri/tests/pass-dep/libc/libc-epoll.rs

Lines changed: 58 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ use std::convert::TryInto;
55
use std::mem::MaybeUninit;
66

77
fn main() {
8+
test_epoll_socketpair();
9+
test_epoll_socketpair_both_sides();
10+
test_socketpair_read();
11+
test_epoll_eventfd();
12+
813
test_event_overwrite();
914
test_not_fully_closed_fd();
1015
test_closed_fd();
11-
test_epoll_socketpair_special_case();
1216
test_two_epoll_instance();
1317
test_epoll_ctl_mod();
14-
test_epoll_socketpair();
15-
test_epoll_eventfd();
1618
test_epoll_ctl_del();
1719
test_pointer();
1820
test_two_same_fd_in_same_epoll_instance();
19-
test_socketpair_read();
2021
}
2122

23+
// Using `as` cast since `EPOLLET` wraps around
24+
const EPOLL_IN_OUT_ET: u32 = (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET) as _;
25+
2226
#[track_caller]
2327
fn check_epoll_wait<const N: usize>(
2428
epfd: i32,
@@ -58,21 +62,17 @@ fn test_epoll_socketpair() {
5862

5963
// Create a socketpair instance.
6064
let mut fds = [-1, -1];
61-
let mut res =
62-
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
65+
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
6366
assert_eq!(res, 0);
6467

6568
// Write to fd[0]
6669
let data = "abcde".as_bytes().as_ptr();
67-
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5).try_into().unwrap() };
70+
let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5) };
6871
assert_eq!(res, 5);
6972

70-
// Register fd[1] with EPOLLIN|EPOLLOUT|EPOLLET
71-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
72-
let epollet = libc::EPOLLET as u32;
73-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLRDHUP).unwrap() | epollet;
73+
// Register fd[1] with EPOLLIN|EPOLLOUT|EPOLLET|EPOLLRDHUP
7474
let mut ev = libc::epoll_event {
75-
events: u32::try_from(flags).unwrap(),
75+
events: (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET | libc::EPOLLRDHUP) as _,
7676
u64: u64::try_from(fds[1]).unwrap(),
7777
};
7878
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
@@ -100,23 +100,17 @@ fn test_epoll_ctl_mod() {
100100

101101
// Create a socketpair instance.
102102
let mut fds = [-1, -1];
103-
let mut res =
104-
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
103+
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
105104
assert_eq!(res, 0);
106105

107106
// Write to fd[0].
108107
let data = "abcde".as_bytes().as_ptr();
109-
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5).try_into().unwrap() };
108+
let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5) };
110109
assert_eq!(res, 5);
111110

112111
// Register fd[1] with EPOLLIN|EPOLLOUT|EPOLLET.
113-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
114-
let epollet = libc::EPOLLET as u32;
115-
let mut flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
116-
let mut ev = libc::epoll_event {
117-
events: u32::try_from(flags).unwrap(),
118-
u64: u64::try_from(fds[1]).unwrap(),
119-
};
112+
// (Not using checked cast as EPOLLET wraps around.)
113+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: u64::try_from(fds[1]).unwrap() };
120114
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
121115
assert_ne!(res, -1);
122116

@@ -126,9 +120,8 @@ fn test_epoll_ctl_mod() {
126120
assert!(check_epoll_wait::<8>(epfd, vec![(expected_event, expected_value)]));
127121

128122
// Test EPOLLRDHUP.
129-
flags |= u32::try_from(libc::EPOLLRDHUP).unwrap();
130123
let mut ev = libc::epoll_event {
131-
events: u32::try_from(flags).unwrap(),
124+
events: (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET | libc::EPOLLRDHUP) as _,
132125
u64: u64::try_from(fds[1]).unwrap(),
133126
};
134127
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_MOD, fds[1], &mut ev) };
@@ -151,23 +144,16 @@ fn test_epoll_ctl_del() {
151144

152145
// Create a socketpair instance.
153146
let mut fds = [-1, -1];
154-
let mut res =
155-
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
147+
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
156148
assert_eq!(res, 0);
157149

158150
// Write to fd[0]
159151
let data = "abcde".as_bytes().as_ptr();
160-
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5).try_into().unwrap() };
152+
let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5) };
161153
assert_eq!(res, 5);
162154

163155
// Register fd[1] with EPOLLIN|EPOLLOUT|EPOLLET
164-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
165-
let epollet = libc::EPOLLET as u32;
166-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
167-
let mut ev = libc::epoll_event {
168-
events: u32::try_from(flags).unwrap(),
169-
u64: u64::try_from(fds[1]).unwrap(),
170-
};
156+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: u64::try_from(fds[1]).unwrap() };
171157
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
172158
assert_ne!(res, -1);
173159

@@ -185,22 +171,16 @@ fn test_two_epoll_instance() {
185171

186172
// Create a socketpair instance.
187173
let mut fds = [-1, -1];
188-
let mut res =
189-
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
174+
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
190175
assert_eq!(res, 0);
191176

192177
// Write to the socketpair.
193178
let data = "abcde".as_bytes().as_ptr();
194-
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5).try_into().unwrap() };
179+
let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5) };
195180
assert_eq!(res, 5);
196181

197182
// Register one side of the socketpair with EPOLLIN | EPOLLOUT | EPOLLET.
198-
let epollet = libc::EPOLLET as u32;
199-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
200-
let mut ev = libc::epoll_event {
201-
events: u32::try_from(flags).unwrap(),
202-
u64: u64::try_from(fds[1]).unwrap(),
203-
};
183+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: u64::try_from(fds[1]).unwrap() };
204184
let res = unsafe { libc::epoll_ctl(epfd1, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
205185
assert_ne!(res, -1);
206186
let res = unsafe { libc::epoll_ctl(epfd2, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
@@ -230,17 +210,15 @@ fn test_two_same_fd_in_same_epoll_instance() {
230210
assert_ne!(newfd, -1);
231211

232212
// Register both fd to the same epoll instance.
233-
let epollet = libc::EPOLLET as u32;
234-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
235-
let mut ev = libc::epoll_event { events: u32::try_from(flags).unwrap(), u64: 5 as u64 };
236-
let mut res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
213+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: 5 as u64 };
214+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
237215
assert_ne!(res, -1);
238-
res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, newfd, &mut ev) };
216+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, newfd, &mut ev) };
239217
assert_ne!(res, -1);
240218

241219
// Write to the socketpair.
242220
let data = "abcde".as_bytes().as_ptr();
243-
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5).try_into().unwrap() };
221+
let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5) };
244222
assert_eq!(res, 5);
245223

246224
//Two notification should be received.
@@ -259,23 +237,15 @@ fn test_epoll_eventfd() {
259237

260238
// Write to the eventfd instance.
261239
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
262-
let res: i32 = unsafe {
263-
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
264-
};
240+
let res = unsafe { libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8) };
265241
assert_eq!(res, 8);
266242

267243
// Create an epoll instance.
268244
let epfd = unsafe { libc::epoll_create1(0) };
269245
assert_ne!(epfd, -1);
270246

271247
// Register eventfd with EPOLLIN | EPOLLOUT | EPOLLET
272-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
273-
let epollet = libc::EPOLLET as u32;
274-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
275-
let mut ev = libc::epoll_event {
276-
events: u32::try_from(flags).unwrap(),
277-
u64: u64::try_from(fd).unwrap(),
278-
};
248+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: u64::try_from(fd).unwrap() };
279249
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd, &mut ev) };
280250
assert_ne!(res, -1);
281251

@@ -296,20 +266,15 @@ fn test_pointer() {
296266
assert_eq!(res, 0);
297267

298268
// Register fd[1] with EPOLLIN|EPOLLOUT|EPOLLET
299-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
300-
let epollet = libc::EPOLLET as u32;
301-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLRDHUP).unwrap() | epollet;
302269
let data = MaybeUninit::<u64>::uninit().as_ptr();
303-
let mut ev = libc::epoll_event {
304-
events: u32::try_from(flags).unwrap(),
305-
u64: data.expose_provenance() as u64,
306-
};
270+
let mut ev =
271+
libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: data.expose_provenance() as u64 };
307272
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
308273
assert_ne!(res, -1);
309274
}
310275

311276
// When read/write happened on one side of the socketpair, only the other side will be notified.
312-
fn test_epoll_socketpair_special_case() {
277+
fn test_epoll_socketpair_both_sides() {
313278
// Create an epoll instance.
314279
let epfd = unsafe { libc::epoll_create1(0) };
315280
assert_ne!(epfd, -1);
@@ -320,18 +285,16 @@ fn test_epoll_socketpair_special_case() {
320285
assert_eq!(res, 0);
321286

322287
// Register both fd to the same epoll instance.
323-
let epollet = libc::EPOLLET as u32;
324-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
325-
let mut ev = libc::epoll_event { events: u32::try_from(flags).unwrap(), u64: fds[0] as u64 };
326-
let mut res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[0], &mut ev) };
288+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: fds[0] as u64 };
289+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[0], &mut ev) };
327290
assert_ne!(res, -1);
328-
let mut ev = libc::epoll_event { events: u32::try_from(flags).unwrap(), u64: fds[1] as u64 };
329-
res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
291+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: fds[1] as u64 };
292+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
330293
assert_ne!(res, -1);
331294

332295
// Write to fds[1].
333296
let data = "abcde".as_bytes().as_ptr();
334-
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5).try_into().unwrap() };
297+
let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
335298
assert_eq!(res, 5);
336299

337300
//Two notification should be received.
@@ -346,9 +309,7 @@ fn test_epoll_socketpair_special_case() {
346309

347310
// Read from fds[0].
348311
let mut buf: [u8; 5] = [0; 5];
349-
res = unsafe {
350-
libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
351-
};
312+
let res = unsafe { libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
352313
assert_eq!(res, 5);
353314
assert_eq!(buf, "abcde".as_bytes());
354315

@@ -370,21 +331,13 @@ fn test_closed_fd() {
370331
let fd = unsafe { libc::eventfd(0, flags) };
371332

372333
// Register eventfd with EPOLLIN | EPOLLOUT | EPOLLET
373-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
374-
let epollet = libc::EPOLLET as u32;
375-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
376-
let mut ev = libc::epoll_event {
377-
events: u32::try_from(flags).unwrap(),
378-
u64: u64::try_from(fd).unwrap(),
379-
};
334+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: u64::try_from(fd).unwrap() };
380335
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd, &mut ev) };
381336
assert_ne!(res, -1);
382337

383338
// Write to the eventfd instance.
384339
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
385-
let res: i32 = unsafe {
386-
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
387-
};
340+
let res = unsafe { libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8) };
388341
assert_eq!(res, 8);
389342

390343
// Close the eventfd.
@@ -415,13 +368,7 @@ fn test_not_fully_closed_fd() {
415368
assert_ne!(newfd, -1);
416369

417370
// Register eventfd with EPOLLIN | EPOLLOUT | EPOLLET
418-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
419-
let epollet = libc::EPOLLET as u32;
420-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
421-
let mut ev = libc::epoll_event {
422-
events: u32::try_from(flags).unwrap(),
423-
u64: u64::try_from(fd).unwrap(),
424-
};
371+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: u64::try_from(fd).unwrap() };
425372
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd, &mut ev) };
426373
assert_ne!(res, -1);
427374

@@ -436,9 +383,7 @@ fn test_not_fully_closed_fd() {
436383

437384
// Write to the eventfd instance to produce notification.
438385
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
439-
let res: i32 = unsafe {
440-
libc::write(newfd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
441-
};
386+
let res = unsafe { libc::write(newfd, sized_8_data.as_ptr() as *const libc::c_void, 8) };
442387
assert_eq!(res, 8);
443388

444389
// Close the dupped fd.
@@ -458,29 +403,24 @@ fn test_event_overwrite() {
458403

459404
// Write to the eventfd instance.
460405
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
461-
let res: i32 = unsafe {
462-
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
463-
};
406+
let res = unsafe { libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8) };
464407
assert_eq!(res, 8);
465408

466409
// Create an epoll instance.
467410
let epfd = unsafe { libc::epoll_create1(0) };
468411
assert_ne!(epfd, -1);
469412

470413
// Register eventfd with EPOLLIN | EPOLLOUT | EPOLLET
471-
// EPOLLET is negative number for i32 so casting is needed to do proper bitwise OR for u32.
472-
let epollet = libc::EPOLLET as u32;
473-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
474414
let mut ev = libc::epoll_event {
475-
events: u32::try_from(flags).unwrap(),
415+
events: (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET) as _,
476416
u64: u64::try_from(fd).unwrap(),
477417
};
478418
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd, &mut ev) };
479419
assert_ne!(res, -1);
480420

481421
// Read from the eventfd instance.
482422
let mut buf: [u8; 8] = [0; 8];
483-
let res: i32 = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), 8).try_into().unwrap() };
423+
let res = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), 8) };
484424
assert_eq!(res, 8);
485425

486426
// Check result from epoll_wait.
@@ -502,18 +442,22 @@ fn test_socketpair_read() {
502442
assert_eq!(res, 0);
503443

504444
// Register both fd to the same epoll instance.
505-
let epollet = libc::EPOLLET as u32;
506-
let flags = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap() | epollet;
507-
let mut ev = libc::epoll_event { events: u32::try_from(flags).unwrap(), u64: fds[0] as u64 };
508-
let mut res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[0], &mut ev) };
445+
let mut ev = libc::epoll_event {
446+
events: (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET) as _,
447+
u64: fds[0] as u64,
448+
};
449+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[0], &mut ev) };
509450
assert_ne!(res, -1);
510-
let mut ev = libc::epoll_event { events: u32::try_from(flags).unwrap(), u64: fds[1] as u64 };
511-
res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
451+
let mut ev = libc::epoll_event {
452+
events: (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET) as _,
453+
u64: fds[1] as u64,
454+
};
455+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
512456
assert_ne!(res, -1);
513457

514458
// Write 5 bytes to fds[1].
515459
let data = "abcde".as_bytes().as_ptr();
516-
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5).try_into().unwrap() };
460+
let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
517461
assert_eq!(res, 5);
518462

519463
//Two notification should be received.
@@ -528,9 +472,7 @@ fn test_socketpair_read() {
528472

529473
// Read 3 bytes from fds[0].
530474
let mut buf: [u8; 3] = [0; 3];
531-
res = unsafe {
532-
libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
533-
};
475+
let res = unsafe { libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
534476
assert_eq!(res, 3);
535477
assert_eq!(buf, "abc".as_bytes());
536478

@@ -542,9 +484,7 @@ fn test_socketpair_read() {
542484

543485
// Read until the buffer is empty.
544486
let mut buf: [u8; 2] = [0; 2];
545-
res = unsafe {
546-
libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
547-
};
487+
let res = unsafe { libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
548488
assert_eq!(res, 2);
549489
assert_eq!(buf, "de".as_bytes());
550490

0 commit comments

Comments
 (0)