public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug ipa/96503] New: attribute alloc_size effect lost after inlining
@ 2020-08-06 15:51 msebor at gcc dot gnu.org
  2022-09-30  1:54 ` [Bug ipa/96503] " kees at outflux dot net
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-08-06 15:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

            Bug ID: 96503
           Summary: attribute alloc_size effect lost after inlining
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ipa
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
                CC: marxin at gcc dot gnu.org
  Target Milestone: ---

Similar to pr96502, the test case below shows that the effect of attribute
alloc_size on warnings is also lost after inlining.  I open this as a separate
bug (and expect to raise others) because I expect the "fixes" to be different
in each case.   Like pr96502, this also came up in the following discussion:
https://gcc.gnu.org/pipermail/gcc-patches/2020-August/551526.html

$ cat x.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout x.c
int* f (int);

__attribute__ ((alloc_size (1), noinline)) int*
 f0 (int n) { return f (n); }

void h0 (void)
{
  int *p = f0 (3);
  __builtin_memset (p, 0, 3 * sizeof p);   // warning (good)
}

__attribute__ ((alloc_size (1))) int*
f1 (int n) { return f (n); }

void h1 (void)
{ 
  int *p = f1 (3);
  __builtin_memset (p, 0, 3 * sizeof p);   // missing warning
}

x.c: In function ‘h0’:
x.c:9:3: warning: ‘__builtin_memset’ forming offset [3, 23] is out of the
bounds [0, 3] [-Warray-bounds]
    9 |   __builtin_memset (p, 0, 3 * sizeof p);   // warning (good)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
@ 2022-09-30  1:54 ` kees at outflux dot net
  2022-11-18 23:57 ` pageexec at gmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: kees at outflux dot net @ 2022-09-30  1:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

Kees Cook <kees at outflux dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kees at outflux dot net

--- Comment #1 from Kees Cook <kees at outflux dot net> ---
Created attachment 53643
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53643&action=edit
PoC showing unexpected __bdos results across inlines

Fixing this is needed for the Linux kernel to do much useful with alloc_size.
Most of the allocators are inline wrappers, for example.

This can be additionally shown to break __builtin_dynamic_object_size(), which
means all FORTIFY_SOURCE of alloc_size-marked inlines is broken. :(
https://godbolt.org/z/jTKjY3s1j

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
  2022-09-30  1:54 ` [Bug ipa/96503] " kees at outflux dot net
@ 2022-11-18 23:57 ` pageexec at gmail dot com
  2023-10-25  5:46 ` muecker at gwdg dot de
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pageexec at gmail dot com @ 2022-11-18 23:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

PaX Team <pageexec at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pageexec at gmail dot com

--- Comment #2 from Siddhesh Poyarekar <siddhesh at gcc dot gnu.org> ---
(In reply to Kees Cook from comment #1)
> Created attachment 53643 [details]
> PoC showing unexpected __bdos results across inlines
> 
> Fixing this is needed for the Linux kernel to do much useful with
> alloc_size. Most of the allocators are inline wrappers, for example.

For cases where the size doesn't really change across the inlines, it ought to
be sufficient to annotate the non-inlined implementation function, e.g. in case
of kvmalloc, annotate kvmalloc_node as __alloc_size(1).

For other cases it may be less trivial, e.g.:

/* Some padding the wrapper adds to the actual allocation.  */
size_t metadata_size;

__attribute__ ((alloc_size (1))) void *alloc_wrapper (size_t sz)
{
  return real_alloc (size + metadata_size);
}

extern void *real_alloc (size_t) __attribute__ ((alloc_size(1)));

here the compiler will end up seeing the padded size, which may not be correct.

To fix this we'll have to store the alloc_size info somewhere (ptr_info seems
to be aliasing-specific, so maybe a new member to tree_ssa_name) during
inlining and then teach the tree-object-size pass to access it.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
  2022-09-30  1:54 ` [Bug ipa/96503] " kees at outflux dot net
  2022-11-18 23:57 ` pageexec at gmail dot com
@ 2023-10-25  5:46 ` muecker at gwdg dot de
  2023-10-25  5:54 ` sjames at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: muecker at gwdg dot de @ 2023-10-25  5:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

Martin Uecker <muecker at gwdg dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |muecker at gwdg dot de

--- Comment #3 from Martin Uecker <muecker at gwdg dot de> ---
Was just to file this bug...  

Note that the access attribute could be translated into the same builtin
suggested for counted_by in

https://gcc.gnu.org/pipermail/gcc-patches/2023-October/634177.html

and then this could work.  This would also simply the BDOS path I think because
special code for the access attribute could go.

Example: https://godbolt.org/z/1TTePn7hn 

static
#if 0
char *propagate(char *p, int s)
    [[gnu::access(read_only, 1, 2)]]
#else
char *propagate(char *p; int s; 
    char p[(p = pointer_with_size(p, s), s)], int s)
#endif
{
    printf("%ld\n", __builtin_dynamic_object_size(p, 0));
    return p;
}

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-10-25  5:46 ` muecker at gwdg dot de
@ 2023-10-25  5:54 ` sjames at gcc dot gnu.org
  2023-10-25 10:55 ` siddhesh at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: sjames at gcc dot gnu.org @ 2023-10-25  5:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

Sam James <sjames at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://github.com/systemd/
                   |                            |systemd/issues/22801,
                   |                            |https://github.com/systemd/
                   |                            |systemd/pull/25688

--- Comment #4 from Sam James <sjames at gcc dot gnu.org> ---
This came up in the wild at https://github.com/systemd/systemd/issues/22801 and
https://github.com/systemd/systemd/pull/25688.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-10-25  5:54 ` sjames at gcc dot gnu.org
@ 2023-10-25 10:55 ` siddhesh at gcc dot gnu.org
  2023-10-25 11:08 ` siddhesh at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: siddhesh at gcc dot gnu.org @ 2023-10-25 10:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

--- Comment #5 from Siddhesh Poyarekar <siddhesh at gcc dot gnu.org> ---
This could work for alloc_size, but not quite for access.  pointer_with_size
(or __builtin_with_size as you suggested in that thread) would need to express
access semantics too, to be able to express everything that access does.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-10-25 10:55 ` siddhesh at gcc dot gnu.org
@ 2023-10-25 11:08 ` siddhesh at gcc dot gnu.org
  2023-10-25 13:03 ` muecker at gwdg dot de
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: siddhesh at gcc dot gnu.org @ 2023-10-25 11:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

--- Comment #6 from Siddhesh Poyarekar <siddhesh at gcc dot gnu.org> ---
So basically,

  __builtin_with_access(void *ptr, size_t size, int access)

where access ==

-1: Unknown access semantics
0: none
1: read_only
2: write_only
3: read_write

should address both access and alloc_size and even counted_by.  We would need
to emit the builtin in the caller as well as callee of the function that has
the access attribute while for alloc_size, we only need to emit this in the
caller.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-10-25 11:08 ` siddhesh at gcc dot gnu.org
@ 2023-10-25 13:03 ` muecker at gwdg dot de
  2023-10-25 13:40 ` siddhesh at gcc dot gnu.org
  2023-11-03 19:04 ` uecker at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: muecker at gwdg dot de @ 2023-10-25 13:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

--- Comment #7 from Martin Uecker <muecker at gwdg dot de> ---
Am Mittwoch, dem 25.10.2023 um 11:08 +0000 schrieb siddhesh at gcc dot gnu.org:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503
> 
> --- Comment #6 from Siddhesh Poyarekar <siddhesh at gcc dot gnu.org> ---
> So basically,
> 
>   __builtin_with_access(void *ptr, size_t size, int access)
> 
> where access ==
> 
> -1: Unknown access semantics
> 0: none
> 1: read_only
> 2: write_only
> 3: read_write
> 
> should address both access and alloc_size and even counted_by.  

Yes, sounds good.

> We would need
> to emit the builtin in the caller as well as callee of the function that has
> the access attribute while for alloc_size, we only need to emit this in the
> caller.

Yes, makes sense, although I guess caller part for "access"
is only for warning and not relevant for BDOS, so could 
potentially stay as it is for now.

For __builtin_with_access we probably only want to allow
reducing the object size, while the 'extend_size' workaround 
used by systemd (cf comment #4) would need to extend it. 
Maybe we need another flag?

Martin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2023-10-25 13:03 ` muecker at gwdg dot de
@ 2023-10-25 13:40 ` siddhesh at gcc dot gnu.org
  2023-11-03 19:04 ` uecker at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: siddhesh at gcc dot gnu.org @ 2023-10-25 13:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

--- Comment #8 from Siddhesh Poyarekar <siddhesh at gcc dot gnu.org> ---
(In reply to Martin Uecker from comment #7)
> For __builtin_with_access we probably only want to allow
> reducing the object size, while the 'extend_size' workaround 
> used by systemd (cf comment #4) would need to extend it. 
> Maybe we need another flag?

I've been thinking of a new __object_size__ attribute to annotate functions
that behave like __builtin_object_size so that calls to it override (and not
just reduce) sizes returned by allocations.  I can then use it to annotate a
supported malloc_usable_size replacement (say, malloc_size_estimate) that
actually works like __builtin_object_size and can then be used by systemd.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug ipa/96503] attribute alloc_size effect lost after inlining
  2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2023-10-25 13:40 ` siddhesh at gcc dot gnu.org
@ 2023-11-03 19:04 ` uecker at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: uecker at gcc dot gnu.org @ 2023-11-03 19:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

uecker at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |uecker at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-11-03

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-11-03 19:04 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-06 15:51 [Bug ipa/96503] New: attribute alloc_size effect lost after inlining msebor at gcc dot gnu.org
2022-09-30  1:54 ` [Bug ipa/96503] " kees at outflux dot net
2022-11-18 23:57 ` pageexec at gmail dot com
2023-10-25  5:46 ` muecker at gwdg dot de
2023-10-25  5:54 ` sjames at gcc dot gnu.org
2023-10-25 10:55 ` siddhesh at gcc dot gnu.org
2023-10-25 11:08 ` siddhesh at gcc dot gnu.org
2023-10-25 13:03 ` muecker at gwdg dot de
2023-10-25 13:40 ` siddhesh at gcc dot gnu.org
2023-11-03 19:04 ` uecker at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).