1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use dirs::cache_dir;
use std::collections::{BTreeMap, HashMap};
use wasmer_runtime::{cache::*, compile, error::RuntimeError, Func, Module};
pub struct Opt {
name: String,
backend: String,
cache_prefix: Option<String>,
entrypoint: String,
args: Vec<String>,
env: BTreeMap<String, String>,
}
impl Opt {
pub fn new(name: String) -> Self {
Opt {
name: name,
backend: "cranelift".into(),
cache_prefix: None,
entrypoint: "_start".into(),
args: vec![],
env: BTreeMap::<String, String>::new(),
}
}
pub fn backend(mut self, backend: String) -> Self {
self.backend = backend;
self
}
pub fn cache_prefix(mut self, prefix: String) -> Self {
self.cache_prefix = Some(prefix);
self
}
pub fn entrypoint(mut self, entrypoint: String) -> Self {
self.entrypoint = entrypoint;
self
}
pub fn args(mut self, argv: Vec<String>) -> Self {
self.args = argv;
self
}
pub fn env(mut self, env: BTreeMap<String, String>) -> Self {
self.env = env;
self
}
pub fn system_env(mut self) -> Self {
for (key, value) in std::env::vars() {
self.env.insert(key, value);
}
self
}
pub fn setenv<T, U>(mut self, key: T, val: U) -> Self
where
T: Into<String>,
U: Into<String>,
{
self.env.insert(key.into(), val.into());
self
}
}
pub fn run(opt: Opt, wasm_bytes: &[u8]) -> Result<Status, Error> {
if opt.backend != "cranelift" {
Err(Error::BadBackend(opt.backend))?;
}
let module: Module = match opt.cache_prefix {
None => compile(wasm_bytes)?,
Some(pfx) => {
let mut cache_dir = cache_dir().unwrap();
cache_dir.push(pfx);
let mut fs_cache = unsafe { FileSystemCache::new(cache_dir)? };
let key = WasmHash::generate(&wasm_bytes);
match fs_cache.load(key) {
Ok(modu) => modu,
_ => {
let modu = compile(wasm_bytes).expect("module to compile");
fs_cache
.store(key, modu.clone())
.expect("cache storage to work");
modu
}
}
}
};
let entrypoint = opt.entrypoint.clone();
let imports = crate::import_object(opt.name.clone(), opt.args, opt.env);
let mut instance = module.instantiate(&imports)?;
let result = instance
.exports
.get::<Func<(), ()>>(&entrypoint)
.map_err(|_| Error::CantFindEntrypoint(entrypoint.clone()))?
.call();
let mut status = Status::default();
status.exit_code = if let Err(RuntimeError::User(why)) = result {
match why.downcast_ref::<crate::ExitCode>() {
Some(exit) => exit.code,
_ => {
log::error!("{} exited violently: {:?}", opt.name, why);
1
}
}
} else {
0
};
let (_, env) = crate::Process::get_memory_and_environment(instance.context_mut(), 0);
status.resource_urls = env.resource_urls.clone();
status.called_functions = env.called_functions.clone();
Ok(status)
}
#[derive(Default, Debug)]
pub struct Status {
pub exit_code: i32,
pub resource_urls: Vec<String>,
pub called_functions: Box<HashMap<String, u32>>,
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("CWA runtime error: {0}")]
CWAError(#[from] crate::error::Error),
#[error("bad backend, wanted cranelift and not {0}")]
BadBackend(String),
#[error("IO Error: {0}")]
IOError(#[from] std::io::Error),
#[error("can't find entrypoint {0}")]
CantFindEntrypoint(String),
#[error("Compilation error: {0}")]
CompilationError(#[from] wasmer_runtime::error::CompileError),
#[error("runtime error: {0}")]
RuntimeError(#[from] wasmer_runtime::error::Error),
}