Sarthak-Pandey opened a new pull request, #21881:
URL: https://github.com/apache/nuttx/pull/21881

   ## Summary
   
   Fixes #21731: Socket option renumbering in #20713 breaks Rust's libc.
   
   ### Why change is necessary?
   PR #20713 and followup commit aa86cb9919 renumbered `SOL_SOCKET` options and 
shifted `__SO_PROTOCOL` from 16 to 11 to avoid a 32-bit integer overflow in 
`socket_conn_s:s_options`.
   However, languages like Rust (in `libc` crate `src/unix/nuttx/mod.rs`), Go, 
and external POSIX binaries hardcode these constants for NuttX. This created 
severe ABI incompatibilities:
   1. `SO_REUSEADDR` was moved from 11 to 0, while 11 was assigned to 
`SO_SNDTIMEO`.
   2. Rust's standard library always calls `setsockopt(..., SOL_SOCKET, 
SO_REUSEADDR, ...)` during `TcpListener::bind()`. Because Rust passes 11 with 
`sizeof(int) == 4`, NuttX parsed this as `SO_SNDTIMEO` (expecting `struct 
timeval` of 8/16 bytes) and failed with `-EINVAL` (`errno 22`).
   3. 29 socket options across `SOL_SOCKET`, `SOL_IP`, `SOL_IPV6`, and 
`SOL_TCP` were shifted, breaking `SO_ERROR` (ret=-1, errno 92), `TCP_NODELAY` 
(ret=-1, errno 38), etc.
   
   ### How the change works:
   1. **Public ABI Restored**: Restored all public `SOL_SOCKET` option 
constants in `include/sys/socket.h` and `arch/sim/src/sim/sim_hostusrsock.h` to 
their historical, stable POSIX values (`SO_REUSEADDR = 11`, `SO_ERROR = 4`, 
`SO_RCVBUF = 8`, `SO_SNDTIMEO = 14`, `__SO_PROTOCOL = 16`, etc.).
   2. **64-bit Bitmask Expansion**: Expanded `sockopt_t` in 
`include/nuttx/net/net.h` from `uint32_t` to `uint64_t` and updated 
`_SO_BIT(o)` in `net/socket/socket.h` to `(1ULL << (o))`. This cleanly and 
permanently solves the bitmask overflow problem while keeping `__SO_PROTOCOL = 
16`, leaving 48 free bits for protocol-level options.
   3. **Dead Code Removed**: Removed redundant `s_options` bit setting/clearing 
for `SO_RCVTIMEO` and `SO_SNDTIMEO` in `net/socket/setsockopt.c`, as socket 
timeouts are stored in `conn->s_rcvtimeo` and `conn->s_sndtimeo`.
   
   Fixes #21731
   
   ---
   
   ## Impact
   
   * **Is new feature added? Is existing feature changed?**
     - Existing feature fixed: Public POSIX socket option ABI restored to 
stable values; `sockopt_t` expanded to 64-bit to prevent overflow.
   * **Impact on user:**
     - Rust networking (`std::net::TcpListener`, `TcpStream`, `UdpSocket`, 
etc.), external C/C++ runtimes, and POSIX applications work seamlessly on NuttX 
master without breaking at `bind()`.
   * **Impact on build:**
     - NO (Standard build process unchanged; passes `nxstyle` with 0 errors).
   * **Impact on hardware:**
     - Memory: Adds 4 bytes to `struct socket_conn_s` (or 0 bytes where 
alignment/padding already existed). No architectural dependencies.
   * **Impact on documentation:**
     - NO.
   * **Impact on security:**
     - NO.
   * **Impact on compatibility:**
     - Fully backward compatible with NuttX 13.1.0 ABI and language runtimes 
(Rust libc, etc.).
   * **Anything else to consider?**
     - Resolves userspace ABI regression introduced in #20713.
   
   ---
   
   ## Testing
   
   1. Verified all modified files against `nxstyle` style checker: 0 errors, 0 
warnings.
   2. Verified `SO_REUSEADDR` (11), `SO_ERROR` (4), `SO_SNDTIMEO` (14), 
`TCP_NODELAY` (16), `IP_TTL` (30) constants match `rust-lang/libc` 
(`src/unix/nuttx/mod.rs`).
   3. Verified bitmask shift operations on 64-bit `sockopt_t` (`1ULL << 16` 
through `1ULL << 47`) do not trigger integer overflow.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to