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
use std::fmt::Debug;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use crate::plugin::Plugin;
#[derive(Clone, Serialize, Deserialize)]
pub struct EnvConfig {
max_memory: usize,
max_fuel: Option<u64>,
allowed_namespaces: Vec<String>,
preopened_dirs: Vec<String>,
#[serde(skip)]
plugins: Vec<Plugin>,
wasi_args: Option<Vec<String>>,
wasi_envs: Option<Vec<(String, String)>>,
}
impl Debug for EnvConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
f.debug_struct("EnvConfig")
.field("max_memory", &self.max_memory)
.field("max_fuel", &self.max_fuel)
.field("allowed_namespaces", &self.allowed_namespaces)
.field("preopened_dirs", &self.preopened_dirs)
.field("wasi_args", &self.wasi_args)
.field("wasi_envs", &self.wasi_envs)
.finish()
}
}
impl EnvConfig {
pub fn new(max_memory: usize, max_fuel: Option<u64>) -> Self {
Self {
max_memory,
max_fuel,
allowed_namespaces: Vec::new(),
preopened_dirs: Vec::new(),
plugins: Vec::new(),
wasi_args: None,
wasi_envs: None,
}
}
pub fn max_memory(&self) -> usize {
self.max_memory
}
pub fn max_fuel(&self) -> Option<u64> {
self.max_fuel
}
pub fn allowed_namespace(&self) -> &[String] {
&self.allowed_namespaces
}
pub fn allow_namespace<S: Into<String>>(&mut self, namespace: S) {
self.allowed_namespaces.push(namespace.into())
}
pub fn preopened_dirs(&self) -> &[String] {
&self.preopened_dirs
}
pub fn preopen_dir<S: Into<String>>(&mut self, dir: S) {
self.preopened_dirs.push(dir.into())
}
pub fn plugins(&self) -> &Vec<Plugin> {
&self.plugins
}
pub fn add_plugin(&mut self, module: Vec<u8>) -> Result<()> {
let plugin = Plugin::new(module)?;
self.plugins.push(plugin);
Ok(())
}
pub fn set_wasi_args(&mut self, args: Vec<String>) {
self.wasi_args = Some(args);
}
pub fn wasi_args(&self) -> &Option<Vec<String>> {
&self.wasi_args
}
pub fn set_wasi_envs(&mut self, envs: Vec<(String, String)>) {
self.wasi_envs = Some(envs);
}
pub fn wasi_envs(&self) -> &Option<Vec<(String, String)>> {
&self.wasi_envs
}
}
impl Default for EnvConfig {
fn default() -> Self {
Self {
max_memory: 0xA00000000,
max_fuel: None,
allowed_namespaces: vec![
String::from("lunatic::"),
String::from("wasi_snapshot_preview1::"),
],
preopened_dirs: vec![],
plugins: vec![],
wasi_args: None,
wasi_envs: None,
}
}
}