public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@gotplt.org>
To: Qing Zhao <qing.zhao@oracle.com>
Cc: jakub Jelinek <jakub@redhat.com>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: Another bug for __builtin_object_size? (Or expected behavior)
Date: Thu, 17 Aug 2023 16:57:32 -0400	[thread overview]
Message-ID: <23d86518-1e8c-aee4-eab9-2ed0ef7eabc3@gotplt.org> (raw)
In-Reply-To: <C0A4E1C0-4669-485A-A60B-8F60A0065501@oracle.com>

On 2023-08-17 16:23, Qing Zhao wrote:
>>> Then, I think whatever MIN or MAX, the early phase has more precise information than the later phase, we should use its result if it’s NOT UNKNOWN?
>>
>> We can't be sure about that though, can we?  For example for something like this:
>>
>> struct S
>> {
>>   int a;
>>   char b[10];
>>   int c;
>> };
>>
>> size_t foo (struct S *s)
>> {
>>   return __builtin_object_size (s->b, 1);
>> }
>>
>> size_t bar ()
>> {
>>   struct S *in = malloc (8);
>>
>>   return foo (in);
>> }
>>
>> returns 10 for __builtin_object_size in early_objsz but when it sees the malloc in the later objsz pass, it returns 4:
>>
>> $ gcc/cc1 -fdump-tree-objsz-details -quiet -o - -O bug.c
>> ...
>> foo:
>> .LFB0:
>> 	.cfi_startproc
>> 	movl	$10, %eax
>> 	ret
>> 	.cfi_endproc
>> ...
>> bar:
>> .LFB1:
>> 	.cfi_startproc
>> 	movl	$4, %eax
>> 	ret
>> 	.cfi_endproc
>> ...
>>
>> In fact, this ends up returning the wrong result for OST_MINIMUM:
>>
>> $ gcc/cc1 -fdump-tree-objsz-details -quiet -o - -O bug.c
>> ...
>> foo:
>> .LFB0:
>> 	.cfi_startproc
>> 	movl	$10, %eax
>> 	ret
>> 	.cfi_endproc
>> ...
>> bar:
>> .LFB1:
>> 	.cfi_startproc
>> 	movl	$10, %eax
>> 	ret
>> 	.cfi_endproc
>> ...
>>
>> bar ought to have returned 4 too (and I'm betting the later objsz must have seen that) but it got overridden by the earlier estimate of 10.
> 
> Okay, I see.
> 
> Then is this the similar issue we discussed previously?  (As following:)
> 
> "
>> Hi, Sid and Jakub,
>> I have a question in the following source portion of the routine “addr_object_size” of gcc/tree-object-size.cc:
>>   743       bytes = compute_object_offset (TREE_OPERAND (ptr, 0), var);
>>   744       if (bytes != error_mark_node)
>>   745         {
>>   746           bytes = size_for_offset (var_size, bytes);
>>   747           if (var != pt_var && pt_var_size && TREE_CODE (pt_var) == MEM_REF)
>>   748             {
>>   749               tree bytes2 = compute_object_offset (TREE_OPERAND (ptr, 0),
>>   750                                                    pt_var);
>>   751               if (bytes2 != error_mark_node)
>>   752                 {
>>   753                   bytes2 = size_for_offset (pt_var_size, bytes2);
>>   754                   bytes = size_binop (MIN_EXPR, bytes, bytes2);
>>   755                 }
>>   756             }
>>   757         }
>> At line 754, why we always use “MIN_EXPR” whenever it’s for OST_MINIMUM or not?
>> Shall we use
>> (object_size_type & OST_MINIMUM
>>                              ? MIN_EXPR : MAX_EXPR)
> 
> That MIN_EXPR is not for OST_MINIMUM.  It is to cater for allocations like this:
> 
> typedef struct
> {
>   int a;
> } A;
> 
> size_t f()
> {
>   A *p = malloc (1);
> 
>   return __builtin_object_size (p, 0);
> }
> 
> where the returned size should be 1 and not sizeof (int).  The mode doesn't really matter in this case.
> “
> 
> If this is the same issue, I think we can use the same solution: always use MIN_EXPR,
> What do you think?

It's not exactly the same issue, the earlier discussion was about 
choosing sizes in the same pass while the current one is about choosing 
between passes, but I agree it "rhymes".  This is what I was alluding to 
originally (for OST_MINIMUM use MIN_EXPR if both passes returned a pass) 
but I haven't thought about it hard enough to be 100% confident that 
it's the better solution, especially for OST_MAXIMUM.

Thanks,
Sid

  reply	other threads:[~2023-08-17 20:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-16 15:59 Qing Zhao
2023-08-16 20:16 ` Qing Zhao
2023-08-17 11:00 ` Siddhesh Poyarekar
2023-08-17 13:58   ` Qing Zhao
2023-08-17 17:49     ` Siddhesh Poyarekar
2023-08-17 19:27       ` Qing Zhao
2023-08-17 19:59         ` Siddhesh Poyarekar
2023-08-17 20:23           ` Qing Zhao
2023-08-17 20:57             ` Siddhesh Poyarekar [this message]
2023-08-17 21:25               ` Qing Zhao
2023-08-17 21:32                 ` Siddhesh Poyarekar
2023-08-18 16:00                   ` Qing Zhao
2023-08-23 16:40                     ` Qing Zhao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=23d86518-1e8c-aee4-eab9-2ed0ef7eabc3@gotplt.org \
    --to=siddhesh@gotplt.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=qing.zhao@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).