From patchwork Wed Jan 18 19:12:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1728422 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=BLVB/dA/; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NxwQ411wBz23fT for ; Thu, 19 Jan 2023 06:12:59 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E8D8385800C for ; Wed, 18 Jan 2023 19:12:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E8D8385800C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674069176; bh=sVbB+2hK4ZKK5KjB9H5daHz4VhUqaW0+Q+ZDDOupA1I=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BLVB/dA/E8tCIvjlDPQZ+qZutxtF3bQMNBdIndHjEueFon4nkMqkn0gBmEnNS2nwh E8XK7lQ/YXviE+i60qoxQQpQOhFK9h4P+8EI13pxx3rpZHxH0+Wya8I0lZJBxF1Vsb b43wXVxTVdxYdYU5qriRkRlXrWU3lf51QjatMD2o= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by sourceware.org (Postfix) with ESMTPS id E64053858D28 for ; Wed, 18 Jan 2023 19:12:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E64053858D28 Received: by mail-pj1-x1035.google.com with SMTP id a14-20020a17090a70ce00b00229a2f73c56so3423217pjm.3 for ; Wed, 18 Jan 2023 11:12:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sVbB+2hK4ZKK5KjB9H5daHz4VhUqaW0+Q+ZDDOupA1I=; b=RRf83f1BMmvWv2Dkaee7ZVwJZTYWb1qKatWEaidAhzMraZ3BeikCdhq2/KwQS2QZm4 ZpXbhXnW9pE3J4vn226izyxMXXzzpFZ+2V/0fA7GkDXRy+dra7LhqB4xduP+x24wQDwz Xd614R/+aADAOW8nQGvO+oyhHVgB5WKgfaJxcmbOOEXfDx0mgvRqXTDv+R6UmSyfZm1E 9hEYqtq1BeFxFfB4eKxAavC37JOuUnipRUe/ZK2/o5+WHHyD3uh+x4tO+2uPq5JHC0Zg 0sIj1NJz5nONVTFkciiC1XXTYlGQhwlYfmE5Fm2MwNsfy2h8dO8DCN6RoFWb5HS3t7R5 LkXQ== X-Gm-Message-State: AFqh2kqmBU9Sec9vjqWfXvVQuPyVpUWMtCRJQlEZDUq+oefrMK3QZwA/ ndVbAiJW9jBEpeq6/GZ2PFDPdA5vQK8= X-Google-Smtp-Source: AMrXdXvY1/ZqPohZA33RzV8yQiHvV4WKWozEMfOHHn3gDekgo8ZEIowDNXZDpf+Ad0sPESo9aQg3rw== X-Received: by 2002:a17:902:b484:b0:193:2d53:3fcd with SMTP id y4-20020a170902b48400b001932d533fcdmr7947837plr.17.1674069153936; Wed, 18 Jan 2023 11:12:33 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.58.35.172]) by smtp.gmail.com with ESMTPSA id x15-20020a170902ec8f00b001895d871c95sm23436873plg.70.2023.01.18.11.12.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 11:12:33 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 92888740147 for ; Wed, 18 Jan 2023 11:12:31 -0800 (PST) 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 Message-Id: <20230118191231.29839-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3023.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Gcc-patches" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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" } */ +}