• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define pr_fmt(fmt) "kcov: " fmt
2 
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 
24 /* Number of 64-bit words written per one comparison: */
25 #define KCOV_WORDS_PER_CMP 4
26 
27 /*
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)
33  *  - 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).
39  */
40 struct 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 
check_kcov_mode(enum kcov_mode needed_mode,struct task_struct * t)58 static 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 
canonicalize_ip(unsigned long ip)80 static unsigned long canonicalize_ip(unsigned long ip)
81 {
82 #ifdef CONFIG_RANDOMIZE_BASE
83 	ip -= kaslr_offset();
84 #endif
85 	return ip;
86 }
87 
88 /*
89  * Entry point from instrumented code.
90  * This is called once per basic-block/edge.
91  */
__sanitizer_cov_trace_pc(void)92 void notrace __sanitizer_cov_trace_pc(void)
93 {
94 	struct task_struct *t;
95 	unsigned long *area;
96 	unsigned long ip = canonicalize_ip(_RET_IP_);
97 	unsigned long pos;
98 
99 	t = current;
100 	if (!check_kcov_mode(KCOV_MODE_TRACE_PC, t))
101 		return;
102 
103 	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);
109 	}
110 }
111 EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
112 
113 #ifdef CONFIG_KCOV_ENABLE_COMPARISONS
write_comp_data(u64 type,u64 arg1,u64 arg2,u64 ip)114 static 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 
__sanitizer_cov_trace_cmp1(u8 arg1,u8 arg2)147 void notrace __sanitizer_cov_trace_cmp1(u8 arg1, u8 arg2)
148 {
149 	write_comp_data(KCOV_CMP_SIZE(0), arg1, arg2, _RET_IP_);
150 }
151 EXPORT_SYMBOL(__sanitizer_cov_trace_cmp1);
152 
__sanitizer_cov_trace_cmp2(u16 arg1,u16 arg2)153 void notrace __sanitizer_cov_trace_cmp2(u16 arg1, u16 arg2)
154 {
155 	write_comp_data(KCOV_CMP_SIZE(1), arg1, arg2, _RET_IP_);
156 }
157 EXPORT_SYMBOL(__sanitizer_cov_trace_cmp2);
158 
__sanitizer_cov_trace_cmp4(u32 arg1,u32 arg2)159 void notrace __sanitizer_cov_trace_cmp4(u32 arg1, u32 arg2)
160 {
161 	write_comp_data(KCOV_CMP_SIZE(2), arg1, arg2, _RET_IP_);
162 }
163 EXPORT_SYMBOL(__sanitizer_cov_trace_cmp4);
164 
__sanitizer_cov_trace_cmp8(u64 arg1,u64 arg2)165 void notrace __sanitizer_cov_trace_cmp8(u64 arg1, u64 arg2)
166 {
167 	write_comp_data(KCOV_CMP_SIZE(3), arg1, arg2, _RET_IP_);
168 }
169 EXPORT_SYMBOL(__sanitizer_cov_trace_cmp8);
170 
__sanitizer_cov_trace_const_cmp1(u8 arg1,u8 arg2)171 void 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 }
176 EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp1);
177 
__sanitizer_cov_trace_const_cmp2(u16 arg1,u16 arg2)178 void 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 }
183 EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp2);
184 
__sanitizer_cov_trace_const_cmp4(u32 arg1,u32 arg2)185 void notrace __sanitizer_cov_trace_const_cmp4(u32 arg1, u32 arg2)
186 {
187 	write_comp_data(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
188 			_RET_IP_);
189 }
190 EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp4);
191 
__sanitizer_cov_trace_const_cmp8(u64 arg1,u64 arg2)192 void 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 }
197 EXPORT_SYMBOL(__sanitizer_cov_trace_const_cmp8);
198 
__sanitizer_cov_trace_switch(u64 val,u64 * cases)199 void 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 }
225 EXPORT_SYMBOL(__sanitizer_cov_trace_switch);
226 #endif /* ifdef CONFIG_KCOV_ENABLE_COMPARISONS */
227 
kcov_get(struct kcov * kcov)228 static void kcov_get(struct kcov *kcov)
229 {
230 	atomic_inc(&kcov->refcount);
231 }
232 
kcov_put(struct kcov * kcov)233 static 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 
kcov_task_init(struct task_struct * t)241 void 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 
kcov_task_exit(struct task_struct * t)249 void 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;
264 	kcov->mode = KCOV_MODE_INIT;
265 	spin_unlock(&kcov->lock);
266 	kcov_put(kcov);
267 }
268 
kcov_mmap(struct file * filep,struct vm_area_struct * vma)269 static 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);
283 	if (kcov->mode != KCOV_MODE_INIT || vma->vm_pgoff != 0 ||
284 	    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 	}
299 exit:
300 	spin_unlock(&kcov->lock);
301 	vfree(area);
302 	return res;
303 }
304 
kcov_open(struct inode * inode,struct file * filep)305 static 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;
312 	kcov->mode = KCOV_MODE_DISABLED;
313 	atomic_set(&kcov->refcount, 1);
314 	spin_lock_init(&kcov->lock);
315 	filep->private_data = kcov;
316 	return nonseekable_open(inode, filep);
317 }
318 
kcov_close(struct inode * inode,struct file * filep)319 static int kcov_close(struct inode *inode, struct file *filep)
320 {
321 	kcov_put(filep->private_data);
322 	return 0;
323 }
324 
kcov_ioctl_locked(struct kcov * kcov,unsigned int cmd,unsigned long arg)325 static 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;
348 		kcov->mode = KCOV_MODE_INIT;
349 		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 		 */
358 		if (kcov->mode != KCOV_MODE_INIT || !kcov->area)
359 			return -EINVAL;
360 		if (kcov->t != NULL)
361 			return -EBUSY;
362 		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;
372 		t = current;
373 		/* Cache in task struct for performance. */
374 		t->kcov_size = kcov->size;
375 		t->kcov_area = kcov->area;
376 		/* See comment in check_kcov_mode(). */
377 		barrier();
378 		WRITE_ONCE(t->kcov_mode, kcov->mode);
379 		t->kcov = kcov;
380 		kcov->t = t;
381 		/* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
382 		kcov_get(kcov);
383 		return 0;
384 	case KCOV_DISABLE:
385 		/* Disable coverage for the current task. */
386 		unused = arg;
387 		if (unused != 0 || current->kcov != kcov)
388 			return -EINVAL;
389 		t = current;
390 		if (WARN_ON(kcov->t != t))
391 			return -EINVAL;
392 		kcov_task_init(t);
393 		kcov->t = NULL;
394 		kcov->mode = KCOV_MODE_INIT;
395 		kcov_put(kcov);
396 		return 0;
397 	default:
398 		return -ENOTTY;
399 	}
400 }
401 
kcov_ioctl(struct file * filep,unsigned int cmd,unsigned long arg)402 static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
403 {
404 	struct kcov *kcov;
405 	int res;
406 
407 	kcov = filep->private_data;
408 	spin_lock(&kcov->lock);
409 	res = kcov_ioctl_locked(kcov, cmd, arg);
410 	spin_unlock(&kcov->lock);
411 	return res;
412 }
413 
414 static const struct file_operations kcov_fops = {
415 	.open		= kcov_open,
416 	.unlocked_ioctl	= kcov_ioctl,
417 	.compat_ioctl	= kcov_ioctl,
418 	.mmap		= kcov_mmap,
419 	.release        = kcov_close,
420 };
421 
kcov_init(void)422 static int __init kcov_init(void)
423 {
424 	if (!debugfs_create_file("kcov", 0600, NULL, NULL, &kcov_fops)) {
425 		pr_err("failed to create kcov in debugfs\n");
426 		return -ENOMEM;
427 	}
428 	return 0;
429 }
430 
431 device_initcall(kcov_init);
432