Skip to content

Commit 46159fd

Browse files
committed
Add fd UB tests
1 parent e05eb20 commit 46159fd

9 files changed

Lines changed: 217 additions & 0 deletions

‎tests/ub/fd_close_invalid.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// panic-ub: refcount
2+
#include <unistd.h>
3+
4+
int main(void) { return close(1234) == -1 ? 0 : 1; }

‎tests/ub/fd_double_close.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// panic-ub: refcount
2+
#include <fcntl.h>
3+
#include <unistd.h>
4+
5+
int main(void) {
6+
int fd = open("/dev/null", O_RDONLY);
7+
close(fd);
8+
return close(fd) == -1 ? 0 : 1;
9+
}

‎tests/ub/fd_use_after_close.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// panic-ub: refcount
2+
#include <fcntl.h>
3+
#include <unistd.h>
4+
5+
int main(void) {
6+
int fd = open("/dev/null", O_RDONLY);
7+
close(fd);
8+
char buf[4];
9+
ssize_t n = read(fd, buf, sizeof(buf));
10+
return n == -1 ? 0 : 1;
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
return if (((FdRegistry::close(1234) == -1_i32) as i32) != 0) {
14+
0
15+
} else {
16+
1
17+
};
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let fd: Value<i32> = Rc::new(RefCell::new({
14+
let __mode = match &[].first() {
15+
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
16+
None => nix::sys::stat::Mode::empty(),
17+
};
18+
match nix::fcntl::open(
19+
Ptr::from_string_literal(b"/dev/null")
20+
.to_rust_string()
21+
.as_str(),
22+
nix::fcntl::OFlag::from_bits_retain(0),
23+
__mode,
24+
) {
25+
Ok(__ofd) => FdRegistry::register(__ofd),
26+
Err(__e) => {
27+
libcc2rs::cpp2rust_errno().write(__e as i32);
28+
-1
29+
}
30+
}
31+
}));
32+
FdRegistry::close((*fd.borrow()));
33+
return if (((FdRegistry::close((*fd.borrow())) == -1_i32) as i32) != 0) {
34+
0
35+
} else {
36+
1
37+
};
38+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let fd: Value<i32> = Rc::new(RefCell::new({
14+
let __mode = match &[].first() {
15+
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
16+
None => nix::sys::stat::Mode::empty(),
17+
};
18+
match nix::fcntl::open(
19+
Ptr::from_string_literal(b"/dev/null")
20+
.to_rust_string()
21+
.as_str(),
22+
nix::fcntl::OFlag::from_bits_retain(0),
23+
__mode,
24+
) {
25+
Ok(__ofd) => FdRegistry::register(__ofd),
26+
Err(__e) => {
27+
libcc2rs::cpp2rust_errno().write(__e as i32);
28+
-1
29+
}
30+
}
31+
}));
32+
FdRegistry::close((*fd.borrow()));
33+
let buf: Value<Box<[u8]>> = Rc::new(RefCell::new(
34+
(0..4).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
35+
));
36+
let n: Value<isize> = Rc::new(RefCell::new(
37+
match FdRegistry::with_fd((*fd.borrow()), |__fd| {
38+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>)
39+
.to_any()
40+
.reinterpret_cast::<u8>()
41+
.with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| {
42+
nix::unistd::read(__fd, __buf)
43+
})
44+
}) {
45+
Ok(__n) => __n as isize,
46+
Err(__e) => {
47+
libcc2rs::cpp2rust_errno().write(__e as i32);
48+
-1
49+
}
50+
},
51+
));
52+
return if ((((*n.borrow()) == (-1_i32 as isize)) as i32) != 0) {
53+
0
54+
} else {
55+
1
56+
};
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
return if ((((libc::close(1234)) == (-1_i32)) as i32) != 0) {
16+
0
17+
} else {
18+
1
19+
};
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut fd: i32 = (unsafe {
16+
libc::open(
17+
(c"/dev/null".as_ptr().cast_mut()).cast_const() as *const i8,
18+
0 as i32,
19+
)
20+
});
21+
libc::close(fd);
22+
return if ((((libc::close(fd)) == (-1_i32)) as i32) != 0) {
23+
0
24+
} else {
25+
1
26+
};
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut fd: i32 = (unsafe {
16+
libc::open(
17+
(c"/dev/null".as_ptr().cast_mut()).cast_const() as *const i8,
18+
0 as i32,
19+
)
20+
});
21+
libc::close(fd);
22+
let mut buf: [libc::c_char; 4] = [(0 as libc::c_char); 4];
23+
let mut n: isize = libc::read(
24+
fd,
25+
(buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void),
26+
::std::mem::size_of::<[libc::c_char; 4]>(),
27+
);
28+
return if ((((n) == (-1_i32 as isize)) as i32) != 0) {
29+
0
30+
} else {
31+
1
32+
};
33+
}

0 commit comments

Comments
 (0)