Skip to content

Fix CMSG_DATA(3) and friends on BSD #1212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2019
Merged
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
39 changes: 39 additions & 0 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Apple (ios/darwin)-specific definitions
//!
//! This covers *-apple-* triples currently
use dox::mem;

pub type c_char = i8;
pub type clock_t = c_ulong;
Expand Down Expand Up @@ -2383,7 +2384,45 @@ pub const SF_IMMUTABLE: ::c_uint = 0x00020000;
pub const SF_APPEND: ::c_uint = 0x00040000;
pub const UF_HIDDEN: ::c_uint = 0x00008000;

fn __DARWIN_ALIGN32(p: usize) -> usize {
const __DARWIN_ALIGNBYTES32: usize = mem::size_of::<u32>() - 1;
p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32
}

f! {
pub fn CMSG_NXTHDR(mhdr: *const ::msghdr,
cmsg: *const ::cmsghdr) -> *mut ::cmsghdr {
if cmsg.is_null() {
return ::CMSG_FIRSTHDR(mhdr);
};
let cmsg_len = (*cmsg).cmsg_len as usize;
let next = cmsg as usize + __DARWIN_ALIGN32(cmsg_len as usize)
+ __DARWIN_ALIGN32(mem::size_of::<::cmsghdr>());
let max = (*mhdr).msg_control as usize
+ (*mhdr).msg_controllen as usize;
if next > max {
0 as *mut ::cmsghdr
} else {
next as *mut ::cmsghdr
}
}

pub fn CMSG_DATA(cmsg: *const ::cmsghdr) -> *mut ::c_uchar {
(cmsg as *mut ::c_uchar)
.offset(__DARWIN_ALIGN32(mem::size_of::<::cmsghdr>()) as isize)
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(__DARWIN_ALIGN32(mem::size_of::<::cmsghdr>())
+ __DARWIN_ALIGN32(length as usize))
as ::c_uint
}

pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
__DARWIN_ALIGN32(mem::size_of::<::cmsghdr>() + length as usize)
as ::c_uint
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
status >> 8
}
Expand Down
33 changes: 33 additions & 0 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,39 @@ pub const SF_NOHISTORY: ::c_ulong = 0x00400000;
pub const SF_CACHE: ::c_ulong = 0x00800000;
pub const SF_XLINK: ::c_ulong = 0x01000000;

fn _CMSG_ALIGN(n: usize) -> usize {
(n + 3) & !3
}

f! {
pub fn CMSG_DATA(cmsg: *const ::cmsghdr) -> *mut ::c_uchar {
(cmsg as *mut ::c_uchar)
.offset(_CMSG_ALIGN(mem::size_of::<::cmsghdr>()) as isize)
}

pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
_CMSG_ALIGN(mem::size_of::<::cmsghdr>()) + length as usize
}

pub fn CMSG_NXTHDR(mhdr: *const ::msghdr, cmsg: *const ::cmsghdr)
-> *mut ::cmsghdr
{
let next = cmsg as usize + _CMSG_ALIGN((*cmsg).cmsg_len)
+ _CMSG_ALIGN(mem::size_of::<::cmsghdr>());
let max = (*mhdr).msg_control as usize
+ (*mhdr).msg_controllen as usize;
if next <= max {
(cmsg as usize + _CMSG_ALIGN((*cmsg).cmsg_len)) as *mut ::cmsghdr
} else {
0 as *mut ::cmsghdr
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
_CMSG_ALIGN(mem::size_of::<::cmsghdr>()) + _CMSG_ALIGN(length as usize)
}
}

extern {
pub fn mprotect(addr: *mut ::c_void, len: ::size_t, prot: ::c_int)
-> ::c_int;
Expand Down
5 changes: 5 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use dox::mem;

pub type c_long = i64;
pub type c_ulong = u64;
pub type time_t = i64;
Expand Down Expand Up @@ -29,4 +31,7 @@ s! {
}
}

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_longlong>() - 1;
pub const MAP_32BIT: ::c_int = 0x00080000;
38 changes: 38 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use dox::mem;

pub type fflags_t = u32;
pub type clock_t = i32;
pub type ino_t = u32;
Expand Down Expand Up @@ -962,7 +964,43 @@ pub const UF_READONLY: ::c_ulong = 0x00001000;
pub const UF_HIDDEN: ::c_ulong = 0x00008000;
pub const SF_SNAPSHOT: ::c_ulong = 0x00200000;

fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
}

f! {
pub fn CMSG_DATA(cmsg: *const ::cmsghdr) -> *mut ::c_uchar {
(cmsg as *mut ::c_uchar)
.offset(_ALIGN(mem::size_of::<::cmsghdr>()) as isize)
}

pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
_ALIGN(mem::size_of::<::cmsghdr>()) as ::c_uint + length
}

pub fn CMSG_NXTHDR(mhdr: *const ::msghdr, cmsg: *const ::cmsghdr)
-> *mut ::cmsghdr
{
if cmsg.is_null() {
return ::CMSG_FIRSTHDR(mhdr);
};
let next = cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)
+ _ALIGN(mem::size_of::<::cmsghdr>());
let max = (*mhdr).msg_control as usize
+ (*mhdr).msg_controllen as usize;
if next > max {
0 as *mut ::cmsghdr
} else {
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize))
as *mut ::cmsghdr
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(_ALIGN(mem::size_of::<::cmsghdr>()) + _ALIGN(length as usize))
as ::c_uint
}

pub fn uname(buf: *mut ::utsname) -> ::c_int {
__xuname(256, buf as *mut ::c_void)
}
Expand Down
6 changes: 6 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/x86.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use dox::mem;

pub type c_long = i32;
pub type c_ulong = u32;
pub type time_t = i32;
Expand Down Expand Up @@ -29,3 +31,7 @@ s! {
__unused: [u8; 8],
}
}

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_long>() - 1;
5 changes: 5 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use dox::mem;

pub type c_long = i64;
pub type c_ulong = u64;
pub type time_t = i64;
Expand Down Expand Up @@ -29,4 +31,7 @@ s! {
}
}

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_long>() - 1;
pub const MAP_32BIT: ::c_int = 0x00080000;
37 changes: 4 additions & 33 deletions src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,43 +340,14 @@ pub const POLLRDBAND: ::c_short = 0x080;
pub const POLLWRBAND: ::c_short = 0x100;

f! {
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
(*mhdr).msg_control as *mut cmsghdr
pub fn CMSG_FIRSTHDR(mhdr: *const ::msghdr) -> *mut ::cmsghdr {
if (*mhdr).msg_controllen as usize >= mem::size_of::<::cmsghdr>() {
(*mhdr).msg_control as *mut ::cmsghdr
} else {
0 as *mut cmsghdr
0 as *mut ::cmsghdr
}
}

pub fn CMSG_NXTHDR(mhdr: *const msghdr,
cmsg: *const cmsghdr) -> *mut cmsghdr {
if cmsg.is_null() {
return CMSG_FIRSTHDR(mhdr);
};
let pad = mem::align_of::<cmsghdr>() - 1;
let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
let max = (*mhdr).msg_control as usize
+ (*mhdr).msg_controllen as usize;
if next < max {
next as *mut cmsghdr
} else {
0 as *mut cmsghdr
}
}

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

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
let pad = mem::align_of::<cmsghdr>() as ::c_uint - 1;
mem::size_of::<cmsghdr>() as ::c_uint + ((length + pad) & !pad)
}

pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
mem::size_of::<cmsghdr>() as ::c_uint + length
}

pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
let fd = fd as usize;
Expand Down
38 changes: 38 additions & 0 deletions src/unix/bsd/netbsdlike/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use dox::mem;

pub type time_t = i64;
pub type mode_t = u32;
pub type nlink_t = ::uint32_t;
Expand Down Expand Up @@ -593,7 +595,43 @@ pub const SF_APPEND: ::c_ulong = 0x00040000;

pub const TIMER_ABSTIME: ::c_int = 1;

fn _ALIGN(p: usize) -> usize {
(p + _ALIGNBYTES) & !_ALIGNBYTES
}

f! {
pub fn CMSG_DATA(cmsg: *const ::cmsghdr) -> *mut ::c_uchar {
(cmsg as *mut ::c_uchar)
.offset(_ALIGN(mem::size_of::<::cmsghdr>()) as isize)
}

pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
_ALIGN(mem::size_of::<::cmsghdr>()) as ::c_uint + length
}

pub fn CMSG_NXTHDR(mhdr: *const ::msghdr, cmsg: *const ::cmsghdr)
-> *mut ::cmsghdr
{
if cmsg.is_null() {
return ::CMSG_FIRSTHDR(mhdr);
};
let next = cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)
+ _ALIGN(mem::size_of::<::cmsghdr>());
let max = (*mhdr).msg_control as usize
+ (*mhdr).msg_controllen as usize;
if next > max {
0 as *mut ::cmsghdr
} else {
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize))
as *mut ::cmsghdr
}
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
(_ALIGN(mem::size_of::<::cmsghdr>()) + _ALIGN(length as usize))
as ::c_uint
}

pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
status >> 8
}
Expand Down
6 changes: 6 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use dox::mem;

use PT_FIRSTMACH;

pub type c_long = i64;
pub type c_ulong = u64;
pub type c_char = u8;
pub type __cpu_simple_lock_nv_t = ::c_uchar;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_int>() - 1;

pub const PT_GETREGS: ::c_int = PT_FIRSTMACH + 0;
pub const PT_SETREGS: ::c_int = PT_FIRSTMACH + 1;
pub const PT_GETFPREGS: ::c_int = PT_FIRSTMACH + 2;
Expand Down
6 changes: 6 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/arm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use dox::mem;

use PT_FIRSTMACH;

pub type c_long = i32;
pub type c_ulong = u32;
pub type c_char = u8;
pub type __cpu_simple_lock_nv_t = ::c_int;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_longlong>() - 1;

pub const PT_GETREGS: ::c_int = PT_FIRSTMACH + 1;
pub const PT_SETREGS: ::c_int = PT_FIRSTMACH + 2;
pub const PT_GETFPREGS: ::c_int = PT_FIRSTMACH + 3;
Expand Down
6 changes: 6 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/powerpc.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use dox::mem;

use PT_FIRSTMACH;

pub type c_long = i32;
pub type c_ulong = u32;
pub type c_char = u8;
pub type __cpu_simple_lock_nv_t = ::c_int;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_double>() - 1;

pub const PT_STEP: ::c_int = PT_FIRSTMACH + 0;
pub const PT_GETREGS: ::c_int = PT_FIRSTMACH + 1;
pub const PT_SETREGS: ::c_int = PT_FIRSTMACH + 2;
4 changes: 4 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/sparc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ pub type c_long = i64;
pub type c_ulong = u64;
pub type c_char = i8;
pub type __cpu_simple_lock_nv_t = ::c_uchar;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = 0xf;
6 changes: 6 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/x86.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use dox::mem;

pub type c_long = i32;
pub type c_ulong = u32;
pub type c_char = i8;
pub type __cpu_simple_lock_nv_t = ::c_uchar;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_int>() - 1;
6 changes: 6 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use dox::mem;

use PT_FIRSTMACH;

pub type c_long = i64;
pub type c_ulong = u64;
pub type c_char = i8;
pub type __cpu_simple_lock_nv_t = ::c_uchar;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_long>() - 1;

pub const PT_STEP: ::c_int = PT_FIRSTMACH + 0;
pub const PT_GETREGS: ::c_int = PT_FIRSTMACH + 1;
pub const PT_SETREGS: ::c_int = PT_FIRSTMACH + 2;
Expand Down
6 changes: 6 additions & 0 deletions src/unix/bsd/netbsdlike/openbsdlike/openbsd/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use dox::mem;

pub type c_long = i64;
pub type c_ulong = u64;
pub type c_char = u8;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_long>() - 1;
6 changes: 6 additions & 0 deletions src/unix/bsd/netbsdlike/openbsdlike/openbsd/x86.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use dox::mem;

pub type c_long = i32;
pub type c_ulong = u32;
pub type c_char = i8;

// should be pub(crate), but that requires Rust 1.18.0
#[doc(hidden)]
pub const _ALIGNBYTES: usize = mem::size_of::<::c_int>() - 1;
Loading