kcov.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t)
  56. {
  57. enum kcov_mode 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 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 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. t->kcov_mode = KCOV_MODE_DISABLED;
  217. t->kcov_size = 0;
  218. t->kcov_area = NULL;
  219. t->kcov = NULL;
  220. }
  221. void kcov_task_exit(struct task_struct *t)
  222. {
  223. struct kcov *kcov;
  224. kcov = t->kcov;
  225. if (kcov == NULL)
  226. return;
  227. spin_lock(&kcov->lock);
  228. if (WARN_ON(kcov->t != t)) {
  229. spin_unlock(&kcov->lock);
  230. return;
  231. }
  232. /* Just to not leave dangling references behind. */
  233. kcov_task_init(t);
  234. kcov->t = NULL;
  235. kcov->mode = KCOV_MODE_INIT;
  236. spin_unlock(&kcov->lock);
  237. kcov_put(kcov);
  238. }
  239. static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
  240. {
  241. int res = 0;
  242. void *area;
  243. struct kcov *kcov = vma->vm_file->private_data;
  244. unsigned long size, off;
  245. struct page *page;
  246. area = vmalloc_user(vma->vm_end - vma->vm_start);
  247. if (!area)
  248. return -ENOMEM;
  249. spin_lock(&kcov->lock);
  250. size = kcov->size * sizeof(unsigned long);
  251. if (kcov->mode != KCOV_MODE_INIT || vma->vm_pgoff != 0 ||
  252. vma->vm_end - vma->vm_start != size) {
  253. res = -EINVAL;
  254. goto exit;
  255. }
  256. if (!kcov->area) {
  257. kcov->area = area;
  258. vma->vm_flags |= VM_DONTEXPAND;
  259. spin_unlock(&kcov->lock);
  260. for (off = 0; off < size; off += PAGE_SIZE) {
  261. page = vmalloc_to_page(kcov->area + off);
  262. if (vm_insert_page(vma, vma->vm_start + off, page))
  263. WARN_ONCE(1, "vm_insert_page() failed");
  264. }
  265. return 0;
  266. }
  267. exit:
  268. spin_unlock(&kcov->lock);
  269. vfree(area);
  270. return res;
  271. }
  272. static int kcov_open(struct inode *inode, struct file *filep)
  273. {
  274. struct kcov *kcov;
  275. kcov = kzalloc(sizeof(*kcov), GFP_KERNEL);
  276. if (!kcov)
  277. return -ENOMEM;
  278. kcov->mode = KCOV_MODE_DISABLED;
  279. atomic_set(&kcov->refcount, 1);
  280. spin_lock_init(&kcov->lock);
  281. filep->private_data = kcov;
  282. return nonseekable_open(inode, filep);
  283. }
  284. static int kcov_close(struct inode *inode, struct file *filep)
  285. {
  286. kcov_put(filep->private_data);
  287. return 0;
  288. }
  289. static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
  290. unsigned long arg)
  291. {
  292. struct task_struct *t;
  293. unsigned long size, unused;
  294. switch (cmd) {
  295. case KCOV_INIT_TRACE:
  296. /*
  297. * Enable kcov in trace mode and setup buffer size.
  298. * Must happen before anything else.
  299. */
  300. if (kcov->mode != KCOV_MODE_DISABLED)
  301. return -EBUSY;
  302. /*
  303. * Size must be at least 2 to hold current position and one PC.
  304. * Later we allocate size * sizeof(unsigned long) memory,
  305. * that must not overflow.
  306. */
  307. size = arg;
  308. if (size < 2 || size > INT_MAX / sizeof(unsigned long))
  309. return -EINVAL;
  310. kcov->size = size;
  311. kcov->mode = KCOV_MODE_INIT;
  312. return 0;
  313. case KCOV_ENABLE:
  314. /*
  315. * Enable coverage for the current task.
  316. * At this point user must have been enabled trace mode,
  317. * and mmapped the file. Coverage collection is disabled only
  318. * at task exit or voluntary by KCOV_DISABLE. After that it can
  319. * be enabled for another task.
  320. */
  321. if (kcov->mode != KCOV_MODE_INIT || !kcov->area)
  322. return -EINVAL;
  323. t = current;
  324. if (kcov->t != NULL || t->kcov != NULL)
  325. return -EBUSY;
  326. if (arg == KCOV_TRACE_PC)
  327. kcov->mode = KCOV_MODE_TRACE_PC;
  328. else if (arg == KCOV_TRACE_CMP)
  329. #ifdef CONFIG_KCOV_ENABLE_COMPARISONS
  330. kcov->mode = KCOV_MODE_TRACE_CMP;
  331. #else
  332. return -ENOTSUPP;
  333. #endif
  334. else
  335. return -EINVAL;
  336. /* Cache in task struct for performance. */
  337. t->kcov_size = kcov->size;
  338. t->kcov_area = kcov->area;
  339. /* See comment in check_kcov_mode(). */
  340. barrier();
  341. WRITE_ONCE(t->kcov_mode, kcov->mode);
  342. t->kcov = kcov;
  343. kcov->t = t;
  344. /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
  345. kcov_get(kcov);
  346. return 0;
  347. case KCOV_DISABLE:
  348. /* Disable coverage for the current task. */
  349. unused = arg;
  350. if (unused != 0 || current->kcov != kcov)
  351. return -EINVAL;
  352. t = current;
  353. if (WARN_ON(kcov->t != t))
  354. return -EINVAL;
  355. kcov_task_init(t);
  356. kcov->t = NULL;
  357. kcov->mode = KCOV_MODE_INIT;
  358. kcov_put(kcov);
  359. return 0;
  360. default:
  361. return -ENOTTY;
  362. }
  363. }
  364. static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  365. {
  366. struct kcov *kcov;
  367. int res;
  368. kcov = filep->private_data;
  369. spin_lock(&kcov->lock);
  370. res = kcov_ioctl_locked(kcov, cmd, arg);
  371. spin_unlock(&kcov->lock);
  372. return res;
  373. }
  374. static const struct file_operations kcov_fops = {
  375. .open = kcov_open,
  376. .unlocked_ioctl = kcov_ioctl,
  377. .compat_ioctl = kcov_ioctl,
  378. .mmap = kcov_mmap,
  379. .release = kcov_close,
  380. };
  381. static int __init kcov_init(void)
  382. {
  383. /*
  384. * The kcov debugfs file won't ever get removed and thus,
  385. * there is no need to protect it against removal races. The
  386. * use of debugfs_create_file_unsafe() is actually safe here.
  387. */
  388. if (!debugfs_create_file_unsafe("kcov", 0600, NULL, NULL, &kcov_fops)) {
  389. pr_err("failed to create kcov in debugfs\n");
  390. return -ENOMEM;
  391. }
  392. return 0;
  393. }
  394. device_initcall(kcov_init);