kcov.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. */
  60. if (!t || !in_task())
  61. return;
  62. mode = READ_ONCE(t->kcov_mode);
  63. if (mode == KCOV_MODE_TRACE) {
  64. unsigned long *area;
  65. unsigned long pos;
  66. unsigned long ip = _RET_IP_;
  67. #ifdef CONFIG_RANDOMIZE_BASE
  68. ip -= kaslr_offset();
  69. #endif
  70. /*
  71. * There is some code that runs in interrupts but for which
  72. * in_interrupt() returns false (e.g. preempt_schedule_irq()).
  73. * READ_ONCE()/barrier() effectively provides load-acquire wrt
  74. * interrupts, there are paired barrier()/WRITE_ONCE() in
  75. * kcov_ioctl_locked().
  76. */
  77. barrier();
  78. area = t->kcov_area;
  79. /* The first word is number of subsequent PCs. */
  80. pos = READ_ONCE(area[0]) + 1;
  81. if (likely(pos < t->kcov_size)) {
  82. area[pos] = ip;
  83. WRITE_ONCE(area[0], pos);
  84. }
  85. }
  86. }
  87. EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
  88. static void kcov_get(struct kcov *kcov)
  89. {
  90. atomic_inc(&kcov->refcount);
  91. }
  92. static void kcov_put(struct kcov *kcov)
  93. {
  94. if (atomic_dec_and_test(&kcov->refcount)) {
  95. vfree(kcov->area);
  96. kfree(kcov);
  97. }
  98. }
  99. void kcov_task_init(struct task_struct *t)
  100. {
  101. t->kcov_mode = KCOV_MODE_DISABLED;
  102. t->kcov_size = 0;
  103. t->kcov_area = NULL;
  104. t->kcov = NULL;
  105. }
  106. void kcov_task_exit(struct task_struct *t)
  107. {
  108. struct kcov *kcov;
  109. kcov = t->kcov;
  110. if (kcov == NULL)
  111. return;
  112. spin_lock(&kcov->lock);
  113. if (WARN_ON(kcov->t != t)) {
  114. spin_unlock(&kcov->lock);
  115. return;
  116. }
  117. /* Just to not leave dangling references behind. */
  118. kcov_task_init(t);
  119. kcov->t = NULL;
  120. spin_unlock(&kcov->lock);
  121. kcov_put(kcov);
  122. }
  123. static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
  124. {
  125. int res = 0;
  126. void *area;
  127. struct kcov *kcov = vma->vm_file->private_data;
  128. unsigned long size, off;
  129. struct page *page;
  130. area = vmalloc_user(vma->vm_end - vma->vm_start);
  131. if (!area)
  132. return -ENOMEM;
  133. spin_lock(&kcov->lock);
  134. size = kcov->size * sizeof(unsigned long);
  135. if (kcov->mode == KCOV_MODE_DISABLED || vma->vm_pgoff != 0 ||
  136. vma->vm_end - vma->vm_start != size) {
  137. res = -EINVAL;
  138. goto exit;
  139. }
  140. if (!kcov->area) {
  141. kcov->area = area;
  142. vma->vm_flags |= VM_DONTEXPAND;
  143. spin_unlock(&kcov->lock);
  144. for (off = 0; off < size; off += PAGE_SIZE) {
  145. page = vmalloc_to_page(kcov->area + off);
  146. if (vm_insert_page(vma, vma->vm_start + off, page))
  147. WARN_ONCE(1, "vm_insert_page() failed");
  148. }
  149. return 0;
  150. }
  151. exit:
  152. spin_unlock(&kcov->lock);
  153. vfree(area);
  154. return res;
  155. }
  156. static int kcov_open(struct inode *inode, struct file *filep)
  157. {
  158. struct kcov *kcov;
  159. kcov = kzalloc(sizeof(*kcov), GFP_KERNEL);
  160. if (!kcov)
  161. return -ENOMEM;
  162. atomic_set(&kcov->refcount, 1);
  163. spin_lock_init(&kcov->lock);
  164. filep->private_data = kcov;
  165. return nonseekable_open(inode, filep);
  166. }
  167. static int kcov_close(struct inode *inode, struct file *filep)
  168. {
  169. kcov_put(filep->private_data);
  170. return 0;
  171. }
  172. static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
  173. unsigned long arg)
  174. {
  175. struct task_struct *t;
  176. unsigned long size, unused;
  177. switch (cmd) {
  178. case KCOV_INIT_TRACE:
  179. /*
  180. * Enable kcov in trace mode and setup buffer size.
  181. * Must happen before anything else.
  182. */
  183. if (kcov->mode != KCOV_MODE_DISABLED)
  184. return -EBUSY;
  185. /*
  186. * Size must be at least 2 to hold current position and one PC.
  187. * Later we allocate size * sizeof(unsigned long) memory,
  188. * that must not overflow.
  189. */
  190. size = arg;
  191. if (size < 2 || size > INT_MAX / sizeof(unsigned long))
  192. return -EINVAL;
  193. kcov->size = size;
  194. kcov->mode = KCOV_MODE_TRACE;
  195. return 0;
  196. case KCOV_ENABLE:
  197. /*
  198. * Enable coverage for the current task.
  199. * At this point user must have been enabled trace mode,
  200. * and mmapped the file. Coverage collection is disabled only
  201. * at task exit or voluntary by KCOV_DISABLE. After that it can
  202. * be enabled for another task.
  203. */
  204. unused = arg;
  205. if (unused != 0 || kcov->mode == KCOV_MODE_DISABLED ||
  206. kcov->area == NULL)
  207. return -EINVAL;
  208. if (kcov->t != NULL)
  209. return -EBUSY;
  210. t = current;
  211. /* Cache in task struct for performance. */
  212. t->kcov_size = kcov->size;
  213. t->kcov_area = kcov->area;
  214. /* See comment in __sanitizer_cov_trace_pc(). */
  215. barrier();
  216. WRITE_ONCE(t->kcov_mode, kcov->mode);
  217. t->kcov = kcov;
  218. kcov->t = t;
  219. /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
  220. kcov_get(kcov);
  221. return 0;
  222. case KCOV_DISABLE:
  223. /* Disable coverage for the current task. */
  224. unused = arg;
  225. if (unused != 0 || current->kcov != kcov)
  226. return -EINVAL;
  227. t = current;
  228. if (WARN_ON(kcov->t != t))
  229. return -EINVAL;
  230. kcov_task_init(t);
  231. kcov->t = NULL;
  232. kcov_put(kcov);
  233. return 0;
  234. default:
  235. return -ENOTTY;
  236. }
  237. }
  238. static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  239. {
  240. struct kcov *kcov;
  241. int res;
  242. kcov = filep->private_data;
  243. spin_lock(&kcov->lock);
  244. res = kcov_ioctl_locked(kcov, cmd, arg);
  245. spin_unlock(&kcov->lock);
  246. return res;
  247. }
  248. static const struct file_operations kcov_fops = {
  249. .open = kcov_open,
  250. .unlocked_ioctl = kcov_ioctl,
  251. .mmap = kcov_mmap,
  252. .release = kcov_close,
  253. };
  254. static int __init kcov_init(void)
  255. {
  256. /*
  257. * The kcov debugfs file won't ever get removed and thus,
  258. * there is no need to protect it against removal races. The
  259. * use of debugfs_create_file_unsafe() is actually safe here.
  260. */
  261. if (!debugfs_create_file_unsafe("kcov", 0600, NULL, NULL, &kcov_fops)) {
  262. pr_err("failed to create kcov in debugfs\n");
  263. return -ENOMEM;
  264. }
  265. return 0;
  266. }
  267. device_initcall(kcov_init);