Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Support FreeBSD #419

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion lucet-module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
bincode = "1.1.4"
minisign = "0.5.19"
object = "0.18.0"
object = "0.20.0"
byteorder = "1.3"
memoffset = "0.5.3"
thiserror = "1.0.4"
Expand Down
2 changes: 1 addition & 1 deletion lucet-objdump/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = ["Lucet team <[email protected]>"]
edition = "2018"

[dependencies]
object = "0.18"
object = "0.20"
byteorder="1.2.1"
colored="1.8.0"
lucet-module = { path = "../lucet-module", version = "=0.7.0-dev" }
Expand Down
1 change: 1 addition & 0 deletions lucet-runtime/lucet-runtime-internals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ anyhow = "1.0"
bitflags = "1.0"
bincode = "1.1.4"
byteorder = "1.3"
cfg-if = "0.1"
lazy_static = "1.4"
libc = "0.2.65"
libloading = "0.6"
Expand Down
30 changes: 17 additions & 13 deletions lucet-runtime/lucet-runtime-internals/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,23 @@ pub const MINSIGSTKSZ: usize = libc::MINSIGSTKSZ;
///
/// [sigstksz]: https://pubs.opengroup.org/onlinepubs/009695399/basedefs/signal.h.html
pub const DEFAULT_SIGNAL_STACK_SIZE: usize = {
// on Linux, `SIGSTKSZ` is too small for the signal handler when compiled in debug mode
#[cfg(all(debug_assertions, not(target_os = "macos")))]
const SIZE: usize = 12 * 1024;

// on Mac, `SIGSTKSZ` is way larger than we need; it would be nice to combine these debug cases once
// `std::cmp::max` is a const fn
#[cfg(all(debug_assertions, target_os = "macos"))]
const SIZE: usize = libc::SIGSTKSZ;

#[cfg(not(debug_assertions))]
const SIZE: usize = libc::SIGSTKSZ;

SIZE
cfg_if::cfg_if! {
if #[cfg(target_os = "freebsd")] {
// on FreeBSD/amd64, `SIGSTKSZ` is not a multiple of the page size
// (34816 == MINSIGSTKSZ(2048) + 32768)
12 * 1024
} else if #[cfg(target_os = "macos")] {
// on Mac, `SIGSTKSZ` is way larger than we need;
// it would be nice to combine these debug cases once
// `std::cmp::max` is a const fn
libc::SIGSTKSZ
} else if #[cfg(debug_assertions)] {
// on Linux, `SIGSTKSZ` is too small for the signal handler when compiled in debug mode
12 * 1024
} else {
libc::SIGSTKSZ
}
}
};

impl Limits {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,6 @@ _lucet_context_activate:
#endif

/* Mark that we don't need executable stack. */
#if defined(__linux__) && defined(__ELF__)
#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__ELF__)
.section .note.GNU-stack,"",%progbits
#endif
4 changes: 2 additions & 2 deletions lucet-runtime/lucet-runtime-internals/src/region/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl MmapRegion {
region.limits.total_memory_size(),
ProtFlags::PROT_NONE,
MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE,
0,
-1,
0,
)?
}
Expand Down Expand Up @@ -436,7 +436,7 @@ unsafe fn mmap_aligned(
alignment_offset: usize,
) -> Result<*mut c_void, Error> {
let addr = ptr::null_mut();
let fd = 0;
let fd = -1;
let offset = 0;

let padded_length = requested_length + alignment + alignment_offset;
Expand Down
56 changes: 56 additions & 0 deletions lucet-runtime/lucet-runtime-internals/src/sysdeps/freebsd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use libc::{c_void, ucontext_t};

#[derive(Clone, Copy, Debug)]
pub struct UContextPtr(*mut ucontext_t);

impl UContextPtr {
#[inline]
pub fn new(ptr: *mut c_void) -> Self {
assert!(!ptr.is_null(), "non-null context");
UContextPtr(ptr as *mut ucontext_t)
}

#[inline]
pub fn get_ip(self) -> *const c_void {
let mcontext = &unsafe { self.0.as_ref().unwrap() }.uc_mcontext;
mcontext.mc_rip as *const _
}

#[inline]
pub fn set_ip(self, new_ip: *const c_void) {
let mut mcontext = &mut unsafe { self.0.as_mut().unwrap() }.uc_mcontext;
mcontext.mc_rip = new_ip as i64;
}

#[inline]
pub fn set_rdi(self, new_rdi: u64) {
let mut mcontext = &mut unsafe { self.0.as_mut().unwrap() }.uc_mcontext;
mcontext.mc_rdi = new_rdi as i64;
}
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct UContext {
context: *mut ucontext_t,
}

impl UContext {
#[inline]
pub fn new(ptr: *mut c_void) -> Self {
UContext {
context: unsafe { (ptr as *mut ucontext_t).as_mut().expect("non-null context") },
}
}

pub fn as_ptr(&mut self) -> UContextPtr {
UContextPtr::new(self.context as *mut _ as *mut _)
}
}

impl Into<UContext> for UContextPtr {
#[inline]
fn into(self) -> UContext {
UContext { context: self.0 }
}
}
6 changes: 6 additions & 0 deletions lucet-runtime/lucet-runtime-internals/src/sysdeps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ mod macos;
#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "freebsd")]
mod freebsd;

#[cfg(unix)]
mod unix;

Expand All @@ -13,5 +16,8 @@ pub use macos::*;
#[cfg(target_os = "linux")]
pub use linux::*;

#[cfg(target_os = "freebsd")]
pub use freebsd::*;

#[cfg(unix)]
pub use unix::*;
10 changes: 5 additions & 5 deletions lucet-runtime/lucet-runtime-tests/src/guest_fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ macro_rules! guest_fault_tests {
static ref RECOVERABLE_PTR_LOCK: Mutex<()> = Mutex::new(());
}

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
const INVALID_PERMISSION_FAULT: libc::c_int = SIGSEGV;
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
const INVALID_PERMISSION_FAULT: libc::c_int = SIGBUS;

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
const INVALID_PERMISSION_SIGNAL: Signal = Signal::SIGSEGV;
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
const INVALID_PERMISSION_SIGNAL: Signal = Signal::SIGBUS;

$(
Expand Down Expand Up @@ -233,7 +233,7 @@ macro_rules! guest_fault_tests {
4096,
ProtFlags::PROT_NONE,
MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE,
0,
-1,
0,
)
.expect("mmap succeeds") as *mut libc::c_char;
Expand Down
2 changes: 1 addition & 1 deletion lucet-runtime/lucet-runtime-tests/src/guest_fault/traps.S
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ _guest_func_oob:
#endif
.cfi_endproc

#if defined(__linux__) && defined(__ELF__)
#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__ELF__)
.section ".note.GNU-stack","",@progbits
#endif
2 changes: 1 addition & 1 deletion lucet-spectest/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ScriptEnv {
.write(&objfile_path)
.map_err(ScriptError::CodegenError)?;

let mut cmd_ld = Command::new("ld");
let mut cmd_ld = Command::new(std::env::var("LD").unwrap_or("ld".to_string()));
cmd_ld.arg(objfile_path.clone());
cmd_ld.arg("-shared");
cmd_ld.arg("-o");
Expand Down
6 changes: 5 additions & 1 deletion lucetc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,11 @@ fn ldflags_default(target: &Triple) -> String {
use target_lexicon::OperatingSystem;

match target.operating_system {
OperatingSystem::Linux => "-shared",
OperatingSystem::Linux
| OperatingSystem::Freebsd
| OperatingSystem::Dragonfly
| OperatingSystem::Netbsd
| OperatingSystem::Openbsd => "-shared",
OperatingSystem::Darwin | OperatingSystem::MacOSX { .. } => {
"-dylib -dead_strip -export_dynamic -undefined dynamic_lookup"
}
Expand Down