summaryrefslogtreecommitdiff
path: root/src/chunklets/x86.c
blob: 506a6ec7798902969bcc6c528e495d4447cdb682 (plain) (blame)
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
/*
 * Copyright © Michael Smith <mikesmiffy128@gmail.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

// _Static_assert needs MSVC >= 2019, and this check is irrelevant on Windows
#ifndef _MSC_VER
_Static_assert((unsigned char)-1 == 255, "this code requires 8-bit chars");
#endif

#include "x86.h"

static int mrmsib(const unsigned char *p, int addrlen) {
	if (addrlen == 4 || *p & 0xC0) {
		int sib = addrlen == 4 && *p < 0xC0 && (*p & 7) == 4;
		switch (*p & 0xC0) {
			case 0x40: // disp8
				return 2 + sib;
			case 0: // disp16/32
				if ((*p & 7) != 5) {
					// disp8/32 via SIB
					if (sib && (p[1] & 7) == 5) return *p & 0x40 ? 3 : 6;
					return 1 + sib;
				}
			case 0x80:
				return 1 + addrlen + sib;
		}
	}
	if (addrlen == 2 && (*p & 0xC7) == 0x06) return 3;
	return 1; // note: include the mrm itself in the byte count
}

enum {
	CLASS_UNKNOWN,

	CLASS_PFX,
	CLASS_NO,
	CLASS_I8,
	CLASS_IW,
	CLASS_IWI,
	CLASS_I16,
	CLASS_MRM,
	CLASS_MRMI8,
	CLASS_MRMIW,
	CLASS_ENTER,
	CLASS_CRAZY8,
	CLASS_CRAZYW,
	CLASS_2BYTE,

	CLASS2B_NO = 1,
	CLASS2B_IW,
	CLASS2B_MRM,
	CLASS2B_MRMI8
};

// note: could theoretically shrink these tables down to 128B each although to
// do it at compile time without the use of _BitInt(4) would be kind of tricky.
static const unsigned char classtab[256] = {
#define TABLEENT(name, val) [val] = CLASS_PFX,
	X86_PREFIXES(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_NO,
	X86_OPS_1BYTE_NO(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_I8,
	X86_OPS_1BYTE_I8(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_IW,
	X86_OPS_1BYTE_IW(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_IWI,
	X86_OPS_1BYTE_IWI(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_I16,
	X86_OPS_1BYTE_I16(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_MRM,
	X86_OPS_1BYTE_MRM(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_MRMI8,
	X86_OPS_1BYTE_MRM_I8(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS_MRMIW,
	X86_OPS_1BYTE_MRM_IW(TABLEENT)
#undef TABLEENT
	[X86_ENTER] = CLASS_ENTER,
	[X86_CRAZY8] = CLASS_CRAZY8,
	[X86_CRAZYW] = CLASS_CRAZYW,
	[X86_2BYTE] = CLASS_2BYTE
};

static const unsigned char classtab_2b[256] = {
	// we don't support any 3 byte ops for now; implement if ever needed...
	[X86_3BYTE1] = CLASS_UNKNOWN,
	[X86_3BYTE2] = CLASS_UNKNOWN,
	[X86_3DNOW] = CLASS_UNKNOWN,
#define TABLEENT(name, val) [val] = CLASS2B_NO,
	X86_OPS_2BYTE_NO(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS2B_IW,
	X86_OPS_2BYTE_IW(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS2B_MRM,
	X86_OPS_2BYTE_MRM(TABLEENT)
#undef TABLEENT
#define TABLEENT(name, val) [val] = CLASS2B_MRMI8,
	X86_OPS_2BYTE_MRM_I8(TABLEENT)
#undef TABLEENT
};

#ifndef X86_DISABLE_SIZE_OPT
#if defined(__clang__)
__attribute((minsize))
#elif defined(__GNUC__)
__attribute((optimize("Os")))
#endif
#endif
int x86_len(const unsigned char *insn) {
#define CASES(name, _) case name:
	int pfxlen = 0, addrlen = 4, operandlen = 4;

p:	switch (classtab[*insn]) {
		case CLASS_UNKNOWN: return -1;
		case CLASS_PFX:
			switch (*insn) {
				case X86_PFX_ADSZ: addrlen = 2; goto P; // bit dumb sorry
				case X86_PFX_OPSZ: operandlen = 2;
P:				X86_SEG_PREFIXES(CASES)
				case X86_PFX_LOCK: case X86_PFX_REPN: case X86_PFX_REP:
					// instruction can only be 15 bytes. this could go over, oh
					// well, just don't want to loop for 8 million years
					if (++pfxlen == 14) return -1;
					++insn;
					goto p;
		}
		case CLASS_NO: return pfxlen + 1;
		case CLASS_I8: operandlen = 1;
		case CLASS_IW: return pfxlen + 1 + operandlen;
		case CLASS_IWI: return pfxlen + 1 + addrlen;
		case CLASS_I16: return pfxlen + 3;
		case CLASS_MRM: return pfxlen + 1 + mrmsib(insn + 1, addrlen);
		case CLASS_MRMI8: operandlen = 1;
		case CLASS_MRMIW:
			return pfxlen + 1 + operandlen + mrmsib(insn + 1, addrlen);
		case CLASS_ENTER: return pfxlen + 4;
		case CLASS_CRAZY8: operandlen = 1;
		case CLASS_CRAZYW:
			if ((insn[1] & 0x38) >= 0x10) operandlen = 0;
			return pfxlen + 1 + operandlen + mrmsib(insn + 1, addrlen);
		case CLASS_2BYTE: ++insn; goto b2;
	}
#if defined(__GNUC__) || defined(__clang__)
	__builtin_unreachable();
#elif defined(_MSC_VER)
	__assume(0);
#else
	return -1;
#endif

b2:	switch (classtab_2b[*insn]) {
		case CLASS_UNKNOWN: return -1;
		case CLASS2B_NO: return pfxlen + 2;
		case CLASS2B_IW: return pfxlen + 2 + operandlen;
		case CLASS2B_MRM: return pfxlen + 2 + mrmsib(insn + 1, addrlen);
		case CLASS2B_MRMI8: operandlen = 1;
			return pfxlen + 2 + operandlen + mrmsib(insn + 1, addrlen);
	}
#if defined(__GNUC__) || defined(__clang__)
	__builtin_unreachable();
#elif defined(_MSC_VER)
	__assume(0);
#else
	return -1;
#endif
#undef CASES
}

// vi: sw=4 ts=4 noet tw=80 cc=80