From patchwork Tue Jun 20 16:42:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9799957 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A8B7F60328 for ; Tue, 20 Jun 2017 16:43:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0D7626B41 for ; Tue, 20 Jun 2017 16:43:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A59D527FA6; Tue, 20 Jun 2017 16:43:13 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 497DC26B41 for ; Tue, 20 Jun 2017 16:43:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751598AbdFTQnC (ORCPT ); Tue, 20 Jun 2017 12:43:02 -0400 Received: from mail-qk0-f196.google.com ([209.85.220.196]:35840 "EHLO mail-qk0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751128AbdFTQnC (ORCPT ); Tue, 20 Jun 2017 12:43:02 -0400 Received: by mail-qk0-f196.google.com with SMTP id r62so11680287qkf.3 for ; Tue, 20 Jun 2017 09:43:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=3z8IaT3ka5bq80oFBt3FtOLsaEJUSFYPkovY2cP5pdk=; b=lRXIT+Q7uXF16SGfhF5h6ZmyTZbJ9l4efPGpMve9xp1ebylnlfFdDvV+Qhc9RNLmN8 EgZJQonI7wAY+94MYgkk0NqUsMd1Le9p2ffP9P5wCjCgdZUI/i0SPH4ogI+frIphkI5f lAVv5B1CKbQq7dYbPXZfuobdYKztr8wajaKZrb5UdZyNWtPrpU0itPUV+7pKjqzbXwyS /IhcDHhbsrU1GxjpGJvpfScGffjxYqlsMy/DyD8UFuXWm0dJx/Gar7SnyKYn5DSf74lF 4lpkppW7itPqN7TVbwNA3nU6q/VO/K6X4LstsopdQLAUq5RCAuC+t8IKnzzumhrj+rRT FkXA== 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; bh=3z8IaT3ka5bq80oFBt3FtOLsaEJUSFYPkovY2cP5pdk=; b=lhQRJ1Qzc8jZiHNtqADDfkP86oCJWAAlbm3SCK3g5C7STUxXxczk8Cui77Wns0FiuF jqd40l/DOh5cJxBnqGKS0Kx3BSKDpAGtNDSs5B927cjJEOTvbugmDUNVYXUsDNfc83PZ PiBCPtXgWtPQVARFe2HU29C6NWeB407fuufoTWo4MWF1X99zoRDaGdLaea8b7jHoGrey iUn5cKeUylFVC/kvGH0XJ0mo8FtbOvKVuS3zMHoQoPwxDJIvRAsLoWOM0fe7VuE63fLs l5na4JWboGbx0FL9WWwfgk5Ue4ra7TGSJ55DD5dH3n07hReEj88KnqMWZBL7bt0dU9YI swEw== X-Gm-Message-State: AKS2vOycWUQNZ7fWswP7/BcM/F7NBq23TG9JGeUgoCEr6NJIzxswVyv1 pxVwUY0L07Wjqjl8BHg= X-Received: by 10.80.221.75 with SMTP id u11mr21889253edk.134.1497976980929; Tue, 20 Jun 2017 09:43:00 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:837:f300:4131:d708:a975:bd4c]) by smtp.gmail.com with ESMTPSA id r28sm7805623edd.33.2017.06.20.09.43.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Jun 2017 09:43:00 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH] fix: __builtin_bswap{16,32,64}() constantness Date: Tue, 20 Jun 2017 18:42:53 +0200 Message-Id: <20170620164253.76292-1-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.13.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 When expanding those builtins, we directly excludes a non-constant arg (via the expression expansion cost) and the to retrieve the value we use const_expression_value(). But const_expression_value() is to be used for expressions that qualify as 'integer constant expression' as per the C standard. However we want to be able to use this builtin with all integer expressions having a known value at compile time, even those not qualified as true 'integer constant expression'. Fix this by using get_expression_value_silent() instead of const_expression_value(). Note: this bug was found by testing sparse on the git tree on ARM64. Signed-off-by: Luc Van Oostenryck --- builtin.c | 4 +++- validation/builtin-bswap-constant.c | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/builtin.c b/builtin.c index dbb81032b..4f2a106cb 100644 --- a/builtin.c +++ b/builtin.c @@ -179,13 +179,15 @@ static struct symbol_op choose_op = { /* The argument is constant and valid if the cost is zero */ static int expand_bswap(struct expression *expr, int cost) { + struct expression *arg; long long val; if (cost) return cost; /* the arguments number & type have already been checked */ - val = const_expression_value(first_expression(expr->args)); + arg = first_expression(expr->args); + val = get_expression_value_silent(arg); switch (expr->ctype->bit_size) { case 16: expr->value = __builtin_bswap16(val); break; case 32: expr->value = __builtin_bswap32(val); break; diff --git a/validation/builtin-bswap-constant.c b/validation/builtin-bswap-constant.c index 4560f67e9..18574d4c5 100644 --- a/validation/builtin-bswap-constant.c +++ b/validation/builtin-bswap-constant.c @@ -16,6 +16,12 @@ unsigned long long bswap64(void) return __builtin_bswap64(0x123456789abcdef0ULL); } +unsigned int half_constant(void); +unsigned int half_constant(void) +{ + int v = 0x12345678; + return __builtin_bswap32(v); +} /* * check-name: builtin-bswap-constant * check-command: test-linearize $file