xref: /freebsd/contrib/processor-trace/libipt/test/src/ptunit-insn_decoder.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*
2  * Copyright (c) 2019, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *  * Neither the name of Intel Corporation nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "ptunit.h"
30 
31 #include "pt_insn_decoder.h"
32 
33 #include "intel-pt.h"
34 
35 
36 /* A test fixture providing a decoder operating on a small buffer. */
37 struct test_fixture {
38 	/* The packet_decoder. */
39 	struct pt_insn_decoder decoder;
40 
41 	/* The configuration. */
42 	struct pt_config config;
43 
44 	/* The buffer it operates on. */
45 	uint8_t buffer[24];
46 
47 	/* The test fixture initialization and finalization functions. */
48 	struct ptunit_result (*init)(struct test_fixture *tfix);
49 	struct ptunit_result (*fini)(struct test_fixture *tfix);
50 };
51 
52 static struct ptunit_result tfix_init(struct test_fixture *tfix)
53 {
54 	struct pt_config *config;
55 	uint8_t *buffer;
56 	int errcode;
57 
58 	config = &tfix->config;
59 	buffer = tfix->buffer;
60 
61 	memset(buffer, 0, sizeof(tfix->buffer));
62 
63 	pt_config_init(config);
64 	config->begin = buffer;
65 	config->end = buffer + sizeof(tfix->buffer);
66 
67 	errcode = pt_insn_decoder_init(&tfix->decoder, config);
68 	ptu_int_eq(errcode, 0);
69 
70 	return ptu_passed();
71 }
72 
73 static struct ptunit_result decoder_init_null(void)
74 {
75 	struct pt_insn_decoder decoder;
76 	struct pt_config config;
77 	int errcode;
78 
79 	errcode = pt_insn_decoder_init(NULL, &config);
80 	ptu_int_eq(errcode, -pte_internal);
81 
82 	errcode = pt_insn_decoder_init(&decoder, NULL);
83 	ptu_int_eq(errcode, -pte_invalid);
84 
85 	return ptu_passed();
86 }
87 
88 static struct ptunit_result decoder_fini_null(void)
89 {
90 	pt_insn_decoder_fini(NULL);
91 
92 	return ptu_passed();
93 }
94 
95 static struct ptunit_result alloc_decoder_null(void)
96 {
97 	struct pt_insn_decoder *decoder;
98 
99 	decoder = pt_insn_alloc_decoder(NULL);
100 	ptu_null(decoder);
101 
102 	return ptu_passed();
103 }
104 
105 static struct ptunit_result free_decoder_null(void)
106 {
107 	pt_insn_free_decoder(NULL);
108 
109 	return ptu_passed();
110 }
111 
112 static struct ptunit_result sync_forward_null(void)
113 {
114 	int errcode;
115 
116 	errcode = pt_insn_sync_forward(NULL);
117 	ptu_int_eq(errcode, -pte_invalid);
118 
119 	return ptu_passed();
120 }
121 
122 static struct ptunit_result sync_backward_null(void)
123 {
124 	int errcode;
125 
126 	errcode = pt_insn_sync_backward(NULL);
127 	ptu_int_eq(errcode, -pte_invalid);
128 
129 	return ptu_passed();
130 }
131 
132 static struct ptunit_result sync_set_null(void)
133 {
134 	int errcode;
135 
136 	errcode = pt_insn_sync_set(NULL, 0ull);
137 	ptu_int_eq(errcode, -pte_invalid);
138 
139 	return ptu_passed();
140 }
141 
142 static struct ptunit_result sync_set_eos(struct test_fixture *tfix)
143 {
144 	int errcode;
145 
146 	errcode = pt_insn_sync_set(&tfix->decoder, sizeof(tfix->buffer) + 1);
147 	ptu_int_eq(errcode, -pte_eos);
148 
149 	return ptu_passed();
150 }
151 
152 static struct ptunit_result get_offset_null(void)
153 {
154 	struct pt_insn_decoder decoder;
155 	uint64_t offset;
156 	int errcode;
157 
158 	errcode = pt_insn_get_offset(NULL, &offset);
159 	ptu_int_eq(errcode, -pte_invalid);
160 
161 	errcode = pt_insn_get_offset(&decoder, NULL);
162 	ptu_int_eq(errcode, -pte_invalid);
163 
164 	return ptu_passed();
165 }
166 
167 static struct ptunit_result get_offset_init(struct test_fixture *tfix)
168 {
169 	uint64_t offset;
170 	int errcode;
171 
172 	errcode = pt_insn_get_offset(&tfix->decoder, &offset);
173 	ptu_int_eq(errcode, -pte_nosync);
174 
175 	return ptu_passed();
176 }
177 
178 static struct ptunit_result get_sync_offset_null(void)
179 {
180 	struct pt_insn_decoder decoder;
181 	uint64_t offset;
182 	int errcode;
183 
184 	errcode = pt_insn_get_sync_offset(NULL, &offset);
185 	ptu_int_eq(errcode, -pte_invalid);
186 
187 	errcode = pt_insn_get_sync_offset(&decoder, NULL);
188 	ptu_int_eq(errcode, -pte_invalid);
189 
190 	return ptu_passed();
191 }
192 
193 static struct ptunit_result get_config_null(void)
194 {
195 	const struct pt_config *config;
196 
197 	config = pt_insn_get_config(NULL);
198 	ptu_null(config);
199 
200 	return ptu_passed();
201 }
202 
203 static struct ptunit_result get_image_null(void)
204 {
205 	const struct pt_image *image;
206 
207 	image = pt_insn_get_image(NULL);
208 	ptu_null(image);
209 
210 	return ptu_passed();
211 }
212 
213 static struct ptunit_result set_image_null(void)
214 {
215 	struct pt_image image;
216 	int errcode;
217 
218 	errcode = pt_insn_set_image(NULL, &image);
219 	ptu_int_eq(errcode, -pte_invalid);
220 
221 	errcode = pt_insn_set_image(NULL, NULL);
222 	ptu_int_eq(errcode, -pte_invalid);
223 
224 	return ptu_passed();
225 }
226 
227 static struct ptunit_result get_config(struct test_fixture *tfix)
228 {
229 	const struct pt_config *config;
230 
231 	config = pt_insn_get_config(&tfix->decoder);
232 	ptu_ptr(config);
233 
234 	return ptu_passed();
235 }
236 
237 static struct ptunit_result time_null(void)
238 {
239 	struct pt_insn_decoder decoder;
240 	uint64_t time;
241 	uint32_t lost_mtc, lost_cyc;
242 	int errcode;
243 
244 	errcode = pt_insn_time(NULL, &time, &lost_mtc, &lost_cyc);
245 	ptu_int_eq(errcode, -pte_invalid);
246 
247 	errcode = pt_insn_time(&decoder, NULL, &lost_mtc, &lost_cyc);
248 	ptu_int_eq(errcode, -pte_invalid);
249 
250 	return ptu_passed();
251 }
252 
253 static struct ptunit_result cbr_null(void)
254 {
255 	struct pt_insn_decoder decoder;
256 	uint32_t cbr;
257 	int errcode;
258 
259 	errcode = pt_insn_core_bus_ratio(NULL, &cbr);
260 	ptu_int_eq(errcode, -pte_invalid);
261 
262 	errcode = pt_insn_core_bus_ratio(&decoder, NULL);
263 	ptu_int_eq(errcode, -pte_invalid);
264 
265 	return ptu_passed();
266 }
267 
268 static struct ptunit_result asid_null(void)
269 {
270 	struct pt_insn_decoder decoder;
271 	struct pt_asid asid;
272 	int errcode;
273 
274 	errcode = pt_insn_asid(NULL, &asid, sizeof(asid));
275 	ptu_int_eq(errcode, -pte_invalid);
276 
277 	errcode = pt_insn_asid(&decoder, NULL, sizeof(asid));
278 	ptu_int_eq(errcode, -pte_invalid);
279 
280 	return ptu_passed();
281 }
282 
283 static struct ptunit_result next_null(void)
284 {
285 	struct pt_insn_decoder decoder;
286 	struct pt_insn insn;
287 	int errcode;
288 
289 	errcode = pt_insn_next(NULL, &insn, sizeof(insn));
290 	ptu_int_eq(errcode, -pte_invalid);
291 
292 	errcode = pt_insn_next(&decoder, NULL, sizeof(insn));
293 	ptu_int_eq(errcode, -pte_invalid);
294 
295 	return ptu_passed();
296 }
297 
298 static struct ptunit_result event_null(void)
299 {
300 	struct pt_insn_decoder decoder;
301 	struct pt_event event;
302 	int errcode;
303 
304 	errcode = pt_insn_event(NULL, &event, sizeof(event));
305 	ptu_int_eq(errcode, -pte_invalid);
306 
307 	errcode = pt_insn_event(&decoder, NULL, sizeof(event));
308 	ptu_int_eq(errcode, -pte_invalid);
309 
310 	return ptu_passed();
311 }
312 
313 int main(int argc, char **argv)
314 {
315 	struct test_fixture tfix;
316 	struct ptunit_suite suite;
317 
318 	tfix.init = tfix_init;
319 	tfix.fini = NULL;
320 
321 	suite = ptunit_mk_suite(argc, argv);
322 
323 	ptu_run(suite, decoder_init_null);
324 	ptu_run(suite, decoder_fini_null);
325 	ptu_run(suite, alloc_decoder_null);
326 	ptu_run(suite, free_decoder_null);
327 
328 	ptu_run(suite, sync_forward_null);
329 	ptu_run(suite, sync_backward_null);
330 	ptu_run(suite, sync_set_null);
331 	ptu_run_f(suite, sync_set_eos, tfix);
332 
333 	ptu_run(suite, get_offset_null);
334 	ptu_run_f(suite, get_offset_init, tfix);
335 	ptu_run(suite, get_sync_offset_null);
336 
337 	ptu_run(suite, get_image_null);
338 	ptu_run(suite, set_image_null);
339 
340 	ptu_run(suite, get_config_null);
341 	ptu_run_f(suite, get_config, tfix);
342 
343 	ptu_run(suite, time_null);
344 	ptu_run(suite, cbr_null);
345 	ptu_run(suite, asid_null);
346 
347 	ptu_run(suite, next_null);
348 	ptu_run(suite, event_null);
349 
350 	return ptunit_report(&suite);
351 }
352