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

sparc64 has incorrect ABI for struct containing f64 and f32 #122620

Open
erikdesjardins opened this issue Mar 17, 2024 · 2 comments
Open

sparc64 has incorrect ABI for struct containing f64 and f32 #122620

erikdesjardins opened this issue Mar 17, 2024 · 2 comments
Labels
A-ABI Area: Concerning the application binary interface (ABI) C-bug Category: This is a bug. I-miscompile Issue: Correct Rust code lowers to incorrect machine code I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness O-SPARC Target: SPARC processors P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@erikdesjardins
Copy link
Contributor

erikdesjardins commented Mar 17, 2024

I tried this code:

// with --target=sparc64-unknown-linux-gnu

#[repr(C)]
pub struct DoubleFloat {
    f: f64,
    g: f32,
}

#[no_mangle]
pub extern "C" fn foo(x: DoubleFloat) {}

I expected to see this happen: Compiles successfully

Instead, this happened:

Before #122619, this ICEs the compiler (https://godbolt.org/z/bKn6d5jeM):

thread 'rustc' panicked at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/compiler/rustc_abi/src/lib.rs:565:13:
Size::sub: 0 - 8 would result in negative size

(see backtrace below).

After #122619, this will compile, but it will use a bizarre ABI that's probably wrong:

EDIT: I decided not to fix this ICE in #122619, since it's better to error than generate wrong code. But when the ICE is fixed, the current ABI code generates something nonsensical:

declare void @foo({ double, float, i32, i64 } inreg)

Meta

rustc --version --verbose:

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

(also reproducible on very recent master 766bdce)

Backtrace

thread 'rustc' panicked at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/compiler/rustc_abi/src/lib.rs:565:13:
Size::sub: 0 - 8 would result in negative size
stack backtrace:
   0:     0x7f8961a1a6f6 - std::backtrace_rs::backtrace::libunwind::trace::hbee8a7973eeb6c93
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f8961a1a6f6 - std::backtrace_rs::backtrace::trace_unsynchronized::hc8ac75eea3aa6899
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f8961a1a6f6 - std::sys_common::backtrace::_print_fmt::hc7f3e3b5298b1083
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f8961a1a6f6 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hbb235daedd7c6190
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f8961a6cf40 - core::fmt::rt::Argument::fmt::h76c38a80d925a410
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/rt.rs:142:9
   5:     0x7f8961a6cf40 - core::fmt::write::h3ed6aeaa977c8e45
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f8961a0e53f - std::io::Write::write_fmt::h78b18af5775fedb5
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/io/mod.rs:1810:15
   7:     0x7f8961a1a4d4 - std::sys_common::backtrace::_print::h5d645a07e0fcfdbb
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f8961a1a4d4 - std::sys_common::backtrace::print::h85035a511aafe7a8
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f8961a1d267 - std::panicking::default_hook::{{closure}}::hcce8cea212785a25
  10:     0x7f8961a1cfc9 - std::panicking::default_hook::hf5fcb0f213fe709a
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:292:9
  11:     0x7f895ea1461c - std[79729d9c385e1623]::panicking::update_hook::<alloc[6df67106ebca92c0]::boxed::Box<rustc_driver_impl[66ed8fdbde15dc6c]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f8961a1d9b6 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hbc5ccf4eb663e1e5
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/boxed.rs:2029:9
  13:     0x7f8961a1d9b6 - std::panicking::rust_panic_with_hook::h095fccf1dc9379ee
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:783:13
  14:     0x7f8961a1d702 - std::panicking::begin_panic_handler::{{closure}}::h032ba12139b353db
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:657:13
  15:     0x7f8961a1abf6 - std::sys_common::backtrace::__rust_end_short_backtrace::h9259bc2ff8fd0f76
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f8961a1d460 - rust_begin_unwind
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
  17:     0x7f8961a69645 - core::panicking::panic_fmt::h784f20a50eaab275
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
  18:     0x7f895f544152 - rustc_target[edc8d30e58965941]::abi::call::sparc64::arg_scalar::<rustc_middle[e0f609162babcb92]::ty::layout::LayoutCx<rustc_middle[e0f609162babcb92]::ty::context::TyCtxt>>
  19:     0x7f895f54460d - rustc_target[edc8d30e58965941]::abi::call::sparc64::arg_scalar_pair::<rustc_middle[e0f609162babcb92]::ty::layout::LayoutCx<rustc_middle[e0f609162babcb92]::ty::context::TyCtxt>>
  20:     0x7f895f54486b - rustc_target[edc8d30e58965941]::abi::call::sparc64::parse_structure::<rustc_middle[e0f609162babcb92]::ty::Ty, rustc_middle[e0f609162babcb92]::ty::layout::LayoutCx<rustc_middle[e0f609162babcb92]::ty::context::TyCtxt>>
  21:     0x7f895f5442a2 - rustc_target[edc8d30e58965941]::abi::call::sparc64::classify_arg::<rustc_middle[e0f609162babcb92]::ty::Ty, rustc_middle[e0f609162babcb92]::ty::layout::LayoutCx<rustc_middle[e0f609162babcb92]::ty::context::TyCtxt>>
  22:     0x7f895f544a33 - rustc_target[edc8d30e58965941]::abi::call::sparc64::compute_abi_info::<rustc_middle[e0f609162babcb92]::ty::Ty, rustc_middle[e0f609162babcb92]::ty::layout::LayoutCx<rustc_middle[e0f609162babcb92]::ty::context::TyCtxt>>
  23:     0x7f89607772dc - rustc_ty_utils[112fed09b8de77ee]::abi::fn_abi_new_uncached
  24:     0x7f896018f406 - rustc_ty_utils[112fed09b8de77ee]::abi::fn_abi_of_instance
  25:     0x7f896018ddb3 - rustc_query_impl[aa9389cf74aaab3]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[aa9389cf74aaab3]::query_impl::fn_abi_of_instance::dynamic_query::{closure#2}::{closure#0}, rustc_middle[e0f609162babcb92]::query::erase::Erased<[u8; 16usize]>>
  26:     0x7f8960102553 - rustc_query_system[84cc5057ded7d916]::query::plumbing::try_execute_query::<rustc_query_impl[aa9389cf74aaab3]::DynamicConfig<rustc_query_system[84cc5057ded7d916]::query::caches::DefaultCache<rustc_middle[e0f609162babcb92]::ty::ParamEnvAnd<(rustc_middle[e0f609162babcb92]::ty::instance::Instance, &rustc_middle[e0f609162babcb92]::ty::list::List<rustc_middle[e0f609162babcb92]::ty::Ty>)>, rustc_middle[e0f609162babcb92]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[aa9389cf74aaab3]::plumbing::QueryCtxt, false>
  27:     0x7f89601021ef - rustc_query_impl[aa9389cf74aaab3]::query_impl::fn_abi_of_instance::get_query_non_incr::__rust_end_short_backtrace
  28:     0x7f895d402af9 - <rustc_codegen_llvm[3c1678ba3e8e7d64]::context::CodegenCx as rustc_codegen_ssa[8f0ba34f7d763469]::traits::declare::PreDefineMethods>::predefine_fn
  29:     0x7f89607214a8 - rustc_codegen_llvm[3c1678ba3e8e7d64]::base::compile_codegen_unit::module_codegen
  30:     0x7f8960942a86 - <rustc_codegen_llvm[3c1678ba3e8e7d64]::LlvmCodegenBackend as rustc_codegen_ssa[8f0ba34f7d763469]::traits::backend::ExtraBackendMethods>::compile_codegen_unit
  31:     0x7f8960595746 - <rustc_codegen_llvm[3c1678ba3e8e7d64]::LlvmCodegenBackend as rustc_codegen_ssa[8f0ba34f7d763469]::traits::backend::CodegenBackend>::codegen_crate
  32:     0x7f896059331f - rustc_interface[bf5cafa581ab7832]::passes::start_codegen
  33:     0x7f8960592b22 - <rustc_interface[bf5cafa581ab7832]::queries::Queries>::codegen_and_build_linker
  34:     0x7f89608fe7a0 - rustc_interface[bf5cafa581ab7832]::interface::run_compiler::<core[f975038e3cc9791c]::result::Result<(), rustc_span[7a149c27976a99e7]::ErrorGuaranteed>, rustc_driver_impl[66ed8fdbde15dc6c]::run_compiler::{closure#0}>::{closure#0}
  35:     0x7f8960a2c7db - std[79729d9c385e1623]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[bf5cafa581ab7832]::util::run_in_thread_with_globals<rustc_interface[bf5cafa581ab7832]::interface::run_compiler<core[f975038e3cc9791c]::result::Result<(), rustc_span[7a149c27976a99e7]::ErrorGuaranteed>, rustc_driver_impl[66ed8fdbde15dc6c]::run_compiler::{closure#0}>::{closure#0}, core[f975038e3cc9791c]::result::Result<(), rustc_span[7a149c27976a99e7]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f975038e3cc9791c]::result::Result<(), rustc_span[7a149c27976a99e7]::ErrorGuaranteed>>
  36:     0x7f8960a2c639 - <<std[79729d9c385e1623]::thread::Builder>::spawn_unchecked_<rustc_interface[bf5cafa581ab7832]::util::run_in_thread_with_globals<rustc_interface[bf5cafa581ab7832]::interface::run_compiler<core[f975038e3cc9791c]::result::Result<(), rustc_span[7a149c27976a99e7]::ErrorGuaranteed>, rustc_driver_impl[66ed8fdbde15dc6c]::run_compiler::{closure#0}>::{closure#0}, core[f975038e3cc9791c]::result::Result<(), rustc_span[7a149c27976a99e7]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f975038e3cc9791c]::result::Result<(), rustc_span[7a149c27976a99e7]::ErrorGuaranteed>>::{closure#1} as core[f975038e3cc9791c]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  37:     0x7f8961a278e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h12de4fc57affb195
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/boxed.rs:2015:9
  38:     0x7f8961a278e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3c619f45059d5cf1
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/boxed.rs:2015:9
  39:     0x7f8961a278e5 - std::sys::unix::thread::Thread::new::thread_start::hbac657605e4b7389
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys/unix/thread.rs:108:17
  40:     0x7f895ba94ac3 - <unknown>
  41:     0x7f895bb26850 - <unknown>
  42:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.76.0 (07dca489a 2024-02-04) running on x86_64-unknown-linux-gnu

note: compiler flags: -C debuginfo=1 -C llvm-args=--x86-asm-syntax=intel --crate-type rlib -C opt-level=3 -C debuginfo=0 -C no-prepopulate-passes

query stack during panic:
#0 [fn_abi_of_instance] computing call ABI of `foo`
end of query stack

@erikdesjardins erikdesjardins added the C-bug Category: This is a bug. label Mar 17, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 17, 2024
erikdesjardins referenced this issue Mar 17, 2024
Tests added in cast-target-abi.rs, covering the single element, array,
and prefix cases in `CastTarget::llvm_type`, and the Rust-is-larger/smaller
cases in the Rust<->ABI copying code.

ffi-out-of-bounds-loads.rs was overhauled to be runnable on any
platform. Its alignment also increases due to the removal of a `min` in
the previous commit; this was probably an insufficient workaround for
this issue or similar. The higher alignment is fine, since the alloca is
actually aligned to 8 bytes, as the test checks now confirm.
@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness O-SPARC Target: SPARC processors A-ABI Area: Concerning the application binary interface (ABI) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 17, 2024
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 17, 2024
@erikdesjardins
Copy link
Contributor Author

erikdesjardins commented Mar 17, 2024

Very similar to #115399, although the backtraces are a bit different--this panic occurs in sparc64::arg_scalar, that one in sparc64::classify_arg

(sparc already has a few open soundness issues, I guess this is just another one to add to the pile: https://github.com/rust-lang/rust/issues?q=is%3Aopen+label%3AO-SPARC+label%3AI-unsound)

@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Mar 18, 2024
@workingjubilee workingjubilee added I-miscompile Issue: Correct Rust code lowers to incorrect machine code I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness and removed I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ABI Area: Concerning the application binary interface (ABI) C-bug Category: This is a bug. I-miscompile Issue: Correct Rust code lowers to incorrect machine code I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness O-SPARC Target: SPARC processors P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants