Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart extension crash #10352

Closed
1 task done
flukejones opened this issue Apr 10, 2024 · 10 comments
Closed
1 task done

Dart extension crash #10352

flukejones opened this issue Apr 10, 2024 · 10 comments
Labels
defect [core label] extension infrastructure Feedback for extensions APIs, creation, management, etc panic / crash [core label]

Comments

@flukejones
Copy link
Contributor

Check for existing issues

  • Completed

Describe the bug / provide steps to reproduce it

Install dart extension as suggested by Zed on opening a dart file.

Environment

Zed: v1.0.0 (Zed Dev 4151ba1)
OS: Linux 1.0.0
Memory: 31 GiB
Architecture: x86_64

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

Thread "main" panicked with "range end index 267037 out of range for slice of length 253952" at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-environ-19.0.0/src/component/translate.rs:537:46
   0: Zed::init_panic_hook::{{closure}}
             at crates/zed/src/main.rs:674:29
   1: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/alloc/src/boxed.rs:2029:9
      std::panicking::rust_panic_with_hook
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:785:13
   2: std::panicking::begin_panic_handler::{{closure}}
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:659:13
   3: std::sys_common::backtrace::__rust_end_short_backtrace
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:171:18
   4: rust_begin_unwind
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:647:5
   5: core::panicking::panic_fmt
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/panicking.rs:72:14
   6: core::slice::index::slice_end_index_len_fail_rt
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/slice/index.rs:76:5
      core::slice::index::slice_end_index_len_fail
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/slice/index.rs:68:9
   7: <core::ops::range::Range<usize> as core::slice::index::SliceIndex<[T]>>::index
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/slice/index.rs:394:13
      core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/slice/index.rs:18:9
      wasmtime_environ::component::translate::Translator::translate_payload
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-environ-19.0.0/src/component/translate.rs:537:46
   8: wasmtime_environ::component::translate::Translator::translate
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-environ-19.0.0/src/component/translate.rs:329:19
   9: wasmtime::compile::build_component_artifacts
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-19.0.0/src/compile.rs:127:9
      wasmtime::runtime::component::component::Component::from_binary
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-19.0.0/src/runtime/component/component.rs:234:41
  10: extension::wasm_host::WasmHost::load_extension::{{closure}}
             at crates/extension/src/wasm_host.rs:114:29
  11: extension::ExtensionStore::extensions_updated::{{closure}}::{{closure}}::{{closure}}::{{closure}}
             at crates/extension/src/extension_store.rs:1086:26
      extension::ExtensionStore::extensions_updated::{{closure}}::{{closure}}
             at crates/extension/src/extension_store.rs:1091:18
  12: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/future/future.rs:124:9
      <async_task::runnable::Builder<M>::spawn_local::Checked<F> as core::future::future::Future>::poll
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-task-4.7.0/src/runnable.rs:455:26
      async_task::raw::RawTask<F,T,S,M>::run
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-task-4.7.0/src/raw.rs:557:17
  13: async_task::runnable::Runnable<M>::run
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-task-4.7.0/src/runnable.rs:781:18
      gpui::platform::linux::x11::client::X11Client::new::{{closure}}
             at crates/gpui/src/platform/linux/x11/client.rs:84:26
      <core::cell::RefCell<calloop::sources::DispatcherInner<S,F>> as calloop::sources::EventDispatcher<Data>>::process_events::{{closure}}
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/sources/mod.rs:327:61
      <calloop::sources::channel::Channel<T> as calloop::sources::EventSource>::process_events::{{closure}}
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/sources/channel.rs:187:32
      <calloop::sources::ping::eventfd::PingSource as calloop::sources::EventSource>::process_events::{{closure}}
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/sources/ping/eventfd.rs:138:21
      <calloop::sources::generic::Generic<F,E> as calloop::sources::EventSource>::process_events
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/sources/generic.rs:290:9
      <calloop::sources::ping::eventfd::PingSource as calloop::sources::EventSource>::process_events
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/sources/ping/eventfd.rs:127:9
      <calloop::sources::channel::Channel<T> as calloop::sources::EventSource>::process_events
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/sources/channel.rs:184:9
      <core::cell::RefCell<calloop::sources::DispatcherInner<S,F>> as calloop::sources::EventDispatcher<Data>>::process_events
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/sources/mod.rs:326:9
  14: calloop::loop_logic::EventLoop<Data>::dispatch_events
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/loop_logic.rs:445:31
      calloop::loop_logic::EventLoop<Data>::dispatch
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/loop_logic.rs:559:9
      calloop::loop_logic::EventLoop<Data>::run
             at /home/luke/.cargo/registry/src/index.crates.io-6f17d22bba15001f/calloop-0.12.4/src/loop_logic.rs:596:13
      <gpui::platform::linux::wayland::client::WaylandClient as gpui::platform::linux::platform::LinuxClient>::run
             at crates/gpui/src/platform/linux/wayland/client.rs:310:9
  15: gpui::platform::linux::platform::<impl gpui::platform::Platform for P>::run
             at crates/gpui/src/platform/linux/platform.rs:178:9
  16: gpui::app::App::run
             at crates/gpui/src/app.rs:142:9
  17: Zed::main
             at crates/zed/src/main.rs:160:5
  18: core::ops::function::FnOnce::call_once
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/ops/function.rs:250:5
      std::sys_common::backtrace::__rust_begin_short_backtrace
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/sys_common/backtrace.rs:155:18
  19: std::rt::lang_start::{{closure}}
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/rt.rs:166:18
  20: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/ops/function.rs:284:13
      std::panicking::try::do_call
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:554:40
      std::panicking::try
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:518:19
      std::panic::catch_unwind
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panic.rs:142:14
      std::rt::lang_start_internal::{{closure}}
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/rt.rs:148:48
      std::panicking::try::do_call
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:554:40
      std::panicking::try
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:518:19
      std::panic::catch_unwind
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panic.rs:142:14
      std::rt::lang_start_internal
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/rt.rs:148:20
  21: main
  22: __libc_start_call_main
  23: __libc_start_main_impl
  24: _start
@flukejones flukejones added admin read Pending admin review defect [core label] panic / crash [core label] triage Maintainer needs to classify the issue labels Apr 10, 2024
@pedrosoares
Copy link
Contributor

Same problem here.

To disable the failing extension just remove the part bellow from the file ~/Library/Application\ Support/Zed/extensions/index.json

"dart": {
      "manifest": {
        "id": "dart",
        "name": "Dart",
        "version": "0.0.1",
        "schema_version": 1,
        "description": "Dart support.",
        "repository": "https://github.com/zed-industries/zed",
        "authors": [
          "Abdullah Alsigar <abdullah.alsigar@gmail.com>",
          "Flo <flo80@users.noreply.github.com>"
        ],
        "lib": {
          "kind": "Rust",
          "version": "0.0.6"
        },
        "themes": [],
        "languages": [
          "languages/dart"
        ],
        "grammars": {
          "dart": {
            "repository": "https://github.com/agent3bood/tree-sitter-dart",
            "rev": "48934e3bf757a9b78f17bdfaa3e2b4284656fdc7"
          }
        },
        "language_servers": {
          "dart": {
            "language": "Dart"
          }
        }
      },
      "dev": false
    },

Environment

Zed: Zed 0.129.2
OS: Mac Os Sonoma 14.4
Memory: 32 GiB
Architecture: x86_64

@maxdeviant
Copy link
Member

@flukejones Do you see the same crash if you install the Dart extension using "Install Dev Extension" and pointing to extensions/dart in the Zed repo?

@maxbrunsfeld
Copy link
Collaborator

For anyone who is experiencing this crash (@flukejones, @pedrosoares) could you share the content of the following file?

~/Library/Application Support/Zed/extensions/installed/dart/extension.wasm

From the panic, it looks like the installed wasm file is somehow invalid. I'm curious if I can reproduce the problem if I have the same wasm file as you.

@maxbrunsfeld
Copy link
Collaborator

Ok, never mind, we have found a way of reproducing the bug, by truncating a WASM file at a certain position.

@PedroSolares
Copy link

PedroSolares commented Apr 10, 2024

@maxbrunsfeld I did a build from master (26299fb) and error log was the fallowing:

[2024-04-10T12:16:07-03:00 ERROR util] crates/extension/src/extension_store.rs:1093: failed to load wasm extension dart

Caused by:
    unexpected end-of-file (at offset 0x2c684)

I did head and tail this file ~/Library/Application Support/Zed/extensions/installed/dart/extension.wasm:

Head

qtextmethofunction
                  constructorfielvariableclass	interfacemodulpropertyunitvalueenumkeywordsnippetcolorfile	referencefolder
                                                                                                                               enum-membeconstantstructevenoperatortype-parameterotherzcompletion-kindq
plain-textsnippetotherzinsert-text-formatkskkrlabelsdetailkindinsert-text-format
completionqilemodule	namespacepackageclassmethopropertyfield
                                                               constructorenum	interfacfunctiovariablconstantstringnumberbooleanarrayobjectkeynull
                                                                                                                                                   enum-memberstructevenoperatortype-parameterotherz
                                                                                                                                                                                                    symbol-kind	rkind
namessymbol

zed:extension/lsp$Bossp@get-environment
wasi:cli/environment@0.2.0Bj@statusexit
Berrorli/exit@0.2.0
wasi:io/error@0.2.0
output-streamerroriqlast-operation-failedclosed
                                               stream-error
                                                           input-streamhjw@sel![method]output-stream.check-write	p}j@selcontents

ethod]output-stream.write
                         .[method]output-stream.blocking-write-and-flush
                                                                        @self
                                                                             $[method]output-stream.blocking-flush

Tail

get-stderr
get-stderr	exit
exit 	get-stdin       get-stdin!
get-stdou?
get-stdout"
           __main_module__envwasi:filesystem/preopens@0.2.0asi:filesystem/types@0.2.0wasi:io/error@0.2.wasi:io/streams@0.2.0	wasi:cli/environment@0.2.0
wasi:cli/stderr@0.2.0
                     wasi:cli/exit@0.2.0
wasi:cli/stdout@0.2.0>cabi_export_realloccabi_import_reallo$imports



get-directories
$(	#[method]descriptor.write-via-stream    )	$[method]descriptor.append-via-stream
 	ethod]descriptor.get-type
                                	[method]descriptor.stat
(       #[method]descriptor.metadata-hash-at    filesystem-error-cod&![method]output-stream.check-writ ethod]output-stream.writ3.[method]output-stream.blocking-write-and-flus)$[method]output-stream.blocking-flusget-environment
$fd_write
         environ_getenviron_sizes_getfd_prestat_getfd_prestat_dir_namepath_filestat_get	proc_exit$imports0%1&2'3(4)5*6+7,8-9.10/11012113214315416517618719820921:22;@init-extensio<9
                                                                                                                                                                                    init-extension(j"s@language-server-idworktree1:Clanguage-server-command!cabi_post_lan=uage-server-command
 >;
?  language-server-command(j$s@language-server-idworktree1<a&language-server-initialization-options0cabi_post_language-server-initialization-options
 @=
A  ,&language-server-initialization-optionsc'language-server-workspace-configuration1cabi_post_language-server-workspace-configuration
 B=
   -'language-server-workspace-configurationpk-p?j?s@language-server-ids
C                                                                       completions>?Alabels-for-completions cabi_post_labels-for-completions
 DB
E  labels-for-completions&p@language-server-idssymbols??9labels-for-symbolscabi_post_labels-for-symbols
 FD
   labels-for-symbols/	producers
wit-component0.201.0%    

My internet connection it is unstable currently, probably downloaded the file partialy?

@maxbrunsfeld
Copy link
Collaborator

We opened a Wasmtime issue for this bug, and have added a workaround for it in Zed. bytecodealliance/wasmtime#8322

@maxbrunsfeld
Copy link
Collaborator

In the meantime, you should try reinstalling the Dart extension. It looks like your download was partially corrupted.

maxdeviant added a commit that referenced this issue Apr 10, 2024
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](#10352)).

---------

Co-authored-by: Max <max@zed.dev>
gcp-cherry-pick-bot bot pushed a commit that referenced this issue Apr 10, 2024
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](#10352)).

---------

Co-authored-by: Max <max@zed.dev>
gcp-cherry-pick-bot bot pushed a commit that referenced this issue Apr 10, 2024
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](#10352)).

---------

Co-authored-by: Max <max@zed.dev>
maxdeviant added a commit that referenced this issue Apr 10, 2024
)

Cherry-picked 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](#10352)).

---------

Co-authored-by: Max <max@zed.dev>

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
Co-authored-by: Max <max@zed.dev>
@pedrosoares
Copy link
Contributor

In the meantime, you should try reinstalling the Dart extension. It looks like your download was partially corrupted.

Can confirm, it works.

maxdeviant added a commit that referenced this issue Apr 10, 2024
)

Cherry-picked 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](#10352)).

---------

Co-authored-by: Max <max@zed.dev>

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
Co-authored-by: Max <max@zed.dev>
@JosephTLyons
Copy link
Contributor

The fix for this just went out in v0.130.5.

@JosephTLyons JosephTLyons added extension infrastructure Feedback for extensions APIs, creation, management, etc and removed triage Maintainer needs to classify the issue admin read Pending admin review labels Apr 10, 2024
@flukejones
Copy link
Contributor Author

Sorry about not following up sooner (I'm UTC+12). Excellent work y'all, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect [core label] extension infrastructure Feedback for extensions APIs, creation, management, etc panic / crash [core label]
Projects
None yet
Development

No branches or pull requests

6 participants