kcov.c 6.4 KB

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