Skip to content

Commit

Permalink
Initial freebsd work
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ncMN committed Jun 9, 2022
1 parent 4d6eca1 commit 5f1ef76
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 23 deletions.
1 change: 1 addition & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ case $HOST_TARGET in
MIRI_TEST_TARGET=i686-unknown-linux-gnu run_tests
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
MIRI_TEST_TARGET=i686-pc-windows-msvc run_tests
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests
;;
x86_64-apple-darwin)
MIRI_TEST_TARGET=mips64-unknown-linux-gnuabi64 run_tests # big-endian architecture
Expand Down
104 changes: 104 additions & 0 deletions diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
diff --git a/src/shims/unix/freebsd/mod.rs b/src/shims/unix/freebsd/mod.rs
new file mode 100644
index 00000000..329f2217
--- /dev/null
+++ b/src/shims/unix/freebsd/mod.rs
@@ -0,0 +1 @@
+// placeholder
\ No newline at end of file
diff --git a/src/shims/unix/mod.rs b/src/shims/unix/mod.rs
index f40dfaef..4002b056 100644
--- a/src/shims/unix/mod.rs
+++ b/src/shims/unix/mod.rs
@@ -7,5 +7,6 @@ mod thread;

mod linux;
mod macos;
+mod freebsd;

pub use fs::{DirHandler, FileHandler};
diff --git a/tests/pass/libc.rs b/tests/pass/libc.rs
index bf5ae982..20a06ffb 100644
--- a/tests/pass/libc.rs
+++ b/tests/pass/libc.rs
@@ -5,12 +5,12 @@

extern crate libc;

-#[cfg(target_os = "linux")]
+#[cfg(target_os = "linux, freebsd")]
fn tmp() -> std::path::PathBuf {
std::env::var("MIRI_TEMP").map(std::path::PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
}

-#[cfg(target_os = "linux")]
+#[cfg(target_os = "linux, freebsd")]
fn test_posix_fadvise() {
use std::convert::TryInto;
use std::fs::{remove_file, File};
@@ -40,7 +40,7 @@ fn test_posix_fadvise() {
assert_eq!(result, 0);
}

-#[cfg(target_os = "linux")]
+#[cfg(target_os = "linux, freebsd")]
fn test_sync_file_range() {
use std::fs::{remove_file, File};
use std::io::Write;
@@ -191,7 +191,7 @@ fn test_rwlock_libc_static_initializer() {
/// Test whether the `prctl` shim correctly sets the thread name.
///
/// Note: `prctl` exists only on Linux.
-#[cfg(target_os = "linux")]
+#[cfg(target_os = "linux,freebsd")]
fn test_prctl_thread_name() {
use std::ffi::CString;
use libc::c_long;
@@ -231,7 +231,7 @@ fn test_thread_local_errno() {
}

/// Tests whether clock support exists at all
-#[cfg(target_os = "linux")]
+#[cfg(target_os = "linux,freebsd")]
fn test_clocks() {
let mut tp = std::mem::MaybeUninit::<libc::timespec>::uninit();
let is_error = unsafe {
@@ -252,11 +252,18 @@ fn test_clocks() {
assert_eq!(is_error, 0);
}

+#[cfg(target_os = "linux,freebsd,macos")]
+fn test_getpid() {
+ unsafe {
+ assert_eq!(libc::getpid(), std::process::id());
+ }
+}
+
fn main() {
- #[cfg(target_os = "linux")]
+ #[cfg(target_os = "linux,freebsd")]
test_posix_fadvise();

- #[cfg(target_os = "linux")]
+ #[cfg(target_os = "linux,freebsd")]
test_sync_file_range();

test_mutex_libc_init_recursive();
@@ -264,14 +271,14 @@ fn main() {
test_mutex_libc_init_errorcheck();
test_rwlock_libc_static_initializer();

- #[cfg(target_os = "linux")]
+ #[cfg(target_os = "linux,freebsd")]
test_mutex_libc_static_initializer_recursive();

- #[cfg(target_os = "linux")]
+ #[cfg(target_os = "linux,freebsd")]
test_prctl_thread_name();

test_thread_local_errno();

- #[cfg(target_os = "linux")]
+ #[cfg(target_os = "linux,freebsd")]
test_clocks();
}
2 changes: 1 addition & 1 deletion src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// Platform-specific shims
_ => match this.tcx.sess.target.os.as_ref() {
"linux" | "macos" => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
"linux" | "macos" | "freebsd" => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
"windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
target => throw_unsup_format!("the target `{}` is not supported", target),
}
Expand Down
4 changes: 4 additions & 0 deletions src/shims/unix/dlsym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use rustc_target::spec::abi::Abi;
use crate::*;
use shims::unix::linux::dlsym as linux;
use shims::unix::macos::dlsym as macos;
use shims::unix::freebsd::dlsym as freebsd;

#[derive(Debug, Copy, Clone)]
pub enum Dlsym {
Linux(linux::Dlsym),
MacOs(macos::Dlsym),
FreeBSD(freebsd::Dlsym)
}

impl Dlsym {
Expand All @@ -18,6 +20,7 @@ impl Dlsym {
Ok(match target_os {
"linux" => linux::Dlsym::from_str(name)?.map(Dlsym::Linux),
"macos" => macos::Dlsym::from_str(name)?.map(Dlsym::MacOs),
"freebsd" => freebsd::Dlsym::from_str(name)?.map(Dlsym::FreeBSD),
_ => unreachable!(),
})
}
Expand All @@ -40,6 +43,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
match dlsym {
Dlsym::Linux(dlsym) => linux::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
Dlsym::MacOs(dlsym) => macos::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
Dlsym::FreeBSD(dlsym) => freebsd::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret)
}
}
}
1 change: 1 addition & 0 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
match this.tcx.sess.target.os.as_ref() {
"linux" => return shims::unix::linux::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
"macos" => return shims::unix::macos::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
"freebsd" => return shims::unix::freebsd::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
_ => unreachable!(),
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/shims/unix/freebsd/dlsym.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use rustc_middle::mir;

use log::trace;

use crate::*;
use helpers::check_arg_count;

#[derive(Debug, Copy, Clone)]
#[allow(non_camel_case_types)]
pub enum Dlsym {
getentropy,
}

impl Dlsym {
// Returns an error for unsupported symbols, and None if this symbol
// should become a NULL pointer (pretend it does not exist).
pub fn from_str<'tcx>(name: &str) -> InterpResult<'tcx, Option<Dlsym>> {
Ok(match name {
"getentropy" => Some(Dlsym::getentropy),
_ => throw_unsup_format!("unsupported macOS dlsym: {}", name),
})
}
}

impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
fn call_dlsym(
&mut self,
dlsym: Dlsym,
args: &[OpTy<'tcx, Tag>],
dest: &PlaceTy<'tcx, Tag>,
ret: Option<mir::BasicBlock>,
) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
let ret = ret.expect("we don't support any diverging dlsym");
assert!(this.tcx.sess.target.os == "macos");

match dlsym {
Dlsym::getentropy => {
let [ptr, len] = check_arg_count(args)?;
let ptr = this.read_pointer(ptr)?;
let len = this.read_scalar(len)?.to_machine_usize(this)?;
this.gen_random(ptr, len)?;
this.write_null(dest)?;
}
}

trace!("{:?}", this.dump_place(**dest));
this.go_to_block(ret);
Ok(())
}
}
23 changes: 23 additions & 0 deletions src/shims/unix/freebsd/foreign_items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use rustc_middle::mir;
use rustc_span::Symbol;
use rustc_target::spec::abi::Abi;

use crate::*;
use shims::foreign_items::EmulateByNameResult;

impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}

pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
fn emulate_foreign_item_by_name(
&mut self,
link_name: Symbol,
abi: Abi,
args: &[OpTy<'tcx, Tag>],
dest: &PlaceTy<'tcx, Tag>,
_ret: mir::BasicBlock,
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
let this = self.eval_context_mut();
// match
Ok(EmulateByNameResult::NeedsJumping)
}
}
2 changes: 2 additions & 0 deletions src/shims/unix/freebsd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod foreign_items;
pub mod dlsym;
15 changes: 3 additions & 12 deletions src/shims/unix/macos/dlsym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ use rustc_middle::mir;
use log::trace;

use crate::*;
use helpers::check_arg_count;

#[derive(Debug, Copy, Clone)]
#[allow(non_camel_case_types)]
pub enum Dlsym {
getentropy,
}

impl Dlsym {
// Returns an error for unsupported symbols, and None if this symbol
// should become a NULL pointer (pretend it does not exist).
pub fn from_str<'tcx>(name: &str) -> InterpResult<'tcx, Option<Dlsym>> {
Ok(match name {
"getentropy" => Some(Dlsym::getentropy),
_ => throw_unsup_format!("unsupported macOS dlsym: {}", name),
_ => throw_unsup_format!("unsupported freebsd dlsym: {}", name),
})
}
}
Expand All @@ -33,16 +30,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
let ret = ret.expect("we don't support any diverging dlsym");
assert!(this.tcx.sess.target.os == "macos");
assert!(this.tcx.sess.target.os == "freebsd");

match dlsym {
Dlsym::getentropy => {
let [ptr, len] = check_arg_count(args)?;
let ptr = this.read_pointer(ptr)?;
let len = this.read_scalar(len)?.to_machine_usize(this)?;
this.gen_random(ptr, len)?;
this.write_null(dest)?;
}
_ => {}
}

trace!("{:?}", this.dump_place(**dest));
Expand Down
1 change: 1 addition & 0 deletions src/shims/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ mod thread;

mod linux;
mod macos;
mod freebsd;

pub use fs::{DirHandler, FileHandler};
27 changes: 17 additions & 10 deletions tests/pass/libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

extern crate libc;

#[cfg(target_os = "linux")]
#[cfg(target_os = "linux, freebsd")]
fn tmp() -> std::path::PathBuf {
std::env::var("MIRI_TEMP").map(std::path::PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "linux, freebsd")]
fn test_posix_fadvise() {
use std::convert::TryInto;
use std::fs::{remove_file, File};
Expand Down Expand Up @@ -40,7 +40,7 @@ fn test_posix_fadvise() {
assert_eq!(result, 0);
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "linux, freebsd")]
fn test_sync_file_range() {
use std::fs::{remove_file, File};
use std::io::Write;
Expand Down Expand Up @@ -191,7 +191,7 @@ fn test_rwlock_libc_static_initializer() {
/// Test whether the `prctl` shim correctly sets the thread name.
///
/// Note: `prctl` exists only on Linux.
#[cfg(target_os = "linux")]
#[cfg(target_os = "linux,freebsd")]
fn test_prctl_thread_name() {
use std::ffi::CString;
use libc::c_long;
Expand Down Expand Up @@ -231,7 +231,7 @@ fn test_thread_local_errno() {
}

/// Tests whether clock support exists at all
#[cfg(target_os = "linux")]
#[cfg(target_os = "linux,freebsd")]
fn test_clocks() {
let mut tp = std::mem::MaybeUninit::<libc::timespec>::uninit();
let is_error = unsafe {
Expand All @@ -252,26 +252,33 @@ fn test_clocks() {
assert_eq!(is_error, 0);
}

#[cfg(target_os = "linux,freebsd,macos")]
fn test_getpid() {
unsafe {
assert_eq!(libc::getpid(), std::process::id());
}
}

fn main() {
#[cfg(target_os = "linux")]
#[cfg(target_os = "linux,freebsd")]
test_posix_fadvise();

#[cfg(target_os = "linux")]
#[cfg(target_os = "linux,freebsd")]
test_sync_file_range();

test_mutex_libc_init_recursive();
test_mutex_libc_init_normal();
test_mutex_libc_init_errorcheck();
test_rwlock_libc_static_initializer();

#[cfg(target_os = "linux")]
#[cfg(target_os = "linux,freebsd")]
test_mutex_libc_static_initializer_recursive();

#[cfg(target_os = "linux")]
#[cfg(target_os = "linux,freebsd")]
test_prctl_thread_name();

test_thread_local_errno();

#[cfg(target_os = "linux")]
#[cfg(target_os = "linux,freebsd")]
test_clocks();
}

0 comments on commit 5f1ef76

Please sign in to comment.