Skip to content

Commit fa966c5

Browse files
committed
Initial freebsd work
1 parent 2151756 commit fa966c5

File tree

11 files changed

+209
-23
lines changed

11 files changed

+209
-23
lines changed

ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ case $HOST_TARGET in
5050
MIRI_TEST_TARGET=i686-unknown-linux-gnu run_tests
5151
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
5252
MIRI_TEST_TARGET=i686-pc-windows-msvc run_tests
53+
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests
5354
;;
5455
x86_64-apple-darwin)
5556
MIRI_TEST_TARGET=mips64-unknown-linux-gnuabi64 run_tests # big-endian architecture

diff

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
diff --git a/src/shims/unix/freebsd/mod.rs b/src/shims/unix/freebsd/mod.rs
2+
new file mode 100644
3+
index 00000000..329f2217
4+
--- /dev/null
5+
+++ b/src/shims/unix/freebsd/mod.rs
6+
@@ -0,0 +1 @@
7+
+// placeholder
8+
\ No newline at end of file
9+
diff --git a/src/shims/unix/mod.rs b/src/shims/unix/mod.rs
10+
index f40dfaef..4002b056 100644
11+
--- a/src/shims/unix/mod.rs
12+
+++ b/src/shims/unix/mod.rs
13+
@@ -7,5 +7,6 @@ mod thread;
14+
15+
mod linux;
16+
mod macos;
17+
+mod freebsd;
18+
19+
pub use fs::{DirHandler, FileHandler};
20+
diff --git a/tests/pass/libc.rs b/tests/pass/libc.rs
21+
index bf5ae982..20a06ffb 100644
22+
--- a/tests/pass/libc.rs
23+
+++ b/tests/pass/libc.rs
24+
@@ -5,12 +5,12 @@
25+
26+
extern crate libc;
27+
28+
-#[cfg(target_os = "linux")]
29+
+#[cfg(target_os = "linux, freebsd")]
30+
fn tmp() -> std::path::PathBuf {
31+
std::env::var("MIRI_TEMP").map(std::path::PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
32+
}
33+
34+
-#[cfg(target_os = "linux")]
35+
+#[cfg(target_os = "linux, freebsd")]
36+
fn test_posix_fadvise() {
37+
use std::convert::TryInto;
38+
use std::fs::{remove_file, File};
39+
@@ -40,7 +40,7 @@ fn test_posix_fadvise() {
40+
assert_eq!(result, 0);
41+
}
42+
43+
-#[cfg(target_os = "linux")]
44+
+#[cfg(target_os = "linux, freebsd")]
45+
fn test_sync_file_range() {
46+
use std::fs::{remove_file, File};
47+
use std::io::Write;
48+
@@ -191,7 +191,7 @@ fn test_rwlock_libc_static_initializer() {
49+
/// Test whether the `prctl` shim correctly sets the thread name.
50+
///
51+
/// Note: `prctl` exists only on Linux.
52+
-#[cfg(target_os = "linux")]
53+
+#[cfg(target_os = "linux,freebsd")]
54+
fn test_prctl_thread_name() {
55+
use std::ffi::CString;
56+
use libc::c_long;
57+
@@ -231,7 +231,7 @@ fn test_thread_local_errno() {
58+
}
59+
60+
/// Tests whether clock support exists at all
61+
-#[cfg(target_os = "linux")]
62+
+#[cfg(target_os = "linux,freebsd")]
63+
fn test_clocks() {
64+
let mut tp = std::mem::MaybeUninit::<libc::timespec>::uninit();
65+
let is_error = unsafe {
66+
@@ -252,11 +252,18 @@ fn test_clocks() {
67+
assert_eq!(is_error, 0);
68+
}
69+
70+
+#[cfg(target_os = "linux,freebsd,macos")]
71+
+fn test_getpid() {
72+
+ unsafe {
73+
+ assert_eq!(libc::getpid(), std::process::id());
74+
+ }
75+
+}
76+
+
77+
fn main() {
78+
- #[cfg(target_os = "linux")]
79+
+ #[cfg(target_os = "linux,freebsd")]
80+
test_posix_fadvise();
81+
82+
- #[cfg(target_os = "linux")]
83+
+ #[cfg(target_os = "linux,freebsd")]
84+
test_sync_file_range();
85+
86+
test_mutex_libc_init_recursive();
87+
@@ -264,14 +271,14 @@ fn main() {
88+
test_mutex_libc_init_errorcheck();
89+
test_rwlock_libc_static_initializer();
90+
91+
- #[cfg(target_os = "linux")]
92+
+ #[cfg(target_os = "linux,freebsd")]
93+
test_mutex_libc_static_initializer_recursive();
94+
95+
- #[cfg(target_os = "linux")]
96+
+ #[cfg(target_os = "linux,freebsd")]
97+
test_prctl_thread_name();
98+
99+
test_thread_local_errno();
100+
101+
- #[cfg(target_os = "linux")]
102+
+ #[cfg(target_os = "linux,freebsd")]
103+
test_clocks();
104+
}

src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
702702

703703
// Platform-specific shims
704704
_ => match this.tcx.sess.target.os.as_ref() {
705-
"linux" | "macos" => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
705+
"linux" | "macos" | "freebsd" => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
706706
"windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
707707
target => throw_unsup_format!("the target `{}` is not supported", target),
708708
}

src/shims/unix/dlsym.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ use rustc_target::spec::abi::Abi;
44
use crate::*;
55
use shims::unix::linux::dlsym as linux;
66
use shims::unix::macos::dlsym as macos;
7+
use shims::unix::freebsd::dlsym as freebsd;
78

89
#[derive(Debug, Copy, Clone)]
910
pub enum Dlsym {
1011
Linux(linux::Dlsym),
1112
MacOs(macos::Dlsym),
13+
FreeBSD(freebsd::Dlsym)
1214
}
1315

1416
impl Dlsym {
@@ -18,6 +20,7 @@ impl Dlsym {
1820
Ok(match target_os {
1921
"linux" => linux::Dlsym::from_str(name)?.map(Dlsym::Linux),
2022
"macos" => macos::Dlsym::from_str(name)?.map(Dlsym::MacOs),
23+
"freebsd" => freebsd::Dlsym::from_str(name)?.map(Dlsym::FreeBSD),
2124
_ => unreachable!(),
2225
})
2326
}
@@ -40,6 +43,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4043
match dlsym {
4144
Dlsym::Linux(dlsym) => linux::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
4245
Dlsym::MacOs(dlsym) => macos::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
46+
Dlsym::FreeBSD(dlsym) => freebsd::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret)
4347
}
4448
}
4549
}

src/shims/unix/foreign_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
479479
match this.tcx.sess.target.os.as_ref() {
480480
"linux" => return shims::unix::linux::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
481481
"macos" => return shims::unix::macos::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
482+
"freebsd" => return shims::unix::freebsd::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
482483
_ => unreachable!(),
483484
}
484485
}

src/shims/unix/freebsd/dlsym.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use rustc_middle::mir;
2+
3+
use log::trace;
4+
5+
use crate::*;
6+
use helpers::check_arg_count;
7+
8+
#[derive(Debug, Copy, Clone)]
9+
#[allow(non_camel_case_types)]
10+
pub enum Dlsym {
11+
getentropy,
12+
}
13+
14+
impl Dlsym {
15+
// Returns an error for unsupported symbols, and None if this symbol
16+
// should become a NULL pointer (pretend it does not exist).
17+
pub fn from_str<'tcx>(name: &str) -> InterpResult<'tcx, Option<Dlsym>> {
18+
Ok(match name {
19+
"getentropy" => Some(Dlsym::getentropy),
20+
_ => throw_unsup_format!("unsupported macOS dlsym: {}", name),
21+
})
22+
}
23+
}
24+
25+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
26+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
27+
fn call_dlsym(
28+
&mut self,
29+
dlsym: Dlsym,
30+
args: &[OpTy<'tcx, Tag>],
31+
dest: &PlaceTy<'tcx, Tag>,
32+
ret: Option<mir::BasicBlock>,
33+
) -> InterpResult<'tcx> {
34+
let this = self.eval_context_mut();
35+
let ret = ret.expect("we don't support any diverging dlsym");
36+
assert!(this.tcx.sess.target.os == "macos");
37+
38+
match dlsym {
39+
Dlsym::getentropy => {
40+
let [ptr, len] = check_arg_count(args)?;
41+
let ptr = this.read_pointer(ptr)?;
42+
let len = this.read_scalar(len)?.to_machine_usize(this)?;
43+
this.gen_random(ptr, len)?;
44+
this.write_null(dest)?;
45+
}
46+
}
47+
48+
trace!("{:?}", this.dump_place(**dest));
49+
this.go_to_block(ret);
50+
Ok(())
51+
}
52+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use rustc_middle::mir;
2+
use rustc_span::Symbol;
3+
use rustc_target::spec::abi::Abi;
4+
5+
use crate::*;
6+
use shims::foreign_items::EmulateByNameResult;
7+
8+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
9+
10+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
11+
fn emulate_foreign_item_by_name(
12+
&mut self,
13+
link_name: Symbol,
14+
abi: Abi,
15+
args: &[OpTy<'tcx, Tag>],
16+
dest: &PlaceTy<'tcx, Tag>,
17+
_ret: mir::BasicBlock,
18+
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
19+
let this = self.eval_context_mut();
20+
// match
21+
Ok(EmulateByNameResult::NeedsJumping)
22+
}
23+
}

src/shims/unix/freebsd/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod foreign_items;
2+
pub mod dlsym;

src/shims/unix/macos/dlsym.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@ use rustc_middle::mir;
33
use log::trace;
44

55
use crate::*;
6-
use helpers::check_arg_count;
76

87
#[derive(Debug, Copy, Clone)]
98
#[allow(non_camel_case_types)]
109
pub enum Dlsym {
11-
getentropy,
1210
}
1311

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

3835
match dlsym {
39-
Dlsym::getentropy => {
40-
let [ptr, len] = check_arg_count(args)?;
41-
let ptr = this.read_pointer(ptr)?;
42-
let len = this.read_scalar(len)?.to_machine_usize(this)?;
43-
this.gen_random(ptr, len)?;
44-
this.write_null(dest)?;
45-
}
36+
_ => {}
4637
}
4738

4839
trace!("{:?}", this.dump_place(**dest));

src/shims/unix/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ mod thread;
77

88
mod linux;
99
mod macos;
10+
mod freebsd;
1011

1112
pub use fs::{DirHandler, FileHandler};

tests/pass/libc.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
extern crate libc;
77

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

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

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

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

255+
#[cfg(target_os = "linux,freebsd,macos")]
256+
fn test_getpid() {
257+
unsafe {
258+
assert_eq!(libc::getpid(), std::process::id());
259+
}
260+
}
261+
255262
fn main() {
256-
#[cfg(target_os = "linux")]
263+
#[cfg(target_os = "linux,freebsd")]
257264
test_posix_fadvise();
258265

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

262269
test_mutex_libc_init_recursive();
263270
test_mutex_libc_init_normal();
264271
test_mutex_libc_init_errorcheck();
265272
test_rwlock_libc_static_initializer();
266273

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

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

273280
test_thread_local_errno();
274281

275-
#[cfg(target_os = "linux")]
282+
#[cfg(target_os = "linux,freebsd")]
276283
test_clocks();
277284
}

0 commit comments

Comments
 (0)