wasm_host.rs

  1pub(crate) mod wit;
  2
  3use crate::ExtensionManifest;
  4use anyhow::{anyhow, bail, Context as _, Result};
  5use fs::{normalize_path, Fs};
  6use futures::{
  7    channel::{
  8        mpsc::{self, UnboundedSender},
  9        oneshot,
 10    },
 11    future::BoxFuture,
 12    Future, FutureExt, StreamExt as _,
 13};
 14use gpui::BackgroundExecutor;
 15use language::LanguageRegistry;
 16use node_runtime::NodeRuntime;
 17use std::{
 18    path::{Path, PathBuf},
 19    sync::{Arc, OnceLock},
 20};
 21use util::{http::HttpClient, SemanticVersion};
 22use wasmtime::{
 23    component::{Component, ResourceTable},
 24    Engine, Store,
 25};
 26use wasmtime_wasi as wasi;
 27use wit::Extension;
 28
 29pub(crate) struct WasmHost {
 30    engine: Engine,
 31    http_client: Arc<dyn HttpClient>,
 32    node_runtime: Arc<dyn NodeRuntime>,
 33    pub(crate) language_registry: Arc<LanguageRegistry>,
 34    fs: Arc<dyn Fs>,
 35    pub(crate) work_dir: PathBuf,
 36}
 37
 38#[derive(Clone)]
 39pub struct WasmExtension {
 40    tx: UnboundedSender<ExtensionCall>,
 41    pub(crate) manifest: Arc<ExtensionManifest>,
 42    #[allow(unused)]
 43    zed_api_version: SemanticVersion,
 44}
 45
 46pub(crate) struct WasmState {
 47    manifest: Arc<ExtensionManifest>,
 48    pub(crate) table: ResourceTable,
 49    ctx: wasi::WasiCtx,
 50    pub(crate) host: Arc<WasmHost>,
 51}
 52
 53type ExtensionCall = Box<
 54    dyn Send + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, ()>,
 55>;
 56
 57fn wasm_engine() -> wasmtime::Engine {
 58    static WASM_ENGINE: OnceLock<wasmtime::Engine> = OnceLock::new();
 59
 60    WASM_ENGINE
 61        .get_or_init(|| {
 62            let mut config = wasmtime::Config::new();
 63            config.wasm_component_model(true);
 64            config.async_support(true);
 65            wasmtime::Engine::new(&config).unwrap()
 66        })
 67        .clone()
 68}
 69
 70impl WasmHost {
 71    pub fn new(
 72        fs: Arc<dyn Fs>,
 73        http_client: Arc<dyn HttpClient>,
 74        node_runtime: Arc<dyn NodeRuntime>,
 75        language_registry: Arc<LanguageRegistry>,
 76        work_dir: PathBuf,
 77    ) -> Arc<Self> {
 78        Arc::new(Self {
 79            engine: wasm_engine(),
 80            fs,
 81            work_dir,
 82            http_client,
 83            node_runtime,
 84            language_registry,
 85        })
 86    }
 87
 88    pub fn load_extension(
 89        self: &Arc<Self>,
 90        wasm_bytes: Vec<u8>,
 91        manifest: Arc<ExtensionManifest>,
 92        executor: BackgroundExecutor,
 93    ) -> impl 'static + Future<Output = Result<WasmExtension>> {
 94        let this = self.clone();
 95        async move {
 96            let component = Component::from_binary(&this.engine, &wasm_bytes)
 97                .context("failed to compile wasm component")?;
 98
 99            let mut zed_api_version = None;
100            for part in wasmparser::Parser::new(0).parse_all(&wasm_bytes) {
101                if let wasmparser::Payload::CustomSection(s) = part? {
102                    if s.name() == "zed:api-version" {
103                        zed_api_version = parse_extension_version(s.data());
104                        if zed_api_version.is_none() {
105                            bail!(
106                                "extension {} has invalid zed:api-version section: {:?}",
107                                manifest.id,
108                                s.data()
109                            );
110                        }
111                    }
112                }
113            }
114
115            let Some(zed_api_version) = zed_api_version else {
116                bail!("extension {} has no zed:api-version section", manifest.id);
117            };
118
119            let mut store = wasmtime::Store::new(
120                &this.engine,
121                WasmState {
122                    ctx: this.build_wasi_ctx(&manifest).await?,
123                    manifest: manifest.clone(),
124                    table: ResourceTable::new(),
125                    host: this.clone(),
126                },
127            );
128
129            let (mut extension, instance) =
130                Extension::instantiate_async(&mut store, zed_api_version, &component).await?;
131
132            extension
133                .call_init_extension(&mut store)
134                .await
135                .context("failed to initialize wasm extension")?;
136
137            let (tx, mut rx) = mpsc::unbounded::<ExtensionCall>();
138            executor
139                .spawn(async move {
140                    let _instance = instance;
141                    while let Some(call) = rx.next().await {
142                        (call)(&mut extension, &mut store).await;
143                    }
144                })
145                .detach();
146
147            Ok(WasmExtension {
148                manifest,
149                tx,
150                zed_api_version,
151            })
152        }
153    }
154
155    async fn build_wasi_ctx(&self, manifest: &Arc<ExtensionManifest>) -> Result<wasi::WasiCtx> {
156        use cap_std::{ambient_authority, fs::Dir};
157
158        let extension_work_dir = self.work_dir.join(manifest.id.as_ref());
159        self.fs
160            .create_dir(&extension_work_dir)
161            .await
162            .context("failed to create extension work dir")?;
163
164        let work_dir_preopen = Dir::open_ambient_dir(&extension_work_dir, ambient_authority())
165            .context("failed to preopen extension work directory")?;
166        let current_dir_preopen = work_dir_preopen
167            .try_clone()
168            .context("failed to preopen extension current directory")?;
169        let extension_work_dir = extension_work_dir.to_string_lossy();
170
171        let perms = wasi::FilePerms::all();
172        let dir_perms = wasi::DirPerms::all();
173
174        Ok(wasi::WasiCtxBuilder::new()
175            .inherit_stdio()
176            .preopened_dir(current_dir_preopen, dir_perms, perms, ".")
177            .preopened_dir(work_dir_preopen, dir_perms, perms, &extension_work_dir)
178            .env("PWD", &extension_work_dir)
179            .env("RUST_BACKTRACE", "full")
180            .build())
181    }
182
183    pub fn path_from_extension(&self, id: &Arc<str>, path: &Path) -> PathBuf {
184        let extension_work_dir = self.work_dir.join(id.as_ref());
185        normalize_path(&extension_work_dir.join(path))
186    }
187
188    pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Result<PathBuf> {
189        let extension_work_dir = self.work_dir.join(id.as_ref());
190        let path = normalize_path(&extension_work_dir.join(path));
191        if path.starts_with(&extension_work_dir) {
192            Ok(path)
193        } else {
194            Err(anyhow!("cannot write to path {}", path.display()))
195        }
196    }
197}
198
199fn parse_extension_version(data: &[u8]) -> Option<SemanticVersion> {
200    if data.len() == 6 {
201        Some(SemanticVersion {
202            major: u16::from_be_bytes([data[0], data[1]]) as _,
203            minor: u16::from_be_bytes([data[2], data[3]]) as _,
204            patch: u16::from_be_bytes([data[4], data[5]]) as _,
205        })
206    } else {
207        None
208    }
209}
210
211impl WasmExtension {
212    pub async fn call<T, Fn>(&self, f: Fn) -> T
213    where
214        T: 'static + Send,
215        Fn: 'static
216            + Send
217            + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, T>,
218    {
219        let (return_tx, return_rx) = oneshot::channel();
220        self.tx
221            .clone()
222            .unbounded_send(Box::new(move |extension, store| {
223                async {
224                    let result = f(extension, store).await;
225                    return_tx.send(result).ok();
226                }
227                .boxed()
228            }))
229            .expect("wasm extension channel should not be closed yet");
230        return_rx.await.expect("wasm extension channel")
231    }
232}
233
234impl WasmState {
235    fn work_dir(&self) -> PathBuf {
236        self.host.work_dir.join(self.manifest.id.as_ref())
237    }
238}
239
240impl wasi::WasiView for WasmState {
241    fn table(&mut self) -> &mut ResourceTable {
242        &mut self.table
243    }
244
245    fn ctx(&mut self) -> &mut wasi::WasiCtx {
246        &mut self.ctx
247    }
248}