Name Date Size

..25-Oct-20244 KiB

.gitignoreH A D25-Oct-202439

android_libpthread/H25-Oct-20244 KiB

android_librt/H25-Oct-20244 KiB

cloner.cH A D25-Oct-20244.5 KiB

errnos.hH A D25-Oct-20243.8 KiB

gen_version.shH A D25-Oct-2024320

get_path.cH A D25-Oct-20242.7 KiB

ltp.pc.inH A D25-Oct-2024192

MakefileH A D25-Oct-2024879

newlib_tests/H25-Oct-20244 KiB

parse_opts.cH A D25-Oct-202416 KiB

random_range.cH A D25-Oct-202421.6 KiB

README.mdH A D25-Oct-20247.5 KiB

safe_file_ops.cH A D25-Oct-20249.5 KiB

safe_macros.cH A D25-Oct-202430.3 KiB

safe_net.cH A D25-Oct-202413.3 KiB

safe_pthread.cH A D25-Oct-20245.1 KiB

safe_stdio.cH A D25-Oct-20242.5 KiB

self_exec.cH A D25-Oct-20244.7 KiB

signame.hH A D25-Oct-20242.1 KiB

tests/H25-Oct-20244 KiB

tlibio.cH A D25-Oct-202460.4 KiB

tst_af_alg.cH A D25-Oct-20245.8 KiB

tst_ansi_color.cH A D25-Oct-2024966

tst_arch.cH A D25-Oct-20241.8 KiB

tst_assert.cH A D25-Oct-20242.2 KiB

tst_bool_expr.cH A D25-Oct-20248.1 KiB

tst_buffers.cH A D25-Oct-20242.9 KiB

tst_capability.cH A D25-Oct-20242 KiB

tst_cgroup.cH A D25-Oct-202436.9 KiB

tst_checkpoint.cH A D25-Oct-20244.5 KiB

tst_checksum.cH A D25-Oct-20243.4 KiB

tst_clocks.cH A D25-Oct-20243.5 KiB

tst_clone.cH A D25-Oct-2024850

tst_cmd.cH A D25-Oct-20248.2 KiB

tst_coredump.cH A D25-Oct-2024731

tst_cpu.cH A D25-Oct-20242.8 KiB

tst_crypto.cH A D25-Oct-20242.5 KiB

tst_device.cH A D25-Oct-202413.5 KiB

tst_dir_is_empty.cH A D25-Oct-20241.3 KiB

tst_epoll.cH A D25-Oct-20241.5 KiB

tst_fill_file.cH A D25-Oct-20242.3 KiB

tst_fill_fs.cH A D25-Oct-20242.8 KiB

tst_fips.cH A D25-Oct-2024452

tst_fs_has_free.cH A D25-Oct-20241.2 KiB

tst_fs_link_count.cH A D25-Oct-20244.8 KiB

tst_fs_setup.cH A D25-Oct-20241.1 KiB

tst_fs_type.cH A D25-Oct-20241.5 KiB

tst_get_bad_addr.cH A D25-Oct-20241 KiB

tst_hugepage.cH A D25-Oct-20242.1 KiB

tst_ioctl.cH A D25-Oct-2024728

tst_kconfig.cH A D25-Oct-202410 KiB

tst_kernel.cH A D25-Oct-20244.6 KiB

tst_kvercmp.cH A D25-Oct-20243.8 KiB

tst_lockdown.cH A D25-Oct-20242.1 KiB

tst_memutils.cH A D25-Oct-20243.9 KiB

tst_mkfs.cH A D25-Oct-20242.9 KiB

tst_module.cH A D25-Oct-20243 KiB

tst_net.cH A D25-Oct-20245.3 KiB

tst_netdevice.cH A D25-Oct-202416.8 KiB

tst_parse_opts.cH A D25-Oct-20241.3 KiB

tst_path_has_mnt_flags.cH A D25-Oct-20242 KiB

tst_pid.cH A D25-Oct-20244.4 KiB

tst_process_state.cH A D25-Oct-20241.6 KiB

tst_rand_data.cH A D25-Oct-202416.7 KiB

tst_res.cH A D25-Oct-202414.9 KiB

tst_resource.cH A D25-Oct-20243.7 KiB

tst_rtctime.cH A D25-Oct-20242.9 KiB

tst_rtnetlink.cH A D25-Oct-20248.9 KiB

tst_safe_file_at.cH A D25-Oct-20244.9 KiB

tst_safe_io_uring.cH A D25-Oct-20243.3 KiB

tst_safe_macros.cH A D25-Oct-202412.2 KiB

tst_safe_sysv_ipc.cH A D25-Oct-20245.8 KiB

tst_safe_timerfd.cH A D25-Oct-20241.5 KiB

tst_sig.cH A D25-Oct-20247.1 KiB

tst_sig_proc.cH A D25-Oct-2024471

tst_status.cH A D25-Oct-20241.2 KiB

tst_supported_fs_types.cH A D25-Oct-20243.5 KiB

tst_sys_conf.cH A D25-Oct-20243 KiB

tst_taint.cH A D25-Oct-20242.6 KiB

tst_test.cH A D25-Oct-202436.8 KiB

tst_thread_state.cH A D25-Oct-2024734

tst_timer.cH A D25-Oct-20241.2 KiB

tst_timer_test.cH A D25-Oct-202410.9 KiB

tst_tmpdir.cH A D25-Oct-20248.3 KiB

tst_uid.cH A D25-Oct-20242.6 KiB

tst_virt.cH A D25-Oct-20244 KiB

tst_wallclock.cH A D25-Oct-20243.1 KiB

README.md

1# Test library design document
2
3## High-level picture
4
5    library process
6    +----------------------------+
7    | main                       |
8    |  tst_run_tcases            |
9    |   do_setup                 |
10    |   for_each_variant         |
11    |    for_each_filesystem     |   test process
12    |     fork_testrun ------------->+--------------------------------------------+
13    |      waitpid               |   | testrun                                    |
14    |                            |   |  do_test_setup                             |
15    |                            |   |   tst_test->setup                          |
16    |                            |   |  run_tests                                 |
17    |                            |   |   tst_test->test(i) or tst_test->test_all  |
18    |                            |   |  do_test_cleanup                           |
19    |                            |   |   tst_test->cleanup                        |
20    |                            |   |  exit(0)                                   |
21    |   do_exit                  |   +--------------------------------------------+
22    |    do_cleanup              |
23    |     exit(ret)              |
24    +----------------------------+
25
26## Test lifetime overview
27
28When a test is executed the very first thing to happen is that we check for
29various test prerequisites. These are described in the tst\_test structure and
30range from simple '.require\_root' to a more complicated kernel .config boolean
31expressions such as: "CONFIG\_X86\_INTEL\_UMIP=y | CONFIG\_X86\_UMIP=y".
32
33If all checks are passed the process carries on with setting up the test
34environment as requested in the tst\_test structure. There are many different
35setup steps that have been put into the test library again ranging from rather
36simple creation of a unique test temporary directory to a bit more complicated
37ones such as preparing, formatting, and mounting a block device.
38
39The test library also intializes shrared memory used for IPC at this step.
40
41Once all the prerequisites are checked and test environment has been prepared
42we can move on executing the testcase itself. The actual test is executed in a
43forked process, however there are a few hops before we get there.
44
45First of all there are test variants, which means that the test is re-executed
46several times with a slightly different setting. This is usually used to test a
47family of similar syscalls, where we test each of these syscalls exactly the
48same, but without re-executing the test binary itself. Test variants are
49implemented as a simple global variable counter that gets increased on each
50iteration. In a case of syscall tests we switch between which syscall to call
51based on the global counter.
52
53Then there is all\_filesystems flag which is mostly the same as test variants
54but executes the test for each filesystem supported by the system. Note that we
55can get cartesian product between test variants and all filesystems as well.
56
57In a pseudo code it could be expressed as:
58
59```
60for test_variants:
61	for all_filesystems:
62		fork_testrun()
63```
64
65Before we fork() the test process the test library sets up a timeout alarm and
66also a heartbeat signal handlers and also sets up an alarm(2) accordingly to
67the test timeout. When a test times out the test library gets SIGALRM and the
68alarm handler mercilessly kills all forked children by sending SIGKILL to the
69whole process group. The heartbeat handler is used by the test process to reset
70this timer for example when the test functions run in a loop.
71
72With that done we finally fork() the test process. The test process firstly
73resets signal handlers and sets its pid to be a process group leader so that we
74can slaughter all children if needed. The test library proceeds with suspending
75itself in waitpid() syscall and waits for the child to finish at this point.
76
77The test process goes ahead and calls the test setup() function if present in
78the tst\_test structure. It's important that we execute all test callbacks
79after we have forked the process, that way we cannot crash the test library
80process. The setup can also cause the test to exit prematurely by either direct
81or indirect (SAFE\_MACROS()) call to tst\_brk().  In this case the
82fork\_testrun() function exits, but the loops for test variants or filesystems
83carries on.
84
85All that is left to be done is to actually execute the tests, what happnes now
86depends on the -i and -I command line parameters that can request that the
87run() or run\_all() callbacks are executed N times or for N seconds. Again the
88test can exit at any time by direct or indirect call to tst\_brk().
89
90Once the test is finished all that is left for the test process is the test
91cleanup(). So if a there is a cleanup() callback in the tst\_test structure
92it's executed. The cleanup() callback runs in a special context where the
93tst\_brk(TBROK, ...) calls are converted into tst\_res(TWARN, ...) calls. This
94is because we found out that carrying on with partially broken cleanup is
95usually better option than exiting it in the middle.
96
97The test cleanup() is also called by the tst\_brk() handler in order to cleanup
98before exiting the test process, hence it must be able to cope even with
99partial test setup. Usually it suffices to make sure to clean up only
100resources that already have been set up and to do that in an inverse order that
101we did in setup().
102
103Once the test process exits or leaves the run() or run\_all() function the test
104library wakes up from the waitpid() call, and checks if the test process
105exited normally.
106
107Once the testrun is finished the test library does a cleanup() as well to clean
108up resources set up in the test library setup(), reports test results and
109finally exits the process.
110
111### Test library and fork()-ing
112
113Things are a bit more complicated when fork()-ing is involved, however the test
114results are stored in a page of a shared memory and incremented by atomic
115operations, hence the results are stored right after the test reporting
116function returns from the test library and the access is, by definition,
117race-free as well.
118
119On the other hand the test library, apart from sending a SIGKILL to the whole
120process group on timeout, does not track grandchildren.
121
122This especially means that:
123
124- The test exits once the main test process exits.
125
126- While the test results are, by the design, propagated to the test library
127  we may still miss a child that gets killed by a signal or exits unexpectedly.
128
129The test writer should, because of this, take care for reaping these proceses
130properly, in most cases this could be simply done by calling
131tst\_reap\_children() to collect and dissect deceased.
132
133Also note that tst\_brk() does exit only the current process, so if a child
134process calls tst\_brk() the counters are incremented and only the process
135exits.
136
137### Test library and exec()
138
139The piece of mapped memory to store the results to is not preserved over
140exec(2), hence to use the test library from a binary started by an exec() it
141has to be remaped. In this case the process must to call tst\_reinit() before
142calling any other library functions. In order to make this happen the program
143environment carries LTP\_IPC\_PATH variable with a path to the backing file on
144tmpfs. This also allows us to use the test library from shell testcases.
145
146### Test library and process synchronization
147
148The piece of mapped memory is also used as a base for a futex-based
149synchronization primitives called checkpoints. And as said previously the
150memory can be mapped to any process by calling the tst\_reinit() function. As a
151matter of a fact there is even a tst\_checkpoint binary that allows us to use
152the checkpoints from shell code as well.
153