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

Plugin crashes with rkyv LayoutError. #6807

Closed
Drevoed opened this issue Jan 13, 2023 · 20 comments · Fixed by #7154 or #7170
Closed

Plugin crashes with rkyv LayoutError. #6807

Drevoed opened this issue Jan 13, 2023 · 20 comments · Fixed by #7154 or #7170
Assignees
Milestone

Comments

@Drevoed
Copy link
Contributor

Drevoed commented Jan 13, 2023

Describe the bug

SWC crashes when trying to invoke a plugin.

versions:

  • @swc/core - 1.3.26
  • swc_core - 0.56.1

backtrace:

❯ SWC_DEBUG=1 RUST_BACKTRACE=full swc --sync demo/src/index.ts
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: LayoutError', /Users/k.mironov/.cargo/registry/src/github.com-1ecc6299db9ec823/rkyv-0.7.37/src/impls/core/mod.rs:265:67
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Panic: PanicInfo { payload: Any { .. }, message: Some(failed to invoke plugin: failed to invoke plugin on 'Some("demo/src/index.ts")'

Caused by:
    0: failed to invoke `@effector/swc-plugin` as js transform plugin at node_modules/@effector/swc-plugin/target/wasm32-wasi/debug/effector_swc_plugin.wasm
    1: RuntimeError: unreachable
           at __rust_start_panic (<module>[11103]:0x438b25)
           at rust_panic (<module>[11093]:0x438815)
           at std::panicking::rust_panic_with_hook::had8ec4151dd4c568 (<module>[11092]:0x4387e4)
           at std::panicking::begin_panic_handler::{{closure}}::h2ac729f2457e9805 (<module>[11080]:0x437aa2)
           at std::sys_common::backtrace::__rust_end_short_backtrace::hfe2c5b74343c0cd6 (<module>[11079]:0x4379cc)
           at rust_begin_unwind (<module>[11087]:0x438060)
           at core::panicking::panic_fmt::hd7053c514eb59581 (<module>[11226]:0x4461cd)
           at core::result::unwrap_failed::h7e5c994e75ab37e8 (<module>[11275]:0x44ca56)
           at core::result::Result<T,E>::unwrap::h2356290abca8ee50 (<module>[10820]:0x4287c8)
           at rkyv::impls::core::<impl rkyv::DeserializeUnsized<[U],D> for [T]>::deserialize_unsized::h315b3a8965376629 (<module>[5344]:0x2b5bfa)
           at rkyv::impls::alloc::vec::<impl rkyv::Deserialize<alloc::vec::Vec<T>,D> for rkyv::vec::ArchivedVec<<T as rkyv::Archive>::Archived>>::deserialize::h04d1ec3ea3e970d8 (<module>[222]:0x13be0)
           at swc_ecma_ast::module_decl::_::<impl rkyv::Deserialize<swc_ecma_ast::module_decl::ImportDecl,__D> for <swc_ecma_ast::module_decl::ImportDecl as rkyv::Archive>::Archived>::deserialize::h185c61ae65c5c0b5 (<module>[5304]:0x2a51dd)
           at swc_ecma_ast::module_decl::_::<impl rkyv::Deserialize<swc_ecma_ast::module_decl::ModuleDecl,__D> for <swc_ecma_ast::module_decl::ModuleDecl as rkyv::Archive>::Archived>::deserialize::h952789b151c9a970 (<module>[5305]:0x2a597c)
           at swc_ecma_ast::module::_::<impl rkyv::Deserialize<swc_ecma_ast::module::ModuleItem,__D> for <swc_ecma_ast::module::ModuleItem as rkyv::Archive>::Archived>::deserialize::h88d8e5b435c0c70e (<module>[5724]:0x2cf7ff)
           at rkyv::impls::core::<impl rkyv::DeserializeUnsized<[U],D> for [T]>::deserialize_unsized::h006528daf2248e8c (<module>[5738]:0x2d413c)
           at rkyv::impls::alloc::vec::<impl rkyv::Deserialize<alloc::vec::Vec<T>,D> for rkyv::vec::ArchivedVec<<T as rkyv::Archive>::Archived>>::deserialize::h380c70590928e990 (<module>[227]:0x147f2)
           at swc_ecma_ast::module::_::<impl rkyv::Deserialize<swc_ecma_ast::module::Module,__D> for <swc_ecma_ast::module::Module as rkyv::Archive>::Archived>::deserialize::h86f80a54809dc175 (<module>[5721]:0x2ce741)
           at swc_ecma_ast::module::_::<impl rkyv::Deserialize<swc_ecma_ast::module::Program,__D> for <swc_ecma_ast::module::Program as rkyv::Archive>::Archived>::deserialize::he0c1e07561c3b81a (<module>[5723]:0x2cefcf)
           at swc_common::plugin::serialized::PluginSerializedBytes::deserialize::hd942b90ece579bfb (<module>[4013]:0x1ad338)
           at swc_common::plugin::serialized::deserialize_from_ptr::h3a43eb3fbcea6d36 (<module>[4012]:0x1ac686)
           at __transform_plugin_process_impl (<module>[6596]:0x31c0a3)
           at __transform_plugin_process_impl.command_export (<module>[11393]:0x45418b)
    2: unreachable

Stack backtrace:
   0: _napi_register_module_v1
   1: _wasmer_vm_raise_trap
   2: _napi_register_module_v1
   3: _napi_register_module_v1
   4: _napi_register_module_v1
   5: _napi_register_module_v1
   6: _napi_register_module_v1
   7: _napi_register_module_v1
   8: <unknown>
   9: __ZN6v8impl12_GLOBAL__N_123FunctionCallbackWrapper6InvokeERKN2v820FunctionCallbackInfoINS2_5ValueEEE
  10: __ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEENS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EEPmi
  11: __ZN2v88internal21Builtin_HandleApiCallEiPmPNS0_7IsolateE), location: Location { file: "/Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/swc-0.241.5/src/plugin.rs", line: 228, col: 14 }, can_unwind: true }
Backtrace:    0: <unknown>
   1: <unknown>
   2: _napi_register_module_v1
   3: _napi_register_module_v1
   4: _napi_register_module_v1
   5: _napi_register_module_v1
   6: _wasmer_vm_raise_trap
   7: _wasmer_vm_raise_trap
   8: _napi_register_module_v1
   9: _napi_register_module_v1
  10: _napi_register_module_v1
  11: _napi_register_module_v1
  12: <unknown>
  13: __ZN6v8impl12_GLOBAL__N_123FunctionCallbackWrapper6InvokeERKN2v820FunctionCallbackInfoINS2_5ValueEEE
  14: __ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEENS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EEPmi
  15: __ZN2v88internal21Builtin_HandleApiCallEiPmPNS0_7IsolateE

failed to handle: failed to invoke plugin: failed to invoke plugin on 'Some("demo/src/index.ts")'

Caused by:
    0: failed to invoke `@effector/swc-plugin` as js transform plugin at node_modules/@effector/swc-plugin/target/wasm32-wasi/debug/effector_swc_plugin.wasm
    1: RuntimeError: unreachable
           at __rust_start_panic (<module>[11103]:0x438b25)
           at rust_panic (<module>[11093]:0x438815)
           at std::panicking::rust_panic_with_hook::had8ec4151dd4c568 (<module>[11092]:0x4387e4)
           at std::panicking::begin_panic_handler::{{closure}}::h2ac729f2457e9805 (<module>[11080]:0x437aa2)
           at std::sys_common::backtrace::__rust_end_short_backtrace::hfe2c5b74343c0cd6 (<module>[11079]:0x4379cc)
           at rust_begin_unwind (<module>[11087]:0x438060)
           at core::panicking::panic_fmt::hd7053c514eb59581 (<module>[11226]:0x4461cd)
           at core::result::unwrap_failed::h7e5c994e75ab37e8 (<module>[11275]:0x44ca56)
           at core::result::Result<T,E>::unwrap::h2356290abca8ee50 (<module>[10820]:0x4287c8)
           at rkyv::impls::core::<impl rkyv::DeserializeUnsized<[U],D> for [T]>::deserialize_unsized::h315b3a8965376629 (<module>[5344]:0x2b5bfa)
           at rkyv::impls::alloc::vec::<impl rkyv::Deserialize<alloc::vec::Vec<T>,D> for rkyv::vec::ArchivedVec<<T as rkyv::Archive>::Archived>>::deserialize::h04d1ec3ea3e970d8 (<module>[222]:0x13be0)
           at swc_ecma_ast::module_decl::_::<impl rkyv::Deserialize<swc_ecma_ast::module_decl::ImportDecl,__D> for <swc_ecma_ast::module_decl::ImportDecl as rkyv::Archive>::Archived>::deserialize::h185c61ae65c5c0b5 (<module>[5304]:0x2a51dd)
           at swc_ecma_ast::module_decl::_::<impl rkyv::Deserialize<swc_ecma_ast::module_decl::ModuleDecl,__D> for <swc_ecma_ast::module_decl::ModuleDecl as rkyv::Archive>::Archived>::deserialize::h952789b151c9a970 (<module>[5305]:0x2a597c)
           at swc_ecma_ast::module::_::<impl rkyv::Deserialize<swc_ecma_ast::module::ModuleItem,__D> for <swc_ecma_ast::module::ModuleItem as rkyv::Archive>::Archived>::deserialize::h88d8e5b435c0c70e (<module>[5724]:0x2cf7ff)
           at rkyv::impls::core::<impl rkyv::DeserializeUnsized<[U],D> for [T]>::deserialize_unsized::h006528daf2248e8c (<module>[5738]:0x2d413c)
           at rkyv::impls::alloc::vec::<impl rkyv::Deserialize<alloc::vec::Vec<T>,D> for rkyv::vec::ArchivedVec<<T as rkyv::Archive>::Archived>>::deserialize::h380c70590928e990 (<module>[227]:0x147f2)
           at swc_ecma_ast::module::_::<impl rkyv::Deserialize<swc_ecma_ast::module::Module,__D> for <swc_ecma_ast::module::Module as rkyv::Archive>::Archived>::deserialize::h86f80a54809dc175 (<module>[5721]:0x2ce741)
           at swc_ecma_ast::module::_::<impl rkyv::Deserialize<swc_ecma_ast::module::Program,__D> for <swc_ecma_ast::module::Program as rkyv::Archive>::Archived>::deserialize::he0c1e07561c3b81a (<module>[5723]:0x2cefcf)
           at swc_common::plugin::serialized::PluginSerializedBytes::deserialize::hd942b90ece579bfb (<module>[4013]:0x1ad338)
           at swc_common::plugin::serialized::deserialize_from_ptr::h3a43eb3fbcea6d36 (<module>[4012]:0x1ac686)
           at __transform_plugin_process_impl (<module>[6596]:0x31c0a3)
           at __transform_plugin_process_impl.command_export (<module>[11393]:0x45418b)
    2: unreachable

Stack backtrace:
   0: _napi_register_module_v1
   1: _wasmer_vm_raise_trap
   2: _napi_register_module_v1
   3: _napi_register_module_v1
   4: _napi_register_module_v1
   5: _napi_register_module_v1
   6: _napi_register_module_v1
   7: _napi_register_module_v1
   8: <unknown>
   9: __ZN6v8impl12_GLOBAL__N_123FunctionCallbackWrapper6InvokeERKN2v820FunctionCallbackInfoINS2_5ValueEEE
  10: __ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEENS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EEPmi
  11: __ZN2v88internal21Builtin_HandleApiCallEiPmPNS0_7IsolateE

Stack backtrace:
   0: _napi_register_module_v1
   1: _wasmer_vm_raise_trap
   2: <unknown>
   3: __ZN6v8impl12_GLOBAL__N_123FunctionCallbackWrapper6InvokeERKN2v820FunctionCallbackInfoINS2_5ValueEEE
   4: __ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEENS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EEPmi
   5: __ZN2v88internal21Builtin_HandleApiCallEiPmPNS0_7IsolateE
Error: Failed to compile 1 file with swc.
    at Object.assertCompilationResult (/Users/k.mironov/WebstormProjects/swc-plugin/node_modules/.pnpm/@swc+cli@0.1.59_@swc+core@1.3.26/node_modules/@swc/cli/lib/swc/util.js:123:15)
    at files (/Users/k.mironov/WebstormProjects/swc-plugin/node_modules/.pnpm/@swc+cli@0.1.59_@swc+core@1.3.26/node_modules/@swc/cli/lib/swc/file.js:173:18)
    at async _default (/Users/k.mironov/WebstormProjects/swc-plugin/node_modules/.pnpm/@swc+cli@0.1.59_@swc+core@1.3.26/node_modules/@swc/cli/lib/swc/file.js:192:9)

Input code

import {createEvent, createStore} from 'effector';
import {factory} from "./factory";

const increment = createEvent();

console.log(createStore(createEvent()))
console.log(factory(increment));

Config

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "experimental": {
      "plugins": [
        [
          "@effector/swc-plugin",
          {
            "addLoc": true,
            "factories": ["./tests/fixtures/factories/factory"]
          }
        ]
      ]
    }
  }
}

Playground link

No response

Expected behavior

Plugin runs.

Actual behavior

Plugin system crashes.

Version

1.3.26

Additional context

https://github.com/effector/swc-plugin

Repository of a plugin, you can run cargo build and pnpm i to reproduce it there.

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2023

This is an issue of @effector/swc-plugin.
There was a breaking change of AST due to TypeScript 5.0.

019cf37

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 13, 2023
@Drevoed
Copy link
Contributor Author

Drevoed commented Jan 13, 2023

Yes, but the plugin is recompiled without any errors with the latest version. Shouldn't the AST match then?

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2023

No. rkyv does not work in such way

@Drevoed
Copy link
Contributor Author

Drevoed commented Jan 13, 2023

How to fix this then? I'm genuinely curious. I though that if the in-memory representation matches both in plugin and in host - then everything should be alright.

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2023

You should update swc_core and publish a new version

@Drevoed
Copy link
Contributor Author

Drevoed commented Jan 13, 2023

Yeah well as the first message said: versions are up to date.

package.json:
@swc/core - 1.3.26

Cargo.toml:
swc_core - 0.56.1

Just tried it locally, same thing happens. Otherwise I would not open this issue 😃

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2023

@Drevoed
Copy link
Contributor Author

Drevoed commented Jan 13, 2023

I updated the repository. You can reproduce the bug there now.

Uses debug build.

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2023

It fails with

오후 8:29:09 [vite] Internal server error: failed to handle: failed to invoke plugin: failed to invoke plugin on 'Some("/Users/kdy1/projects/lab/swc-plugin/demo/src/index.ts")'

Caused by:
    0: failed to invoke `@effector/swc-plugin` as js transform plugin at node_modules/@effector/swc-plugin/target/wasm32-wasi/release/effector_swc_plugin.wasm
    1: RuntimeError: call stack exhausted
    2: stk_ovf
  Plugin: swc
  File: /Users/kdy1/projects/lab/swc-plugin/demo/src/index.ts

not layout error

@Drevoed
Copy link
Contributor Author

Drevoed commented Jan 13, 2023

Well it crashes on my machine and some more users with LayoutError :(

Some also report heap_get_oob crash which mught be related to #6464 and #5060.

So it seems like there is definitely some kind of UB goind around, that's why I'm against closing the issue.

Machine is BTW:

macOS monterey 12.6.1
intel chip

@kdy1 kdy1 reopened this Jan 13, 2023
@kwonoj kwonoj self-assigned this Jan 13, 2023
@kwonoj
Copy link
Member

kwonoj commented Jan 13, 2023

Tentatively assigned myself to try repro. (Well, if I have some time..)

@Drevoed
Copy link
Contributor Author

Drevoed commented Jan 20, 2023

@kwonoj

I've managed to find the cuplrit. This crash happens only if plugin is compiled with a nightly rustc.

Will try to bisect further to find out on exactly which version of swc and nightly rustc this has started to happen, but I think this will still be helpful.

@thekip
Copy link

thekip commented Jan 24, 2023

@Drevoed it seems author of stailwc also encountered this issue, and had to fix nightly version in theirs pipilines https://github.com/arlyon/stailwc/blob/master/.github/workflows/release.yml#L21

@ducaale
Copy link

ducaale commented Feb 5, 2023

In case it helps, I was facing LayoutError error with Vite 4 and a "hello world" SWC plugin. I had to downgrade my rust toolchain from 1.67 to 1.66 to workaround this issue.

@kdy1
Copy link
Member

kdy1 commented Feb 8, 2023

Can you check nightly-2023-02-08? Seems like it's working.

@kdy1
Copy link
Member

kdy1 commented Feb 9, 2023

@Drevoed What was your initial rustc version?
I want to add it to rkyv/rkyv#338

SevereCloud added a commit to VKCOM/swc-plugin-transform-remove-imports that referenced this issue Feb 9, 2023
SevereCloud added a commit to VKCOM/swc-plugin-transform-remove-imports that referenced this issue Feb 9, 2023
@thekip
Copy link

thekip commented Feb 23, 2023

@kdy1 also confirms LayoutError when build with rust 1.67. Downgrading to 1.65 (or as mentioned earlier to 1.66) fixes the issue.

@arlyon
Copy link

arlyon commented Mar 10, 2023

Chiming in here as I have been trying to bisect nightly toolchain versions to find the exact one that crashes. At least regarding the heap oob error, it is not quite as simple as 1.66 vs 1.67. The error appears on all platforms on nightly-2022-11-24 and above.

nightly-2022-11-23

vite v4.1.3 building for production...
✓ 62 modules transformed.
dist/index.html                  0.40 kB
dist/assets/index-5afa1126.js  182.71 kB │ gzip: 59.83 kB
✨  Done in 1.45s.

nightly-2022-11-24

Caused by:
    0: failed to invoke `/Users/arlyon/Programming/stailwc/target/wasm32-wasi/release/stailwc.wasm` as js transform plugin at /Users/arlyon/Programming/stailwc/target/wasm32-wasi/release/stailwc.wasm
    1: RuntimeError: out of bounds memory access
    2: heap_get_oob

That leads us to this set of commits:

https://github.com/rust-lang/rust/commits/master?since=2022-11-23&until=2022-11-24

note: I also get the same issue on nightly-2023-02-08

@kdy1
Copy link
Member

kdy1 commented Mar 29, 2023

Reopening as rkyv bug is not fixed

@kdy1 kdy1 reopened this Mar 29, 2023
kdy1 added a commit that referenced this issue Mar 30, 2023
**Description:**

This PR also updates `rkyv` to `=0.7.40`.

**Related issue:**

 - Closes #6807.
@kdy1 kdy1 modified the milestones: Planned, v1.3.44 Mar 30, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Apr 29, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Apr 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.