kcov.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #define pr_fmt(fmt) "kcov: " fmt
  2. #define DISABLE_BRANCH_PROFILING
  3. #include <linux/atomic.h>
  4. #include <linux/compiler.h>
  5. #include <linux/errno.h>
  6. #include <linux/export.h>
  7. #include <linux/types.h>
  8. #include <linux/file.h>
  9. #include <linux/fs.h>
  10. #include <linux/init.h>
  11. #include <linux/mm.h>
  12. #include <linux/preempt.h>
  13. #include <linux/printk.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/kcov.h>
  21. #include <asm/setup.h>
  22. /*
  23. * kcov descriptor (one per opened debugfs file).
  24. * State transitions of the descriptor:
  25. * - initial state after open()
  26. * - then there must be a single ioctl(KCOV_INIT_TRACE) call
  27. * - then, mmap() call (several calls are allowed but not useful)
  28. * - then, repeated enable/disable for a task (only one task a time allowed)
  29. */
  30. struct kcov {
  31. /*
  32. * Reference counter. We keep one for:
  33. * - opened file descriptor
  34. * - task with enabled coverage (we can't unwire it from another task)
  35. */
  36. atomic_t refcount;
  37. /* The lock protects mode, size, area and t. */
  38. spinlock_t lock;
  39. enum kcov_mode mode;
  40. /* Size of arena (in long's for KCOV_MODE_TRACE). */
  41. unsigned size;
  42. /* Coverage buffer shared with user space. */
  43. void *area;
  44. /* Task for which we collect coverage, or NULL. */
  45. struct task_struct *t;
  46. };
  47. /*
  48. * Entry point from instrumented code.
  49. * This is called once per basic-block/edge.
  50. */
  51. void notrace __sanitizer_cov_trace_pc(void)
  52. {
  53. struct task_struct *t;
  54. enum kcov_mode mode;
  55. t = current;
  56. /*
  57. * We are interested in code coverage as a function of a syscall inputs,
  58. * so we ignore code executed in interrupts.
  59. * The checks for whether we are in an interrupt are open-coded, because
  60. * 1. We can't use in_interrupt() here, since it also returns true
  61. * when we are inside local_bh_disable() section.
  62. * 2. We don't want to use (in_irq() | in_serving_softirq() | in_nmi()),
  63. * since that leads to slower generated code (three separate tests,
  64. * one for each of the flags).
  65. */
  66. if (!t || (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET
  67. | NMI_MASK)))
  68. return;
  69. mode = READ_ONCE(t->kcov_mode);
  70. if (mode == KCOV_MODE_TRACE) {
  71. unsigned long *area;
  72. unsigned long pos;
  73. unsigned long ip = _RET_IP_;
  74. #ifdef CONFIG_RANDOMIZE_BASE
  75. ip -= kaslr_offset();
  76. #endif
  77. /*
  78. * There is some code that runs in interrupts but for which
  79. * in_interrupt() returns false (e.g. preempt_schedule_irq()).
  80. * READ_ONCE()/barrier() effectively provides load-acquire wrt
  81. * interrupts, there are paired barrier()/WRITE_ONCE() in
  82. * kcov_ioctl_locked().
  83. */
  84. barrier();
  85. area = t->kcov_area;
  86. /* The first word is number of subsequent PCs. */
  87. pos = READ_ONCE(area[0]) + 1;
  88. if (likely(pos < t->kcov_size)) {
  89. area[pos] = ip;
  90. WRITE_ONCE(area[0], pos);
  91. }
  92. }
  93. }
  94. EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
  95. static void kcov_get(struct kcov *kcov)
  96. {
  97. atomic_inc(&kcov->refcount);
  98. }
  99. static void kcov_put(struct kcov *kcov)
  100. {
  101. if (atomic_dec_and_test(&kcov->refcount)) {
  102. vfree(kcov->area);
  103. kfree(kcov);
  104. }
  105. }
  106. void kcov_task_init(struct task_struct *t)
  107. {
  108. t->kcov_mode = KCOV_MODE_DISABLED;
  109. t->kcov_size = 0;
  110. t->kcov_area = NULL;
  111. t->kcov = NULL;
  112. }
  113. void kcov_task_exit(struct task_struct *t)
  114. {
  115. struct kcov *kcov;
  116. kcov = t->kcov;
  117. if (kcov == NULL)
  118. return;
  119. spin_lock(&kcov->lock);
  120. if (WARN_ON(kcov->t != t)) {
  121. spin_unlock(&kcov->lock);
  122. return;
  123. }
  124. /* Just to not leave dangling references behind. */
  125. kcov_task_init(t);
  126. kcov->t = NULL;
  127. spin_unlock(&kcov->lock);
  128. kcov_put(kcov);
  129. }
  130. static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
  131. {
  132. int res = 0;
  133. void *area;
  134. struct kcov *kcov = vma->vm_file->private_data;
  135. unsigned long size, off;
  136. struct page *page;
  137. area = vmalloc_user(vma->vm_end - vma->vm_start);
  138. if (!area)
  139. return -ENOMEM;
  140. spin_lock(&kcov->lock);
  141. size = kcov->size * sizeof(unsigned long);
  142. if (kcov->mode == KCOV_MODE_DISABLED || vma->vm_pgoff != 0 ||
  143. vma->vm_end - vma->vm_start != size) {
  144. res = -EINVAL;
  145. goto exit;
  146. }
  147. if (!kcov->area) {
  148. kcov->area = area;
  149. vma->vm_flags |= VM_DONTEXPAND;
  150. spin_unlock(&kcov->lock);
  151. for (off = 0; off < size; off += PAGE_SIZE) {
  152. page = vmalloc_to_page(kcov->area + off);
  153. if (vm_insert_page(vma, vma->vm_start + off, page))
  154. WARN_ONCE(1, "vm_insert_page() failed");
  155. }
  156. return 0;
  157. }
  158. exit:
  159. spin_unlock(&kcov->lock);
  160. vfree(area);
  161. return res;
  162. }
  163. static int kcov_open(struct inode *inode, struct file *filep)
  164. {
  165. struct kcov *kcov;
  166. kcov = kzalloc(sizeof(*kcov), GFP_KERNEL);
  167. if (!kcov)
  168. return -ENOMEM;
  169. atomic_set(&kcov->refcount, 1);
  170. spin_lock_init(&kcov->lock);
  171. filep->private_data = kcov;
  172. return nonseekable_open(inode, filep);
  173. }
  174. static int kcov_close(struct inode *inode, struct file *filep)
  175. {
  176. kcov_put(filep->private_data);
  177. return 0;
  178. }
  179. static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
  180. unsigned long arg)
  181. {
  182. struct task_struct *t;
  183. unsigned long size, unused;
  184. switch (cmd) {
  185. case KCOV_INIT_TRACE:
  186. /*
  187. * Enable kcov in trace mode and setup buffer size.
  188. * Must happen before anything else.
  189. */
  190. if (kcov->mode != KCOV_MODE_DISABLED)
  191. return -EBUSY;
  192. /*
  193. * Size must be at least 2 to hold current position and one PC.
  194. * Later we allocate size * sizeof(unsigned long) memory,
  195. * that must not overflow.
  196. */
  197. size = arg;
  198. if (size < 2 || size > INT_MAX / sizeof(unsigned long))
  199. return -EINVAL;
  200. kcov->size = size;
  201. kcov->mode = KCOV_MODE_TRACE;
  202. return 0;
  203. case KCOV_ENABLE:
  204. /*
  205. * Enable coverage for the current task.
  206. * At this point user must have been enabled trace mode,
  207. * and mmapped the file. Coverage collection is disabled only
  208. * at task exit or voluntary by KCOV_DISABLE. After that it can
  209. * be enabled for another task.
  210. */
  211. unused = arg;
  212. if (unused != 0 || kcov->mode == KCOV_MODE_DISABLED ||
  213. kcov->area == NULL)
  214. return -EINVAL;
  215. if (kcov->t != NULL)
  216. return -EBUSY;
  217. t = current;
  218. /* Cache in task struct for performance. */
  219. t->kcov_size = kcov->size;
  220. t->kcov_area = kcov->area;
  221. /* See comment in __sanitizer_cov_trace_pc(). */
  222. barrier();
  223. WRITE_ONCE(t->kcov_mode, kcov->mode);
  224. t->kcov = kcov;
  225. kcov->t = t;
  226. /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
  227. kcov_get(kcov);
  228. return 0;
  229. case KCOV_DISABLE:
  230. /* Disable coverage for the current task. */
  231. unused = arg;
  232. if (unused != 0 || current->kcov != kcov)
  233. return -EINVAL;
  234. t = current;
  235. if (WARN_ON(kcov->t != t))
  236. return -EINVAL;
  237. kcov_task_init(t);
  238. kcov->t = NULL;
  239. kcov_put(kcov);
  240. return 0;
  241. default:
  242. return -ENOTTY;
  243. }
  244. }
  245. static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  246. {
  247. struct kcov *kcov;
  248. int res;
  249. kcov = filep->private_data;
  250. spin_lock(&kcov->lock);
  251. res = kcov_ioctl_locked(kcov, cmd, arg);
  252. spin_unlock(&kcov->lock);
  253. return res;
  254. }
  255. static const struct file_operations kcov_fops = {
  256. .open = kcov_open,
  257. .unlocked_ioctl = kcov_ioctl,
  258. .mmap = kcov_mmap,
  259. .release = kcov_close,
  260. };
  261. static int __init kcov_init(void)
  262. {
  263. /*
  264. * The kcov debugfs file won't ever get removed and thus,
  265. * there is no need to protect it against removal races. The
  266. * use of debugfs_create_file_unsafe() is actually safe here.
  267. */
  268. if (!debugfs_create_file_unsafe("kcov", 0600, NULL, NULL, &kcov_fops)) {
  269. pr_err("failed to create kcov in debugfs\n");
  270. return -ENOMEM;
  271. }
  272. return 0;
  273. }
  274. device_initcall(kcov_init);