public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] x86: Check invalid third argument to __builtin_ia32_prefetch
Date: Wed, 18 Jan 2023 11:12:31 -0800	[thread overview]
Message-ID: <20230118191231.29839-1-hjl.tools@gmail.com> (raw)

Check invalid third argument to __builtin_ia32_prefetch when expaning
__builtin_ia32_prefetch to avoid ICE later.

gcc/

	PR target/108436
	* config/i386/i386-expand.cc (ix86_expand_builtin): Check
	invalid third argument to __builtin_ia32_prefetch.

gcc/testsuite/

	* gcc.target/i386/pr108436.c: New test.
---
 gcc/config/i386/i386-expand.cc           | 12 ++++++++++++
 gcc/testsuite/gcc.target/i386/pr108436.c | 15 +++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr108436.c

diff --git a/gcc/config/i386/i386-expand.cc b/gcc/config/i386/i386-expand.cc
index 54f700cd09d..e2e2d28bb47 100644
--- a/gcc/config/i386/i386-expand.cc
+++ b/gcc/config/i386/i386-expand.cc
@@ -13175,6 +13175,12 @@ ix86_expand_builtin (tree exp, rtx target, rtx subtarget,
 
 	if (INTVAL (op3) == 1)
 	  {
+	    if (INTVAL (op2) < 2 || INTVAL (op2) > 3)
+	      {
+		error ("invalid third argument");
+		return const0_rtx;
+	      }
+
 	    if (TARGET_64BIT && TARGET_PREFETCHI
 		&& local_func_symbolic_operand (op0, GET_MODE (op0)))
 	      emit_insn (gen_prefetchi (op0, op2));
@@ -13195,6 +13201,12 @@ ix86_expand_builtin (tree exp, rtx target, rtx subtarget,
 		op0 = copy_addr_to_reg (op0);
 	      }
 
+	    if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
+	      {
+		warning (0, "invalid third argument to %<__builtin_ia32_prefetch%>; using zero");
+		op2 = const0_rtx;
+	      }
+
 	    if (TARGET_3DNOW || TARGET_PREFETCH_SSE
 		|| TARGET_PRFCHW || TARGET_PREFETCHWT1)
 	      emit_insn (gen_prefetch (op0, op1, op2));
diff --git a/gcc/testsuite/gcc.target/i386/pr108436.c b/gcc/testsuite/gcc.target/i386/pr108436.c
new file mode 100644
index 00000000000..d51f25863a5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr108436.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-mprefetchi" } */
+
+int
+foo (int a)
+{
+  return a + 1;
+}
+
+void
+bad (int *p)
+{
+  __builtin_ia32_prefetch (p, 0, 4, 0);   /* { dg-warning "invalid third argument to '__builtin_ia32_prefetch'; using zero" } */
+  __builtin_ia32_prefetch (foo, 0, 4, 1);   /* { dg-error "invalid third argument" } */
+}
-- 
2.39.0


             reply	other threads:[~2023-01-18 19:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-18 19:12 H.J. Lu [this message]
2023-01-19  3:43 ` Hongtao Liu

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=20230118191231.29839-1-hjl.tools@gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=gcc-patches@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).