Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 33 additions & 36 deletions libc-test/tests/cmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

#[cfg(unix)]
mod t {

use std::mem;

use libc::{
self,
c_uchar,
c_uint,
c_void,
Expand All @@ -17,8 +15,6 @@ mod t {

extern "C" {
pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr;
// see below
#[cfg(not(target_arch = "sparc64"))]
pub fn cmsg_nxthdr(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr;
pub fn cmsg_space(length: c_uint) -> usize;
pub fn cmsg_len(length: c_uint) -> usize;
Expand Down Expand Up @@ -52,15 +48,10 @@ mod t {
#[test]
fn test_cmsg_len() {
for l in 0..128 {
unsafe {
assert_eq!(libc::CMSG_LEN(l) as usize, cmsg_len(l));
}
assert_eq!(libc::CMSG_LEN(l) as usize, unsafe { cmsg_len(l) });
}
}

// Skip on sparc64
// https://github.com/rust-lang/libc/issues/1239
#[cfg(not(target_arch = "sparc64"))]
#[test]
fn test_cmsg_nxthdr() {
// Helps to align the buffer on the stack.
Expand All @@ -69,42 +60,48 @@ mod t {

const CAPACITY: usize = 512;
let mut buffer = Align8([0_u8; CAPACITY]);
let pcmsghdr = buffer.0.as_mut_ptr().cast::<cmsghdr>();

let mut mhdr: msghdr = unsafe { mem::zeroed() };
for start_ofs in 0..64 {
let pcmsghdr = buffer.0.as_mut_ptr().cast::<cmsghdr>();
mhdr.msg_control = pcmsghdr.cast::<c_void>();
mhdr.msg_controllen = (160 - start_ofs) as _;
for cmsg_len in 0..64 {
// Address must be a multiple of 0x4 for testing on AIX.
if cfg!(target_os = "aix") && cmsg_len % std::mem::size_of::<cmsghdr>() != 0 {
continue;
}
for next_cmsg_len in 0..32 {
mhdr.msg_control = pcmsghdr.cast::<c_void>();

for trunc in 0..64 {
mhdr.msg_controllen = (160 - trunc) as _;

for cmsg_payload_len in 0..64 {
let mut current_cmsghdr_ptr = pcmsghdr;
assert!(!current_cmsghdr_ptr.is_null());
let mut count = 0;

while !current_cmsghdr_ptr.is_null() {
unsafe {
pcmsghdr.cast::<u8>().write_bytes(0, CAPACITY);
(*pcmsghdr).cmsg_len = cmsg_len as _;
let libc_next = libc::CMSG_NXTHDR(&mhdr, pcmsghdr);
let next = cmsg_nxthdr(&mhdr, pcmsghdr);
assert_eq!(libc_next, next);

if !libc_next.is_null() {
(*libc_next).cmsg_len = next_cmsg_len;
let libc_next = libc::CMSG_NXTHDR(&mhdr, pcmsghdr);
let next = cmsg_nxthdr(&mhdr, pcmsghdr);
assert_eq!(libc_next, next);
}
(*current_cmsghdr_ptr).cmsg_len =
libc::CMSG_LEN(cmsg_payload_len as _) as _;

let libc_next = libc::CMSG_NXTHDR(&mhdr, current_cmsghdr_ptr);
let system_next = cmsg_nxthdr(&mhdr, current_cmsghdr_ptr);
assert_eq!(
system_next, libc_next,
"msg_crontrollen: {}, payload_len: {}, count: {}",
mhdr.msg_controllen, cmsg_payload_len, count
);

current_cmsghdr_ptr = libc_next;
count += 1;
}
}

unsafe {
pcmsghdr.cast::<u8>().write_bytes(0, CAPACITY);
}
}
}
}

#[test]
fn test_cmsg_space() {
unsafe {
for l in 0..128 {
assert_eq!(libc::CMSG_SPACE(l) as usize, cmsg_space(l));
}
for l in 0..128 {
assert_eq!(libc::CMSG_SPACE(l) as usize, unsafe { cmsg_space(l) });
}
}
}
51 changes: 2 additions & 49 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,8 @@ pub const O_NOFOLLOW: c_int = 0x00000080;
pub const HUGETLB_FLAG_ENCODE_SHIFT: u32 = 26;
pub const MAP_HUGE_SHIFT: u32 = 26;

// END_PUB_CONST

// intentionally not public, only used for fd_set
cfg_if! {
if #[cfg(target_pointer_width = "32")] {
Expand All @@ -3011,8 +3013,6 @@ cfg_if! {
}
}

// END_PUB_CONST

f! {
pub fn FD_CLR(fd: c_int, set: *mut fd_set) -> () {
let fd = fd as usize;
Expand Down Expand Up @@ -3069,40 +3069,6 @@ f! {
pub fn CPU_EQUAL(set1: &cpu_set_t, set2: &cpu_set_t) -> bool {
set1.bits == set2.bits
}

pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
cmsg.offset(1) as *mut c_uchar
}

pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
if ((*cmsg).cmsg_len as size_t) < size_of::<cmsghdr>() {
core::ptr::null_mut::<cmsghdr>()
} else if __CMSG_NEXT(cmsg).add(size_of::<cmsghdr>()) >= __MHDR_END(mhdr) {
core::ptr::null_mut::<cmsghdr>()
} else {
__CMSG_NEXT(cmsg).cast()
}
}

pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
if (*mhdr).msg_controllen as size_t >= size_of::<cmsghdr>() {
(*mhdr).msg_control.cast()
} else {
core::ptr::null_mut::<cmsghdr>()
}
}

pub const fn CMSG_ALIGN(len: size_t) -> size_t {
(len + size_of::<size_t>() - 1) & !(size_of::<size_t>() - 1)
}

pub const fn CMSG_SPACE(len: c_uint) -> c_uint {
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(size_of::<cmsghdr>())) as c_uint
}

pub const fn CMSG_LEN(len: c_uint) -> c_uint {
(CMSG_ALIGN(size_of::<cmsghdr>()) + len as size_t) as c_uint
}
}

safe_f! {
Expand Down Expand Up @@ -3168,19 +3134,6 @@ safe_f! {
}
}

fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t {
((unsafe { (*cmsg).cmsg_len as size_t } + size_of::<c_long>() - 1) & !(size_of::<c_long>() - 1))
as ssize_t
}

fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar {
(unsafe { cmsg.offset(__CMSG_LEN(cmsg)) }) as *mut c_uchar
}

fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar {
unsafe { (*mhdr).msg_control.offset((*mhdr).msg_controllen as isize) }.cast()
}

// EXTERN_FN

#[link(name = "c")]
Expand Down
1 change: 1 addition & 0 deletions src/new/aix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
//! * Headers are not public
//! * Manual pages: <https://www.ibm.com/docs/en/aix> (under "Technical reference" for that version)

pub(crate) mod sys;
pub(crate) mod unistd;
3 changes: 3 additions & 0 deletions src/new/aix/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Directory: `sys/`
pub(crate) mod socket;
9 changes: 9 additions & 0 deletions src/new/aix/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Header: `sys/socket.h`

pub use crate::new::common::posix::sys::socket::{
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_NXTHDR,
CMSG_SPACE,
};
7 changes: 7 additions & 0 deletions src/new/apple/libc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Entrypoint for Apple headers, usually found as part of the Xcode SDK.
//!
//! <https://github.com/apple-oss-distributions/Libc/tree/main/include>
pub(crate) mod signal;
pub(crate) mod sys;
pub(crate) mod unistd;
3 changes: 3 additions & 0 deletions src/new/apple/libc/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Directory: `sys/`
pub(crate) mod socket;
20 changes: 20 additions & 0 deletions src/new/apple/libc/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Header: `sys/socket.h`
//!
//! <https://github.com/freebsd/freebsd-src/blob/main/sys/sys/socket.h>

pub(crate) type __ALIGN_BOUNDARY = c_long;

pub use crate::new::common::posix::sys::socket::{
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_SPACE,
};

pub unsafe fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const crate::cmsghdr) -> *mut crate::cmsghdr {
if cmsg.is_null() {
return CMSG_FIRSTHDR(mhdr);
}

crate::new::common::posix::sys::socket::CMSG_NXTHDR(mhdr, cmsg)
}
1 change: 1 addition & 0 deletions src/new/apple/xnu/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! <https://github.com/apple-oss-distributions/xnu/tree/main/bsd/sys>

pub(crate) mod signal;
pub(crate) mod socket;

/// Directory: `sys/_types`
///
Expand Down
11 changes: 11 additions & 0 deletions src/new/apple/xnu/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Header: `sys/socket.h`
//!
//! <https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/socket.h>

pub use crate::new::common::bsd::sys::socket::CMSG_NXTHDR;
pub use crate::new::common::posix::sys::socket::{
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_SPACE,
};
11 changes: 11 additions & 0 deletions src/new/bionic_libc/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Header: `sys/socket.h`
//!
//! <https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/sys/socket.h>

use crate::prelude::*;

Expand Down Expand Up @@ -26,6 +28,15 @@ s! {
}
}

pub use crate::new::common::posix::sys::socket::{
CMSG_ALIGN,
CMSG_DATA,
CMSG_FIRSTHDR,
CMSG_LEN,
CMSG_NXTHDR,
CMSG_SPACE,
};

extern "C" {
pub fn recvmmsg(
sockfd: c_int,
Expand Down
16 changes: 16 additions & 0 deletions src/new/common/bsd.rs
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
//! Interfaces common across the BSD family.

pub(crate) mod sys {
pub(crate) mod socket {
#[cfg(not(target_os = "dragonfly"))]
pub unsafe fn CMSG_NXTHDR(
mhdr: *const crate::msghdr,
cmsg: *const crate::cmsghdr,
) -> *mut crate::cmsghdr {
if cmsg.is_null() {
return crate::CMSG_FIRSTHDR(mhdr);
}

crate::new::common::posix::sys::socket::CMSG_NXTHDR(mhdr, cmsg)
}
}
}
2 changes: 2 additions & 0 deletions src/new/common/posix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
))]
pub(crate) mod pthread;
pub(crate) mod unistd;

pub(crate) mod sys;
3 changes: 3 additions & 0 deletions src/new/common/posix/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Directory: `sys/`
pub(crate) mod socket;
Loading