[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-09 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #36 from GCC Commits  ---
The master branch has been updated by Jan Hubicka :

https://gcc.gnu.org/g:27b4500ec3f97e0e8da9949b352a32147af8edf5

commit r16-7968-g27b4500ec3f97e0e8da9949b352a32147af8edf5
Author: Jan Hubicka 
Date:   Tue Mar 10 07:56:23 2026 +0100

Fix merging of flags in ipa_merge_modref_summary_after_inlining

When merging the modref summary after inlining, we merge all of the flags
of the outer functions that was inlined into. But things go wrong as
now the flags includes both ECF_NORETURN and ECF_NOTHROW. This happens
because the function which was being inlined had ECF_NOTHROW while caller
had ECF_NORETURN. When both of these are set, ignore_stores_p and
ignore_nondeterminism_p return true. But in this case the inner function is
still just nothrow and not noreturn.

Originally the code was written to only merge ECF_PURE and ECF_CONST where
this logic is correct. This fixes merigng of ignore_stores and
ECF_LOOPING_CONST_OR_PURE flags.

Bootstrapped and tested on x86_64-linux-gnu.

PR tree-optimization/120987

gcc/ChangeLog:

* ipa-modref.cc (ipa_merge_modref_summary_after_inlining): Mask
off non const/pure/novops related flags when combining of outer
functions.

gcc/testsuite/ChangeLog:

* g++.dg/torture/pr120987-1.C: New test.

Co-authored-by: Andrew Pinski 

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-04 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

Andrew Pinski  changed:

   What|Removed |Added

 Status|ASSIGNED|NEW

--- Comment #35 from Andrew Pinski  ---
Based on the review, back to honza.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #34 from Andrew Pinski  ---
(In reply to Andrew Pinski from comment #33)
> Created attachment 63819 [details]
> Patch which I am testing
> 
> Note this without the additional comment which I will add before submitting
> it.

And no this does not fix pr103953.C.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #33 from Andrew Pinski  ---
Created attachment 63819
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=63819&action=edit
Patch which I am testing

Note this without the additional comment which I will add before submitting it.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #32 from Andrew Pinski  ---
(In reply to Sam James from comment #31)
> (In reply to Andrew Pinski from comment #30)
> >for (n = edge->caller; n->inlined_to; n = n->callers->caller)
> > -flags |= flags_from_decl_or_type (n->decl);
> > -  flags |= flags_from_decl_or_type (n->decl);
> > +flags |= flags_from_decl_or_type (n->decl) & flags_mask;
> > +  flags |= flags_from_decl_or_type (n->decl) & flags_mask;
> 
> It still looks odd because n is left as n->callers->caller, so you get it
> applied twice in that case. Can you add a comment?

Yes I will add one.
But basically that is the outer most function where this was inlined into (in
this case).

so we going from inner most (edge->caller), to the outer most (which has a null
inlined_to). The second outer most has a non-null inlined_to.
Going one further you get a null :).

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread sjames at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #31 from Sam James  ---
(In reply to Andrew Pinski from comment #30)
>for (n = edge->caller; n->inlined_to; n = n->callers->caller)
> -flags |= flags_from_decl_or_type (n->decl);
> -  flags |= flags_from_decl_or_type (n->decl);
> +flags |= flags_from_decl_or_type (n->decl) & flags_mask;
> +  flags |= flags_from_decl_or_type (n->decl) & flags_mask;

It still looks odd because n is left as n->callers->caller, so you get it
applied twice in that case. Can you add a comment?

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

Andrew Pinski  changed:

   What|Removed |Added

 Status|NEW |ASSIGNED

--- Comment #30 from Andrew Pinski  ---
Ok, I think I have a patch which seems correct and such.
```
[apinski@xeond2 gcc]$ git diff
diff --git a/gcc/ipa-modref.cc b/gcc/ipa-modref.cc
index fc00acecfce..d10a2752e05 100644
--- a/gcc/ipa-modref.cc
+++ b/gcc/ipa-modref.cc
@@ -5345,9 +5345,14 @@ ipa_merge_modref_summary_after_inlining (cgraph_edge
*edge)
   int flags = flags_from_decl_or_type (edge->callee->decl);
   /* Combine in outer flags.  */
   cgraph_node *n;
+  /* Only combine const/pure/novops related flags.
+ NoReturn and NoThrow cannot be combined.
+ An outer noreturn function might throw but an inner might be marked as
nothrow.  */
+  int flags_mask;
+  flags_mask = ECF_CONST | ECF_PURE | ECF_NOVOPS | ECF_LOOPING_CONST_OR_PURE;
   for (n = edge->caller; n->inlined_to; n = n->callers->caller)
-flags |= flags_from_decl_or_type (n->decl);
-  flags |= flags_from_decl_or_type (n->decl);
+flags |= flags_from_decl_or_type (n->decl) & flags_mask;
+  flags |= flags_from_decl_or_type (n->decl) & flags_mask;
   bool ignore_stores = ignore_stores_p (edge->caller->decl, flags);

   if (!callee_info && to_info)
```

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #29 from Andrew Pinski  ---
(In reply to Andrew Pinski from comment #28) 
> Or maybe the fix is ignore_stores_p below the loop only should look
> the original decl's flags ...

That is this patch:
```
[apinski@xeond2 gcc]$ git diff
diff --git a/gcc/ipa-modref.cc b/gcc/ipa-modref.cc
index fc00acecfce..12f7de05877 100644
--- a/gcc/ipa-modref.cc
+++ b/gcc/ipa-modref.cc
@@ -5343,12 +5343,12 @@ ipa_merge_modref_summary_after_inlining (cgraph_edge
*edge)
   class modref_summary_lto *callee_info_lto
 = summaries_lto ? summaries_lto->get (edge->callee) : NULL;
   int flags = flags_from_decl_or_type (edge->callee->decl);
+  bool ignore_stores = ignore_stores_p (edge->caller->decl, flags);
   /* Combine in outer flags.  */
   cgraph_node *n;
   for (n = edge->caller; n->inlined_to; n = n->callers->caller)
 flags |= flags_from_decl_or_type (n->decl);
   flags |= flags_from_decl_or_type (n->decl);
-  bool ignore_stores = ignore_stores_p (edge->caller->decl, flags);

   if (!callee_info && to_info)
 {

```

The problem is ignore_nondeterminism_p does the same check as ignore_stores_p
and ignore_nondeterminism_p is called below with the combined flags.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #28 from Andrew Pinski  ---
(In reply to Andrew Pinski from comment #27)
> This means this loop:
> ```
>   /* Combine in outer flags.  */
>   cgraph_node *n;
>   for (n = edge->caller; n->inlined_to; n = n->callers->caller)
> flags |= flags_from_decl_or_type (n->decl);
>   flags |= flags_from_decl_or_type (n->decl);
> ```
> is wrong.
> I am not even sure this loop even makes sense.

I removed this loop and the combine of outer flags and I didn't get any
regressions and it fixes the wrong code here because of what I wrote in comment
#27 as it was combining NOTHROW and NORETURN  and getting the wrong info. Or
maybe the fix is ignore_stores_p below the loop only should look the original
decl's flags ...

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2026-03-03 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #27 from Andrew Pinski  ---
```
/* Return true if ECF flags says that stores can be ignored.  */

static bool
ignore_stores_p (tree caller, int flags)
{
  if (flags & (ECF_PURE | ECF_CONST | ECF_NOVOPS))
return true;
  if ((flags & (ECF_NORETURN | ECF_NOTHROW)) == (ECF_NORETURN | ECF_NOTHROW)
  || (!opt_for_fn (caller, flag_exceptions) && (flags & ECF_NORETURN)))
return true;
  return false;
}
```

I am trying to understand this. This says if noreturn+nothrow function can
safely ignore the stores which seems reasonible.
And no noreturn with -fno-exceptions can also safely ignore the stores.
But if we inline a nothrow function into a noreturn, then we get
noreturn/nothrow. Which then messes up ignore_stores_p.
Which in this case we have:
gdb_exception::gdb_exception is a nothrow function which is inlined into
throw_exception which is a noreturn function.

This means this loop:
```
  /* Combine in outer flags.  */
  cgraph_node *n;
  for (n = edge->caller; n->inlined_to; n = n->callers->caller)
flags |= flags_from_decl_or_type (n->decl);
  flags |= flags_from_decl_or_type (n->decl);
```
is wrong.
I am not even sure this loop even makes sense.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2025-07-14 Thread vries at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #26 from Tom de Vries  ---
Eventually, a-test-2.c.121t.retslot has:
...
   [count: 0]:
:
  _3 = MEM[(struct __shared_count *)&file_exception + 8B]._M_pi;
  if (_3 != 0B)
...
and then the following a-test-2.c.122t.fre3 introduces the wrong code:
...
   [count: 0]:
:
  if (_15 != 0B)
...
where _15 is the initial nonnull value of
file_exception.message._M_refcount._M_pi, and we need the actual value as
updated by throw_exception, which is null.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2025-07-13 Thread rguenther at suse dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #25 from rguenther at suse dot de  ---
On Sat, 12 Jul 2025, vries at gcc dot gnu.org wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987
> 
> --- Comment #22 from Tom de Vries  ---
> FYI, I've submitted a workaround for gdb that disables -fipa-modref for 
> release
> gcc 12.0 - 16.0 (
> https://sourceware.org/pipermail/gdb-patches/2025-July/219213.html ), assuming
> that his will be fixed in 16.1.

It's usually better to place a noipa attribute on a select function
where the issue shows up than disabling a whole pass which might
lead to other latent bugs pop up here and there given test coverage
with -fno-ipa-* or -fno-tree-* is very weak.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2025-07-13 Thread vries at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #24 from Tom de Vries  ---
Created attachment 61858
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61858&action=edit
debug patch

Using this patch (on top of current trunk), I get the following in
a-test-2.c.092i.inline:
...
Considering gdb_exception::gdb_exception(gdb_exception&&)/482 with 15 size
 to be inlined into void throw_exception(gdb_exception&&)/487 in test-2.c:28
 Estimated badness is -0.002532, frequency 1.00.
 Called 0 (precise) times
flags from gdb_exception::gdb_exception(gdb_exception&&)/482:  nothrow
flags from void throw_exception(gdb_exception&&)/487:  noreturn
merged flags for void throw_exception(gdb_exception&&)/487:  noreturn nothrow
IGNORE_STORES: 1
  Parm map:  -1 0
Updated mod-ref summary for void throw_exception(gdb_exception&&)/487
  loads:
  Base 0: alias set 0
Ref 0: alias set 0
  access: Parm 0 param offset:0 offset:0 size:64 max_size:128
  stores:
  Side effects
  Try dse
  parm 0 flags: no_direct_clobber no_direct_escape no_indirect_escape
...

Without the "Minor ipa-modref tweaks" change we have instead:
...
Considering gdb_exception::gdb_exception(gdb_exception&&)/482 with 15 size
 to be inlined into void throw_exception(gdb_exception&&)/487 in test-2.c:28
 Estimated badness is -0.002532, frequency 1.00.
 Called 0 (precise) times
flags from gdb_exception::gdb_exception(gdb_exception&&)/482:  nothrow
merged flags for void throw_exception(gdb_exception&&)/487:  nothrow
IGNORE_STORES: 0
  Parm map:  -1 0
Updated mod-ref summary for void throw_exception(gdb_exception&&)/487
  loads:
  Base 0: alias set 0
Ref 0: alias set 0
  access: Parm 0 param offset:0 offset:0 size:64 max_size:128
  stores:
Every base
  Side effects
  Nondeterministic
  Try dse
  parm 0 flags: no_direct_escape
... 

I think that the change that the commit brought is not unreasonable:
throw_exception is noreturn before inlining, and should be so after inlining.

It seems wrong to me that we propagate the nothrow flag from the gdb_exception
constructor to throw_exception.  That already happened before the commit.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2025-07-13 Thread sjames at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #23 from Sam James  ---
(In reply to Tom de Vries from comment #20)
> Seems to be fixed by:

I've regtested this and it was fine.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2025-07-12 Thread vries at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #22 from Tom de Vries  ---
FYI, I've submitted a workaround for gdb that disables -fipa-modref for release
gcc 12.0 - 16.0 (
https://sourceware.org/pipermail/gdb-patches/2025-July/219213.html ), assuming
that his will be fixed in 16.1.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2025-07-11 Thread vries at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

--- Comment #21 from Tom de Vries  ---
(In reply to Tom de Vries from comment #20)
> Seems to be fixed by:
> ...
> diff --git a/gcc/ipa-modref.c b/gcc/ipa-modref.c
> index 9e537b04196..fbdf8da36df 100644
> --- a/gcc/ipa-modref.c
> +++ b/gcc/ipa-modref.c
> @@ -5109,7 +5109,6 @@ ipa_merge_modref_summary_after_inlining (cgraph_edge
> *edge)
>cgraph_node *n;
>for (n = edge->caller; n->inlined_to; n = n->callers->caller)
>  flags |= flags_from_decl_or_type (n->decl);
> -  flags |= flags_from_decl_or_type (n->decl);
>bool ignore_stores = ignore_stores_p (edge->caller->decl, flags);
>  
>if (!callee_info && to_info)
> ...

Which causes this difference in test-2.c.085i.modref:
...
-  parm 0 flags: no_direct_clobber no_direct_escape no_indirect_escape
+  parm 0 flags: no_direct_escape
...
in the analysis of throw_exception.

I don't understand these flags very well, but my guess is that it's the
no_direct_clobber that is causing the problems.

[Bug ipa/120987] [13/14/15/16 regression] gdb build with lto triggers use after free since r12-5541-g2cadaa1f134bec

2025-07-11 Thread sjames at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120987

Sam James  changed:

   What|Removed |Added

Summary|[14/15/16 regression] gdb   |[13/14/15/16 regression]
   |build with lto triggers use |gdb build with lto triggers
   |after free since|use after free since
   |r12-5541-g2cadaa1f134bec|r12-5541-g2cadaa1f134bec
   Target Milestone|14.4|13.5