public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Torbjorn SVENSSON <torbjorn.svensson@foss.st.com>
To: Nick Alcock <nick.alcock@oracle.com>
Cc: <binutils@sourceware.org>, <amodra@gmail.com>,
	Yvan ROUX <yvan.roux@foss.st.com>
Subject: Re: [PATCH v3] libctf: ctf_member_next needs to return (ssize_t)-1 on error
Date: Fri, 29 Sep 2023 14:11:50 +0200	[thread overview]
Message-ID: <ce011a56-2a75-5a9e-73ef-04f9307b6850@foss.st.com> (raw)
In-Reply-To: <87r0mimero.fsf@esperi.org.uk>



On 2023-09-28 18:41, Nick Alcock wrote:
> On 26 Sep 2023, Torbjorn SVENSSON said:
> 
>> On 2023-09-26 16:51, Nick Alcock wrote:
>>> On 13 Sep 2023, Torbjorn SVENSSON outgrape:
>>>> On 2023-09-13 20:37, Nick Alcock wrote:
>>>>> On 13 Sep 2023, Torbjörn SVENSSON verbalised:
>>> Honestly I suspect all we need is a better name:
>>> ctf_set_int_errno(...);
>>> ctf_set_type_errno(...)
>>> and then use one or the other, consistently. (Neither needs to call the
>>> other: they're only two lines long!)
>>
>> Ok. I've updated the patch (V4) to be like you suggested above.
> 
> Thanks!
> 
>>>> I suppose the ctf_set_errno_unsigned could even be a macro in the ctf-impl.h header file.
>>> I'd make both of them inline functions personally (I bet it would reduce
>>> code size!)
>>
>> I do not see any major difference in code size for the ld.exe binary after the change.
> 
> Oh well, it was just a pious hope really.
> 
>>>>>> +int
>>>>>> +ctf_set_errno_signed (ctf_dict_t *fp, int err)
>>>>>> +{
>>>>>> +  fp->ctf_errno = err;
>>>>>> +  /* Don't rely on CTF_ERR here as it will not properly sign extend on 64-bit
>>>>>> +     Windows ABI.  */
>>>>>> +  return -1;
>>>>>> +}
>>>>> ... that Windows is not really the problem here. It's more
>>>>> /* Don't rely on CTF_ERR here; it is a ctf_id_t (unsigned long), and
>>>>>       it will be truncated to a non--1 value on platforms on which int
>>>>>       and unsigned long are different sizes.  */
>>>>> perhaps? (At least, I think that's what's going on.)
>>>>
>>>> The problem happens when the signed integral type is wider than unsigned long.
>>> ... sizeof(signed int) > sizeof(unsigned long int)?! Is that even
>>> possible? I would have assumed from the C type hierarchy and the integer
>>> conversion rank rules would have required that unsigned long int was at
>>> least as big as any non-long integral type, but I don't see anywhere
>>> it's required in the standard, dammit...
>>
>> I don't know about the 'sizeof(signed int) > sizeof(unsigned long
>> int)' part, but what I said was _integral type_, not _int_. In the
> 
> Ah true. My apologies.
> 
>> case where I saw the problem, it was ssize_t but I'm not sure what
>> that maps to, but it's wider than unsigned long int apparently in this
>> case.
> 
> Aha! So this is *not* a problem with functions returning int -- it is
> specifically a problem with functions returning *size_t types*.
> 

So, I'm not sure if were are talking past each other, but I don't think 
it's a *size_t type* issue either.

As I see it, there are 3 different scenarios.

1. The function returns a signed integral type, then -1 would be fine. 
Even a simple (int)-1 would work as it would be sign-extended.

2. The function returns an unsigned integral type that is as wide, or 
wider, than unsigned long. In this case, CTF_ERR will be zero extended 
and the value will be UINT_something_MAX (the number of binary ones 
depends on the sizeof(unsigned long)).

3. The function return an unsigned integral type that is narrower than 
unsigned long. In this case, the cast to unsigned long is wider than the 
return type and may overflow(?). Is it safe to return something bigger 
than can be represented in the return type?

I don't know if the third option above is applicable as I have not 
checked the width of the types that is calling the ctf_set_errno() 
function before my patch, but the other 2 are there.

In my mind, I see it as we need 2 different implementations. One that 
returns a simple -1 and another one that casts it for all unsigned 
calls, but maybe I'm oversimplifying this.


To make things easier, I would actually consider just having the assign 
statement and the return statement inlined (without a macro or inline 
function) directly where they are supposed to be.
Would you be open to removing the ctf_set_errno() function completely 
and just expand it to where it's called (almost like my v2 patch, but 
everywhere instead)?
Alternatively, we could start the discussion on adopting the C11 
standard instead, but I fear that this will be a much longer discussion 
than figuring out what the best approach would be for libctf.


> My apologies, I misunderstood the entire problem.  We probably *do*
> still want ctf_set_errno_signed for functions returning int (for clarity
> if nothing else), but for ssize_t in particular this won't do: we
> probably want a ctf_set_errno_ssize_t or something. The name is awful
> but I wasted a day failing to think of a better one :(
> 
> There are very few functions returning (s)size_t in libctf:
> 
> extern size_t ctf_archive_count (const ctf_archive_t *);
> extern ssize_t ctf_type_lname (ctf_dict_t *, ctf_id_t, char *, size_t);
> extern ssize_t ctf_type_size (ctf_dict_t *, ctf_id_t);
> extern ssize_t ctf_type_align (ctf_dict_t *, ctf_id_t);
> extern ssize_t ctf_member_next (ctf_dict_t *, ctf_id_t, ctf_next_t **,
> 				const char **name, ctf_id_t *membtype,
> 				int flags);
> 
> Of these, ctf_archive_count () cannot fail, so the problem reduces to
> ssize_t alone.  These functions should probably
> 
>      return (ssize_t) ctf_set_errno_signed (...))
> 
> (it's rare enough that a utility functions to do this is probably
> unnecessary).
> 
> We also have (in ctf_type_lname):
> 
>    if (str == NULL)
>      return CTF_ERR;			/* errno is set for us.  */
> 
> This should probably become a straight -1 (no cast necessary).

Yes, this is another place where the problem appears.
Looks like the -Wsign-conversion does not warn when it's the return 
statement for the function. Doing a simple grep in the tree reveals 56 
places that needs to be verified.

> ctf_type_size () already gets this right (but needs _ssize_t adjustments
> to its ctf_set_errno () calls, as does get_vbytes_common in ctf-open.c).
> The same is true of ctf_type_align (), and, of course, ctf_member_next
> ().
> 
>>>>> This probably needs testing on a wide variety of platforms with
>>>>> different type sizes. I'll add throwing this through my entire test
>>>>> matrix to my todo list, and fix any bugs observed: but the basic idea
>>>>> looks sound to me.
>>>>
>>>> Do you want to run this full matrix before or after submitting the patch?
>>>> If it's before; when do you think you will have time to do that?
>>>>
>>>> Let me know how you want to proceed.
>>> OK, I'm back from various conferences so I can throw tests past this at
>>> any time, it's largely automated. So once I stop faffing about and
>>> changing my mind and we converge on something I'll throw it past every
>>> test I've got. (It takes a day or so.)
>>
>> If you do not see any problem with the V4 patch, then please go ahead
>> and run the tests that you have to get a verdict.
> 
> ... sorry, I'm still flailing at it. Maybe the above is helpful? (It's
> only a very small change atop what you've already done, I think.)
> 

Let me know how you want to handle this. :)

Kind regards,
Torbjörn


  reply	other threads:[~2023-09-29 12:11 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-24 11:32 [PATCH] " Torbjörn SVENSSON
2023-08-25  2:22 ` Alan Modra
2023-08-25 16:53   ` [PATCH v2] " Torbjörn SVENSSON
2023-08-30  8:34     ` Torbjorn SVENSSON
2023-08-30  9:39       ` Alan Modra
2023-09-07 12:10         ` Nick Alcock
2023-09-08 12:58           ` Torbjorn SVENSSON
2023-09-12 14:23             ` Nick Alcock
2023-09-12 18:44               ` Torbjorn SVENSSON
2023-09-13  9:57               ` [PATCH v3] " Torbjörn SVENSSON
2023-09-13 18:37                 ` Nick Alcock
2023-09-13 20:20                   ` Torbjorn SVENSSON
2023-09-20 17:44                     ` Torbjorn SVENSSON
2023-09-26 14:51                     ` Nick Alcock
2023-09-26 17:28                       ` [PATCH v4] " Torbjörn SVENSSON
2023-09-26 17:49                       ` [PATCH v3] " Torbjorn SVENSSON
2023-09-28 16:41                         ` Nick Alcock
2023-09-29 12:11                           ` Torbjorn SVENSSON [this message]
2023-10-02 10:57                             ` Nick Alcock
2023-10-03 12:59                               ` Torbjorn SVENSSON
2023-10-03 20:53                                 ` Nick Alcock
2023-10-05  8:39                                   ` [PATCH v5] libctf: Sanitize error types for PR 30836 Torbjörn SVENSSON
2023-10-09 10:27                                     ` Nick Alcock
2023-10-09 14:44                                       ` [PATCH v6] " Torbjörn SVENSSON
2023-10-09 15:11                                         ` [PATCH v7] " Torbjörn SVENSSON
2023-10-11 11:14                                           ` Nick Alcock
2023-10-13 14:01                                           ` [PATCH] libctf: check for problems with error returns Nick Alcock
2023-10-13 18:31                                             ` Torbjorn SVENSSON
2023-10-15 19:18                                               ` Nick Alcock
2023-10-16 12:51                                                 ` [PATCH v8] libctf: Sanitize error types for PR 30836 Torbjörn SVENSSON
2023-10-17 15:15                                                   ` Nick Alcock
2023-10-17 15:35                                                     ` Torbjorn SVENSSON
2023-10-17 18:54                                                       ` [PATCH] libctf: Return CTF_ERR in ctf_type_resolve_unsliced " Torbjörn SVENSSON
2023-10-17 19:40                                                         ` Nick Alcock
2023-10-18  7:40                                                           ` Torbjorn SVENSSON
2023-10-20 17:01                                                             ` Nick Alcock
2023-10-16 13:02                                                 ` [PATCH] libctf: check for problems with error returns Torbjorn SVENSSON
2023-10-17 14:45                                                   ` Nick Alcock
2024-01-30 12:46                                             ` Andreas Schwab
2024-01-30 14:22                                               ` Nick Alcock
2024-01-30 14:27                                                 ` Andreas Schwab
2024-03-09  2:44                                                   ` Sam James
2024-03-11 15:14                                                     ` Nick Alcock
2024-03-12  6:52                                                       ` Sam James

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=ce011a56-2a75-5a9e-73ef-04f9307b6850@foss.st.com \
    --to=torbjorn.svensson@foss.st.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=nick.alcock@oracle.com \
    --cc=yvan.roux@foss.st.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).