public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexandre Oliva <oliva@adacore.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Introduce hardbool attribute for C
Date: Fri, 08 Jul 2022 12:28:57 -0300	[thread overview]
Message-ID: <or5yk7k4hy.fsf@lxoliva.fsfla.org> (raw)
In-Reply-To: <CAFiYyc3NFmmipnVOYxL0=8RWvtkX3ziPnjBzYbuu07bnoy93Bg@mail.gmail.com> (Richard Biener's message of "Fri, 8 Jul 2022 08:58:34 +0200")

On Jul  8, 2022, Richard Biener <richard.guenther@gmail.com> wrote:

> Does this follow some other compilers / language?

It is analogous to Ada's booleans with representation clauses and
runtime validation checking at use points.

> Is such feature used in existing code?

Not that I know.  The attribute name was my choice.

That said, we have already delivered the experimental implementation to
the customer who requested it (GCC was in stage3, thus the delayed
submission), so by now they may already have some code using it.

> Why is it useful to allow arbitrary values for true/false?

Increasing the hamming distance between legitimate values is desirable
to catch hardware-based attacks, but booleans are probably the only
builtin type that has room for that.

> Why is the default 0 and ~0 rather than 0 and 1 as for _Bool?

My understanding is that the goal is to maximize the hamming distance
between the legitimate values, so as to increase the sensibility to
errors.

There was no requirement for defaults to be these values, however.  The
examples often used 0x5a and 0xa5, but those seemed too arbitrary to be
defaults.  I found ~1 and 1 to be too nasty, so I went for 0 and ~0,
that are still recognizable as false and true values, respectively,
though I'm not sure whether this is advantageous.

>> +@smallexample
>> +typedef char __attribute__ ((__hardbool__ (0x5a))) hbool;
>> +hbool first = 0;       /* False, stored as (char)0x5a.  */
>> +hbool second = !first; /* True, stored as ~(char)0x5a.  */
> hbool thrid;

> what's the initial value of 'third'?

If it's an automatic variable, it's uninitialized, as expected for C.
It might by chance happen to hold one of the legitimate values, but odds
are it doesn't, and if so, accessing it will trap.

If it's a static-storage variable, it will be zero-initialized as
expected, but the zero will be mapped to the representation for false.

> The documentation should probably be explicit about this case.

Agreed, thanks, will do.

> If the underlying representation is an Enum, why not have
> hardened_enum instead?

In Ada, Booleans are enumerator types with various conventions and
builtin operations, with or without a representation clause, that sets
the representation values for the enumerators.  Other enumerations
aren't subject to such conventions as automatic conversions between
Boolean types with different representations, that enable all of them to
be used interchangeably (source-wise) in logical expressions.

It would nevertheless be feasible to implement hardened enumerator
types, that, like Ada, perform runtime validation checking that the
stored value corresponds to one of the enumerators.  This would not fit
some common use cases of enumerator types, e.g. those in which
enumerators define bits or masks, and different enumerators are combined
into a single variable.  This was not the feature that we were asked to
implement, though.

> A hardened _Bool might want to have a special NaT value as well I
> guess?

That might sound appealing, but ISTM that it would instead break the
symmetry of the maximal hamming distance between the representation
values for true and false.  OTOH, NaB, if so desired, would be just any
other value; the challenge would be to get such a value stored in a
variable, given that actual booleans can only hold true (nonzero) or
false (zero), and neither would convert to NaB.

-- 
Alexandre Oliva, happy hacker                https://FSFLA.org/blogs/lxo/
   Free Software Activist                       GNU Toolchain Engineer
Disinformation flourishes because many people care deeply about injustice
but very few check the facts.  Ask me about <https://stallmansupport.org>

  parent reply	other threads:[~2022-07-08 15:29 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-07 19:59 Alexandre Oliva
2022-07-08  6:58 ` Richard Biener
2022-07-08 13:39   ` Sebastian Huber
2022-07-08 15:28   ` Alexandre Oliva
2022-07-08 15:28   ` Alexandre Oliva [this message]
2022-07-11  9:59     ` Richard Biener
2022-08-09 13:53 ` Alexandre Oliva
2023-06-16  5:35   ` Alexandre Oliva
2023-06-19 18:48     ` Bernhard Reutner-Fischer
2023-06-22  1:08       ` Alexandre Oliva
2023-06-22 21:15         ` Bernhard Reutner-Fischer
2023-06-24  2:57           ` Alexandre Oliva
2023-06-21 15:57     ` Qing Zhao
2023-06-22  2:35       ` Alexandre Oliva
2023-06-23 21:40         ` Qing Zhao
2023-06-24  2:38           ` Alexandre Oliva
2023-06-26 19:05             ` Qing Zhao
2023-06-28  7:26               ` Alexandre Oliva
2023-06-28 15:07                 ` Qing Zhao
2023-06-29 10:30                   ` Alexandre Oliva
2023-06-29 15:21                     ` Qing Zhao
2023-06-24  4:42     ` Alexandre Oliva
2023-10-20  5:31       ` [PATCH v4] " Alexandre Oliva
2023-11-20 12:45         ` Alexandre Oliva
2023-11-20 13:56           ` Richard Biener
2023-11-29  9:30             ` Alexandre Oliva
     [not found]   ` <20230403012803.29ccf00b@nbbrfq>
     [not found]     ` <orzg7l8rr4.fsf@lxoliva.fsfla.org>
     [not found]       ` <6890D6BA-73DC-4F91-9413-228492A7F09B@gmail.com>
     [not found]         ` <ora5x0x8x9.fsf@lxoliva.fsfla.org>
2023-06-16  6:52           ` [PATCH] " Thomas Koenig
2023-06-16  8:03             ` Alexandre Oliva

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=or5yk7k4hy.fsf@lxoliva.fsfla.org \
    --to=oliva@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.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).