From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id 7135F3858CDA; Thu, 19 Jan 2023 21:44:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7135F3858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674164674; bh=nhDNW94rdidCvtJRR4i3mLZkelDBMWs5Xxh4otkj4I0=; h=From:To:Subject:Date:From; b=INR3t7Cq3krf6YeGuWTjqMCIxb2ya+SNMpRcrNZCVx1DMVhM3riQKel1uk2I++td2 baOzk+CdSWT9O1iI6KSH7w2dVPAtoqhY7b3j2MxEs7bQEBY0SvIOzTt6ics4Mgp7fD Izc79//159hg9njOkYzZIk1Y4wwrEyPmN9NAjkq8= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: H.J. Lu To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5263] x86: Check invalid third argument to __builtin_ia32_prefetch X-Act-Checkin: gcc X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: 46644ec99cb355845b23bb1d02775c057ed8ee88 X-Git-Newrev: 77a67e3a9294c825ac1a2b205fbb266e7c29e82b Message-Id: <20230119214434.7135F3858CDA@sourceware.org> Date: Thu, 19 Jan 2023 21:44:34 +0000 (GMT) List-Id: https://gcc.gnu.org/g:77a67e3a9294c825ac1a2b205fbb266e7c29e82b commit r13-5263-g77a67e3a9294c825ac1a2b205fbb266e7c29e82b Author: H.J. Lu Date: Wed Jan 18 11:08:14 2023 -0800 x86: Check invalid third argument to __builtin_ia32_prefetch 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. Diff: --- gcc/config/i386/i386-expand.cc | 12 ++++++++++++ gcc/testsuite/gcc.target/i386/pr108436.c | 15 +++++++++++++++ 2 files changed, 27 insertions(+) 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" } */ +}