Commit Graph

6 Commits

Author SHA1 Message Date
Joel Slebodnick 5ac12bed5e gen_compile_commands: handle multiple lines per .mod file
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2160559
Tested: Kernel booted on Orin board with no warnings using device tree

commit a4ab14e1d8fe83cc1ed8910b788117ec2ed25179
Author: John Hubbard <jhubbard@nvidia.com>
Date:   Mon Jun 27 18:23:53 2022 -0700

    gen_compile_commands: handle multiple lines per .mod file

    scripts/clang-tools/gen_compile_commands.py incorrectly assumes that
    each .mod file only contains one line. That assumption was correct when
    the script was originally created, but commit 9413e7640564 ("kbuild:
    split the second line of *.mod into *.usyms") changed the .mod file
    format so that there is one entry per line, and potentially many lines.

    The problem can be reproduced by using Kbuild to generate
    compile_commands.json, like this:

        make CC=clang compile_commands.json

    In many cases, the problem might be overlooked because many subsystems
    only have one line anyway. However, in some subsystems (Nouveau, with
    762 entries, is a notable example) it results in skipping most of the
    subsystem.

    Fix this by fully processing each .mod file.

    Fixes: 9413e7640564 ("kbuild: split the second line of *.mod into *.usyms")
    Signed-off-by: John Hubbard <jhubbard@nvidia.com>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    (cherry picked from commit a4ab14e1d8fe83cc1ed8910b788117ec2ed25179)

Signed-off-by: Joel Slebodnick <jslebodn@redhat.com>
2023-03-24 17:06:54 -04:00
Íñigo Huguet b3d200ae43 gen_compile_commands: fix missing 'sys' package
Bugzilla: https://bugzilla.redhat.com/2136526

commit ec783c7cb2495c5a3b8ca10db8056d43c528f940
Author: Kortan <kortanzh@gmail.com>
Date:   Wed Sep 8 11:28:48 2021 +0800

    gen_compile_commands: fix missing 'sys' package
    
    We need to import the 'sys' package since the script has called
    sys.exit() method.
    
    Fixes: 6ad7cbc015 ("Makefile: Add clang-tidy and static analyzer support to makefile")
    Signed-off-by: Kortan <kortanzh@gmail.com>
    Reviewed-by: Nathan Chancellor <nathan@kernel.org>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Íñigo Huguet <ihuguet@redhat.com>
2022-10-20 16:21:43 +02:00
Íñigo Huguet 2bf1096055 gen_compile_commands: extract compiler command from a series of commands
Bugzilla: https://bugzilla.redhat.com/2136526

commit 265264b814c2121513eab2e1cffe196502af0961
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Aug 19 09:57:33 2021 +0900

    gen_compile_commands: extract compiler command from a series of commands
    
    The current gen_compile_commands.py assumes that objects are always
    built by a single command.
    
    It makes sense to support cases where objects are built by a series of
    commands:
    
      cmd_<object> := <command1> ; <command2>
    
    One use-case is that <command1> is a compiler command, and <command2>
    an objtool command.
    
    It allows *.cmd files to contain an objtool command so that any change
    in it triggers object rebuilds.
    
    If ; appears after the C source file, take the first command.
    
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Kees Cook <keescook@chromium.org>

Signed-off-by: Íñigo Huguet <ihuguet@redhat.com>
2022-10-20 16:21:43 +02:00
Masahiro Yamada 585d32f9b0 gen_compile_commands: prune some directories
If directories are passed to gen_compile_commands.py, os.walk() traverses
all the subdirectories to search for .cmd files, but we know some of them
are not worth traversing.

Use the 'topdown' parameter of os.walk to prune them.

Documentation about the 'topdown' option of os.walk:
  When topdown is True, the caller can modify the dirnames list
  in-place (perhaps using del or slice assignment), and walk() will
  only recurse into the subdirectories whose names remain in dirnames;
  this can be used to prune the search, impose a specific order of
  visiting, or even to inform walk() about directories the caller
  creates or renames before it resumes walk() again. Modifying
  dirnames when topdown is False has no effect on the behavior of
  the walk, because in bottom-up mode the directories in dirnames
  are generated before dirpath itself is generated.

This commit prunes four directories, .git, Documentation, include, and
tools.

The first three do not contain any C files, so skipping them makes this
script work slightly faster. My main motivation is the last one, tools/
directory.

Commit 6ca4c6d259 ("gen_compile_commands: do not support .cmd files
under tools/ directory") stopped supporting the tools/ directory.
The current code no longer picks up .cmd files from the tools/
directory.

If you run:

  ./scripts/clang-tools/gen_compile_commands.py --log_level=INFO

then, you will see several "File ... not found" log messages.

This is expected, and I do not want to support the tools/ directory.
However, without an explicit comment "do not support tools/", somebody
might try to get it back. Clarify this.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Nathan Chancellor <nathan@kernel.org>
2021-02-16 22:23:56 +09:00
Masahiro Yamada 074075aea2 scripts/clang-tools: switch explicitly to Python 3
For the same reason as commit 51839e29cb ("scripts: switch explicitly
to Python 3"), switch some more scripts, which I tested and confirmed
working on Python 3.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Nathan Chancellor <nathan@kernel.org>
2021-02-03 09:01:08 +09:00
Nathan Huckleberry 6ad7cbc015 Makefile: Add clang-tidy and static analyzer support to makefile
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 <nhuck@google.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
2020-08-27 00:44:33 +09:00