PistonDevelopers / glutin_window

A Piston window back-end using the Glutin library
MIT License
25 stars 40 forks source link

Thread panic in Rust 1.48.0 #187

Closed Chetic closed 3 years ago

Chetic commented 3 years ago

This very simple project crashes on startup when using Rust 1.48.0 but not 1.47.0: https://github.com/Chetic/rustler

thread 'main' panicked at 'attempted to zero-initialize type `glutin::ContextWrapper<glutin::PossiblyCurrent, glutin::Window>`, which is invalid', C:\Users\cheti\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\mem\mod.rs:622:9
stack backtrace:
   0:     0x7ff6634bea59 - std::backtrace_rs::backtrace::dbghelp::trace
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs:98
   1:     0x7ff6634bea59 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ff6634bea59 - std::sys_common::backtrace::_print_fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:79
   3:     0x7ff6634bea59 - std::sys_common::backtrace::_print::{{impl}}::fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:58
   4:     0x7ff6634d277b - core::fmt::write
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\fmt\mod.rs:1080
   5:     0x7ff6634bba98 - std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\io\mod.rs:1516
   6:     0x7ff6634c17d4 - std::sys_common::backtrace::_print
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:61
   7:     0x7ff6634c17d4 - std::sys_common::backtrace::print
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:48
   8:     0x7ff6634c17d4 - std::panicking::default_hook::{{closure}}
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:208
   9:     0x7ff6634c13b8 - std::panicking::default_hook
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:227
  10:     0x7ff6634c208f - std::panicking::rust_panic_with_hook
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:577
  11:     0x7ff6634c1bf5 - std::panicking::begin_panic_handler::{{closure}}
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:484
  12:     0x7ff6634bf33f - std::sys_common::backtrace::__rust_end_short_backtrace<closure-0,!>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:153
  13:     0x7ff6634c1ba9 - std::panicking::begin_panic_handler
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:483
  14:     0x7ff6634d04a0 - core::panicking::panic_fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\panicking.rs:85
  15:     0x7ff6634d03ec - core::panicking::panic
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\panicking.rs:50
  16:     0x7ff6632f34a7 - core::mem::zeroed
                               at C:\Users\cheti\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\mem\mod.rs:622
  17:     0x7ff6632f34a7 - glutin_window::{{impl}}::make_current
                               at C:\Users\cheti\.cargo\registry\src\github.com-1ecc6299db9ec823\pistoncore-glutin_window-0.67.0\src\lib.rs:539
  18:     0x7ff6632b0d96 - piston_window::PistonWindow<glutin_window::GlutinWindow>::draw_2d<glutin_window::GlutinWindow,input::Event,closure-0,tuple<>>
                               at C:\Users\cheti\.cargo\registry\src\github.com-1ecc6299db9ec823\piston_window-0.116.0\src\lib.rs:286
  19:     0x7ff66326d5df - rustler::main
                               at C:\dev\rustler\src\main.rs:71
  20:     0x7ff66328097b - core::ops::function::FnOnce::call_once<fn(),tuple<>>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops\function.rs:227
  21:     0x7ff66325a4fb - std::sys_common::backtrace::__rust_begin_short_backtrace<fn(),tuple<>>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\sys_common\backtrace.rs:137
  22:     0x7ff6632b4661 - std::rt::lang_start::{{closure}}<tuple<>>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\rt.rs:66
  23:     0x7ff6634c23d3 - core::ops::function::impls::{{impl}}::call_once
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops\function.rs:280
  24:     0x7ff6634c23d3 - std::panicking::try::do_call
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:381
  25:     0x7ff6634c23d3 - std::panicking::try
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:345
  26:     0x7ff6634c23d3 - std::panic::catch_unwind
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panic.rs:382
  27:     0x7ff6634c23d3 - std::rt::lang_start_internal
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\rt.rs:51
  28:     0x7ff6632b4633 - std::rt::lang_start<tuple<>>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\rt.rs:65
  29:     0x7ff66326d7e0 - main
  30:     0x7ff6634d8394 - invoke_main
                               at d:\agent\_work\63\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  31:     0x7ff6634d8394 - __scrt_common_main_seh
                               at d:\agent\_work\63\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  32:     0x7ff8b4747c24 - BaseThreadInitThunk
  33:     0x7ff8b66ed4d1 - RtlUserThreadStart
error: process didn't exit successfully: `target\debug\rustler.exe` (exit code: 101)

Process finished with exit code 101

Could be related to a compatibility change in Rust 1.48.0 (the first one): https://github.com/rust-lang/rust/blob/1.48.0/RELEASES.md#compatibility-notes Suspect function in glutin_window: https://github.com/PistonDevelopers/glutin_window/blob/master/src/lib.rs#L535

python50 commented 3 years ago

I came here after encountering the same issue, I have reproduced the crash using the "hello world" example for the piston game engine. More details on my particular reproduction steps here: https://github.com/PistonDevelopers/piston-examples/issues/464

A recent change seems to have caused glutin_window to start crashing in a reproducible manner. Piston's "hello world" example definitely used to work without glutin crashing - now it crashes.

Perhaps a change in the standard library behavior(s) in Rust 1.48 versus 1.47?

The problem appears to be here


impl OpenGLWindow for GlutinWindow {
    fn get_proc_address(&mut self, proc_name: &str) -> ProcAddress {
        self.ctx.get_proc_address(proc_name) as *const _
    }

    fn is_current(&self) -> bool {
        self.ctx.is_current()
    }

    fn make_current(&mut self) {
        use std::mem::{replace, zeroed, forget};

        #[allow(invalid_value)]
        let ctx = replace(&mut self.ctx, unsafe{zeroed()});
        forget(replace(&mut self.ctx, unsafe {ctx.make_current().unwrap()}));
    }
}
python50 commented 3 years ago

Apparently, calling std::mem:zeroed() to generate a zero initialized instance of glutin::ContextWrapper<glutin::PossiblyCurrent, glutin::Window> is undefined behavior. This is probably because it contains function references or some other "non-nullable" type.