Emit error on array size overflow
When the byte size required for an array overflow we should emit an error.
Fixes #3962
I think your Fix is partly right there are two parts to fix this properly.
- Overflow check the value
- Add a MAX alloc limit of 2gb i think is what rustc does from reading it ages ago.
So 1,
I think you need to add a check like you have or you could put it into typechecking over in https://github.com/Rust-GCC/gccrs/blob/b7c9aaaf9f12abedf78e38ac12d110dbbec1d543/gcc/rust/typecheck/rust-hir-type-check-expr.cc#L1153
But maybe its enough to leave it wher you have it.
You should be able to do:
if (TREE_OVERFLOW_P (capacity_expr))
{
}
We track the capacity expr as part of the Array type now.
PArt 2: detect the HUGE array
tree len = capacity_expr
tree esize = TYPE_SIZE_UNIT (elt_type); // bytes, INTEGER_CST
tree bytes = fold_build2 (MULT_EXPR, sizetype,
fold_convert (sizetype, len),
fold_convert (sizetype, esize));
if (TREE_OVERFLOW_P (bytes))
Or also put in a new constant for 2gb maybe make it a new option in lang.opts so it can be changed but it defaults to 2gb as the final check.
I think should do this.
Also debug_tree is super useful here
I think this is a good sign your getting:
Executing on host: /home/runner/work/gccrs/gccrs/gccrs-build/gcc/testsuite/rust/../../gccrs -B/home/runner/work/gccrs/gccrs/gccrs-build/gcc/testsuite/rust/../../ /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/issue-3962.rs -m32 -fdiagnostics-plain-output -frust-incomplete-and-experimental-compiler-do-not-use -S -o issue-3962.s (timeout = 300)
spawn -ignore SIGHUP /home/runner/work/gccrs/gccrs/gccrs-build/gcc/testsuite/rust/../../gccrs -B/home/runner/work/gccrs/gccrs/gccrs-build/gcc/testsuite/rust/../../ /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/issue-3962.rs -m32 -fdiagnostics-plain-output -frust-incomplete-and-experimental-compiler-do-not-use -S -o issue-3962.s
/home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/issue-3962.rs:2:19: error: left shift count >= width of type
compiler exited with status 1
FAIL: rust/compile/issue-3962.rs at line 3 (test for errors, line 2)
FAIL: rust/compile/issue-3962.rs (test for excess errors)
Excess errors:
/home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/issue-3962.rs:2:19: error: left shift count >= width of type
not100% sure whats the best way to add tests for errors on m32 vs m64 here @dkm might know better
2. Add a MAX alloc limit of 2gb i think is what rustc does from reading it ages ago.
Or also put in a new constant for 2gb maybe make it a new option in lang.opts so it can be changed but it defaults to 2gb as the final check.
Are you sure about that ?
https://godbolt.org/z/Mr684qqsT
rustc does not throw an error message when the allocation is too big.
EDIT: It does since rustc 1.55 although I can't find anything about a 2Gb limit.