Skip to content

Commit 0ceae33

Browse files
Gracefully fail when compiling structs with too large array (#4461)
Fixes an internal compiler error (ICE) in Kani that occurs when compiling code containing structs with extremely large array sizes. Instead of triggering an ICE with `span_bug!`, the compiler now emits a proper error message. This aligns with how `rustc` handles similar size overflow errors. Resolves: #2236 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Co-authored-by: Zyad Hassan <[email protected]>
1 parent 55a00d2 commit 0ceae33

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

kani-compiler/src/codegen_cprover_gotoc/context/goto_ctx.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,20 @@ impl<'tcx> LayoutOfHelpers<'tcx> for GotocCtx<'tcx, '_> {
848848

849849
#[inline]
850850
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
851-
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
851+
// Handle SizeOverflow errors gracefully instead of causing an ICE
852+
if let LayoutError::SizeOverflow(_) = err {
853+
self.tcx
854+
.dcx()
855+
.struct_span_err(
856+
span,
857+
format!("values of the type `{}` are too big for the target architecture", ty),
858+
)
859+
.emit();
860+
self.tcx.dcx().abort_if_errors();
861+
unreachable!()
862+
} else {
863+
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
864+
}
852865
}
853866
}
854867

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0080]: values of the type `[u8; usize::MAX]` are too big for the target architecture\
2+
lib/rustlib/src/rust/library/core/src/mem/mod.rs\
3+
|\
4+
const SIZE: usize = intrinsics::size_of::<Self>();\
5+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `<S<u8> as std::mem::SizedTypeProperties>::SIZE` failed here\
6+
7+
note: the above error was encountered while instantiating `fn std::mem::size_of::<S<u8>>`\
8+
tests/ui/ice-size-overflow/large_array.rs\
9+
|\
10+
std::mem::size_of::<S<u8>>()\
11+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error; 1 warning emitted
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! This test checks that the compiler handles extremely large array sizes
5+
//! gracefully with a proper error message instead of causing an ICE.
6+
//! Previously, this would trigger an internal compiler error at
7+
//! kani-compiler/src/codegen_cprover_gotoc/context/goto_ctx.rs:371:9
8+
9+
struct S<T> {
10+
x: [T; !0],
11+
}
12+
13+
pub fn f() -> usize {
14+
std::mem::size_of::<S<u8>>()
15+
}
16+
17+
#[kani::proof]
18+
fn main() {
19+
let _x = f();
20+
}

0 commit comments

Comments
 (0)