-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathengine.rs
More file actions
288 lines (247 loc) · 9.16 KB
/
engine.rs
File metadata and controls
288 lines (247 loc) · 9.16 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use anyhow::{anyhow, Result};
use rust_embed::RustEmbed;
use std::{collections::HashSet, io::Cursor, path::PathBuf};
use wasi_common::{I32Exit, WasiCtx};
use wasmtime::{AsContextMut, Config, Engine, Linker, Module, Store};
use crate::{
function_run_result::{
FunctionOutput::{self, InvalidJsonOutput, JsonOutput},
FunctionRunResult, InvalidOutput,
},
logs::LogStream,
};
#[derive(Clone)]
pub struct ProfileOpts {
pub interval: u32,
pub out: PathBuf,
}
#[derive(RustEmbed)]
#[folder = "providers/"]
struct StandardProviders;
fn import_modules(
module: &Module,
engine: &Engine,
linker: &mut Linker<WasiCtx>,
mut store: &mut Store<WasiCtx>,
) {
let imported_modules: HashSet<String> =
module.imports().map(|i| i.module().to_string()).collect();
imported_modules.iter().for_each(|imported_module| {
let imported_module_bytes = StandardProviders::get(&format!("{imported_module}.wasm"));
if let Some(bytes) = imported_module_bytes {
let imported_module = Module::from_binary(engine, &bytes.data)
.unwrap_or_else(|_| panic!("Failed to load module {imported_module}"));
let imported_module_instance = linker
.instantiate(&mut store, &imported_module)
.expect("Failed to instantiate imported instance");
linker
.instance(
&mut store,
"javy_quickjs_provider_v1",
imported_module_instance,
)
.expect("Failed to import module");
}
});
}
pub fn run(
function_path: PathBuf,
input: Vec<u8>,
export: &str,
profile_opts: Option<&ProfileOpts>,
) -> Result<FunctionRunResult> {
let engine = Engine::new(
Config::new()
.wasm_multi_memory(true)
.consume_fuel(true)
.epoch_interruption(true),
)?;
let module = Module::from_file(&engine, &function_path)
.map_err(|e| anyhow!("Couldn't load the Function {:?}: {}", &function_path, e))?;
let input_stream = wasi_common::pipe::ReadPipe::new(Cursor::new(input));
let output_stream = wasi_common::pipe::WritePipe::new_in_memory();
let error_stream = wasi_common::pipe::WritePipe::new(LogStream::default());
let memory_usage: u64;
let instructions: u64;
let mut error_logs: String = String::new();
let profile_data: Option<String>;
{
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
let wasi = deterministic_wasi_ctx::build_wasi_ctx();
wasi.set_stdin(Box::new(input_stream));
wasi.set_stdout(Box::new(output_stream.clone()));
wasi.set_stderr(Box::new(error_stream.clone()));
let mut store = Store::new(&engine, wasi);
store.add_fuel(u64::MAX)?;
store.set_epoch_deadline(1);
import_modules(&module, &engine, &mut linker, &mut store);
linker.module(&mut store, "Function", &module)?;
let instance = linker.instantiate(&mut store, &module)?;
let func = instance.get_typed_func::<(), ()>(store.as_context_mut(), export)?;
let module_result;
(module_result, profile_data) = if let Some(profile_opts) = profile_opts {
let (result, profile_data) = wasmprof::ProfilerBuilder::new(&mut store)
.frequency(profile_opts.interval)
.weight_unit(wasmprof::WeightUnit::Fuel)
.profile(|store| func.call(store.as_context_mut(), ()));
(
result,
Some(profile_data.into_collapsed_stacks().to_string()),
)
} else {
(func.call(store.as_context_mut(), ()), None)
};
// modules may exit with a specific exit code, an exit code of 0 is considered success but is reported as
// a GuestFault by wasmtime, so we need to map it to a success result. Any other exit code is considered
// a failure.
let module_result =
module_result.or_else(|error| match error.downcast_ref::<wasi_common::I32Exit>() {
Some(I32Exit(0)) => Ok(()),
Some(I32Exit(code)) => Err(anyhow!("module exited with code: {}", code)),
None => Err(error),
});
// This is a hack to get the memory usage. Wasmtime requires a mutable borrow to a store for caching.
// We need this mutable borrow to fall out of scope so that we can measure memory usage.
// https://docs.rs/wasmtime/0.37.0/wasmtime/struct.Instance.html#why-does-get_export-take-a-mutable-context
let memory_names: Vec<String> = instance
.exports(&mut store)
.filter(|export| export.clone().into_memory().is_some())
.map(|export| export.name().to_string())
.collect();
memory_usage = memory_names
.iter()
.map(|name| {
let memory = instance.get_memory(&mut store, name).unwrap();
memory.data_size(&store) as u64
})
.sum::<u64>()
/ 1024;
instructions = store.fuel_consumed().unwrap_or_default();
match module_result {
Ok(_) => {}
Err(e) => {
error_logs = e.to_string();
}
}
};
let mut logs = error_stream
.try_into_inner()
.expect("Log stream reference still exists");
logs.append(error_logs.as_bytes())
.expect("Couldn't append error logs");
let raw_output = output_stream
.try_into_inner()
.expect("Output stream reference still exists")
.into_inner();
let output: FunctionOutput = match serde_json::from_slice(&raw_output) {
Ok(json_output) => JsonOutput(json_output),
Err(error) => InvalidJsonOutput(InvalidOutput {
stdout: std::str::from_utf8(&raw_output)
.map_err(|e| anyhow!("Couldn't print Function Output: {}", e))
.unwrap()
.to_owned(),
error: error.to_string(),
}),
};
let name = function_path.file_name().unwrap().to_str().unwrap();
let size = function_path.metadata()?.len() / 1024;
let function_run_result = FunctionRunResult::new(
name.to_string(),
size,
memory_usage,
instructions,
logs.to_string(),
output,
profile_data,
);
Ok(function_run_result)
}
#[cfg(test)]
mod tests {
use colored::Colorize;
use super::*;
use std::path::Path;
const LINEAR_MEMORY_USAGE: u64 = 159 * 64;
const DEFAULT_EXPORT: &str = "_start";
#[test]
fn test_js_function() {
let input = include_bytes!("../benchmark/build/js_function_input.json").to_vec();
let function_run_result = run(
Path::new("benchmark/build/js_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
);
assert!(function_run_result.is_ok());
}
#[test]
fn test_exit_code_zero() {
let input = include_bytes!("../benchmark/build/product_discount.json").to_vec();
let function_run_result = run(
Path::new("benchmark/build/exit_code_function_zero.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
assert_eq!(function_run_result.logs, "");
}
#[test]
fn test_exit_code_one() {
let input = include_bytes!("../benchmark/build/product_discount.json").to_vec();
let function_run_result = run(
Path::new("benchmark/build/exit_code_function_one.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
assert_eq!(function_run_result.logs, "module exited with code: 1");
}
#[test]
fn test_linear_memory_usage_in_kb() {
let input = include_bytes!("../benchmark/build/product_discount.json").to_vec();
let function_run_result = run(
Path::new("benchmark/build/linear_memory_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
assert_eq!(function_run_result.memory_usage, LINEAR_MEMORY_USAGE);
}
#[test]
fn test_logs_truncation() {
let input = "{}".as_bytes().to_vec();
let function_run_result = run(
Path::new("benchmark/build/log_truncation_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
assert!(function_run_result
.logs
.contains(&"...[TRUNCATED]".red().to_string()));
}
#[test]
fn test_file_size_in_kb() {
let input = include_bytes!("../benchmark/build/product_discount.json").to_vec();
let function_run_result = run(
Path::new("benchmark/build/size_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
assert_eq!(
function_run_result.size,
Path::new("benchmark/build/size_function.wasm")
.metadata()
.unwrap()
.len()
/ 1024
);
}
}