public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kees at outflux dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug sanitizer/102317] signed integer overflow sanitizer cannot work well with -fno-strict-overflow
Date: Tue, 14 Sep 2021 15:57:28 +0000	[thread overview]
Message-ID: <bug-102317-4-cBV7piZ33K@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-102317-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #7 from Kees Cook <kees at outflux dot net> ---
The problem the kernel needs to solve is basically having our cake and eating
it too. :)

In _most_ situations, we want signed overflows to trap (i.e. get caught by
"-fsanitize=signed-integer-overflow").

In some limited situations, we want to be able to depend on 2s-complement
wrap-around on overflow -- and we would create a helper function to do this
work, marked with __attribute__((no_sanitize("signed-integer-overflow"))). (We
need this most for our atomic_t/refcount_t code, as well as all the open-coded
bounds checks -- see details below.)

For the latter case (wrap-around), the kernel must depend on the effects of
"-fwrapv-pointer" and "-fwrapv" because otherwise some cases end up being
elided due to being classified as undefined behavior.

(In reply to Jakub Jelinek from comment #6)
> That doesn't make sense.  -fsanitize=signed-integer-overflow also removes
> that undefined behavior by defining what happens on signed integer overflow,
> one can choose whether to get a non-fatal runtime diagnostic + wrapv
> behavior, or fatal runtime diagnostic, or just abort.

There doesn't appear to be a way to remove the UB _and_ the diagnostic on a
case-by-case basis.

The matrix of behaviors:

    code generation: UB, no UB (2s-complement wrap-around)

    diagnostics:     no diagnostic, warn or trap

-fno-strict-overflow provides "no UB", and "no diagnostic". ("warn or trap" are
not available.)

-fsanitize=signed-integer-overflow provides "no UB" and "warn or trap". ("no
diagnostic" is not available; using
__attribute__((no_sanitize("signed-integer-overflow"))) also removes "no UB")

So, on a per-function basis, we need to _either_ catch overflow _or_
deterministically perform 2s-complement wrap-around.


If there's a way to achieve this already, I would be very grateful, as I've not
been able to find the correct combination.


Background on the kernel's use of -fno-strict-overflow:
https://git.kernel.org/linus/a137802ee839ace40079bebde24cfb416f73208a
and (earlier) -fwrapv:
https://git.kernel.org/linus/68df3755e383e6fecf2354a67b08f92f18536594

An example of wanting no-UB without diagnostic:
https://git.kernel.org/linus/adb03115f4590baa280ddc440a8eff08a6be0cb7
which ultimately had to be reverted:
https://git.kernel.org/linus/a6211caa634da39d861a47437ffcda8b38ef421b
and all of UBSAN integer overflow support was removed:
https://git.kernel.org/linus/6aaa31aeb9cf260e1b7155cc11ec864f052db5ec

  parent reply	other threads:[~2021-09-14 15:57 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-13 21:31 [Bug sanitizer/102317] New: " qinzhao at gcc dot gnu.org
2021-09-13 21:35 ` [Bug sanitizer/102317] " pinskia at gcc dot gnu.org
2021-09-13 21:41 ` qinzhao at gcc dot gnu.org
2021-09-13 21:45 ` pinskia at gcc dot gnu.org
2021-09-13 21:57 ` jakub at gcc dot gnu.org
2021-09-13 22:01 ` qing.zhao at oracle dot com
2021-09-14  5:55 ` jakub at gcc dot gnu.org
2021-09-14 15:57 ` kees at outflux dot net [this message]
2021-09-14 16:04 ` jakub at gcc dot gnu.org
2021-09-14 18:36 ` kees at outflux dot net
2021-09-14 18:53 ` jakub at gcc dot gnu.org
2021-09-23 22:58 ` kees at outflux dot net
2023-09-07 20:53 ` qinzhao at gcc dot gnu.org
2024-02-15  1:22 ` i at maskray dot me

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=bug-102317-4-cBV7piZ33K@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).