kcov.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. kcov: code coverage for fuzzing
  2. ===============================
  3. kcov exposes kernel code coverage information in a form suitable for coverage-
  4. guided fuzzing (randomized testing). Coverage data of a running kernel is
  5. exported via the "kcov" debugfs file. Coverage collection is enabled on a task
  6. basis, and thus it can capture precise coverage of a single system call.
  7. Note that kcov does not aim to collect as much coverage as possible. It aims
  8. to collect more or less stable coverage that is function of syscall inputs.
  9. To achieve this goal it does not collect coverage in soft/hard interrupts
  10. and instrumentation of some inherently non-deterministic parts of kernel is
  11. disbled (e.g. scheduler, locking).
  12. Usage
  13. -----
  14. Configure the kernel with::
  15. CONFIG_KCOV=y
  16. CONFIG_KCOV requires gcc built on revision 231296 or later.
  17. Profiling data will only become accessible once debugfs has been mounted::
  18. mount -t debugfs none /sys/kernel/debug
  19. The following program demonstrates kcov usage from within a test program::
  20. #include <stdio.h>
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/mman.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #define KCOV_INIT_TRACE _IOR('c', 1, unsigned long)
  31. #define KCOV_ENABLE _IO('c', 100)
  32. #define KCOV_DISABLE _IO('c', 101)
  33. #define COVER_SIZE (64<<10)
  34. int main(int argc, char **argv)
  35. {
  36. int fd;
  37. unsigned long *cover, n, i;
  38. /* A single fd descriptor allows coverage collection on a single
  39. * thread.
  40. */
  41. fd = open("/sys/kernel/debug/kcov", O_RDWR);
  42. if (fd == -1)
  43. perror("open"), exit(1);
  44. /* Setup trace mode and trace size. */
  45. if (ioctl(fd, KCOV_INIT_TRACE, COVER_SIZE))
  46. perror("ioctl"), exit(1);
  47. /* Mmap buffer shared between kernel- and user-space. */
  48. cover = (unsigned long*)mmap(NULL, COVER_SIZE * sizeof(unsigned long),
  49. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  50. if ((void*)cover == MAP_FAILED)
  51. perror("mmap"), exit(1);
  52. /* Enable coverage collection on the current thread. */
  53. if (ioctl(fd, KCOV_ENABLE, 0))
  54. perror("ioctl"), exit(1);
  55. /* Reset coverage from the tail of the ioctl() call. */
  56. __atomic_store_n(&cover[0], 0, __ATOMIC_RELAXED);
  57. /* That's the target syscal call. */
  58. read(-1, NULL, 0);
  59. /* Read number of PCs collected. */
  60. n = __atomic_load_n(&cover[0], __ATOMIC_RELAXED);
  61. for (i = 0; i < n; i++)
  62. printf("0x%lx\n", cover[i + 1]);
  63. /* Disable coverage collection for the current thread. After this call
  64. * coverage can be enabled for a different thread.
  65. */
  66. if (ioctl(fd, KCOV_DISABLE, 0))
  67. perror("ioctl"), exit(1);
  68. /* Free resources. */
  69. if (munmap(cover, COVER_SIZE * sizeof(unsigned long)))
  70. perror("munmap"), exit(1);
  71. if (close(fd))
  72. perror("close"), exit(1);
  73. return 0;
  74. }
  75. After piping through addr2line output of the program looks as follows::
  76. SyS_read
  77. fs/read_write.c:562
  78. __fdget_pos
  79. fs/file.c:774
  80. __fget_light
  81. fs/file.c:746
  82. __fget_light
  83. fs/file.c:750
  84. __fget_light
  85. fs/file.c:760
  86. __fdget_pos
  87. fs/file.c:784
  88. SyS_read
  89. fs/read_write.c:562
  90. If a program needs to collect coverage from several threads (independently),
  91. it needs to open /sys/kernel/debug/kcov in each thread separately.
  92. The interface is fine-grained to allow efficient forking of test processes.
  93. That is, a parent process opens /sys/kernel/debug/kcov, enables trace mode,
  94. mmaps coverage buffer and then forks child processes in a loop. Child processes
  95. only need to enable coverage (disable happens automatically on thread end).