blob: f8f3f4cb9a2072d6b67c7d7325b1d020842e2110 [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
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -07004#include <linux/compiler.h>
5#include <linux/types.h>
6#include <linux/file.h>
7#include <linux/fs.h>
8#include <linux/mm.h>
9#include <linux/printk.h>
Kefeng Wang166ad0e2016-12-07 14:44:36 -080010#include <linux/sched.h>
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070011#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/vmalloc.h>
14#include <linux/debugfs.h>
15#include <linux/uaccess.h>
16#include <linux/kcov.h>
Alexander Popov11c63f42016-12-19 16:23:09 -080017#include <asm/setup.h>
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070018
19/*
20 * kcov descriptor (one per opened debugfs file).
21 * State transitions of the descriptor:
22 * - initial state after open()
23 * - then there must be a single ioctl(KCOV_INIT_TRACE) call
24 * - then, mmap() call (several calls are allowed but not useful)
25 * - then, repeated enable/disable for a task (only one task a time allowed)
26 */
27struct kcov {
28 /*
29 * Reference counter. We keep one for:
30 * - opened file descriptor
31 * - task with enabled coverage (we can't unwire it from another task)
32 */
33 atomic_t refcount;
34 /* The lock protects mode, size, area and t. */
35 spinlock_t lock;
36 enum kcov_mode mode;
37 /* Size of arena (in long's for KCOV_MODE_TRACE). */
38 unsigned size;
39 /* Coverage buffer shared with user space. */
40 void *area;
41 /* Task for which we collect coverage, or NULL. */
42 struct task_struct *t;
43};
44
45/*
46 * Entry point from instrumented code.
47 * This is called once per basic-block/edge.
48 */
James Morsebdab42d2016-04-28 16:18:52 -070049void notrace __sanitizer_cov_trace_pc(void)
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070050{
51 struct task_struct *t;
52 enum kcov_mode mode;
53
54 t = current;
55 /*
56 * We are interested in code coverage as a function of a syscall inputs,
57 * so we ignore code executed in interrupts.
Andrey Konovalovb274c0b2016-10-27 17:46:21 -070058 * The checks for whether we are in an interrupt are open-coded, because
59 * 1. We can't use in_interrupt() here, since it also returns true
60 * when we are inside local_bh_disable() section.
61 * 2. We don't want to use (in_irq() | in_serving_softirq() | in_nmi()),
62 * since that leads to slower generated code (three separate tests,
63 * one for each of the flags).
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070064 */
Andrey Konovalovb274c0b2016-10-27 17:46:21 -070065 if (!t || (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET
66 | NMI_MASK)))
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070067 return;
68 mode = READ_ONCE(t->kcov_mode);
69 if (mode == KCOV_MODE_TRACE) {
70 unsigned long *area;
71 unsigned long pos;
Alexander Popov11c63f42016-12-19 16:23:09 -080072 unsigned long ip = _RET_IP_;
73
74#ifdef CONFIG_RANDOMIZE_BASE
75 ip -= kaslr_offset();
76#endif
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070077
78 /*
79 * There is some code that runs in interrupts but for which
80 * in_interrupt() returns false (e.g. preempt_schedule_irq()).
81 * READ_ONCE()/barrier() effectively provides load-acquire wrt
82 * interrupts, there are paired barrier()/WRITE_ONCE() in
83 * kcov_ioctl_locked().
84 */
85 barrier();
86 area = t->kcov_area;
87 /* The first word is number of subsequent PCs. */
88 pos = READ_ONCE(area[0]) + 1;
89 if (likely(pos < t->kcov_size)) {
Alexander Popov11c63f42016-12-19 16:23:09 -080090 area[pos] = ip;
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -070091 WRITE_ONCE(area[0], pos);
92 }
93 }
94}
95EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
96
97static void kcov_get(struct kcov *kcov)
98{
99 atomic_inc(&kcov->refcount);
100}
101
102static void kcov_put(struct kcov *kcov)
103{
104 if (atomic_dec_and_test(&kcov->refcount)) {
105 vfree(kcov->area);
106 kfree(kcov);
107 }
108}
109
110void kcov_task_init(struct task_struct *t)
111{
112 t->kcov_mode = KCOV_MODE_DISABLED;
113 t->kcov_size = 0;
114 t->kcov_area = NULL;
115 t->kcov = NULL;
116}
117
118void kcov_task_exit(struct task_struct *t)
119{
120 struct kcov *kcov;
121
122 kcov = t->kcov;
123 if (kcov == NULL)
124 return;
125 spin_lock(&kcov->lock);
126 if (WARN_ON(kcov->t != t)) {
127 spin_unlock(&kcov->lock);
128 return;
129 }
130 /* Just to not leave dangling references behind. */
131 kcov_task_init(t);
132 kcov->t = NULL;
133 spin_unlock(&kcov->lock);
134 kcov_put(kcov);
135}
136
137static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
138{
139 int res = 0;
140 void *area;
141 struct kcov *kcov = vma->vm_file->private_data;
142 unsigned long size, off;
143 struct page *page;
144
145 area = vmalloc_user(vma->vm_end - vma->vm_start);
146 if (!area)
147 return -ENOMEM;
148
149 spin_lock(&kcov->lock);
150 size = kcov->size * sizeof(unsigned long);
151 if (kcov->mode == KCOV_MODE_DISABLED || vma->vm_pgoff != 0 ||
152 vma->vm_end - vma->vm_start != size) {
153 res = -EINVAL;
154 goto exit;
155 }
156 if (!kcov->area) {
157 kcov->area = area;
158 vma->vm_flags |= VM_DONTEXPAND;
159 spin_unlock(&kcov->lock);
160 for (off = 0; off < size; off += PAGE_SIZE) {
161 page = vmalloc_to_page(kcov->area + off);
162 if (vm_insert_page(vma, vma->vm_start + off, page))
163 WARN_ONCE(1, "vm_insert_page() failed");
164 }
165 return 0;
166 }
167exit:
168 spin_unlock(&kcov->lock);
169 vfree(area);
170 return res;
171}
172
173static int kcov_open(struct inode *inode, struct file *filep)
174{
175 struct kcov *kcov;
176
177 kcov = kzalloc(sizeof(*kcov), GFP_KERNEL);
178 if (!kcov)
179 return -ENOMEM;
180 atomic_set(&kcov->refcount, 1);
181 spin_lock_init(&kcov->lock);
182 filep->private_data = kcov;
183 return nonseekable_open(inode, filep);
184}
185
186static int kcov_close(struct inode *inode, struct file *filep)
187{
188 kcov_put(filep->private_data);
189 return 0;
190}
191
192static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
193 unsigned long arg)
194{
195 struct task_struct *t;
196 unsigned long size, unused;
197
198 switch (cmd) {
199 case KCOV_INIT_TRACE:
200 /*
201 * Enable kcov in trace mode and setup buffer size.
202 * Must happen before anything else.
203 */
204 if (kcov->mode != KCOV_MODE_DISABLED)
205 return -EBUSY;
206 /*
207 * Size must be at least 2 to hold current position and one PC.
208 * Later we allocate size * sizeof(unsigned long) memory,
209 * that must not overflow.
210 */
211 size = arg;
212 if (size < 2 || size > INT_MAX / sizeof(unsigned long))
213 return -EINVAL;
214 kcov->size = size;
215 kcov->mode = KCOV_MODE_TRACE;
216 return 0;
217 case KCOV_ENABLE:
218 /*
219 * Enable coverage for the current task.
220 * At this point user must have been enabled trace mode,
221 * and mmapped the file. Coverage collection is disabled only
222 * at task exit or voluntary by KCOV_DISABLE. After that it can
223 * be enabled for another task.
224 */
225 unused = arg;
226 if (unused != 0 || kcov->mode == KCOV_MODE_DISABLED ||
227 kcov->area == NULL)
228 return -EINVAL;
229 if (kcov->t != NULL)
230 return -EBUSY;
231 t = current;
232 /* Cache in task struct for performance. */
233 t->kcov_size = kcov->size;
234 t->kcov_area = kcov->area;
235 /* See comment in __sanitizer_cov_trace_pc(). */
236 barrier();
237 WRITE_ONCE(t->kcov_mode, kcov->mode);
238 t->kcov = kcov;
239 kcov->t = t;
240 /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
241 kcov_get(kcov);
242 return 0;
243 case KCOV_DISABLE:
244 /* Disable coverage for the current task. */
245 unused = arg;
246 if (unused != 0 || current->kcov != kcov)
247 return -EINVAL;
248 t = current;
249 if (WARN_ON(kcov->t != t))
250 return -EINVAL;
251 kcov_task_init(t);
252 kcov->t = NULL;
253 kcov_put(kcov);
254 return 0;
255 default:
256 return -ENOTTY;
257 }
258}
259
260static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
261{
262 struct kcov *kcov;
263 int res;
264
265 kcov = filep->private_data;
266 spin_lock(&kcov->lock);
267 res = kcov_ioctl_locked(kcov, cmd, arg);
268 spin_unlock(&kcov->lock);
269 return res;
270}
271
272static const struct file_operations kcov_fops = {
273 .open = kcov_open,
274 .unlocked_ioctl = kcov_ioctl,
275 .mmap = kcov_mmap,
276 .release = kcov_close,
277};
278
279static int __init kcov_init(void)
280{
Nicolai Stangedf4565f2016-05-24 14:05:05 +0200281 /*
282 * The kcov debugfs file won't ever get removed and thus,
283 * there is no need to protect it against removal races. The
284 * use of debugfs_create_file_unsafe() is actually safe here.
285 */
286 if (!debugfs_create_file_unsafe("kcov", 0600, NULL, NULL, &kcov_fops)) {
Dmitry Vyukov5c9a8752016-03-22 14:27:30 -0700287 pr_err("failed to create kcov in debugfs\n");
288 return -ENOMEM;
289 }
290 return 0;
291}
292
293device_initcall(kcov_init);