Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1080415pxf; Thu, 18 Mar 2021 20:38:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz05Oqy4Q7Y5C9Zb0R3WXGxcK/pk6QziZrbgc2AJghZuTsYsCnCJ7m5in23IZWZL7bBObRJ X-Received: by 2002:a17:906:2612:: with SMTP id h18mr1981188ejc.309.1616125097917; Thu, 18 Mar 2021 20:38:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616125097; cv=none; d=google.com; s=arc-20160816; b=r+KnF1M15JeqLV91pPlelRamrWjfA4LLn+VcPiFl3zCbxwYGDiXmKB9uRA7sNRqGqH tvztDtOXkYLwi6LxU843ANPiNGiGN0EINqszNLQu8+g0eYcEM10tjVq0OdOPwNCcM+iA e1lOG4EnQxY1EStoKbRd8zzzc9Pctp96F11YgXQ5JgDy9xjuj1XWhmqAYNKeI+Gglb/B BhKFsPxPHPLZ0IaU5ejj2TTTrMRTZHRkHqpMieTGsf3zsVJ7+88GYBISC1ybuvDdN1HW IHZA3O0Wv1s/rj0suUSMLl776rzW/VMVzCAKV4bJI9h2LGcDaHYiMv5TlyCRUIQQg+rQ m6sQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=YThJXKoad88QyIUzJQjgHEKYHEHZlB/t9+1Xws9BWbo=; b=lAzMnnbQiPzB+WjL5BS2Dh71dYvwrCp1XnSOr/IwSQv4Nuo9ZvyVQurplZRcWrY3Jg 1FVX1VNLkGgH/ww9xTYryvh83diSy7PrdKaLr25a1s7IuN6I/uzsnZ1A4QUceOFo9eZB 3+uXIFm8r9kumKSG9pOy1Enk5QLABpuvBbnVc4Ope0U8m9/WqzhBNuBs6QPpkQ/Ft0Ka ivMdqLdnpmlfkzJuUpzv8BQVshbohjmj92fGBrvsVolPQ9ZWyfFnGzqPzqONQYoDBXTB SqjdNz1Gr3kUvs5MjM6KZbPrDskUqBh8SAw0f4LdU7kRsgTDMQTyt2akp5PbU0xb9juQ P8Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="VD/B9cBb"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b26si3221179eja.162.2021.03.18.20.37.55; Thu, 18 Mar 2021 20:38:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="VD/B9cBb"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233526AbhCSDg5 (ORCPT + 99 others); Thu, 18 Mar 2021 23:36:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233527AbhCSDgl (ORCPT ); Thu, 18 Mar 2021 23:36:41 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20BCBC06174A for ; Thu, 18 Mar 2021 20:36:41 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id l123so4927835pfl.8 for ; Thu, 18 Mar 2021 20:36:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=YThJXKoad88QyIUzJQjgHEKYHEHZlB/t9+1Xws9BWbo=; b=VD/B9cBb2K0S5lmYwaCJosnP9TGGztKB/20JQ/fkjVjsoPdByO4kUXUEqrDflhCX+C HPiW2HJantqBOkGpty+EeKd1wDi1qXyxhree4Xy3osahOFJDq2flVQZcRIg5vZKsnMI9 Folr58pqKFC1vWRJZan8ui7JUGvSXLHCgcRVA= 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=YThJXKoad88QyIUzJQjgHEKYHEHZlB/t9+1Xws9BWbo=; b=IWGjsY98poY4hLVeyNouA//m7SJbESnxVfZJJkTN9t7udZiFEwLmto8shUzpNl/erX ra298jVP3AWbytKGa+MIxGOP0nG0mc331BrkaiuI4E/EGeM03RxpZR8oq8fasNBxKmi4 WlMD6xU5cPSRcpqxCpLbQIjHuUEtZ6R9MUyLWhonZTzqfFuVlrM6wjsWz8h606BVXGU9 xE7hjmKBEj6HwaE99u9PHt6CQSGZUvwUSPG/gvd1aiiJp6xu+RyvSygLKqpemiMmJV7a zOi0TPXpWv1MO2rwAVm455rtJtd4B3E0zwM8MJohn7HCfZWXic5lE25byY5znSnaJEkz m4Sw== X-Gm-Message-State: AOAM533b5xnoYFVJvmW3e3HO66A8fm8ggophqTPjWS0w/yPWQgAgNJTM eKFAj8PkanHSuNmS/ni21XGZmg== X-Received: by 2002:a62:5b43:0:b029:1ef:21ad:846 with SMTP id p64-20020a625b430000b02901ef21ad0846mr7206410pfb.51.1616125000617; Thu, 18 Mar 2021 20:36:40 -0700 (PDT) Received: from evanbenn1.syd.corp.google.com ([2401:fa00:9:15:d0d6:1466:f005:1b0a]) by smtp.gmail.com with ESMTPSA id z2sm3909741pfq.198.2021.03.18.20.36.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Mar 2021 20:36:40 -0700 (PDT) From: Evan Benn To: Julia Lawall Cc: Evan Benn , Gilles Muller , Michal Marek , Nicolas Palix , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [PATCH] scripts/coccinelle: Add script to detect sign extension Date: Fri, 19 Mar 2021 14:36:15 +1100 Message-Id: <20210319142952.1.I2c82789dba4e68d61595862188e8bf4d31a05d38@changeid> X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, I am attempting to create a coccinelle script that will detect possibly buggy usage of the bitwise operators where integer promotion may result in bugs, usually due to sign extension. I know this script needs a lot more work, but I am just beginning to learn the syntax of coccinelle. At this stage I am mainly looking for advice if this is even worth continuing, or if I am on the wrong track entirely. Here is an example of the bug I hope to find: https://lore.kernel.org/lkml/20210317013758.GA134033@roeck-us.net/ Where ints and unsigned are mixed in bitwise operations, and the sizes differ. Thanks Evan Benn Signed-off-by: Evan Benn --- .../coccinelle/tests/int_sign_extend.cocci | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 scripts/coccinelle/tests/int_sign_extend.cocci diff --git a/scripts/coccinelle/tests/int_sign_extend.cocci b/scripts/coccinelle/tests/int_sign_extend.cocci new file mode 100644 index 000000000000..bad61e37e4e7 --- /dev/null +++ b/scripts/coccinelle/tests/int_sign_extend.cocci @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// Mixing signed and unsigned types in bitwise operations risks problems when +/// the 'Usual arithmetic conversions' are applied. +/// For example: +/// https://lore.kernel.org/lkml/20210317013758.GA134033@roeck-us.net/ +/// When a signed int and an unsigned int are compared there is no problem. +/// But if the unsigned is changed to a unsigned long, for example by using BIT +/// the signed value will be sign-extended and could result in incorrect logic. +// Confidence: +// Copyright: (C) 2021 Evan Benn +// Comments: +// Options: + +virtual context +virtual org +virtual report + +@r@ +position p; +{int} s; +{unsigned long} u; +@@ + s@p & u + +@script:python depends on org@ +p << r.p; +@@ + +cocci.print_main("sign extension when comparing bits of signed and unsigned values", p) + +@script:python depends on report@ +p << r.p; +@@ + +coccilib.report.print_report(p[0],"sign extension when comparing bits of signed and unsigned values") -- 2.31.0.291.g576ba9dcdaf-goog