kcov.c 6.4 KB

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