[gcc r14-8504] bpf: emit empty epilogues in naked functions

Jose E. Marchesi jemarch@gcc.gnu.org
Mon Jan 29 17:52:20 GMT 2024


https://gcc.gnu.org/g:1959aeee1e0e0b5eca12178444ba2f28c0ae558f

commit r14-8504-g1959aeee1e0e0b5eca12178444ba2f28c0ae558f
Author: Jose E. Marchesi <jose.marchesi@oracle.com>
Date:   Mon Jan 29 17:47:00 2024 +0100

    bpf: emit empty epilogues in naked functions
    
    This patch fixes the BPF backend to not generate `exit' (return)
    instructions in epilogues of functions that are declared as naked via
    the corresponding compiler attribute.  Having extra exit instructions
    upsets the kernel BPF verifier.
    
    Tested in bpf-unknown-none target in x86_64-linux-gnu host.
    
    gcc/ChangeLog
    
            * config/bpf/bpf.cc (bpf_expand_epilogue): Do not emit a return
            instruction in naked function epilogues.
    
    gcc/testsuite/ChangeLog
    
            * gcc.target/bpf/naked-1.c: Update test to not expect an exit
            instruction in naked function.
            * gcc.target/bpf/naked-2.c: New test.

Diff:
---
 gcc/config/bpf/bpf.cc                  |  5 ++---
 gcc/testsuite/gcc.target/bpf/naked-1.c |  1 -
 gcc/testsuite/gcc.target/bpf/naked-2.c | 10 ++++++++++
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 9af1728d8520..d6ca47eeecbe 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -420,9 +420,8 @@ bpf_expand_epilogue (void)
   /* See note in bpf_expand_prologue for an explanation on why we are
      not restoring callee-saved registers in BPF.  */
 
-  /* If we ever need to do anything else than just generating a return
-     instruction here, please mind the `naked' function attribute.  */
-
+  if (lookup_attribute ("naked", DECL_ATTRIBUTES (cfun->decl)) != NULL_TREE)
+    return;
   emit_jump_insn (gen_exit ());
 }
 
diff --git a/gcc/testsuite/gcc.target/bpf/naked-1.c b/gcc/testsuite/gcc.target/bpf/naked-1.c
index cbbc4c516976..dc8ac2619ccb 100644
--- a/gcc/testsuite/gcc.target/bpf/naked-1.c
+++ b/gcc/testsuite/gcc.target/bpf/naked-1.c
@@ -9,4 +9,3 @@ int __attribute__((naked)) foo()
   __asm__ volatile ("@ naked");
 }
 /* { dg-final { scan-assembler "\t@ naked" } } */
-/* { dg-final { scan-assembler "\texit\n" } } */
diff --git a/gcc/testsuite/gcc.target/bpf/naked-2.c b/gcc/testsuite/gcc.target/bpf/naked-2.c
new file mode 100644
index 000000000000..25aebf84755b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/naked-2.c
@@ -0,0 +1,10 @@
+/* Verify that __attribute__((naked)) produces functions without implicit
+   `exit' instructions in the epilogue.  */
+/* { dg-do compile } */
+/* { dg-options "-O0" } */
+
+int __attribute__((naked)) foo()
+{
+  __asm__ volatile ("exit");
+}
+/* { dg-final { scan-assembler-times "\texit" 1 } } */


More information about the Gcc-cvs mailing list