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