Skip to content

Commit 60a885e

Browse files
committed
Add poll and select rules
1 parent d0695ef commit 60a885e

10 files changed

Lines changed: 455 additions & 0 deletions

File tree

rules/poll/ir_unsafe.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"f1": {
3+
"body": [
4+
{
5+
"text": "libc::poll("
6+
},
7+
{
8+
"placeholder": {
9+
"arg": 0,
10+
"access": "read"
11+
}
12+
},
13+
{
14+
"text": ", "
15+
},
16+
{
17+
"placeholder": {
18+
"arg": 1,
19+
"access": "read"
20+
}
21+
},
22+
{
23+
"text": " as ::libc::nfds_t, "
24+
},
25+
{
26+
"placeholder": {
27+
"arg": 2,
28+
"access": "read"
29+
}
30+
},
31+
{
32+
"text": ")"
33+
}
34+
],
35+
"params": {
36+
"a0": {
37+
"type": "*mut ::libc::pollfd",
38+
"is_unsafe_pointer": true
39+
},
40+
"a1": {
41+
"type": "u64"
42+
},
43+
"a2": {
44+
"type": "i32"
45+
}
46+
},
47+
"return_type": {
48+
"type": "i32"
49+
}
50+
}
51+
}

rules/poll/src.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <poll.h>
5+
6+
int f1(struct pollfd *fds, nfds_t nfds, int timeout) {
7+
return poll(fds, nfds, timeout);
8+
}

rules/poll/tgt_unsafe.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
unsafe fn f1(a0: *mut ::libc::pollfd, a1: u64, a2: i32) -> i32 {
5+
libc::poll(a0, a1 as ::libc::nfds_t, a2)
6+
}

rules/select/ir_unsafe.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"f1": {
3+
"body": [
4+
{
5+
"text": "libc::select("
6+
},
7+
{
8+
"placeholder": {
9+
"arg": 0,
10+
"access": "read"
11+
}
12+
},
13+
{
14+
"text": ", "
15+
},
16+
{
17+
"placeholder": {
18+
"arg": 1,
19+
"access": "read"
20+
}
21+
},
22+
{
23+
"text": ", "
24+
},
25+
{
26+
"placeholder": {
27+
"arg": 2,
28+
"access": "read"
29+
}
30+
},
31+
{
32+
"text": ", "
33+
},
34+
{
35+
"placeholder": {
36+
"arg": 3,
37+
"access": "read"
38+
}
39+
},
40+
{
41+
"text": ", "
42+
},
43+
{
44+
"placeholder": {
45+
"arg": 4,
46+
"access": "read"
47+
}
48+
},
49+
{
50+
"text": ")"
51+
}
52+
],
53+
"params": {
54+
"a0": {
55+
"type": "i32"
56+
},
57+
"a1": {
58+
"type": "*mut ::libc::fd_set",
59+
"is_unsafe_pointer": true
60+
},
61+
"a2": {
62+
"type": "*mut ::libc::fd_set",
63+
"is_unsafe_pointer": true
64+
},
65+
"a3": {
66+
"type": "*mut ::libc::fd_set",
67+
"is_unsafe_pointer": true
68+
},
69+
"a4": {
70+
"type": "*mut ::libc::timeval",
71+
"is_unsafe_pointer": true
72+
}
73+
},
74+
"return_type": {
75+
"type": "i32"
76+
}
77+
}
78+
}

rules/select/src.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <sys/select.h>
5+
6+
int f1(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
7+
struct timeval *timeout) {
8+
return select(nfds, readfds, writefds, exceptfds, timeout);
9+
}

rules/select/tgt_unsafe.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
unsafe fn f1(
5+
a0: i32,
6+
a1: *mut ::libc::fd_set,
7+
a2: *mut ::libc::fd_set,
8+
a3: *mut ::libc::fd_set,
9+
a4: *mut ::libc::timeval,
10+
) -> i32 {
11+
libc::select(a0, a1, a2, a3, a4)
12+
}

tests/unit/out/unsafe/poll.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn test_poll_timeout_0() {
10+
let mut fds: [i32; 2] = [0_i32; 2];
11+
assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0));
12+
let mut pfd: pollfd = std::mem::zeroed::<pollfd>();
13+
pfd.fd = fds[(0) as usize];
14+
pfd.events = 1_i16;
15+
pfd.revents = 0_i16;
16+
assert!(
17+
((((libc::poll((&mut pfd as *mut pollfd), 1_u64 as ::libc::nfds_t, 10)) == (0)) as i32)
18+
!= 0)
19+
);
20+
assert!(((((pfd.revents as i32) == (0)) as i32) != 0));
21+
assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0));
22+
assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0));
23+
}
24+
pub unsafe fn test_poll_readable_1() {
25+
let mut fds: [i32; 2] = [0_i32; 2];
26+
assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0));
27+
assert!(
28+
((((libc::write(
29+
fds[(1) as usize],
30+
(b"x\0".as_ptr().cast_mut() as *const u8 as *const ::libc::c_void),
31+
1_u64 as usize
32+
) as i64)
33+
== (1_i64)) as i32)
34+
!= 0)
35+
);
36+
let mut pfd: pollfd = std::mem::zeroed::<pollfd>();
37+
pfd.fd = fds[(0) as usize];
38+
pfd.events = 1_i16;
39+
pfd.revents = 0_i16;
40+
assert!(
41+
((((libc::poll((&mut pfd as *mut pollfd), 1_u64 as ::libc::nfds_t, 100)) == (1)) as i32)
42+
!= 0)
43+
);
44+
assert!((((((pfd.revents as i32) & (1)) != (0)) as i32) != 0));
45+
assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0));
46+
assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0));
47+
}
48+
pub unsafe fn test_poll_multiple_2() {
49+
let mut p1: [i32; 2] = [0_i32; 2];
50+
let mut p2: [i32; 2] = [0_i32; 2];
51+
assert!(((((libc::pipe(p1.as_mut_ptr())) == (0)) as i32) != 0));
52+
assert!(((((libc::pipe(p2.as_mut_ptr())) == (0)) as i32) != 0));
53+
assert!(
54+
((((libc::write(
55+
p2[(1) as usize],
56+
(b"y\0".as_ptr().cast_mut() as *const u8 as *const ::libc::c_void),
57+
1_u64 as usize
58+
) as i64)
59+
== (1_i64)) as i32)
60+
!= 0)
61+
);
62+
let mut pfds: [pollfd; 2] = [std::mem::zeroed::<pollfd>(); 2];
63+
pfds[(0) as usize].fd = p1[(0) as usize];
64+
pfds[(0) as usize].events = 1_i16;
65+
pfds[(0) as usize].revents = 0_i16;
66+
pfds[(1) as usize].fd = p2[(0) as usize];
67+
pfds[(1) as usize].events = 1_i16;
68+
pfds[(1) as usize].revents = 0_i16;
69+
assert!(((((libc::poll(pfds.as_mut_ptr(), 2_u64 as ::libc::nfds_t, 100)) == (1)) as i32) != 0));
70+
assert!(((((pfds[(0) as usize].revents as i32) == (0)) as i32) != 0));
71+
assert!((((((pfds[(1) as usize].revents as i32) & (1)) != (0)) as i32) != 0));
72+
assert!(((((libc::close(p1[(0) as usize])) == (0)) as i32) != 0));
73+
assert!(((((libc::close(p1[(1) as usize])) == (0)) as i32) != 0));
74+
assert!(((((libc::close(p2[(0) as usize])) == (0)) as i32) != 0));
75+
assert!(((((libc::close(p2[(1) as usize])) == (0)) as i32) != 0));
76+
}
77+
pub unsafe fn test_poll_hup_3() {
78+
let mut fds: [i32; 2] = [0_i32; 2];
79+
assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0));
80+
assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0));
81+
let mut pfd: pollfd = std::mem::zeroed::<pollfd>();
82+
pfd.fd = fds[(0) as usize];
83+
pfd.events = 1_i16;
84+
pfd.revents = 0_i16;
85+
assert!(
86+
((((libc::poll((&mut pfd as *mut pollfd), 1_u64 as ::libc::nfds_t, 100)) >= (1)) as i32)
87+
!= 0)
88+
);
89+
assert!((((((pfd.revents as i32) & (16)) != (0)) as i32) != 0));
90+
assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0));
91+
}
92+
pub fn main() {
93+
unsafe {
94+
std::process::exit(main_0() as i32);
95+
}
96+
}
97+
unsafe fn main_0() -> i32 {
98+
(unsafe { test_poll_timeout_0() });
99+
(unsafe { test_poll_readable_1() });
100+
(unsafe { test_poll_multiple_2() });
101+
(unsafe { test_poll_hup_3() });
102+
return 0;
103+
}

tests/unit/out/unsafe/select.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn fd_set_bit_0(mut fd: i32, mut set: *mut fd_set) {
10+
let mut p: *mut u8 = (set as *mut u8);
11+
(*p.offset(((fd) / (8)) as isize)) = (((*p.offset(((fd) / (8)) as isize)) as i32)
12+
| ((((1) << ((fd) % (8))) as u8) as i32)) as u8;
13+
}
14+
pub unsafe fn fd_isset_bit_1(mut fd: i32, mut set: *const fd_set) -> i32 {
15+
let mut p: *const u8 = (set as *const u8);
16+
return ((((*p.offset(((fd) / (8)) as isize)) as i32) >> ((fd) % (8))) & (1));
17+
}
18+
pub unsafe fn test_select_2() {
19+
let mut fds: [i32; 2] = [0_i32; 2];
20+
assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0));
21+
assert!(
22+
((((libc::write(
23+
fds[(1) as usize],
24+
(b"x\0".as_ptr().cast_mut() as *const u8 as *const ::libc::c_void),
25+
1_u64 as usize
26+
) as i64)
27+
== (1_i64)) as i32)
28+
!= 0)
29+
);
30+
let mut rfds: fd_set = std::mem::zeroed::<fd_set>();
31+
{
32+
let byte_0 = ((&mut rfds as *mut fd_set) as *mut fd_set as *mut ::libc::c_void) as *mut u8;
33+
for offset in 0..::std::mem::size_of::<fd_set>() as u64 {
34+
*byte_0.offset(offset as isize) = 0 as u8;
35+
}
36+
((&mut rfds as *mut fd_set) as *mut fd_set as *mut ::libc::c_void)
37+
};
38+
(unsafe {
39+
let _fd: i32 = fds[(0) as usize];
40+
let _set: *mut fd_set = (&mut rfds as *mut fd_set);
41+
fd_set_bit_0(_fd, _set)
42+
});
43+
let mut tv: timeval = std::mem::zeroed::<timeval>();
44+
tv.tv_sec = 0_i64;
45+
tv.tv_usec = 100000_i64;
46+
assert!(
47+
((((libc::select(
48+
((fds[(0) as usize]) + (1)),
49+
(&mut rfds as *mut fd_set),
50+
std::ptr::null_mut(),
51+
std::ptr::null_mut(),
52+
(&mut tv as *mut timeval)
53+
)) == (1)) as i32)
54+
!= 0)
55+
);
56+
assert!(
57+
((unsafe {
58+
let _fd: i32 = fds[(0) as usize];
59+
let _set: *const fd_set = (&mut rfds as *mut fd_set).cast_const();
60+
fd_isset_bit_1(_fd, _set)
61+
}) != 0)
62+
);
63+
assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0));
64+
assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0));
65+
}
66+
pub fn main() {
67+
unsafe {
68+
std::process::exit(main_0() as i32);
69+
}
70+
}
71+
unsafe fn main_0() -> i32 {
72+
(unsafe { test_select_2() });
73+
return 0;
74+
}

0 commit comments

Comments
 (0)