Emit .cfi_mte_memtag_frame after .cfi_startproc for each function where
the stack memory uses MTE protection.

TBD:
  - Need to double check if checking on frame_size is the right thing to
    do. clang generates cfi_mte_tagged_frame directive irrespective of
    whether the function ended up tagging the stack usage or not.

gcc/ChangeLog:

        * config/aarch64/aarch64.cc (aarch64_can_tag_addresses): Add
        forward declaration.
        (aarch64_post_cfi_startproc): Emit .cfi_mte_tagged_frame CFI
        directive.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/memtag/cfi-mte-memtag-frame-1.c: New test.

---
[New in RFC V2]
---
 gcc/config/aarch64/aarch64.cc                        |  6 ++++++
 .../aarch64/memtag/cfi-mte-memtag-frame-1.c          | 12 ++++++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 
gcc/testsuite/gcc.target/aarch64/memtag/cfi-mte-memtag-frame-1.c

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 4dee57eb255f..bf448895d1ab 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -25162,6 +25162,8 @@ aarch64_asm_output_external (FILE *stream, tree decl, 
const char* name)
   aarch64_asm_output_variant_pcs (stream, decl, name);
 }
 
+bool aarch64_can_tag_addresses (void);
+
 /* Triggered after a .cfi_startproc directive is emitted into the assembly 
file.
    Used to output the .cfi_b_key_frame directive when signing the current
    function with the B key.  */
@@ -25172,6 +25174,10 @@ aarch64_post_cfi_startproc (FILE *f, tree ignored 
ATTRIBUTE_UNUSED)
   if (cfun->machine->frame.laid_out && aarch64_return_address_signing_enabled 
()
       && aarch64_ra_sign_key == AARCH64_KEY_B)
        asm_fprintf (f, "\t.cfi_b_key_frame\n");
+  if (cfun->machine->frame.laid_out && aarch64_can_tag_addresses ()
+      && memtag_sanitize_p ()
+      && !known_eq (cfun->machine->frame.frame_size, 0))
+    asm_fprintf (f, "\t.cfi_mte_tagged_frame\n");
 }
 
 /* Implements TARGET_ASM_FILE_START.  Output the assembly header.  */
diff --git a/gcc/testsuite/gcc.target/aarch64/memtag/cfi-mte-memtag-frame-1.c 
b/gcc/testsuite/gcc.target/aarch64/memtag/cfi-mte-memtag-frame-1.c
new file mode 100644
index 000000000000..43ef5d238b58
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/memtag/cfi-mte-memtag-frame-1.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fsanitize=memtag -march=armv8.5-a+memtag" } */
+
+int use (int *x);
+
+void foo (int n)
+{
+  int x = 99;
+  use (&x);
+}
+
+/* { dg-final { scan-assembler-times ".cfi_mte_tagged_frame" 1 } } */
-- 
2.43.0

Reply via email to