From patchwork Sat Mar 2 17:24:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 10836691 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 88B021399 for ; Sat, 2 Mar 2019 17:25:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5ECA7288D8 for ; Sat, 2 Mar 2019 17:25:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4C75F2AFC7; Sat, 2 Mar 2019 17:25:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DF956288D8 for ; Sat, 2 Mar 2019 17:25:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726282AbfCBRZO (ORCPT ); Sat, 2 Mar 2019 12:25:14 -0500 Received: from mail-ed1-f68.google.com ([209.85.208.68]:38511 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726246AbfCBRZO (ORCPT ); Sat, 2 Mar 2019 12:25:14 -0500 Received: by mail-ed1-f68.google.com with SMTP id h58so932554edb.5 for ; Sat, 02 Mar 2019 09:25:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Kb6ENS8yHSdXDwFfbzr4qH5NSJyVL4cue8uLJ9n3Ep0=; b=JtD2fIsV/45Y4uJD8BlptT13ZXCYHL9wlQabtr8fM/L6E/+AEQpJpytjlsETP8T680 SJskjQNTfm8XdmEXl/vjcqnQ+s9rcP6shoFp+lwooeQOgTHwgjGxjmhHNyARsIpWAyYM johtXZ7dHC/I1f9zuo7Ob3hGNIvIZ2xlxyxx+TYWPWwZIB3iXvb7vho44MSlN3vDC9hk FTThrp8adRmgtI0MRGAkvn6oKfXJLHxOC9pzExzJF48zSC8CashAGwxbkYrZ7QNFC+gI s74zbi28v4lAbRUrxgnsDPiCMQD3WdnJcKCgH4LOPFJEoetMDskqiSGE1Ss8COpimUzq YcGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Kb6ENS8yHSdXDwFfbzr4qH5NSJyVL4cue8uLJ9n3Ep0=; b=M9BnnmVtiuX4OoD1LlbZX92YT8HzQf9NTjC1jM4tdvq9zBm31KGCEQ18wfMlQ5w/f3 vcks2wliua+NKxKtCQpUY3dy1cAW+uipkTpTk3227AjACS8giDROla/9hH+KDaOTdtLO kZI6yKa+xiAm+Tnj/S51YEWwzCJc2kMo/a2ofVlcIWvmov/2SoQBRT3ZDaBSVo9fyoMD 0fS8vNjSoAwPM4pRNZYYaq6itiouJPuPgpNwxOd65L8m2SadCMelhJRc2ggYNYeMPqP6 RgXE5Ci90CPOZgcb1dTeCvM1Op9BbF/38N/kdJUaF/dYr15cHxeF7lJcQRaSSwq32A9w x1+Q== X-Gm-Message-State: APjAAAUzCyncTUkvKhxrGdI9tdjs9nUh4S/vrhtVafRZ5S7Z3wDNe613 0aemrqAvhKrSiRIiEnLIpKzM1Soq X-Google-Smtp-Source: APXvYqwdj3baCp6yhfQr8S1biDBLe/F8obUNzeIYT7EqdsdBsU/rq7BTbReMUzpou5VIbtzlH3skNQ== X-Received: by 2002:a50:8bf9:: with SMTP id n54mr8705291edn.197.1551547512152; Sat, 02 Mar 2019 09:25:12 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:4034:3c00:118c:24c1:d65c:b76a]) by smtp.gmail.com with ESMTPSA id f6sm224575eja.33.2019.03.02.09.25.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 02 Mar 2019 09:25:11 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Luc Van Oostenryck Subject: [PATCH] expand: 'conservative' must not bypass valid simplifications Date: Sat, 2 Mar 2019 18:24:44 +0100 Message-Id: <20190302172444.44644-1-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Thomas Weißschuh During the expansion of shifts, the variable 'conservative' is used to inhibit any possible diagnostics (for example, because he needed information is if the expression is a constant or not). However, this must not inhibit the simplification of valid shift expressions. Unfortunately, by moving the validation inside check_shift_count(), this what was done by commit 0b73dee01 ("big-shift: move the check into check_shift_count()"). Found through a false positive VLA detected in the Linux kernel. The array size was computed through min() on a shifted constant value and sparse complained about it. Fix this by returning early (and thus avoiding possible diagnostics *and* simplification) if 'conservative' is set only if the shift count is invalid. Fixes: 0b73dee0171a15800d0a4ae6225b602bf8961599 Signed-off-by: Thomas Weißschuh Signed-off-by: Luc Van Oostenryck --- expand.c | 2 +- validation/simplify-binops.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 validation/simplify-binops.c diff --git a/expand.c b/expand.c index e8e50b080..7936e29cb 100644 --- a/expand.c +++ b/expand.c @@ -197,7 +197,7 @@ static int simplify_int_binop(struct expression *expr, struct symbol *ctype) return 0; r = right->value; if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) { - if (conservative) + if (conservative && left->type != EXPR_VALUE) return 0; check_shift_count(expr, right); } diff --git a/validation/simplify-binops.c b/validation/simplify-binops.c new file mode 100644 index 000000000..e464c37ac --- /dev/null +++ b/validation/simplify-binops.c @@ -0,0 +1,12 @@ +#define __is_constexpr(x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +static void test(int x) { + static int b[] = { + [__builtin_choose_expr(__is_constexpr(1 << 1), 1, x)] = 0, + }; +} + +/* + * check-name: simplify-binops + */