1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdbool.h>
3 #include <inttypes.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <linux/bitops.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 
10 #include "map_symbol.h"
11 #include "branch.h"
12 #include "event.h"
13 #include "evsel.h"
14 #include "debug.h"
15 #include "util/synthetic-events.h"
16 
17 #include "tests.h"
18 
19 #define COMP(m) do {					\
20 	if (s1->m != s2->m) {				\
21 		pr_debug("Samples differ at '"#m"'\n");	\
22 		return false;				\
23 	}						\
24 } while (0)
25 
26 #define MCOMP(m) do {					\
27 	if (memcmp(&s1->m, &s2->m, sizeof(s1->m))) {	\
28 		pr_debug("Samples differ at '"#m"'\n");	\
29 		return false;				\
30 	}						\
31 } while (0)
32 
33 static bool samples_same(const struct perf_sample *s1,
34 			 const struct perf_sample *s2,
35 			 u64 type, u64 read_format)
36 {
37 	size_t i;
38 
39 	if (type & PERF_SAMPLE_IDENTIFIER)
40 		COMP(id);
41 
42 	if (type & PERF_SAMPLE_IP)
43 		COMP(ip);
44 
45 	if (type & PERF_SAMPLE_TID) {
46 		COMP(pid);
47 		COMP(tid);
48 	}
49 
50 	if (type & PERF_SAMPLE_TIME)
51 		COMP(time);
52 
53 	if (type & PERF_SAMPLE_ADDR)
54 		COMP(addr);
55 
56 	if (type & PERF_SAMPLE_ID)
57 		COMP(id);
58 
59 	if (type & PERF_SAMPLE_STREAM_ID)
60 		COMP(stream_id);
61 
62 	if (type & PERF_SAMPLE_CPU)
63 		COMP(cpu);
64 
65 	if (type & PERF_SAMPLE_PERIOD)
66 		COMP(period);
67 
68 	if (type & PERF_SAMPLE_READ) {
69 		if (read_format & PERF_FORMAT_GROUP)
70 			COMP(read.group.nr);
71 		else
72 			COMP(read.one.value);
73 		if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
74 			COMP(read.time_enabled);
75 		if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
76 			COMP(read.time_running);
77 		/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
78 		if (read_format & PERF_FORMAT_GROUP) {
79 			for (i = 0; i < s1->read.group.nr; i++)
80 				MCOMP(read.group.values[i]);
81 		} else {
82 			COMP(read.one.id);
83 		}
84 	}
85 
86 	if (type & PERF_SAMPLE_CALLCHAIN) {
87 		COMP(callchain->nr);
88 		for (i = 0; i < s1->callchain->nr; i++)
89 			COMP(callchain->ips[i]);
90 	}
91 
92 	if (type & PERF_SAMPLE_RAW) {
93 		COMP(raw_size);
94 		if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) {
95 			pr_debug("Samples differ at 'raw_data'\n");
96 			return false;
97 		}
98 	}
99 
100 	if (type & PERF_SAMPLE_BRANCH_STACK) {
101 		COMP(branch_stack->nr);
102 		COMP(branch_stack->hw_idx);
103 		for (i = 0; i < s1->branch_stack->nr; i++)
104 			MCOMP(branch_stack->entries[i]);
105 	}
106 
107 	if (type & PERF_SAMPLE_REGS_USER) {
108 		size_t sz = hweight_long(s1->user_regs.mask) * sizeof(u64);
109 
110 		COMP(user_regs.mask);
111 		COMP(user_regs.abi);
112 		if (s1->user_regs.abi &&
113 		    (!s1->user_regs.regs || !s2->user_regs.regs ||
114 		     memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) {
115 			pr_debug("Samples differ at 'user_regs'\n");
116 			return false;
117 		}
118 	}
119 
120 	if (type & PERF_SAMPLE_STACK_USER) {
121 		COMP(user_stack.size);
122 		if (memcmp(s1->user_stack.data, s2->user_stack.data,
123 			   s1->user_stack.size)) {
124 			pr_debug("Samples differ at 'user_stack'\n");
125 			return false;
126 		}
127 	}
128 
129 	if (type & PERF_SAMPLE_WEIGHT)
130 		COMP(weight);
131 
132 	if (type & PERF_SAMPLE_WEIGHT_STRUCT)
133 		COMP(ins_lat);
134 
135 	if (type & PERF_SAMPLE_DATA_SRC)
136 		COMP(data_src);
137 
138 	if (type & PERF_SAMPLE_TRANSACTION)
139 		COMP(transaction);
140 
141 	if (type & PERF_SAMPLE_REGS_INTR) {
142 		size_t sz = hweight_long(s1->intr_regs.mask) * sizeof(u64);
143 
144 		COMP(intr_regs.mask);
145 		COMP(intr_regs.abi);
146 		if (s1->intr_regs.abi &&
147 		    (!s1->intr_regs.regs || !s2->intr_regs.regs ||
148 		     memcmp(s1->intr_regs.regs, s2->intr_regs.regs, sz))) {
149 			pr_debug("Samples differ at 'intr_regs'\n");
150 			return false;
151 		}
152 	}
153 
154 	if (type & PERF_SAMPLE_PHYS_ADDR)
155 		COMP(phys_addr);
156 
157 	if (type & PERF_SAMPLE_CGROUP)
158 		COMP(cgroup);
159 
160 	if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
161 		COMP(data_page_size);
162 
163 	if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
164 		COMP(code_page_size);
165 
166 	if (type & PERF_SAMPLE_AUX) {
167 		COMP(aux_sample.size);
168 		if (memcmp(s1->aux_sample.data, s2->aux_sample.data,
169 			   s1->aux_sample.size)) {
170 			pr_debug("Samples differ at 'aux_sample'\n");
171 			return false;
172 		}
173 	}
174 
175 	return true;
176 }
177 
178 static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
179 {
180 	struct evsel evsel = {
181 		.needs_swap = false,
182 		.core = {
183 			. attr = {
184 				.sample_type = sample_type,
185 				.read_format = read_format,
186 			},
187 		},
188 	};
189 	union perf_event *event;
190 	union {
191 		struct ip_callchain callchain;
192 		u64 data[64];
193 	} callchain = {
194 		/* 3 ips */
195 		.data = {3, 201, 202, 203},
196 	};
197 	union {
198 		struct branch_stack branch_stack;
199 		u64 data[64];
200 	} branch_stack = {
201 		/* 1 branch_entry */
202 		.data = {1, -1ULL, 211, 212, 213},
203 	};
204 	u64 regs[64];
205 	const u32 raw_data[] = {0x12345678, 0x0a0b0c0d, 0x11020304, 0x05060708, 0 };
206 	const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
207 	const u64 aux_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282};
208 	struct perf_sample sample = {
209 		.ip		= 101,
210 		.pid		= 102,
211 		.tid		= 103,
212 		.time		= 104,
213 		.addr		= 105,
214 		.id		= 106,
215 		.stream_id	= 107,
216 		.period		= 108,
217 		.weight		= 109,
218 		.cpu		= 110,
219 		.raw_size	= sizeof(raw_data),
220 		.data_src	= 111,
221 		.transaction	= 112,
222 		.raw_data	= (void *)raw_data,
223 		.callchain	= &callchain.callchain,
224 		.no_hw_idx      = false,
225 		.branch_stack	= &branch_stack.branch_stack,
226 		.user_regs	= {
227 			.abi	= PERF_SAMPLE_REGS_ABI_64,
228 			.mask	= sample_regs,
229 			.regs	= regs,
230 		},
231 		.user_stack	= {
232 			.size	= sizeof(data),
233 			.data	= (void *)data,
234 		},
235 		.read		= {
236 			.time_enabled = 0x030a59d664fca7deULL,
237 			.time_running = 0x011b6ae553eb98edULL,
238 		},
239 		.intr_regs	= {
240 			.abi	= PERF_SAMPLE_REGS_ABI_64,
241 			.mask	= sample_regs,
242 			.regs	= regs,
243 		},
244 		.phys_addr	= 113,
245 		.cgroup		= 114,
246 		.data_page_size = 115,
247 		.code_page_size = 116,
248 		.ins_lat        = 117,
249 		.aux_sample	= {
250 			.size	= sizeof(aux_data),
251 			.data	= (void *)aux_data,
252 		},
253 	};
254 	struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},};
255 	struct perf_sample sample_out;
256 	size_t i, sz, bufsz;
257 	int err, ret = -1;
258 
259 	if (sample_type & PERF_SAMPLE_REGS_USER)
260 		evsel.core.attr.sample_regs_user = sample_regs;
261 
262 	if (sample_type & PERF_SAMPLE_REGS_INTR)
263 		evsel.core.attr.sample_regs_intr = sample_regs;
264 
265 	if (sample_type & PERF_SAMPLE_BRANCH_STACK)
266 		evsel.core.attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
267 
268 	for (i = 0; i < sizeof(regs); i++)
269 		*(i + (u8 *)regs) = i & 0xfe;
270 
271 	if (read_format & PERF_FORMAT_GROUP) {
272 		sample.read.group.nr     = 4;
273 		sample.read.group.values = values;
274 	} else {
275 		sample.read.one.value = 0x08789faeb786aa87ULL;
276 		sample.read.one.id    = 99;
277 	}
278 
279 	sz = perf_event__sample_event_size(&sample, sample_type, read_format);
280 	bufsz = sz + 4096; /* Add a bit for overrun checking */
281 	event = malloc(bufsz);
282 	if (!event) {
283 		pr_debug("malloc failed\n");
284 		return -1;
285 	}
286 
287 	memset(event, 0xff, bufsz);
288 	event->header.type = PERF_RECORD_SAMPLE;
289 	event->header.misc = 0;
290 	event->header.size = sz;
291 
292 	err = perf_event__synthesize_sample(event, sample_type, read_format,
293 					    &sample);
294 	if (err) {
295 		pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
296 			 "perf_event__synthesize_sample", sample_type, err);
297 		goto out_free;
298 	}
299 
300 	/* The data does not contain 0xff so we use that to check the size */
301 	for (i = bufsz; i > 0; i--) {
302 		if (*(i - 1 + (u8 *)event) != 0xff)
303 			break;
304 	}
305 	if (i != sz) {
306 		pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
307 			 i, sz);
308 		goto out_free;
309 	}
310 
311 	evsel.sample_size = __evsel__sample_size(sample_type);
312 
313 	err = evsel__parse_sample(&evsel, event, &sample_out);
314 	if (err) {
315 		pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
316 			 "evsel__parse_sample", sample_type, err);
317 		goto out_free;
318 	}
319 
320 	if (!samples_same(&sample, &sample_out, sample_type, read_format)) {
321 		pr_debug("parsing failed for sample_type %#"PRIx64"\n",
322 			 sample_type);
323 		goto out_free;
324 	}
325 
326 	ret = 0;
327 out_free:
328 	free(event);
329 	if (ret && read_format)
330 		pr_debug("read_format %#"PRIx64"\n", read_format);
331 	return ret;
332 }
333 
334 /**
335  * test__sample_parsing - test sample parsing.
336  *
337  * This function implements a test that synthesizes a sample event, parses it
338  * and then checks that the parsed sample matches the original sample.  The test
339  * checks sample format bits separately and together.  If the test passes %0 is
340  * returned, otherwise %-1 is returned.
341  */
342 int test__sample_parsing(struct test *test __maybe_unused, int subtest __maybe_unused)
343 {
344 	const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15};
345 	u64 sample_type;
346 	u64 sample_regs;
347 	size_t i;
348 	int err;
349 
350 	/*
351 	 * Fail the test if it has not been updated when new sample format bits
352 	 * were added.  Please actually update the test rather than just change
353 	 * the condition below.
354 	 */
355 	if (PERF_SAMPLE_MAX > PERF_SAMPLE_WEIGHT_STRUCT << 1) {
356 		pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
357 		return -1;
358 	}
359 
360 	/* Test each sample format bit separately */
361 	for (sample_type = 1; sample_type != PERF_SAMPLE_MAX;
362 	     sample_type <<= 1) {
363 		/* Test read_format variations */
364 		if (sample_type == PERF_SAMPLE_READ) {
365 			for (i = 0; i < ARRAY_SIZE(rf); i++) {
366 				err = do_test(sample_type, 0, rf[i]);
367 				if (err)
368 					return err;
369 			}
370 			continue;
371 		}
372 		sample_regs = 0;
373 
374 		if (sample_type == PERF_SAMPLE_REGS_USER)
375 			sample_regs = 0x3fff;
376 
377 		if (sample_type == PERF_SAMPLE_REGS_INTR)
378 			sample_regs = 0xff0fff;
379 
380 		err = do_test(sample_type, sample_regs, 0);
381 		if (err)
382 			return err;
383 	}
384 
385 	/*
386 	 * Test all sample format bits together
387 	 * Note: PERF_SAMPLE_WEIGHT and PERF_SAMPLE_WEIGHT_STRUCT cannot
388 	 *       be set simultaneously.
389 	 */
390 	sample_type = (PERF_SAMPLE_MAX - 1) & ~PERF_SAMPLE_WEIGHT;
391 	sample_regs = 0x3fff; /* shared yb intr and user regs */
392 	for (i = 0; i < ARRAY_SIZE(rf); i++) {
393 		err = do_test(sample_type, sample_regs, rf[i]);
394 		if (err)
395 			return err;
396 	}
397 
398 	return 0;
399 }
400