Skip to content

Commit

Permalink
Disable timeout in musl libc (WasmEdge#71)
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Prendes <[email protected]>
Signed-off-by: csh <[email protected]>
  • Loading branch information
jprendes authored and L-jasmine committed Oct 31, 2023
1 parent 89ea9a2 commit 7cfb547
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
38 changes: 22 additions & 16 deletions crates/wasmedge-sys/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@
use super::ffi;
#[cfg(all(feature = "async", target_os = "linux"))]
use crate::r#async::fiber::{AsyncState, FiberFuture, TimeoutFiberFuture};
use crate::r#async::fiber::{AsyncState, FiberFuture};

#[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
use crate::r#async::fiber::TimeoutFiberFuture;

use crate::{
instance::{function::AsFunc, module::InnerInstance},
store::Store,
types::WasmEdgeString,
utils::check,
AsInstance, Config, Function, Instance, Module, Statistics, WasmEdgeResult, WasmValue,
};

#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
use std::os::raw::c_void;
use wasmedge_types::error::WasmEdgeError;

#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub(crate) struct JmpState {
pub(crate) sigjmp_buf: *mut setjmp::sigjmp_buf,
}

#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
scoped_tls::scoped_thread_local!(pub(crate) static JMP_BUF: JmpState);

#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
unsafe extern "C" fn sync_timeout(sig: i32, info: *mut libc::siginfo_t) {
if let Some(info) = info.as_mut() {
let si_value = info.si_value();
Expand All @@ -40,7 +43,7 @@ unsafe extern "C" fn sync_timeout(sig: i32, info: *mut libc::siginfo_t) {
}
}
}
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
unsafe extern "C" fn pre_host_func(_: *mut c_void) {
use libc::SIG_BLOCK;

Expand All @@ -49,7 +52,7 @@ unsafe extern "C" fn pre_host_func(_: *mut c_void) {
libc::sigaddset(&mut set, timeout_signo());
libc::pthread_sigmask(SIG_BLOCK, &set, std::ptr::null_mut());
}
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
unsafe extern "C" fn post_host_func(_: *mut c_void) {
use libc::SIG_UNBLOCK;

Expand All @@ -60,19 +63,19 @@ unsafe extern "C" fn post_host_func(_: *mut c_void) {
}

#[inline]
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub(crate) fn timeout_signo() -> i32 {
option_env!("SIG_OFFSET")
.and_then(|s| s.parse().ok())
.unwrap_or(0)
+ libc::SIGRTMIN()
}

#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
static INIT_SIGNAL_LISTEN: std::sync::Once = std::sync::Once::new();

#[inline(always)]
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub(crate) unsafe fn init_signal_listen() {
INIT_SIGNAL_LISTEN.call_once(|| {
let mut new_act: libc::sigaction = std::mem::zeroed();
Expand Down Expand Up @@ -119,7 +122,7 @@ impl Executor {
if ctx.is_null() {
Err(Box::new(WasmEdgeError::ExecutorCreate))
} else {
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
unsafe {
ffi::WasmEdge_ExecutorExperimentalRegisterPreHostFunction(
ctx,
Expand Down Expand Up @@ -195,8 +198,8 @@ impl Executor {
/// # Errors
///
/// If fail to run the host function, then an error is returned.
#[cfg(target_os = "linux")]
#[cfg_attr(docsrs, doc(cfg(target_os = "linux")))]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "linux", not(target_env = "musl")))))]
pub fn call_func_with_timeout(
&self,
func: &mut Function,
Expand Down Expand Up @@ -305,8 +308,11 @@ impl Executor {
/// # Errors
///
/// If fail to run the host function, then an error is returned.
#[cfg_attr(docsrs, doc(cfg(all(feature = "async", target_os = "linux"))))]
#[cfg(all(feature = "async", target_os = "linux"))]
#[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "async", target_os = "linux", not(target_env = "musl"))))
)]
pub async fn call_func_async_with_timeout(
&mut self,
async_state: &AsyncState,
Expand Down
11 changes: 7 additions & 4 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl Executor {
/// # Errors
///
/// If fail to run the host function, then an error is returned.
#[cfg(target_os = "linux")]
#[cfg_attr(docsrs, doc(cfg(target_os = "linux")))]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "linux", not(target_env = "musl")))))]
pub fn run_func_with_timeout(
&self,
func: &Func,
Expand Down Expand Up @@ -123,8 +123,11 @@ impl Executor {
/// # Errors
///
/// If fail to run the host function, then an error is returned.
#[cfg(all(feature = "async", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "async", target_os = "linux"))))]
#[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "async", target_os = "linux", not(target_env = "musl"))))
)]
pub async fn run_func_async_with_timeout(
&self,
async_state: &AsyncState,
Expand Down
2 changes: 1 addition & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'inst, T: ?Sized + SyncInst> Vm<'inst, T> {
/// # Error
///
/// If fail to run the wasm function, then an error is returned.
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub fn run_func_with_timeout(
&mut self,
mod_name: Option<&str>,
Expand Down

0 comments on commit 7cfb547

Please sign in to comment.