|
| 1 | +use anyhow::Context as _; |
| 2 | +use spin_common::{ui::quoted_path, url::parse_file_url}; |
| 3 | +use spin_core::{async_trait, wasmtime, Component}; |
| 4 | +use spin_factors::AppComponent; |
| 5 | + |
| 6 | +#[derive(Default)] |
| 7 | +pub struct ComponentLoader { |
| 8 | + #[cfg(feature = "unsafe-aot-compilation")] |
| 9 | + aot_compilation_enabled: bool, |
| 10 | +} |
| 11 | + |
| 12 | +impl ComponentLoader { |
| 13 | + /// Updates the TriggerLoader to load AOT precompiled components |
| 14 | + /// |
| 15 | + /// **Warning: This feature may bypass important security guarantees of the |
| 16 | + /// Wasmtime security sandbox if used incorrectly! Read this documentation |
| 17 | + /// carefully.** |
| 18 | + /// |
| 19 | + /// Usually, components are compiled just-in-time from portable Wasm |
| 20 | + /// sources. This method causes components to instead be loaded |
| 21 | + /// ahead-of-time as Wasmtime-precompiled native executable binaries. |
| 22 | + /// Precompiled binaries must be produced with a compatible Wasmtime engine |
| 23 | + /// using the same Wasmtime version and compiler target settings - typically |
| 24 | + /// by a host with the same processor that will be executing them. See the |
| 25 | + /// Wasmtime documentation for more information: |
| 26 | + /// https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize |
| 27 | + /// |
| 28 | + /// # Safety |
| 29 | + /// |
| 30 | + /// This method is marked as `unsafe` because it enables potentially unsafe |
| 31 | + /// behavior if used to load malformed or malicious precompiled binaries. |
| 32 | + /// Loading sources from an incompatible Wasmtime engine will fail but is |
| 33 | + /// otherwise safe. This method is safe if it can be guaranteed that |
| 34 | + /// `<TriggerLoader as Loader>::load_component` will only ever be called |
| 35 | + /// with a trusted `LockedComponentSource`. **Precompiled binaries must |
| 36 | + /// never be loaded from untrusted sources.** |
| 37 | + #[cfg(feature = "unsafe-aot-compilation")] |
| 38 | + pub unsafe fn enable_loading_aot_compiled_components(&mut self) { |
| 39 | + self.aot_compilation_enabled = true; |
| 40 | + } |
| 41 | + |
| 42 | + #[cfg(feature = "unsafe-aot-compilation")] |
| 43 | + fn load_precompiled_component( |
| 44 | + &self, |
| 45 | + engine: &wasmtime::Engine, |
| 46 | + path: &std::path::Path, |
| 47 | + ) -> anyhow::Result<Component> { |
| 48 | + assert!(self.aot_compilation_enabled); |
| 49 | + match engine.detect_precompiled_file(path)? { |
| 50 | + Some(wasmtime::Precompiled::Component) => unsafe { |
| 51 | + Component::deserialize_file(engine, path) |
| 52 | + }, |
| 53 | + Some(wasmtime::Precompiled::Module) => { |
| 54 | + anyhow::bail!("expected AOT compiled component but found module"); |
| 55 | + } |
| 56 | + None => { |
| 57 | + anyhow::bail!("expected AOT compiled component but found other data"); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[async_trait] |
| 64 | +impl spin_factors_executor::ComponentLoader for ComponentLoader { |
| 65 | + async fn load_component( |
| 66 | + &self, |
| 67 | + engine: &wasmtime::Engine, |
| 68 | + component: &AppComponent, |
| 69 | + ) -> anyhow::Result<Component> { |
| 70 | + let source = component |
| 71 | + .source() |
| 72 | + .content |
| 73 | + .source |
| 74 | + .as_ref() |
| 75 | + .context("LockedComponentSource missing source field")?; |
| 76 | + let path = parse_file_url(source)?; |
| 77 | + |
| 78 | + #[cfg(feature = "unsafe-aot-compilation")] |
| 79 | + if self.aot_compilation_enabled { |
| 80 | + return self |
| 81 | + .load_precompiled_component(engine, &path) |
| 82 | + .with_context(|| format!("error deserializing component from {path:?}")); |
| 83 | + } |
| 84 | + |
| 85 | + let composed = spin_compose::compose(&ComponentSourceLoader, component.locked) |
| 86 | + .await |
| 87 | + .with_context(|| { |
| 88 | + format!( |
| 89 | + "failed to resolve dependencies for component {:?}", |
| 90 | + component.locked.id |
| 91 | + ) |
| 92 | + })?; |
| 93 | + |
| 94 | + spin_core::Component::new(engine, composed) |
| 95 | + .with_context(|| format!("failed to compile component from {}", quoted_path(&path))) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +struct ComponentSourceLoader; |
| 100 | + |
| 101 | +#[async_trait] |
| 102 | +impl spin_compose::ComponentSourceLoader for ComponentSourceLoader { |
| 103 | + async fn load_component_source( |
| 104 | + &self, |
| 105 | + source: &spin_app::locked::LockedComponentSource, |
| 106 | + ) -> anyhow::Result<Vec<u8>> { |
| 107 | + let source = source |
| 108 | + .content |
| 109 | + .source |
| 110 | + .as_ref() |
| 111 | + .context("LockedComponentSource missing source field")?; |
| 112 | + |
| 113 | + let path = parse_file_url(source)?; |
| 114 | + |
| 115 | + let bytes: Vec<u8> = tokio::fs::read(&path).await.with_context(|| { |
| 116 | + format!( |
| 117 | + "failed to read component source from disk at path {}", |
| 118 | + quoted_path(&path) |
| 119 | + ) |
| 120 | + })?; |
| 121 | + |
| 122 | + let component = spin_componentize::componentize_if_necessary(&bytes)?; |
| 123 | + |
| 124 | + Ok(component.into()) |
| 125 | + } |
| 126 | +} |
0 commit comments