-
Notifications
You must be signed in to change notification settings - Fork 131
Use youki's libcontainer APIs to implement wasmtime shim. #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
407b883
Update youki to the latest main branch and oci-spec to 0.6.1.
Mossaka 8ca83f2
Merge remote-tracking branch 'upstream/main' into oci-0.6.1
Mossaka c555e3f
Removed some uses of unsafe in wasmedge shim
Mossaka fc1bb2e
Use youki's libcontainer APIs to implement wasmtime shim.
Mossaka 83ed1a2
Fix some clippy warnings
Mossaka d8e4c1a
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka d3b7f69
Passed the test_delete_after_create test
Mossaka d998250
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka feb93fc
This commit adds a new crate called "containerd-shim-common".
Mossaka 47ce17d
Apply suggestions from code review
Mossaka 363f1cd
Resolved some review comments
Mossaka 6882dd5
Removed common crate and moved the library to containerd-shim-wasm and
Mossaka fc33358
Rollback accidentally deleted deps
Mossaka b606b25
Removed the libcontainer feature
Mossaka 8c9e43d
Fixed the test_wasi test
Mossaka 11c2e20
Add trace to "cargo test" in CI
Mossaka 839442a
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka 80cd742
Temp commit to run tests on ubuntu 22.04 only
Mossaka ebad282
Rollback ubuntu 20.02 test in CI
Mossaka 560356f
Use the same determine root logic from wasmedge shim
Mossaka 4b106b2
Fixed clippy issues
Mossaka 51182c6
Reset stdio at the end of the tests
Mossaka 7ca42a1
Need rollback: commenting out the stdio thing
Mossaka 8d5c858
Use inherit_stdio()
Mossaka 65177c3
Setup CI build env for wasmtimie e2e
Mossaka bb1e940
Add inpsection step to e2e-wasmtime
Mossaka e120093
Add deps to kind nodes that run k8s tests
Mossaka 76fa9cb
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka f7472fb
Handle Result types from dup and dup2 in both wasmtime and wasmedge s…
Mossaka d807601
Small refactoring
Mossaka 3f31397
Update crates/containerd-shim-wasmtime/src/executor.rs
Mossaka 2c64834
Merge branch 'wasmtime-youki' of https://github.com/Mossaka/runwasi i…
Mossaka 729c3b4
Add a log to when Errno::ECHILD was returned
Mossaka a28fad9
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka aaaea73
Resolve comments
Mossaka dfc680d
Rustfmt
Mossaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| use std::{fs::OpenOptions, os::fd::RawFd, path::PathBuf}; | ||
|
|
||
| use anyhow::{anyhow, Result}; | ||
| use containerd_shim_wasm::sandbox::oci; | ||
| use libc::{dup, dup2, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; | ||
| use libcontainer::workload::{Executor, ExecutorError}; | ||
| use oci_spec::runtime::Spec; | ||
|
|
||
| use wasmtime::{Engine, Linker, Module, Store}; | ||
| use wasmtime_wasi::WasiCtxBuilder; | ||
|
|
||
| use crate::oci_wasmtime::{self, wasi_dir, wasi_file}; | ||
|
|
||
| const EXECUTOR_NAME: &str = "wasmtime"; | ||
|
|
||
| static mut STDIN_FD: Option<RawFd> = None; | ||
| static mut STDOUT_FD: Option<RawFd> = None; | ||
| static mut STDERR_FD: Option<RawFd> = None; | ||
|
|
||
| pub struct WasmtimeExecutor { | ||
| pub stdin: Option<RawFd>, | ||
| pub stdout: Option<RawFd>, | ||
| pub stderr: Option<RawFd>, | ||
| pub engine: Engine, | ||
| } | ||
|
|
||
| impl Executor for WasmtimeExecutor { | ||
| fn exec(&self, spec: &Spec) -> Result<(), ExecutorError> { | ||
| let args = oci::get_args(spec); | ||
| if args.is_empty() { | ||
| return Err(ExecutorError::InvalidArg); | ||
| } | ||
|
Mossaka marked this conversation as resolved.
|
||
|
|
||
| let (mut store, f) = self | ||
| .prepare_function(spec, args) | ||
| .map_err(|err| ExecutorError::Other(format!("failed to prepare function: {}", err)))?; | ||
|
|
||
| log::info!("calling start function"); | ||
| match f.call(&mut store, &[], &mut []) { | ||
| Ok(_) => std::process::exit(0), | ||
| Err(_) => std::process::exit(137), | ||
| }; | ||
| } | ||
|
|
||
| fn can_handle(&self, _spec: &Spec) -> bool { | ||
| true | ||
| } | ||
|
|
||
| fn name(&self) -> &'static str { | ||
| EXECUTOR_NAME | ||
| } | ||
| } | ||
|
|
||
| impl WasmtimeExecutor { | ||
| fn prepare_function( | ||
|
Mossaka marked this conversation as resolved.
Outdated
|
||
| &self, | ||
| spec: &Spec, | ||
| args: &[String], | ||
| ) -> anyhow::Result<(Store<wasi_common::WasiCtx>, wasmtime::Func)> { | ||
| // already in the cgroup | ||
| let env = oci_wasmtime::env_to_wasi(spec); | ||
| log::info!("setting up wasi"); | ||
|
|
||
| let path = wasi_dir(".", OpenOptions::new().read(true))?; | ||
| let mut wasi_builder = WasiCtxBuilder::new() | ||
| .args(args)? | ||
|
Mossaka marked this conversation as resolved.
|
||
| .envs(env.as_slice())? | ||
| .preopened_dir(path, "/")?; | ||
|
Mossaka marked this conversation as resolved.
|
||
|
|
||
| if let Some(stdin) = self.stdin { | ||
| unsafe { | ||
| STDIN_FD = Some(dup(STDIN_FILENO)); | ||
| dup2(stdin, STDIN_FILENO); | ||
| } | ||
| } | ||
|
Mossaka marked this conversation as resolved.
|
||
| if let Some(stdout) = self.stdout { | ||
| unsafe { | ||
| STDOUT_FD = Some(dup(STDOUT_FILENO)); | ||
| dup2(stdout, STDOUT_FILENO); | ||
| } | ||
| } | ||
| if let Some(stderr) = self.stderr { | ||
| unsafe { | ||
| STDERR_FD = Some(dup(STDERR_FILENO)); | ||
| dup2(stderr, STDERR_FILENO); | ||
| } | ||
| } | ||
| log::info!("opening stdin"); | ||
| let stdin_path = PathBuf::from("/dev/stdin"); | ||
| let stdin_wasi_file = wasi_file(stdin_path, OpenOptions::new().read(true))?; | ||
| wasi_builder = wasi_builder.stdin(Box::new(stdin_wasi_file)); | ||
|
|
||
| log::info!("opening stdout"); | ||
| let stdout_path = PathBuf::from("/dev/stdout"); | ||
| let stdout_wasi_file = wasi_file(stdout_path, OpenOptions::new().write(true))?; | ||
| wasi_builder = wasi_builder.stdout(Box::new(stdout_wasi_file)); | ||
|
|
||
| log::info!("opening stderr"); | ||
| let stderr_path = PathBuf::from("/dev/stderr"); | ||
| let stderr_wasi_file = wasi_file(stderr_path, OpenOptions::new().write(true))?; | ||
| wasi_builder = wasi_builder.stderr(Box::new(stderr_wasi_file)); | ||
|
|
||
| log::info!("building wasi context"); | ||
| let wctx = wasi_builder.build(); | ||
|
|
||
| log::info!("wasi context ready"); | ||
| let start = args[0].clone(); | ||
| let mut iterator = start.split('#'); | ||
|
Mossaka marked this conversation as resolved.
Outdated
|
||
| let mut cmd = iterator.next().unwrap().to_string(); | ||
| let stripped = cmd.strip_prefix(std::path::MAIN_SEPARATOR); | ||
| if let Some(strpd) = stripped { | ||
| cmd = strpd.to_string(); | ||
| } | ||
| let method = iterator.next().unwrap_or("_start"); | ||
| let mod_path = cmd; | ||
|
|
||
| log::info!("loading module from file"); | ||
| let module = Module::from_file(&self.engine, mod_path)?; | ||
| let mut linker = Linker::new(&self.engine); | ||
|
|
||
| wasmtime_wasi::add_to_linker(&mut linker, |s| s)?; | ||
| let mut store = Store::new(&self.engine, wctx); | ||
|
|
||
| log::info!("instantiating instance"); | ||
| let i = linker.instantiate(&mut store, &module)?; | ||
|
|
||
| log::info!("getting start function"); | ||
| let f = i | ||
| .get_func(&mut store, method) | ||
| .ok_or_else(|| anyhow!("module does not have a wasi start function".to_string()))?; | ||
| Ok((store, f)) | ||
|
Mossaka marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.