-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm_runtime.rs
More file actions
1311 lines (1167 loc) · 50.4 KB
/
wasm_runtime.rs
File metadata and controls
1311 lines (1167 loc) · 50.4 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! WASM runtime — wasmtime-based sandboxed execution for openSystem apps.
//!
//! Each app runs in an isolated Wasmtime instance with:
//! - A memory limit (64 MiB enforced by WASM linear memory constraints)
//! - Controlled host function exports (`__opensystem_*` stubs for MVP)
//! - stdout/stderr captured via WASI MemoryOutputPipe
//!
//! # API compatibility: wasmtime 42
//! Uses `wasmtime_wasi::p1` (WASIp1 / wasm32-wasip1 target), `WasiP1Ctx` as
//! store state, and `MemoryOutputPipe` from `wasmtime_wasi::p2::pipe`.
use anyhow::{bail, Result};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use wasmtime::{Engine, Linker, Module, Store, Trap};
use wasmtime_wasi::{WasiCtxBuilder, p1, p1::WasiP1Ctx, p2::pipe::MemoryOutputPipe};
/// 64 MiB stdout/stderr capture capacity per execution.
const PIPE_CAPACITY: usize = 64 * 1024 * 1024;
/// Maximum storage value size: 1 MiB.
const MAX_STORAGE_VALUE_SIZE: usize = 1024 * 1024;
/// WASM epoch interruption deadline (ticks). With a 1-second tick interval,
/// this gives apps 30 seconds of execution time before being interrupted.
const EPOCH_DEADLINE: u64 = 30;
/// Maximum HTTP response body size: 4 MiB.
const MAX_HTTP_RESPONSE_SIZE: usize = 4 * 1024 * 1024;
/// Maximum number of concurrent timers per WASM execution.
const MAX_TIMERS: usize = 64;
/// HTTP request timeout in seconds.
const HTTP_TIMEOUT_SECS: u64 = 10;
/// RAII guard for the epoch ticker background thread.
///
/// When dropped, signals the background thread to stop and waits for it to
/// finish. This ensures the ticker thread is cleaned up even on panic/early return.
struct EpochTicker {
done: Arc<AtomicBool>,
handle: Option<std::thread::JoinHandle<()>>,
}
impl EpochTicker {
fn start(engine: Engine) -> Self {
let done = Arc::new(AtomicBool::new(false));
let done_clone = done.clone();
let handle = std::thread::spawn(move || {
while !done_clone.load(Ordering::Relaxed) {
std::thread::sleep(std::time::Duration::from_secs(1));
engine.increment_epoch();
}
});
Self { done, handle: Some(handle) }
}
}
impl Drop for EpochTicker {
fn drop(&mut self) {
self.done.store(true, Ordering::Relaxed);
if let Some(h) = self.handle.take() {
let _ = h.join();
}
}
}
/// A registered timer entry tracking the background thread and its cancellation flag.
struct TimerEntry {
done: Arc<AtomicBool>,
handle: Option<std::thread::JoinHandle<()>>,
}
impl Drop for TimerEntry {
fn drop(&mut self) {
self.done.store(true, Ordering::Relaxed);
if let Some(h) = self.handle.take() {
let _ = h.join();
}
}
}
/// Thread-safe timer registry shared between host functions and the runtime.
///
/// Timers record their callback name and a "fired" flag. The WASM host polls
/// fired timers on each interval tick and invokes the stored callback name.
/// Since calling back into WASM from a background thread is unsafe with
/// wasmtime's single-threaded Store, we use a polling model: background threads
/// mark timers as "fired", and the next WASM host function invocation can check.
#[derive(Clone)]
struct TimerRegistry {
inner: Arc<Mutex<TimerRegistryInner>>,
}
struct TimerRegistryInner {
next_id: u64,
timers: HashMap<u64, TimerEntry>,
/// Callback names for each timer, keyed by timer_id.
callbacks: HashMap<u64, String>,
/// Timer IDs that have fired at least once (polling model).
fired: Vec<u64>,
}
impl TimerRegistry {
fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(TimerRegistryInner {
next_id: 1,
timers: HashMap::new(),
callbacks: HashMap::new(),
fired: Vec::new(),
})),
}
}
/// Register a new interval timer. Returns the timer_id, or 0 if limit reached.
fn set_interval(&self, interval_ms: u64, callback_name: String) -> u64 {
let mut inner = self.inner.lock().unwrap();
if inner.timers.len() >= MAX_TIMERS {
tracing::warn!("[host] timer_set_interval: max timer limit ({}) reached", MAX_TIMERS);
return 0;
}
if interval_ms == 0 {
tracing::warn!("[host] timer_set_interval: interval_ms must be > 0");
return 0;
}
let id = inner.next_id;
inner.next_id += 1;
let done = Arc::new(AtomicBool::new(false));
let done_clone = done.clone();
let registry_ref = self.inner.clone();
let timer_id = id;
let handle = std::thread::spawn(move || {
while !done_clone.load(Ordering::Relaxed) {
// Sleep in 10ms chunks so we can respond to cancellation quickly
let mut remaining = interval_ms;
while remaining > 0 && !done_clone.load(Ordering::Relaxed) {
let chunk = remaining.min(10);
std::thread::sleep(std::time::Duration::from_millis(chunk));
remaining = remaining.saturating_sub(chunk);
}
if done_clone.load(Ordering::Relaxed) {
break;
}
if let Ok(mut reg) = registry_ref.lock() {
reg.fired.push(timer_id);
}
}
});
inner.callbacks.insert(id, callback_name);
inner.timers.insert(id, TimerEntry { done, handle: Some(handle) });
tracing::debug!("[host] timer_set_interval: id={} interval={}ms", id, interval_ms);
id
}
/// Clear (cancel) a timer by id. Returns true if it existed.
///
/// Signals the timer thread to stop but does not block waiting for it.
/// The thread will exit on its next 10ms check cycle.
fn clear(&self, timer_id: u64) -> bool {
let mut inner = self.inner.lock().unwrap();
inner.callbacks.remove(&timer_id);
inner.fired.retain(|&id| id != timer_id);
if let Some(entry) = inner.timers.remove(&timer_id) {
entry.done.store(true, Ordering::Relaxed);
// Don't join — let the thread exit asynchronously.
// The thread handle is dropped here, which detaches it.
tracing::debug!("[host] timer_clear: id={}", timer_id);
// Prevent Drop from joining (which would block)
std::mem::forget(entry);
true
} else {
false
}
}
/// Drain all fired timer IDs and their callback names.
#[allow(dead_code)]
fn drain_fired(&self) -> Vec<(u64, String)> {
let mut inner = self.inner.lock().unwrap();
let fired: Vec<u64> = inner.fired.drain(..).collect();
fired
.into_iter()
.filter_map(|id| {
inner.callbacks.get(&id).map(|cb| (id, cb.clone()))
})
.collect()
}
/// Return the number of active timers.
#[allow(dead_code)]
fn len(&self) -> usize {
self.inner.lock().unwrap().timers.len()
}
/// Check if a timer exists.
#[allow(dead_code)]
fn contains(&self, timer_id: u64) -> bool {
self.inner.lock().unwrap().timers.contains_key(&timer_id)
}
}
/// Output captured from a WASM execution.
#[derive(Debug, Default)]
pub struct WasmOutput {
/// Lines written to stdout by the WASM module.
pub stdout: Vec<String>,
/// Lines written to stderr by the WASM module.
pub stderr: Vec<String>,
}
/// Wasmtime-based sandbox runtime for openSystem apps.
///
/// Create once and reuse — the `Engine` is expensive to initialize.
pub struct WasmRuntime {
engine: Engine,
}
/// Validate a storage key: must be non-empty, no path traversal, no slashes.
pub fn validate_storage_key(key: &str) -> bool {
!key.is_empty()
&& !key.contains('/')
&& !key.contains('\\')
&& !key.contains("..")
&& key.len() <= 255
&& key.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '.')
}
/// Get the storage directory for a given app_id.
pub fn storage_dir_for_app(app_id: &str) -> PathBuf {
let base = std::env::var("OPENSYSTEM_STORAGE_DIR")
.unwrap_or_else(|_| {
let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
format!("{}/.opensystem/storage", home)
});
PathBuf::from(base).join(app_id)
}
impl WasmRuntime {
/// Initialise the wasmtime engine with epoch interruption enabled.
pub fn new() -> Result<Self> {
let mut config = wasmtime::Config::new();
config.epoch_interruption(true);
let engine = Engine::new(&config)?;
Ok(Self { engine })
}
/// Execute a `.wasm` file and return captured output.
///
/// The function calls `_start` (WASI) or `main` if present.
/// Non-zero exit codes via WASI `proc_exit` are treated as errors.
pub fn execute(&self, wasm_path: &Path) -> Result<WasmOutput> {
if !wasm_path.exists() {
bail!("WASM file not found: {}", wasm_path.display());
}
let stdout_pipe = MemoryOutputPipe::new(PIPE_CAPACITY);
let stderr_pipe = MemoryOutputPipe::new(PIPE_CAPACITY);
// Build WASIp1 context with captured stdout/stderr.
let wasi_ctx = WasiCtxBuilder::new()
.stdout(stdout_pipe.clone())
.stderr(stderr_pipe.clone())
.build_p1();
let mut linker: Linker<WasiP1Ctx> = Linker::new(&self.engine);
// Populate all WASI snapshot_preview1 imports.
p1::add_to_linker_sync(&mut linker, |t| t)
.map_err(|e| anyhow::anyhow!("failed to add WASI p1 to linker: {}", e))?;
// Register openSystem host functions.
let timer_registry = TimerRegistry::new();
self.register_host_functions(&mut linker, &timer_registry)?;
let mut store = Store::new(&self.engine, wasi_ctx);
store.set_epoch_deadline(EPOCH_DEADLINE);
// RAII guard: ticker thread stops automatically when _ticker is dropped.
let _ticker = EpochTicker::start(self.engine.clone());
let wasm_bytes = std::fs::read(wasm_path)
.map_err(|e| anyhow::anyhow!("failed to read {}: {}", wasm_path.display(), e))?;
let module = Module::from_binary(&self.engine, &wasm_bytes)
.map_err(|e| anyhow::anyhow!("failed to compile WASM module: {}", e))?;
let instance = linker
.instantiate(&mut store, &module)
.map_err(|e| anyhow::anyhow!("failed to instantiate WASM module: {}", e))?;
// Call _start (WASI entry point).
let result = match instance.get_func(&mut store, "_start") {
Some(f) => f.call(&mut store, &[], &mut []),
None => {
// Fallback: try "main" export (non-WASI).
match instance.get_func(&mut store, "main") {
Some(f) => f.call(&mut store, &[], &mut []),
None => bail!("WASM module exports neither '_start' nor 'main'"),
}
}
};
// WASI proc_exit(0) appears as I32Exit(0) — treat as success.
if let Err(ref e) = result {
if let Some(exit) = e.downcast_ref::<wasmtime_wasi::I32Exit>() {
if exit.0 != 0 {
bail!("WASM exited with non-zero status: {}", exit.0);
}
// exit(0) — normal exit, continue below
} else if e.downcast_ref::<Trap>() == Some(&Trap::Interrupt) {
bail!("WASM execution interrupted: exceeded time limit of {} seconds", EPOCH_DEADLINE);
} else {
return Err(anyhow::anyhow!("WASM execution trapped: {}", result.unwrap_err()));
}
}
let stdout = bytes_to_lines(&stdout_pipe.contents());
let stderr = bytes_to_lines(&stderr_pipe.contents());
Ok(WasmOutput { stdout, stderr })
}
/// Register `__opensystem_*` host functions that the WASM app may import.
///
/// Timer and notification host functions are fully implemented.
/// UI rendering remains a stub for now.
fn register_host_functions(&self, linker: &mut Linker<WasiP1Ctx>, timer_registry: &TimerRegistry) -> Result<()> {
let timer_registry = timer_registry.clone();
// UI render: spec_ptr + spec_len → handle id
linker
.func_wrap(
"env",
"__opensystem_ui_render",
|_spec_ptr: i32, _spec_len: i32| -> i64 {
tracing::debug!("[host] __opensystem_ui_render (stub)");
0i64
},
)
.map_err(|e| anyhow::anyhow!("register ui_render: {}", e))?;
// UI update: handle + diff_ptr + diff_len → ()
linker
.func_wrap(
"env",
"__opensystem_ui_update",
|_handle: i64, _diff_ptr: i32, _diff_len: i32| {
tracing::debug!("[host] __opensystem_ui_update (stub)");
},
)
.map_err(|e| anyhow::anyhow!("register ui_update: {}", e))?;
// Timer set_interval: interval_ms + callback_name_ptr + callback_name_len → timer_id
//
// Registers a periodic timer. The callback_name is a UTF-8 string in WASM memory
// naming the exported WASM function to invoke on each tick. Due to wasmtime's
// single-threaded Store model, callbacks are recorded via a polling mechanism:
// the background thread marks the timer as "fired", and fired timers can be
// queried via the timer registry.
//
// Returns a non-zero timer_id on success, 0 on failure.
{
let timers = timer_registry.clone();
linker
.func_wrap(
"env",
"__opensystem_timer_set_interval",
move |mut caller: wasmtime::Caller<'_, WasiP1Ctx>,
interval_ms: i64,
cb_name_ptr: i32,
cb_name_len: i32|
-> i64 {
if interval_ms <= 0 {
tracing::warn!("[host] timer_set_interval: interval must be > 0");
return 0;
}
let mem = match caller.get_export("memory") {
Some(wasmtime::Extern::Memory(m)) => m,
_ => return 0,
};
let data = mem.data(&caller);
let start = cb_name_ptr as usize;
let end = start + cb_name_len as usize;
if end > data.len() {
return 0;
}
let cb_name = match std::str::from_utf8(&data[start..end]) {
Ok(s) => s.to_string(),
Err(_) => return 0,
};
timers.set_interval(interval_ms as u64, cb_name) as i64
},
)
.map_err(|e| anyhow::anyhow!("register timer_set_interval: {}", e))?;
}
// Timer clear: timer_id → 1 (cleared) / 0 (not found)
{
let timers = timer_registry.clone();
linker
.func_wrap(
"env",
"__opensystem_timer_clear",
move |timer_id: i64| -> i32 {
if timers.clear(timer_id as u64) { 1 } else { 0 }
},
)
.map_err(|e| anyhow::anyhow!("register timer_clear: {}", e))?;
}
// __opensystem_storage_read — read a value from persistent key-value storage.
//
// # Parameters
// - `key_ptr` : i32 — byte offset in WASM linear memory of the key string (UTF-8)
// - `key_len` : i32 — length in bytes of the key string
// - `out_len_ptr`: i32 — byte offset in WASM linear memory where the host will write
// the result length as a little-endian i32 (4 bytes)
//
// # Return value
// - 0 on error (key not found, invalid key, out-of-bounds pointer, I/O error)
// - Non-zero: byte offset in WASM linear memory where the response data begins.
//
// # Memory layout written by the host
//
// ```
// out_len_ptr+0 .. out_len_ptr+4 : i32 LE — number of bytes in the value
// out_len_ptr+4 .. out_len_ptr+4+N: u8[N] — value bytes (N = value length)
// ```
//
// The caller must allocate at least `4 + value_length` bytes starting at
// `out_len_ptr`. The return value equals `out_len_ptr + 4` when successful.
//
// # Key constraints
// Keys must satisfy `validate_storage_key`: ASCII alphanumeric plus `_`, `-`, `.`;
// no slashes, no `..`, max 255 bytes.
//
// # Storage path
// Values are stored as files at `OPENSYSTEM_STORAGE_DIR/<app_id>/<key>`.
// Currently uses a fixed "default" app_id until per-app execution is wired.
linker
.func_wrap(
"env",
"__opensystem_storage_read",
|mut caller: wasmtime::Caller<'_, WasiP1Ctx>,
key_ptr: i32,
key_len: i32,
out_len_ptr: i32|
-> i32 {
let mem = match caller.get_export("memory") {
Some(wasmtime::Extern::Memory(m)) => m,
_ => return 0,
};
let data = mem.data(&caller);
let key_start = key_ptr as usize;
let key_end = key_start + key_len as usize;
if key_end > data.len() {
return 0;
}
let key = match std::str::from_utf8(&data[key_start..key_end]) {
Ok(k) => k.to_string(),
Err(_) => return 0,
};
if !validate_storage_key(&key) {
tracing::warn!("[host] storage_read: invalid key '{}'", key);
return 0;
}
let app_id = "default";
let path = storage_dir_for_app(app_id).join(&key);
let contents = match std::fs::read(&path) {
Ok(c) => c,
Err(_) => return 0,
};
if contents.len() > MAX_STORAGE_VALUE_SIZE {
tracing::warn!("[host] storage_read: value too large for key '{}'", key);
return 0;
}
// Write length to out_len_ptr
let out_len_off = out_len_ptr as usize;
let len_bytes = (contents.len() as i32).to_le_bytes();
let data_mut = mem.data_mut(&mut caller);
if out_len_off + 4 > data_mut.len() {
return 0;
}
data_mut[out_len_off..out_len_off + 4].copy_from_slice(&len_bytes);
// Write data right after out_len_ptr + 4 (simple linear alloc region)
let data_off = out_len_off + 4;
if data_off + contents.len() > data_mut.len() {
return 0;
}
data_mut[data_off..data_off + contents.len()].copy_from_slice(&contents);
tracing::debug!(
"[host] storage_read: key='{}' len={}",
key,
contents.len()
);
data_off as i32
},
)
.map_err(|e| anyhow::anyhow!("register storage_read: {}", e))?;
// Storage write: key_ptr + key_len + val_ptr + val_len → 1 (success) / 0 (failure)
//
// Writes to ~/.opensystem/storage/<app_id>/<key>.
linker
.func_wrap(
"env",
"__opensystem_storage_write",
|mut caller: wasmtime::Caller<'_, WasiP1Ctx>,
key_ptr: i32,
key_len: i32,
val_ptr: i32,
val_len: i32|
-> i32 {
let mem = match caller.get_export("memory") {
Some(wasmtime::Extern::Memory(m)) => m,
_ => return 0,
};
let data = mem.data(&caller);
let key_start = key_ptr as usize;
let key_end = key_start + key_len as usize;
if key_end > data.len() {
return 0;
}
let key = match std::str::from_utf8(&data[key_start..key_end]) {
Ok(k) => k.to_string(),
Err(_) => return 0,
};
if !validate_storage_key(&key) {
tracing::warn!("[host] storage_write: invalid key '{}'", key);
return 0;
}
let val_start = val_ptr as usize;
let val_end = val_start + val_len as usize;
if val_end > data.len() || val_len as usize > MAX_STORAGE_VALUE_SIZE {
return 0;
}
let value = data[val_start..val_end].to_vec();
let app_id = "default";
let dir = storage_dir_for_app(app_id);
if let Err(e) = std::fs::create_dir_all(&dir) {
tracing::warn!("[host] storage_write: failed to create dir: {}", e);
return 0;
}
let path = dir.join(&key);
if let Err(e) = std::fs::write(&path, &value) {
tracing::warn!("[host] storage_write: failed to write: {}", e);
return 0;
}
tracing::debug!(
"[host] storage_write: key='{}' len={}",
key,
value.len()
);
1i32
},
)
.map_err(|e| anyhow::anyhow!("register storage_write: {}", e))?;
// Notify send: title_ptr + title_len + body_ptr + body_len → 1 (success) / 0 (failure)
//
// Sends a desktop notification using `notify-send` on Linux.
// Falls back to printing to stdout on other platforms or if notify-send is unavailable.
linker
.func_wrap(
"env",
"__opensystem_notify_send",
|mut caller: wasmtime::Caller<'_, WasiP1Ctx>,
title_ptr: i32,
title_len: i32,
body_ptr: i32,
body_len: i32|
-> i32 {
let mem = match caller.get_export("memory") {
Some(wasmtime::Extern::Memory(m)) => m,
_ => return 0,
};
let data = mem.data(&caller);
let title_start = title_ptr as usize;
let title_end = title_start + title_len as usize;
if title_end > data.len() {
return 0;
}
let title = match std::str::from_utf8(&data[title_start..title_end]) {
Ok(s) => s.to_string(),
Err(_) => return 0,
};
let body_start = body_ptr as usize;
let body_end = body_start + body_len as usize;
if body_end > data.len() {
return 0;
}
let body = match std::str::from_utf8(&data[body_start..body_end]) {
Ok(s) => s.to_string(),
Err(_) => return 0,
};
tracing::debug!("[host] notify_send: title='{}' body='{}'", title, body);
// Try notify-send on Linux, fall back to println
if cfg!(target_os = "linux") {
match std::process::Command::new("notify-send")
.arg(&title)
.arg(&body)
.output()
{
Ok(output) if output.status.success() => {
return 1;
}
_ => {
// Fall through to println fallback
}
}
}
// Fallback: print to stdout
println!("[notification] {}: {}", title, body);
1
},
)
.map_err(|e| anyhow::anyhow!("register notify_send: {}", e))?;
// __opensystem_net_http_get — perform an HTTP GET and write the response body to WASM memory.
//
// # Parameters
// - `url_ptr` : i32 — byte offset in WASM memory of the URL string (UTF-8, must be https://)
// - `url_len` : i32 — length in bytes of the URL string
// - `out_len_ptr`: i32 — byte offset where the host writes the response body length (i32 LE, 4 bytes),
// followed immediately by the response body bytes.
// - `err_len_ptr`: i32 — byte offset where the host writes the error message length (i32 LE, 4 bytes),
// followed immediately by the UTF-8 error string (if error occurred).
//
// # Return value
// - 0 on error. The error message is written at `err_len_ptr`.
// - Non-zero: `out_len_ptr + 4` — byte offset where the response body begins.
//
// # Memory layout (success)
// ```
// out_len_ptr+0 .. out_len_ptr+4 : i32 LE — response body length N
// out_len_ptr+4 .. out_len_ptr+4+N : u8[N] — response body bytes
// ```
//
// # Security
// Only https:// URLs are allowed. HTTP is blocked to prevent downgrade attacks.
// The URL must have a valid host. Redirects are not followed.
// Response size is capped at 4 MiB (MAX_HTTP_RESPONSE_SIZE).
linker
.func_wrap(
"env",
"__opensystem_net_http_get",
|mut caller: wasmtime::Caller<'_, WasiP1Ctx>,
url_ptr: i32,
url_len: i32,
out_len_ptr: i32,
err_len_ptr: i32|
-> i32 {
let mem = match caller.get_export("memory") {
Some(wasmtime::Extern::Memory(m)) => m,
_ => return 0,
};
// Read URL from WASM memory.
let data = mem.data(&caller);
let url_start = url_ptr as usize;
let url_end = url_start.saturating_add(url_len as usize);
if url_end > data.len() {
return 0;
}
let url = match std::str::from_utf8(&data[url_start..url_end]) {
Ok(u) => u.to_string(),
Err(_) => return 0,
};
let _ = data;
// Helper: write an error string to err_len_ptr.
let write_error = |caller: &mut wasmtime::Caller<'_, WasiP1Ctx>, msg: &str| {
let mem = match caller.get_export("memory") {
Some(wasmtime::Extern::Memory(m)) => m,
_ => return,
};
let msg_bytes = msg.as_bytes();
let err_off = err_len_ptr as usize;
let data_mut = mem.data_mut(caller);
if err_off + 4 + msg_bytes.len() <= data_mut.len() {
let len_bytes = (msg_bytes.len() as i32).to_le_bytes();
data_mut[err_off..err_off + 4].copy_from_slice(&len_bytes);
data_mut[err_off + 4..err_off + 4 + msg_bytes.len()].copy_from_slice(msg_bytes);
}
};
// Validate: only https:// allowed.
let scheme_ok = url.starts_with("https://");
if !scheme_ok {
tracing::warn!("[host] net_http_get: rejected non-https URL");
write_error(&mut caller, "only https:// URLs are allowed");
return 0;
}
// Validate URL has a host.
match url::Url::parse(&url) {
Ok(parsed) if parsed.host().is_none() => {
write_error(&mut caller, "URL must have a host");
return 0;
}
Err(e) => {
write_error(&mut caller, &format!("invalid URL: {}", e));
return 0;
}
_ => {}
}
// Perform the HTTP GET (synchronous, no redirects).
let response = ureq::builder()
.timeout(std::time::Duration::from_secs(HTTP_TIMEOUT_SECS))
.redirects(0)
.build()
.get(&url)
.call();
let body_bytes = match response {
Err(e) => {
let msg = format!("HTTP GET failed: {}", e);
tracing::warn!("[host] net_http_get: {}", msg);
write_error(&mut caller, &msg);
return 0;
}
Ok(resp) => {
let mut buf = Vec::new();
let mut reader = resp.into_reader();
use std::io::Read;
let mut limited = (&mut reader).take(MAX_HTTP_RESPONSE_SIZE as u64 + 1);
if let Err(e) = limited.read_to_end(&mut buf) {
let msg = format!("failed to read response body: {}", e);
tracing::warn!("[host] net_http_get: {}", msg);
write_error(&mut caller, &msg);
return 0;
}
if buf.len() > MAX_HTTP_RESPONSE_SIZE {
let msg = format!("response exceeds {} byte limit", MAX_HTTP_RESPONSE_SIZE);
tracing::warn!("[host] net_http_get: {}", msg);
write_error(&mut caller, &msg);
return 0;
}
buf
}
};
// Write response to WASM memory at out_len_ptr.
let mem = match caller.get_export("memory") {
Some(wasmtime::Extern::Memory(m)) => m,
_ => return 0,
};
let out_off = out_len_ptr as usize;
let data_mut = mem.data_mut(&mut caller);
if out_off + 4 + body_bytes.len() > data_mut.len() {
return 0;
}
let len_bytes = (body_bytes.len() as i32).to_le_bytes();
data_mut[out_off..out_off + 4].copy_from_slice(&len_bytes);
data_mut[out_off + 4..out_off + 4 + body_bytes.len()].copy_from_slice(&body_bytes);
tracing::debug!("[host] net_http_get: url='{}' len={}", url, body_bytes.len());
(out_off + 4) as i32
},
)
.map_err(|e| anyhow::anyhow!("register net_http_get: {}", e))?;
Ok(())
}
}
impl Default for WasmRuntime {
fn default() -> Self {
Self::new().expect("failed to create WasmRuntime")
}
}
fn bytes_to_lines(bytes: &[u8]) -> Vec<String> {
String::from_utf8_lossy(bytes)
.lines()
.map(|l| l.to_string())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_runtime_new_succeeds() {
let rt = WasmRuntime::new();
assert!(rt.is_ok(), "WasmRuntime::new() should succeed");
}
#[test]
fn test_execute_nonexistent_file_fails() {
let rt = WasmRuntime::new().unwrap();
let result = rt.execute(Path::new("/nonexistent/path/app.wasm"));
assert!(result.is_err());
let msg = result.unwrap_err().to_string();
assert!(
msg.contains("not found") || msg.contains("No such file"),
"unexpected error: {}",
msg
);
}
#[test]
fn test_execute_invalid_bytes_fails() {
let mut f = NamedTempFile::new().unwrap();
f.write_all(b"this is not valid wasm").unwrap();
let rt = WasmRuntime::new().unwrap();
let result = rt.execute(f.path());
assert!(result.is_err(), "invalid wasm bytes should fail to compile");
}
/// Minimal no-op `_start` module — validates instantiation + execution path.
#[test]
fn test_execute_noop_start() {
let wat = r#"(module (func (export "_start")))"#;
let wasm_bytes = wat::parse_str(wat).expect("WAT parse failed");
let mut f = NamedTempFile::new().unwrap();
f.write_all(&wasm_bytes).unwrap();
let rt = WasmRuntime::new().unwrap();
let result = rt.execute(f.path());
assert!(
result.is_ok(),
"no-op _start should succeed: {:?}",
result.err()
);
}
#[test]
fn test_wasm_output_debug() {
let out = WasmOutput {
stdout: vec!["line 1".into()],
stderr: vec![],
};
let dbg = format!("{:?}", out);
assert!(dbg.contains("line 1"));
}
#[test]
fn test_bytes_to_lines_empty() {
let lines = bytes_to_lines(b"");
assert!(lines.is_empty());
}
#[test]
fn test_bytes_to_lines_multiline() {
let lines = bytes_to_lines(b"foo\nbar\nbaz");
assert_eq!(lines, vec!["foo", "bar", "baz"]);
}
#[test]
fn test_bytes_to_lines_trailing_newline() {
let lines = bytes_to_lines(b"hello\n");
assert_eq!(lines, vec!["hello"]);
}
#[test]
fn test_validate_storage_key_valid() {
assert!(validate_storage_key("my-key"));
assert!(validate_storage_key("my_key"));
assert!(validate_storage_key("settings.json"));
assert!(validate_storage_key("data123"));
}
#[test]
fn test_validate_storage_key_invalid() {
assert!(!validate_storage_key(""));
assert!(!validate_storage_key("../etc/passwd"));
assert!(!validate_storage_key("foo/bar"));
assert!(!validate_storage_key("foo\\bar"));
assert!(!validate_storage_key(".."));
assert!(!validate_storage_key("key with spaces"));
}
#[test]
fn test_validate_storage_key_too_long() {
let long_key = "a".repeat(256);
assert!(!validate_storage_key(&long_key));
let ok_key = "a".repeat(255);
assert!(validate_storage_key(&ok_key));
}
#[test]
fn test_storage_dir_for_app() {
std::env::set_var("OPENSYSTEM_STORAGE_DIR", "/tmp/test-storage");
let dir = storage_dir_for_app("my-app");
assert_eq!(dir, PathBuf::from("/tmp/test-storage/my-app"));
std::env::remove_var("OPENSYSTEM_STORAGE_DIR");
}
#[test]
fn test_storage_roundtrip_via_filesystem() {
// Test the storage functions directly via the filesystem (not through WASM)
let tmp = tempfile::TempDir::new().unwrap();
std::env::set_var("OPENSYSTEM_STORAGE_DIR", tmp.path().to_str().unwrap());
let app_dir = storage_dir_for_app("test-app");
std::fs::create_dir_all(&app_dir).unwrap();
let key = "test-key";
let value = b"hello world";
std::fs::write(app_dir.join(key), value).unwrap();
let read_back = std::fs::read(app_dir.join(key)).unwrap();
assert_eq!(read_back, value);
std::env::remove_var("OPENSYSTEM_STORAGE_DIR");
}
#[test]
fn test_epoch_interruption_config() {
// Verify the engine is created with epoch interruption enabled
let rt = WasmRuntime::new().unwrap();
// If epoch interruption were not enabled, set_epoch_deadline would panic.
// We verify by creating a store and setting the deadline.
let wasi_ctx = WasiCtxBuilder::new().build_p1();
let mut store = Store::new(&rt.engine, wasi_ctx);
store.set_epoch_deadline(EPOCH_DEADLINE);
// If we get here, epoch interruption is configured correctly.
}
#[test]
fn test_validate_storage_key_special_chars() {
assert!(!validate_storage_key("key@home"));
assert!(!validate_storage_key("key#1"));
assert!(!validate_storage_key("key=value"));
assert!(!validate_storage_key("key\x00null"));
assert!(!validate_storage_key("日本語"));
}
#[test]
fn test_validate_storage_key_boundary_length() {
// 255 is exactly valid
assert!(validate_storage_key(&"x".repeat(255)));
// 256 is too long
assert!(!validate_storage_key(&"x".repeat(256)));
// 1 is valid
assert!(validate_storage_key("a"));
}
#[test]
fn test_validate_storage_key_dot_variants() {
assert!(validate_storage_key("config.toml"));
assert!(validate_storage_key(".hidden"));
assert!(!validate_storage_key(".."));
assert!(!validate_storage_key("path/../escape"));
}
#[test]
fn test_bytes_to_lines_crlf() {
// Rust's `lines()` splits on both \n and \r\n, stripping the line ending
let lines = bytes_to_lines(b"line1\r\nline2\r\n");
assert_eq!(lines, vec!["line1", "line2"]);
}
#[test]
fn test_bytes_to_lines_single_line_no_newline() {
let lines = bytes_to_lines(b"single");
assert_eq!(lines, vec!["single"]);
}
#[test]
fn test_bytes_to_lines_invalid_utf8() {
// Invalid UTF-8 should not panic (from_utf8_lossy handles it)
let lines = bytes_to_lines(&[0xFF, 0xFE, b'\n', b'o', b'k']);
assert_eq!(lines.len(), 2);
assert_eq!(lines[1], "ok");
}
#[test]
fn test_wasm_output_default() {
let out = WasmOutput::default();
assert!(out.stdout.is_empty());
assert!(out.stderr.is_empty());
}
#[test]
fn test_storage_dir_for_app_default_fallback() {
// When OPENSYSTEM_STORAGE_DIR is set, uses that base + app_id
let tmp = tempfile::tempdir().unwrap();
let base = tmp.path().to_string_lossy().to_string();
std::env::set_var("OPENSYSTEM_STORAGE_DIR", &base);
let dir = storage_dir_for_app("test-app");
assert_eq!(dir, tmp.path().join("test-app"));
}