kcov.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) "kcov: " fmt
  3. #define DISABLE_BRANCH_PROFILING
  4. #include <linux/atomic.h>
  5. #include <linux/compiler.h>
  6. #include <linux/errno.h>
  7. #include <linux/export.h>
  8. #include <linux/types.h>
  9. #include <linux/file.h>
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/preempt.h>
  14. #include <linux/printk.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/kcov.h>
  22. #include <asm/setup.h>
  23. /* Number of 64-bit words written per one comparison: */
  24. #define KCOV_WORDS_PER_CMP 4
  25. /*
  26. * kcov descriptor (one per opened debugfs file).
  27. * State transitions of the descriptor:
  28. * - initial state after open()
  29. * - then there must be a single ioctl(KCOV_INIT_TRACE) call
  30. * - then, mmap() call (several calls are allowed but not useful)
  31. * - then, ioctl(KCOV_ENABLE, arg), where arg is
  32. * KCOV_TRACE_PC - to trace only the PCs
  33. * or
  34. * KCOV_TRACE_CMP - to trace only the comparison operands
  35. * - then, ioctl(KCOV_DISABLE) to disable the task.
  36. * Enabling/disabling ioctls can be repeated (only one task a time allowed).
  37. */
  38. struct kcov {
  39. /*
  40. * Reference counter. We keep one for:
  41. * - opened file descriptor
  42. * - task with enabled coverage (we can't unwire it from another task)
  43. */
  44. atomic_t refcount;
  45. /* The lock protects mode, size, area and t. */
  46. spinlock_t lock;
  47. enum kcov_mode mode;
  48. /* Size of arena (in long's for KCOV_MODE_TRACE). */
  49. unsigned size;
  50. /* Coverage buffer shared with user space. */
  51. void *area;
  52. /* Task for which we collect coverage, or NULL. */
  53. struct task_struct *t;
  54. };
  55. static notrace bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t)
  56. {
  57. unsigned int mode;
  58. /*
  59. * We are interested in code coverage as a function of a syscall inputs,
  60. * so we ignore code executed in interrupts.
  61. */
  62. if (!in_task())
  63. return false;
  64. mode = READ_ONCE(t->kcov_mode);
  65. /*
  66. * There is some code that runs in interrupts but for which
  67. * in_interrupt() returns false (e.g. preempt_schedule_irq()).
  68. * READ_ONCE()/barrier() effectively provides load-acquire wrt
  69. * interrupts, there are paired barrier()/WRITE_ONCE() in
  70. * kcov_ioctl_locked().
  71. */
  72. barrier();
  73. return mode == needed_mode;
  74. }
  75. static notrace unsigned long canonicalize_ip(unsigned long ip)
  76. {
  77. #ifdef CONFIG_RANDOMIZE_BASE
  78. ip -= kaslr_offset();
  79. #endif
  80. return ip;
  81. }
  82. /*
  83. * Entry point from instrumented code.
  84. * This is called once per basic-block/edge.
  85. */
  86. void notrace __sanitizer_cov_trace_pc(void)
  87. {
  88. struct task_struct *t;
  89. unsigned long *area;
  90. unsigned long ip = canonicalize_ip(_RET_IP_);
  91. unsigned long pos;
  92. t = current;
  93. if (!check_kcov_mode(KCOV_MODE_TRACE_PC, t))
  94. return;
  95. area = t->kcov_area;
  96. /* The first 64-bit word is the number of subsequent PCs. */
  97. pos = READ_ONCE(area[0]) + 1;
  98. if (likely(pos < t->kcov_size)) {
  99. area[pos] = ip;
  100. WRITE_ONCE(area[0], pos);
  101. }
  102. }
  103. EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
  104. #ifdef CONFIG_KCOV_ENABLE_COMPARISONS
  105. static void notrace write_comp_data(u64 type, u64 arg1, u64 arg2, u64 ip)
  106. {
  107. struct task_struct *t;
  108. u64 *area;
  109. u64 count, start_index, end_pos, max_pos;
  110. t = current;
  111. if (!check_kcov_mode(KCOV_MODE_TRACE_CMP, t))
  112. return;
  113. ip = canonicalize_ip(ip);
  114. /*
  115. * We write all comparison arguments and types as u64.
  116. * The buffer was allocated for t->kcov_size unsigned longs.
  117. */
  118. area = (u64 *)t->kcov_area;
  119. max_pos = t->kcov_size * sizeof(unsigned long);
  120. count = READ_ONCE(area[0]);
  121. /* Every record is KCOV_WORDS_PER_CMP 64-bit words. */
  122. start_index = 1 + count * KCOV_WORDS_PER_CMP;
  123. end_pos = (start_index + KCOV_WORDS_PER_CMP) * sizeof(u64);
  124. if (likely(end_pos <= max_pos)) {
  125. area[start_index] = type;
  126. area[start_index + 1] = arg1;
  127. area[start_index + 2] = arg2;
  128. area[start_index + 3] = ip;
  129. WRITE_ONCE(area[0], count + 1);
  130. }
  131. }
  132. void notrace __sanitizer_cov_trace_cmp1(u8 arg1, u8 arg2)
  133. {
  134. write_comp_data(KCOV_CMP_SIZE(0), arg1, arg2, _RET_IP_);
  135. }
  136. EXPORT_SYMBOL(__sanitizer_cov_trace_cmp1);
  137. void notrace __sanitizer_cov_trace_cmp2(u16 arg1, u16 arg2)
  138. {
  139. write_comp_data(KCOV_CMP_SIZE(1), arg1, arg2, _RET_IP_);
  140. }
  141. EXPORT_SYMBOL(__sanitizer_cov_trace_cmp2);
  142. void notrace __sanitizer_cov_trace_cmp4(u32 arg1, u32 arg2)
  143. {
  144. write_comp_data(KCOV_CMP_SIZE(2), arg1, arg2, _RET_IP_);
  145. }
  146. EXPORT_SYMBOL(__sanitizer_cov_trace_cmp4);
  147. void notrace __sanitizer_cov_trace_cmp8(u64 arg1, u64 arg2)
  148. {
  149. write_comp_data(KCOV_CMP_SIZE(3), arg1, arg2, _RET_IP_);
  150. }
  151. EXPORT_SYMBOL(__sanitizer_cov_trace_cmp8);
  152. void notrace __sanitizer_cov_trace_const_cmp1(u8 arg1, u8 arg2)
  153. {
  154. write_comp_data(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2,
  155. _RET_IP_);
  156. }
  157. EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp1);
  158. void notrace __sanitizer_cov_trace_const_cmp2(u16 arg1, u16 arg2)
  159. {
  160. write_comp_data(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2,
  161. _RET_IP_);
  162. }
  163. EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp2);
  164. void notrace __sanitizer_cov_trace_const_cmp4(u32 arg1, u32 arg2)
  165. {
  166. write_comp_data(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
  167. _RET_IP_);
  168. }
  169. EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp4);
  170. void notrace __sanitizer_cov_trace_const_cmp8(u64 arg1, u64 arg2)
  171. {
  172. write_comp_data(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2,
  173. _RET_IP_);
  174. }
  175. EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp8);
  176. void notrace __sanitizer_cov_trace_switch(u64 val, u64 *cases)
  177. {
  178. u64 i;
  179. u64 count = cases[0];
  180. u64 size = cases[1];
  181. u64 type = KCOV_CMP_CONST;
  182. switch (size) {
  183. case 8:
  184. type |= KCOV_CMP_SIZE(0);
  185. break;
  186. case 16:
  187. type |= KCOV_CMP_SIZE(1);
  188. break;
  189. case 32:
  190. type |= KCOV_CMP_SIZE(2);
  191. break;
  192. case 64:
  193. type |= KCOV_CMP_SIZE(3);
  194. break;
  195. default:
  196. return;
  197. }
  198. for (i = 0; i < count; i++)
  199. write_comp_data(type, cases[i + 2], val, _RET_IP_);
  200. }
  201. EXPORT_SYMBOL(__sanitizer_cov_trace_switch);
  202. #endif /* ifdef CONFIG_KCOV_ENABLE_COMPARISONS */
  203. static void kcov_get(struct kcov *kcov)
  204. {
  205. atomic_inc(&kcov->refcount);
  206. }
  207. static void kcov_put(struct kcov *kcov)
  208. {
  209. if (atomic_dec_and_test(&kcov->refcount)) {
  210. vfree(kcov->area);
  211. kfree(kcov);
  212. }
  213. }
  214. void kcov_task_init(struct task_struct *t)
  215. {
  216. WRITE_ONCE(t->kcov_mode, KCOV_MODE_DISABLED);
  217. barrier();
  218. t->kcov_size = 0;
  219. t->kcov_area = NULL;
  220. t->kcov = NULL;
  221. }
  222. void kcov_task_exit(struct task_struct *t)
  223. {
  224. struct kcov *kcov;
  225. kcov = t->kcov;
  226. if (kcov == NULL)
  227. return;
  228. spin_lock(&kcov->lock);
  229. if (WARN_ON(kcov->t != t)) {
  230. spin_unlock(&kcov->lock);
  231. return;
  232. }
  233. /* Just to not leave dangling references behind. */
  234. kcov_task_init(t);
  235. kcov->t = NULL;
  236. kcov->mode = KCOV_MODE_INIT;
  237. spin_unlock(&kcov->lock);
  238. kcov_put(kcov);
  239. }
  240. static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
  241. {
  242. int res = 0;
  243. void *area;
  244. struct kcov *kcov = vma->vm_file->private_data;
  245. unsigned long size, off;
  246. struct page *page;
  247. area = vmalloc_user(vma->vm_end - vma->vm_start);
  248. if (!area)
  249. return -ENOMEM;
  250. spin_lock(&kcov->lock);
  251. size = kcov->size * sizeof(unsigned long);
  252. if (kcov->mode != KCOV_MODE_INIT || vma->vm_pgoff != 0 ||
  253. vma->vm_end - vma->vm_start != size) {
  254. res = -EINVAL;
  255. goto exit;
  256. }
  257. if (!kcov->area) {
  258. kcov->area = area;
  259. vma->vm_flags |= VM_DONTEXPAND;
  260. spin_unlock(&kcov->lock);
  261. for (off = 0; off < size; off += PAGE_SIZE) {
  262. page = vmalloc_to_page(kcov->area + off);
  263. if (vm_insert_page(vma, vma->vm_start + off, page))
  264. WARN_ONCE(1, "vm_insert_page() failed");
  265. }
  266. return 0;
  267. }
  268. exit:
  269. spin_unlock(&kcov->lock);
  270. vfree(area);
  271. return res;
  272. }
  273. static int kcov_open(struct inode *inode, struct file *filep)
  274. {
  275. struct kcov *kcov;
  276. kcov = kzalloc(sizeof(*kcov), GFP_KERNEL);
  277. if (!kcov)
  278. return -ENOMEM;
  279. kcov->mode = KCOV_MODE_DISABLED;
  280. atomic_set(&kcov->refcount, 1);
  281. spin_lock_init(&kcov->lock);
  282. filep->private_data = kcov;
  283. return nonseekable_open(inode, filep);
  284. }
  285. static int kcov_close(struct inode *inode, struct file *filep)
  286. {
  287. kcov_put(filep->private_data);
  288. return 0;
  289. }
  290. /*
  291. * Fault in a lazily-faulted vmalloc area before it can be used by
  292. * __santizer_cov_trace_pc(), to avoid recursion issues if any code on the
  293. * vmalloc fault handling path is instrumented.
  294. */
  295. static void kcov_fault_in_area(struct kcov *kcov)
  296. {
  297. unsigned long stride = PAGE_SIZE / sizeof(unsigned long);
  298. unsigned long *area = kcov->area;
  299. unsigned long offset;
  300. for (offset = 0; offset < kcov->size; offset += stride)
  301. READ_ONCE(area[offset]);
  302. }
  303. static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
  304. unsigned long arg)
  305. {
  306. struct task_struct *t;
  307. unsigned long size, unused;
  308. switch (cmd) {
  309. case KCOV_INIT_TRACE:
  310. /*
  311. * Enable kcov in trace mode and setup buffer size.
  312. * Must happen before anything else.
  313. */
  314. if (kcov->mode != KCOV_MODE_DISABLED)
  315. return -EBUSY;
  316. /*
  317. * Size must be at least 2 to hold current position and one PC.
  318. * Later we allocate size * sizeof(unsigned long) memory,
  319. * that must not overflow.
  320. */
  321. size = arg;
  322. if (size < 2 || size > INT_MAX / sizeof(unsigned long))
  323. return -EINVAL;
  324. kcov->size = size;
  325. kcov->mode = KCOV_MODE_INIT;
  326. return 0;
  327. case KCOV_ENABLE:
  328. /*
  329. * Enable coverage for the current task.
  330. * At this point user must have been enabled trace mode,
  331. * and mmapped the file. Coverage collection is disabled only
  332. * at task exit or voluntary by KCOV_DISABLE. After that it can
  333. * be enabled for another task.
  334. */
  335. if (kcov->mode != KCOV_MODE_INIT || !kcov->area)
  336. return -EINVAL;
  337. t = current;
  338. if (kcov->t != NULL || t->kcov != NULL)
  339. return -EBUSY;
  340. if (arg == KCOV_TRACE_PC)
  341. kcov->mode = KCOV_MODE_TRACE_PC;
  342. else if (arg == KCOV_TRACE_CMP)
  343. #ifdef CONFIG_KCOV_ENABLE_COMPARISONS
  344. kcov->mode = KCOV_MODE_TRACE_CMP;
  345. #else
  346. return -ENOTSUPP;
  347. #endif
  348. else
  349. return -EINVAL;
  350. kcov_fault_in_area(kcov);
  351. /* Cache in task struct for performance. */
  352. t->kcov_size = kcov->size;
  353. t->kcov_area = kcov->area;
  354. /* See comment in check_kcov_mode(). */
  355. barrier();
  356. WRITE_ONCE(t->kcov_mode, kcov->mode);
  357. t->kcov = kcov;
  358. kcov->t = t;
  359. /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
  360. kcov_get(kcov);
  361. return 0;
  362. case KCOV_DISABLE:
  363. /* Disable coverage for the current task. */
  364. unused = arg;
  365. if (unused != 0 || current->kcov != kcov)
  366. return -EINVAL;
  367. t = current;
  368. if (WARN_ON(kcov->t != t))
  369. return -EINVAL;
  370. kcov_task_init(t);
  371. kcov->t = NULL;
  372. kcov->mode = KCOV_MODE_INIT;
  373. kcov_put(kcov);
  374. return 0;
  375. default:
  376. return -ENOTTY;
  377. }
  378. }
  379. static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  380. {
  381. struct kcov *kcov;
  382. int res;
  383. kcov = filep->private_data;
  384. spin_lock(&kcov->lock);
  385. res = kcov_ioctl_locked(kcov, cmd, arg);
  386. spin_unlock(&kcov->lock);
  387. return res;
  388. }
  389. static const struct file_operations kcov_fops = {
  390. .open = kcov_open,
  391. .unlocked_ioctl = kcov_ioctl,
  392. .compat_ioctl = kcov_ioctl,
  393. .mmap = kcov_mmap,
  394. .release = kcov_close,
  395. };
  396. static int __init kcov_init(void)
  397. {
  398. /*
  399. * The kcov debugfs file won't ever get removed and thus,
  400. * there is no need to protect it against removal races. The
  401. * use of debugfs_create_file_unsafe() is actually safe here.
  402. */
  403. if (!debugfs_create_file_unsafe("kcov", 0600, NULL, NULL, &kcov_fops)) {
  404. pr_err("failed to create kcov in debugfs\n");
  405. return -ENOMEM;
  406. }
  407. return 0;
  408. }
  409. device_initcall(kcov_init);