subr_kcov.c revision 1.8.2.2 1 1.8.2.2 christos /* $NetBSD: subr_kcov.c,v 1.8.2.2 2019/06/10 22:09:03 christos Exp $ */
2 1.8.2.2 christos
3 1.8.2.2 christos /*
4 1.8.2.2 christos * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.8.2.2 christos * All rights reserved.
6 1.8.2.2 christos *
7 1.8.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.8.2.2 christos * by Siddharth Muralee.
9 1.8.2.2 christos *
10 1.8.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.8.2.2 christos * modification, are permitted provided that the following conditions
12 1.8.2.2 christos * are met:
13 1.8.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.8.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.8.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.8.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.8.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.8.2.2 christos *
19 1.8.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.8.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.8.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.8.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.8.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.8.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.8.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.8.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.8.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.8.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.8.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.8.2.2 christos */
31 1.8.2.2 christos
32 1.8.2.2 christos #include <sys/cdefs.h>
33 1.8.2.2 christos
34 1.8.2.2 christos #include <sys/module.h>
35 1.8.2.2 christos #include <sys/param.h>
36 1.8.2.2 christos #include <sys/systm.h>
37 1.8.2.2 christos #include <sys/kernel.h>
38 1.8.2.2 christos
39 1.8.2.2 christos #include <sys/conf.h>
40 1.8.2.2 christos #include <sys/condvar.h>
41 1.8.2.2 christos #include <sys/file.h>
42 1.8.2.2 christos #include <sys/filedesc.h>
43 1.8.2.2 christos #include <sys/kmem.h>
44 1.8.2.2 christos #include <sys/mman.h>
45 1.8.2.2 christos #include <sys/mutex.h>
46 1.8.2.2 christos #include <sys/queue.h>
47 1.8.2.2 christos
48 1.8.2.2 christos #include <uvm/uvm_extern.h>
49 1.8.2.2 christos #include <sys/kcov.h>
50 1.8.2.2 christos
51 1.8.2.2 christos #define KCOV_BUF_MAX_ENTRIES (256 << 10)
52 1.8.2.2 christos
53 1.8.2.2 christos #define KCOV_CMP_CONST 1
54 1.8.2.2 christos #define KCOV_CMP_SIZE(x) ((x) << 1)
55 1.8.2.2 christos
56 1.8.2.2 christos static dev_type_open(kcov_open);
57 1.8.2.2 christos
58 1.8.2.2 christos const struct cdevsw kcov_cdevsw = {
59 1.8.2.2 christos .d_open = kcov_open,
60 1.8.2.2 christos .d_close = noclose,
61 1.8.2.2 christos .d_read = noread,
62 1.8.2.2 christos .d_write = nowrite,
63 1.8.2.2 christos .d_ioctl = noioctl,
64 1.8.2.2 christos .d_stop = nostop,
65 1.8.2.2 christos .d_tty = notty,
66 1.8.2.2 christos .d_poll = nopoll,
67 1.8.2.2 christos .d_mmap = nommap,
68 1.8.2.2 christos .d_kqfilter = nokqfilter,
69 1.8.2.2 christos .d_discard = nodiscard,
70 1.8.2.2 christos .d_flag = D_OTHER | D_MPSAFE
71 1.8.2.2 christos };
72 1.8.2.2 christos
73 1.8.2.2 christos static int kcov_fops_ioctl(file_t *, u_long, void *);
74 1.8.2.2 christos static int kcov_fops_close(file_t *);
75 1.8.2.2 christos static int kcov_fops_mmap(file_t *, off_t *, size_t, int, int *, int *,
76 1.8.2.2 christos struct uvm_object **, int *);
77 1.8.2.2 christos
78 1.8.2.2 christos const struct fileops kcov_fileops = {
79 1.8.2.2 christos .fo_read = fbadop_read,
80 1.8.2.2 christos .fo_write = fbadop_write,
81 1.8.2.2 christos .fo_ioctl = kcov_fops_ioctl,
82 1.8.2.2 christos .fo_fcntl = fnullop_fcntl,
83 1.8.2.2 christos .fo_poll = fnullop_poll,
84 1.8.2.2 christos .fo_stat = fbadop_stat,
85 1.8.2.2 christos .fo_close = kcov_fops_close,
86 1.8.2.2 christos .fo_kqfilter = fnullop_kqfilter,
87 1.8.2.2 christos .fo_restart = fnullop_restart,
88 1.8.2.2 christos .fo_mmap = kcov_fops_mmap,
89 1.8.2.2 christos };
90 1.8.2.2 christos
91 1.8.2.2 christos /*
92 1.8.2.2 christos * The KCOV descriptors (KD) are allocated during open(), and are associated
93 1.8.2.2 christos * with a file descriptor.
94 1.8.2.2 christos *
95 1.8.2.2 christos * An LWP can 'enable' a KD. When this happens, this LWP becomes the owner of
96 1.8.2.2 christos * the KD, and no LWP can 'disable' this KD except the owner.
97 1.8.2.2 christos *
98 1.8.2.2 christos * A KD is freed when its file descriptor is closed _iff_ the KD is not active
99 1.8.2.2 christos * on an LWP. If it is, we ask the LWP to free it when it exits.
100 1.8.2.2 christos *
101 1.8.2.2 christos * The buffers mmapped are in a dedicated uobj, therefore there is no risk
102 1.8.2.2 christos * that the kernel frees a buffer still mmapped in a process: the uobj
103 1.8.2.2 christos * refcount will be non-zero, so the backing is not freed until an munmap
104 1.8.2.2 christos * occurs on said process.
105 1.8.2.2 christos */
106 1.8.2.2 christos
107 1.8.2.2 christos typedef struct kcov_desc {
108 1.8.2.2 christos kmutex_t lock;
109 1.8.2.2 christos kcov_int_t *buf;
110 1.8.2.2 christos struct uvm_object *uobj;
111 1.8.2.2 christos size_t bufnent;
112 1.8.2.2 christos size_t bufsize;
113 1.8.2.2 christos int mode;
114 1.8.2.2 christos bool enabled;
115 1.8.2.2 christos bool lwpfree;
116 1.8.2.2 christos } kcov_t;
117 1.8.2.2 christos
118 1.8.2.2 christos static specificdata_key_t kcov_lwp_key;
119 1.8.2.2 christos
120 1.8.2.2 christos static void
121 1.8.2.2 christos kcov_lock(kcov_t *kd)
122 1.8.2.2 christos {
123 1.8.2.2 christos
124 1.8.2.2 christos mutex_enter(&kd->lock);
125 1.8.2.2 christos }
126 1.8.2.2 christos
127 1.8.2.2 christos static void
128 1.8.2.2 christos kcov_unlock(kcov_t *kd)
129 1.8.2.2 christos {
130 1.8.2.2 christos
131 1.8.2.2 christos mutex_exit(&kd->lock);
132 1.8.2.2 christos }
133 1.8.2.2 christos
134 1.8.2.2 christos static void
135 1.8.2.2 christos kcov_free(kcov_t *kd)
136 1.8.2.2 christos {
137 1.8.2.2 christos
138 1.8.2.2 christos KASSERT(kd != NULL);
139 1.8.2.2 christos if (kd->buf != NULL) {
140 1.8.2.2 christos uvm_deallocate(kernel_map, (vaddr_t)kd->buf, kd->bufsize);
141 1.8.2.2 christos }
142 1.8.2.2 christos mutex_destroy(&kd->lock);
143 1.8.2.2 christos kmem_free(kd, sizeof(*kd));
144 1.8.2.2 christos }
145 1.8.2.2 christos
146 1.8.2.2 christos static void
147 1.8.2.2 christos kcov_lwp_free(void *arg)
148 1.8.2.2 christos {
149 1.8.2.2 christos kcov_t *kd = (kcov_t *)arg;
150 1.8.2.2 christos
151 1.8.2.2 christos if (kd == NULL) {
152 1.8.2.2 christos return;
153 1.8.2.2 christos }
154 1.8.2.2 christos kcov_lock(kd);
155 1.8.2.2 christos kd->enabled = false;
156 1.8.2.2 christos kcov_unlock(kd);
157 1.8.2.2 christos if (kd->lwpfree) {
158 1.8.2.2 christos kcov_free(kd);
159 1.8.2.2 christos }
160 1.8.2.2 christos }
161 1.8.2.2 christos
162 1.8.2.2 christos static int
163 1.8.2.2 christos kcov_allocbuf(kcov_t *kd, uint64_t nent)
164 1.8.2.2 christos {
165 1.8.2.2 christos size_t size;
166 1.8.2.2 christos int error;
167 1.8.2.2 christos
168 1.8.2.2 christos if (nent < 2 || nent > KCOV_BUF_MAX_ENTRIES)
169 1.8.2.2 christos return EINVAL;
170 1.8.2.2 christos if (kd->buf != NULL)
171 1.8.2.2 christos return EEXIST;
172 1.8.2.2 christos
173 1.8.2.2 christos size = roundup(nent * KCOV_ENTRY_SIZE, PAGE_SIZE);
174 1.8.2.2 christos kd->bufnent = nent - 1;
175 1.8.2.2 christos kd->bufsize = size;
176 1.8.2.2 christos kd->uobj = uao_create(kd->bufsize, 0);
177 1.8.2.2 christos
178 1.8.2.2 christos /* Map the uobj into the kernel address space, as wired. */
179 1.8.2.2 christos kd->buf = NULL;
180 1.8.2.2 christos error = uvm_map(kernel_map, (vaddr_t *)&kd->buf, kd->bufsize, kd->uobj,
181 1.8.2.2 christos 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_SHARE,
182 1.8.2.2 christos UVM_ADV_RANDOM, 0));
183 1.8.2.2 christos if (error) {
184 1.8.2.2 christos uao_detach(kd->uobj);
185 1.8.2.2 christos return error;
186 1.8.2.2 christos }
187 1.8.2.2 christos error = uvm_map_pageable(kernel_map, (vaddr_t)kd->buf,
188 1.8.2.2 christos (vaddr_t)kd->buf + size, false, 0);
189 1.8.2.2 christos if (error) {
190 1.8.2.2 christos uvm_deallocate(kernel_map, (vaddr_t)kd->buf, size);
191 1.8.2.2 christos return error;
192 1.8.2.2 christos }
193 1.8.2.2 christos
194 1.8.2.2 christos return 0;
195 1.8.2.2 christos }
196 1.8.2.2 christos
197 1.8.2.2 christos /* -------------------------------------------------------------------------- */
198 1.8.2.2 christos
199 1.8.2.2 christos static int
200 1.8.2.2 christos kcov_open(dev_t dev, int flag, int mode, struct lwp *l)
201 1.8.2.2 christos {
202 1.8.2.2 christos struct file *fp;
203 1.8.2.2 christos int error, fd;
204 1.8.2.2 christos kcov_t *kd;
205 1.8.2.2 christos
206 1.8.2.2 christos error = fd_allocfile(&fp, &fd);
207 1.8.2.2 christos if (error)
208 1.8.2.2 christos return error;
209 1.8.2.2 christos
210 1.8.2.2 christos kd = kmem_zalloc(sizeof(*kd), KM_SLEEP);
211 1.8.2.2 christos mutex_init(&kd->lock, MUTEX_DEFAULT, IPL_NONE);
212 1.8.2.2 christos
213 1.8.2.2 christos return fd_clone(fp, fd, flag, &kcov_fileops, kd);
214 1.8.2.2 christos }
215 1.8.2.2 christos
216 1.8.2.2 christos static int
217 1.8.2.2 christos kcov_fops_close(file_t *fp)
218 1.8.2.2 christos {
219 1.8.2.2 christos kcov_t *kd = fp->f_data;
220 1.8.2.2 christos
221 1.8.2.2 christos kcov_lock(kd);
222 1.8.2.2 christos if (kd->enabled) {
223 1.8.2.2 christos kd->lwpfree = true;
224 1.8.2.2 christos kcov_unlock(kd);
225 1.8.2.2 christos } else {
226 1.8.2.2 christos kcov_unlock(kd);
227 1.8.2.2 christos kcov_free(kd);
228 1.8.2.2 christos }
229 1.8.2.2 christos fp->f_data = NULL;
230 1.8.2.2 christos
231 1.8.2.2 christos return 0;
232 1.8.2.2 christos }
233 1.8.2.2 christos
234 1.8.2.2 christos static int
235 1.8.2.2 christos kcov_fops_ioctl(file_t *fp, u_long cmd, void *addr)
236 1.8.2.2 christos {
237 1.8.2.2 christos int error = 0;
238 1.8.2.2 christos int mode;
239 1.8.2.2 christos kcov_t *kd;
240 1.8.2.2 christos
241 1.8.2.2 christos kd = fp->f_data;
242 1.8.2.2 christos if (kd == NULL)
243 1.8.2.2 christos return ENXIO;
244 1.8.2.2 christos kcov_lock(kd);
245 1.8.2.2 christos
246 1.8.2.2 christos switch (cmd) {
247 1.8.2.2 christos case KCOV_IOC_SETBUFSIZE:
248 1.8.2.2 christos if (kd->enabled) {
249 1.8.2.2 christos error = EBUSY;
250 1.8.2.2 christos break;
251 1.8.2.2 christos }
252 1.8.2.2 christos error = kcov_allocbuf(kd, *((uint64_t *)addr));
253 1.8.2.2 christos break;
254 1.8.2.2 christos case KCOV_IOC_ENABLE:
255 1.8.2.2 christos if (kd->enabled) {
256 1.8.2.2 christos error = EBUSY;
257 1.8.2.2 christos break;
258 1.8.2.2 christos }
259 1.8.2.2 christos if (lwp_getspecific(kcov_lwp_key) != NULL) {
260 1.8.2.2 christos error = EBUSY;
261 1.8.2.2 christos break;
262 1.8.2.2 christos }
263 1.8.2.2 christos if (kd->buf == NULL) {
264 1.8.2.2 christos error = ENOBUFS;
265 1.8.2.2 christos break;
266 1.8.2.2 christos }
267 1.8.2.2 christos
268 1.8.2.2 christos mode = *((int *)addr);
269 1.8.2.2 christos switch (mode) {
270 1.8.2.2 christos case KCOV_MODE_NONE:
271 1.8.2.2 christos case KCOV_MODE_TRACE_PC:
272 1.8.2.2 christos case KCOV_MODE_TRACE_CMP:
273 1.8.2.2 christos kd->mode = mode;
274 1.8.2.2 christos break;
275 1.8.2.2 christos default:
276 1.8.2.2 christos error = EINVAL;
277 1.8.2.2 christos }
278 1.8.2.2 christos if (error)
279 1.8.2.2 christos break;
280 1.8.2.2 christos
281 1.8.2.2 christos lwp_setspecific(kcov_lwp_key, kd);
282 1.8.2.2 christos kd->enabled = true;
283 1.8.2.2 christos break;
284 1.8.2.2 christos case KCOV_IOC_DISABLE:
285 1.8.2.2 christos if (!kd->enabled) {
286 1.8.2.2 christos error = ENOENT;
287 1.8.2.2 christos break;
288 1.8.2.2 christos }
289 1.8.2.2 christos if (lwp_getspecific(kcov_lwp_key) != kd) {
290 1.8.2.2 christos error = ENOENT;
291 1.8.2.2 christos break;
292 1.8.2.2 christos }
293 1.8.2.2 christos lwp_setspecific(kcov_lwp_key, NULL);
294 1.8.2.2 christos kd->enabled = false;
295 1.8.2.2 christos break;
296 1.8.2.2 christos default:
297 1.8.2.2 christos error = EINVAL;
298 1.8.2.2 christos }
299 1.8.2.2 christos
300 1.8.2.2 christos kcov_unlock(kd);
301 1.8.2.2 christos return error;
302 1.8.2.2 christos }
303 1.8.2.2 christos
304 1.8.2.2 christos static int
305 1.8.2.2 christos kcov_fops_mmap(file_t *fp, off_t *offp, size_t size, int prot, int *flagsp,
306 1.8.2.2 christos int *advicep, struct uvm_object **uobjp, int *maxprotp)
307 1.8.2.2 christos {
308 1.8.2.2 christos off_t off = *offp;
309 1.8.2.2 christos kcov_t *kd;
310 1.8.2.2 christos int error = 0;
311 1.8.2.2 christos
312 1.8.2.2 christos if (prot & PROT_EXEC)
313 1.8.2.2 christos return EACCES;
314 1.8.2.2 christos if (off < 0)
315 1.8.2.2 christos return EINVAL;
316 1.8.2.2 christos if (size > KCOV_BUF_MAX_ENTRIES * KCOV_ENTRY_SIZE)
317 1.8.2.2 christos return EINVAL;
318 1.8.2.2 christos if (off > KCOV_BUF_MAX_ENTRIES * KCOV_ENTRY_SIZE)
319 1.8.2.2 christos return EINVAL;
320 1.8.2.2 christos
321 1.8.2.2 christos kd = fp->f_data;
322 1.8.2.2 christos if (kd == NULL)
323 1.8.2.2 christos return ENXIO;
324 1.8.2.2 christos kcov_lock(kd);
325 1.8.2.2 christos
326 1.8.2.2 christos if ((size + off) > kd->bufsize) {
327 1.8.2.2 christos error = ENOMEM;
328 1.8.2.2 christos goto out;
329 1.8.2.2 christos }
330 1.8.2.2 christos
331 1.8.2.2 christos uao_reference(kd->uobj);
332 1.8.2.2 christos
333 1.8.2.2 christos *uobjp = kd->uobj;
334 1.8.2.2 christos *maxprotp = prot;
335 1.8.2.2 christos *advicep = UVM_ADV_RANDOM;
336 1.8.2.2 christos
337 1.8.2.2 christos out:
338 1.8.2.2 christos kcov_unlock(kd);
339 1.8.2.2 christos return error;
340 1.8.2.2 christos }
341 1.8.2.2 christos
342 1.8.2.2 christos static inline bool
343 1.8.2.2 christos in_interrupt(void)
344 1.8.2.2 christos {
345 1.8.2.2 christos return curcpu()->ci_idepth >= 0;
346 1.8.2.2 christos }
347 1.8.2.2 christos
348 1.8.2.2 christos void __sanitizer_cov_trace_pc(void);
349 1.8.2.2 christos
350 1.8.2.2 christos void
351 1.8.2.2 christos __sanitizer_cov_trace_pc(void)
352 1.8.2.2 christos {
353 1.8.2.2 christos extern int cold;
354 1.8.2.2 christos uint64_t idx;
355 1.8.2.2 christos kcov_t *kd;
356 1.8.2.2 christos
357 1.8.2.2 christos if (__predict_false(cold)) {
358 1.8.2.2 christos /* Do not trace during boot. */
359 1.8.2.2 christos return;
360 1.8.2.2 christos }
361 1.8.2.2 christos
362 1.8.2.2 christos if (in_interrupt()) {
363 1.8.2.2 christos /* Do not trace in interrupts. */
364 1.8.2.2 christos return;
365 1.8.2.2 christos }
366 1.8.2.2 christos
367 1.8.2.2 christos kd = lwp_getspecific(kcov_lwp_key);
368 1.8.2.2 christos if (__predict_true(kd == NULL)) {
369 1.8.2.2 christos /* Not traced. */
370 1.8.2.2 christos return;
371 1.8.2.2 christos }
372 1.8.2.2 christos
373 1.8.2.2 christos if (!kd->enabled) {
374 1.8.2.2 christos /* Tracing not enabled */
375 1.8.2.2 christos return;
376 1.8.2.2 christos }
377 1.8.2.2 christos
378 1.8.2.2 christos if (kd->mode != KCOV_MODE_TRACE_PC) {
379 1.8.2.2 christos /* PC tracing mode not enabled */
380 1.8.2.2 christos return;
381 1.8.2.2 christos }
382 1.8.2.2 christos
383 1.8.2.2 christos idx = kd->buf[0];
384 1.8.2.2 christos if (idx < kd->bufnent) {
385 1.8.2.2 christos kd->buf[idx+1] =
386 1.8.2.2 christos (intptr_t)__builtin_return_address(0);
387 1.8.2.2 christos kd->buf[0] = idx + 1;
388 1.8.2.2 christos }
389 1.8.2.2 christos }
390 1.8.2.2 christos
391 1.8.2.2 christos static void
392 1.8.2.2 christos trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, intptr_t pc)
393 1.8.2.2 christos {
394 1.8.2.2 christos extern int cold;
395 1.8.2.2 christos uint64_t idx;
396 1.8.2.2 christos kcov_t *kd;
397 1.8.2.2 christos
398 1.8.2.2 christos if (__predict_false(cold)) {
399 1.8.2.2 christos /* Do not trace during boot. */
400 1.8.2.2 christos return;
401 1.8.2.2 christos }
402 1.8.2.2 christos
403 1.8.2.2 christos if (in_interrupt()) {
404 1.8.2.2 christos /* Do not trace in interrupts. */
405 1.8.2.2 christos return;
406 1.8.2.2 christos }
407 1.8.2.2 christos
408 1.8.2.2 christos kd = lwp_getspecific(kcov_lwp_key);
409 1.8.2.2 christos if (__predict_true(kd == NULL)) {
410 1.8.2.2 christos /* Not traced. */
411 1.8.2.2 christos return;
412 1.8.2.2 christos }
413 1.8.2.2 christos
414 1.8.2.2 christos if (!kd->enabled) {
415 1.8.2.2 christos /* Tracing not enabled */
416 1.8.2.2 christos return;
417 1.8.2.2 christos }
418 1.8.2.2 christos
419 1.8.2.2 christos if (kd->mode != KCOV_MODE_TRACE_CMP) {
420 1.8.2.2 christos /* CMP tracing mode not enabled */
421 1.8.2.2 christos return;
422 1.8.2.2 christos }
423 1.8.2.2 christos
424 1.8.2.2 christos idx = kd->buf[0];
425 1.8.2.2 christos if ((idx * 4 + 4) <= kd->bufnent) {
426 1.8.2.2 christos kd->buf[idx * 4 + 1] = type;
427 1.8.2.2 christos kd->buf[idx * 4 + 2] = arg1;
428 1.8.2.2 christos kd->buf[idx * 4 + 3] = arg2;
429 1.8.2.2 christos kd->buf[idx * 4 + 4] = pc;
430 1.8.2.2 christos kd->buf[0] = idx + 1;
431 1.8.2.2 christos }
432 1.8.2.2 christos }
433 1.8.2.2 christos
434 1.8.2.2 christos void __sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2);
435 1.8.2.2 christos
436 1.8.2.2 christos void
437 1.8.2.2 christos __sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2)
438 1.8.2.2 christos {
439 1.8.2.2 christos
440 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(0), arg1, arg2,
441 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
442 1.8.2.2 christos }
443 1.8.2.2 christos
444 1.8.2.2 christos void __sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2);
445 1.8.2.2 christos
446 1.8.2.2 christos void
447 1.8.2.2 christos __sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2)
448 1.8.2.2 christos {
449 1.8.2.2 christos
450 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(1), arg1, arg2,
451 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
452 1.8.2.2 christos }
453 1.8.2.2 christos
454 1.8.2.2 christos void __sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2);
455 1.8.2.2 christos
456 1.8.2.2 christos void
457 1.8.2.2 christos __sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2)
458 1.8.2.2 christos {
459 1.8.2.2 christos
460 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(2), arg1, arg2,
461 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
462 1.8.2.2 christos }
463 1.8.2.2 christos
464 1.8.2.2 christos void __sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2);
465 1.8.2.2 christos
466 1.8.2.2 christos void
467 1.8.2.2 christos __sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2)
468 1.8.2.2 christos {
469 1.8.2.2 christos
470 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(3), arg1, arg2,
471 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
472 1.8.2.2 christos }
473 1.8.2.2 christos
474 1.8.2.2 christos void __sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2);
475 1.8.2.2 christos
476 1.8.2.2 christos void
477 1.8.2.2 christos __sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2)
478 1.8.2.2 christos {
479 1.8.2.2 christos
480 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2,
481 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
482 1.8.2.2 christos }
483 1.8.2.2 christos
484 1.8.2.2 christos void __sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2);
485 1.8.2.2 christos
486 1.8.2.2 christos void
487 1.8.2.2 christos __sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2)
488 1.8.2.2 christos {
489 1.8.2.2 christos
490 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2,
491 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
492 1.8.2.2 christos }
493 1.8.2.2 christos
494 1.8.2.2 christos void __sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2);
495 1.8.2.2 christos
496 1.8.2.2 christos void
497 1.8.2.2 christos __sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2)
498 1.8.2.2 christos {
499 1.8.2.2 christos
500 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
501 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
502 1.8.2.2 christos }
503 1.8.2.2 christos
504 1.8.2.2 christos void __sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2);
505 1.8.2.2 christos
506 1.8.2.2 christos void
507 1.8.2.2 christos __sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2)
508 1.8.2.2 christos {
509 1.8.2.2 christos
510 1.8.2.2 christos trace_cmp(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2,
511 1.8.2.2 christos (intptr_t)__builtin_return_address(0));
512 1.8.2.2 christos }
513 1.8.2.2 christos
514 1.8.2.2 christos void __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases);
515 1.8.2.2 christos
516 1.8.2.2 christos void
517 1.8.2.2 christos __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
518 1.8.2.2 christos {
519 1.8.2.2 christos uint64_t i, nbits, ncases, type;
520 1.8.2.2 christos intptr_t pc;
521 1.8.2.2 christos
522 1.8.2.2 christos pc = (intptr_t)__builtin_return_address(0);
523 1.8.2.2 christos ncases = cases[0];
524 1.8.2.2 christos nbits = cases[1];
525 1.8.2.2 christos
526 1.8.2.2 christos switch (nbits) {
527 1.8.2.2 christos case 8:
528 1.8.2.2 christos type = KCOV_CMP_SIZE(0);
529 1.8.2.2 christos break;
530 1.8.2.2 christos case 16:
531 1.8.2.2 christos type = KCOV_CMP_SIZE(1);
532 1.8.2.2 christos break;
533 1.8.2.2 christos case 32:
534 1.8.2.2 christos type = KCOV_CMP_SIZE(2);
535 1.8.2.2 christos break;
536 1.8.2.2 christos case 64:
537 1.8.2.2 christos type = KCOV_CMP_SIZE(3);
538 1.8.2.2 christos break;
539 1.8.2.2 christos default:
540 1.8.2.2 christos return;
541 1.8.2.2 christos }
542 1.8.2.2 christos type |= KCOV_CMP_CONST;
543 1.8.2.2 christos
544 1.8.2.2 christos for (i = 0; i < ncases; i++)
545 1.8.2.2 christos trace_cmp(type, cases[i + 2], val, pc);
546 1.8.2.2 christos }
547 1.8.2.2 christos
548 1.8.2.2 christos /* -------------------------------------------------------------------------- */
549 1.8.2.2 christos
550 1.8.2.2 christos MODULE(MODULE_CLASS_MISC, kcov, NULL);
551 1.8.2.2 christos
552 1.8.2.2 christos static void
553 1.8.2.2 christos kcov_init(void)
554 1.8.2.2 christos {
555 1.8.2.2 christos
556 1.8.2.2 christos lwp_specific_key_create(&kcov_lwp_key, kcov_lwp_free);
557 1.8.2.2 christos }
558 1.8.2.2 christos
559 1.8.2.2 christos static int
560 1.8.2.2 christos kcov_modcmd(modcmd_t cmd, void *arg)
561 1.8.2.2 christos {
562 1.8.2.2 christos
563 1.8.2.2 christos switch (cmd) {
564 1.8.2.2 christos case MODULE_CMD_INIT:
565 1.8.2.2 christos kcov_init();
566 1.8.2.2 christos return 0;
567 1.8.2.2 christos case MODULE_CMD_FINI:
568 1.8.2.2 christos return EINVAL;
569 1.8.2.2 christos default:
570 1.8.2.2 christos return ENOTTY;
571 1.8.2.2 christos }
572 1.8.2.2 christos }
573