public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101419] New: collapsing memset() calls can break __builtin_object_size()
@ 2021-07-11 14:45 kees at outflux dot net
  2021-07-12  7:59 ` [Bug tree-optimization/101419] " rguenth at gcc dot gnu.org
                   ` (24 more replies)
  0 siblings, 25 replies; 26+ messages in thread
From: kees at outflux dot net @ 2021-07-11 14:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101419
           Summary: collapsing memset() calls can break
                    __builtin_object_size()
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kees at outflux dot net
  Target Milestone: ---

Created attachment 51131
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51131&action=edit
memset collapsing breaks __builtin_object_size()

I've found a strange misoptimization around memset() and
__builtin_object_size(). If there are two memset() calls that can be collapsed
(due to being fully overlapping, I assume), use of __builtin_object_size() may
return the wrong result.

The example code shows that __builtin_object_size(&int_value, 1) returns 1
instead of 4:

> $ gcc  -Wall -Wextra -fno-strict-aliasing -fwrapv -O2 -c -o wat.o wat.c 
> In function ‘do_wipe’,
>     inlined from ‘loops’ at wat.c:24:3:
> wat.c:15:3: warning: call to ‘__detected_overflow’ declared with attribute warning: detected overflow [-Wattribute-warning]
>    15 |   __detected_overflow(__builtin_object_size(&info->lg, 1), sizeof(info->lg));
>       |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here, info->lg is int, but the call to __builtin_object_size() resolves to the
size of info->sm (char). This can be seen directly in the resulting output:

> $ objdump -rd wat.o
> ...
> 0000000000000000 <loops>:
>    0:   53                      push   %rbx
>    1:   be 04 00 00 00          mov    $0x4,%esi
>    6:   48 89 fb                mov    %rdi,%rbx
>    9:   c6 07 00                movb   $0x0,(%rdi)
>    c:   bf 01 00 00 00          mov    $0x1,%edi
>   11:   e8 00 00 00 00          call   16 <loops+0x16>
>                         12: R_X86_64_PLT32      __detected_overflow-0x4
>   16:   c7 03 00 00 00 00       movl   $0x0,(%rbx)
>   1c:   5b                      pop    %rbx
>   1d:   c3                      ret    

The first argument to __detected_overflow() is "1", instead of 4.

Any changes to this example code makes the bug disappear (removal of loops,
removal of empty asm, or reordering of memset() calls).

Using Compiler Explorer, this bug appears to have been introduced between GCC
8.5 and 9.1: https://godbolt.org/z/oGq5K9fE4

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

end of thread, other threads:[~2023-07-07 10:40 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-11 14:45 [Bug c/101419] New: collapsing memset() calls can break __builtin_object_size() kees at outflux dot net
2021-07-12  7:59 ` [Bug tree-optimization/101419] " rguenth at gcc dot gnu.org
2021-07-12  8:33 ` [Bug tree-optimization/101419] [9/10/11/12 Regression] " jakub at gcc dot gnu.org
2021-07-12  9:03 ` rguenth at gcc dot gnu.org
2021-07-12  9:58 ` jakub at gcc dot gnu.org
2021-07-12 10:37 ` jakub at gcc dot gnu.org
2021-07-12 10:50 ` rguenth at gcc dot gnu.org
2021-07-12 10:59 ` jakub at gcc dot gnu.org
2021-07-12 11:21 ` jakub at gcc dot gnu.org
2021-07-12 11:26 ` jakub at gcc dot gnu.org
2021-07-12 11:42 ` jakub at gcc dot gnu.org
2021-07-12 11:51 ` rguenth at gcc dot gnu.org
2021-07-12 11:52 ` rguenth at gcc dot gnu.org
2021-07-12 11:56 ` rguenth at gcc dot gnu.org
2021-07-12 12:04 ` jakub at gcc dot gnu.org
2021-07-12 12:07 ` jakub at gcc dot gnu.org
2021-07-12 13:20 ` rguenther at suse dot de
2021-07-12 13:24 ` rguenther at suse dot de
2021-07-12 13:32 ` jakub at gcc dot gnu.org
2021-07-12 14:39 ` rguenther at suse dot de
2021-07-12 15:59 ` jakub at gcc dot gnu.org
2021-07-13  9:05 ` cvs-commit at gcc dot gnu.org
2021-07-13 13:05 ` [Bug tree-optimization/101419] [9/10/11 " jakub at gcc dot gnu.org
2022-05-27  9:45 ` [Bug tree-optimization/101419] [10/11 " rguenth at gcc dot gnu.org
2022-06-28 10:45 ` jakub at gcc dot gnu.org
2023-07-07 10:40 ` [Bug tree-optimization/101419] [11 " rguenth 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).