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    ) -> impl 'static + Future<Output = Result<WasmExtension>> {
110        let this = self.clone();
111        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    for part in wasmparser::Parser::new(0).parse_all(wasm_bytes) {
202        if let wasmparser::Payload::CustomSection(s) = part? {
203            if s.name() == "zed:api-version" {
204                let version = parse_wasm_extension_version_custom_section(s.data());
205                if let Some(version) = version {
206                    return Ok(version);
207                } else {
208                    bail!(
209                        "extension {} has invalid zed:api-version section: {:?}",
210                        extension_id,
211                        s.data()
212                    );
213                }
214            }
215        }
216    }
217    bail!("extension {} has no zed:api-version section", extension_id)
218}
219
220fn parse_wasm_extension_version_custom_section(data: &[u8]) -> Option<SemanticVersion> {
221    if data.len() == 6 {
222        Some(SemanticVersion::new(
223            u16::from_be_bytes([data[0], data[1]]) as _,
224            u16::from_be_bytes([data[2], data[3]]) as _,
225            u16::from_be_bytes([data[4], data[5]]) as _,
226        ))
227    } else {
228        None
229    }
230}
231
232impl WasmExtension {
233    pub async fn call<T, Fn>(&self, f: Fn) -> T
234    where
235        T: 'static + Send,
236        Fn: 'static
237            + Send
238            + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, T>,
239    {
240        let (return_tx, return_rx) = oneshot::channel();
241        self.tx
242            .clone()
243            .unbounded_send(Box::new(move |extension, store| {
244                async {
245                    let result = f(extension, store).await;
246                    return_tx.send(result).ok();
247                }
248                .boxed()
249            }))
250            .expect("wasm extension channel should not be closed yet");
251        return_rx.await.expect("wasm extension channel")
252    }
253}
254
255impl WasmState {
256    fn on_main_thread<T, Fn>(&self, f: Fn) -> impl 'static + Future<Output = T>
257    where
258        T: 'static + Send,
259        Fn: 'static + Send + for<'a> FnOnce(&'a mut AsyncAppContext) -> LocalBoxFuture<'a, T>,
260    {
261        let (return_tx, return_rx) = oneshot::channel();
262        self.host
263            .main_thread_message_tx
264            .clone()
265            .unbounded_send(Box::new(move |cx| {
266                async {
267                    let result = f(cx).await;
268                    return_tx.send(result).ok();
269                }
270                .boxed_local()
271            }))
272            .expect("main thread message channel should not be closed yet");
273        async move { return_rx.await.expect("main thread message channel") }
274    }
275
276    fn work_dir(&self) -> PathBuf {
277        self.host.work_dir.join(self.manifest.id.as_ref())
278    }
279}
280
281impl wasi::WasiView for WasmState {
282    fn table(&mut self) -> &mut ResourceTable {
283        &mut self.table
284    }
285
286    fn ctx(&mut self) -> &mut wasi::WasiCtx {
287        &mut self.ctx
288    }
289}