Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2569267ybh; Fri, 24 Jul 2020 16:53:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMB3v7TH4FaXKabNU1fjn+kNR+Kc1s1bJhtffxxGVuwVTkFbwBolGvW/3RzAh2AIsfB6Cu X-Received: by 2002:a50:cf09:: with SMTP id c9mr11549062edk.90.1595634792280; Fri, 24 Jul 2020 16:53:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595634792; cv=none; d=google.com; s=arc-20160816; b=b4XB3ipW8UHME8Y8Yrvkzh0RgXjVhM5XXH4UTCNBDX1fcbeZHfjGxXW9AEmrY/O78i 8bULaWkfbUp2k5eq9zf/BFr1d36lpBAJh9HpKf2hbg7JeJPFs6C9QPOfywUXmX0nXRLI UtoTHuwo+x3ZRfQ3KSFHw7TmM4B1P5UnKqJoCUQ2cZrkEdo9zNwvJtQdiYQFxCKzHoLF OzErTGIA8zFbAFHEQyAFRUEbFkURv0NoeM7nmSzEgfiO044LDLRVuZ4nfiJ2/21RBaON 3R8V7f9jTUvWWj06hSZpOZRXMGmwntFoNogezkAA6Wo0wcnI01O5Ih5x/37aLqlulDwG aXFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=4BCBDdYVKr2pnpemi8Z83oTWDFHWkw+Kw4e1jr8VSz4=; b=MFhqinIaN4//7taInZGtYRcncNRznHH5EebUW8LPGbd/AjwASs5ISnGO1Rcn+JWqxF IActrDr9UbrDKuh7/f/FCUGgn/dS2UKF/IoWIjvdmvqwQGs44MtZZ1ziXcqQDTW9FQ4b xpVPmjrfIkXmC1TSYf7SVIdR/RuMbwamJfwi5CEEr2p3El8LokdePwnDR/sgXhK308OX i6EIBTOVwqyr0LZeQhz0VxohAQZKEyFOg5SPFld0dkl/K43sutiOQNznSwouSEv6BCFz PJYJ4c0/V3F6iytIunKIyQF67tKBzzmyTerj9UZKTbyLQecuQRHWsy5Z2U1nEyZlr4YV F4QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WDEWNkl4; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f5si1390457edt.71.2020.07.24.16.52.48; Fri, 24 Jul 2020 16:53:12 -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=@google.com header.s=20161025 header.b=WDEWNkl4; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726618AbgGXXwj (ORCPT + 99 others); Fri, 24 Jul 2020 19:52:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726562AbgGXXwj (ORCPT ); Fri, 24 Jul 2020 19:52:39 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB3A0C0619E4 for ; Fri, 24 Jul 2020 16:52:38 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id k5so6378752pjg.3 for ; Fri, 24 Jul 2020 16:52:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=4BCBDdYVKr2pnpemi8Z83oTWDFHWkw+Kw4e1jr8VSz4=; b=WDEWNkl4UgKDRf66kEhfSPTTVBjajwdrDhk0t1E500+h+TkcHD2jVC0liY5AWQ0Ltc j7APQv1dcjXnazjID9Aqs3lbUp0bLur+WQEPKyufOdoaknMaIZBsZbXedKMIvjohucA8 2GAy2l+LW68Bk+B/v8lgY05phzsuXTbdiWSKiDVofLkeblKUS6kRYtIbdlLD2erHZOkr fY4/J5Mzvwz679dkm4+med0Jarx9srzN0kPLepch/EVHf7Nz7vnExvpV+ZDLkAE7TzN6 ykEdOcVUU/P4kd6IuEaDXa2h0Z97zU1kVmz0/djPeTZmZl/9BaFdRP/qY5TVeXPJ/BHk 2bMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=4BCBDdYVKr2pnpemi8Z83oTWDFHWkw+Kw4e1jr8VSz4=; b=Cy25oQ1QqBmldneO3nwLlINWuNMNqLnaZiBrkCKgNOxGeHfV03ZxNcE+sr+YZ6wwj2 xLksN2NK+Af6l2cNSyYsg1zIqyfNCixfHk3Ar5HC5hA7yXl7LRtT0phWXeftLxOl9slG dKgbzKmMKpc8VqTBu7vapzT+Mx51AWUOiqa2OIsHCyWs7EEtUK+RVGLa7rvx6EnJ51V3 Ea5k8KsCOBUgibkOJAK3fa/GPdiz6z2fHbRWWhSdkrIKrrzyMc5Ti7EbcLGbtc4wiGXz 38JJRdoXBsRsP/F+2L/ptrlMKRqxbu6OmfmCSbT5dJEMY1SZN4dd0OsxM0T9658i3Rbp RomQ== X-Gm-Message-State: AOAM532KzBakbfM2hWWRS/QXr9PPgxYswsbJUG/q6jsUIWmBwRvYbzNg sShFUX4zgjOO0MoxcyCeSJgCMWRynTtcI40xxkuUsxJAruA= X-Received: by 2002:a17:90a:21ef:: with SMTP id q102mr7975971pjc.101.1595634757961; Fri, 24 Jul 2020 16:52:37 -0700 (PDT) MIME-Version: 1.0 References: <20200724193551.2158677-1-nhuck@google.com> In-Reply-To: <20200724193551.2158677-1-nhuck@google.com> From: Nick Desaulniers Date: Fri, 24 Jul 2020 16:52:26 -0700 Message-ID: Subject: Re: [PATCH v6] Makefile: Add clang-tidy and static analyzer support to makefile To: Nathan Huckleberry Cc: Masahiro Yamada , Michal Marek , Linux Kbuild mailing list , LKML , clang-built-linux , Pirama Arumuga Nainar , Bill Wendling , Tom Roeder Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 24, 2020 at 12:38 PM 'Nathan Huckleberry' via Clang Built Linux wrote: > > This patch adds clang-tidy and the clang static-analyzer as make > targets. The goal of this patch is to make static analysis tools > usable and extendable by any developer or researcher who is familiar > with basic c++. > > The current static analysis tools require intimate knowledge of the > internal workings of the static analysis. Clang-tidy and the clang > static analyzers expose an easy to use api and allow users unfamiliar > with clang to write new checks with relative ease. > > ===Clang-tidy=== > > Clang-tidy is an easily extendable 'linter' that runs on the AST. > Clang-tidy checks are easy to write and understand. A check consists of > two parts, a matcher and a checker. The matcher is created using a > domain specific language that acts on the AST > (https://clang.llvm.org/docs/LibASTMatchersReference.html). When AST > nodes are found by the matcher a callback is made to the checker. The > checker can then execute additional checks and issue warnings. > > Here is an example clang-tidy check to report functions that have calls > to local_irq_disable without calls to local_irq_enable and vice-versa. > Functions flagged with __attribute((annotation("ignore_irq_balancing"))) > are ignored for analysis. (https://reviews.llvm.org/D65828) > > ===Clang static analyzer=== > > The clang static analyzer is a more powerful static analysis tool that > uses symbolic execution to find bugs. Currently there is a check that > looks for potential security bugs from invalid uses of kmalloc and > kfree. There are several more general purpose checks that are useful for > the kernel. > > The clang static analyzer is well documented and designed to be > extensible. > (https://clang-analyzer.llvm.org/checker_dev_manual.html) > (https://github.com/haoNoQ/clang-analyzer-guide/releases/download/v0.1/clang-analyzer-guide-v0.1.pdf) > > The main draw of the clang tools is how accessible they are. The clang > documentation is very nice and these tools are built specifically to be > easily extendable by any developer. They provide an accessible method of > bug-finding and research to people who are not overly familiar with the > kernel codebase. > > Signed-off-by: Nathan Huckleberry Thanks, patch LGTM now, but when I run clang-tidy for x86, I get a bunch of errors that make this look broken. Let's sort this out internally, next week? For aarch64 defconfig clang-tidy already observes 3 instances of linuxkernel-must-check-errs. Nice. I think the script that generates compile_commands.json doesn't limit itself to the current arch; if I don't do a bunch of `ARCH=x make clean` I get additional garbage in the analyses from garbage in the compile_commands.json. > --- > Changes v5->v6 > * Minor style fixes > MAINTAINERS | 1 + > Makefile | 3 + > scripts/clang-tools/Makefile.clang-tools | 23 ++++++ > .../{ => clang-tools}/gen_compile_commands.py | 0 > scripts/clang-tools/run-clang-tools.py | 77 +++++++++++++++++++ > 5 files changed, 104 insertions(+) > create mode 100644 scripts/clang-tools/Makefile.clang-tools > rename scripts/{ => clang-tools}/gen_compile_commands.py (100%) > create mode 100755 scripts/clang-tools/run-clang-tools.py > > diff --git a/MAINTAINERS b/MAINTAINERS > index 1d4aa7f942de..a444564e5572 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -4198,6 +4198,7 @@ W: https://clangbuiltlinux.github.io/ > B: https://github.com/ClangBuiltLinux/linux/issues > C: irc://chat.freenode.net/clangbuiltlinux > F: Documentation/kbuild/llvm.rst > +F: scripts/clang-tools/ > K: \b(?i:clang|llvm)\b > > CLEANCACHE API > diff --git a/Makefile b/Makefile > index fe0164a654c7..3e2df010b342 100644 > --- a/Makefile > +++ b/Makefile > @@ -747,6 +747,7 @@ KBUILD_CFLAGS += $(call cc-option,-fno-allow-store-data-races) > > include scripts/Makefile.kcov > include scripts/Makefile.gcc-plugins > +include scripts/clang-tools/Makefile.clang-tools > > ifdef CONFIG_READABLE_ASM > # Disable optimizations that make assembler listings hard to read. > @@ -1543,6 +1544,8 @@ help: > @echo ' export_report - List the usages of all exported symbols' > @echo ' headerdep - Detect inclusion cycles in headers' > @echo ' coccicheck - Check with Coccinelle' > + @echo ' clang-analyzer - Check with clang static analyzer' > + @echo ' clang-tidy - Check with clang-tidy' > @echo '' > @echo 'Tools:' > @echo ' nsdeps - Generate missing symbol namespace dependencies' > diff --git a/scripts/clang-tools/Makefile.clang-tools b/scripts/clang-tools/Makefile.clang-tools > new file mode 100644 > index 000000000000..5c9d76f77595 > --- /dev/null > +++ b/scripts/clang-tools/Makefile.clang-tools > @@ -0,0 +1,23 @@ > +# SPDX-License-Identifier: GPL-2.0 > +# > +# Copyright (C) Google LLC, 2020 > +# > +# Author: Nathan Huckleberry > +# > +PHONY += clang-tidy > +clang-tidy: > +ifdef CONFIG_CC_IS_CLANG > + $(PYTHON3) scripts/clang-tools/gen_compile_commands.py > + $(PYTHON3) scripts/clang-tools/run-clang-tools.py clang-tidy compile_commands.json > +else > + $(error clang-tidy requires CC=clang) > +endif > + > +PHONY += clang-analyzer > +clang-analyzer: > +ifdef CONFIG_CC_IS_CLANG > + $(PYTHON3) scripts/clang-tools/gen_compile_commands.py > + $(PYTHON3) scripts/clang-tools/run-clang-tools.py clang-analyzer compile_commands.json > +else > + $(error clang-analyzer requires CC=clang) > +endif > diff --git a/scripts/gen_compile_commands.py b/scripts/clang-tools/gen_compile_commands.py > similarity index 100% > rename from scripts/gen_compile_commands.py > rename to scripts/clang-tools/gen_compile_commands.py > diff --git a/scripts/clang-tools/run-clang-tools.py b/scripts/clang-tools/run-clang-tools.py > new file mode 100755 > index 000000000000..1f4cd706ec01 > --- /dev/null > +++ b/scripts/clang-tools/run-clang-tools.py > @@ -0,0 +1,77 @@ > +#!/usr/bin/env python > +# SPDX-License-Identifier: GPL-2.0 > +# > +# Copyright (C) Google LLC, 2020 > +# > +# Author: Nathan Huckleberry > +# > +"""A helper routine run clang-tidy and the clang static-analyzer on > +compile_commands.json. > +""" > + > +import argparse > +import json > +import logging > +import multiprocessing > +import os > +import subprocess > +import sys > + > + > +def parse_arguments(): > + """Set up and parses command-line arguments. > + Returns: > + args: Dict of parsed args > + Has keys: [file, type] > + """ > + usage = """Run clang-tidy or the clang static-analyzer on a > + compilation database.""" > + parser = argparse.ArgumentParser(description=usage) > + > + type_help = "Type of analysis to be performed" > + parser.add_argument("type", > + choices=["clang-tidy", "clang-analyzer"], > + help=type_help) > + file_path_help = "Path to the compilation database to parse" > + parser.add_argument("file", type=str, help=file_path_help) > + > + return parser.parse_args() > + > + > +def init(l, t): > + global lock > + global analysis_type > + lock = l > + analysis_type = t > + > + > +def run_analysis(entry): > + filename = entry["file"] > + # Disable all checks, then re-enable the ones we want > + checks = "-checks=-*," > + if analysis_type == "clang-tidy": > + checks += "linuxkernel-*" > + else: > + checks += "clang-analyzer-*" > + p = subprocess.run( > + ["clang-tidy", "-p", os.getcwd(), checks, filename], > + stdout=subprocess.PIPE, > + stderr=subprocess.STDOUT) > + with lock: > + sys.stderr.buffer.write(p.stdout) > + > + > +def main(): > + args = parse_arguments() > + filename = args.file > + > + lock = multiprocessing.Lock() > + pool = multiprocessing.Pool(initializer=init, initargs=(lock, args.type)) > + # Read JSON data into the datastore variable > + with open(filename, "r") as f: > + datastore = json.load(f) > + pool.map(run_analysis, datastore) > + > + > +if __name__ == "__main__": > + main() > -- > 2.28.0.rc0.142.g3c755180ce-goog > > -- > You received this message because you are subscribed to the Google Groups "Clang Built Linux" group. > To unsubscribe from this group and stop receiving emails from it, send an email to clang-built-linux+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20200724193551.2158677-1-nhuck%40google.com. -- Thanks, ~Nick Desaulniers