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.
Showing
- gcc/gimple-range-op.cc 27 additions, 0 deletionsgcc/gimple-range-op.cc
- gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c 15 additions, 0 deletionsgcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c
- gcc/testsuite/gcc.dg/predict-20.c 2 additions, 1 deletiongcc/testsuite/gcc.dg/predict-20.c
- gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-21.c 4 additions, 3 deletionsgcc/testsuite/gcc.dg/tree-ssa/ssa-lim-21.c
Loading
Please register or sign in to comment