aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/subr_coverage.c
blob: e38f3b36e78598675c0881b755741906c5a9e175 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2018 The FreeBSD Foundation. All rights reserved.
 * Copyright (C) 2018, 2019 Andrew Turner
 *
 * This software was developed by Mitchell Horne under sponsorship of
 * the FreeBSD Foundation.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/coverage.h>

#include <machine/atomic.h>

void __sanitizer_cov_trace_pc(void);
void __sanitizer_cov_trace_cmp1(uint8_t, uint8_t);
void __sanitizer_cov_trace_cmp2(uint16_t, uint16_t);
void __sanitizer_cov_trace_cmp4(uint32_t, uint32_t);
void __sanitizer_cov_trace_cmp8(uint64_t, uint64_t);
void __sanitizer_cov_trace_const_cmp1(uint8_t, uint8_t);
void __sanitizer_cov_trace_const_cmp2(uint16_t, uint16_t);
void __sanitizer_cov_trace_const_cmp4(uint32_t, uint32_t);
void __sanitizer_cov_trace_const_cmp8(uint64_t, uint64_t);
void __sanitizer_cov_trace_switch(uint64_t, uint64_t *);

static cov_trace_pc_t cov_trace_pc;
static cov_trace_cmp_t cov_trace_cmp;

void
cov_register_pc(cov_trace_pc_t trace_pc)
{

	atomic_store_ptr(&cov_trace_pc, trace_pc);
}

void
cov_unregister_pc(void)
{

	atomic_store_ptr(&cov_trace_pc, NULL);
}

void
cov_register_cmp(cov_trace_cmp_t trace_cmp)
{

	atomic_store_ptr(&cov_trace_cmp, trace_cmp);
}

void
cov_unregister_cmp(void)
{

	atomic_store_ptr(&cov_trace_cmp, NULL);
}

/*
 * Main entry point. A call to this function will be inserted
 * at every edge, and if coverage is enabled for the thread
 * this function will add the PC to the buffer.
 */
void
__sanitizer_cov_trace_pc(void)
{
	cov_trace_pc_t trace_pc;

	trace_pc = atomic_load_ptr(&cov_trace_pc);
	if (trace_pc != NULL)
		trace_pc((uint64_t)__builtin_return_address(0));
}

/*
 * Comparison entry points. When the kernel performs a comparison
 * operation the compiler inserts a call to one of the following
 * functions to record the operation.
 */
void
__sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(0), arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

void
__sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(1), arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

void
__sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(2), arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

void
__sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(3), arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

void
__sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(0) | COV_CMP_CONST, arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

void
__sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(1) | COV_CMP_CONST, arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

void
__sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(2) | COV_CMP_CONST, arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

void
__sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2)
{
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp != NULL)
		trace_cmp(COV_CMP_SIZE(3) | COV_CMP_CONST, arg1, arg2,
		    (uint64_t)__builtin_return_address(0));
}

/*
 * val is the switch operand
 * cases[0] is the number of case constants
 * cases[1] is the size of val in bits
 * cases[2..n] are the case constants
 */
void
__sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
{
	uint64_t i, count, ret, type;
	cov_trace_cmp_t trace_cmp;

	trace_cmp = atomic_load_ptr(&cov_trace_cmp);
	if (trace_cmp == NULL)
		return;

	count = cases[0];
	ret = (uint64_t)__builtin_return_address(0);

	switch (cases[1]) {
	case 8:
		type = COV_CMP_SIZE(0);
		break;
	case 16:
		type = COV_CMP_SIZE(1);
		break;
	case 32:
		type = COV_CMP_SIZE(2);
		break;
	case 64:
		type = COV_CMP_SIZE(3);
		break;
	default:
		return;
	}

	val |= COV_CMP_CONST;

	for (i = 0; i < count; i++)
		if (!trace_cmp(type, val, cases[i + 2], ret))
			return;
}