From patchwork Mon Jun 27 19:05:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12897002 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3600BC433EF for ; Mon, 27 Jun 2022 19:05:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234713AbiF0TFw (ORCPT ); Mon, 27 Jun 2022 15:05:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232675AbiF0TFv (ORCPT ); Mon, 27 Jun 2022 15:05:51 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 370BF25C7 for ; Mon, 27 Jun 2022 12:05:50 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id ge10so21109930ejb.7 for ; Mon, 27 Jun 2022 12:05:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9k6J9hkLR2Zl8PfPRX09C48/fSb3eQ2WsroE1G/XK7Y=; b=jdI//FTt0jH+kOdIJwHvTUxGnhaaFPRnJDgOsxfWrglGGsLc4AlWyQA6+c0wBdrDf7 FTGaNJXMgjGStLwvScFEsHz1u3JLWzuSS/uZARTFX/Iu/nAlZkh/rrlZIFCZJzlZENFW jVWGvxjvLzjXMtmRSBOup+ztDXJ5dW6FrWGyY23INgqXABgHtgVuXlq3PgM03r9L1u5p MpgiTbboKQu4B5w/a15aaOYIdlnEWK4OCiVoWS1RT36AJDivz2Hl17eHPGuO1oOK5WT4 DA0EtvogtugqwLq4F+fucLWsbZ8LXLOQKTY0tJaRJ7FlGXmquO2MpB02SyPUhYsU9rJ3 d1Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9k6J9hkLR2Zl8PfPRX09C48/fSb3eQ2WsroE1G/XK7Y=; b=27N2gOfPMAJijgDtUrV0F6xsdZo36iMQb25KQPf4J1te24+d5SOY8GkK/bSK2Qewtq wAivrYRzdsBEC5ZhDCXln7WrTTmZJ3/62Hvpw9rDNdGt6sc/wKwL91w6GsuIBBTw1iJe lQTt64S64qaP7BLO0DG4xJr42TRaAKbd2fC8HnCdhVr7y0I0s+bO8gVtKmHA32r6HO6B dWkP4npFe1BVwl1OYp+DUmOBEzQ7y2n+n2eLKR13iOrHv3L25E/sugADJ9q53btnMsSi pkGMy4vBSya1s5VhKx4eyX2ydXJxClYIAQYFVvJ6N/wl2b97kRCUnK7erSx7NaNALblC /K7Q== X-Gm-Message-State: AJIora8NvH9EmDN2cSDKlzuFaC6Yr/aC9JL/9fMymaJSllQZWjmMgBpe DAoJOBFEnig19IM+90Pt7Fv49sjlgT0= X-Google-Smtp-Source: AGRyM1vYdAWa551Xa0odmRlBr78NGQ0cVWKRrUEztGHM3n3MzfV4SqcAthfqliU5wt6RtyRL1AofcQ== X-Received: by 2002:a17:906:79ca:b0:705:111f:12dc with SMTP id m10-20020a17090679ca00b00705111f12dcmr13794757ejo.602.1656356748759; Mon, 27 Jun 2022 12:05:48 -0700 (PDT) Received: from localhost.localdomain (239.125-180-91.adsl-dyn.isp.belgacom.be. [91.180.125.239]) by smtp.gmail.com with ESMTPSA id p24-20020a170906615800b00709343c0017sm5456776ejl.98.2022.06.27.12.05.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 12:05:48 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Bart Van Assche , Luc Van Oostenryck Subject: [PATCH 1/5] bitwise: add testcases Date: Mon, 27 Jun 2022 21:05:36 +0200 Message-Id: <20220627190540.13358-2-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> References: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Currently bitwise types only support bitwise operations (&, |, ^ and ~) and the constant 0 (since this value is invariant for all bitwise operations and endianness conversion). But the incoming series will relax this a little bit. So, add a few testcases for it. Signed-off-by: Luc Van Oostenryck --- validation/bitwise-cmp.c | 32 ++++++++++++++++++++++++++++++++ validation/bitwise-is-signed.c | 22 ++++++++++++++++++++++ validation/linear/bitwise-cmps.c | 18 ++++++++++++++++++ validation/linear/bitwise-cmpu.c | 18 ++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 validation/bitwise-cmp.c create mode 100644 validation/bitwise-is-signed.c create mode 100644 validation/linear/bitwise-cmps.c create mode 100644 validation/linear/bitwise-cmpu.c diff --git a/validation/bitwise-cmp.c b/validation/bitwise-cmp.c new file mode 100644 index 000000000000..ca12b5e51e8e --- /dev/null +++ b/validation/bitwise-cmp.c @@ -0,0 +1,32 @@ +#define M 0xffffffff + +typedef int __attribute__((bitwise)) b32; + +static int eq0(b32 x, b32 y) { return (x == 0); } +static int eqm(b32 x, b32 y) { return (x == M); } +static int eqx(b32 x, b32 y) { return (x == y); } + +static int ne0(b32 x, b32 y) { return (x != 0); } +static int nem(b32 x, b32 y) { return (x != M); } +static int nex(b32 x, b32 y) { return (x != y); } + +static int lt0(b32 x, b32 y) { return (x < 0); } +static int ltm(b32 x, b32 y) { return (x < M); } +static int ltx(b32 x, b32 y) { return (x < y); } + +static int lte0(b32 x, b32 y) { return (x <= 0); } +static int ltem(b32 x, b32 y) { return (x <= M); } +static int ltex(b32 x, b32 y) { return (x <= y); } + +static int gte0(b32 x, b32 y) { return (x >= 0); } +static int gtem(b32 x, b32 y) { return (x >= M); } +static int gtex(b32 x, b32 y) { return (x >= y); } + +static int gt0(b32 x, b32 y) { return (x > 0); } +static int gtm(b32 x, b32 y) { return (x > M); } +static int gtx(b32 x, b32 y) { return (x > y); } + +/* + * check-name: bitwise-cmp + * check-known-to-fail + */ diff --git a/validation/bitwise-is-signed.c b/validation/bitwise-is-signed.c new file mode 100644 index 000000000000..dd9c147173cd --- /dev/null +++ b/validation/bitwise-is-signed.c @@ -0,0 +1,22 @@ +#define __bitwise __attribute__((bitwise)) + +#define is_signed_type(type) (((type)-1) <= 0) + +typedef signed int __bitwise s; +typedef unsigned int __bitwise u; + +int fos(void); +int fou(void); + + +int fos(void) { return is_signed_type(s); } +int fou(void) { return !is_signed_type(u); } + +/* + * check-name: bitwise-is-signed + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/linear/bitwise-cmps.c b/validation/linear/bitwise-cmps.c new file mode 100644 index 000000000000..6122944a42c6 --- /dev/null +++ b/validation/linear/bitwise-cmps.c @@ -0,0 +1,18 @@ +typedef signed int __attribute__((bitwise)) bs32; + +static int ltu(bs32 x, bs32 y) { return (x < y); } +static int lteu(bs32 x, bs32 y) { return (x <= y); } +static int gteu(bs32 x, bs32 y) { return (x >= y); } +static int gtu(bs32 x, bs32 y) { return (x > y); } + +/* + * check-name: bitwise-cmps + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-excludes: setb\\. + * check-output-excludes: setbe\\. + * check-output-excludes: setae\\. + * check-output-excludes: seta\\. + */ diff --git a/validation/linear/bitwise-cmpu.c b/validation/linear/bitwise-cmpu.c new file mode 100644 index 000000000000..8932436a7764 --- /dev/null +++ b/validation/linear/bitwise-cmpu.c @@ -0,0 +1,18 @@ +typedef unsigned int __attribute__((bitwise)) bu32; + +static int ltu(bu32 x, bu32 y) { return (x < y); } +static int lteu(bu32 x, bu32 y) { return (x <= y); } +static int gteu(bu32 x, bu32 y) { return (x >= y); } +static int gtu(bu32 x, bu32 y) { return (x > y); } + +/* + * check-name: bitwise-cmpu + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-excludes: setlt\\. + * check-output-excludes: setlte\\. + * check-output-excludes: setgte\\. + * check-output-excludes: setgt\\. + */ From patchwork Mon Jun 27 19:05:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12897003 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27164C43334 for ; Mon, 27 Jun 2022 19:05:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237548AbiF0TFx (ORCPT ); Mon, 27 Jun 2022 15:05:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235066AbiF0TFw (ORCPT ); Mon, 27 Jun 2022 15:05:52 -0400 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C71C25CE for ; Mon, 27 Jun 2022 12:05:51 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id o9so14388086edt.12 for ; Mon, 27 Jun 2022 12:05:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=O0ltlf8Y5LDaNVwRLH9nm1tiZfoBbWKE4JiYzHKpQK4=; b=kBw8BUqUEYUzPahOiR8RPORp4URbQ7xey7B4eFIkWlBGaRqiWNqm8XvrO7GmADqAaC LNGoDbE6TFhDE6ADmnphubdfRifyNSOLs9eYknxfEwzaqTMtDtt8/ag3AZu9VQvsLn4k nzCJbbVkmM3XNN4uas4tRwZaS4v2ZVYeuQIwaRqGUc4brjMWQqDDVbm7L0FSjBvNUFsw nXza03JW2dOpYMW+iJ5U1Ch0PjqF+7B1fcu6URS/fGFopF2vQAzeg+GRZ2gBNcllQkwx k0mPE4jRYjXDN37VQYJoIKYXkHAl7B9TUIj5xuSwnwyCa0/GFWtDWyyLMlrU88YE8Voo 68vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=O0ltlf8Y5LDaNVwRLH9nm1tiZfoBbWKE4JiYzHKpQK4=; b=qWt0biKz4vHxAJEKzzgErU1p8K066ofJIqT7oYj2B/B0S4eT/tjhOaumYayE1KRmI5 OtqK9q5RrP8hD3RNI6xbGw83sfYUcSXbLatbc4QS3+xGGGNacwxEx7oRz0zjyON2vVSa UDY7uhlGLsJgtGs+IH9wUs2CXUbkpfiPoEmDxF8Oo/hTcyVMsjfX5v4YbFcDKyRktmP9 JkBipgQbRQAY5D8cwA/ty+cAwMl4MWcZQT7pAjNKqdo5uM6vba+nena5pEW45DSiHll2 KDtbp8MAIfcBN7vQg8MvwGbe1jduGp8nvjjUbNC5tg4qn+v+wi3p2c6dOoGNvNB7Pr5L yyJQ== X-Gm-Message-State: AJIora9GqrHTxMY3VPC4zXGXU2We9hNSXkBo0z9ByBiNRm/c7eScg+Nf X66dND5kPiMvn1dV3touJ54kU2dM6dQ= X-Google-Smtp-Source: AGRyM1skz9TSyel+NHHhZIUbVpCwIuPzpe/soU1iM54SyYLeaQxf7XFfqSwGd14Jh6/LvuYfcGDLwQ== X-Received: by 2002:a05:6402:430a:b0:435:8ec9:31ec with SMTP id m10-20020a056402430a00b004358ec931ecmr18528842edc.248.1656356749603; Mon, 27 Jun 2022 12:05:49 -0700 (PDT) Received: from localhost.localdomain (239.125-180-91.adsl-dyn.isp.belgacom.be. [91.180.125.239]) by smtp.gmail.com with ESMTPSA id p24-20020a170906615800b00709343c0017sm5456776ejl.98.2022.06.27.12.05.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 12:05:49 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Bart Van Assche , Luc Van Oostenryck Subject: [PATCH 2/5] bitwise: accept all ones as non-restricted value Date: Mon, 27 Jun 2022 21:05:37 +0200 Message-Id: <20220627190540.13358-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> References: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Currently, the only value bitwise types can act on is 0 because the this value is anyway invariant for all bitwise operations and endianness conversions. But, a bit-pattern of all ones has the same properties and is also very often used. So, accept all ones as a valid value for bitwise operations. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 2 +- validation/bitwise-cast.c | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/evaluate.c b/evaluate.c index 61f59ee3908e..bcbcdf1ef0cc 100644 --- a/evaluate.c +++ b/evaluate.c @@ -417,7 +417,7 @@ static int restricted_value(struct expression *v, struct symbol *type) { if (v->type != EXPR_VALUE) return 1; - if (v->value != 0) + if (v->value != 0 && v->value != bits_mask(type->bit_size)) return 1; return 0; } diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c index 0583461cb745..1075a3e9410c 100644 --- a/validation/bitwise-cast.c +++ b/validation/bitwise-cast.c @@ -35,6 +35,19 @@ static __be32 quuy(void) return (__attribute__((force)) __be32) 1730; } +/* Implicit casts of all ones, legal */ +static __be32 foo1(void) +{ + __be32 x = 0xffffffff; + return x; +} + +/* Explicit cast of all ones, legal */ +static __be32 bar1(void) +{ + return (__be32)0xffffffff; +} + /* * check-name: conversions to bitwise types * check-command: sparse -Wbitwise $file From patchwork Mon Jun 27 19:05:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12897004 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13DB5C433EF for ; Mon, 27 Jun 2022 19:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237751AbiF0TF7 (ORCPT ); Mon, 27 Jun 2022 15:05:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232675AbiF0TFx (ORCPT ); Mon, 27 Jun 2022 15:05:53 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF44225D1 for ; Mon, 27 Jun 2022 12:05:51 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id mf9so21236929ejb.0 for ; Mon, 27 Jun 2022 12:05:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JdyJgs7OQYfPWhdxx4pQzYYW/R/ijOX67FT24uUrsOg=; b=pdk0CbI9qZ6t/B/W6bLxWYF+VjKHURUtGnUqgUXpXfWYcJAcEGosri5fO7otyeBmNK LjeHVjwZRZ/syZeXL9clX8hgjPqVU1R8L3mRwfX2jf0thrMz+NSVB87blJggXpR8iPTr NFuEAvAS2dynSTYM9ZsP6flKGGGGmmIaeKD1bXDvnmIfYaVNa/4ISZI5C2PDmmFqvMuw vFiCKgpaekNYzO6gNOJuon7k4cdoQzdvwtxO2c/q7jx41UBxm5Deq1QLm4pwlpMLK9D6 379/xQNM9gRJFv79cPf3b2r4FKyZ3zlqLMhEATdy4wGmLlfRfX2/tpv4AMl9p4S5dDih mGWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JdyJgs7OQYfPWhdxx4pQzYYW/R/ijOX67FT24uUrsOg=; b=R75KpfugplVv5djTg9uF58uLC/WpEwCzTud+F0wTqgz1r80H1Eqj2qyj3aomW2RdlV iXVAvOMfabpqum/70FUj8nf/Ooo6n1CYDAmKEZIjnrDmb17PUjAd7bNl++GwDMp8tAJJ 8CBI990f2Rqyu2lDRoCF6OvxwVQTVSHYmBy2M0nGvEjO/iowhPjdx36jKJ57hrl+sGn0 M9m9DkHamG7c77NWip3Ixj0HOMmy8g2ux3N3Sm5/Wokjv4Vl50ZHzAThIP/+71gvQqRt 6IzQLrKL6O02yHAvXOw5zAT0BjOAtZ7Mrbw5wt+heRBBY2iXLkXU9kdsodV84efvioOh c3wg== X-Gm-Message-State: AJIora+VUebOJa1ZDrMHb/pReMgKT6jE3AdzM4bl6/e23GgwMKFTiDmk XSk3jHlo8+ufd2tozPKkbOp1eRV68FY= X-Google-Smtp-Source: AGRyM1u2nvcOU+q4OCROx1PPzWiUZ3Q+PvD6fNn9r5+2u5bbJYR/zqgYjW6F3cKwsHSxyWbcD57sZw== X-Received: by 2002:a17:906:b1c1:b0:726:b009:4b63 with SMTP id bv1-20020a170906b1c100b00726b0094b63mr4622191ejb.24.1656356750405; Mon, 27 Jun 2022 12:05:50 -0700 (PDT) Received: from localhost.localdomain (239.125-180-91.adsl-dyn.isp.belgacom.be. [91.180.125.239]) by smtp.gmail.com with ESMTPSA id p24-20020a170906615800b00709343c0017sm5456776ejl.98.2022.06.27.12.05.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 12:05:50 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Bart Van Assche , Luc Van Oostenryck Subject: [PATCH 3/5] bitwise: allow compares for bitwise types Date: Mon, 27 Jun 2022 21:05:38 +0200 Message-Id: <20220627190540.13358-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> References: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Currently, bitwise types are restricted to bitwise operations (&, |, ^ and ~) as well as equality comparisons. This patch makes the others comparisons valid for bitwise types too. Warning: This change make sense in the context of [1] but doesn't make sense for the 'main' bitwise types: __be32 and friends. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 4 ++++ validation/bitwise-cmp.c | 1 - validation/linear/bitwise-cmps.c | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/evaluate.c b/evaluate.c index bcbcdf1ef0cc..bb8c0caa905a 100644 --- a/evaluate.c +++ b/evaluate.c @@ -435,6 +435,10 @@ static int restricted_binop(int op, struct symbol *type) case '^': case '?': return 2; /* keep fouled */ + case '<': + case '>': + case SPECIAL_LTE: + case SPECIAL_GTE: case SPECIAL_EQUAL: case SPECIAL_NOTEQUAL: return 3; /* warn if fouled */ diff --git a/validation/bitwise-cmp.c b/validation/bitwise-cmp.c index ca12b5e51e8e..8c3e6894072d 100644 --- a/validation/bitwise-cmp.c +++ b/validation/bitwise-cmp.c @@ -28,5 +28,4 @@ static int gtx(b32 x, b32 y) { return (x > y); } /* * check-name: bitwise-cmp - * check-known-to-fail */ diff --git a/validation/linear/bitwise-cmps.c b/validation/linear/bitwise-cmps.c index 6122944a42c6..f83ab7fe47db 100644 --- a/validation/linear/bitwise-cmps.c +++ b/validation/linear/bitwise-cmps.c @@ -8,7 +8,6 @@ static int gtu(bs32 x, bs32 y) { return (x > y); } /* * check-name: bitwise-cmps * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-excludes: setb\\. From patchwork Mon Jun 27 19:05:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12897006 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 812AFCCA47E for ; Mon, 27 Jun 2022 19:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237721AbiF0TGA (ORCPT ); Mon, 27 Jun 2022 15:06:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235066AbiF0TF5 (ORCPT ); Mon, 27 Jun 2022 15:05:57 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A800325C7 for ; Mon, 27 Jun 2022 12:05:52 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id r18so6541642edb.9 for ; Mon, 27 Jun 2022 12:05:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=t3FlkA6KGyMSetsfDdWNAVCtVWa7Kh2+c3Hx2va4nkU=; b=DCio9e6WSIjJ8d6mbmqKUjJZw9Z3M+U6b/TFRN3+5skIH4IW+wEZADToJPfsK43/05 ntOKW6RjUVGTQZ7XIETzHOZPqFhLoNuKi2HypzSVHd4vlBueR55PlfS43RVATIYo+7dr sW42KdG6b5+h6Rjl9iZeV2rYQIffAIIurPxhivqZ3CvpsWgtbMIXty7VrXud+XHpYWoQ zPzCLW/cs4oyXuesui7N9dGJryA2suyhkuKOKWwnakgS/gDeGosCwUprKot4X91QapCJ lPcCr5xmyy8j9e80JUUbC9U+VRU4p7RtMdWya5498y0TQ8p7My+B5J+MAqbehTHut3Oo FmXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=t3FlkA6KGyMSetsfDdWNAVCtVWa7Kh2+c3Hx2va4nkU=; b=wxd4PXzE/J0oyVrSpVqCqgzRqhDxbd5CfWP3OcakzARr8awfUO0e4a0vjOrMNpcBEl 91BxZzAw+Euszm4xK3FnvkHJgpjVCJyGLQ1tSgrL2m2WWkug3H6hLItRcjF9QbVuc5fA Bg8GWfvZanMBHWqCRbpBoWXRnvvIq1gWB4EY+KsjD74CSBi681z8H4FZ6P4sQEFlhIb1 8HQgPr93zgqmt5P06eP6GchzSL9mO0IvTdAev9kQbktkDt2s/d4gTaynnxqR+lCtf0Rn I/pC/rBmyaNCYyFF5404bJGViqSXzsRPN82+2SN4aoSiXW/IA8ijkpnXc/fEA4/QiWpe hyOg== X-Gm-Message-State: AJIora85lsSqMB7GAmX5fDNtqtOzuiBSDTWKoyoEHUIksVY14FTpCbPg 5Ln5/d5Q9afQ+ITK7aUE3YGZJkPc82U= X-Google-Smtp-Source: AGRyM1u3MLVAeMypU3rrFb5O1LlVhhWu2SkVfJBKAiERKHvZINMHW+uTg69Nyu025NYqiF0+zdhf+A== X-Received: by 2002:a05:6402:2312:b0:437:69ec:adef with SMTP id l18-20020a056402231200b0043769ecadefmr1188133eda.366.1656356751318; Mon, 27 Jun 2022 12:05:51 -0700 (PDT) Received: from localhost.localdomain (239.125-180-91.adsl-dyn.isp.belgacom.be. [91.180.125.239]) by smtp.gmail.com with ESMTPSA id p24-20020a170906615800b00709343c0017sm5456776ejl.98.2022.06.27.12.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 12:05:50 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Bart Van Assche , Luc Van Oostenryck Subject: [PATCH 4/5] bitwise: do not remove the signedness of bitwise types Date: Mon, 27 Jun 2022 21:05:39 +0200 Message-Id: <20220627190540.13358-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> References: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org When bitwise types were added [1] the signedness modifiers were removed from them. More exactly, it was MOD_SPECIFIER which was removed. I suppose this was done because then MOD_SPECIFIER contained the signedness bits but also MOD_CHAR, MOD_LONG, ... and those had to be removed. But currently MOD_SPECIFIER contains only MOD_SIGNEDNESS and the signedness info can be useful for bitwise types too. So, do not removed anymore MOD_SPECIFIER from the bitwise types' modifiers. [1] commit 032f492af0ac ("[PATCH] __attribute__((bitwise))") Signed-off-by: Luc Van Oostenryck --- parse.c | 1 - show-parse.c | 2 +- validation/linear/bitwise-cmpu.c | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/parse.c b/parse.c index 3d6fef7cb011..14fe9e15448a 100644 --- a/parse.c +++ b/parse.c @@ -1586,7 +1586,6 @@ static struct token *declaration_specifiers(struct token *token, struct decl_sta } type = alloc_symbol(token->pos, SYM_BASETYPE); *type = *ctx->ctype.base_type; - type->ctype.modifiers &= ~MOD_SPECIFIER; type->ctype.base_type = ctx->ctype.base_type; type->type = SYM_RESTRICT; ctx->ctype.base_type = type; diff --git a/show-parse.c b/show-parse.c index e2fc18bb4b3d..2ab2479b8181 100644 --- a/show-parse.c +++ b/show-parse.c @@ -318,7 +318,7 @@ deeper: if (as) prepend(name, "%s ", show_as(as)); - if (sym && (sym->type == SYM_BASETYPE || sym->type == SYM_ENUM)) + if (sym && (sym->type == SYM_BASETYPE || sym->type == SYM_ENUM || sym->type == SYM_RESTRICT)) mod &= ~MOD_SPECIFIER; s = modifier_string(mod); len = strlen(s); diff --git a/validation/linear/bitwise-cmpu.c b/validation/linear/bitwise-cmpu.c index 8932436a7764..e151b9741fa3 100644 --- a/validation/linear/bitwise-cmpu.c +++ b/validation/linear/bitwise-cmpu.c @@ -8,7 +8,6 @@ static int gtu(bu32 x, bu32 y) { return (x > y); } /* * check-name: bitwise-cmpu * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-excludes: setlt\\. From patchwork Mon Jun 27 19:05:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12897007 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44D8DC433EF for ; Mon, 27 Jun 2022 19:06:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235066AbiF0TGB (ORCPT ); Mon, 27 Jun 2022 15:06:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237731AbiF0TF5 (ORCPT ); Mon, 27 Jun 2022 15:05:57 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BE2625CE for ; Mon, 27 Jun 2022 12:05:53 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id u15so21089618ejc.10 for ; Mon, 27 Jun 2022 12:05:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TR0QCYW1q5RLAc3Oo9msp5UqgHZzA89aYBB4rMPEmQs=; b=gmtagYc0QdlGmWBGix/L6frSpx0D86980PSJuoVtDgsiso94GnnfeO8wdBXyxZ7Pn4 DYeNzKKVMxfEHps485JwDYZT0/qJYO/iblhrDrOo0rRVDmFc1zHqsfZGPFKfQspa3+4L phzBisLrsxUYLcOLiTgI2SwkdtcVcVh2RmthI9z3jym6fDwwAJ4b+ujcGiH93uQYPBDv wOsWIYInNJ/bFdAjpOCOy8AVm0yBZcbfklZLPNLBEfNRTyddBOoieVL+7MyXIXhFrp1z 2i0umPfRow8VM4gM7iJJH5n3A+7MQaTftYWg9AYz4kpmjR30vWMzw9DKBu9W8uGAtKZA /qcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TR0QCYW1q5RLAc3Oo9msp5UqgHZzA89aYBB4rMPEmQs=; b=IRVdvIWk6t/HVjdLeWUYxMHtpfHBWRulGVew5X8xSVLSPAq4g1jP44E43qY4TFvlkE +TY9NA+M0ulB0nec7+umibaA4THdXPkMGNRYC2DBo8KX5OZYZbRoJeFvVlTM4BUqL7bD 5HDwUY+bupSxnUzmnkzMB10zyAdcTGQGODUpvF5ujBzhIGtvUvsXA8sIVJ3z5PejK9Ry 7FqMGp8hMcg+PNkzrn5LrIhf1+HXp+ZmAOWD7NbQVlqJks1NVwkxbl+cxoKgsPcHPQi8 js9n6E3m0jYoSYpuRIg0qFfCcaUgs94SY2lJyPZIRQ5xlCDKgWcG2Sa7SVicYi12xry3 WV0A== X-Gm-Message-State: AJIora8I7e3j9f96dZtWWRcddb1Sv9PWAyYCVCgUtklJEdV+S1CGvTAF zpgQzB5nW3U5rC3UY5RV6nIjv9xPhLM= X-Google-Smtp-Source: AGRyM1uEQnvvcvDjqOvZtQ3hhWkE2or1TJ7QKVnauLoAnHFhjcYGX/9Jx7Vrh3e9urJ5gSZFz4MwAA== X-Received: by 2002:a17:906:c10:b0:6f4:6c70:b00f with SMTP id s16-20020a1709060c1000b006f46c70b00fmr14109130ejf.660.1656356752036; Mon, 27 Jun 2022 12:05:52 -0700 (PDT) Received: from localhost.localdomain (239.125-180-91.adsl-dyn.isp.belgacom.be. [91.180.125.239]) by smtp.gmail.com with ESMTPSA id p24-20020a170906615800b00709343c0017sm5456776ejl.98.2022.06.27.12.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 12:05:51 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Bart Van Assche , Luc Van Oostenryck Subject: [PATCH 5/5] bitwise: early expansion of simple constants Date: Mon, 27 Jun 2022 21:05:40 +0200 Message-Id: <20220627190540.13358-6-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> References: <20220627190540.13358-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org C has only positive constants: -1 is really an expression, the unary '-' operator applied to the constant 1. '-1' as a constant value only exists after the expansion of constant expressions. This is rather unfortunate since it inhibits easy testing of such constants in the evaluation phase, like here for restricted_value(). So, expand expressions like +CTE, -CTE or ~CTE before calling restricted_value(). Signed-off-by: Luc Van Oostenryck --- evaluate.c | 48 +++++++++++++++++++++++++++++++++- validation/bitwise-cast.c | 13 +++++++++ validation/bitwise-is-signed.c | 1 - 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/evaluate.c b/evaluate.c index bb8c0caa905a..33cc85c8d40f 100644 --- a/evaluate.c +++ b/evaluate.c @@ -413,11 +413,56 @@ static struct symbol *bad_expr_type(struct expression *expr) return expr->ctype = &bad_ctype; } +static bool expand_simple_constant(struct expression *expr, struct symbol *type) +{ + unsigned long long val, mask; + struct expression *pre; + struct symbol *ctype; + unsigned size; + + if (expr->type != EXPR_PREOP) + return false; + pre = expr->unop; + if (pre->type != EXPR_VALUE) + return false; + + ctype = pre->ctype; + if (!ctype || ctype != type) + return false; + + size = ctype->bit_size; + if (!size) + return false; + + mask = sign_bit(size); + switch (expr->op) { + case '+': + val = pre->value; + break; + case '-': + val = pre->value; + if (val == mask && !(ctype->ctype.modifiers & MOD_UNSIGNED)) + return false; + val = -val; + break; + case '~': + val = pre->value; + val = ~val; + break; + default: + return false; + } + expr->op = 0; + expr->type = EXPR_VALUE; + expr->value = val & bits_mask(size); + return true; +} + static int restricted_value(struct expression *v, struct symbol *type) { if (v->type != EXPR_VALUE) return 1; - if (v->value != 0 && v->value != bits_mask(type->bit_size)) + if (v->value != 0 && v->value != bits_mask(v->ctype->bit_size)) return 1; return 0; } @@ -1919,6 +1964,7 @@ Normal: if (!(class & TYPE_FLOAT)) { ctype = integer_promotion(ctype); expr->unop = cast_to(expr->unop, ctype); + expand_simple_constant(expr, ctype); } else if (expr->op != '~') { /* no conversions needed */ } else { diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c index 1075a3e9410c..01af56c73751 100644 --- a/validation/bitwise-cast.c +++ b/validation/bitwise-cast.c @@ -48,6 +48,19 @@ static __be32 bar1(void) return (__be32)0xffffffff; } +/* Implicit casts of minus one, legal */ +static __be32 foom(void) +{ + __be32 x = -1; + return x; +} + +/* Explicit cast of minus one, legal */ +static __be32 barm(void) +{ + return (__be32)-1; +} + /* * check-name: conversions to bitwise types * check-command: sparse -Wbitwise $file diff --git a/validation/bitwise-is-signed.c b/validation/bitwise-is-signed.c index dd9c147173cd..99d16bd20f9b 100644 --- a/validation/bitwise-is-signed.c +++ b/validation/bitwise-is-signed.c @@ -15,7 +15,6 @@ int fou(void) { return !is_signed_type(u); } /* * check-name: bitwise-is-signed * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1