public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@gotplt.org>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] tree-object-size: Support strndup and strdup
Date: Thu, 22 Sep 2022 11:26:29 -0400	[thread overview]
Message-ID: <a1577272-5276-104d-f4f5-fe1c167be702@gotplt.org> (raw)
In-Reply-To: <Yyxc3GXsigF0r8XL@tucnak>

On 2022-09-22 09:02, Jakub Jelinek wrote:
> On Mon, Aug 15, 2022 at 03:23:11PM -0400, Siddhesh Poyarekar wrote:
>> --- a/gcc/tree-object-size.cc
>> +++ b/gcc/tree-object-size.cc
>> @@ -495,6 +495,18 @@ decl_init_size (tree decl, bool min)
>>     return size;
>>   }
>>   
>> +/* Get the outermost object that PTR may point into.  */
>> +
>> +static tree
>> +get_whole_object (const_tree ptr)
>> +{
>> +  tree pt_var = TREE_OPERAND (ptr, 0);
>> +  while (handled_component_p (pt_var))
>> +    pt_var = TREE_OPERAND (pt_var, 0);
>> +
>> +  return pt_var;
>> +}
> 
> Not sure why you want a new function for this.
> This is essentially get_base_address (TREE_OPERAND (ptr, 0)).

Oh, so can addr_object_size be simplified to use get_base_address too?

>>   /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
>>      OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
>>      If unknown, return size_unknown (object_size_type).  */
>> +  if (!size_valid_p (sz, object_size_type)
>> +       || size_unknown_p (sz, object_size_type))
>> +    {
>> +      tree wholesrc = NULL_TREE;
>> +      if (TREE_CODE (src) == ADDR_EXPR)
>> +	wholesrc = get_whole_object (src);
>> +
>> +      if (!(object_size_type & OST_MINIMUM)
>> +	  || (wholesrc && TREE_CODE (wholesrc) == STRING_CST))
> 
> Is this safe?  I mean get_whole_object will also skip ARRAY_REFs with
> variable indexes etc. and the STRING_CST could have embedded '\0's
> in it.
> Even if c_strlen (src, 1) is constant, I don't see what you can assume
> for object size of strndup ("abcd\0efgh", n); for minimum, except 1.

Can't we assume MIN(5, n) for STRING_CST?

For ARRAY_REFs, it may end up being MIN(array_size, n) and not account 
for the NUL termination but I was thinking of that as being a better 
option than bailing out.  Should we try harder here and return, e.g. 
strlen or some equivalent?

> But on the other side, 1 is a safe minimum for OST_MINIMUM of both
> strdup and strndup if you don't find anything more specific (exact strlen
> for strndup) because the terminating '\0' will be always there.

OK, I can return size_one_node as the final return value for OST_MINIMUM 
if we don't find a suitable expression.

> Other than that you'd need to consider INTEGER_CST second strndup argument
> or ranges of the second argument etc.
> E.g. maximum for OST_DYNAMIC could be for strndup (src, n)
> MIN (__bdos (src, ?), n + 1).

Yeah, that's what I return in the end:

   return fold_build2 (MIN_EXPR, sizetype,
                      fold_build2 (PLUS_EXPR, sizetype, size_one_node,n),
                      sz);

where sz is __bdos(src)

> 
>> @@ -2113,7 +2177,7 @@ const pass_data pass_data_object_sizes =
>>     PROP_objsz, /* properties_provided */
>>     0, /* properties_destroyed */
>>     0, /* todo_flags_start */
>> -  0, /* todo_flags_finish */
>> +  TODO_update_ssa_no_phi, /* todo_flags_finish */
>>   };
>>   
>>   class pass_object_sizes : public gimple_opt_pass
>> @@ -2153,7 +2217,7 @@ const pass_data pass_data_early_object_sizes =
>>     0, /* properties_provided */
>>     0, /* properties_destroyed */
>>     0, /* todo_flags_start */
>> -  0, /* todo_flags_finish */
>> +  TODO_update_ssa_no_phi, /* todo_flags_finish */
>>   };
> 
> This is quite expensive.  Do you really need full ssa update, or just
> TODO_update_ssa_only_virtuals would be enough (is it for the missing
> vuse on the strlen call if you emit it)?
> In any case, would be better not to do that always, but only if you
> really need it (emitted the strlen call somewhere; e.g. if __bdos is
> never used, only __bos, it is certainly not needed), todo flags
> can be both in todo_flags_finish and in return value from execute method.

Thanks, I'll find a cheaper way to do this.

Thanks,
Sid

  reply	other threads:[~2022-09-22 15:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-15 19:23 Siddhesh Poyarekar
2022-08-29 14:16 ` Siddhesh Poyarekar
2022-09-07 19:21   ` Siddhesh Poyarekar
2022-09-15 14:00     ` Siddhesh Poyarekar
2022-09-22 13:02 ` Jakub Jelinek
2022-09-22 15:26   ` Siddhesh Poyarekar [this message]
2022-09-23 13:02     ` Jakub Jelinek
2022-11-02 22:30       ` Siddhesh Poyarekar
2022-11-04 12:48 ` [PATCH v2] " Siddhesh Poyarekar
2022-11-04 13:43   ` Prathamesh Kulkarni
2022-11-04 13:47     ` Siddhesh Poyarekar
2022-11-17 19:47   ` Siddhesh Poyarekar
2022-11-20 15:42   ` Jeff Law
2022-11-21 14:27     ` Siddhesh Poyarekar
2022-11-22 20:43       ` Jeff Law
2022-11-22 23:13         ` Siddhesh Poyarekar

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=a1577272-5276-104d-f4f5-fe1c167be702@gotplt.org \
    --to=siddhesh@gotplt.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.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).