ZIm/crates/extension/src
Marshall Bowers 081e9b9a60
Fix panic when loading malformed Wasm files (#10370)
This PR fixes a potential panic that could occur when loading malformed
Wasm files.

We now use the `parse_wasm_extension_version` function that was
previously used just to extract the Zed extension API version from the
Wasm bytes as a pre-validation step. By parsing the entirety of the Wasm
file here instead of returning as soon as we find the version, the
invalid Wasm bytes are now surfaced as an `Err` instead of a panic.

We were able to replicate the panic using the following test:

```rs
#[gpui::test]
async fn test_bad_wasm(cx: &mut TestAppContext) {
    init_test(cx);

    let wasm_host = cx.update(|cx| {
        WasmHost::new(
            FakeFs::new(cx.background_executor().clone()),
            FakeHttpClient::with_200_response(),
            FakeNodeRuntime::new(),
            Arc::new(LanguageRegistry::test(cx.background_executor().clone())),
            PathBuf::from("/the/work/dir".to_string()),
            cx,
        )
    });

    let mut wasm_bytes = std::fs::read("/Users/maxdeviant/Library/Application Support/Zed/extensions/installed/dart/extension.wasm").unwrap();

    // This is the error message we were seeing in the stack trace:
    // range end index 267037 out of range for slice of length 253952

    dbg!(&wasm_bytes.len());

    // Truncate the bytes to the same point:
    wasm_bytes.truncate(253952);

    std::fs::write("/tmp/bad-extension.wasm", wasm_bytes.clone()).unwrap();

    let manifest = Arc::new(ExtensionManifest {
        id: "the-extension".into(),
        name: "The Extension".into(),
        version: "0.0.1".into(),
        schema_version: SchemaVersion(1),
        description: Default::default(),
        repository: Default::default(),
        authors: Default::default(),
        lib: LibManifestEntry {
            kind: None,
            version: None,
        },
        themes: Default::default(),
        languages: Default::default(),
        grammars: Default::default(),
        language_servers: Default::default(),
    });

    // 💥
    let result = wasm_host
        .load_extension(wasm_bytes, manifest, cx.executor())
        .await;
    dbg!(result.map(|_| ()));
```



Release Notes:

- Fixed a crash that could occur when loading malformed Wasm extensions
([#10352](https://github.com/zed-industries/zed/issues/10352)).

---------

Co-authored-by: Max <max@zed.dev>
2024-04-10 14:13:43 -04:00
..
wasm_host Sanitize ranges in code labels coming from extensions (#10307) 2024-04-08 19:53:25 -07:00
extension_builder.rs Add support for building a Tree-sitter grammar at a given path (#9965) 2024-03-29 14:30:10 -04:00
extension_lsp_adapter.rs Only apply host-side executable fix to binaries downloaded by the extension (#10318) 2024-04-09 09:13:36 -04:00
extension_manifest.rs Add support for using a language server with multiple languages (#10293) 2024-04-08 14:24:56 -04:00
extension_settings.rs Add the ability for extensions to provide language settings (#10296) 2024-04-08 19:17:12 -04:00
extension_store.rs Add support for using a language server with multiple languages (#10293) 2024-04-08 14:24:56 -04:00
extension_store_test.rs Add label_for_completion to extension API (#10175) 2024-04-04 13:56:04 -04:00
wasm_host.rs Fix panic when loading malformed Wasm files (#10370) 2024-04-10 14:13:43 -04:00