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