]> git.ipfire.org Git - people/ms/gcc.git/commit - gcc/gimple-range-op.cc
tree-optimization/109170 - bogus use-after-free with __builtin_expect
authorRichard Biener <rguenther@suse.de>
Fri, 17 Mar 2023 12:14:49 +0000 (13:14 +0100)
committerRichard Biener <rguenther@suse.de>
Tue, 21 Mar 2023 08:21:29 +0000 (09:21 +0100)
commit5f413dc41ee4f8bc3a0fc295f98b75dceae52fa8
treeabe9fce7572f5c71e9c9a82faa0bb469054d9574
parent259bd768640328cc98647c5cf8b0d6dcfba6d4bf
tree-optimization/109170 - bogus use-after-free with __builtin_expect

The following adds a missing range-op for __builtin_expect which
helps -Wuse-after-free to detect the case a realloc original
pointer is used when the result was NULL.  The implementation
should handle all argument one pass-through builtins we handle
in the fnspec machinery, but that's defered to GCC 14.

The gcc.dg/tree-ssa/ssa-lim-21.c testcase needs adjustment because

   for (int j = 0; j < m; j++)
     if (__builtin_expect (m, 0))
       for (int i = 0; i < m; i++)

is now correctly optimized to a unconditional jump by EVRP - m
cannot be zero when the outer loop is entered.  I've adjusted
the outer loop to iterate 'n' times which makes us apply store-motion
to 'count' and 'q->data1' but only out of the inner loop and
as expected not apply store motion to 'q->data' at all.

The gcc.dg/predict-20.c testcase relies on broken behavior of
profile estimation when trying to handle __builtin_expect values
flowing into PHI nodes.  I have opened PR109210 and removed
the expected matching from the testcase.

PR tree-optimization/109170
* gimple-range-op.cc (cfn_pass_through_arg1): New.
(gimple_range_op_handler::maybe_builtin_call): Handle
__builtin_expect via cfn_pass_through_arg1.

* gcc.dg/Wuse-after-free-pr109170.c: New testcase.
* gcc.dg/tree-ssa/ssa-lim-21.c: Adjust.
* gcc.dg/predict-20.c: Likewise.
gcc/gimple-range-op.cc
gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/predict-20.c
gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-21.c