rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.43k stars 12.6k forks source link

Compiling project makes rustc emit STATUS_ILLEGAL_INSTRUCTION #77667

Open ralfbiedert opened 4 years ago

ralfbiedert commented 4 years ago

I was trying to compile this 3rd party project with cargo build when I received an exit code: 0xc000001d, STATUS_ILLEGAL_INSTRUCTION.

Depending on what Rust version I use I get slightly different errors, but I can reproduce the behavior as follows:

Command Version Result
cargo build 1.46 STATUS_ILLEGAL_INSTRUCTION
cargo +beta build 1.48.0-beta STATUS_STACK_BUFFER_OVERRUN
cargo +nightly build 2020-10-06 STATUS_STACK_BUFFER_OVERRUN.
cargo build --release 1.46 Compiles.
cargo +beta build --release 1.48.0-beta Compiles.
cargo +nightly build --release 2020-10-06 Compiles.
cargo check 1.46 Works and reports no issues.

In other words --release builds are fine, while debug builds fail.

A failing build that produces a STATUS_STACK_BUFFER_OVERRUN looks like this:

PS D:\Development\Source\_thirdparty\rust-cheatsheet> cargo +nightly build
   Compiling proc-macro2 v1.0.19
   Compiling unicode-xid v0.2.1
   Compiling syn v1.0.38
   Compiling memchr v2.3.3
   Compiling version_check v0.1.5
   Compiling serde_derive v1.0.115
   Compiling bytes v0.5.6
   Compiling v_escape v0.12.1
   Compiling serde v1.0.115
   Compiling v_htmlescape v0.10.0
   Compiling bitflags v1.2.1
   Compiling linked-hash-map v0.5.3
   Compiling cfg-if v0.1.10
   Compiling dtoa v0.4.6
   Compiling either_n v0.2.0
   Compiling lazy_static v1.4.0
   Compiling yaml-rust v0.4.4
   Compiling nom v4.2.3
   Compiling buf-min v0.1.1
   Compiling combine v4.2.1
   Compiling quote v1.0.7
   Compiling v_escape_derive v0.8.1
   Compiling serde_yaml v0.8.13
   Compiling cheatsheet-gen v0.1.0 (D:\Development\Source\_thirdparty\rust-cheatsheet)
error: Stream Error: The stream is too short to perform the requested operation.
error: could not compile `cheatsheet-gen`

Caused by:
  process didn't exit successfully: `rustc --crate-name cheatsheet_gen --edition=2018 src\main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=db2d8889f40e140f --out-dir D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps -C incremental=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\incremental -L dependency=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps --extern bitflags=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libbitflags-b3aa381b8c8a158c.rlib --extern combine=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libcombine-a4a9f1aaa590fa13.rlib --extern either_n=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libeither_n-8a7740130419f71a.rlib --extern lazy_static=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\liblazy_static-c07407d0f2563334.rlib --extern serde=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libserde-a435d4a09a21010e.rlib --extern serde_yaml=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libserde_yaml-ee801d4aeb4bdd7f.rlib --extern v_htmlescape=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libv_htmlescape-a10e79e7a0db13f9.rlib` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

A build that fails with STATUS_ILLEGAL_INSTRUCTION looks similar but outputs this instead at the end:

 `rustc --crate-name cheatsheet_gen --edition=2018 src\main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=db2d8889f40e140f --out-dir D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps -C incremental=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\incremental -L dependency=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps --extern bitflags=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libbitflags-4b4428b676fd67d4.rlib --extern combine=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libcombine-70407e7c92f08ddf.rlib --extern either_n=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libeither_n-7aad0be053028d4d.rlib --extern lazy_static=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\liblazy_static-3058c6f12d44e685.rlib --extern serde=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libserde-7dcd74385906b4ee.rlib --extern serde_yaml=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libserde_yaml-0e470368d8293784.rlib --extern v_htmlescape=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libv_htmlescape-fe2fd1af7676b224.rlib` (exit code: 0xc000001d, STATUS_ILLEGAL_INSTRUCTION)

Running rustc --version --verbose for my latest stable gives

rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-pc-windows-msvc
release: 1.46.0
LLVM version: 10.0

Additional Info

jonas-schievink commented 4 years ago

The issue does not seem to occur on Linux.

error: Stream Error: The stream is too short to perform the requested operation.

That's odd. Does this come from LLVM?

jonas-schievink commented 4 years ago

Oops, misread the Rust versions, doesn't look like a regression so far

mati865 commented 4 years ago

Does not reproduce on Windows 10 with windows-gnu. Can you run it in debugger? It's definitely crashing in LLVM so RUST_BACKTRACE cannot help.

Also you have some flags in your ~/.cargo/config (-C target-cpu=native caused similar issues in the past).

ralfbiedert commented 4 years ago

I did some more testing:

[target.aarch64-linux-android]
linker = "C:/Users/rb/.ndk/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android21-clang.cmd"
Setting in [profile.dev] Result
opt-level=1 STATUS_ILLEGAL_INSTRUCTION
opt-level=2 STATUS_ILLEGAL_INSTRUCTION
opt-level=3 STATUS_ILLEGAL_INSTRUCTION
debug=0 Compiles.
debug=1 Compiles.
debug=2 STATUS_ILLEGAL_INSTRUCTION
debug-assertions=false STATUS_ILLEGAL_INSTRUCTION
overflow-checks=false STATUS_ILLEGAL_INSTRUCTION
lto=false STATUS_ILLEGAL_INSTRUCTION
lto=true STATUS_ILLEGAL_INSTRUCTION
lto="thin" STATUS_ILLEGAL_INSTRUCTION
lto="off" STATUS_ILLEGAL_INSTRUCTION
incremental=false STATUS_ILLEGAL_INSTRUCTION
codegen-units=1 STATUS_ILLEGAL_INSTRUCTION
codegen-units=256 STATUS_ILLEGAL_INSTRUCTION
Setting in [profile.release] Result
debug=0 Compiles.
debug=1 Compiles.
debug=2 STATUS_ILLEGAL_INSTRUCTION

Can you run it in debugger?

I can try, but how would I best do that?

mati865 commented 4 years ago

Ok, I honestly have no idea how to debug it:

* thread #11, stop reason = Exception 0xc0000005 encountered at address 0x000002: User-mode data execution prevention (D
EP) violation at location 0x00000002
  * frame #0: 0x0000000000000002
    frame #1: 0x000000001474f820
Aaron1011 commented 3 years ago

I was able to reproduce this under Linux with a stage1 rustc, using the following hack:

  1. Install https://github.com/est31/msvc-wine-rust
  2. Build a local stage1 rustc
  3. Install the most recent nightly, and run rustup target install x86_64-pc-windows-msvc
  4. Run the following shell script:
set -x

rm -r weird
cp -r build/x86_64-unknown-linux-gnu/stage1 weird
cp -r  ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc weird/lib/rustlib
  1. Run rustup toolchain link weird /path/to/weird
  2. From rust-cheatsheet, run cargo +weird build --target x86_64-pc-windows-msvc

This allowed me to obtain the following backtrace (with LLVM debug assertions enabled):

#0  0x00007ffb5521a615 in raise () from /usr/lib/libc.so.6
#1  0x00007ffb55203862 in abort () from /usr/lib/libc.so.6
#2  0x00007ffb52ee49a1 in llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#3  0x00007ffb54920f56 in llvm::ArrayRef<unsigned char> llvm::codeview::SimpleTypeSerializer::serialize<llvm::codeview::ClassRecord>(llvm::codeview::ClassRecord&) ()
   from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#4  0x00007ffb5392f190 in llvm::CodeViewDebug::lowerTypeClass(llvm::DICompositeType const*) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#5  0x00007ffb53924cec in llvm::CodeViewDebug::getTypeIndex(llvm::DIType const*, llvm::DIType const*) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#6  0x00007ffb5392e78c in llvm::CodeViewDebug::lowerTypeFunction(llvm::DISubroutineType const*) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#7  0x00007ffb53924cec in llvm::CodeViewDebug::getTypeIndex(llvm::DIType const*, llvm::DIType const*) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#8  0x00007ffb53924149 in llvm::CodeViewDebug::getFuncIdForSubprogram(llvm::DISubprogram const*) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#9  0x00007ffb53927c57 in llvm::CodeViewDebug::emitDebugInfoForFunction(llvm::Function const*, llvm::CodeViewDebug::FunctionInfo&) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#10 0x00007ffb5392627f in llvm::CodeViewDebug::endModule() () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#11 0x00007ffb538b942d in llvm::AsmPrinter::doFinalization(llvm::Module&) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#12 0x00007ffb53123765 in llvm::FPPassManager::doFinalization(llvm::Module&) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#13 0x00007ffb5311d4a6 in llvm::legacy::PassManagerImpl::run(llvm::Module&) () from /home/aaron/repos/rust/weird/bin/../lib/../lib/libLLVM-11-rust-dev.so
#14 0x00007ffb56143635 in LLVMRustWriteOutputFile () from /home/aaron/repos/rust/weird/bin/../lib/librustc_driver-791e4e962741912d.so
#15 0x00007ffb55f7a3c8 in rustc_codegen_llvm::back::write::write_output_file () at compiler/rustc_codegen_llvm/src/back/write.rs:56
#16 0x00007ffb56099cf7 in rustc_codegen_llvm::back::write::codegen::{{closure}} () at compiler/rustc_codegen_llvm/src/back/write.rs:803
#17 rustc_codegen_llvm::back::write::codegen::with_codegen () at compiler/rustc_codegen_llvm/src/back/write.rs:691
#18 0x00007ffb55f8c88e in rustc_codegen_llvm::back::write::codegen () at compiler/rustc_codegen_llvm/src/back/write.rs:802
#19 <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::write::WriteBackendMethods>::codegen () at compiler/rustc_codegen_llvm/src/lib.rs:174
#20 0x00007ffb55f683bc in rustc_codegen_ssa::back::write::finish_intra_module_work () at /home/aaron/repos/rust/compiler/rustc_codegen_ssa/src/back/write.rs:887
#21 0x00007ffb55f624a7 in rustc_codegen_ssa::back::write::execute_optimize_work_item () at /home/aaron/repos/rust/compiler/rustc_codegen_ssa/src/back/write.rs:806
#22 rustc_codegen_ssa::back::write::execute_work_item () at /home/aaron/repos/rust/compiler/rustc_codegen_ssa/src/back/write.rs:722
#23 0x00007ffb560ca1de in rustc_codegen_ssa::back::write::spawn_work::{{closure}} () at /home/aaron/repos/rust/compiler/rustc_codegen_ssa/src/back/write.rs:1588
#24 std::sys_common::backtrace::__rust_begin_short_backtrace () at /home/aaron/repos/rust/library/std/src/sys_common/backtrace.rs:137
#25 0x00007ffb5609653d in std::thread::Builder::spawn_unchecked::{{closure}}::{{closure}} () at /home/aaron/repos/rust/library/std/src/thread/mod.rs:464
#26 <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once () at /home/aaron/repos/rust/library/std/src/panic.rs:308
#27 std::panicking::try::do_call () at /home/aaron/repos/rust/library/std/src/panicking.rs:381
#28 std::panicking::try () at /home/aaron/repos/rust/library/std/src/panicking.rs:345
#29 std::panic::catch_unwind () at /home/aaron/repos/rust/library/std/src/panic.rs:382
#30 std::thread::Builder::spawn_unchecked::{{closure}} () at /home/aaron/repos/rust/library/std/src/thread/mod.rs:463
#31 core::ops::function::FnOnce::call_once{{vtable-shim}} () at /home/aaron/repos/rust/library/core/src/ops/function.rs:227
#32 0x00007ffb55480e08 in <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once () at /home/aaron/repos/rust/library/alloc/src/boxed.rs:1042
#33 <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once () at /home/aaron/repos/rust/library/alloc/src/boxed.rs:1042
#34 0x00007ffb55489a9a in std::sys::unix::thread::Thread::new::thread_start () at library/std/src/sys/unix/thread.rs:87
#35 0x00007ffb515643e9 in start_thread () from /usr/lib/libpthread.so.0
#36 0x00007ffb552dd293 in clone () from /usr/lib/libc.so.6

The following LLVM code looks relevant: https://github.com/llvm/llvm-project/blob/f68d174c16217dd28858af2ce9ce5f33ac8af756/llvm/lib/DebugInfo/CodeView/SimpleTypeSerializer.cpp#L30

https://github.com/llvm/llvm-project/blob/c0da957bb05b7044bec83f8159c57b1d9ab9eb59/llvm/include/llvm/DebugInfo/CodeView/RecordSerialization.h#L29-L31

It looks like LLVM may be trying to serialize a very long Rust type, which ends up exceeding MaxRecordLength = 0xFF00

briansmith commented 3 years ago

https://github.com/rust-lang/rust/pull/77948 rebases rustc onto the final LLVM 11.0 release. According to that MR's milestone, it is scheduled for Rust 1.49. It isn't clear to me how to verify whether the Rust 1.49 release will actually contain that fix though.

@ralfbiedert and others who are having this issue, could you update Rust Beta to the latest version (presumably, one very close to what will be released as Rust 1.49 final) and report back whether the latest Rust Beta fixes this issue for you?

briansmith commented 3 years ago

FWIW, my team has encountered an issue very similar to this in 1.47, but not 1.46, and only when we're compiling for 64-bit Android targets. This would indicate to me that this isn't a Windows-specific bug.

briansmith commented 3 years ago

@ralfbiedert and others who are having this issue, could you update Rust Beta to the latest version (presumably, one very close to what will be released as Rust 1.49 final) and report back whether the latest Rust Beta fixes this issue for you?

The beta channel is now already 1.50.0-beta. Instead you can follow the directions at https://blog.rust-lang.org/inside-rust/2020/12/29/1.49.0-prerelease.html to install the 1.49.0 pre-release.

I followed those instructions and built my projects with the 1.49.0 pre-release targeting Android and my build succeeded where previously the same configuration failed with 1.48.0 with (signal: 11, SIGSEGV: invalid memory reference).

ralfbiedert commented 3 years ago

Ok, I just updated stable via RUSTUP_DIST_SERVER to

rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-pc-windows-msvc
release: 1.49.0

When I run cargo build in the project now I get STATUS_STACK_BUFFER_OVERRUN:

   [...]
   Compiling nom v4.2.3
   Compiling buf-min v0.1.1
   Compiling combine v4.2.1
   Compiling quote v1.0.7
   Compiling v_escape_derive v0.8.1
   Compiling serde_yaml v0.8.13
   Compiling cheatsheet-gen v0.1.0 (D:\Development\Source\_thirdparty\rust-cheatsheet)
error: Stream Error: The stream is too short to perform the requested operation.
error: could not compile `cheatsheet-gen`

Caused by:
  process didn't exit successfully: `rustc --crate-name cheatsheet_gen --edition=2018 src\main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=db2d8889f40e140f --out-dir D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps -C linker=C:/Users/rb/scoop/shims/lld-link.exe -C incremental=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\incremental -L dependency=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps --extern bitflags=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libbitflags-808ca99e95faa8ae.rlib --extern combine=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libcombine-2ff8681a4a840a07.rlib --extern either_n=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libeither_n-5cbdae01f5acc47f.rlib --extern lazy_static=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\liblazy_static-a5fdf74fcdafbee9.rlib --extern serde=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libserde-e4c3e65b940c2046.rlib --extern serde_yaml=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libserde_yaml-38d2deeae8d2037a.rlib --extern v_htmlescape=D:\Development\Source\_thirdparty\rust-cheatsheet\target\debug\deps\libv_htmlescape-3ce086faae9d8288.rlib` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

Update, rustc 1.51.0-nightly (158f8d034 2020-12-29) fails with the same error.

bugadani commented 3 years ago

I managed to hit this same issue with a different crate using rustc 1.52.0-nightly (1705a7d64 2021-03-18).

To reproduce, check out the data_holder branch of git@github.com:bugadani/embedded-gui.git (this commit) and try to build the calculator example.

Does not reproduce on WSL. Compiles fine on Windows with --release

error: Stream Error: The stream is too short to perform the requested operation.
error: could not compile `embedded-gui`

Caused by:
  process didn't exit successfully: `rustc --crate-name calculator --edition=2018 examples\calculator.rs 
--error-format=json 
--json=diagnostic-rendered-ansi 
--crate-type bin --emit=dep-info,link 
-C embed-bitcode=no -C debuginfo=2 
-C metadata=80352bbf14fdfebb 
--out-dir C:\_Hobby\RustLibraries\embedded-gui\target\debug\examples 
-C incremental=C:\_Hobby\RustLibraries\embedded-gui\target\debug\incremental 
-L dependency=C:\_Hobby\RustLibraries\embedded-gui\target\debug\deps 
--extern backend_embedded_graphics=C:\_Hobby\RustLibraries\embedded-gui\target\debug\deps\libbackend_embedded_graphics-8b6cd8529e0fc58d.rlib 
--extern embedded_graphics=C:\_Hobby\RustLibraries\embedded-gui\target\debug\deps\libembedded_graphics-d313357ca9d438a0.rlib 
--extern embedded_graphics_simulator=C:\_Hobby\RustLibraries\embedded-gui\target\debug\deps\libembedded_graphics_simulator-7dd88e0af16aed95.rlib 
--extern embedded_gui=C:\_Hobby\RustLibraries\embedded-gui\target\debug\deps\libembedded_gui-f62f849b7c07d6c3.rlib 
--extern heapless=C:\_Hobby\RustLibraries\embedded-gui\target\debug\deps\libheapless-50999711d995f99b.rlib 
--extern object_chain=C:\_Hobby\RustLibraries\embedded-gui\target\debug\deps\libobject_chain-96ea005c3c1e7f60.rlib`
 (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

It's worth noting that I'm abusing the type system pretty bad.