Skip to content

Commit

Permalink
Replace use of the libc by the rustix crate
Browse files Browse the repository at this point in the history
In the interest of reducing 3rd-party dependencies and maintaining
control over our code, I would not only like to restore our old polling
code in favour of the polling crate (#344), but also switch to using
rustix. The benefit of using rustix is that at least on Linux we can
avoid libc calls. While this may result in a performance improvement,
it's really more about slowly reducing our dependencies on 3rd-party
libraries, including libc. The API might also be a bit more pleasant
to use compared to using the libc crate directly.

Changelog: changed
  • Loading branch information
thaodt authored and yorickpeterse committed Jul 10, 2023
1 parent c03f6ff commit f2379c6
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 103 deletions.
55 changes: 51 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
@@ -1,5 +1,6 @@
[workspace]
members = ["ast", "inko", "compiler", "rt"]
resolver = "2"

[profile.release]
panic = "abort"
Expand Down
1 change: 1 addition & 0 deletions rt/Cargo.toml
Expand Up @@ -19,6 +19,7 @@ rand = { version = "^0.8", features = ["default", "small_rng"] }
polling = "^2.8"
unicode-segmentation = "^1.8"
backtrace = "^0.3"
rustix = { version = "^0.38", features = ["fs", "mm", "param", "process", "net", "std", "time"], default-features = false }

[dependencies.socket2]
version = "^0.5"
Expand Down
45 changes: 27 additions & 18 deletions rt/src/memory_map.rs
@@ -1,7 +1,6 @@
use crate::page::{multiple_of_page_size, page_size};
use libc::{
c_int, mmap, mprotect, munmap, MAP_ANON, MAP_FAILED, MAP_PRIVATE,
PROT_NONE, PROT_READ, PROT_WRITE,
use rustix::mm::{
mmap_anonymous, mprotect, munmap, MapFlags, MprotectFlags, ProtFlags,
};
use std::io::{Error, Result as IoResult};
use std::ptr::null_mut;
Expand All @@ -12,16 +11,16 @@ pub(crate) struct MemoryMap {
pub(crate) len: usize,
}

fn mmap_options(_stack: bool) -> c_int {
let base = MAP_PRIVATE | MAP_ANON;
fn mmap_options(_stack: bool) -> MapFlags {
let base = MapFlags::PRIVATE;

#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
if _stack {
return base | libc::MAP_STACK;
return base | MapFlags::STACK;
}

base
Expand All @@ -32,34 +31,44 @@ impl MemoryMap {
let size = multiple_of_page_size(size);
let opts = mmap_options(stack);

let ptr = unsafe {
mmap(null_mut(), size, PROT_READ | PROT_WRITE, opts, -1, 0)
let res = unsafe {
mmap_anonymous(
null_mut(),
size,
ProtFlags::READ | ProtFlags::WRITE,
opts,
)
};

if ptr == MAP_FAILED {
panic!("mmap(2) failed: {}", Error::last_os_error());
match res {
Ok(ptr) => MemoryMap { ptr: ptr as *mut u8, len: size },
Err(e) => panic!(
"mmap(2) failed: {}",
Error::from_raw_os_error(e.raw_os_error())
),
}

MemoryMap { ptr: ptr as *mut u8, len: size }
}

pub(crate) fn protect(&mut self, start: usize) -> IoResult<()> {
let res = unsafe {
mprotect(self.ptr.add(start) as _, page_size(), PROT_NONE)
mprotect(
self.ptr.add(start) as _,
page_size(),
MprotectFlags::empty(),
)
};

if res == 0 {
Ok(())
} else {
Err(Error::last_os_error())
match res {
Ok(_) => Ok(()),
Err(e) => Err(Error::from_raw_os_error(e.raw_os_error())),
}
}
}

impl Drop for MemoryMap {
fn drop(&mut self) {
unsafe {
munmap(self.ptr as _, self.len);
let _ = munmap(self.ptr as _, self.len);
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions rt/src/page.rs
@@ -1,16 +1,11 @@
use libc::{sysconf, _SC_PAGESIZE};
use std::sync::atomic::{AtomicUsize, Ordering};

static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);

fn page_size_raw() -> usize {
unsafe { sysconf(_SC_PAGESIZE) as usize }
}

pub(crate) fn page_size() -> usize {
match PAGE_SIZE.load(Ordering::Relaxed) {
0 => {
let size = page_size_raw();
let size = rustix::param::page_size();

PAGE_SIZE.store(size, Ordering::Relaxed);
size
Expand Down
17 changes: 6 additions & 11 deletions rt/src/runtime.rs
Expand Up @@ -27,8 +27,10 @@ use std::io::{stdout, Write as _};
use std::process::exit as rust_exit;
use std::thread;

#[cfg(unix)]
fn ignore_sigpipe() {
const SIGPIPE: i32 = 13;
const SIG_IGN: usize = 1;

extern "C" {
// Broken pipe errors default to terminating the entire program, making it
// impossible to handle such errors. This is especially problematic for
// sockets, as writing to a socket closed on the other end would terminate
Expand All @@ -37,14 +39,7 @@ fn ignore_sigpipe() {
// While Rust handles this for us when compiling an executable, it doesn't
// do so when compiling it to a static library and linking it to our
// generated code, so we must handle this ourselves.
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_IGN);
}
}

#[cfg(not(unix))]
fn ignore_sigpipe() {
// Not needed on these platforms
fn signal(sig: i32, handler: usize) -> usize;
}

#[no_mangle]
Expand All @@ -65,7 +60,7 @@ pub unsafe extern "system" fn inko_runtime_start(
class: ClassPointer,
method: NativeAsyncMethod,
) {
ignore_sigpipe();
signal(SIGPIPE, SIG_IGN);
(*runtime).start(class, method);
flush_stdout();
}
Expand Down
25 changes: 4 additions & 21 deletions rt/src/runtime/time.rs
@@ -1,19 +1,11 @@
use crate::mem::Float;
use crate::state::State;
use rustix::time;
use std::mem::MaybeUninit;

fn utc() -> f64 {
unsafe {
let mut ts = MaybeUninit::uninit();

if libc::clock_gettime(libc::CLOCK_REALTIME, ts.as_mut_ptr()) != 0 {
panic!("clock_gettime() failed");
}

let ts = ts.assume_init();

ts.tv_sec as f64 + (ts.tv_nsec as f64 / 1_000_000_000.0)
}
let ts = time::clock_gettime(time::ClockId::Realtime);
ts.tv_sec as f64 + (ts.tv_nsec as f64 / 1_000_000_000.0)
}

fn offset() -> i64 {
Expand All @@ -22,16 +14,7 @@ fn offset() -> i64 {
fn tzset();
}

let ts = {
let mut ts = MaybeUninit::uninit();

if libc::clock_gettime(libc::CLOCK_REALTIME, ts.as_mut_ptr()) != 0 {
panic!("clock_gettime() failed");
}

ts.assume_init()
};

let ts = time::clock_gettime(time::ClockId::Realtime);
let mut tm = MaybeUninit::uninit();

// localtime_r() doesn't necessarily call tzset() for us.
Expand Down
13 changes: 4 additions & 9 deletions rt/src/scheduler/mod.rs
Expand Up @@ -5,19 +5,14 @@ pub mod timeouts;
use std::thread::available_parallelism;

#[cfg(target_os = "linux")]
use {
libc::{cpu_set_t, sched_setaffinity, CPU_SET},
std::mem::{size_of, zeroed},
};
use rustix::process::{sched_setaffinity, CpuSet, Pid};

#[cfg(target_os = "linux")]
pub(crate) fn pin_thread_to_core(core: usize) {
unsafe {
let mut set: cpu_set_t = zeroed();
let mut set = CpuSet::new();
set.set(core);

CPU_SET(core, &mut set);
sched_setaffinity(0, size_of::<cpu_set_t>(), &set);
}
let _ = sched_setaffinity(Pid::from_raw(0), &set);
}

#[cfg(not(target_os = "linux"))]
Expand Down
11 changes: 7 additions & 4 deletions rt/src/socket.rs
Expand Up @@ -4,7 +4,7 @@ use crate::network_poller::Interest;
use crate::process::ProcessPointer;
use crate::socket::socket_address::SocketAddress;
use crate::state::State;
use libc::{EINPROGRESS, EISCONN};
use rustix::io::Errno;
use socket2::{Domain, SockAddr, Socket as RawSocket, Type};
use std::io::{self, Read};
use std::mem::transmute;
Expand Down Expand Up @@ -197,7 +197,8 @@ impl Socket {
Ok(_) => Ok(()),
Err(ref e)
if e.kind() == io::ErrorKind::WouldBlock
|| e.raw_os_error() == Some(EINPROGRESS) =>
|| e.raw_os_error()
== Some(Errno::INPROGRESS.raw_os_error()) =>
{
if let Ok(Some(err)) = self.inner.take_error() {
// When performing a connect(), the error returned may be
Expand All @@ -208,8 +209,10 @@ impl Socket {

Err(io::Error::from(io::ErrorKind::WouldBlock))
}
Err(ref e) if e.raw_os_error() == Some(EISCONN) => {
// We may run into an EISCONN if a previous connect(2) attempt
Err(ref e)
if e.raw_os_error() == Some(Errno::ISCONN.raw_os_error()) =>
{
// We may run into an ISCONN if a previous connect(2) attempt
// would block. In this case we can just continue.
Ok(())
}
Expand Down

0 comments on commit f2379c6

Please sign in to comment.