Skip to content

Commit 633139d

Browse files
Write Unix socket addresses in refcount output
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
1 parent e8228f1 commit 633139d

4 files changed

Lines changed: 475 additions & 0 deletions

File tree

‎rules/socket/shim.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ impl Sockaddr {
256256
out.reinterpret_cast::<SockaddrIn6>()
257257
.write(SockaddrIn6::from_libc(&l));
258258
}
259+
Some(AddressFamily::Unix) => {
260+
if let Some(addr) = ss.as_unix_addr() {
261+
let l: &::libc::sockaddr_un = addr.as_ref();
262+
let len = out_len.read().min(ss.len()) as usize;
263+
if len != 0 {
264+
let bytes = (::libc::AF_UNIX as u16)
265+
.to_ne_bytes()
266+
.into_iter()
267+
.chain(l.sun_path.iter().map(|&b| b as u8));
268+
out.reinterpret_cast::<u8>().with_slice_mut(len, |buf| {
269+
for (dst, src) in buf.iter_mut().zip(bytes) {
270+
*dst = src;
271+
}
272+
});
273+
}
274+
}
275+
}
259276
_ => {}
260277
}
261278
out_len.write(ss.len());
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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 check_name_0(mut fd: i32, mut path: Ptr<i8>) {
10+
let full: Value<libcc2rs::SockaddrUn> = Rc::new(RefCell::new(Default::default()));
11+
match ({ ((full.as_pointer()) as Ptr<libcc2rs::SockaddrUn>).to_any() },) {
12+
(mut __tmp_0,) => {
13+
__tmp_0.memset((165) as u8, 110usize as usize);
14+
(__tmp_0).clone()
15+
}
16+
};
17+
let actual: Value<libc::socklen_t> = Rc::new(RefCell::new((110usize as libc::socklen_t)));
18+
assert!(
19+
(((match (
20+
{ (full.as_pointer()).reinterpret_cast::<libcc2rs::Sockaddr>() },
21+
{ (actual.as_pointer()) },
22+
) {
23+
(mut __tmp_1, mut __tmp_2) => {
24+
match nix::sys::socket::getsockname::<nix::sys::socket::SockaddrStorage>(fd) {
25+
Ok(__ss) => {
26+
Sockaddr::encode(&__ss, &__tmp_1, &__tmp_2);
27+
0
28+
}
29+
Err(__e) => {
30+
libcc2rs::cpp2rust_errno().write(__e as i32);
31+
-1
32+
}
33+
}
34+
}
35+
} == 0) as i32)
36+
!= 0)
37+
);
38+
assert!(({ (((({ (*full.borrow()).sun_family } as i32) == 1) as i32) != 0) }));
39+
if !(path).is_null() {
40+
assert!(
41+
(((((array_field_ptr!(full.as_pointer(), sun_path) as Ptr::<i8>) as Ptr<i8>)
42+
.to_any()
43+
.memcmp(&(path).to_any(), path.to_c_string_iterator().count())
44+
== 0) as i32)
45+
!= 0)
46+
);
47+
} else {
48+
assert!(({ (((((*actual.borrow()) as usize) == 2_usize) as i32) != 0) }));
49+
}
50+
println!("{}", ((*actual.borrow()) as u32));
51+
let mut capacities: [libc::socklen_t; 6] = [
52+
(0 as libc::socklen_t),
53+
(1 as libc::socklen_t),
54+
(2 as libc::socklen_t),
55+
(*actual.borrow()).wrapping_sub((1 as libc::socklen_t)),
56+
(*actual.borrow()),
57+
(*actual.borrow()).wrapping_add((1 as libc::socklen_t)),
58+
];
59+
let mut i: u32 = 0_u32;
60+
'loop_: while ((((i as usize)
61+
< (::std::mem::size_of::<[libc::socklen_t; 6]>() as usize)
62+
.wrapping_div((::std::mem::size_of::<libc::socklen_t>() as usize)))
63+
as i32)
64+
!= 0)
65+
{
66+
let out: Value<Box<[u8]>> =
67+
Rc::new(RefCell::new((0..118).map(|_| 0_u8).collect::<Box<[u8]>>()));
68+
match ({ ((out.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any() },) {
69+
(mut __tmp_3,) => {
70+
__tmp_3.memset((165) as u8, ::std::mem::size_of::<[u8; 118]>() as usize);
71+
(__tmp_3).clone()
72+
}
73+
};
74+
let len: Value<libc::socklen_t> = Rc::new(RefCell::new(capacities[(i) as usize]));
75+
assert!(
76+
(((match (
77+
{ (out.as_pointer() as Ptr::<u8>).reinterpret_cast::<libcc2rs::Sockaddr>() },
78+
{ (len.as_pointer()) },
79+
) {
80+
(mut __tmp_4, mut __tmp_5) => {
81+
match nix::sys::socket::getsockname::<nix::sys::socket::SockaddrStorage>(fd) {
82+
Ok(__ss) => {
83+
Sockaddr::encode(&__ss, &__tmp_4, &__tmp_5);
84+
0
85+
}
86+
Err(__e) => {
87+
libcc2rs::cpp2rust_errno().write(__e as i32);
88+
-1
89+
}
90+
}
91+
}
92+
} == 0) as i32)
93+
!= 0)
94+
);
95+
assert!(({ ((((*len.borrow()) == (*actual.borrow())) as i32) != 0) }));
96+
let mut copied: u32 = (if (((capacities[(i) as usize] < (*actual.borrow())) as i32) != 0) {
97+
capacities[(i) as usize]
98+
} else {
99+
(*actual.borrow())
100+
} as u32);
101+
assert!(
102+
(((((out.as_pointer() as Ptr::<u8>) as Ptr<u8>)
103+
.to_any()
104+
.memcmp(
105+
&((full.as_pointer()) as Ptr<libcc2rs::SockaddrUn>).to_any(),
106+
(copied as usize)
107+
)
108+
== 0) as i32)
109+
!= 0)
110+
);
111+
let mut j: u32 = copied;
112+
'loop_: while ((((j as usize) < ::std::mem::size_of::<[u8; 118]>()) as i32) != 0) {
113+
assert!(({ (((((*out.borrow())[(j) as usize] as i32) == 165) as i32) != 0) }));
114+
j.prefix_inc();
115+
}
116+
i.prefix_inc();
117+
}
118+
}
119+
pub fn main() {
120+
__cpp2rust_init_globals();
121+
std::process::exit(main_0());
122+
}
123+
fn main_0() -> i32 {
124+
let mut fd: i32 = {
125+
let __family = match 1 {
126+
::libc::AF_INET => nix::sys::socket::AddressFamily::Inet,
127+
::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6,
128+
::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix,
129+
__d => panic!("socket: unsupported domain {__d}"),
130+
};
131+
let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_DGRAM);
132+
let __ty = match libc::SOCK_DGRAM & !nix::sys::socket::SockFlag::all().bits() {
133+
::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream,
134+
::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram,
135+
__t => panic!("socket: unsupported type {__t}"),
136+
};
137+
let __proto = match 0 {
138+
0 => None,
139+
::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp),
140+
::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp),
141+
__p => panic!("socket: unsupported protocol {__p}"),
142+
};
143+
match nix::sys::socket::socket(__family, __ty, __flags, __proto) {
144+
Ok(__ofd) => FdRegistry::register(__ofd),
145+
Err(__e) => {
146+
libcc2rs::cpp2rust_errno().write(__e as i32);
147+
-1
148+
}
149+
}
150+
};
151+
assert!((((fd >= 0) as i32) != 0));
152+
({ check_name_0(fd, Ptr::<i8>::null()) });
153+
let addr: Value<libcc2rs::SockaddrUn> = Rc::new(RefCell::new(Default::default()));
154+
match ({ ((addr.as_pointer()) as Ptr<libcc2rs::SockaddrUn>).to_any() },) {
155+
(mut __tmp_6,) => {
156+
__tmp_6.memset((0) as u8, 110usize as usize);
157+
(__tmp_6).clone()
158+
}
159+
};
160+
(*addr.borrow_mut()).sun_family = (1 as libc::sa_family_t);
161+
match ({ ((array_field_ptr!(addr.as_pointer(), sun_path) as Ptr<i8>) as Ptr<i8>).to_any() },) {
162+
(mut __tmp_7,) => {
163+
__tmp_7.memcpy(
164+
&Ptr::<i8>::from_string_literal(b"socket-unix-name").to_any(),
165+
::std::mem::size_of::<[i8; 17]>() as usize,
166+
);
167+
(__tmp_7).clone()
168+
}
169+
};
170+
assert!(
171+
(((match Sockaddr::decode(
172+
&(addr.as_pointer()).reinterpret_cast::<libcc2rs::Sockaddr>(),
173+
(((2_usize as usize).wrapping_add(
174+
(array_field_ptr!(addr.as_pointer(), sun_path) as Ptr::<i8>)
175+
.to_c_string_iterator()
176+
.count()
177+
)) as libc::socklen_t)
178+
) {
179+
Some(__addr) => match nix::sys::socket::bind(fd, &*__addr) {
180+
Ok(()) => 0,
181+
Err(__e) => {
182+
libcc2rs::cpp2rust_errno().write(__e as i32);
183+
-1
184+
}
185+
},
186+
None => {
187+
libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT);
188+
-1
189+
}
190+
} == 0) as i32)
191+
!= 0)
192+
);
193+
({
194+
check_name_0(
195+
fd,
196+
(array_field_ptr!(addr.as_pointer(), sun_path) as Ptr<i8>),
197+
)
198+
});
199+
assert!((((FdRegistry::close(fd) == 0) as i32) != 0));
200+
assert!(
201+
(((match nix::unistd::unlink(
202+
(array_field_ptr!(addr.as_pointer(), sun_path) as Ptr::<i8>)
203+
.to_rust_string()
204+
.as_str()
205+
) {
206+
Ok(()) => 0,
207+
Err(__e) => {
208+
libcc2rs::cpp2rust_errno().write(__e as i32);
209+
-1
210+
}
211+
} == 0) as i32)
212+
!= 0)
213+
);
214+
return 0;
215+
}
216+
pub fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)