diff --git a/crates/jit/src/function_table.rs b/crates/jit/src/function_table.rs index 625ae9d7d9..ed982cf7a2 100644 --- a/crates/jit/src/function_table.rs +++ b/crates/jit/src/function_table.rs @@ -109,14 +109,14 @@ impl Drop for FunctionTable { /// Represents a runtime function table. /// /// This is used to register JIT code with the operating system to enable stack walking and unwinding. -#[cfg(any(target_os = "macos", target_os = "linux"))] +#[cfg(unix)] pub(crate) struct FunctionTable { functions: Vec, relocs: Vec, published: Option>, } -#[cfg(any(target_os = "macos", target_os = "linux"))] +#[cfg(unix)] impl FunctionTable { /// Creates a new function table. pub fn new() -> Self { @@ -191,7 +191,7 @@ impl FunctionTable { } } -#[cfg(any(target_os = "macos", target_os = "linux"))] +#[cfg(unix)] impl Drop for FunctionTable { fn drop(&mut self) { extern "C" { diff --git a/crates/runtime/src/instance.rs b/crates/runtime/src/instance.rs index 656b10ba74..6137c1aef9 100644 --- a/crates/runtime/src/instance.rs +++ b/crates/runtime/src/instance.rs @@ -34,7 +34,7 @@ use wasmtime_environ::wasm::{ use wasmtime_environ::{DataInitializer, Module, TableElements, VMOffsets}; cfg_if::cfg_if! { - if #[cfg(any(target_os = "linux", target_os = "macos"))] { + if #[cfg(unix)] { pub type SignalHandler = dyn Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool; impl InstanceHandle { diff --git a/crates/runtime/src/traphandlers.rs b/crates/runtime/src/traphandlers.rs index 04ba4b2c36..0a2b91b6e6 100644 --- a/crates/runtime/src/traphandlers.rs +++ b/crates/runtime/src/traphandlers.rs @@ -30,7 +30,7 @@ extern "C" { } cfg_if::cfg_if! { - if #[cfg(any(target_os = "linux", target_os = "macos"))] { + if #[cfg(unix)] { #[no_mangle] pub unsafe extern "C" fn HandleTrap( pc: *mut u8, diff --git a/crates/wasi-common/yanix/src/file.rs b/crates/wasi-common/yanix/src/file.rs index 397cc8bd30..ee24d00709 100644 --- a/crates/wasi-common/yanix/src/file.rs +++ b/crates/wasi-common/yanix/src/file.rs @@ -1,5 +1,6 @@ use crate::{Errno, Result}; use bitflags::bitflags; +use cfg_if::cfg_if; use std::{ convert::TryInto, ffi::{CString, OsStr, OsString}, @@ -48,14 +49,23 @@ bitflags! { const APPEND = libc::O_APPEND; const CREAT = libc::O_CREAT; const DIRECTORY = libc::O_DIRECTORY; - #[cfg(any(target_os = "android", - target_os = "ios", - target_os = "linux", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd", - target_os = "emscripten"))] - const DSYNC = libc::O_DSYNC; + const DSYNC = { + // Have to use cfg_if: https://github.com/bitflags/bitflags/issues/137 + cfg_if! { + if #[cfg(any(target_os = "android", + target_os = "ios", + target_os = "linux", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "emscripten"))] { + libc::O_DSYNC + } else if #[cfg(target_os = "freebsd")] { + // https://github.com/bytecodealliance/wasmtime/pull/756 + libc::O_SYNC + } + } + }; const EXCL = libc::O_EXCL; #[cfg(any(target_os = "dragonfly", target_os = "freebsd", diff --git a/crates/wasi-common/yanix/src/sys/bsd/dir.rs b/crates/wasi-common/yanix/src/sys/bsd/dir.rs index 41a8d5941a..2a0b4b3a0e 100644 --- a/crates/wasi-common/yanix/src/sys/bsd/dir.rs +++ b/crates/wasi-common/yanix/src/sys/bsd/dir.rs @@ -42,6 +42,12 @@ pub(crate) fn iter_impl(dir: &Dir) -> Option> { } impl EntryExt for Entry { + #[cfg(target_os = "freebsd")] + fn ino(&self) -> u64 { + self.0.d_fileno.into() + } + + #[cfg(not(target_os = "freebsd"))] fn ino(&self) -> u64 { self.0.d_ino.into() } diff --git a/crates/wasi-common/yanix/src/sys/bsd/fadvise.rs b/crates/wasi-common/yanix/src/sys/bsd/fadvise.rs index 74150f3f27..0f60dcfb32 100644 --- a/crates/wasi-common/yanix/src/sys/bsd/fadvise.rs +++ b/crates/wasi-common/yanix/src/sys/bsd/fadvise.rs @@ -1,6 +1,7 @@ use crate::{Errno, Result}; use std::{convert::TryInto, os::unix::prelude::*}; +#[cfg(not(any(target_os = "freebsd", target_os = "netbsd")))] #[derive(Debug, Copy, Clone)] #[repr(i32)] pub enum PosixFadviseAdvice { @@ -12,6 +13,18 @@ pub enum PosixFadviseAdvice { DontNeed, } +#[cfg(any(target_os = "freebsd", target_os = "netbsd"))] +#[derive(Debug, Copy, Clone)] +#[repr(i32)] +pub enum PosixFadviseAdvice { + Normal = libc::POSIX_FADV_NORMAL, + Sequential = libc::POSIX_FADV_SEQUENTIAL, + Random = libc::POSIX_FADV_RANDOM, + NoReuse = libc::POSIX_FADV_NOREUSE, + WillNeed = libc::POSIX_FADV_WILLNEED, + DontNeed = libc::POSIX_FADV_DONTNEED, +} + // There's no posix_fadvise on macOS but we can use fcntl with F_RDADVISE // command instead to achieve the same #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -38,9 +51,23 @@ pub unsafe fn posix_fadvise( Errno::from_success_code(libc::fcntl(fd, libc::F_RDADVISE, &advisory)) } -// TODO -// On non-macOS BSD's we leave it as no-op for now -#[cfg(not(any(target_os = "macos", target_os = "ios")))] +#[cfg(any(target_os = "freebsd", target_os = "netbsd"))] +pub unsafe fn posix_fadvise( + fd: RawFd, + offset: libc::off_t, + len: libc::off_t, + advice: PosixFadviseAdvice, +) -> Result<()> { + Errno::from_success_code(libc::posix_fadvise(fd, offset, len, advice as libc::c_int)) +} + +// On BSDs without support we leave it as no-op +#[cfg(not(any( + target_os = "macos", + target_os = "ios", + target_os = "freebsd", + target_os = "netbsd" +)))] pub unsafe fn posix_fadvise( _fd: RawFd, _offset: libc::off_t,