wasm-sandbox 0.4.1

A secure WebAssembly sandbox with dead-simple ease of use, progressive complexity APIs, and comprehensive safety controls
Documentation
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
# Development Tools Integration


📖 **[← Back to Documentation]../README.md** | 🏠 **[← Main README]../../README.md** | 🚀 **[API Reference]https://docs.rs/wasm-sandbox**

Integration with development tools, IDEs, debuggers, and development workflows for productive wasm-sandbox development.

## IDE Support


### VS Code Extension


```json
{
    "name": "wasm-sandbox",
    "displayName": "WebAssembly Sandbox",
    "description": "Development tools for wasm-sandbox projects",
    "version": "1.0.0",
    "engines": {
        "vscode": "^1.60.0"
    },
    "categories": ["Debuggers", "Other"],
    "activationEvents": [
        "onLanguage:rust",
        "workspaceContains:**/sandbox.toml"
    ],
    "contributes": {
        "commands": [
            {
                "command": "wasm-sandbox.compile",
                "title": "Compile to WebAssembly",
                "category": "WASM Sandbox"
            },
            {
                "command": "wasm-sandbox.run",
                "title": "Run in Sandbox",
                "category": "WASM Sandbox"
            },
            {
                "command": "wasm-sandbox.debug",
                "title": "Debug WebAssembly",
                "category": "WASM Sandbox"
            }
        ],
        "configuration": {
            "title": "WASM Sandbox",
            "properties": {
                "wasm-sandbox.runtime": {
                    "type": "string",
                    "default": "wasmtime",
                    "enum": ["wasmtime", "wasmer"],
                    "description": "WebAssembly runtime to use"
                },
                "wasm-sandbox.optimization": {
                    "type": "string",
                    "default": "speed",
                    "enum": ["none", "speed", "size"],
                    "description": "Optimization level"
                }
            }
        }
    }
}
```

### Language Server Protocol


```rust
use tower_lsp::{LspService, Server};
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;

pub struct WasmSandboxLanguageServer {
    client: tower_lsp::Client,
    workspace: Arc<RwLock<Workspace>>,
}

#[tower_lsp::async_trait]

impl tower_lsp::LanguageServer for WasmSandboxLanguageServer {
    async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
        Ok(InitializeResult {
            capabilities: ServerCapabilities {
                text_document_sync: Some(TextDocumentSyncCapability::Kind(
                    TextDocumentSyncKind::FULL,
                )),
                completion_provider: Some(CompletionOptions {
                    resolve_provider: Some(true),
                    trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
                    ..Default::default()
                }),
                hover_provider: Some(HoverProviderCapability::Simple(true)),
                definition_provider: Some(OneOf::Left(true)),
                code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
                execute_command_provider: Some(ExecuteCommandOptions {
                    commands: vec![
                        "wasm-sandbox.compile".to_string(),
                        "wasm-sandbox.run".to_string(),
                        "wasm-sandbox.validate".to_string(),
                    ],
                    ..Default::default()
                }),
                ..Default::default()
            },
            ..Default::default()
        })
    }

    async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> {
        let completions = self.provide_completions(&params).await;
        Ok(Some(CompletionResponse::Array(completions)))
    }

    async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
        let hover_info = self.provide_hover_info(&params).await;
        Ok(hover_info)
    }

    async fn execute_command(&self, params: ExecuteCommandParams) -> Result<Option<serde_json::Value>> {
        match params.command.as_str() {
            "wasm-sandbox.compile" => {
                self.compile_workspace().await?;
                Ok(None)
            }
            "wasm-sandbox.run" => {
                self.run_sandbox().await?;
                Ok(None)
            }
            _ => Ok(None),
        }
    }
}

impl WasmSandboxLanguageServer {
    async fn provide_completions(&self, params: &CompletionParams) -> Vec<CompletionItem> {
        let mut completions = Vec::new();

        // API completions
        completions.extend(self.get_api_completions());
        
        // Configuration completions
        completions.extend(self.get_config_completions());
        
        // Function completions
        completions.extend(self.get_function_completions());

        completions
    }

    fn get_api_completions(&self) -> Vec<CompletionItem> {
        vec![
            CompletionItem {
                label: "WasmSandbox::builder()".to_string(),
                kind: Some(CompletionItemKind::METHOD),
                detail: Some("Create a new sandbox builder".to_string()),
                documentation: Some(Documentation::String(
                    "Creates a new WasmSandbox builder with default configuration".to_string()
                )),
                insert_text: Some("WasmSandbox::builder()".to_string()),
                ..Default::default()
            },
            CompletionItem {
                label: "sandbox.call()".to_string(),
                kind: Some(CompletionItemKind::METHOD),
                detail: Some("Call a WebAssembly function".to_string()),
                insert_text: Some("call(\"${1:function_name}\", ${2:args})".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
        ]
    }
}
```

## Debugging Support


### Debug Adapter Protocol


```rust
use dap::{DebugAdapter, Event, Request, Response};

pub struct WasmSandboxDebugAdapter {
    sandbox: Option<WasmSandbox>,
    breakpoints: HashMap<String, Vec<Breakpoint>>,
    call_stack: Vec<StackFrame>,
    variables: HashMap<i64, Vec<Variable>>,
}

impl DebugAdapter for WasmSandboxDebugAdapter {
    async fn handle_request(&mut self, request: Request) -> Option<Response> {
        match request.command.as_str() {
            "initialize" => Some(self.handle_initialize(request).await),
            "launch" => Some(self.handle_launch(request).await),
            "setBreakpoints" => Some(self.handle_set_breakpoints(request).await),
            "continue" => Some(self.handle_continue(request).await),
            "next" => Some(self.handle_next(request).await),
            "stepIn" => Some(self.handle_step_in(request).await),
            "stackTrace" => Some(self.handle_stack_trace(request).await),
            "variables" => Some(self.handle_variables(request).await),
            _ => None,
        }
    }

    async fn handle_initialize(&mut self, request: Request) -> Response {
        Response {
            request_seq: request.seq,
            success: true,
            command: request.command,
            body: Some(json!({
                "supportsBreakpointLocationsRequest": true,
                "supportsStepInTargetsRequest": true,
                "supportsConfigurationDoneRequest": true,
                "supportsEvaluateForHovers": true,
                "supportsExceptionInfoRequest": true,
            })),
            ..Default::default()
        }
    }

    async fn handle_launch(&mut self, request: Request) -> Response {
        let args = request.arguments.unwrap_or_default();
        let wasm_file = args["program"].as_str().unwrap_or("program.wasm");

        // Create debug-enabled sandbox
        match WasmSandbox::builder()
            .enable_debugging(true)
            .source_file(wasm_file)
            .build()
            .await
        {
            Ok(sandbox) => {
                self.sandbox = Some(sandbox);
                Response {
                    request_seq: request.seq,
                    success: true,
                    command: request.command,
                    ..Default::default()
                }
            }
            Err(e) => Response {
                request_seq: request.seq,
                success: false,
                command: request.command,
                message: Some(e.to_string()),
                ..Default::default()
            }
        }
    }

    async fn handle_set_breakpoints(&mut self, request: Request) -> Response {
        let args = request.arguments.unwrap_or_default();
        let source = args["source"]["path"].as_str().unwrap_or("");
        let breakpoints: Vec<_> = args["breakpoints"]
            .as_array()
            .unwrap_or(&vec![])
            .iter()
            .map(|bp| Breakpoint {
                line: bp["line"].as_u64().unwrap_or(0) as usize,
                column: bp.get("column").and_then(|c| c.as_u64()).map(|c| c as usize),
                condition: bp.get("condition").and_then(|c| c.as_str()).map(String::from),
            })
            .collect();

        self.breakpoints.insert(source.to_string(), breakpoints.clone());

        // Set breakpoints in sandbox
        if let Some(sandbox) = &mut self.sandbox {
            for bp in &breakpoints {
                let _ = sandbox.set_breakpoint(BreakpointLocation {
                    file: source.to_string(),
                    line: bp.line,
                    column: bp.column,
                }).await;
            }
        }

        Response {
            request_seq: request.seq,
            success: true,
            command: request.command,
            body: Some(json!({
                "breakpoints": breakpoints.iter().map(|bp| {
                    json!({
                        "verified": true,
                        "line": bp.line,
                        "column": bp.column
                    })
                }).collect::<Vec<_>>()
            })),
            ..Default::default()
        }
    }
}
```

## Build Integration


### Cargo Integration


```toml
# Cargo.toml

[package]
name = "my-wasm-project"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-sandbox = "0.3.0"
serde = { version = "1.0", features = ["derive"] }

[package.metadata.wasm-sandbox]
runtime = "wasmtime"
optimization = "speed"
security = "strict"
resource_limits = { memory = "128MB", cpu = "1.0" }

[[package.metadata.wasm-sandbox.wrapper]]
type = "http_server"
port = 8080
routes = [
    { path = "/api/process", function = "process_data" },
    { path = "/api/health", function = "health_check" }
]
```

### Custom Build Script


```rust
// build.rs
use std::process::Command;

fn main() {
    // Compile to WebAssembly
    let output = Command::new("cargo")
        .args(&[
            "build",
            "--target", "wasm32-wasi",
            "--release"
        ])
        .output()
        .expect("Failed to compile to WebAssembly");

    if !output.status.success() {
        panic!("WebAssembly compilation failed: {}", String::from_utf8_lossy(&output.stderr));
    }

    // Optimize with wasm-opt
    Command::new("wasm-opt")
        .args(&[
            "-O3",
            "--enable-bulk-memory",
            "--enable-reference-types",
            "target/wasm32-wasi/release/my_wasm_project.wasm",
            "-o",
            "optimized.wasm"
        ])
        .output()
        .expect("Failed to optimize WebAssembly");

    // Generate wrapper
    wasm_sandbox::build::generate_wrapper(
        "optimized.wasm",
        &wasm_sandbox::build::WrapperConfig {
            wrapper_type: wasm_sandbox::build::WrapperType::HttpServer,
            output_file: "src/generated_wrapper.rs".into(),
            ..Default::default()
        }
    ).expect("Failed to generate wrapper");

    println!("cargo:rerun-if-changed=src/lib.rs");
    println!("cargo:rerun-if-changed=Cargo.toml");
}
```

## Testing Framework


```rust
use wasm_sandbox::testing::{SandboxTest, TestBuilder};

#[wasm_sandbox::test]

async fn test_basic_function() -> Result<(), Box<dyn std::error::Error>> {
    let sandbox = TestBuilder::new()
        .source("test_module.wasm")
        .timeout(Duration::from_secs(5))
        .build()
        .await?;

    let result: i32 = sandbox.call("add", (2, 3)).await?;
    assert_eq!(result, 5);

    Ok(())
}

#[wasm_sandbox::test]

async fn test_memory_limits() -> Result<(), Box<dyn std::error::Error>> {
    let sandbox = TestBuilder::new()
        .source("memory_test.wasm")
        .memory_limit(1024 * 1024) // 1MB
        .build()
        .await?;

    // This should fail due to memory limit
    let result = sandbox.call("allocate_large", 2 * 1024 * 1024).await;
    assert!(result.is_err());

    Ok(())
}

#[wasm_sandbox::benchmark]

async fn bench_function_call(b: &mut Bencher) -> Result<(), Box<dyn std::error::Error>> {
    let sandbox = TestBuilder::new()
        .source("bench_module.wasm")
        .build()
        .await?;

    b.iter(|| async {
        let _result: i32 = sandbox.call("fast_function", 42).await.unwrap();
    });

    Ok(())
}
```

## Performance Profiling


```rust
pub struct SandboxProfiler {
    profiling_data: Arc<RwLock<ProfilingData>>,
    sample_rate: Duration,
}

impl SandboxProfiler {
    pub async fn profile_execution<T, R>(&self, sandbox: &WasmSandbox, function: &str, args: &T) -> Result<ProfiledResult<R>, ProfileError>
    where
        T: serde::Serialize,
        R: for<'de> serde::Deserialize<'de>,
    {
        let start_time = Instant::now();
        let start_memory = sandbox.get_memory_usage().await?;
        let start_fuel = sandbox.get_fuel_remaining().await?;

        // Start sampling
        let _sampler = self.start_sampling(sandbox).await?;

        // Execute function
        let result = sandbox.call(function, args).await?;

        // Collect final metrics
        let end_time = Instant::now();
        let end_memory = sandbox.get_memory_usage().await?;
        let end_fuel = sandbox.get_fuel_remaining().await?;

        let profile = ExecutionProfile {
            duration: end_time.duration_since(start_time),
            memory_delta: end_memory.saturating_sub(start_memory),
            fuel_consumed: start_fuel.saturating_sub(end_fuel),
            call_count: self.get_call_count().await,
            hotspots: self.analyze_hotspots().await,
        };

        Ok(ProfiledResult {
            result,
            profile,
        })
    }
}
```

Next: **[Performance Guide](performance.md)** - Optimization strategies

---

**Development Excellence:** Integrate wasm-sandbox seamlessly into your development workflow with powerful tooling support.