public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109707] New: Skip __attribute__((naked)) functions for -pg and -finstrument-functions
@ 2023-05-03  4:41 i at maskray dot me
  2023-05-03  4:41 ` [Bug c/109707] " i at maskray dot me
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: i at maskray dot me @ 2023-05-03  4:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109707
           Summary: Skip __attribute__((naked)) functions for -pg and
                    -finstrument-functions
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: i at maskray dot me
  Target Milestone: ---

The asm in a naked function may reasonably expect the argument registers and
the
return address register (if present) to be live.

When using -pg and -finstrument-functions, functions are instrumented by adding
a function call to mcount/__cyg_profile_func_enter, which will clobber these
registers.
If the return address register is clobbered, the function will be unable to
return to the caller, possibly causing an infinite loop.

% riscv64-linux-gnu-gcc a.c -finstrument-functions
-Wl,--dynamic-linker=/usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1
-Wl,-rpath=/usr/riscv64-linux-gnu/lib
% ./a.out   # infinite loop
% riscv64-linux-gnu-gcc a.c -pg
-Wl,--dynamic-linker=/usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1
-Wl,-rpath=/usr/riscv64-linux-gnu/lib
% ./a.out   # infinite loop

% arm-linux-gnueabihf-gcc a.c -finstrument-functions
-Wl,--dynamic-linker=/usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3
-Wl,-rpath=/usr/arm-linux-gnueabihf/lib
% ./a.out   # infinite loop


arm -pg uses `push {lr}; bl __gnu_mcount_nc`, so a naked function works with
-pg, but this appears to be a rare exception.

% arm-linux-gnueabihf-gcc a.c -pg
-Wl,--dynamic-linker=/usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3
-Wl,-rpath=/usr/arm-linux-gnueabihf/lib
% ./a.out   # good

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

* [Bug c/109707] Skip __attribute__((naked)) functions for -pg and -finstrument-functions
  2023-05-03  4:41 [Bug c/109707] New: Skip __attribute__((naked)) functions for -pg and -finstrument-functions i at maskray dot me
@ 2023-05-03  4:41 ` i at maskray dot me
  2023-05-03  4:44 ` [Bug middle-end/109707] naked attribute should imply the no_instrument_function attribute pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: i at maskray dot me @ 2023-05-03  4:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Fangrui Song <i at maskray dot me> ---
I came here from a PR for Clang:
https://github.com/llvm/llvm-project/issues/62504

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

* [Bug middle-end/109707] naked attribute should imply the no_instrument_function attribute
  2023-05-03  4:41 [Bug c/109707] New: Skip __attribute__((naked)) functions for -pg and -finstrument-functions i at maskray dot me
  2023-05-03  4:41 ` [Bug c/109707] " i at maskray dot me
@ 2023-05-03  4:44 ` pinskia at gcc dot gnu.org
  2023-05-03  4:45 ` pinskia at gcc dot gnu.org
  2023-05-03  5:02 ` i at maskray dot me
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-03  4:44 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|target                      |middle-end
            Summary|Skip __attribute__((naked)) |naked attribute should
                   |functions for -pg and       |imply the
                   |-finstrument-functions      |no_instrument_function
                   |                            |attribute

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
As a workaround, you could add the attribute no_instrument_function.

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

* [Bug middle-end/109707] naked attribute should imply the no_instrument_function attribute
  2023-05-03  4:41 [Bug c/109707] New: Skip __attribute__((naked)) functions for -pg and -finstrument-functions i at maskray dot me
  2023-05-03  4:41 ` [Bug c/109707] " i at maskray dot me
  2023-05-03  4:44 ` [Bug middle-end/109707] naked attribute should imply the no_instrument_function attribute pinskia at gcc dot gnu.org
@ 2023-05-03  4:45 ` pinskia at gcc dot gnu.org
  2023-05-03  5:02 ` i at maskray dot me
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-03  4:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> As a workaround, you could add the attribute no_instrument_function.

Which is documented here:
https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Common-Function-Attributes.html#index-no_005finstrument_005ffunction-function-attribute

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

* [Bug middle-end/109707] naked attribute should imply the no_instrument_function attribute
  2023-05-03  4:41 [Bug c/109707] New: Skip __attribute__((naked)) functions for -pg and -finstrument-functions i at maskray dot me
                   ` (2 preceding siblings ...)
  2023-05-03  4:45 ` pinskia at gcc dot gnu.org
@ 2023-05-03  5:02 ` i at maskray dot me
  3 siblings, 0 replies; 5+ messages in thread
From: i at maskray dot me @ 2023-05-03  5:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Fangrui Song <i at maskray dot me> ---
(In reply to Andrew Pinski from comment #2)
> As a workaround, you could add the attribute no_instrument_function.

Yes. I thought of this possibility, but pcc's reply on
https://github.com/llvm/llvm-project/issues/62504 makes sense to me: the only
one reasonable way is probably to skip naked functions.


The Linux kernel ran into this combination in 2009 and they switched to use
__attribute__((naked)) and __attribute__((__no_instrument_function__)) together
in https://git.kernel.org/linus/446c92b2901bedb3725d29b4e73def8aba623ffc 

> #define __naked				__attribute__((naked)) notrace
>
>
> (before hotpatch/patchable_function_entry) #define notrace                 __attribute__((__no_instrument_function__))

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

end of thread, other threads:[~2023-05-03  5:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-03  4:41 [Bug c/109707] New: Skip __attribute__((naked)) functions for -pg and -finstrument-functions i at maskray dot me
2023-05-03  4:41 ` [Bug c/109707] " i at maskray dot me
2023-05-03  4:44 ` [Bug middle-end/109707] naked attribute should imply the no_instrument_function attribute pinskia at gcc dot gnu.org
2023-05-03  4:45 ` pinskia at gcc dot gnu.org
2023-05-03  5:02 ` i at maskray dot me

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