Skip to content

Commit 8d04a3f

Browse files
authored
Merge pull request #113 from bjorn3/miri_fixes
Fix running inside miri when using in-process accounting
2 parents 65921f1 + 5f0b099 commit 8d04a3f

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

src/unix.rs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use std::mem::MaybeUninit;
88
use std::os::unix::prelude::*;
99
use std::path::Path;
1010
use std::process::Command;
11-
use std::ptr;
1211
use std::sync::{
1312
atomic::{AtomicBool, Ordering},
14-
Arc, Once,
13+
Arc,
1514
};
1615
use std::thread::{self, Builder, JoinHandle};
1716
use std::time::Duration;
@@ -72,30 +71,19 @@ impl Client {
7271
unsafe fn mk() -> io::Result<Client> {
7372
let mut pipes = [0; 2];
7473

75-
// Attempt atomically-create-with-cloexec if we can on Linux,
76-
// detected by using the `syscall` function in `libc` to try to work
77-
// with as many kernels/glibc implementations as possible.
74+
// Atomically-create-with-cloexec on Linux.
7875
#[cfg(target_os = "linux")]
76+
if libc::pipe2(pipes.as_mut_ptr(), libc::O_CLOEXEC) == -1 {
77+
return Err(io::Error::last_os_error());
78+
}
79+
80+
#[cfg(not(target_os = "linux"))]
7981
{
80-
static PIPE2_AVAILABLE: AtomicBool = AtomicBool::new(true);
81-
if PIPE2_AVAILABLE.load(Ordering::SeqCst) {
82-
match libc::syscall(libc::SYS_pipe2, pipes.as_mut_ptr(), libc::O_CLOEXEC) {
83-
-1 => {
84-
let err = io::Error::last_os_error();
85-
if err.raw_os_error() == Some(libc::ENOSYS) {
86-
PIPE2_AVAILABLE.store(false, Ordering::SeqCst);
87-
} else {
88-
return Err(err);
89-
}
90-
}
91-
_ => return Ok(Client::from_fds(pipes[0], pipes[1])),
92-
}
93-
}
82+
cvt(libc::pipe(pipes.as_mut_ptr()))?;
83+
drop(set_cloexec(pipes[0], true));
84+
drop(set_cloexec(pipes[1], true));
9485
}
9586

96-
cvt(libc::pipe(pipes.as_mut_ptr()))?;
97-
drop(set_cloexec(pipes[0], true));
98-
drop(set_cloexec(pipes[1], true));
9987
Ok(Client::from_fds(pipes[0], pipes[1]))
10088
}
10189

@@ -390,19 +378,24 @@ pub(crate) fn spawn_helper(
390378
state: Arc<super::HelperState>,
391379
mut f: Box<dyn FnMut(io::Result<crate::Acquired>) + Send>,
392380
) -> io::Result<Helper> {
393-
static USR1_INIT: Once = Once::new();
394-
let mut err = None;
395-
USR1_INIT.call_once(|| unsafe {
396-
let mut new: libc::sigaction = mem::zeroed();
397-
new.sa_sigaction = sigusr1_handler as usize;
398-
new.sa_flags = libc::SA_SIGINFO as _;
399-
if libc::sigaction(libc::SIGUSR1, &new, ptr::null_mut()) != 0 {
400-
err = Some(io::Error::last_os_error());
401-
}
402-
});
381+
#[cfg(not(miri))]
382+
{
383+
use std::sync::Once;
384+
385+
static USR1_INIT: Once = Once::new();
386+
let mut err = None;
387+
USR1_INIT.call_once(|| unsafe {
388+
let mut new: libc::sigaction = mem::zeroed();
389+
new.sa_sigaction = sigusr1_handler as usize;
390+
new.sa_flags = libc::SA_SIGINFO as _;
391+
if libc::sigaction(libc::SIGUSR1, &new, std::ptr::null_mut()) != 0 {
392+
err = Some(io::Error::last_os_error());
393+
}
394+
});
403395

404-
if let Some(e) = err.take() {
405-
return Err(e);
396+
if let Some(e) = err.take() {
397+
return Err(e);
398+
}
406399
}
407400

408401
let state2 = state.clone();
@@ -447,6 +440,7 @@ impl Helper {
447440
if state.consumer_done {
448441
break;
449442
}
443+
#[cfg(not(miri))]
450444
unsafe {
451445
// Ignore the return value here of `pthread_kill`,
452446
// apparently on OSX if you kill a dead thread it will
@@ -549,6 +543,7 @@ fn cvt(t: c_int) -> io::Result<c_int> {
549543
}
550544
}
551545

546+
#[cfg(not(miri))]
552547
extern "C" fn sigusr1_handler(
553548
_signum: c_int,
554549
_info: *mut libc::siginfo_t,
@@ -563,12 +558,7 @@ mod test {
563558

564559
use crate::{test::run_named_fifo_try_acquire_tests, Client};
565560

566-
use std::{
567-
fs::File,
568-
io::{self, Write},
569-
os::unix::io::AsRawFd,
570-
sync::Arc,
571-
};
561+
use std::{fs::File, io::Write, os::unix::io::AsRawFd, sync::Arc};
572562

573563
fn from_imp_client(imp: ClientImp) -> Client {
574564
Client {
@@ -622,7 +612,7 @@ mod test {
622612
#[cfg(not(target_os = "linux"))]
623613
assert_eq!(
624614
new_client_from_pipe().0.try_acquire().unwrap_err().kind(),
625-
io::ErrorKind::Unsupported
615+
std::io::ErrorKind::Unsupported
626616
);
627617

628618
#[cfg(target_os = "linux")]

0 commit comments

Comments
 (0)