Skip to content

Commit 30456c3

Browse files
committed
This commit implements can_handle in wasmtime executor. It checks
if the entrypoint binary has wasm as the path extension. If not, the wasmtime executor will not be used. Signed-off-by: jiaxiao zhou <jiazho@microsoft.com> This commit adds a default executor to the wasmtime shim as a backup so it enables running wasm and containers side-by-side. Signed-off-by: jiaxiao zhou <jiazho@microsoft.com> Add wat extension support in can_handle Signed-off-by: jiaxiao zhou <jiazho@microsoft.com>
1 parent aaaea73 commit 30456c3

2 files changed

Lines changed: 60 additions & 10 deletions

File tree

crates/containerd-shim-wasmtime/src/executor.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use nix::unistd::{dup, dup2};
2-
use std::{fs::OpenOptions, os::fd::RawFd};
2+
use std::{fs::OpenOptions, os::fd::RawFd, path::PathBuf};
33

44
use anyhow::{anyhow, Result, Context};
55
use containerd_shim_wasm::sandbox::oci;
@@ -39,8 +39,35 @@ impl Executor for WasmtimeExecutor {
3939
};
4040
}
4141

42-
fn can_handle(&self, _spec: &Spec) -> bool {
43-
true
42+
fn can_handle(&self, spec: &Spec) -> bool {
43+
// check if the entrypoint of the spec is a wasm binary.
44+
let args = oci::get_args(spec);
45+
if args.is_empty() {
46+
return false;
47+
}
48+
49+
let start = args[0].clone();
50+
let mut iterator = start.split('#');
51+
let mut cmd = iterator.next().unwrap().to_string();
52+
let stripped = cmd.strip_prefix(std::path::MAIN_SEPARATOR);
53+
if let Some(strpd) = stripped {
54+
cmd = strpd.to_string();
55+
}
56+
57+
let mut path = PathBuf::from(cmd);
58+
if path.is_relative() {
59+
path = std::env::current_dir().unwrap().join(path);
60+
}
61+
62+
// TODO: do we need to validate the wasm binary?
63+
// ```rust
64+
// let bytes = std::fs::read(path).unwrap();
65+
// wasmparser::validate(&bytes).is_ok()
66+
// ```
67+
68+
path.extension()
69+
.map(|ext| ext == "wasm" || ext == "wat")
70+
.unwrap_or(false)
4471
}
4572

4673
fn name(&self) -> &'static str {

crates/containerd-shim-wasmtime/src/instance.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use containerd_shim_wasm::sandbox::instance_utils::{
44
};
55
use libcontainer::container::builder::ContainerBuilder;
66
use libcontainer::container::{Container, ContainerStatus};
7+
use libcontainer::workload::default::DefaultExecutor;
78
use nix::errno::Errno;
89
use nix::sys::wait::waitid;
910
use serde::{Deserialize, Serialize};
@@ -19,7 +20,7 @@ use chrono::{DateTime, Utc};
1920
use containerd_shim_wasm::sandbox::error::Error;
2021
use containerd_shim_wasm::sandbox::instance::Wait;
2122
use containerd_shim_wasm::sandbox::{EngineGetter, Instance, InstanceConfig};
22-
use libc::{dup2, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
23+
use libc::{dup, dup2, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
2324
use libc::{SIGINT, SIGKILL};
2425
use libcontainer::syscall::syscall::create_syscall;
2526
use log::error;
@@ -243,13 +244,35 @@ impl Wasi {
243244
let stdout = maybe_open_stdio(stdout).context("could not open stdout")?;
244245
let stderr = maybe_open_stdio(stderr).context("could not open stderr")?;
245246

247+
let wasmtime_executor = Box::new(WasmtimeExecutor {
248+
engine,
249+
stdin,
250+
stdout,
251+
stderr,
252+
});
253+
let default_executor = Box::<DefaultExecutor>::default();
254+
255+
if let Some(stdin) = stdin {
256+
unsafe {
257+
STDIN_FD = Some(dup(STDIN_FILENO));
258+
dup2(stdin, STDIN_FILENO);
259+
}
260+
}
261+
if let Some(stdout) = stdout {
262+
unsafe {
263+
STDOUT_FD = Some(dup(STDOUT_FILENO));
264+
dup2(stdout, STDOUT_FILENO);
265+
}
266+
}
267+
if let Some(stderr) = stderr {
268+
unsafe {
269+
STDERR_FD = Some(dup(STDERR_FILENO));
270+
dup2(stderr, STDERR_FILENO);
271+
}
272+
}
273+
246274
let container = ContainerBuilder::new(self.id.clone(), syscall.as_ref())
247-
.with_executor(vec![Box::new(WasmtimeExecutor {
248-
stdin,
249-
stdout,
250-
stderr,
251-
engine,
252-
})])?
275+
.with_executor(vec![wasmtime_executor, default_executor])?
253276
.with_root_path(self.rootdir.clone())?
254277
.as_init(&self.bundle)
255278
.with_systemd(false)

0 commit comments

Comments
 (0)