summaryrefslogtreecommitdiff
path: root/usr/src/cmd/dtrace/test/tst/common/bitfields/tst.bitfields.c
blob: 180127191a4226c28a63232207deae446cfed137 (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
/*
 * 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 2022 Oxide Computer Company
 */

/*
 * This is designed to allow us to execute print() and various dereferencing
 * operations with bitfields. It reads the values from argc and passes them to
 * functions that we can then take apart. Crtitically this uses bitfields
 * constructed via CTF and not the D compiler.
 */

#include <err.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>

typedef struct bit0 {
	uint32_t a:3;
	uint32_t b:2;
	uint32_t c:1;
	uint32_t d:1;
	uint32_t e:1;
	uint32_t f:1;
	uint32_t g:3;
	uint32_t h:3;
	uint32_t i:5;
	uint32_t j:4;
	uint32_t k:6;
	uint32_t l:1;
	uint32_t m:1;
} bit0_t;

typedef struct bit1 {
	uint16_t a:1;
	uint16_t b:8;
	uint16_t c:3;
	uint16_t d:2;
	uint16_t e:1;
	uint16_t f:1;
} bit1_t;

void
mumble(FILE *f, bit0_t *zero, bit1_t *one)
{
	(void) fprintf(f, "%u\n%u\n", zero->k, one->d);
}

int
main(int argc, char *argv[])
{
	unsigned long l;
	uint16_t u16;
	uint32_t u32;
	FILE *f;

	if (argc != 3) {
		errx(EXIT_FAILURE, "Need two ints");
	}

	f = fopen("/dev/null", "rw+");
	if (f == NULL) {
		err(EXIT_FAILURE, "failed to open /dev/null");
	}

	errno = 0;
	l = strtoul(argv[1], NULL, 0);
	if (errno != 0 || l == 0 || l > UINT16_MAX) {
		errx(EXIT_FAILURE, "invalid u16 value: %s", argv[1]);
	}
	u16 = (uint16_t)l;

	l = strtoul(argv[2], NULL, 0);
	if (errno != 0 || l == 0 || l > UINT32_MAX) {
		errx(EXIT_FAILURE, "invalid u32 value: %s", argv[1]);
	}
	u32 = (uint32_t)l;
	mumble(f, (bit0_t *)&u32, (bit1_t *)&u16);

	return (0);
}