use std::ffi::c_int;
use std::io;
use snare_interpose::{Host, NetResult as HostResult};
use crate::limits::{Privileges, Rlimit, SysConfig};
const RLIMIT_MEMLOCK: c_int = libc::RLIMIT_MEMLOCK as c_int;
#[cfg(target_os = "linux")]
const RLIMIT_RTPRIO: c_int = libc::RLIMIT_RTPRIO as c_int;
#[cfg(target_os = "linux")]
const RLIMIT_NICE: c_int = libc::RLIMIT_NICE as c_int;
fn ok(n: i64) -> Option<HostResult> {
Some(HostResult::Ok(n))
}
fn err(errno: c_int) -> Option<HostResult> {
Some(HostResult::Err(errno))
}
fn from_os(v: libc::rlim_t) -> u64 {
#[cfg(target_os = "macos")]
let infinite = v >= libc::RLIM_INFINITY;
#[cfg(not(target_os = "macos"))]
let infinite = v == libc::RLIM_INFINITY;
if infinite { Rlimit::INFINITY } else { v }
}
fn to_os(v: u64) -> libc::rlim_t {
if v == Rlimit::INFINITY {
libc::RLIM_INFINITY
} else {
v
}
}
pub(crate) fn real_rlimit(resource: c_int) -> io::Result<Rlimit> {
let mut r = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
#[cfg(target_os = "linux")]
let rc = unsafe { libc::getrlimit(resource as libc::__rlimit_resource_t, &mut r) };
#[cfg(not(target_os = "linux"))]
let rc = unsafe { libc::getrlimit(resource, &mut r) };
if rc != 0 {
return Err(io::Error::last_os_error());
}
Ok(Rlimit {
cur: from_os(r.rlim_cur),
max: from_os(r.rlim_max),
})
}
fn slot(p: &mut Privileges, resource: c_int) -> Option<&mut Rlimit> {
match resource {
RLIMIT_MEMLOCK => Some(&mut p.memlock_limit),
#[cfg(target_os = "linux")]
RLIMIT_RTPRIO => Some(&mut p.rtprio_limit),
#[cfg(target_os = "linux")]
RLIMIT_NICE => Some(&mut p.nice_limit),
_ => None,
}
}
fn modelled(resource: c_int) -> bool {
slot(&mut Privileges::none(), resource).is_some()
}
unsafe fn read_rlimit(ptr: *const u8) -> Rlimit {
let r = unsafe { (ptr as *const libc::rlimit).read_unaligned() };
Rlimit {
cur: from_os(r.rlim_cur),
max: from_os(r.rlim_max),
}
}
unsafe fn write_rlimit(ptr: *mut u8, r: Rlimit) {
let out = libc::rlimit {
rlim_cur: to_os(r.cur),
rlim_max: to_os(r.max),
};
unsafe { (ptr as *mut libc::rlimit).write_unaligned(out) };
}
fn check_set(p: &Privileges, old: Rlimit, new: Rlimit) -> Result<(), c_int> {
if new.cur > new.max {
return Err(libc::EINVAL);
}
let raises = if cfg!(target_os = "macos") {
(new.cur > old.max || new.max > old.max) && !p.root
} else {
new.max > old.max && !p.sys_resource
};
if raises { Err(libc::EPERM) } else { Ok(()) }
}
pub(crate) unsafe fn prlimit(
sys: &SysConfig,
pid: i32,
resource: c_int,
new: *const u8,
old: *mut u8,
) -> Option<HostResult> {
if !modelled(resource) || (pid != 0 && pid != std::process::id() as i32) {
return None;
}
let new = (!new.is_null()).then(|| unsafe { read_rlimit(new) });
let mut result = Ok(());
let mut before = Rlimit::new(0);
sys.set_privileges(|p| {
let current = *slot(p, resource).unwrap();
before = current;
if let Some(new) = new {
result = check_set(p, current, new);
if result.is_ok() {
*slot(p, resource).unwrap() = new;
}
}
});
if let Err(e) = result {
return err(e);
}
if !old.is_null() {
unsafe { write_rlimit(old, before) };
}
ok(0)
}
pub(crate) unsafe fn getrlimit(
sys: &SysConfig,
resource: c_int,
out: *mut u8,
) -> Option<HostResult> {
if !modelled(resource) {
return None;
}
#[cfg(not(target_os = "linux"))]
if out.is_null() {
return err(libc::EFAULT);
}
unsafe { prlimit(sys, 0, resource, std::ptr::null(), out) }
}
pub(crate) unsafe fn setrlimit(
sys: &SysConfig,
resource: c_int,
new: *const u8,
) -> Option<HostResult> {
if !modelled(resource) {
return None;
}
if new.is_null() {
return err(libc::EFAULT);
}
unsafe { prlimit(sys, 0, resource, new, std::ptr::null_mut()) }
}
#[cfg(target_os = "linux")]
#[derive(Clone, Copy)]
pub(crate) struct SchedState {
pub(crate) policy: c_int,
pub(crate) rt_priority: c_int,
pub(crate) nice: c_int,
pub(crate) reset_on_fork: bool,
}
#[cfg(target_os = "linux")]
pub(crate) const SETPARAM_POLICY: c_int = -1;
#[cfg(target_os = "linux")]
const SCHED_DEADLINE: c_int = 6;
#[cfg(target_os = "linux")]
fn is_nice_reduction(p: &Privileges, nice: c_int) -> bool {
(20 - nice) as u64 <= p.nice_limit.cur
}
#[cfg(target_os = "linux")]
pub(crate) fn sched_setscheduler(
p: &Privileges,
cur: SchedState,
policy: c_int,
priority: c_int,
) -> Result<SchedState, c_int> {
let (policy, reset_on_fork) = if policy == SETPARAM_POLICY {
(cur.policy, cur.reset_on_fork)
} else {
let reset = policy & libc::SCHED_RESET_ON_FORK != 0;
let policy = policy & !libc::SCHED_RESET_ON_FORK;
let valid = matches!(
policy,
libc::SCHED_OTHER | libc::SCHED_FIFO | libc::SCHED_RR | libc::SCHED_BATCH
) || policy == libc::SCHED_IDLE
|| policy == SCHED_DEADLINE;
if !valid {
return Err(libc::EINVAL);
}
(policy, reset)
};
let priority = priority as u32;
let rt = policy == libc::SCHED_FIFO || policy == libc::SCHED_RR;
if priority > 99 || policy == SCHED_DEADLINE || rt != (priority != 0) {
return Err(libc::EINVAL);
}
if !p.sys_nice {
let rtprio = p.rtprio_limit.cur;
let denied = (rt && policy != cur.policy && rtprio == 0)
|| (rt && priority > cur.rt_priority as u32 && u64::from(priority) > rtprio)
|| (cur.policy == libc::SCHED_IDLE
&& policy != libc::SCHED_IDLE
&& !is_nice_reduction(p, cur.nice))
|| (cur.reset_on_fork && !reset_on_fork);
if denied {
return Err(libc::EPERM);
}
}
Ok(SchedState {
policy,
rt_priority: priority as c_int,
nice: cur.nice,
reset_on_fork,
})
}
#[cfg(target_os = "linux")]
pub(crate) fn setpriority(p: &Privileges, current: c_int, nice: c_int) -> Result<c_int, c_int> {
let nice = nice.clamp(-20, 19);
if nice < current && !p.sys_nice && !is_nice_reduction(p, nice) {
return Err(libc::EACCES);
}
Ok(nice)
}
#[derive(Default)]
pub(crate) struct Wired {
runs: Vec<(u64, u64, u32)>,
all: bool,
}
impl Wired {
fn pages(&self) -> u64 {
self.runs.iter().map(|&(s, e, _)| e - s).sum()
}
fn unwired_in(&self, start: u64, end: u64) -> u64 {
let covered: u64 = self
.runs
.iter()
.map(|&(s, e, _)| e.min(end).saturating_sub(s.max(start)))
.sum();
(end - start) - covered
}
fn update(&mut self, start: u64, end: u64, f: impl Fn(u32) -> u32) {
let mut out = Vec::with_capacity(self.runs.len() + 2);
let mut at = start;
for &(s, e, c) in &self.runs {
if e <= start || s >= end {
out.push((s, e, c));
continue;
}
if s < start {
out.push((s, start, c));
}
if at < s {
out.push((at, s, f(0)));
}
out.push((s.max(start), e.min(end), f(c)));
if e > end {
out.push((end, e, c));
}
at = e.min(end);
}
if at < end {
out.push((at, end, f(0)));
}
out.retain(|&(s, e, c)| c != 0 && s < e);
out.sort_unstable_by_key(|&(s, _, _)| s);
self.runs = out;
}
}
fn page_size() -> u64 {
snare_interpose::real(|| unsafe { libc::sysconf(libc::_SC_PAGESIZE) }) as u64
}
fn page_range(addr: u64, len: u64) -> Option<(u64, u64)> {
let ps = page_size();
let end = addr.checked_add(len)?;
Some((addr / ps, end.div_ceil(ps)))
}
#[cfg(target_os = "linux")]
fn total_vm_pages() -> u64 {
snare_interpose::real(|| std::fs::read_to_string("/proc/self/statm"))
.ok()
.and_then(|s| s.split_whitespace().next()?.parse().ok())
.unwrap_or(u64::MAX)
}
fn mlock_check(
p: &Privileges,
w: &Wired,
addr: u64,
len: u64,
) -> Result<Option<(u64, u64)>, c_int> {
if cfg!(target_os = "macos") && len == 0 {
return Ok(None);
}
let limit = p.memlock_limit.cur;
if cfg!(target_os = "linux") && limit == 0 && !p.ipc_lock {
return Err(libc::EPERM);
}
let (start, end) = page_range(addr, len).ok_or(libc::EINVAL)?;
if start == end {
return Ok(None);
}
let ps = page_size();
let limit_pages = if limit == Rlimit::INFINITY {
u64::MAX
} else {
limit / ps
};
let locked = w.pages() + w.unwired_in(start, end);
if cfg!(target_os = "macos") {
if locked > limit_pages {
return Err(libc::EAGAIN);
}
} else if !w.all && locked > limit_pages && !p.ipc_lock {
return Err(libc::ENOMEM);
}
Ok(Some((start, end)))
}
fn wire(w: &mut Wired, range: Option<(u64, u64)>) {
if let Some((start, end)) = range {
if cfg!(target_os = "macos") {
w.update(start, end, |c| c.saturating_add(1));
} else {
w.update(start, end, |_| 1);
}
}
}
fn unwire(w: &mut Wired, addr: u64, len: u64) -> Result<(), c_int> {
let (start, end) = page_range(addr, len).ok_or(libc::EINVAL)?;
if start < end {
if cfg!(target_os = "macos") {
w.update(start, end, |c| c.saturating_sub(1));
} else {
w.update(start, end, |_| 0);
}
}
Ok(())
}
#[cfg(target_os = "linux")]
fn mlockall_check(p: &Privileges, flags: c_int) -> Result<(), c_int> {
let known = libc::MCL_CURRENT | libc::MCL_FUTURE | libc::MCL_ONFAULT;
if flags == 0 || flags & !known != 0 || flags == libc::MCL_ONFAULT {
return Err(libc::EINVAL);
}
let limit = p.memlock_limit.cur;
if limit == 0 && !p.ipc_lock {
return Err(libc::EPERM);
}
let limit_pages = if limit == Rlimit::INFINITY {
u64::MAX
} else {
limit / page_size()
};
if flags & libc::MCL_CURRENT != 0 && !p.ipc_lock && total_vm_pages() > limit_pages {
return Err(libc::ENOMEM);
}
Ok(())
}
pub(crate) fn mlock(sys: &SysConfig, addr: u64, len: u64) -> Option<HostResult> {
let p = sys.privileges();
let mut w = sys.wired();
match mlock_check(&p, &w, addr, len) {
Ok(range) => {
wire(&mut w, range);
ok(0)
}
Err(e) => err(e),
}
}
pub(crate) fn munlock(sys: &SysConfig, addr: u64, len: u64) -> Option<HostResult> {
match unwire(&mut sys.wired(), addr, len) {
Ok(()) => ok(0),
Err(e) => err(e),
}
}
#[cfg(target_os = "linux")]
pub(crate) fn mlockall(sys: &SysConfig, flags: c_int) -> Option<HostResult> {
let p = sys.privileges();
if let Err(e) = mlockall_check(&p, flags) {
return err(e);
}
if flags & libc::MCL_CURRENT != 0 {
sys.wired().all = true;
}
ok(0)
}
#[cfg(target_os = "linux")]
pub(crate) fn munlockall(sys: &SysConfig) -> Option<HostResult> {
*sys.wired() = Wired::default();
ok(0)
}
#[cfg(target_os = "linux")]
pub(crate) unsafe fn route_syscall(
host: &dyn Host,
number: i64,
args: [i64; 6],
) -> Option<HostResult> {
match number {
n if n == libc::SYS_prlimit64 => unsafe {
host.prlimit(
args[0] as i32,
args[1] as c_int,
args[2] as *const u8,
args[3] as *mut u8,
)
},
#[cfg(target_arch = "x86_64")]
n if n == libc::SYS_getrlimit => unsafe {
host.getrlimit(args[0] as c_int, args[1] as *mut u8)
},
#[cfg(target_arch = "x86_64")]
n if n == libc::SYS_setrlimit => unsafe {
host.setrlimit(args[0] as c_int, args[1] as *const u8)
},
n if n == libc::SYS_mlock => host.mlock(args[0] as u64, args[1] as u64),
n if n == libc::SYS_munlock => host.munlock(args[0] as u64, args[1] as u64),
n if n == libc::SYS_mlockall => host.mlockall(args[0] as c_int),
n if n == libc::SYS_munlockall => host.munlockall(),
_ => None,
}
}
pub(crate) struct ProcHost {
pub(crate) shared: std::sync::Arc<crate::scope::SimShared>,
}
impl ProcHost {
fn sys(&self) -> &SysConfig {
&self.shared.sys
}
#[cfg(target_os = "linux")]
fn real_sched(tid: i32) -> Option<SchedState> {
let policy = unsafe { libc::sched_getscheduler(tid) };
if policy < 0 {
return None;
}
let mut param = libc::sched_param { sched_priority: 0 };
if unsafe { libc::sched_getparam(tid, &mut param) } != 0 {
return None;
}
Some(SchedState {
policy: policy & !libc::SCHED_RESET_ON_FORK,
rt_priority: param.sched_priority,
nice: Self::real_nice(tid)?,
reset_on_fork: policy & libc::SCHED_RESET_ON_FORK != 0,
})
}
#[cfg(target_os = "linux")]
fn real_nice(tid: i32) -> Option<c_int> {
let raw = unsafe { libc::syscall(libc::SYS_getpriority, libc::PRIO_PROCESS, tid) };
(raw >= 0).then(|| 20 - raw as c_int)
}
#[cfg(target_os = "linux")]
fn gate_sched(&self, pid: i32, policy: c_int, param: *const u8) -> Option<HostResult> {
if param.is_null() || pid < 0 {
return None;
}
let priority = unsafe { (param as *const c_int).read_unaligned() };
let cur = Self::real_sched(pid)?;
match sched_setscheduler(&self.sys().privileges(), cur, policy, priority) {
Err(libc::EPERM) => err(libc::EPERM),
_ => None,
}
}
}
impl Host for ProcHost {
unsafe fn getrlimit(&self, resource: c_int, rlim: *mut u8) -> Option<HostResult> {
unsafe { getrlimit(self.sys(), resource, rlim) }
}
unsafe fn setrlimit(&self, resource: c_int, rlim: *const u8) -> Option<HostResult> {
unsafe { setrlimit(self.sys(), resource, rlim) }
}
unsafe fn prlimit(
&self,
pid: i32,
resource: c_int,
new: *const u8,
old: *mut u8,
) -> Option<HostResult> {
unsafe { prlimit(self.sys(), pid, resource, new, old) }
}
fn mlock(&self, addr: u64, len: u64) -> Option<HostResult> {
let p = self.sys().privileges();
let mut w = self.sys().wired();
match mlock_check(&p, &w, addr, len) {
Err(e) => err(e),
Ok(range) => {
if unsafe { libc::mlock(addr as *const libc::c_void, len as usize) } != 0 {
return err(io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::ENOMEM));
}
wire(&mut w, range);
ok(0)
}
}
}
fn munlock(&self, addr: u64, len: u64) -> Option<HostResult> {
if unsafe { libc::munlock(addr as *const libc::c_void, len as usize) } != 0 {
return err(io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::ENOMEM));
}
let _ = unwire(&mut self.sys().wired(), addr, len);
ok(0)
}
#[cfg(target_os = "linux")]
fn mlockall(&self, flags: c_int) -> Option<HostResult> {
if let Err(e) = mlockall_check(&self.sys().privileges(), flags) {
return err(e);
}
if unsafe { libc::mlockall(flags) } != 0 {
return err(io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::ENOMEM));
}
if flags & libc::MCL_CURRENT != 0 {
self.sys().wired().all = true;
}
ok(0)
}
#[cfg(target_os = "linux")]
fn munlockall(&self) -> Option<HostResult> {
unsafe { libc::munlockall() };
munlockall(self.sys())
}
#[cfg(target_os = "linux")]
unsafe fn sched_setscheduler(
&self,
pid: i32,
policy: c_int,
param: *const u8,
) -> Option<HostResult> {
if policy < 0 {
return None;
}
self.gate_sched(pid, policy, param)
}
#[cfg(target_os = "linux")]
unsafe fn sched_setparam(&self, pid: i32, param: *const u8) -> Option<HostResult> {
self.gate_sched(pid, SETPARAM_POLICY, param)
}
#[cfg(target_os = "linux")]
unsafe fn setpriority(&self, which: c_int, who: u32, prio: c_int) -> Option<HostResult> {
if which != libc::PRIO_PROCESS as c_int {
return None;
}
let current = Self::real_nice(who as i32)?;
match setpriority(&self.sys().privileges(), current, prio) {
Err(e) => err(e),
Ok(_) => None,
}
}
#[cfg(target_os = "linux")]
unsafe fn pthread_setschedparam(
&self,
thread: u64,
policy: c_int,
param: *const u8,
) -> Option<HostResult> {
if thread != unsafe { libc::pthread_self() } as u64 {
return None;
}
match self.gate_sched(0, policy, param) {
Some(HostResult::Err(e)) => err(e),
_ => None,
}
}
#[cfg(target_os = "linux")]
unsafe fn syscall(&self, number: i64, args: [i64; 6]) -> Option<HostResult> {
match number {
n if n == libc::SYS_sched_setscheduler => unsafe {
self.sched_setscheduler(args[0] as i32, args[1] as c_int, args[2] as *const u8)
},
n if n == libc::SYS_sched_setparam => unsafe {
self.sched_setparam(args[0] as i32, args[1] as *const u8)
},
n if n == libc::SYS_setpriority => unsafe {
self.setpriority(args[0] as c_int, args[1] as u32, args[2] as c_int)
},
_ => unsafe { route_syscall(self, number, args) },
}
}
#[cfg(target_os = "macos")]
unsafe fn sysctl(
&self,
name: *const c_int,
namelen: u32,
oldp: *mut u8,
oldlenp: *mut usize,
newp: *const u8,
newlen: usize,
) -> Option<HostResult> {
let stats = crate::netstats::macos::StatsHost(self.shared.clone());
unsafe { stats.sysctl(name, namelen, oldp, oldlenp, newp, newlen) }
}
#[cfg(target_os = "macos")]
unsafe fn sysctlbyname(
&self,
name: *const std::ffi::c_char,
oldp: *mut u8,
oldlenp: *mut usize,
newp: *const u8,
newlen: usize,
) -> Option<HostResult> {
let stats = crate::netstats::macos::StatsHost(self.shared.clone());
unsafe { stats.sysctlbyname(name, oldp, oldlenp, newp, newlen) }
}
#[cfg(target_os = "macos")]
unsafe fn sysctlnametomib(
&self,
name: *const std::ffi::c_char,
mib: *mut c_int,
sizep: *mut usize,
) -> Option<HostResult> {
let stats = crate::netstats::macos::StatsHost(self.shared.clone());
unsafe { stats.sysctlnametomib(name, mib, sizep) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wired_runs_split_and_merge() {
let mut w = Wired::default();
w.update(10, 20, |c| c + 1);
w.update(15, 25, |c| c + 1);
assert_eq!(w.runs, vec![(10, 15, 1), (15, 20, 2), (20, 25, 1)]);
assert_eq!(w.pages(), 15);
assert_eq!(w.unwired_in(0, 30), 15);
w.update(12, 22, |c| c.saturating_sub(1));
assert_eq!(w.runs, vec![(10, 12, 1), (15, 20, 1), (22, 25, 1)]);
w.update(0, 100, |_| 0);
assert!(w.runs.is_empty());
}
}