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