getsockname reports storage capacity instead of the IP address length

perfloop-oss/cpp2rust · Extent mismatch

https://perfloop.ai/t/oss/case_24szn3058q

Verdict

VERIFIED · settled 2026-11-20 · merged as Cpp2Rust/cpp2rust#666

What happened: The check fails before this change and passes with it.

Hypothesis

Successful refcount getsockname calls can report an address length larger than the address they wrote. On Linux this can tell a caller that a complete IPv4 address needs 128 bytes instead of 16. Consumer exposure and downstream effects remain unmeasured.

The ordinary f12 rule calls nix::getsockname::<SockaddrStorage> and sends successful results to Sockaddr::encode. Its final statement writes ss.len(). In the resolved nix 0.30.1 implementation on Linux, SockaddrStorage::len returns the container size for non-Unix families. The encoder's concrete IP branches write 16-byte or 28-byte shim values. This claim concerns successful Linux IP writeback, not early syscall errors or macOS behavior.

checkProperty(prop_h2rz6tveme) at e2b679de9fa36c3b015b4604759ab0ff53003fa4 ran cargo test --manifest-path libcc2rs/Cargo.toml --test perfloop_sockaddr_length -- --nocapture --test-threads=1. It failed on its first case: an unbound IPv4 datagram socket, capacity zero, and 128 physical bytes. Native libc succeeded and returned 16; encode completed and wrote 128. The eight-byte replay is stored on that node. An earlier full-size control on prop_gtdb5pvh0k also had matching address bytes but lengths 16 versus 128. Neither run completed its planned grid or exercised the translator end to end.

The smallest Case check is the saved public-runtime replay, followed by a native-C/refcount getsockname comparison with full-size IPv4 and IPv6 buffers. Correct actual lengths with unchanged address bytes would confirm the repair; equality already at the unchanged baseline would reject this mechanism. Keep truncation and Unix output as separate regression and investigation questions. No performance gain is claimed.

The fresh open-PR read and actual diffs show no competing length repair. https://github.com/Cpp2Rust/cpp2rust/pull/579 changes standard-descriptor ownership and C++ streams, not address encoding. https://github.com/Cpp2Rust/cpp2rust/pull/561 changes empty-initializer rule matching and return-type checks, not this writeback. Their inspected heads were 2a70743fb1f6950cffcc8a985a36c5a98542589d and 14d0fc3f55a502d80606b233684666b610cdf87d.

Change to test: Report the actual length of the returned address rather than its storage container. Derive fixed IP lengths from the concrete address or retain the kernel's returned length, while preserving variable Unix lengths and keeping input capacity separate.

Where it lives

perfloop-oss/cpp2rust · rules/socket/tgt_refcount.rs

Evidence

The check fails before this change and passes with it: `For Linux IPv4/IPv6 datagram sockets with 128-byte backing buffers, refcount getsockname, getpeername and recvfrom preserve successful native results and address lengths independently of caller capacity, with matching native address bytes for full-size buffers.`

The check fails before this change and passes with it: `For Linux Unix datagram receives from named or unnamed senders and Unix stream socketpairs, using 128-byte source buffers and caller capacities 0, 1, 2, 8 and 128, refcount recvfrom preserves native successful results and payloads, reports the native source length and bytes when a source address is provided, and leaves outputs untouched when no source address is provided or requested.`

The check fails before this change and passes with it: `Sockaddr::encode rejects an initialized AF_UNSPEC address instead of reporting successful address writeback.`

Checks: 3 of 3 passed. Verification: no defect found.

Timeline