public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug translation/113066] New: Returning from a function marked noreturn allows execution to fall-through
@ 2023-12-18 15:23 luke.geeson at cs dot ucl.ac.uk
  2023-12-18 15:34 ` [Bug middle-end/113066] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: luke.geeson at cs dot ucl.ac.uk @ 2023-12-18 15:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113066
           Summary: Returning from a function marked noreturn allows
                    execution to fall-through
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: translation
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luke.geeson at cs dot ucl.ac.uk
  Target Milestone: ---

Consider the following program.

```
[[noreturn]] inline void ub_if_reached() { return; }

void P0 () {
  atomic_store_explicit(y,1,memory_order_release);
  ub_if_reached();
}
```

If compiled to target Armv8-a using `clang -O3`
(https://godbolt.org/z/T33Yncjsh), the whole of `P0` is optimised away.

This is because `ub_if_reached` is marked as `noreturn` which means this
function should not return, however the `return` statement does exactly that,
inducing undefined behaviour.

Clang warns of this behaviour, and optimises away the code in `P0` before the
call, since it assumes a program is UB-free and that `P0` is therefore
unreachable.  Further if we define an `f` that calls a `g` that calls `P0` from
`main` then all functions including `main` are also optimised away
(https://godbolt.org/z/qsraYr7o5).

Technically the standards and compiler are not required to do anything here as
there is UB, however if this program is compiled and linked with a program that
calls `P0` then the resultant execution may fallthrough to the code below `P0`.
Both GCC and LLVM allow `P0` to fallthrough - this is not good and only
functions guaranteed to return should be subject to this optimisation.

In practice we can fix this by:

- emitting an explicit abort (or undefined instruction that causes a trap) at
the end of `noreturn` functions like `ub_if_reached`, or
- if the compiler can statically determine that a `noreturn` function does in
fact `return` then ignore the noreturn attribute, and emit an abort on return.

As far as I can tell, this behaviour arises in clang, but clang has an optional
`-mllvm -trap-unreachable` to prevent this behaviour. I was made aware that GCC
has `-fsanitize=undefined` `-fsanitize-trap=undefined` for this, but these seem
to be general options. It would be good for GCC to have a flag similar to
`-mllvm -trap-unreachable`.

Disclaimer: The views of the authors expressed in this bug report are my own
and are not endorsed by Arm or any other company mentioned.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/113066] Returning from a function marked noreturn allows execution to fall-through
  2023-12-18 15:23 [Bug translation/113066] New: Returning from a function marked noreturn allows execution to fall-through luke.geeson at cs dot ucl.ac.uk
@ 2023-12-18 15:34 ` pinskia at gcc dot gnu.org
  2023-12-18 15:37 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 15:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
GCC does warn not just once but twice:
```
<source>: In function 'ub_if_reached':
<source>:2:44: warning: function declared 'noreturn' has a 'return' statement
    2 | [[noreturn]] inline void ub_if_reached() { return; }
      |                                            ^~~~~~
<source>:2:44: warning: 'noreturn' function does return
    2 | [[noreturn]] inline void ub_if_reached() { return; }
      |                                            ^
```

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/113066] Returning from a function marked noreturn allows execution to fall-through
  2023-12-18 15:23 [Bug translation/113066] New: Returning from a function marked noreturn allows execution to fall-through luke.geeson at cs dot ucl.ac.uk
  2023-12-18 15:34 ` [Bug middle-end/113066] " pinskia at gcc dot gnu.org
@ 2023-12-18 15:37 ` pinskia at gcc dot gnu.org
  2023-12-18 15:40 ` pinskia at gcc dot gnu.org
  2023-12-18 15:57 ` luke.geeson at cs dot ucl.ac.uk
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 15:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
GCC 2.95.3 has the same behavior ....

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/113066] Returning from a function marked noreturn allows execution to fall-through
  2023-12-18 15:23 [Bug translation/113066] New: Returning from a function marked noreturn allows execution to fall-through luke.geeson at cs dot ucl.ac.uk
  2023-12-18 15:34 ` [Bug middle-end/113066] " pinskia at gcc dot gnu.org
  2023-12-18 15:37 ` pinskia at gcc dot gnu.org
@ 2023-12-18 15:40 ` pinskia at gcc dot gnu.org
  2023-12-18 15:57 ` luke.geeson at cs dot ucl.ac.uk
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 15:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note -funreachable-traps works for what you want it to work at -O1 and above
but not at -O0 due to ub_if_reached not being inlined ...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/113066] Returning from a function marked noreturn allows execution to fall-through
  2023-12-18 15:23 [Bug translation/113066] New: Returning from a function marked noreturn allows execution to fall-through luke.geeson at cs dot ucl.ac.uk
                   ` (2 preceding siblings ...)
  2023-12-18 15:40 ` pinskia at gcc dot gnu.org
@ 2023-12-18 15:57 ` luke.geeson at cs dot ucl.ac.uk
  3 siblings, 0 replies; 5+ messages in thread
From: luke.geeson at cs dot ucl.ac.uk @ 2023-12-18 15:57 UTC (permalink / raw)
  To: gcc-bugs

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

Luke Geeson <luke.geeson at cs dot ucl.ac.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #4 from Luke Geeson <luke.geeson at cs dot ucl.ac.uk> ---
Ah my apologies - I missed that command line flag when I was checking...

I'm sorry for wasting your time. I will close and mark it as invalid.
Thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-18 15:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-18 15:23 [Bug translation/113066] New: Returning from a function marked noreturn allows execution to fall-through luke.geeson at cs dot ucl.ac.uk
2023-12-18 15:34 ` [Bug middle-end/113066] " pinskia at gcc dot gnu.org
2023-12-18 15:37 ` pinskia at gcc dot gnu.org
2023-12-18 15:40 ` pinskia at gcc dot gnu.org
2023-12-18 15:57 ` luke.geeson at cs dot ucl.ac.uk

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).