bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.95k stars 3.55k forks source link

Bevy App with DefaultPlugins Segfaults on M1 Mac #10524

Closed hedonhermdev closed 1 month ago

hedonhermdev commented 11 months ago

Bevy version

bevy = "0.12.0"

Relevant system information

bash-5.2$ rustc --version
rustc 1.75.0-nightly (edf0b1db0 2023-11-10)
AdapterInfo { name: "Apple M1 Pro", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }

What you did

Tried running code from the tutiorial. I think this is the minimum version of the code that fails for me. I am not very familiar with the ecosystem but happy to help isolate the issue further.

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .run()
}

What went wrong

Bevy app segfaults after printing a single line of tracing log.

2023-11-12T18:31:48.332656Z  INFO bevy_render::renderer: AdapterInfo { name: "Apple M1 Pro", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }
Segmentation fault: 11

Running with lldb seems to be helpful in isolating the issue to wgpu_core

2023-11-12T18:27:06.500994Z  INFO bevy_render::renderer: AdapterInfo { name: "Apple M1 Pro", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }
2023-11-12 23:57:06.889187+0530 first[26749:246755] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x60000000c240> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2023-11-12 23:57:06.914831+0530 first[26749:246755]   HALC_ProxyObjectMap.cpp:153    HALC_ProxyObjectMap::_CopyObjectByObjectID: failed to create the local object
2023-11-12 23:57:06.915343+0530 first[26749:246755]      HALC_ShellDevice.cpp:2609   HALC_ShellDevice::RebuildControlList: couldn't find the control object
Process 26749 stopped
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xa120aaad70)
    frame #0: 0x0000000100f0b600 first`wgpu_core::storage::Storage$LT$T$C$I$GT$::get::h5cf782490d782689(self=0x0000000100000016, id=Id<wgpu_core::resource::TextureView<wgpu_hal::empty::Api>> @ x1) at storage.rs:105:45 [opt]
   102      /// Panics if there is an epoch mismatch, or the entry is empty.
   103      pub(crate) fn get(&self, id: I) -> Result<&T, InvalidId> {
   104          let (index, epoch, _) = id.unzip();
-> 105          let (result, storage_epoch) = match self.map.get(index as usize) {
   106              Some(&Element::Occupied(ref v, epoch)) => (Ok(v), epoch),
   107              Some(&Element::Vacant) => panic!("{}[{}] does not exist", self.kind, index),
   108              Some(&Element::Error(epoch, ..)) => (Err(InvalidId), epoch),
hedonhermdev commented 11 months ago

If it helps, I'm also running this in a nix environment.

`flake.nix` for reference ```nix { devShell = pkgs.mkShell { nativeBuildInputs = with pkgs; [ rustToolchain rustPlatform.bindgenHook darwin.libobjc ]; buildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs.darwin.apple_sdk.frameworks; [ AudioToolbox AudioUnit CoreAudio OpenAL AppKit Metal MetalKit ]); }; } ```
mockersf commented 11 months ago

Works for me without nix. Can you check?

hedonhermdev commented 11 months ago

Is the AdapterInfo output I see as expected? Am I supposed to see something in the driver output?

I am unable to compile the app when I try without Nix. I get an AudioInit/AudioInit.h missing error.

  process didn't exit successfully: `/Users/tirthjain/src/bevy/first/target/debug/build/coreaudio-sys-837c466b273fcef0/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=COREAUDIO_SDK_PATH
  cargo:rustc-link-lib=framework=AudioUnit
  cargo:rustc-link-lib=framework=CoreAudio
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS

  --- stderr
  /Users/tirthjain/.cargo/registry/src/index.crates.io-6f17d22bba15001f/coreaudio-sys-0.2.13/coreaudio.h:1:10: fatal error: 'AudioUnit/AudioUnit.h' file not found
  thread 'main' panicked at /Users/tirthjain/.cargo/registry/src/index.crates.io-6f17d22bba15001f/coreaudio-sys-0.2.13/build.rs:150:39:
  unable to generate bindings: ClangDiagnostic("/Users/tirthjain/.cargo/registry/src/index.crates.io-6f17d22bba15001f/coreaudio-sys-0.2.13/coreaudio.h:1:10: fatal error: 'AudioUnit/AudioUnit.h' file not found\n")
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
hedonhermdev commented 11 months ago

Hmm, tried on a fresh user profile without nix, seems to work. Will try to isolate the issue on the nix end.

sergeypdev commented 11 months ago

I have the same problem without nix.

The problem only appears when I have

[profile.dev.package."*"]
opt-level = 3

opt-level = 0, 1 and 2 work fine.

gak commented 11 months ago

Also got a segfault on startup, M2 Max.

@sergeypdev's suggestion worked, but also switching from nightly to stable worked (while having opt-level = 3).

Also, not using nix, and on Sonoma 14.1. sccache.

Patryk27 commented 11 months ago

Same here (I'm also using Nix, but not in this particular project - there's just Nix installed on my system, but no flake.nix etc. in the project's directory).

In my case it reliably crashes when dropping a vector, though: ``` * thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0) frame #0: 0x0000000100d6f728 super-space-smugglers`core::ptr::drop_in_place$LT$alloc..vec..Vec$LT$wgpu_core..device..queue..TempResource$LT$wgpu_hal..metal..Api$GT$$GT$$GT$::h14008803189cb240 + 16 super-space-smugglers`core::ptr::drop_in_place$LT$alloc..vec..Vec$LT$wgpu_core..device..queue..TempResource$LT$wgpu_hal..metal..Api$GT$$GT$$GT$::h14008803189cb240: -> 0x100d6f728 <+16>: ldr x19, [x0] 0x100d6f72c <+20>: ldr x1, [x0, #0x10] 0x100d6f730 <+24>: mov x0, x19 0x100d6f734 <+28>: bl 0x100d67fb0 ; core::ptr::drop_in_place$LT$$u5b$wgpu_core..device..queue..TempResource$LT$wgpu_hal..metal..Api$GT$$u5d$$GT$::h918dd0b0733e35b1 (.llvm.6438287593069277353) Target 0: (super-space-smugglers) stopped. (lldb) bt * thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0) * frame #0: 0x0000000100d6f728 super-space-smugglers`core::ptr::drop_in_place$LT$alloc..vec..Vec$LT$wgpu_core..device..queue..TempResource$LT$wgpu_hal..metal..Api$GT$$GT$$GT$::h14008803189cb240 + 16 frame #1: 0x0000000100d75f64 super-space-smugglers`wgpu_core::device::resource::Device$LT$A$GT$::create_texture::h794eded992d5f309 + 516 frame #2: 0x0000000100d3ffe4 super-space-smugglers`wgpu_core::device::global::_$LT$impl$u20$wgpu_core..global..Global$LT$G$GT$$GT$::device_create_texture::h2e877d21e7708dbe + 348 frame #3: 0x0000000100d9d168 super-space-smugglers`_$LT$wgpu..backend..direct..Context$u20$as$u20$wgpu..context..Context$GT$::device_create_texture::hf5d90766b8b80942 + 300 frame #4: 0x0000000100da5524 super-space-smugglers`_$LT$T$u20$as$u20$wgpu..context..DynContext$GT$::device_create_texture::hda3244536cc0d95e + 60 frame #5: 0x0000000100e3ed04 super-space-smugglers`_$LT$wgpu..Device$u20$as$u20$wgpu..util..device..DeviceExt$GT$::create_texture_with_data::h0b4712eedbe151cb + 172 frame #6: 0x0000000100a99cd8 super-space-smugglers`bevy_render::texture::fallback_image::fallback_image_new::h20f5f05a928ff6da (.llvm.14164061319848069605) + 544 frame #7: 0x0000000100a9a374 super-space-smugglers`_$LT$bevy_render..texture..fallback_image..FallbackImage$u20$as$u20$bevy_ecs..world..FromWorld$GT$::from_world::h9c2f6322ce121a3b + 1032 frame #8: 0x0000000100ba4b04 super-space-smugglers`_$LT$bevy_render..texture..ImagePlugin$u20$as$u20$bevy_app..plugin..Plugin$GT$::finish::h15106517244ce30c + 996 frame #9: 0x0000000101336530 super-space-smugglers`bevy_app::app::App::finish::h2e29d0df8191eeb8 + 104 frame #10: 0x0000000101336320 super-space-smugglers`bevy_app::app::App::run::hd24cb4357ab3ae86 + 156 frame #11: 0x0000000100005b0c super-space-smugglers`super_space_smugglers::main::h605c3154e4f4e431 + 124 frame #12: 0x00000001000045e8 super-space-smugglers`std::sys_common::backtrace::__rust_begin_short_backtrace::h6b63dbd254529ea3 + 12 frame #13: 0x0000000100004638 super-space-smugglers`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h48f11f8bb7e5f85c + 16 frame #14: 0x00000001014256a8 super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::hcadd4b42bd9d59b8 at function.rs:284:13 [opt] frame #15: 0x00000001014256a0 super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] std::panicking::try::do_call::h0e610377df6ea9bb at panicking.rs:552:40 [opt] frame #16: 0x00000001014256a0 super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] std::panicking::try::h412e62d39ec5e05f at panicking.rs:516:19 [opt] frame #17: 0x00000001014256a0 super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] std::panic::catch_unwind::h6b57e4dfd21ff80a at panic.rs:142:14 [opt] frame #18: 0x00000001014256a0 super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::h2958bcea6cde6c69 at rt.rs:148:48 [opt] frame #19: 0x00000001014256a0 super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] std::panicking::try::do_call::h91708d351044ca16 at panicking.rs:552:40 [opt] frame #20: 0x000000010142569c super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] std::panicking::try::hf7b77fa2c189c6ad at panicking.rs:516:19 [opt] frame #21: 0x000000010142569c super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 [inlined] std::panic::catch_unwind::h4741301cceeb608b at panic.rs:142:14 [opt] frame #22: 0x000000010142569c super-space-smugglers`std::rt::lang_start_internal::hea4720c823b0b053 at rt.rs:148:20 [opt] frame #23: 0x000000010000461c super-space-smugglers`std::rt::lang_start::h969dbd94c9c629dd + 44 frame #24: 0x0000000100005b98 super-space-smugglers`main + 32 frame #25: 0x000000018afe10e0 dyld`start + 2360 ```

edit: works in debug mode, though!

Patryk27 commented 11 months ago

Seems to have been reported at https://github.com/rust-lang/rust/issues/117902 - the recent working toolchain is nightly-2023-10-18.

Edit: it looks like disabling cross-crate inlining "solves" the issue, i.e.:

RUSTFLAGS='-Zcross-crate-inline-threshold=0' cargo run --release
lee-orr commented 11 months ago

Running into this with dexterous_developer as well - for now lowering the optimizations on bevy, but looking forward to a resolution.

gak commented 10 months ago

Just updated nightly: rustc 1.76.0-nightly (a96d57bdb 2023-12-15) (from rustc 1.76.0-nightly (a57770440 2023-11-16))

.. and the segfault went away when using opt-level = 3

Looks like the related issue is fixed: https://github.com/rust-lang/rust/issues/117902#issuecomment-1858840328

BenjaminBrienen commented 1 month ago

@alice-i-cecile should probably close this as stale, unable to reproduce, an upstream/rustc problem, solved, or some combination thereof. :)