Skip to content
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

feat: add WASM hook system and a dummy plugin #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1,380 changes: 1,363 additions & 17 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ env_logger = "0.9.0"
strum = "0.24.0"
strum_macros = "0.24.0"
anyhow = "1.0.57"
wasmer = "3.0.0-rc.2"
wasmer-wasi = "3.0.0-rc.2"
prost = "0.11.2"
bytes = "1.2.1"

[dev-dependencies]
assert_cmd = "2.0.4"
assert_fs = "1.0.7"
predicates = "2.1.1"
trycmd = "0.14.0"

[build-dependencies]
prost-build = "0.11.2"

[workspace]

members = [
"hook",
".",
]
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate prost_build;

fn main() {
prost_build::compile_protos(&["proto/hook.proto"], &["proto"]).unwrap();
}
2 changes: 2 additions & 0 deletions hook/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-wasi"
7 changes: 7 additions & 0 deletions hook/Cargo.lock

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

11 changes: 11 additions & 0 deletions hook/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "hook"
version = "0.1.0"
edition = "2021"

[dependencies]
prost = "0.11.2"
bytes = "1.2.1"

[build-dependencies]
prost-build = "0.11.2"
5 changes: 5 additions & 0 deletions hook/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate prost_build;

fn main() {
prost_build::compile_protos(&["../proto/hook.proto"], &["../proto"]).unwrap();
}
27 changes: 27 additions & 0 deletions hook/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use prost::Message;
use std::io;
use std::io::prelude::*;
use std::io::Cursor;

pub mod hook {
include!(concat!(env!("OUT_DIR"), "/hook.rs"));
}

fn main() {
let mut stdin = io::stdin().lock();
let input = stdin.fill_buf().unwrap();

let req = hook::Req::decode(&mut Cursor::new(input)).unwrap();
for (key, value) in std::env::vars() {
eprintln!("{key}: {value}");
}

let mut resp = hook::Resp::default();
resp.bar = req.foo;

let mut output = Vec::new();
output.reserve(resp.encoded_len());
resp.encode(&mut output).unwrap();

io::stdout().write_all(&output).unwrap();
}
11 changes: 11 additions & 0 deletions proto/hook.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package hook;

message Req {
string foo = 1;
}

message Resp {
string bar = 1;
}
56 changes: 54 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use crate::data::{self, ObjectType, OID};
use anyhow::Context;
use anyhow::{Context, Ok};
use clap::{Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
use prost::Message;
use std::io::{Cursor, Read, Write};
use std::{env, fs, path::PathBuf};
use wasmer::{Instance, Module, Store};
use wasmer_wasi::{WasiBidirectionalSharedPipePair, WasiState};

pub mod hook {
include!(concat!(env!("OUT_DIR"), "/hook.rs"));
}

#[derive(Parser, Debug)]
#[command(about = "A bad git clone, in Rust", long_about = None)]
Expand Down Expand Up @@ -81,25 +89,69 @@ fn hash_object(context: &data::Context, path: PathBuf) -> anyhow::Result<()> {
context.ensure_init()?;
let data = fs::read(&path).context(format!("could not read '{}'", path.display()))?;
println!("{}", context.hash_object(data, ObjectType::Blob)?);
hook()?;
Ok(())
}

fn cat_file(context: &data::Context, object: String) -> anyhow::Result<()> {
context.ensure_init()?;
let data = context.get_object(OID(object), &[ObjectType::Blob])?;
println!("{}", String::from_utf8_lossy(&data));
hook()?;
Ok(())
}

fn write_tree(context: &data::Context) -> anyhow::Result<()> {
context.ensure_init()?;
println!("{}", context.write_tree(&context.work_dir)?);
hook()?;
Ok(())
}

fn read_tree(context: &data::Context, object: String) -> anyhow::Result<()> {
context.ensure_init()?;
context.read_tree(OID(object), &context.work_dir)
context.read_tree(OID(object), &context.work_dir)?;
hook()?;
Ok(())
}

fn hook() -> anyhow::Result<()> {
let mut store = Store::default();
let module = Module::from_file(&store, "target/wasm32-wasi/debug/hook.wasm")?;
let mut input = WasiBidirectionalSharedPipePair::new().with_blocking(false);
let mut output = WasiBidirectionalSharedPipePair::new().with_blocking(false);
let wasi_env = WasiState::new("hello")
.stdin(Box::new(input.clone()))
.stdout(Box::new(output.clone()))
.finalize(&mut store)?;

let import_object = wasi_env.import_object(&mut store, &module)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

let memory = instance.exports.get_memory("memory")?;
wasi_env.data_mut(&mut store).set_memory(memory.clone());

let mut req = hook::Req::default();
req.foo = String::from("Hello world");

let mut req_b = Vec::new();
req_b.reserve(req.encoded_len());
req.encode(&mut req_b).unwrap();
input.write_all(&req_b)?;

instance
.exports
.get_function("_start")?
.call(&mut store, &[])?;

let mut resp_b = Vec::new();
output.read_to_end(&mut resp_b)?;

let resp = hook::Resp::decode(&mut Cursor::new(resp_b)).unwrap();

println!("Read \"{:?}\" from the WASI stdout!", resp);

Ok(())
}

#[test]
Expand Down