On Wed, Mar 08, 2023 at 09:38:43AM +0000, Richard Biener via Gcc-patches wrote:
> The following plugs one place in extract_muldiv where it should avoid
> folding when sanitizing overflow.
>
> I'm unsure about the testcase, I didn't find any that tests for
> a runtime sanitizer error ...
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu.
>
> OK?
>
> PR middle-end/108995
> * fold-const.cc (extract_muldiv_1): Avoid folding
> (CST * b) / CST2 when sanitizing overflow and we rely on
> overflow being undefined.
This is ok.
>
> * gcc.dg/ubsan/pr108995.c: New testcase.
As for testcase, there are many testcases that test for runtime sanitizer
errors. For ubsan, it is more common to test -fsanitize-recover= and
just dg-output scan the output for expected diagnostics (many examples
in that directory).
Another possibility is to test for the no recovery, see e.g.
gcc.dg/ubsan/bounds-3.c. In that case there should be
/* { dg-do run } */
and
/* { dg-shouldfail "ubsan" } */
but dg-output checking for the exact wording is still highly desirable.
The test also relies on 32-bit ints, so it should be dg-do run { target int32 }
I think.
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/ubsan/pr108995.c
> @@ -0,0 +1,15 @@
> +/* { dg-do run { xfail *-*-* } } */
> +/* With optimization we constant fold and diagnose the overflow and do
> + not sanitize anything. */
> +/* { dg-skip-if "" { *-*-* } { "*" } { ! "-O0" } } */
> +/* { dg-options "-fsanitize=undefined -fno-sanitize-recover=undefined" } */
> +
> +int a;
> +const int b = 44514;
> +int *c = &a;
> +
> +int main ()
> +{
> + *c = 65526 * b / 6;
> + return 0;
> +}
> --
> 2.35.3
Jakub