Skip to content

Commit 3517587

Browse files
Write Unix socket addresses in refcount getsockname (#584)
## Summary Refcount `getsockname` now writes the family and address bytes for Unix sockets instead of returning success with an unchanged output buffer. The Unix branch of the shared encoder copies no more than the caller's capacity or the actual address length, leaves the suffix untouched, and reports the full length. `getpeername` and `recvfrom` inherit the same Unix writeback. [tests/unit/socket_unix_name.c](tests/unit/socket_unix_name.c) checks unnamed and pathname-bound sockets, shortened buffers, returned lengths and protected suffixes. It uses `sockaddr_storage` buffers and the platform's returned length rather than assuming an unnamed length or a `sockaddr_un` size. The new refcount fixture fails against the unchanged runtime at the missing-family assertion. Addresses that nix cannot represent as `UnixAddr` retain the existing fallback. IP output length and capacity handling, and Unix input decoding, are unchanged. Native-byte comparison is Linux-scoped. ## Testing The lit runner reports 1,058 passed, 14 expected failures, and 0 failures in `tests/unit` for each Linux Release and Debug build; the Release runs report 76 passed in `tests/ub` and 20 passed in `tests/multi-file`, with no failures or skips. On the base revision with only the new test files added, `tests/unit` reports 1,057 passed, 14 expected failures, and 1 failure: `socket_unix_name.c/refcount`, at the missing-family assertion. `check-libcc2rs` passes 70 unit tests, with 0 failures or ignored tests and 0 doctests. `check-libcc2rs-macros` passes 1 compile-fail test and 22 control-flow tests, with 0 failures or ignored tests and 0 doctests. `ninja format` completed without changing any files. macOS validation remains target-CI-owned. [Case and evidence](<https://app.perfloop.ai/t/oss/case_kpsk46099k>) --- Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás Senart](https://github.com/tsenart). To ask for a change, comment or review in plain words. [How Perfloop handles pull requests](https://perfloop.ai/docs/work/pull-requests). --------- Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
1 parent e8228f1 commit 3517587

4 files changed

Lines changed: 506 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: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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::SockaddrStorage> = Rc::new(RefCell::new(Default::default()));
11+
match ({ ((full.as_pointer()) as Ptr<libcc2rs::SockaddrStorage>).to_any() },) {
12+
(mut __tmp_0,) => {
13+
__tmp_0.memset((165) as u8, 128usize as usize);
14+
(__tmp_0).clone()
15+
}
16+
};
17+
let actual: Value<libc::socklen_t> = Rc::new(RefCell::new((128usize 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()).ss_family } as i32) == 1) as i32) != 0) }));
39+
assert!(({ (((((*actual.borrow()) as usize) >= 2_usize) as i32) != 0) }));
40+
assert!(({ (((((*actual.borrow()) as usize) < 128usize) as i32) != 0) }));
41+
if !(path).is_null() {
42+
assert!(
43+
(((((array_field_ptr!(
44+
((full.as_pointer()).reinterpret_cast::<libcc2rs::SockaddrUn>()),
45+
sun_path
46+
) as Ptr::<i8>) as Ptr<i8>)
47+
.to_any()
48+
.memcmp(&(path).to_any(), path.to_c_string_iterator().count())
49+
== 0) as i32)
50+
!= 0)
51+
);
52+
}
53+
let mut capacities: [libc::socklen_t; 6] = [
54+
(0 as libc::socklen_t),
55+
(1 as libc::socklen_t),
56+
(2 as libc::socklen_t),
57+
(*actual.borrow()).wrapping_sub((1 as libc::socklen_t)),
58+
(*actual.borrow()),
59+
(*actual.borrow()).wrapping_add((1 as libc::socklen_t)),
60+
];
61+
let mut i: u32 = 0_u32;
62+
'loop_: while ((((i as usize)
63+
< (::std::mem::size_of::<[libc::socklen_t; 6]>() as usize)
64+
.wrapping_div((::std::mem::size_of::<libc::socklen_t>() as usize)))
65+
as i32)
66+
!= 0)
67+
{
68+
let out: Value<Box<[u8]>> =
69+
Rc::new(RefCell::new((0..136).map(|_| 0_u8).collect::<Box<[u8]>>()));
70+
match ({ ((out.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any() },) {
71+
(mut __tmp_3,) => {
72+
__tmp_3.memset((165) as u8, ::std::mem::size_of::<[u8; 136]>() as usize);
73+
(__tmp_3).clone()
74+
}
75+
};
76+
let len: Value<libc::socklen_t> = Rc::new(RefCell::new(capacities[(i) as usize]));
77+
assert!(
78+
(((match (
79+
{ (out.as_pointer() as Ptr::<u8>).reinterpret_cast::<libcc2rs::Sockaddr>() },
80+
{ (len.as_pointer()) },
81+
) {
82+
(mut __tmp_4, mut __tmp_5) => {
83+
match nix::sys::socket::getsockname::<nix::sys::socket::SockaddrStorage>(fd) {
84+
Ok(__ss) => {
85+
Sockaddr::encode(&__ss, &__tmp_4, &__tmp_5);
86+
0
87+
}
88+
Err(__e) => {
89+
libcc2rs::cpp2rust_errno().write(__e as i32);
90+
-1
91+
}
92+
}
93+
}
94+
} == 0) as i32)
95+
!= 0)
96+
);
97+
assert!(({ ((((*len.borrow()) == (*actual.borrow())) as i32) != 0) }));
98+
let mut copied: u32 = (if (((capacities[(i) as usize] < (*actual.borrow())) as i32) != 0) {
99+
capacities[(i) as usize]
100+
} else {
101+
(*actual.borrow())
102+
} as u32);
103+
assert!(
104+
(((((out.as_pointer() as Ptr::<u8>) as Ptr<u8>)
105+
.to_any()
106+
.memcmp(
107+
&((full.as_pointer()) as Ptr<libcc2rs::SockaddrStorage>).to_any(),
108+
(copied as usize)
109+
)
110+
== 0) as i32)
111+
!= 0)
112+
);
113+
let mut j: u32 = copied;
114+
'loop_: while ((((j as usize) < ::std::mem::size_of::<[u8; 136]>()) as i32) != 0) {
115+
assert!(({ (((((*out.borrow())[(j) as usize] as i32) == 165) as i32) != 0) }));
116+
j.prefix_inc();
117+
}
118+
i.prefix_inc();
119+
}
120+
}
121+
pub fn main() {
122+
__cpp2rust_init_globals();
123+
std::process::exit(main_0());
124+
}
125+
fn main_0() -> i32 {
126+
let mut fd: i32 = {
127+
let __family = match 1 {
128+
::libc::AF_INET => nix::sys::socket::AddressFamily::Inet,
129+
::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6,
130+
::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix,
131+
__d => panic!("socket: unsupported domain {__d}"),
132+
};
133+
let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_DGRAM);
134+
let __ty = match libc::SOCK_DGRAM & !nix::sys::socket::SockFlag::all().bits() {
135+
::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream,
136+
::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram,
137+
__t => panic!("socket: unsupported type {__t}"),
138+
};
139+
let __proto = match 0 {
140+
0 => None,
141+
::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp),
142+
::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp),
143+
__p => panic!("socket: unsupported protocol {__p}"),
144+
};
145+
match nix::sys::socket::socket(__family, __ty, __flags, __proto) {
146+
Ok(__ofd) => FdRegistry::register(__ofd),
147+
Err(__e) => {
148+
libcc2rs::cpp2rust_errno().write(__e as i32);
149+
-1
150+
}
151+
}
152+
};
153+
assert!((((fd >= 0) as i32) != 0));
154+
({ check_name_0(fd, Ptr::<i8>::null()) });
155+
let addr: Value<libcc2rs::SockaddrStorage> = Rc::new(RefCell::new(Default::default()));
156+
match ({ ((addr.as_pointer()) as Ptr<libcc2rs::SockaddrStorage>).to_any() },) {
157+
(mut __tmp_6,) => {
158+
__tmp_6.memset((0) as u8, 128usize as usize);
159+
(__tmp_6).clone()
160+
}
161+
};
162+
let len: Value<libc::socklen_t> = Rc::new(RefCell::new((128usize as libc::socklen_t)));
163+
assert!(
164+
(((match (
165+
{ (addr.as_pointer()).reinterpret_cast::<libcc2rs::Sockaddr>() },
166+
{ (len.as_pointer()) },
167+
) {
168+
(mut __tmp_7, mut __tmp_8) => {
169+
match nix::sys::socket::getsockname::<nix::sys::socket::SockaddrStorage>(fd) {
170+
Ok(__ss) => {
171+
Sockaddr::encode(&__ss, &__tmp_7, &__tmp_8);
172+
0
173+
}
174+
Err(__e) => {
175+
libcc2rs::cpp2rust_errno().write(__e as i32);
176+
-1
177+
}
178+
}
179+
}
180+
} == 0) as i32)
181+
!= 0)
182+
);
183+
let mut un: Ptr<libcc2rs::SockaddrUn> =
184+
(addr.as_pointer()).reinterpret_cast::<libcc2rs::SockaddrUn>();
185+
match ({ ((array_field_ptr!(un, sun_path) as Ptr<i8>) as Ptr<i8>).to_any() },) {
186+
(mut __tmp_9,) => {
187+
__tmp_9.memcpy(
188+
&Ptr::<i8>::from_string_literal(b"socket-unix-name").to_any(),
189+
::std::mem::size_of::<[i8; 17]>() as usize,
190+
);
191+
(__tmp_9).clone()
192+
}
193+
};
194+
assert!(
195+
(((match Sockaddr::decode(
196+
&(addr.as_pointer()).reinterpret_cast::<libcc2rs::Sockaddr>(),
197+
(((2_usize as usize).wrapping_add(
198+
(array_field_ptr!(un, sun_path) as Ptr::<i8>)
199+
.to_c_string_iterator()
200+
.count()
201+
)) as libc::socklen_t)
202+
) {
203+
Some(__addr) => match nix::sys::socket::bind(fd, &*__addr) {
204+
Ok(()) => 0,
205+
Err(__e) => {
206+
libcc2rs::cpp2rust_errno().write(__e as i32);
207+
-1
208+
}
209+
},
210+
None => {
211+
libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT);
212+
-1
213+
}
214+
} == 0) as i32)
215+
!= 0)
216+
);
217+
({ check_name_0(fd, (array_field_ptr!(un, sun_path) as Ptr<i8>)) });
218+
assert!((((FdRegistry::close(fd) == 0) as i32) != 0));
219+
assert!(
220+
(((match nix::unistd::unlink(
221+
(array_field_ptr!(un, sun_path) as Ptr::<i8>)
222+
.to_rust_string()
223+
.as_str()
224+
) {
225+
Ok(()) => 0,
226+
Err(__e) => {
227+
libcc2rs::cpp2rust_errno().write(__e as i32);
228+
-1
229+
}
230+
} == 0) as i32)
231+
!= 0)
232+
);
233+
return 0;
234+
}
235+
pub fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)