summaryrefslogtreecommitdiff
path: root/lib/runnercomms.c
blob: 034ba0a2e4ff855efc2cfacfea2f4b9d5baa963c (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/* SPDX-License-Identifier: MIT */
/*
 * Copyright © 2022 Intel Corporation
 */

#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "igt_aux.h"
#include "runnercomms.h"

/**
 * SECTION:runnercomms
 * @short_description: Structured communication to igt_runner
 * @title: runnercomms
 * @include: runnercomms.h
 *
 * This library provides means for the tests to communicate to
 * igt_runner with a formally specified protocol, avoiding
 * shortcomings and pain points of text-based communication.
 */

static sig_atomic_t runner_socket_fd = -1;

/**
 * set_runner_socket:
 * @fd: socket connected to runner
 *
 * If the passed fd is a valid socket, globally sets it to be the fd
 * to use to talk to igt_runner.
 */
void set_runner_socket(int fd)
{
	struct stat sb;

	if (fstat(fd, &sb))
		return;

	if (!S_ISSOCK(sb.st_mode))
		return;

	/*
	 * We only sanity-check that the fd is a socket. We don't
	 * check that it's a datagram socket etc.
	 */

	runner_socket_fd = fd;
}

/**
 * runner_connected:
 *
 * Returns whether set_runner_socket has been called with a valid
 * socket fd. Note: Will be true forever after that point. This
 * function is used to mainly determine whether log strings will be
 * output to the socket or to stdout/stderr and that cannot be changed
 * even if the socket is lost midway.
 */
bool runner_connected(void)
{
	return runner_socket_fd >= 0;
}

/**
 * send_to_runner:
 * @packet: packet to send
 *
 * Sends the given communications packet to igt_runner. Calls free()
 * on the packet, don't reuse it.
 */
void send_to_runner(struct runnerpacket *packet)
{
	if (runner_connected())
		write(runner_socket_fd, packet, packet->size);
	free(packet);
}

/* If enough data left, copy the data to dst, advance p, reduce size */
static void read_integer(void* dst, size_t bytes, const char **p, uint32_t *size)
{
	if (*size < bytes) {
		*size = 0;
		return;
	}

	memcpy(dst, *p, bytes);
	*p += bytes;
	*size -= bytes;
}

/* If nul-termination can be found, set dststr to point to the cstring, advance p, reduce size */
static void read_cstring(const char **dststr, const char **p, uint32_t *size)
{
	const char *end;

	end = memchr(*p, '\0', *size);
	if (end == NULL) {
		*size = 0;
		return;
	}

	*dststr = *p;
	*size -= end - *p + 1;
	*p = end + 1;
}

/**
 * read_runnerpacket:
 * @packet: runner communications packet to read
 *
 * Checks that the internal data of the communications packet is valid
 * and the contents can safely be inspected without further checking
 * for out-of-bounds etc. Constructs a runnerpacket_read_helper which
 * will, for c-style strings, point to various sub-values directly in
 * the #data field within @packet. Those are valid only as long as
 * @packet is valid.
 *
 * Returns: An appropriately constructed runnerpacket_read_helper. On
 * data validation errors, the #type of the returned value will be
 * #PACKETTYPE_INVALID.
 */
runnerpacket_read_helper read_runnerpacket(const struct runnerpacket *packet)
{
	runnerpacket_read_helper ret = {};
	uint32_t sizeleft;
	const char *p;

	if (packet->size < sizeof(*packet)) {
		ret.type = PACKETTYPE_INVALID;
		return ret;
	}

	ret.type = packet->type;
	sizeleft = packet->size - sizeof(*packet);
	p = packet->data;

	switch (packet->type) {
	case PACKETTYPE_LOG:
		read_integer(&ret.log.stream, sizeof(ret.log.stream), &p, &sizeleft);
		read_cstring(&ret.log.text, &p, &sizeleft);

		if (ret.log.text == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	case PACKETTYPE_EXEC:
		read_cstring(&ret.exec.cmdline, &p, &sizeleft);

		if (ret.exec.cmdline == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	case PACKETTYPE_EXIT:
		read_integer(&ret.exit.exitcode, sizeof(ret.exit.exitcode), &p, &sizeleft);
		read_cstring(&ret.exit.timeused, &p, &sizeleft);

		break;
	case PACKETTYPE_SUBTEST_START:
		read_cstring(&ret.subteststart.name, &p, &sizeleft);

		if (ret.subteststart.name == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	case PACKETTYPE_SUBTEST_RESULT:
		read_cstring(&ret.subtestresult.name, &p, &sizeleft);
		read_cstring(&ret.subtestresult.result, &p, &sizeleft);
		read_cstring(&ret.subtestresult.timeused, &p, &sizeleft);
		read_cstring(&ret.subtestresult.reason, &p, &sizeleft);

		if (ret.subtestresult.name == NULL ||
		    ret.subtestresult.result == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	case PACKETTYPE_DYNAMIC_SUBTEST_START:
		read_cstring(&ret.dynamicsubteststart.name, &p, &sizeleft);

		if (ret.dynamicsubteststart.name == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	case PACKETTYPE_DYNAMIC_SUBTEST_RESULT:
		read_cstring(&ret.dynamicsubtestresult.name, &p, &sizeleft);
		read_cstring(&ret.dynamicsubtestresult.result, &p, &sizeleft);
		read_cstring(&ret.dynamicsubtestresult.timeused, &p, &sizeleft);
		read_cstring(&ret.dynamicsubtestresult.reason, &p, &sizeleft);

		if (ret.dynamicsubtestresult.name == NULL ||
		    ret.dynamicsubtestresult.result == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	case PACKETTYPE_VERSIONSTRING:
		read_cstring(&ret.versionstring.text, &p, &sizeleft);

		if (ret.versionstring.text == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	case PACKETTYPE_RESULT_OVERRIDE:
		read_cstring(&ret.resultoverride.result, &p, &sizeleft);

		if (ret.resultoverride.result == NULL)
			ret.type = PACKETTYPE_INVALID;

		break;
	default:
		ret.type = PACKETTYPE_INVALID;
		break;
	}

	return ret;
}

struct runnerpacket *runnerpacket_log(uint8_t stream, const char *text)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	size = sizeof(struct runnerpacket) + sizeof(stream) + strlen(text) + 1;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_LOG;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	memcpy(p, &stream, sizeof(stream));
	p += sizeof(stream);

	strcpy(p, text);
	p += strlen(text) + 1;

	return packet;
}

struct runnerpacket *runnerpacket_exec(char **argv)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;
	int i;

	size = sizeof(struct runnerpacket);

	for (i = 0; argv[i] != NULL; i++)
		size += strlen(argv[i]) + 1; // followed by a space of \0 so +1 either way for each

	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_EXEC;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	for (i = 0; argv[i] != NULL; i++) {
		if (i != 0)
			*p++ = ' ';

		strcpy(p, argv[i]);
		p += strlen(argv[i]);
	}
	p[0] = '\0';

	return packet;
}

struct runnerpacket *runnerpacket_exit(int32_t exitcode, const char *timeused)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	size = sizeof(struct runnerpacket) + sizeof(exitcode) + strlen(timeused) + 1;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_EXIT;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	memcpy(p, &exitcode, sizeof(exitcode));
	p += sizeof(exitcode);

	strcpy(p, timeused);
	p += strlen(timeused) + 1;

	return packet;
}

struct runnerpacket *runnerpacket_subtest_start(const char *name)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	size = sizeof(struct runnerpacket) + strlen(name) + 1;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_SUBTEST_START;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	strcpy(p, name);
	p += strlen(name) + 1;

	return packet;
}

struct runnerpacket *runnerpacket_subtest_result(const char *name, const char *result,
						 const char *timeused, const char *reason)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	if (reason == NULL)
		reason = "";

	size = sizeof(struct runnerpacket) + strlen(name) + strlen(result) + strlen(timeused) + strlen(reason) + 4;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_SUBTEST_RESULT;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	strcpy(p, name);
	p += strlen(name) + 1;

	strcpy(p, result);
	p += strlen(result) + 1;

	strcpy(p, timeused);
	p += strlen(timeused) + 1;

	strcpy(p, reason);
	p += strlen(reason) + 1;

	return packet;
}

struct runnerpacket *runnerpacket_dynamic_subtest_start(const char *name)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	size = sizeof(struct runnerpacket) + strlen(name) + 1;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_DYNAMIC_SUBTEST_START;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	strcpy(p, name);
	p += strlen(name) + 1;

	return packet;
}

struct runnerpacket *runnerpacket_dynamic_subtest_result(const char *name, const char *result,
							 const char *timeused, const char *reason)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	if (reason == NULL)
		reason = "";

	size = sizeof(struct runnerpacket) + strlen(name) + strlen(result) + strlen(timeused) + strlen(reason) + 4;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_DYNAMIC_SUBTEST_RESULT;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	strcpy(p, name);
	p += strlen(name) + 1;

	strcpy(p, result);
	p += strlen(result) + 1;

	strcpy(p, timeused);
	p += strlen(timeused) + 1;

	strcpy(p, reason);
	p += strlen(reason) + 1;

	return packet;
}

struct runnerpacket *runnerpacket_versionstring(const char *text)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	size = sizeof(struct runnerpacket) + strlen(text) + 1;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_VERSIONSTRING;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	strcpy(p, text);
	p += strlen(text) + 1;

	return packet;
}

struct runnerpacket *runnerpacket_resultoverride(const char *result)
{
	struct runnerpacket *packet;
	uint32_t size;
	char *p;

	size = sizeof(struct runnerpacket) + strlen(result) + 1;
	packet = malloc(size);

	packet->size = size;
	packet->type = PACKETTYPE_RESULT_OVERRIDE;
	packet->senderpid = getpid();
	packet->sendertid = gettid();

	p = packet->data;

	strcpy(p, result);
	p += strlen(result) + 1;

	return packet;
}

uint32_t socket_dump_canary(void)
{
	return 'I' << 24 | 'G' << 16 | 'T' << 8 | '1';
}

void log_to_runner_sig_safe(const char *str, size_t len)
{
	size_t prlen = len;

	struct runnerpacket_log_sig_safe p = {
					      .size = sizeof(struct runnerpacket) + sizeof(uint8_t),
					      .type = PACKETTYPE_LOG,
					      .senderpid = getpid(),
					      .sendertid = 0, /* gettid() not signal safe */
					      .stream = STDERR_FILENO,
	};

	if (len > sizeof(p.data) - 1)
		prlen = sizeof(p.data) - 1;
	memcpy(p.data, str, prlen);
	p.size += prlen + 1;

	write(runner_socket_fd, &p, p.size);

	len -= prlen;
	if (len)
		log_to_runner_sig_safe(str + prlen, len);
}

/**
 * comms_read_dump:
 * @fd: Open fd to a comms dump file
 * @visitor: Collection of packet handlers
 *
 * Reads a comms dump file, calling specified handler functions for
 * individual packets.
 *
 * Returns: #COMMSPARSE_ERROR for failures reading or parsing the
 * dump, #COMMSPARSE_EMPTY for empty dumps (no comms used),
 * #COMMSPARSE_SUCCESS for successful read.
 */
int comms_read_dump(int fd, struct comms_visitor *visitor)
{
	struct stat statbuf;
	char *buf, *bufend, *p;
	int ret = COMMSPARSE_EMPTY;
	bool cont = true;

	if (fd < 0)
		return COMMSPARSE_EMPTY;

	if (fstat(fd, &statbuf))
		return COMMSPARSE_ERROR;

	if (statbuf.st_size == 0)
		return COMMSPARSE_EMPTY;

	buf = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
	if (buf == MAP_FAILED)
		return COMMSPARSE_ERROR;

	bufend = buf + statbuf.st_size;
	p = buf;

	while (p != NULL && p != bufend && cont) {
		const struct runnerpacket *packet;
		runnerpacket_read_helper helper;

		if (bufend - p >= sizeof(uint32_t)) {
			uint32_t canary;

			memcpy(&canary, p, sizeof(canary));
			if (canary != socket_dump_canary()) {
				fprintf(stderr,
					"Invalid canary while parsing comms: %"PRIu32", expected %"PRIu32"\n",
					canary, socket_dump_canary());
				munmap(buf, statbuf.st_size);
				return COMMSPARSE_ERROR;
			}
		}
		p += sizeof(uint32_t);

		if (bufend -p < sizeof(struct runnerpacket)) {
			fprintf(stderr,
				"Error parsing comms: Expected runnerpacket after canary, truncated file?\n");
			munmap(buf, statbuf.st_size);
			return COMMSPARSE_ERROR;
		}

		packet = (struct runnerpacket *)p;
		if (bufend -p < packet->size) {
			fprintf(stderr,
				"Error parsing comms: Unexpected end of file, truncated file?\n");
			munmap(buf, statbuf.st_size);
			return COMMSPARSE_ERROR;
		}
		p += packet->size;

		/*
		 * Runner sends EXEC itself before executing the test.
		 * If we get other types, it indicates the test really
		 * uses socket comms.
		 */
		if (packet->type != PACKETTYPE_EXEC)
			ret = COMMSPARSE_SUCCESS;

		switch (packet->type) {
		case PACKETTYPE_INVALID:
			printf("Warning: Unknown packet type %"PRIu32", skipping\n", packet->type);
			break;
		case PACKETTYPE_LOG:
			if (visitor->log) {
				helper = read_runnerpacket(packet);
				cont = visitor->log(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_EXEC:
			if (visitor->exec) {
				helper = read_runnerpacket(packet);
				cont = visitor->exec(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_EXIT:
			if (visitor->exit) {
				helper = read_runnerpacket(packet);
				cont = visitor->exit(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_SUBTEST_START:
			if (visitor->subtest_start) {
				helper = read_runnerpacket(packet);
				cont = visitor->subtest_start(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_SUBTEST_RESULT:
			if (visitor->subtest_result) {
				helper = read_runnerpacket(packet);
				cont = visitor->subtest_result(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_DYNAMIC_SUBTEST_START:
			if (visitor->dynamic_subtest_start) {
				helper = read_runnerpacket(packet);
				cont = visitor->dynamic_subtest_start(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_DYNAMIC_SUBTEST_RESULT:
			if (visitor->dynamic_subtest_result) {
				helper = read_runnerpacket(packet);
				cont = visitor->dynamic_subtest_result(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_VERSIONSTRING:
			if (visitor->versionstring) {
				helper = read_runnerpacket(packet);
				cont = visitor->versionstring(packet, helper, visitor->userdata);
			}
			break;
		case PACKETTYPE_RESULT_OVERRIDE:
			if (visitor->result_override) {
				helper = read_runnerpacket(packet);
				cont = visitor->result_override(packet, helper, visitor->userdata);
			}
			break;
		default:
			printf("Warning: Unknown packet type %"PRIu32"\n", helper.type);
			break;
		}
	}

	munmap(buf, statbuf.st_size);
	return cont ? ret : COMMSPARSE_ERROR;
}