forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm_component_runtime.rs
More file actions
339 lines (309 loc) · 10.6 KB
/
Copy pathwasm_component_runtime.rs
File metadata and controls
339 lines (309 loc) · 10.6 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use crate::types::{NuError, NuResult, Span};
#[cfg(feature = "wasm-backend")]
use wasmtime::component::*;
#[cfg(feature = "wasm-backend")]
use wasmtime::*;
#[cfg(feature = "wasm-backend")]
mod bindings {
wasmtime::component::bindgen!({
world: "actor",
path: "wit/actor.wit",
});
}
#[cfg(feature = "wasm-backend")]
use bindings::host::Host as HostTrait;
#[cfg(feature = "wasm-backend")]
use bindings::Actor;
#[cfg(feature = "wasm-backend")]
pub fn component_config() -> Config {
let mut config = Config::new();
config.wasm_component_model(true);
config.memory_reservation(4 << 30);
config.memory_guard_size(128 << 20);
config.cranelift_opt_level(OptLevel::Speed);
config.wasm_simd(true);
config
}
/// Capability controls for the WASM component host.
///
/// Each flag determines whether a given host function is made available to
/// the guest component. If a flag is `false` and the component imports the
/// corresponding function, instantiation will fail at the linker level.
#[cfg(feature = "wasm-backend")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Capabilities {
pub allow_log: bool,
pub allow_clock: bool,
pub allow_random: bool,
}
#[cfg(feature = "wasm-backend")]
impl Default for Capabilities {
fn default() -> Self {
Capabilities {
allow_log: false,
allow_clock: false,
allow_random: false,
}
}
}
#[cfg(feature = "wasm-backend")]
pub struct HostState {
caps: Capabilities,
log_messages: Vec<String>,
}
#[cfg(feature = "wasm-backend")]
impl HostTrait for HostState {
fn log(&mut self, msg: String) {
if self.caps.allow_log {
self.log_messages.push(msg);
}
}
fn clock_now(&mut self) -> u64 {
if self.caps.allow_clock {
0
} else {
panic!("clock capability denied")
}
}
fn random_u64(&mut self) -> u64 {
if self.caps.allow_random {
0
} else {
panic!("random capability denied")
}
}
}
#[cfg(feature = "wasm-backend")]
pub struct ComponentRuntime {
_engine: Engine,
component: Component,
config: Config,
caps: Capabilities,
}
#[cfg(feature = "wasm-backend")]
impl ComponentRuntime {
pub fn new(wasm_bytes: &[u8]) -> NuResult<Self> {
Self::new_with_caps(wasm_bytes, Capabilities::default())
}
pub fn new_with_caps(wasm_bytes: &[u8], caps: Capabilities) -> NuResult<Self> {
let config = component_config();
let engine = Engine::new(&config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let component = Component::new(&engine, wasm_bytes).map_err(|e| NuError::VMError {
msg: format!("wasmtime component: {}", e),
span: Span::default(),
})?;
Ok(ComponentRuntime {
_engine: engine,
component,
config,
caps,
})
}
fn build_linker(&self, engine: &Engine) -> NuResult<wasmtime::component::Linker<HostState>> {
let mut linker = wasmtime::component::Linker::<HostState>::new(engine);
// Add the host instance manually with the correct name
if self.caps.allow_log || self.caps.allow_clock || self.caps.allow_random {
let mut inst =
linker
.instance("nulang:runtime/host")
.map_err(|e| NuError::VMError {
msg: format!("wasmtime linker: {}", e),
span: Span::default(),
})?;
if self.caps.allow_log {
inst.func_wrap(
"log",
move |mut caller: wasmtime::StoreContextMut<'_, HostState>,
(msg,): (String,)| {
caller.data_mut().log_messages.push(msg);
Ok(())
},
)
.map_err(|e| NuError::VMError {
msg: format!("wasmtime linker: {}", e),
span: Span::default(),
})?;
}
if self.caps.allow_clock {
inst.func_wrap(
"clock-now",
move |mut caller: wasmtime::StoreContextMut<'_, HostState>, _: ()| {
let r = caller.data_mut().clock_now();
Ok((r,))
},
)
.map_err(|e| NuError::VMError {
msg: format!("wasmtime linker: {}", e),
span: Span::default(),
})?;
}
if self.caps.allow_random {
inst.func_wrap(
"random-u64",
move |mut caller: wasmtime::StoreContextMut<'_, HostState>, _: ()| {
let r = caller.data_mut().random_u64();
Ok((r,))
},
)
.map_err(|e| NuError::VMError {
msg: format!("wasmtime linker: {}", e),
span: Span::default(),
})?;
}
}
Ok(linker)
}
pub fn init(&self) -> NuResult<i64> {
let engine = Engine::new(&self.config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let mut store = Store::new(
&engine,
HostState {
caps: self.caps,
log_messages: Vec::new(),
},
);
let linker = self.build_linker(&engine)?;
let actor = Actor::instantiate(&mut store, &self.component, &linker).map_err(|e| {
NuError::VMError {
msg: format!("wasmtime instantiate: {}", e),
span: Span::default(),
}
})?;
actor.call_init(&mut store).map_err(|e| NuError::VMError {
msg: format!("wasmtime call_init: {}", e),
span: Span::default(),
})
}
pub fn handle_message(&self, msg: &[u8]) -> NuResult<i64> {
let engine = Engine::new(&self.config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let mut store = Store::new(
&engine,
HostState {
caps: self.caps,
log_messages: Vec::new(),
},
);
let linker = self.build_linker(&engine)?;
let actor = Actor::instantiate(&mut store, &self.component, &linker).map_err(|e| {
NuError::VMError {
msg: format!("wasmtime instantiate: {}", e),
span: Span::default(),
}
})?;
actor
.call_handle_message(&mut store, msg)
.map_err(|e| NuError::VMError {
msg: format!("wasmtime call_handle_message: {}", e),
span: Span::default(),
})
}
pub fn checkpoint(&self) -> NuResult<Vec<u8>> {
let engine = Engine::new(&self.config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let mut store = Store::new(
&engine,
HostState {
caps: self.caps,
log_messages: Vec::new(),
},
);
let linker = self.build_linker(&engine)?;
let actor = Actor::instantiate(&mut store, &self.component, &linker).map_err(|e| {
NuError::VMError {
msg: format!("wasmtime instantiate: {}", e),
span: Span::default(),
}
})?;
actor
.call_checkpoint(&mut store)
.map_err(|e| NuError::VMError {
msg: format!("wasmtime call_checkpoint: {}", e),
span: Span::default(),
})
}
}
#[cfg(test)]
#[cfg(feature = "wasm-backend")]
mod tests {
use super::*;
/// Minimal WAT component that imports `log` and exports `init`.
const LOG_IMPORT_WAT: &str = r#"
(component
(import "nulang:runtime/host" (instance $host
(export "log" (func (param "msg" string)))
))
)
"#;
#[test]
fn test_component_capability_gate_denies_log() {
let wasm = wat::parse_str(LOG_IMPORT_WAT).expect("parse WAT");
let rt = ComponentRuntime::new_with_caps(
&wasm,
Capabilities {
allow_log: false,
allow_clock: false,
allow_random: false,
},
)
.expect("new_with_caps");
// Direct instantiation with the linker should fail because the host
// interface is not added when allow_log is false.
let engine = wasmtime::Engine::new(&rt.config).expect("engine");
let mut store = wasmtime::Store::new(
&engine,
HostState {
caps: rt.caps,
log_messages: Vec::new(),
},
);
let linker = rt.build_linker(&engine).expect("linker");
let component = wasmtime::component::Component::new(&engine, &wasm).expect("component");
let err = linker
.instantiate(&mut store, &component)
.expect_err("should fail to instantiate without log capability");
assert!(
err.to_string().contains("host"),
"error should mention missing host import: {}",
err
);
}
#[test]
fn test_component_capability_gate_allows_log() {
let wasm = wat::parse_str(LOG_IMPORT_WAT).expect("parse WAT");
let rt = ComponentRuntime::new_with_caps(
&wasm,
Capabilities {
allow_log: true,
allow_clock: false,
allow_random: false,
},
)
.expect("new_with_caps");
// Direct instantiation with the linker should succeed because the host
// interface is added when allow_log is true.
let engine = wasmtime::Engine::new(&rt.config).expect("engine");
let mut store = wasmtime::Store::new(
&engine,
HostState {
caps: rt.caps,
log_messages: Vec::new(),
},
);
let linker = rt.build_linker(&engine).expect("linker");
let component = wasmtime::component::Component::new(&engine, &wasm).expect("component");
let _instance = linker
.instantiate(&mut store, &component)
.expect("should succeed with log capability");
}
}