Skip to content

Commit 43be4ec

Browse files
committed
Add send/recv safe rules
1 parent e585f96 commit 43be4ec

5 files changed

Lines changed: 587 additions & 0 deletions

File tree

‎libcc2rs/src/rc.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,64 @@ thread_local! {
950950
}
951951

952952
impl Ptr<u8> {
953+
pub fn with_slice_mut<R>(&self, len: usize, f: impl FnOnce(&mut [u8]) -> R) -> R {
954+
let off = self.offset;
955+
match &self.kind {
956+
PtrKind::Null => panic!("ub: null pointer"),
957+
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
958+
assert!(off == 0 && len <= 1, "ub: with_slice_mut out of bounds");
959+
let rc = weak.upgrade().expect("ub: dangling pointer");
960+
let mut b = rc.borrow_mut();
961+
f(&mut std::slice::from_mut(&mut *b)[..len])
962+
}
963+
PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => {
964+
let rc = weak.upgrade().expect("ub: dangling pointer");
965+
let mut b = rc.borrow_mut();
966+
f(&mut b[off..off + len])
967+
}
968+
PtrKind::Vec(weak) => {
969+
let rc = weak.upgrade().expect("ub: dangling pointer");
970+
let mut b = rc.borrow_mut();
971+
f(&mut b[off..off + len])
972+
}
973+
PtrKind::Reinterpreted(data) => {
974+
let mut buf = vec![0u8; len];
975+
data.alloc.read_bytes(off, &mut buf);
976+
let r = f(&mut buf);
977+
data.alloc.write_bytes(off, &buf);
978+
r
979+
}
980+
}
981+
}
982+
983+
pub fn with_slice<R>(&self, len: usize, f: impl FnOnce(&[u8]) -> R) -> R {
984+
let off = self.offset;
985+
match &self.kind {
986+
PtrKind::Null => panic!("ub: null pointer"),
987+
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
988+
assert!(off == 0 && len <= 1, "ub: with_slice out of bounds");
989+
let rc = weak.upgrade().expect("ub: dangling pointer");
990+
let b = rc.borrow();
991+
f(&std::slice::from_ref(&*b)[..len])
992+
}
993+
PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => {
994+
let rc = weak.upgrade().expect("ub: dangling pointer");
995+
let b = rc.borrow();
996+
f(&b[off..off + len])
997+
}
998+
PtrKind::Vec(weak) => {
999+
let rc = weak.upgrade().expect("ub: dangling pointer");
1000+
let b = rc.borrow();
1001+
f(&b[off..off + len])
1002+
}
1003+
PtrKind::Reinterpreted(data) => {
1004+
let mut buf = vec![0u8; len];
1005+
data.alloc.read_bytes(off, &mut buf);
1006+
f(&buf)
1007+
}
1008+
}
1009+
}
1010+
9531011
#[allow(clippy::explicit_counter_loop)]
9541012
pub fn memcpy(&self, src: &Self, len: usize) {
9551013
let mut dst = self.clone();

‎rules/socket/tgt_refcount.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,61 @@ fn t2() -> libcc2rs::SockaddrStorage {
1414
fn t3() -> libcc2rs::SockaddrUn {
1515
Default::default()
1616
}
17+
18+
fn f11(a0: i32, a1: i32, a2: i32, a3: Ptr<i32>) -> i32 {
19+
use nix::sys::socket::{AddressFamily, SockFlag, SockProtocol, SockType};
20+
let __out = a3.clone();
21+
let __flag_bits = ::libc::SOCK_CLOEXEC | ::libc::SOCK_NONBLOCK;
22+
match (
23+
AddressFamily::from_i32(a0),
24+
SockType::try_from(a1 & !__flag_bits),
25+
) {
26+
(Some(__f), Ok(__t)) => {
27+
let __flags = SockFlag::from_bits_truncate(a1 & __flag_bits);
28+
let __proto: Option<SockProtocol> = None;
29+
match nix::sys::socket::socketpair(__f, __t, __proto, __flags) {
30+
Ok((__a, __b)) => {
31+
__out.write(::std::os::fd::IntoRawFd::into_raw_fd(__a));
32+
__out
33+
.offset(1)
34+
.write(::std::os::fd::IntoRawFd::into_raw_fd(__b));
35+
0
36+
}
37+
Err(__e) => {
38+
libcc2rs::cpp2rust_errno().write(__e as i32);
39+
-1
40+
}
41+
}
42+
}
43+
_ => {
44+
libcc2rs::cpp2rust_errno().write(::libc::EINVAL);
45+
-1
46+
}
47+
}
48+
}
49+
50+
fn f9(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize {
51+
let __buf = a1.reinterpret_cast::<u8>();
52+
match __buf.with_slice_mut(a2, |__s| {
53+
nix::sys::socket::recv(a0, __s, nix::sys::socket::MsgFlags::from_bits_truncate(a3))
54+
}) {
55+
Ok(__n) => __n as isize,
56+
Err(__e) => {
57+
libcc2rs::cpp2rust_errno().write(__e as i32);
58+
-1
59+
}
60+
}
61+
}
62+
63+
fn f10(a0: i32, a1: AnyPtr, a2: usize, a3: i32) -> isize {
64+
let __buf = a1.reinterpret_cast::<u8>();
65+
match __buf.with_slice(a2, |__s| {
66+
nix::sys::socket::send(a0, __s, nix::sys::socket::MsgFlags::from_bits_truncate(a3))
67+
}) {
68+
Ok(__n) => __n as isize,
69+
Err(__e) => {
70+
libcc2rs::cpp2rust_errno().write(__e as i32);
71+
-1
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)