testing-overview.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ====================
  3. Kernel Testing Guide
  4. ====================
  5. There are a number of different tools for testing the Linux kernel, so knowing
  6. when to use each of them can be a challenge. This document provides a rough
  7. overview of their differences, and how they fit together.
  8. Writing and Running Tests
  9. =========================
  10. The bulk of kernel tests are written using either the kselftest or KUnit
  11. frameworks. These both provide infrastructure to help make running tests and
  12. groups of tests easier, as well as providing helpers to aid in writing new
  13. tests.
  14. If you're looking to verify the behaviour of the Kernel — particularly specific
  15. parts of the kernel — then you'll want to use KUnit or kselftest.
  16. The Difference Between KUnit and kselftest
  17. ------------------------------------------
  18. KUnit (Documentation/dev-tools/kunit/index.rst) is an entirely in-kernel system
  19. for "white box" testing: because test code is part of the kernel, it can access
  20. internal structures and functions which aren't exposed to userspace.
  21. KUnit tests therefore are best written against small, self-contained parts
  22. of the kernel, which can be tested in isolation. This aligns well with the
  23. concept of 'unit' testing.
  24. For example, a KUnit test might test an individual kernel function (or even a
  25. single codepath through a function, such as an error handling case), rather
  26. than a feature as a whole.
  27. This also makes KUnit tests very fast to build and run, allowing them to be
  28. run frequently as part of the development process.
  29. There is a KUnit test style guide which may give further pointers in
  30. Documentation/dev-tools/kunit/style.rst
  31. kselftest (Documentation/dev-tools/kselftest.rst), on the other hand, is
  32. largely implemented in userspace, and tests are normal userspace scripts or
  33. programs.
  34. This makes it easier to write more complicated tests, or tests which need to
  35. manipulate the overall system state more (e.g., spawning processes, etc.).
  36. However, it's not possible to call kernel functions directly from kselftest.
  37. This means that only kernel functionality which is exposed to userspace somehow
  38. (e.g. by a syscall, device, filesystem, etc.) can be tested with kselftest. To
  39. work around this, some tests include a companion kernel module which exposes
  40. more information or functionality. If a test runs mostly or entirely within the
  41. kernel, however, KUnit may be the more appropriate tool.
  42. kselftest is therefore suited well to tests of whole features, as these will
  43. expose an interface to userspace, which can be tested, but not implementation
  44. details. This aligns well with 'system' or 'end-to-end' testing.
  45. For example, all new system calls should be accompanied by kselftest tests.
  46. Code Coverage Tools
  47. ===================
  48. The Linux Kernel supports two different code coverage measurement tools. These
  49. can be used to verify that a test is executing particular functions or lines
  50. of code. This is useful for determining how much of the kernel is being tested,
  51. and for finding corner-cases which are not covered by the appropriate test.
  52. Documentation/dev-tools/gcov.rst is GCC's coverage testing tool, which can be
  53. used with the kernel to get global or per-module coverage. Unlike KCOV, it
  54. does not record per-task coverage. Coverage data can be read from debugfs,
  55. and interpreted using the usual gcov tooling.
  56. Documentation/dev-tools/kcov.rst is a feature which can be built in to the
  57. kernel to allow capturing coverage on a per-task level. It's therefore useful
  58. for fuzzing and other situations where information about code executed during,
  59. for example, a single syscall is useful.
  60. Dynamic Analysis Tools
  61. ======================
  62. The kernel also supports a number of dynamic analysis tools, which attempt to
  63. detect classes of issues when they occur in a running kernel. These typically
  64. each look for a different class of bugs, such as invalid memory accesses,
  65. concurrency issues such as data races, or other undefined behaviour like
  66. integer overflows.
  67. Some of these tools are listed below:
  68. * kmemleak detects possible memory leaks. See
  69. Documentation/dev-tools/kmemleak.rst
  70. * KASAN detects invalid memory accesses such as out-of-bounds and
  71. use-after-free errors. See Documentation/dev-tools/kasan.rst
  72. * UBSAN detects behaviour that is undefined by the C standard, like integer
  73. overflows. See Documentation/dev-tools/ubsan.rst
  74. * KCSAN detects data races. See Documentation/dev-tools/kcsan.rst
  75. * KFENCE is a low-overhead detector of memory issues, which is much faster than
  76. KASAN and can be used in production. See Documentation/dev-tools/kfence.rst
  77. * lockdep is a locking correctness validator. See
  78. Documentation/locking/lockdep-design.rst
  79. * There are several other pieces of debug instrumentation in the kernel, many
  80. of which can be found in lib/Kconfig.debug
  81. These tools tend to test the kernel as a whole, and do not "pass" like
  82. kselftest or KUnit tests. They can be combined with KUnit or kselftest by
  83. running tests on a kernel with these tools enabled: you can then be sure
  84. that none of these errors are occurring during the test.
  85. Some of these tools integrate with KUnit or kselftest and will
  86. automatically fail tests if an issue is detected.
  87. Static Analysis Tools
  88. =====================
  89. In addition to testing a running kernel, one can also analyze kernel source code
  90. directly (**at compile time**) using **static analysis** tools. The tools
  91. commonly used in the kernel allow one to inspect the whole source tree or just
  92. specific files within it. They make it easier to detect and fix problems during
  93. the development process.
  94. Sparse can help test the kernel by performing type-checking, lock checking,
  95. value range checking, in addition to reporting various errors and warnings while
  96. examining the code. See the Documentation/dev-tools/sparse.rst documentation
  97. page for details on how to use it.
  98. Smatch extends Sparse and provides additional checks for programming logic
  99. mistakes such as missing breaks in switch statements, unused return values on
  100. error checking, forgetting to set an error code in the return of an error path,
  101. etc. Smatch also has tests against more serious issues such as integer
  102. overflows, null pointer dereferences, and memory leaks. See the project page at
  103. http://smatch.sourceforge.net/.
  104. Coccinelle is another static analyzer at our disposal. Coccinelle is often used
  105. to aid refactoring and collateral evolution of source code, but it can also help
  106. to avoid certain bugs that occur in common code patterns. The types of tests
  107. available include API tests, tests for correct usage of kernel iterators, checks
  108. for the soundness of free operations, analysis of locking behavior, and further
  109. tests known to help keep consistent kernel usage. See the
  110. Documentation/dev-tools/coccinelle.rst documentation page for details.
  111. Beware, though, that static analysis tools suffer from **false positives**.
  112. Errors and warns need to be evaluated carefully before attempting to fix them.
  113. When to use Sparse and Smatch
  114. -----------------------------
  115. Sparse does type checking, such as verifying that annotated variables do not
  116. cause endianness bugs, detecting places that use ``__user`` pointers improperly,
  117. and analyzing the compatibility of symbol initializers.
  118. Smatch does flow analysis and, if allowed to build the function database, it
  119. also does cross function analysis. Smatch tries to answer questions like where
  120. is this buffer allocated? How big is it? Can this index be controlled by the
  121. user? Is this variable larger than that variable?
  122. It's generally easier to write checks in Smatch than it is to write checks in
  123. Sparse. Nevertheless, there are some overlaps between Sparse and Smatch checks.
  124. Strong points of Smatch and Coccinelle
  125. --------------------------------------
  126. Coccinelle is probably the easiest for writing checks. It works before the
  127. pre-processor so it's easier to check for bugs in macros using Coccinelle.
  128. Coccinelle also creates patches for you, which no other tool does.
  129. For example, with Coccinelle you can do a mass conversion from
  130. ``kmalloc(x * size, GFP_KERNEL)`` to ``kmalloc_array(x, size, GFP_KERNEL)``, and
  131. that's really useful. If you just created a Smatch warning and try to push the
  132. work of converting on to the maintainers they would be annoyed. You'd have to
  133. argue about each warning if can really overflow or not.
  134. Coccinelle does no analysis of variable values, which is the strong point of
  135. Smatch. On the other hand, Coccinelle allows you to do simple things in a simple
  136. way.