public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Victor Do Nascimento <victorldn@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-6216] aarch64: Add front-end argument type checking for target builtins
Date: Wed,  6 Dec 2023 10:41:15 +0000 (GMT)	[thread overview]
Message-ID: <20231206104116.000D4385842B@sourceware.org> (raw)

https://gcc.gnu.org/g:5af697d72d79db7ad5713c18dea8f52cfc79612f

commit r14-6216-g5af697d72d79db7ad5713c18dea8f52cfc79612f
Author: Victor Do Nascimento <victor.donascimento@arm.com>
Date:   Mon Jul 10 20:16:07 2023 +0100

    aarch64: Add front-end argument type checking for target builtins
    
    In implementing the ACLE read/write system register builtins it was
    observed that leaving argument type checking to be done at expand-time
    meant that poorly-formed function calls were being "fixed" by certain
    optimization passes, meaning bad code wasn't being properly picked up
    in checking.
    
    Example:
    
      const char *regname = "amcgcr_el0";
      long long a = __builtin_aarch64_rsr64 (regname);
    
    is reduced by the ccp1 pass to
    
      long long a = __builtin_aarch64_rsr64 ("amcgcr_el0");
    
    As these functions require an argument of STRING_CST type, there needs
    to be a check carried out by the front-end capable of picking this up.
    
    The introduced `check_general_builtin_call' function will be called by
    the TARGET_CHECK_BUILTIN_CALL hook whenever a call to a builtin
    belonging to the AARCH64_BUILTIN_GENERAL category is encountered,
    carrying out any appropriate checks associated with a particular
    builtin function code.
    
    gcc/ChangeLog:
    
            * config/aarch64/aarch64-builtins.cc (aarch64_general_check_builtin_call):
            New.
            * config/aarch64/aarch64-c.cc (aarch64_check_builtin_call):
            Add `aarch64_general_check_builtin_call' call.
            * config/aarch64/aarch64-protos.h (aarch64_general_check_builtin_call):
            New.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/aarch64/acle/rwsr-3.c: New.

Diff:
---
 gcc/config/aarch64/aarch64-builtins.cc         | 31 ++++++++++++++++++++++++++
 gcc/config/aarch64/aarch64-c.cc                |  4 ++--
 gcc/config/aarch64/aarch64-protos.h            |  4 ++++
 gcc/testsuite/gcc.target/aarch64/acle/rwsr-3.c | 18 +++++++++++++++
 4 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/gcc/config/aarch64/aarch64-builtins.cc b/gcc/config/aarch64/aarch64-builtins.cc
index a004e065959..f780b1094f3 100644
--- a/gcc/config/aarch64/aarch64-builtins.cc
+++ b/gcc/config/aarch64/aarch64-builtins.cc
@@ -2127,6 +2127,37 @@ aarch64_general_builtin_decl (unsigned code, bool)
   return aarch64_builtin_decls[code];
 }
 
+bool
+aarch64_general_check_builtin_call (location_t location, vec<location_t>,
+			    unsigned int code, tree fndecl,
+			    unsigned int nargs ATTRIBUTE_UNUSED, tree *args)
+{
+  switch (code)
+    {
+    case AARCH64_RSR:
+    case AARCH64_RSRP:
+    case AARCH64_RSR64:
+    case AARCH64_RSRF:
+    case AARCH64_RSRF64:
+    case AARCH64_WSR:
+    case AARCH64_WSRP:
+    case AARCH64_WSR64:
+    case AARCH64_WSRF:
+    case AARCH64_WSRF64:
+      tree addr = STRIP_NOPS (args[0]);
+      if (TREE_CODE (TREE_TYPE (addr)) != POINTER_TYPE
+	  || TREE_CODE (addr) != ADDR_EXPR
+	  || TREE_CODE (TREE_OPERAND (addr, 0)) != STRING_CST)
+	{
+	  error_at (location, "first argument to %qD must be a string literal",
+		    fndecl);
+	  return false;
+	}
+    }
+  /* Default behavior.  */
+  return true;
+}
+
 typedef enum
 {
   SIMD_ARG_COPY_TO_REG,
diff --git a/gcc/config/aarch64/aarch64-c.cc b/gcc/config/aarch64/aarch64-c.cc
index dd4b32546c2..18422bb5663 100644
--- a/gcc/config/aarch64/aarch64-c.cc
+++ b/gcc/config/aarch64/aarch64-c.cc
@@ -394,8 +394,8 @@ aarch64_check_builtin_call (location_t loc, vec<location_t> arg_loc,
   switch (code & AARCH64_BUILTIN_CLASS)
     {
     case AARCH64_BUILTIN_GENERAL:
-      return true;
-
+      return aarch64_general_check_builtin_call (loc, arg_loc, subcode,
+						 orig_fndecl, nargs, args);
     case AARCH64_BUILTIN_SVE:
       return aarch64_sve::check_builtin_call (loc, arg_loc, subcode,
 					      orig_fndecl, nargs, args);
diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h
index 9e09cf7ee58..60ff61f6d54 100644
--- a/gcc/config/aarch64/aarch64-protos.h
+++ b/gcc/config/aarch64/aarch64-protos.h
@@ -1002,6 +1002,10 @@ tree aarch64_general_builtin_rsqrt (unsigned int);
 void handle_arm_acle_h (void);
 void handle_arm_neon_h (void);
 
+bool aarch64_general_check_builtin_call (location_t, vec<location_t>,
+					 unsigned int, tree, unsigned int,
+					 tree *);
+
 namespace aarch64_sve {
   void init_builtins ();
   void handle_arm_sve_h ();
diff --git a/gcc/testsuite/gcc.target/aarch64/acle/rwsr-3.c b/gcc/testsuite/gcc.target/aarch64/acle/rwsr-3.c
new file mode 100644
index 00000000000..aadd04a7af6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/acle/rwsr-3.c
@@ -0,0 +1,18 @@
+/* Test the __arm_[r,w]sr ACLE intrinsics family.  */
+/* Ensure that illegal behavior is rejected by the compiler.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -O3 -march=armv8-a" } */
+
+#include <arm_acle.h>
+
+void
+test_non_const_sysreg_name ()
+{
+  const char *regname = "trcseqstr";
+  long long a = __arm_rsr64 (regname); /* { dg-error "first argument to '__builtin_aarch64_rsr64' must be a string literal" } */
+  __arm_wsr64 (regname, a); /* { dg-error "first argument to '__builtin_aarch64_wsr64' must be a string literal" } */
+
+  long long b = __arm_rsr64 (nullptr); /* { dg-error "first argument to '__builtin_aarch64_rsr64' must be a string literal" } */
+  __arm_wsr64 (nullptr, b); /* { dg-error "first argument to '__builtin_aarch64_wsr64' must be a string literal" } */
+}

                 reply	other threads:[~2023-12-06 10:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20231206104116.000D4385842B@sourceware.org \
    --to=victorldn@gcc.gnu.org \
    --cc=gcc-cvs@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).