Skip to content

Commit

Permalink
Auto merge of #100782 - thomcc:fix-android-sigaddset, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Align android `sigaddset` impl with the reference impl from Bionic

In #100737 I noticed we were treating the sigset_t as an array of bytes, while referencing code from android (https://github.com/aosp-mirror/platform_bionic/blob/ad8dcd6023294b646e5a8288c0ed431b0845da49/libc/include/android/legacy_signal_inlines.h) which treats it as an array of unsigned long.

That said, the behavior difference is so subtle here that it's not hard to see why nobody noticed. This fixes the implementation to be equivalent to the one in bionic.
  • Loading branch information
bors committed Aug 23, 2022
2 parents 38528d4 + 4ecf876 commit 1cff564
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions library/std/src/sys/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,31 @@ cfg_if::cfg_if! {
}
#[allow(dead_code)]
pub unsafe fn sigaddset(set: *mut libc::sigset_t, signum: libc::c_int) -> libc::c_int {
use crate::{slice, mem};
use crate::{
mem::{align_of, size_of},
slice,
};
use libc::{c_ulong, sigset_t};

// The implementations from bionic (android libc) type pun `sigset_t` as an
// array of `c_ulong`. This works, but lets add a smoke check to make sure
// that doesn't change.
const _: () = assert!(
align_of::<c_ulong>() == align_of::<sigset_t>()
&& (size_of::<sigset_t>() % size_of::<c_ulong>()) == 0
);

let raw = slice::from_raw_parts_mut(set as *mut u8, mem::size_of::<libc::sigset_t>());
let bit = (signum - 1) as usize;
raw[bit / 8] |= 1 << (bit % 8);
if set.is_null() || bit >= (8 * size_of::<sigset_t>()) {
crate::sys::unix::os::set_errno(libc::EINVAL);
return -1;
}
let raw = slice::from_raw_parts_mut(
set as *mut c_ulong,
size_of::<sigset_t>() / size_of::<c_ulong>(),
);
const LONG_BIT: usize = size_of::<c_ulong>() * 8;
raw[bit / LONG_BIT] |= 1 << (bit % LONG_BIT);
return 0;
}
} else {
Expand Down

0 comments on commit 1cff564

Please sign in to comment.