1*61c4878aSAndroid Build Coastguard Worker.. _docs-automated-analysis: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker================== 4*61c4878aSAndroid Build Coastguard WorkerAutomated analysis 5*61c4878aSAndroid Build Coastguard Worker================== 6*61c4878aSAndroid Build Coastguard Worker 7*61c4878aSAndroid Build Coastguard WorkerThe correctness and style of Pigweed's source code is continuously verified 8*61c4878aSAndroid Build Coastguard Workerusing a suite of automated tools. We also make it easy to use the same tools 9*61c4878aSAndroid Build Coastguard Workerto verify the code of projects using Pigweed. 10*61c4878aSAndroid Build Coastguard Worker 11*61c4878aSAndroid Build Coastguard Worker------- 12*61c4878aSAndroid Build Coastguard WorkerSummary 13*61c4878aSAndroid Build Coastguard Worker------- 14*61c4878aSAndroid Build Coastguard WorkerOn presubmit or in CI we verify Pigweed using: 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Worker* pylint 17*61c4878aSAndroid Build Coastguard Worker* mypy 18*61c4878aSAndroid Build Coastguard Worker* clang-tidy 19*61c4878aSAndroid Build Coastguard Worker* AddressSanitizer (asan) 20*61c4878aSAndroid Build Coastguard Worker* ThreadSanitizer (tsan) 21*61c4878aSAndroid Build Coastguard Worker* UndefinedBehaviorSanitizer (ubsan) 22*61c4878aSAndroid Build Coastguard Worker* OSS-Fuzz 23*61c4878aSAndroid Build Coastguard Worker 24*61c4878aSAndroid Build Coastguard WorkerThe rest of this document discusses these tools and their configuration in 25*61c4878aSAndroid Build Coastguard Workergreater detail, and how to use them in your own project. 26*61c4878aSAndroid Build Coastguard Worker 27*61c4878aSAndroid Build Coastguard Worker-------------- 28*61c4878aSAndroid Build Coastguard WorkerAnalysis tools 29*61c4878aSAndroid Build Coastguard Worker-------------- 30*61c4878aSAndroid Build Coastguard Worker 31*61c4878aSAndroid Build Coastguard WorkerStatic analysis 32*61c4878aSAndroid Build Coastguard Worker=============== 33*61c4878aSAndroid Build Coastguard Worker 34*61c4878aSAndroid Build Coastguard WorkerPyLint 35*61c4878aSAndroid Build Coastguard Worker------ 36*61c4878aSAndroid Build Coastguard Worker`PyLint`_ is a customizable Python linter. Pigweed complies with almost all 37*61c4878aSAndroid Build Coastguard Workerthe default checks; see `.pylintrc`_ for details. PyLint detects problems such 38*61c4878aSAndroid Build Coastguard Workeras overly broad catch statements, unused arguments/variables, and mutable 39*61c4878aSAndroid Build Coastguard Workerdefault parameter values. 40*61c4878aSAndroid Build Coastguard Worker 41*61c4878aSAndroid Build Coastguard WorkerFor upstream Pigweed, PyLint can be run with ``ninja python.lint.pylint`` or 42*61c4878aSAndroid Build Coastguard Worker``ninja python.lint``. It's also included in a variety of presubmit steps, 43*61c4878aSAndroid Build Coastguard Workerlike ``static_analysis`` and ``python_checks.gn_python_check``. See the 44*61c4878aSAndroid Build Coastguard Worker`Enabling analysis for your project`_ section to learn how to run PyLint on 45*61c4878aSAndroid Build Coastguard Workeryour Pigweed-based project. 46*61c4878aSAndroid Build Coastguard Worker 47*61c4878aSAndroid Build Coastguard Worker.. _PyLint: https://pylint.org/ 48*61c4878aSAndroid Build Coastguard Worker.. _.pylintrc: https://cs.pigweed.dev/pigweed/+/main:.pylintrc 49*61c4878aSAndroid Build Coastguard Worker 50*61c4878aSAndroid Build Coastguard WorkerMypy 51*61c4878aSAndroid Build Coastguard Worker---- 52*61c4878aSAndroid Build Coastguard WorkerPython 3 allows for `type annotations`_ for variables, function arguments, and 53*61c4878aSAndroid Build Coastguard Workerreturn values. Most, but not all, of Pigweed's Python code has type 54*61c4878aSAndroid Build Coastguard Workerannotations, and these annotations have caught real bugs in code that didn't 55*61c4878aSAndroid Build Coastguard Workeryet have unit tests. `Mypy`_ is an analysis tool that enforces these 56*61c4878aSAndroid Build Coastguard Workerannotations. 57*61c4878aSAndroid Build Coastguard Worker 58*61c4878aSAndroid Build Coastguard WorkerMypy helps find bugs like when a string is passed into a function that expects 59*61c4878aSAndroid Build Coastguard Workera list of strings---since both are iterables this bug might otherwise be hard 60*61c4878aSAndroid Build Coastguard Workerto track down. 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard WorkerMypy can be run with ``ninja python.lint.mypy`` or ``ninja python.lint``. It's 63*61c4878aSAndroid Build Coastguard Workeralso included in a variety of presubmit steps, like ``static_analysis`` and 64*61c4878aSAndroid Build Coastguard Worker``python_checks.gn_python_check``. 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Worker.. _type annotations: https://docs.python.org/3/library/typing.html 67*61c4878aSAndroid Build Coastguard Worker.. _Mypy: http://mypy-lang.org/ 68*61c4878aSAndroid Build Coastguard Worker 69*61c4878aSAndroid Build Coastguard Workerclang-tidy 70*61c4878aSAndroid Build Coastguard Worker---------- 71*61c4878aSAndroid Build Coastguard Worker`clang-tidy`_ is a C++ "linter" and static analysis tool. It identifies 72*61c4878aSAndroid Build Coastguard Workerbug-prone patterns (e.g., use after move), non-idiomatic usage (e.g., creating 73*61c4878aSAndroid Build Coastguard Worker``std::unique_ptr`` with ``new`` rather than ``std::make_unique``), and 74*61c4878aSAndroid Build Coastguard Workerperformance issues (e.g., unnecessary copies of loop variables). 75*61c4878aSAndroid Build Coastguard Worker 76*61c4878aSAndroid Build Coastguard WorkerWhile powerful, clang-tidy defines a very large number of checks, many of which 77*61c4878aSAndroid Build Coastguard Workerare special-purpose (e.g., only applicable to FPGA HLS code, or code using the 78*61c4878aSAndroid Build Coastguard Worker`Abseil`_ library) or have high false positive rates. Pigweed enables over 50 79*61c4878aSAndroid Build Coastguard Workerchecks which are relevant to an embedded C/C++ library and have good 80*61c4878aSAndroid Build Coastguard Workersignal-to-noise ratios. The full list of Pigweed's checks is in `.clang-tidy`_. 81*61c4878aSAndroid Build Coastguard Worker 82*61c4878aSAndroid Build Coastguard WorkerWe do not currently enable the `Clang Static Analyzers`_ because they suffer 83*61c4878aSAndroid Build Coastguard Workerfrom false positives, and their findings are time-consuming to manually verify. 84*61c4878aSAndroid Build Coastguard Worker 85*61c4878aSAndroid Build Coastguard Workerclang-tidy can be run with ``ninja static_analysis`` or ``pw presubmit --step 86*61c4878aSAndroid Build Coastguard Workerstatic_analysis``. Note that as a static analysis tool, clang-tidy will not 87*61c4878aSAndroid Build Coastguard Workerproduce any runnable binaries: it simply analyzes the source files. 88*61c4878aSAndroid Build Coastguard Worker 89*61c4878aSAndroid Build Coastguard Worker.. _clang-tidy: https://clang.llvm.org/extra/clang-tidy/ 90*61c4878aSAndroid Build Coastguard Worker.. _Abseil: https://abseil.io/ 91*61c4878aSAndroid Build Coastguard Worker.. _.clang-tidy: https://cs.pigweed.dev/pigweed/+/main:.clang-tidy 92*61c4878aSAndroid Build Coastguard Worker.. _Clang Static Analyzers: https://clang-analyzer.llvm.org/available_checks.html 93*61c4878aSAndroid Build Coastguard Worker 94*61c4878aSAndroid Build Coastguard Worker 95*61c4878aSAndroid Build Coastguard WorkerClang sanitizers 96*61c4878aSAndroid Build Coastguard Worker================ 97*61c4878aSAndroid Build Coastguard WorkerWe run all of Pigweed's unit tests with the additional instrumentation 98*61c4878aSAndroid Build Coastguard Workerdescribed in this section. For more detail about these sanitizers, see the 99*61c4878aSAndroid Build Coastguard Worker`Github documentation`_. 100*61c4878aSAndroid Build Coastguard Worker 101*61c4878aSAndroid Build Coastguard Worker* asan: `AddressSanitizer`_ detects memory errors such as out-of-bounds access 102*61c4878aSAndroid Build Coastguard Worker and use-after-free. 103*61c4878aSAndroid Build Coastguard Worker* tsan: `ThreadSanitizer`_ detects data races. 104*61c4878aSAndroid Build Coastguard Worker* ubsan: `UndefinedBehaviorSanitizer`_ is a fast undefined behavior detector. 105*61c4878aSAndroid Build Coastguard Worker We use the default ``-fsanitize=undefined`` option. 106*61c4878aSAndroid Build Coastguard Worker 107*61c4878aSAndroid Build Coastguard Worker.. note:: 108*61c4878aSAndroid Build Coastguard Worker Pigweed does not currently support `MemorySanitizer`_ (msan). See 109*61c4878aSAndroid Build Coastguard Worker :bug:`234876100` for details. 110*61c4878aSAndroid Build Coastguard Worker 111*61c4878aSAndroid Build Coastguard WorkerThe exact configurations we use for these sanitizers are in 112*61c4878aSAndroid Build Coastguard Worker`pw_toolchain/host_clang/BUILD.gn <https://cs.pigweed.dev/pigweed/+/main:pw_toolchain/host_clang/BUILD.gn>`_. 113*61c4878aSAndroid Build Coastguard WorkerYou can see the current status of the sanitizer builds in the `Pigweed CI 114*61c4878aSAndroid Build Coastguard Workerconsole`_, as ``pigweed-linux-san-*``. 115*61c4878aSAndroid Build Coastguard Worker 116*61c4878aSAndroid Build Coastguard WorkerUnlike clang-tidy, the clang sanitizers are runtime instrumentation: the 117*61c4878aSAndroid Build Coastguard Workerinstrumented binary needs to be run for issues to be detected. 118*61c4878aSAndroid Build Coastguard Worker 119*61c4878aSAndroid Build Coastguard Worker.. _Github documentation: https://github.com/google/sanitizers 120*61c4878aSAndroid Build Coastguard Worker.. _AddressSanitizer: https://clang.llvm.org/docs/AddressSanitizer.html 121*61c4878aSAndroid Build Coastguard Worker.. _MemorySanitizer: https://clang.llvm.org/docs/MemorySanitizer.html 122*61c4878aSAndroid Build Coastguard Worker.. _Pigweed CI console: https://ci.chromium.org/p/pigweed/g/pigweed/console 123*61c4878aSAndroid Build Coastguard Worker.. _ThreadSanitizer: https://clang.llvm.org/docs/ThreadSanitizer.html 124*61c4878aSAndroid Build Coastguard Worker.. _UndefinedBehaviorSanitizer: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html 125*61c4878aSAndroid Build Coastguard Worker 126*61c4878aSAndroid Build Coastguard Worker 127*61c4878aSAndroid Build Coastguard WorkerFuzzers 128*61c4878aSAndroid Build Coastguard Worker======= 129*61c4878aSAndroid Build Coastguard Worker`Fuzz testing`_ detects errors in software by providing it with randomly 130*61c4878aSAndroid Build Coastguard Workergenerated inputs. We use `OSS-fuzz`_ to continuously uncover potential 131*61c4878aSAndroid Build Coastguard Workervulnerabilities in Pigweed. `Dashboard with Pigweed's latest results`_. See 132*61c4878aSAndroid Build Coastguard Workerthe :ref:`module-pw_fuzzer` module documentation for more details. 133*61c4878aSAndroid Build Coastguard Worker 134*61c4878aSAndroid Build Coastguard Worker.. _Dashboard with Pigweed's latest results: https://oss-fuzz-build-logs.storage.googleapis.com/index.html#pigweed 135*61c4878aSAndroid Build Coastguard Worker.. _Fuzz testing: https://en.wikipedia.org/wiki/Fuzzing 136*61c4878aSAndroid Build Coastguard Worker.. _OSS-fuzz: https://github.com/google/oss-fuzz 137*61c4878aSAndroid Build Coastguard Worker 138*61c4878aSAndroid Build Coastguard Worker.. _Enabling analysis for your project: 139*61c4878aSAndroid Build Coastguard Worker 140*61c4878aSAndroid Build Coastguard Worker---------------------------------- 141*61c4878aSAndroid Build Coastguard WorkerEnabling analysis for your project 142*61c4878aSAndroid Build Coastguard Worker---------------------------------- 143*61c4878aSAndroid Build Coastguard Worker 144*61c4878aSAndroid Build Coastguard WorkerGN 145*61c4878aSAndroid Build Coastguard Worker== 146*61c4878aSAndroid Build Coastguard Worker 147*61c4878aSAndroid Build Coastguard WorkerPyLint and Mypy 148*61c4878aSAndroid Build Coastguard Worker--------------- 149*61c4878aSAndroid Build Coastguard WorkerPyLint and Mypy can be configured to run every time your project is built by 150*61c4878aSAndroid Build Coastguard Workeradding ``python.lint`` to your default build group. (You can also add one or both 151*61c4878aSAndroid Build Coastguard Workerindividually using ``python.lint.mypy`` and ``python.lint.pylint``.) Likewise, 152*61c4878aSAndroid Build Coastguard Workerthese can be added to individual presubmit steps (`examples`_). You can also 153*61c4878aSAndroid Build Coastguard Workerdirectly include the `python_checks.gn_python_lint`_ presubmit step. 154*61c4878aSAndroid Build Coastguard Worker 155*61c4878aSAndroid Build Coastguard Worker.. _examples: https://cs.opensource.google/search?q=file:pigweed_presubmit.py%20%22python.lint%22&sq=&ss=pigweed%2Fpigweed 156*61c4878aSAndroid Build Coastguard Worker.. _python_checks.gn_python_lint: https://cs.pigweed.dev/pigweed/+/main:pw_presubmit/py/pw_presubmit/python_checks.py?q=file:python_checks.py%20gn_python_lint&ss=pigweed%2Fpigweed 157*61c4878aSAndroid Build Coastguard Worker 158*61c4878aSAndroid Build Coastguard Workerclang-tidy 159*61c4878aSAndroid Build Coastguard Worker---------- 160*61c4878aSAndroid Build Coastguard Worker`pw_toolchain/static_analysis_toolchain.gni`_ provides the 161*61c4878aSAndroid Build Coastguard Worker``pw_static_analysis_toolchain`` template that can be used to create a build 162*61c4878aSAndroid Build Coastguard Workergroup performing static analysis. See :ref:`module-pw_toolchain` documentation 163*61c4878aSAndroid Build Coastguard Workerfor more details. This group can then be added as a presubmit step using 164*61c4878aSAndroid Build Coastguard Workerpw_presubmit. 165*61c4878aSAndroid Build Coastguard Worker 166*61c4878aSAndroid Build Coastguard WorkerYou can place a ``.clang-tidy`` file at the root of your repository to control 167*61c4878aSAndroid Build Coastguard Workerwhich checks are executed. See the `clang documentation`_ for a discussion of how 168*61c4878aSAndroid Build Coastguard Workerthe tool chooses which ``.clang-tidy`` files to apply when run on a particular 169*61c4878aSAndroid Build Coastguard Workersource file. 170*61c4878aSAndroid Build Coastguard Worker 171*61c4878aSAndroid Build Coastguard Worker.. _pw_toolchain/static_analysis_toolchain.gni: https://cs.pigweed.dev/pigweed/+/main:pw_toolchain/static_analysis_toolchain.gni 172*61c4878aSAndroid Build Coastguard Worker.. _clang documentation: https://clang.llvm.org/extra/clang-tidy/ 173*61c4878aSAndroid Build Coastguard Worker 174*61c4878aSAndroid Build Coastguard WorkerClang sanitizers 175*61c4878aSAndroid Build Coastguard Worker---------------- 176*61c4878aSAndroid Build Coastguard WorkerThere are two ways to enable sanitizers for your build. 177*61c4878aSAndroid Build Coastguard Worker 178*61c4878aSAndroid Build Coastguard WorkerGN args on debug toolchains 179*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^ 180*61c4878aSAndroid Build Coastguard WorkerIf you are already building your tests with one of the following toolchains (or 181*61c4878aSAndroid Build Coastguard Workera toolchain derived from one of them): 182*61c4878aSAndroid Build Coastguard Worker 183*61c4878aSAndroid Build Coastguard Worker* ``pw_toolchain_host_clang.debug`` 184*61c4878aSAndroid Build Coastguard Worker* ``pw_toolchain_host_clang.speed_optimized`` 185*61c4878aSAndroid Build Coastguard Worker* ``pw_toolchain_host_clang.size_optimized`` 186*61c4878aSAndroid Build Coastguard Worker 187*61c4878aSAndroid Build Coastguard Workeryou can enable the clang sanitizers simply by setting the gn arg 188*61c4878aSAndroid Build Coastguard Worker``pw_toolchain_SANITIZERS`` to the desired subset of 189*61c4878aSAndroid Build Coastguard Worker``["address", "thread", "undefined"]``. 190*61c4878aSAndroid Build Coastguard Worker 191*61c4878aSAndroid Build Coastguard WorkerExample 192*61c4878aSAndroid Build Coastguard Worker....... 193*61c4878aSAndroid Build Coastguard WorkerIf your project defines a toolchain ``host_clang_debug`` that is derived from 194*61c4878aSAndroid Build Coastguard Workerone of the above toolchains, and you'd like to run the ``pw_executable`` target 195*61c4878aSAndroid Build Coastguard Worker``sample_binary`` defined in the ``BUILD.gn`` file in ``examples/sample`` with 196*61c4878aSAndroid Build Coastguard Workerasan, you would run, 197*61c4878aSAndroid Build Coastguard Worker 198*61c4878aSAndroid Build Coastguard Worker.. code-block:: bash 199*61c4878aSAndroid Build Coastguard Worker 200*61c4878aSAndroid Build Coastguard Worker gn gen out --args='pw_toolchain_SANITIZERS=["address"]' 201*61c4878aSAndroid Build Coastguard Worker ninja -C out host_clang_debug/obj/example/sample/bin/sample_binary 202*61c4878aSAndroid Build Coastguard Worker out/host_clang_debug/obj/example/sample/bin/sample_binary 203*61c4878aSAndroid Build Coastguard Worker 204*61c4878aSAndroid Build Coastguard WorkerSanitizer toolchains 205*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^ 206*61c4878aSAndroid Build Coastguard WorkerOtherwise, instead of using ``gn args`` you can build your tests with the 207*61c4878aSAndroid Build Coastguard Workerappropriate toolchain from the following list (or a toolchain derived from one 208*61c4878aSAndroid Build Coastguard Workerof them): 209*61c4878aSAndroid Build Coastguard Worker 210*61c4878aSAndroid Build Coastguard Worker* ``pw_toolchain_host_clang.asan`` 211*61c4878aSAndroid Build Coastguard Worker* ``pw_toolchain_host_clang.ubsan`` 212*61c4878aSAndroid Build Coastguard Worker* ``pw_toolchain_host_clang.tsan`` 213*61c4878aSAndroid Build Coastguard Worker 214*61c4878aSAndroid Build Coastguard WorkerSee the :ref:`module-pw_toolchain` module documentation for more 215*61c4878aSAndroid Build Coastguard Workerabout Pigweed toolchains. 216*61c4878aSAndroid Build Coastguard Worker 217*61c4878aSAndroid Build Coastguard WorkerBazel 218*61c4878aSAndroid Build Coastguard Worker===== 219*61c4878aSAndroid Build Coastguard Worker 220*61c4878aSAndroid Build Coastguard Worker.. _docs-automated-analysis-clang-sanitizers: 221*61c4878aSAndroid Build Coastguard Worker 222*61c4878aSAndroid Build Coastguard WorkerClang sanitizers 223*61c4878aSAndroid Build Coastguard Worker---------------- 224*61c4878aSAndroid Build Coastguard WorkerIf you're using Pigweed's own host toolchain configuration, you can enable 225*61c4878aSAndroid Build Coastguard WorkerAddressSanitizer by building with the appropriate flag: 226*61c4878aSAndroid Build Coastguard Worker 227*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh 228*61c4878aSAndroid Build Coastguard Worker 229*61c4878aSAndroid Build Coastguard Worker bazelisk build --@pigweed//pw_toolchain/host_clang:asan //... 230*61c4878aSAndroid Build Coastguard Worker 231*61c4878aSAndroid Build Coastguard WorkerIf you're building your own toolchain, you can add 232*61c4878aSAndroid Build Coastguard Worker``@pigweed//pw_toolchain_bazel/flag_sets:asan`` to it. 233*61c4878aSAndroid Build Coastguard Worker 234*61c4878aSAndroid Build Coastguard WorkerFuzzers 235*61c4878aSAndroid Build Coastguard Worker======= 236*61c4878aSAndroid Build Coastguard WorkerSee the :ref:`module-pw_fuzzer` module documentation. 237