summaryrefslogtreecommitdiff
path: root/usr/src/test/util-tests/tests/ctf/check-common.c
blob: 60e9799fef8f5351b039e167dcd4f50108403f08 (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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2020 Joyent, Inc.
 */

/*
 * Collection of common utilities for CTF testing.
 */

#include <strings.h>
#include <libctf.h>
#include "check-common.h"

typedef struct ctftests_lookup_cb {
	ctf_file_t *clc_fp;
	ctf_id_t clc_id;
	const char *clc_name;
} ctftests_lookup_cb_t;

typedef struct ctftest_member_cb {
	ctf_file_t *cmc_fp;
	const check_member_t *cmc_members;
	const char *cmc_name;
} ctftest_member_cb_t;

static int
ctftest_lookup_type_cb(ctf_id_t id, boolean_t root, void *arg)
{
	char buf[2048];
	ctftests_lookup_cb_t *clc = arg;

	if (ctf_type_name(clc->clc_fp, id, buf, sizeof (buf)) == NULL)
		return (0);

	if (strcmp(buf, clc->clc_name) != 0)
		return (0);

	clc->clc_id = id;
	return (1);
}

/*
 * This is a variant on the classic ctf_lookup_by_name(). ctf_lookup_by_name()
 * skips qualifiers, which makes sense given what the consumers of it are trying
 * to do. However, that's not what we want here. So instead we basically have to
 * walk the type table.
 */
static ctf_id_t
ctftest_lookup_type(ctf_file_t *fp, const char *name)
{
	ctftests_lookup_cb_t clc;

	clc.clc_fp = fp;
	clc.clc_id = CTF_ERR;
	clc.clc_name = name;

	(void) ctf_type_iter(fp, B_TRUE, ctftest_lookup_type_cb, &clc);
	return (clc.clc_id);
}

static int
ctftest_lookup_object_cb(const char *obj, ctf_id_t type, ulong_t idx, void *arg)
{
	ctftests_lookup_cb_t *clc = arg;

	if (strcmp(obj, clc->clc_name) == 0) {
		clc->clc_id = type;
		return (1);
	}

	return (0);
}

static ctf_id_t
ctftest_lookup_symbol(ctf_file_t *fp, const char *name)
{
	ctftests_lookup_cb_t clc;

	clc.clc_fp = fp;
	clc.clc_id = CTF_ERR;
	clc.clc_name = name;

	(void) ctf_object_iter(fp, ctftest_lookup_object_cb, &clc);
	return (clc.clc_id);
}

typedef struct ctf_function_cb {
	const char *cfc_name;
	ulong_t *cfc_symp;
	ctf_funcinfo_t *cfc_fip;
} ctf_function_cb_t;

static int
ctftest_lookup_function_cb(const char *name, ulong_t symidx,
    ctf_funcinfo_t *fip, void *arg)
{
	ctf_function_cb_t *cfc = arg;
	if (strcmp(name, cfc->cfc_name) != 0)
		return (0);

	*cfc->cfc_symp = symidx;
	*cfc->cfc_fip = *fip;

	return (1);
}

/*
 * Note, this function finds the first one with a matching name. This must not
 * be used when performing searches where a given name may occur more than once.
 */
static boolean_t
ctftest_lookup_function(ctf_file_t *fp, const char *name, ulong_t *symp,
    ctf_funcinfo_t *fip)
{
	ctf_function_cb_t cfc;

	*symp = 0;
	cfc.cfc_name = name;
	cfc.cfc_symp = symp;
	cfc.cfc_fip = fip;
	(void) ctf_function_iter(fp, ctftest_lookup_function_cb, &cfc);
	return (*symp == 0 ? B_FALSE : B_TRUE);
}

boolean_t
ctftest_check_numbers(ctf_file_t *fp, const check_number_t *tests)
{
	uint_t i;
	boolean_t ret = B_TRUE;

	for (i = 0; tests[i].cn_tname != NULL; i++) {
		ctf_id_t id;
		ctf_encoding_t enc;

		if (ctftest_skip(tests[i].cn_skips)) {
			warnx("skipping check numbers test %s due to known "
			    "compiler issue", tests[i].cn_tname);
			continue;
		}

		id = ctftest_lookup_type(fp, tests[i].cn_tname);
		if (id == CTF_ERR) {
			warnx("failed to look up %s", tests[i].cn_tname);
			ret = B_FALSE;
			continue;
		}

		if (ctf_type_kind(fp, id) != tests[i].cn_kind) {
			warnx("type kind mismatch for %s: got %u, expected %u",
			    tests[i].cn_tname, ctf_type_kind(fp, id),
			    tests[i].cn_kind);
			ret = B_FALSE;
			continue;
		}

		if (ctf_type_encoding(fp, id, &enc) == CTF_ERR) {
			warnx("failed to get type encoding for %s: %s",
			    tests[i].cn_tname, ctf_errmsg(ctf_errno(fp)));
			ret = B_FALSE;
			continue;
		}

		if (enc.cte_format != tests[i].cn_flags) {
			warnx("encoding flags mismatch for %s: got 0x%x, "
			    "expected 0x%x", tests[i].cn_tname, enc.cte_format,
			    tests[i].cn_flags);
			ret = B_FALSE;
			continue;
		}

		if (enc.cte_offset != tests[i].cn_offset) {
			warnx("encoding offset mismatch for %s: got 0x%x, "
			    "expected 0x%x", tests[i].cn_tname, enc.cte_offset,
			    tests[i].cn_offset);
			ret = B_FALSE;
			continue;
		}

		if (enc.cte_bits != tests[i].cn_size) {
			warnx("encoding size mismatch for %s: got 0x%x, "
			    "expected 0x%x", tests[i].cn_tname, enc.cte_bits,
			    tests[i].cn_size);
			ret = B_FALSE;
			continue;
		}
	}

	return (ret);
}

typedef struct ctftests_symbol_cb {
	ctf_file_t	*csc_fp;
	boolean_t	csc_ret;
	const check_symbol_t *csc_tests;
} ctftest_symbol_cb_t;

static int
ctftest_check_symbol_cb(const char *obj, ctf_id_t type, ulong_t idx, void *arg)
{
	ctftest_symbol_cb_t *cb = arg;
	const check_symbol_t *tests = cb->csc_tests;
	ctf_file_t *fp = cb->csc_fp;
	uint_t i;

	for (i = 0; tests[i].cs_symbol != NULL; i++) {
		ctf_id_t id;

		if (strcmp(obj, tests[i].cs_symbol) != 0)
			continue;

		id = ctftest_lookup_type(fp, tests[i].cs_type);
		if (id == CTF_ERR) {
			warnx("failed to lookup type %s for symbol %s",
			    tests[i].cs_type, tests[i].cs_symbol);
			cb->csc_ret = B_FALSE;
			return (0);
		}

		if (id != type) {
			warnx("type mismatch for symbol %s, has type id %u, "
			    "but specified type %s has id %u",
			    tests[i].cs_symbol, type, tests[i].cs_type, id);
			cb->csc_ret = B_FALSE;
			return (0);
		}
	}

	return (0);
}

boolean_t
ctftest_check_symbols(ctf_file_t *fp, const check_symbol_t *tests)
{
	ctftest_symbol_cb_t cb;

	cb.csc_fp = fp;
	cb.csc_ret = B_TRUE;
	cb.csc_tests = tests;
	if (ctf_object_iter(fp, ctftest_check_symbol_cb, &cb) != 0)
		return (B_FALSE);
	return (cb.csc_ret);
}


boolean_t
ctftest_check_descent(const char *symbol, ctf_file_t *fp,
    const check_descent_t *tests, boolean_t quiet)
{
	ctf_id_t base;
	uint_t layer = 0;

	/*
	 * First, find the initial type of the symbol.
	 */
	base = ctftest_lookup_symbol(fp, symbol);
	if (base == CTF_ERR) {
		warnx("failed to lookup type for symbol %s", symbol);
		return (B_FALSE);
	}

	while (tests->cd_tname != NULL) {
		ctf_id_t tid;
		int kind;
		ctf_arinfo_t ari;

		if (base == CTF_ERR) {
			if (!quiet) {
				warnx("encountered non-reference type at layer "
				    "%u while still expecting type %s for "
				    "symbol %s", layer,
				    tests->cd_tname, symbol);
			}
			return (B_FALSE);
		}

		tid = ctftest_lookup_type(fp, tests->cd_tname);
		if (tid == CTF_ERR) {
			if (!quiet) {
				warnx("failed to lookup type %s",
				    tests->cd_tname);
			}
			return (B_FALSE);
		}

		if (tid != base) {
			if (!quiet) {
				warnx("type mismatch at layer %u: found id %u, "
				    "but expecting type id %u for type %s, "
				    "symbol %s", layer, base, tid,
				    tests->cd_tname, symbol);
			}
			return (B_FALSE);
		}

		kind = ctf_type_kind(fp, base);
		if (kind != tests->cd_kind) {
			if (!quiet) {
				warnx("type kind mismatch at layer %u: found "
				    "kind %u, but expected kind %u for %s, "
				    "symbol %s", layer, kind, tests->cd_kind,
				    tests->cd_tname, symbol);
			}
			return (B_FALSE);
		}

		switch (kind) {
		case CTF_K_ARRAY:
			if (ctf_array_info(fp, base, &ari) == CTF_ERR) {
				if (!quiet) {
					warnx("failed to lookup array info at "
					    "layer %u for type %s, symbol "
					    "%s: %s", base, tests->cd_tname,
					    symbol, ctf_errmsg(ctf_errno(fp)));
				}
				return (B_FALSE);
			}

			if (tests->cd_nents != ari.ctr_nelems) {
				if (!quiet) {
					warnx("array element mismatch at layer "
					    "%u for type %s, symbol %s: found "
					    "%u, expected %u", layer,
					    tests->cd_tname, symbol,
					    ari.ctr_nelems, tests->cd_nents);
				}
				return (B_FALSE);
			}

			tid = ctftest_lookup_type(fp, tests->cd_contents);
			if (tid == CTF_ERR) {
				if (!quiet) {
					warnx("failed to look up type %s",
					    tests->cd_contents);
				}
				return (B_FALSE);
			}

			if (ari.ctr_contents != tid) {
				if (!quiet) {
					warnx("array contents mismatch at "
					    "layer %u for type %s, symbol %s: "
					    "found %u, expected %s/%u", layer,
					    tests->cd_tname, symbol,
					    ari.ctr_contents,
					    tests->cd_contents, tid);
				}
				return (B_FALSE);
			}
			base = ari.ctr_contents;
			break;
		default:
			base = ctf_type_reference(fp, base);
			break;
		}

		tests++;
		layer++;
	}

	if (base != CTF_ERR) {
		if (!quiet) {
			warnx("found additional type %u in chain, "
			    "but expected no more", base);
		}
		return (B_FALSE);
	}

	return (B_TRUE);
}

int
ctftest_check_enum_count(const char *name, int value, void *arg)
{
	uint_t *u = arg;
	*u = *u + 1;
	return (0);
}

int
ctftest_check_enum_value(const char *name, int value, void *arg)
{
	uint_t i;
	const check_enum_t *enums = arg;

	for (i = 0; enums[i].ce_name != NULL; i++) {
		if (strcmp(enums[i].ce_name, name) != 0)
			continue;
		if (enums[i].ce_value == (int64_t)value)
			return (0);
		warnx("enum %s value mismatch: found %d, expected %" PRId64,
		    name, value, enums[i].ce_value);
		return (1);
	}

	warnx("found no matching entry for enum member %s", name);
	return (1);
}

boolean_t
ctftest_check_enum(const char *type, ctf_file_t *fp, const check_enum_t *enums)
{
	int ret;
	uint_t tcount, ecount;
	ctf_id_t base;

	if ((base = ctftest_lookup_type(fp, type)) == CTF_ERR) {
		warnx("Failed to look up type %s", type);
		return (B_FALSE);
	}

	if (ctf_type_kind(fp, base) != CTF_K_ENUM) {
		warnx("%s is not an enum", type);
		return (B_FALSE);
	}

	/*
	 * First count how many entries we have.
	 */
	tcount = 0;
	while (enums[tcount].ce_name != NULL) {
		tcount++;
	}

	ecount = 0;
	if (ctf_enum_iter(fp, base, ctftest_check_enum_count, &ecount) != 0) {
		warnx("failed to walk enum %s: %s", type,
		    ctf_errmsg(ctf_errno(fp)));
		return (B_FALSE);
	}

	if (tcount != ecount) {
		warnx("enum value mismatch: expected %u values, but found %u",
		    tcount, ecount);
		return (B_FALSE);
	}

	if ((ret = ctf_enum_iter(fp, base, ctftest_check_enum_value,
	    (void *)enums)) != 0) {
		if (ret == -1) {
			warnx("failed to walk enum %s: %s", type,
			    ctf_errmsg(ctf_errno(fp)));
		}
		return (B_FALSE);
	}

	return (B_TRUE);
}

int
ctftest_check_member_count(const char *mname, ctf_id_t mtype, ulong_t bitoff,
    void *arg)
{
	uint_t *countp = arg;
	*countp = *countp + 1;
	return (0);
}

int
ctftest_check_members_cb(const char *mname, ctf_id_t mtype, ulong_t bitoff,
    void *arg)
{
	uint_t i;
	const ctftest_member_cb_t *cmc = arg;
	const check_member_t *members = cmc->cmc_members;
	ctf_file_t *fp = cmc->cmc_fp;

	for (i = 0; members[i].cm_name != NULL; i++) {
		boolean_t bad = B_FALSE;
		char buf[2048];

		if (strcmp(mname, members[i].cm_name) != 0)
			continue;

		if (bitoff != members[i].cm_offset) {
			warnx("member %s of type %s has mismatched bit offset: "
			    "found %lu, expected %lu", mname, cmc->cmc_name,
			    bitoff, members[i].cm_offset);
			bad = B_TRUE;
		}

		if (ctf_type_name(fp, mtype, buf, sizeof (buf)) == NULL) {
			warnx("failed to obtain type name for member %s",
			    mname, ctf_errmsg(ctf_errno(fp)));
			bad = B_TRUE;
		} else if (strcmp(buf, members[i].cm_type) != 0) {
			warnx("member %s has bad type, found %s, expected %s",
			    mname, buf, members[i].cm_type);
			bad = B_TRUE;
		}

		return (bad ? 1 : 0);
	}

	warnx("found no matching entry for member %s of type %s", mname,
	    cmc->cmc_name);
	return (1);
}

boolean_t
ctftest_check_members(const char *type, ctf_file_t *fp, int kind,
    size_t size, const check_member_t *members)
{
	int ret;
	uint_t tcount, mcount;
	ctf_id_t base;
	ctftest_member_cb_t cmc;

	if ((base = ctftest_lookup_type(fp, type)) == CTF_ERR) {
		warnx("failed to look up type %s", type);
		return (B_FALSE);
	}

	if (ctf_type_kind(fp, base) != kind) {
		warnx("%s has kind %s, expected %s", type,
		    ctf_kind_name(fp, ctf_type_kind(fp, base)),
		    ctf_kind_name(fp, kind));
		return (B_FALSE);
	}

	if (size != ctf_type_size(fp, base)) {
		warnx("%s has bad size, expected %lu, found %lu",
		    type, size, ctf_type_size(fp, base));
		return (B_FALSE);
	}

	/*
	 * First count how many entries we have.
	 */
	tcount = 0;
	while (members[tcount].cm_name != NULL) {
		tcount++;
	}

	mcount = 0;
	if (ctf_member_iter(fp, base, ctftest_check_member_count, &mcount) !=
	    0) {
		warnx("failed to walk members of %s: %s", type,
		    ctf_errmsg(ctf_errno(fp)));
		return (B_FALSE);
	}

	if (tcount != mcount) {
		warnx("type member mismatch: expected %u values, but found %u",
		    tcount, mcount);
		return (B_FALSE);
	}

	cmc.cmc_fp = fp;
	cmc.cmc_members = members;
	cmc.cmc_name = type;
	if ((ret = ctf_member_iter(fp, base, ctftest_check_members_cb,
	    &cmc)) != 0) {
		if (ret == -1) {
			warnx("failed to walk type %s: %s", type,
			    ctf_errmsg(ctf_errno(fp)));
		}
		return (B_FALSE);
	}

	return (B_TRUE);
}

boolean_t
ctftest_check_function(const char *symbol, ctf_file_t *fp, const char *rtype,
    uint_t nargs, uint_t flags, const char **argv)
{
	ulong_t sym;
	ctf_funcinfo_t fi;
	uint_t i;
	boolean_t ret = B_TRUE;
	ctf_id_t *args;
	char buf[2048];


	if (!ctftest_lookup_function(fp, symbol, &sym, &fi)) {
		warnx("failed to look up function %s", symbol);
		return (B_FALSE);
	}

	if (ctf_type_name(fp, fi.ctc_return, buf, sizeof (buf)) == NULL) {
		warnx("failed to lookup return type name for function %s",
		    symbol);
		ret = B_FALSE;
	} else if (strcmp(rtype, buf) != 0) {
		warnx("return type has wrong type: found %s, expected %s",
		    buf, rtype);
		ret = B_FALSE;
	}

	if (nargs != fi.ctc_argc) {
		warnx("function argument mismatch: found %u, expected %u",
		    fi.ctc_argc, nargs);
		ret = B_FALSE;
	}

	if (flags != fi.ctc_flags) {
		warnx("function flags mismatch, found 0x%x, expected 0x%x",
		    fi.ctc_flags, flags);
		ret = B_FALSE;
	}

	if (!ret || fi.ctc_argc == 0) {
		return (ret);
	}

	if ((args = calloc(fi.ctc_argc, sizeof (ctf_id_t))) == NULL) {
		warnx("failed to allocate memory for function arguments");
		return (B_FALSE);
	}

	if (ctf_func_args(fp, sym, fi.ctc_argc, args) != 0) {
		warnx("failed to get function information: %s",
		    ctf_errmsg(ctf_errno(fp)));
		free(args);
		return (B_FALSE);
	}

	for (i = 0; i < fi.ctc_argc; i++) {
		if (ctf_type_name(fp, args[i], buf, sizeof (buf)) == NULL) {
			warnx("failed to obtain type name for argument %u",
			    i, ctf_errmsg(ctf_errno(fp)));
			ret = B_FALSE;
			break;
		}

		if (strcmp(buf, argv[i]) != 0) {
			warnx("argument %u has wrong type: found %s, "
			    "expected %s", i, buf, argv[i]);
			ret = B_FALSE;
			break;
		}
	}

	free(args);
	return (ret);
}

boolean_t
ctftest_check_fptr(const char *type, ctf_file_t *fp, const char *rtype,
    uint_t nargs, uint_t flags, const char **argv)
{
	ctf_id_t tid;
	ctf_funcinfo_t fi;
	uint_t i;
	boolean_t ret = B_TRUE;
	ctf_id_t *args;
	char buf[2048];


	if ((tid = ctf_lookup_by_name(fp, type)) == CTF_ERR) {
		warnx("failed to look up type %s: %s", type,
		    ctf_errmsg(ctf_errno(fp)));
		return (B_FALSE);
	}

	/*
	 * Perform two CTF type resolves, one for the function pointer and one
	 * for the typedef that gets passed in.
	 */
	if ((tid = ctf_type_resolve(fp, tid)) == CTF_ERR) {
		warnx("failed to convert type %s to base type: %s", type,
		    ctf_errmsg(ctf_errno(fp)));
		return (B_FALSE);
	}

	if (ctf_type_kind(fp, tid) == CTF_K_POINTER &&
	    (tid = ctf_type_reference(fp, tid)) == CTF_ERR) {
		warnx("failed to convert type %s to base type: %s", type,
		    ctf_errmsg(ctf_errno(fp)));
		return (B_FALSE);
	}

	if (ctf_func_info_by_id(fp, tid, &fi) != 0) {
		warnx("failed to get function information for type %s: %s",
		    type, ctf_errmsg(ctf_errno(fp)));
		return (B_FALSE);
	}

	if (ctf_type_name(fp, fi.ctc_return, buf, sizeof (buf)) == NULL) {
		warnx("failed to lookup return type name for function %s",
		    type);
		ret = B_FALSE;
	} else if (strcmp(rtype, buf) != 0) {
		warnx("return type has wrong type: found %s, expected %s",
		    buf, rtype);
		ret = B_FALSE;
	}

	if (nargs != fi.ctc_argc) {
		warnx("function argument mismatch: found %u, expected %u",
		    fi.ctc_argc, nargs);
		ret = B_FALSE;
	}

	if (flags != fi.ctc_flags) {
		warnx("function flags mismatch, found 0x%x, expected 0x%x",
		    fi.ctc_flags, flags);
		ret = B_FALSE;
	}

	if (!ret || fi.ctc_argc == 0) {
		return (ret);
	}

	if ((args = calloc(fi.ctc_argc, sizeof (ctf_id_t))) == NULL) {
		warnx("failed to allocate memory for function arguments");
		return (B_FALSE);
	}

	if (ctf_func_args_by_id(fp, tid, fi.ctc_argc, args) != 0) {
		warnx("failed to get function information: %s",
		    ctf_errmsg(ctf_errno(fp)));
		free(args);
		return (B_FALSE);
	}

	for (i = 0; i < fi.ctc_argc; i++) {
		if (ctf_type_name(fp, args[i], buf, sizeof (buf)) == NULL) {
			warnx("failed to obtain type name for argument %u",
			    i, ctf_errmsg(ctf_errno(fp)));
			ret = B_FALSE;
			break;
		}

		if (strcmp(buf, argv[i]) != 0) {
			warnx("argument %u has wrong type: found %s, "
			    "expected %s", i, buf, argv[i]);
			ret = B_FALSE;
			break;
		}
	}

	free(args);
	return (ret);
}

boolean_t
ctftest_check_size(const char *type, ctf_file_t *fp, size_t size)
{
	ctf_id_t base;

	if ((base = ctftest_lookup_type(fp, type)) == CTF_ERR) {
		warnx("Failed to look up type %s", type);
		return (B_FALSE);
	}

	if (size != ctf_type_size(fp, base)) {
		warnx("%s has bad size, expected %lu, found %lu",
		    type, size, ctf_type_size(fp, base));
		return (B_FALSE);
	}

	return (B_TRUE);
}

typedef struct ctftest_duplicates {
	ctf_file_t *ctd_fp;
	char **ctd_names;
	size_t ctd_len;
	size_t ctd_curent;
	boolean_t ctd_ret;
} ctftest_duplicates_t;

static int
ctftest_duplicates_cb(ctf_id_t id, boolean_t root, void *arg)
{
	char buf[2048];
	ctftest_duplicates_t *dup = arg;
	size_t i;

	if (ctf_type_name(dup->ctd_fp, id, buf, sizeof (buf)) == NULL) {
		warnx("failed to lookup name for id %ld", id);
		dup->ctd_ret = B_FALSE;
		return (1);
	}

	for (i = 0; i < dup->ctd_curent; i++) {
		if (strcmp(buf, dup->ctd_names[i]) == 0) {
			warnx("encountered duplicate type '%s'", buf);
			dup->ctd_ret = B_FALSE;
			/*
			 * Don't break out of the loop and keep going in case we
			 * find another duplicate.
			 */
			return (0);
		}
	}

	if (dup->ctd_curent == dup->ctd_len) {
		char **n;
		size_t newlen = dup->ctd_len * 2;

		n = recallocarray(dup->ctd_names, dup->ctd_len, newlen,
		    sizeof (char *));
		if (n == NULL) {
			warnx("failed to resize type name array");
			dup->ctd_ret = B_FALSE;
			return (1);
		}

		dup->ctd_names = n;
		dup->ctd_len = newlen;
	}

	dup->ctd_names[dup->ctd_curent] = strdup(buf);
	if (dup->ctd_names[dup->ctd_curent] == NULL) {
		warn("failed to duplicate type name");
		dup->ctd_ret = B_FALSE;
		return (1);
	}
	dup->ctd_curent++;

	return (0);
}

boolean_t
ctftest_duplicates(ctf_file_t *fp)
{
	size_t i;
	ctftest_duplicates_t d;

	bzero(&d, sizeof (d));
	d.ctd_fp = fp;
	d.ctd_len = 4;
	d.ctd_ret = B_TRUE;
	d.ctd_names = recallocarray(NULL, 0, d.ctd_len, sizeof (char *));
	if (d.ctd_names == NULL) {
		warnx("failed to allocate duplicate name storage");
		return (B_FALSE);
	}

	(void) ctf_type_iter(fp, B_TRUE, ctftest_duplicates_cb, &d);

	for (i = 0; i < d.ctd_curent; i++) {
		free(d.ctd_names[i]);
	}
	free(d.ctd_names);

	return (d.ctd_ret);
}

boolean_t
ctftest_skip(check_skip_t skip)
{
	const char *compiler;

	if (skip == 0) {
		return (B_FALSE);
	}

	compiler = getenv("ctf_cc_type");
	if (compiler == NULL) {
		return (B_FALSE);
	}

	if ((skip & SKIP_CLANG) != 0 && strcmp(compiler, "clang") == 0)
		return (B_TRUE);

	return (B_FALSE);
}