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::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: Arc<dyn 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: Arc<dyn 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, instance) = 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                    let _instance = instance;
147                    while let Some(call) = rx.next().await {
148                        (call)(&mut extension, &mut store).await;
149                    }
150                })
151                .detach();
152
153            Ok(WasmExtension {
154                manifest,
155                tx,
156                zed_api_version,
157            })
158        })
159    }
160
161    async fn build_wasi_ctx(&self, manifest: &Arc<ExtensionManifest>) -> Result<wasi::WasiCtx> {
162        use cap_std::{ambient_authority, fs::Dir};
163
164        let extension_work_dir = self.work_dir.join(manifest.id.as_ref());
165        self.fs
166            .create_dir(&extension_work_dir)
167            .await
168            .context("failed to create extension work dir")?;
169
170        let work_dir_preopen = Dir::open_ambient_dir(&extension_work_dir, ambient_authority())
171            .context("failed to preopen extension work directory")?;
172        let current_dir_preopen = work_dir_preopen
173            .try_clone()
174            .context("failed to preopen extension current directory")?;
175        let extension_work_dir = extension_work_dir.to_string_lossy();
176
177        let perms = wasi::FilePerms::all();
178        let dir_perms = wasi::DirPerms::all();
179
180        Ok(wasi::WasiCtxBuilder::new()
181            .inherit_stdio()
182            .preopened_dir(current_dir_preopen, dir_perms, perms, ".")
183            .preopened_dir(work_dir_preopen, dir_perms, perms, &extension_work_dir)
184            .env("PWD", &extension_work_dir)
185            .env("RUST_BACKTRACE", "full")
186            .build())
187    }
188
189    pub fn path_from_extension(&self, id: &Arc<str>, path: &Path) -> PathBuf {
190        let extension_work_dir = self.work_dir.join(id.as_ref());
191        normalize_path(&extension_work_dir.join(path))
192    }
193
194    pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Result<PathBuf> {
195        let extension_work_dir = self.work_dir.join(id.as_ref());
196        let path = normalize_path(&extension_work_dir.join(path));
197        if path.starts_with(&extension_work_dir) {
198            Ok(path)
199        } else {
200            Err(anyhow!("cannot write to path {}", path.display()))
201        }
202    }
203}
204
205pub fn parse_wasm_extension_version(
206    extension_id: &str,
207    wasm_bytes: &[u8],
208) -> Result<SemanticVersion> {
209    let mut version = None;
210
211    for part in wasmparser::Parser::new(0).parse_all(wasm_bytes) {
212        if let wasmparser::Payload::CustomSection(s) =
213            part.context("error parsing wasm extension")?
214        {
215            if s.name() == "zed:api-version" {
216                version = parse_wasm_extension_version_custom_section(s.data());
217                if version.is_none() {
218                    bail!(
219                        "extension {} has invalid zed:api-version section: {:?}",
220                        extension_id,
221                        s.data()
222                    );
223                }
224            }
225        }
226    }
227
228    // The reason we wait until we're done parsing all of the Wasm bytes to return the version
229    // is to work around a panic that can happen inside of Wasmtime when the bytes are invalid.
230    //
231    // By parsing the entirety of the Wasm bytes before we return, we're able to detect this problem
232    // earlier as an `Err` rather than as a panic.
233    version.ok_or_else(|| anyhow!("extension {} has no zed:api-version section", extension_id))
234}
235
236fn parse_wasm_extension_version_custom_section(data: &[u8]) -> Option<SemanticVersion> {
237    if data.len() == 6 {
238        Some(SemanticVersion::new(
239            u16::from_be_bytes([data[0], data[1]]) as _,
240            u16::from_be_bytes([data[2], data[3]]) as _,
241            u16::from_be_bytes([data[4], data[5]]) as _,
242        ))
243    } else {
244        None
245    }
246}
247
248impl WasmExtension {
249    pub async fn call<T, Fn>(&self, f: Fn) -> T
250    where
251        T: 'static + Send,
252        Fn: 'static
253            + Send
254            + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, T>,
255    {
256        let (return_tx, return_rx) = oneshot::channel();
257        self.tx
258            .clone()
259            .unbounded_send(Box::new(move |extension, store| {
260                async {
261                    let result = f(extension, store).await;
262                    return_tx.send(result).ok();
263                }
264                .boxed()
265            }))
266            .expect("wasm extension channel should not be closed yet");
267        return_rx.await.expect("wasm extension channel")
268    }
269}
270
271impl WasmState {
272    fn on_main_thread<T, Fn>(&self, f: Fn) -> impl 'static + Future<Output = T>
273    where
274        T: 'static + Send,
275        Fn: 'static + Send + for<'a> FnOnce(&'a mut AsyncAppContext) -> LocalBoxFuture<'a, T>,
276    {
277        let (return_tx, return_rx) = oneshot::channel();
278        self.host
279            .main_thread_message_tx
280            .clone()
281            .unbounded_send(Box::new(move |cx| {
282                async {
283                    let result = f(cx).await;
284                    return_tx.send(result).ok();
285                }
286                .boxed_local()
287            }))
288            .expect("main thread message channel should not be closed yet");
289        async move { return_rx.await.expect("main thread message channel") }
290    }
291
292    fn work_dir(&self) -> PathBuf {
293        self.host.work_dir.join(self.manifest.id.as_ref())
294    }
295}
296
297impl wasi::WasiView for WasmState {
298    fn table(&mut self) -> &mut ResourceTable {
299        &mut self.table
300    }
301
302    fn ctx(&mut self) -> &mut wasi::WasiCtx {
303        &mut self.ctx
304    }
305}