|
| 1 | +/* |
| 2 | +Copyright 2024 The Hyperlight Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//! This example demonstrates how to: |
| 18 | +//! 1. Get an interrupt handle for a sandbox |
| 19 | +//! 2. Interrupt long-running guest code from another thread |
| 20 | +//! 3. Detect when a sandbox is poisoned |
| 21 | +//! 4. Recover a poisoned sandbox using `restore()` or `unload_module()` |
| 22 | +
|
| 23 | +use std::thread; |
| 24 | +use std::time::Duration; |
| 25 | + |
| 26 | +use examples_common::get_wasm_module_path; |
| 27 | +use hyperlight_wasm::{HyperlightError, Result, SandboxBuilder}; |
| 28 | + |
| 29 | +fn get_time_since_boot_microsecond() -> Result<i64> { |
| 30 | + let res = std::time::SystemTime::now() |
| 31 | + .duration_since(std::time::SystemTime::UNIX_EPOCH)? |
| 32 | + .as_micros(); |
| 33 | + i64::try_from(res).map_err(HyperlightError::IntConversionFailure) |
| 34 | +} |
| 35 | + |
| 36 | +fn main() -> Result<()> { |
| 37 | + println!("=== Hyperlight-Wasm Interruption Example ===\n"); |
| 38 | + |
| 39 | + // Build a sandbox and register host functions |
| 40 | + let mut sandbox = SandboxBuilder::new().build()?; |
| 41 | + sandbox.register( |
| 42 | + "GetTimeSinceBootMicrosecond", |
| 43 | + get_time_since_boot_microsecond, |
| 44 | + )?; |
| 45 | + |
| 46 | + let wasm_sandbox = sandbox.load_runtime()?; |
| 47 | + let mod_path = get_wasm_module_path("RunWasm.aot")?; |
| 48 | + let mut loaded = wasm_sandbox.load_module(mod_path)?; |
| 49 | + |
| 50 | + println!("1. Sandbox created and module loaded"); |
| 51 | + assert!(!loaded.is_poisoned()?); |
| 52 | + println!(" is_poisoned: {}", loaded.is_poisoned()?); |
| 53 | + |
| 54 | + // Take a snapshot before we do anything |
| 55 | + let snapshot = loaded.snapshot()?; |
| 56 | + println!("2. Snapshot taken for later recovery\n"); |
| 57 | + |
| 58 | + // Get an interrupt handle - this can be sent to another thread |
| 59 | + let interrupt = loaded.interrupt_handle()?; |
| 60 | + println!("3. Interrupt handle obtained\n"); |
| 61 | + |
| 62 | + // Spawn a thread that will interrupt the guest after 1 second |
| 63 | + println!("4. Starting long-running guest function..."); |
| 64 | + println!(" (A background thread will interrupt it after 1 second)\n"); |
| 65 | + |
| 66 | + thread::spawn(move || { |
| 67 | + thread::sleep(Duration::from_secs(1)); |
| 68 | + println!(" [Background thread] Calling interrupt.kill()..."); |
| 69 | + interrupt.kill(); |
| 70 | + }); |
| 71 | + |
| 72 | + // Call a long-running guest function that will be interrupted |
| 73 | + let result = loaded.call_guest_function::<i32>("KeepCPUBusy", 100000i32); |
| 74 | + |
| 75 | + match result { |
| 76 | + Ok(_) => panic!(" Guest function completed (unexpected!)"), |
| 77 | + Err(HyperlightError::ExecutionCanceledByHost()) => { |
| 78 | + println!(" Guest function was interrupted (ExecutionCanceledByHost)"); |
| 79 | + } |
| 80 | + Err(e) => panic!(" Unexpected error: {:?}", e), |
| 81 | + } |
| 82 | + |
| 83 | + println!("\n5. Checking sandbox state after interruption:"); |
| 84 | + println!(" is_poisoned: {}", loaded.is_poisoned()?); |
| 85 | + |
| 86 | + // Demonstrate that calling a poisoned sandbox fails |
| 87 | + println!("\n6. Attempting to call guest function on poisoned sandbox..."); |
| 88 | + let result = loaded.call_guest_function::<i32>("CalcFib", 10i32); |
| 89 | + |
| 90 | + match result { |
| 91 | + Ok(_) => panic!(" Call succeeded (unexpected!)"), |
| 92 | + Err(HyperlightError::PoisonedSandbox) => { |
| 93 | + println!(" Call failed with PoisonedSandbox error (expected)"); |
| 94 | + } |
| 95 | + Err(e) => panic!(" Unexpected error: {:?}", e), |
| 96 | + } |
| 97 | + |
| 98 | + // Recovery option 1: Use restore() to recover the sandbox |
| 99 | + println!("\n7. Recovering sandbox using restore()..."); |
| 100 | + loaded.restore(&snapshot)?; |
| 101 | + assert!(!loaded.is_poisoned()?); |
| 102 | + println!(" is_poisoned after restore: {}", loaded.is_poisoned()?); |
| 103 | + |
| 104 | + // Now we can call guest functions again |
| 105 | + println!("\n8. Calling guest function after recovery..."); |
| 106 | + let result: i32 = loaded.call_guest_function("CalcFib", 10i32)?; |
| 107 | + println!(" CalcFib(10) returned: {} (expected 55)", result); |
| 108 | + |
| 109 | + // Demonstrate recovery option 2: unload_module |
| 110 | + println!("\n9. Demonstrating unload_module recovery..."); |
| 111 | + |
| 112 | + // First, poison the sandbox again |
| 113 | + let interrupt = loaded.interrupt_handle()?; |
| 114 | + thread::spawn(move || { |
| 115 | + thread::sleep(Duration::from_millis(500)); |
| 116 | + interrupt.kill(); |
| 117 | + }); |
| 118 | + let _ = loaded.call_guest_function::<i32>("KeepCPUBusy", 100000i32); |
| 119 | + |
| 120 | + assert!(loaded.is_poisoned()?); |
| 121 | + println!(" Sandbox poisoned again {}", loaded.is_poisoned()?); |
| 122 | + |
| 123 | + // unload_module() will recover the sandbox |
| 124 | + let wasm_sandbox = loaded.unload_module()?; |
| 125 | + println!(" Module unloaded (this calls restore internally)"); |
| 126 | + |
| 127 | + // Load a different module and continue |
| 128 | + let hello_path = get_wasm_module_path("HelloWorld.aot")?; |
| 129 | + let mut new_loaded = wasm_sandbox.load_module(hello_path)?; |
| 130 | + assert!(!new_loaded.is_poisoned()?); |
| 131 | + println!( |
| 132 | + " New module loaded, is_poisoned: {}", |
| 133 | + new_loaded.is_poisoned()? |
| 134 | + ); |
| 135 | + |
| 136 | + let result: i32 = |
| 137 | + new_loaded.call_guest_function("HelloWorld", "Recovery successful!".to_string())?; |
| 138 | + |
| 139 | + println!(" HelloWorld returned: {}", result); |
| 140 | + |
| 141 | + println!("\n=== Example Complete ==="); |
| 142 | + Ok(()) |
| 143 | +} |
0 commit comments