2021-02-02 14:06:04 +08:00
|
|
|
#!/usr/bin/env python3
|
2020-08-22 22:56:18 +08:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
#
|
|
|
|
# Copyright (C) Google LLC, 2020
|
|
|
|
#
|
|
|
|
# Author: Nathan Huckleberry <nhuck@google.com>
|
|
|
|
#
|
|
|
|
"""A helper routine run clang-tidy and the clang static-analyzer on
|
|
|
|
compile_commands.json.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import multiprocessing
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments():
|
|
|
|
"""Set up and parses command-line arguments.
|
|
|
|
Returns:
|
|
|
|
args: Dict of parsed args
|
|
|
|
Has keys: [path, 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)
|
|
|
|
path_help = "Path to the compilation database to parse"
|
|
|
|
parser.add_argument("path", type=str, help=path_help)
|
|
|
|
|
2023-10-10 02:39:04 +08:00
|
|
|
checks_help = "Checks to pass to the analysis"
|
|
|
|
parser.add_argument("-checks", type=str, default=None, help=checks_help)
|
|
|
|
header_filter_help = "Pass the -header-filter value to the tool"
|
|
|
|
parser.add_argument("-header-filter", type=str, default=None, help=header_filter_help)
|
|
|
|
|
2020-08-22 22:56:18 +08:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def init(l, a):
|
|
|
|
global lock
|
|
|
|
global args
|
|
|
|
lock = l
|
|
|
|
args = a
|
|
|
|
|
|
|
|
|
|
|
|
def run_analysis(entry):
|
|
|
|
# Disable all checks, then re-enable the ones we want
|
2023-10-10 02:39:04 +08:00
|
|
|
global args
|
|
|
|
checks = None
|
|
|
|
if args.checks:
|
|
|
|
checks = args.checks.split(',')
|
2020-08-22 22:56:18 +08:00
|
|
|
else:
|
2023-10-10 02:39:04 +08:00
|
|
|
checks = ["-*"]
|
|
|
|
if args.type == "clang-tidy":
|
|
|
|
checks.append("linuxkernel-*")
|
|
|
|
else:
|
|
|
|
checks.append("clang-analyzer-*")
|
|
|
|
checks.append("-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling")
|
|
|
|
file = entry["file"]
|
|
|
|
if not file.endswith(".c") and not file.endswith(".cpp"):
|
|
|
|
with lock:
|
|
|
|
print(f"Skipping non-C file: '{file}'", file=sys.stderr)
|
|
|
|
return
|
|
|
|
pargs = ["clang-tidy", "-p", args.path, "-checks=" + ",".join(checks)]
|
|
|
|
if args.header_filter:
|
|
|
|
pargs.append("-header-filter=" + args.header_filter)
|
|
|
|
pargs.append(file)
|
|
|
|
p = subprocess.run(pargs,
|
2020-08-22 22:56:18 +08:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
cwd=entry["directory"])
|
|
|
|
with lock:
|
|
|
|
sys.stderr.buffer.write(p.stdout)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
scripts: handle BrokenPipeError for python scripts
In the follow-up of commit fb3041d61f68 ("kbuild: fix SIGPIPE error
message for AR=gcc-ar and AR=llvm-ar"), Kees Cook pointed out that
tools should _not_ catch their own SIGPIPEs [1] [2].
Based on his feedback, LLVM was fixed [3].
However, Python's default behavior is to show noisy bracktrace when
SIGPIPE is sent. So, scripts written in Python are basically in the
same situation as the buggy llvm tools.
Example:
$ make -s allnoconfig
$ make -s allmodconfig
$ scripts/diffconfig .config.old .config | head -n1
-ALIX n
Traceback (most recent call last):
File "/home/masahiro/linux/scripts/diffconfig", line 132, in <module>
main()
File "/home/masahiro/linux/scripts/diffconfig", line 130, in main
print_config("+", config, None, b[config])
File "/home/masahiro/linux/scripts/diffconfig", line 64, in print_config
print("+%s %s" % (config, new_value))
BrokenPipeError: [Errno 32] Broken pipe
Python documentation [4] notes how to make scripts die immediately and
silently:
"""
Piping output of your program to tools like head(1) will cause a
SIGPIPE signal to be sent to your process when the receiver of its
standard output closes early. This results in an exception like
BrokenPipeError: [Errno 32] Broken pipe. To handle this case,
wrap your entry point to catch this exception as follows:
import os
import sys
def main():
try:
# simulate large output (your code replaces this loop)
for x in range(10000):
print("y")
# flush output here to force SIGPIPE to be triggered
# while inside this try block.
sys.stdout.flush()
except BrokenPipeError:
# Python flushes standard streams on exit; redirect remaining output
# to devnull to avoid another BrokenPipeError at shutdown
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1) # Python exits with error code 1 on EPIPE
if __name__ == '__main__':
main()
Do not set SIGPIPE’s disposition to SIG_DFL in order to avoid
BrokenPipeError. Doing that would cause your program to exit
unexpectedly whenever any socket connection is interrupted while
your program is still writing to it.
"""
Currently, tools/perf/scripts/python/intel-pt-events.py seems to be the
only script that fixes the issue that way.
tools/perf/scripts/python/compaction-times.py uses another approach
signal.signal(signal.SIGPIPE, signal.SIG_DFL) but the Python
documentation clearly says "Don't do it".
I cannot fix all Python scripts since there are so many.
I fixed some in the scripts/ directory.
[1]: https://lore.kernel.org/all/202211161056.1B9611A@keescook/
[2]: https://github.com/llvm/llvm-project/issues/59037
[3]: https://github.com/llvm/llvm-project/commit/4787efa38066adb51e2c049499d25b3610c0877b
[4]: https://docs.python.org/3/library/signal.html#note-on-sigpipe
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
2023-01-12 10:30:06 +08:00
|
|
|
try:
|
|
|
|
args = parse_arguments()
|
2020-08-22 22:56:18 +08:00
|
|
|
|
scripts: handle BrokenPipeError for python scripts
In the follow-up of commit fb3041d61f68 ("kbuild: fix SIGPIPE error
message for AR=gcc-ar and AR=llvm-ar"), Kees Cook pointed out that
tools should _not_ catch their own SIGPIPEs [1] [2].
Based on his feedback, LLVM was fixed [3].
However, Python's default behavior is to show noisy bracktrace when
SIGPIPE is sent. So, scripts written in Python are basically in the
same situation as the buggy llvm tools.
Example:
$ make -s allnoconfig
$ make -s allmodconfig
$ scripts/diffconfig .config.old .config | head -n1
-ALIX n
Traceback (most recent call last):
File "/home/masahiro/linux/scripts/diffconfig", line 132, in <module>
main()
File "/home/masahiro/linux/scripts/diffconfig", line 130, in main
print_config("+", config, None, b[config])
File "/home/masahiro/linux/scripts/diffconfig", line 64, in print_config
print("+%s %s" % (config, new_value))
BrokenPipeError: [Errno 32] Broken pipe
Python documentation [4] notes how to make scripts die immediately and
silently:
"""
Piping output of your program to tools like head(1) will cause a
SIGPIPE signal to be sent to your process when the receiver of its
standard output closes early. This results in an exception like
BrokenPipeError: [Errno 32] Broken pipe. To handle this case,
wrap your entry point to catch this exception as follows:
import os
import sys
def main():
try:
# simulate large output (your code replaces this loop)
for x in range(10000):
print("y")
# flush output here to force SIGPIPE to be triggered
# while inside this try block.
sys.stdout.flush()
except BrokenPipeError:
# Python flushes standard streams on exit; redirect remaining output
# to devnull to avoid another BrokenPipeError at shutdown
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1) # Python exits with error code 1 on EPIPE
if __name__ == '__main__':
main()
Do not set SIGPIPE’s disposition to SIG_DFL in order to avoid
BrokenPipeError. Doing that would cause your program to exit
unexpectedly whenever any socket connection is interrupted while
your program is still writing to it.
"""
Currently, tools/perf/scripts/python/intel-pt-events.py seems to be the
only script that fixes the issue that way.
tools/perf/scripts/python/compaction-times.py uses another approach
signal.signal(signal.SIGPIPE, signal.SIG_DFL) but the Python
documentation clearly says "Don't do it".
I cannot fix all Python scripts since there are so many.
I fixed some in the scripts/ directory.
[1]: https://lore.kernel.org/all/202211161056.1B9611A@keescook/
[2]: https://github.com/llvm/llvm-project/issues/59037
[3]: https://github.com/llvm/llvm-project/commit/4787efa38066adb51e2c049499d25b3610c0877b
[4]: https://docs.python.org/3/library/signal.html#note-on-sigpipe
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
2023-01-12 10:30:06 +08:00
|
|
|
lock = multiprocessing.Lock()
|
|
|
|
pool = multiprocessing.Pool(initializer=init, initargs=(lock, args))
|
|
|
|
# Read JSON data into the datastore variable
|
|
|
|
with open(args.path, "r") as f:
|
|
|
|
datastore = json.load(f)
|
|
|
|
pool.map(run_analysis, datastore)
|
|
|
|
except BrokenPipeError:
|
|
|
|
# Python flushes standard streams on exit; redirect remaining output
|
|
|
|
# to devnull to avoid another BrokenPipeError at shutdown
|
|
|
|
devnull = os.open(os.devnull, os.O_WRONLY)
|
|
|
|
os.dup2(devnull, sys.stdout.fileno())
|
|
|
|
sys.exit(1) # Python exits with error code 1 on EPIPE
|
2020-08-22 22:56:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|