wasi-common/yanix: fix FreeBSD support (#756)

* wasi-common/yanix: fix FreeBSD support

* yanix: add fadvise support on FreeBSD and NetBSD

* runtime,jit: use cfg(unix) instead of linux||macos
This commit is contained in:
myfreeweb
2020-02-09 17:44:48 +03:00
committed by GitHub
parent 2af544de8b
commit 699109658c
6 changed files with 59 additions and 16 deletions

View File

@@ -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<u32>,
relocs: Vec<FunctionTableReloc>,
published: Option<Vec<usize>>,
}
#[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" {

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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",
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"))]
const DSYNC = libc::O_DSYNC;
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",

View File

@@ -42,6 +42,12 @@ pub(crate) fn iter_impl(dir: &Dir) -> Option<Result<EntryImpl>> {
}
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()
}

View File

@@ -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,