162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci====================
462306a36Sopenharmony_ciKernel Testing Guide
562306a36Sopenharmony_ci====================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciThere are a number of different tools for testing the Linux kernel, so knowing
962306a36Sopenharmony_ciwhen to use each of them can be a challenge. This document provides a rough
1062306a36Sopenharmony_cioverview of their differences, and how they fit together.
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciWriting and Running Tests
1462306a36Sopenharmony_ci=========================
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciThe bulk of kernel tests are written using either the kselftest or KUnit
1762306a36Sopenharmony_ciframeworks. These both provide infrastructure to help make running tests and
1862306a36Sopenharmony_cigroups of tests easier, as well as providing helpers to aid in writing new
1962306a36Sopenharmony_citests.
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciIf you're looking to verify the behaviour of the Kernel — particularly specific
2262306a36Sopenharmony_ciparts of the kernel — then you'll want to use KUnit or kselftest.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciThe Difference Between KUnit and kselftest
2662306a36Sopenharmony_ci------------------------------------------
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciKUnit (Documentation/dev-tools/kunit/index.rst) is an entirely in-kernel system
2962306a36Sopenharmony_cifor "white box" testing: because test code is part of the kernel, it can access
3062306a36Sopenharmony_ciinternal structures and functions which aren't exposed to userspace.
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciKUnit tests therefore are best written against small, self-contained parts
3362306a36Sopenharmony_ciof the kernel, which can be tested in isolation. This aligns well with the
3462306a36Sopenharmony_ciconcept of 'unit' testing.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciFor example, a KUnit test might test an individual kernel function (or even a
3762306a36Sopenharmony_cisingle codepath through a function, such as an error handling case), rather
3862306a36Sopenharmony_cithan a feature as a whole.
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciThis also makes KUnit tests very fast to build and run, allowing them to be
4162306a36Sopenharmony_cirun frequently as part of the development process.
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ciThere is a KUnit test style guide which may give further pointers in
4462306a36Sopenharmony_ciDocumentation/dev-tools/kunit/style.rst
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cikselftest (Documentation/dev-tools/kselftest.rst), on the other hand, is
4862306a36Sopenharmony_cilargely implemented in userspace, and tests are normal userspace scripts or
4962306a36Sopenharmony_ciprograms.
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciThis makes it easier to write more complicated tests, or tests which need to
5262306a36Sopenharmony_cimanipulate the overall system state more (e.g., spawning processes, etc.).
5362306a36Sopenharmony_ciHowever, it's not possible to call kernel functions directly from kselftest.
5462306a36Sopenharmony_ciThis means that only kernel functionality which is exposed to userspace somehow
5562306a36Sopenharmony_ci(e.g. by a syscall, device, filesystem, etc.) can be tested with kselftest.  To
5662306a36Sopenharmony_ciwork around this, some tests include a companion kernel module which exposes
5762306a36Sopenharmony_cimore information or functionality. If a test runs mostly or entirely within the
5862306a36Sopenharmony_cikernel, however,  KUnit may be the more appropriate tool.
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cikselftest is therefore suited well to tests of whole features, as these will
6162306a36Sopenharmony_ciexpose an interface to userspace, which can be tested, but not implementation
6262306a36Sopenharmony_cidetails. This aligns well with 'system' or 'end-to-end' testing.
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciFor example, all new system calls should be accompanied by kselftest tests.
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciCode Coverage Tools
6762306a36Sopenharmony_ci===================
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciThe Linux Kernel supports two different code coverage measurement tools. These
7062306a36Sopenharmony_cican be used to verify that a test is executing particular functions or lines
7162306a36Sopenharmony_ciof code. This is useful for determining how much of the kernel is being tested,
7262306a36Sopenharmony_ciand for finding corner-cases which are not covered by the appropriate test.
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciDocumentation/dev-tools/gcov.rst is GCC's coverage testing tool, which can be
7562306a36Sopenharmony_ciused with the kernel to get global or per-module coverage. Unlike KCOV, it
7662306a36Sopenharmony_cidoes not record per-task coverage. Coverage data can be read from debugfs,
7762306a36Sopenharmony_ciand interpreted using the usual gcov tooling.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciDocumentation/dev-tools/kcov.rst is a feature which can be built in to the
8062306a36Sopenharmony_cikernel to allow capturing coverage on a per-task level. It's therefore useful
8162306a36Sopenharmony_cifor fuzzing and other situations where information about code executed during,
8262306a36Sopenharmony_cifor example, a single syscall is useful.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciDynamic Analysis Tools
8662306a36Sopenharmony_ci======================
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciThe kernel also supports a number of dynamic analysis tools, which attempt to
8962306a36Sopenharmony_cidetect classes of issues when they occur in a running kernel. These typically
9062306a36Sopenharmony_cieach look for a different class of bugs, such as invalid memory accesses,
9162306a36Sopenharmony_ciconcurrency issues such as data races, or other undefined behaviour like
9262306a36Sopenharmony_ciinteger overflows.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciSome of these tools are listed below:
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci* kmemleak detects possible memory leaks. See
9762306a36Sopenharmony_ci  Documentation/dev-tools/kmemleak.rst
9862306a36Sopenharmony_ci* KASAN detects invalid memory accesses such as out-of-bounds and
9962306a36Sopenharmony_ci  use-after-free errors. See Documentation/dev-tools/kasan.rst
10062306a36Sopenharmony_ci* UBSAN detects behaviour that is undefined by the C standard, like integer
10162306a36Sopenharmony_ci  overflows. See Documentation/dev-tools/ubsan.rst
10262306a36Sopenharmony_ci* KCSAN detects data races. See Documentation/dev-tools/kcsan.rst
10362306a36Sopenharmony_ci* KFENCE is a low-overhead detector of memory issues, which is much faster than
10462306a36Sopenharmony_ci  KASAN and can be used in production. See Documentation/dev-tools/kfence.rst
10562306a36Sopenharmony_ci* lockdep is a locking correctness validator. See
10662306a36Sopenharmony_ci  Documentation/locking/lockdep-design.rst
10762306a36Sopenharmony_ci* There are several other pieces of debug instrumentation in the kernel, many
10862306a36Sopenharmony_ci  of which can be found in lib/Kconfig.debug
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciThese tools tend to test the kernel as a whole, and do not "pass" like
11162306a36Sopenharmony_cikselftest or KUnit tests. They can be combined with KUnit or kselftest by
11262306a36Sopenharmony_cirunning tests on a kernel with these tools enabled: you can then be sure
11362306a36Sopenharmony_cithat none of these errors are occurring during the test.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciSome of these tools integrate with KUnit or kselftest and will
11662306a36Sopenharmony_ciautomatically fail tests if an issue is detected.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciStatic Analysis Tools
11962306a36Sopenharmony_ci=====================
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciIn addition to testing a running kernel, one can also analyze kernel source code
12262306a36Sopenharmony_cidirectly (**at compile time**) using **static analysis** tools. The tools
12362306a36Sopenharmony_cicommonly used in the kernel allow one to inspect the whole source tree or just
12462306a36Sopenharmony_cispecific files within it. They make it easier to detect and fix problems during
12562306a36Sopenharmony_cithe development process.
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciSparse can help test the kernel by performing type-checking, lock checking,
12862306a36Sopenharmony_civalue range checking, in addition to reporting various errors and warnings while
12962306a36Sopenharmony_ciexamining the code. See the Documentation/dev-tools/sparse.rst documentation
13062306a36Sopenharmony_cipage for details on how to use it.
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciSmatch extends Sparse and provides additional checks for programming logic
13362306a36Sopenharmony_cimistakes such as missing breaks in switch statements, unused return values on
13462306a36Sopenharmony_cierror checking, forgetting to set an error code in the return of an error path,
13562306a36Sopenharmony_cietc. Smatch also has tests against more serious issues such as integer
13662306a36Sopenharmony_cioverflows, null pointer dereferences, and memory leaks. See the project page at
13762306a36Sopenharmony_cihttp://smatch.sourceforge.net/.
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciCoccinelle is another static analyzer at our disposal. Coccinelle is often used
14062306a36Sopenharmony_cito aid refactoring and collateral evolution of source code, but it can also help
14162306a36Sopenharmony_cito avoid certain bugs that occur in common code patterns. The types of tests
14262306a36Sopenharmony_ciavailable include API tests, tests for correct usage of kernel iterators, checks
14362306a36Sopenharmony_cifor the soundness of free operations, analysis of locking behavior, and further
14462306a36Sopenharmony_citests known to help keep consistent kernel usage. See the
14562306a36Sopenharmony_ciDocumentation/dev-tools/coccinelle.rst documentation page for details.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciBeware, though, that static analysis tools suffer from **false positives**.
14862306a36Sopenharmony_ciErrors and warns need to be evaluated carefully before attempting to fix them.
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciWhen to use Sparse and Smatch
15162306a36Sopenharmony_ci-----------------------------
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ciSparse does type checking, such as verifying that annotated variables do not
15462306a36Sopenharmony_cicause endianness bugs, detecting places that use ``__user`` pointers improperly,
15562306a36Sopenharmony_ciand analyzing the compatibility of symbol initializers.
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciSmatch does flow analysis and, if allowed to build the function database, it
15862306a36Sopenharmony_cialso does cross function analysis. Smatch tries to answer questions like where
15962306a36Sopenharmony_ciis this buffer allocated? How big is it? Can this index be controlled by the
16062306a36Sopenharmony_ciuser? Is this variable larger than that variable?
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciIt's generally easier to write checks in Smatch than it is to write checks in
16362306a36Sopenharmony_ciSparse. Nevertheless, there are some overlaps between Sparse and Smatch checks.
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciStrong points of Smatch and Coccinelle
16662306a36Sopenharmony_ci--------------------------------------
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ciCoccinelle is probably the easiest for writing checks. It works before the
16962306a36Sopenharmony_cipre-processor so it's easier to check for bugs in macros using Coccinelle.
17062306a36Sopenharmony_ciCoccinelle also creates patches for you, which no other tool does.
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciFor example, with Coccinelle you can do a mass conversion from
17362306a36Sopenharmony_ci``kmalloc(x * size, GFP_KERNEL)`` to ``kmalloc_array(x, size, GFP_KERNEL)``, and
17462306a36Sopenharmony_cithat's really useful. If you just created a Smatch warning and try to push the
17562306a36Sopenharmony_ciwork of converting on to the maintainers they would be annoyed. You'd have to
17662306a36Sopenharmony_ciargue about each warning if can really overflow or not.
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciCoccinelle does no analysis of variable values, which is the strong point of
17962306a36Sopenharmony_ciSmatch. On the other hand, Coccinelle allows you to do simple things in a simple
18062306a36Sopenharmony_ciway.
181