1
2
3
4 :(scenario check_missing_imm8_operand)
5 % Hide_errors = true;
6 == 0x1
7 cd
8 +error: 'cd' (software interrupt): missing imm8 operand
9
10 :(before "Pack Operands(segment code)")
11 check_operands(code);
12 if (trace_contains_errors()) return;
13
14 :(code)
15 void check_operands(const segment& code) {
16 trace(99, "transform") << "-- check operands" << end();
17 for (int i = 0; i < SIZE(code.lines); ++i) {
18 check_operands(code.lines.at(i));
19 if (trace_contains_errors()) return;
20 }
21 }
22
23 void check_operands(const line& inst) {
24 word op = preprocess_op(inst.words.at(0));
25 if (op.data == "0f") {
26 check_operands_0f(inst);
27 return;
28 }
29 if (op.data == "f3") {
30 check_operands_f3(inst);
31 return;
32 }
33 check_operands(inst, op);
34 }
35
36 word preprocess_op(word op) {
37 op.data = tolower(op.data.c_str());
38
39 if (starts_with(op.data, "0x"))
40 op.data = op.data.substr(2);
41 if (SIZE(op.data) == 1)
42 op.data = string("0")+op.data;
43 return op;
44 }
45
46 void test_preprocess_op() {
47 word w1; w1.data = "0xf";
48 word w2; w2.data = "0f";
49 CHECK_EQ(preprocess_op(w1).data, preprocess_op(w2).data);
50 }
51
52
53
54
55
56 :(before "End Types")
57 enum operand_type {
58
59 MODRM,
60 SUBOP,
61 DISP8,
62 DISP16,
63 DISP32,
64 IMM8,
65 IMM32,
66 NUM_OPERAND_TYPES
67 };
68 :(before "End Globals")
69 vector<string> Operand_type_name;
70 map<string, operand_type> Operand_type;
71 :(before "End One-time Setup")
72 init_op_types();
73 :(code)
74 void init_op_types() {
75 assert(NUM_OPERAND_TYPES <= 8);
76 Operand_type_name.resize(NUM_OPERAND_TYPES);
77
78 DEF(MODRM);
79 DEF(SUBOP);
80 DEF(DISP8);
81 DEF(DISP16);
82 DEF(DISP32);
83 DEF(IMM8);
84 DEF(IMM32);
85
86 }
87
88 :(before "End Globals")
89 map<string, uint8_t> Permitted_operands;
90 const uint8_t INVALID_OPERANDS = 0xff;
91 :(before "End One-time Setup")
92 init_permitted_operands();
93 :(code)
94 void init_permitted_operands() {
95
96
97 put(Permitted_operands, "f4", 0x00);
98
99 put(Permitted_operands, "40", 0x00);
100 put(Permitted_operands, "41", 0x00);
101 put(Permitted_operands, "42", 0x00);
102 put(Permitted_operands, "43", 0x00);
103 put(Permitted_operands, "44", 0x00);
104 put(Permitted_operands, "45", 0x00);
105 put(Permitted_operands, "46", 0x00);
106 put(Permitted_operands, "47", 0x00);
107
108 put(Permitted_operands, "48", 0x00);
109 put(Permitted_operands, "49", 0x00);
110 put(Permitted_operands, "4a", 0x00);
111 put(Permitted_operands, "4b", 0x00);
112 put(Permitted_operands, "4c", 0x00);
113 put(Permitted_operands, "4d", 0x00);
114 put(Permitted_operands, "4e", 0x00);
115 put(Permitted_operands, "4f", 0x00);
116
117 put(Permitted_operands, "50", 0x00);
118 put(Permitted_operands, "51", 0x00);
119 put(Permitted_operands, "52", 0x00);
120 put(Permitted_operands, "53", 0x00);
121 put(Permitted_operands, "54", 0x00);
122 put(Permitted_operands, "55", 0x00);
123 put(Permitted_operands, "56", 0x00);
124 put(Permitted_operands, "57", 0x00);
125
126 put(Permitted_operands, "58", 0x00);
127 put(Permitted_operands, "59", 0x00);
128 put(Permitted_operands, "5a", 0x00);
129 put(Permitted_operands, "5b", 0x00);
130 put(Permitted_operands, "5c", 0x00);
131 put(Permitted_operands, "5d", 0x00);
132 put(Permitted_operands, "5e", 0x00);
133 put(Permitted_operands, "5f", 0x00);
134
135 put(Permitted_operands, "c3", 0x00);
136
137
138
139
140
141
142 put(Permitted_operands, "eb", 0x04);
143 put(Permitted_operands, "74", 0x04);
144 put(Permitted_operands, "75", 0x04);
145 put(Permitted_operands, "7c", 0x04);
146 put(Permitted_operands, "7d", 0x04);
147 put(Permitted_operands, "7e", 0x04);
148 put(Permitted_operands, "7f", 0x04);
149
150
151
152
153 put(Permitted_operands, "e9", 0x08);
154
155
156
157
158 put(Permitted_operands, "e8", 0x10);
159
160
161
162
163 put(Permitted_operands, "cd", 0x20);
164
165
166
167
168 put(Permitted_operands, "05", 0x40);
169 put(Permitted_operands, "2d", 0x40);
170 put(Permitted_operands, "25", 0x40);
171 put(Permitted_operands, "0d", 0x40);
172 put(Permitted_operands, "35", 0x40);
173 put(Permitted_operands, "3d", 0x40);
174 put(Permitted_operands, "68", 0x40);
175
176 put(Permitted_operands, "b8", 0x40);
177 put(Permitted_operands, "b9", 0x40);
178 put(Permitted_operands, "ba", 0x40);
179 put(Permitted_operands, "bb", 0x40);
180 put(Permitted_operands, "bc", 0x40);
181 put(Permitted_operands, "bd", 0x40);
182 put(Permitted_operands, "be", 0x40);
183 put(Permitted_operands, "bf", 0x40);
184
185
186
187
188
189
190 put(Permitted_operands, "01", 0x01);
191 put(Permitted_operands, "03", 0x01);
192
193 put(Permitted_operands, "29", 0x01);
194 put(Permitted_operands, "2b", 0x01);
195
196 put(Permitted_operands, "21", 0x01);
197 put(Permitted_operands, "23", 0x01);
198
199 put(Permitted_operands, "09", 0x01);
200 put(Permitted_operands, "0b", 0x01);
201
202 put(Permitted_operands, "31", 0x01);
203 put(Permitted_operands, "33", 0x01);
204
205 put(Permitted_operands, "39", 0x01);
206 put(Permitted_operands, "3b", 0x01);
207
208 put(Permitted_operands, "88", 0x01);
209 put(Permitted_operands, "89", 0x01);
210 put(Permitted_operands, "8a", 0x01);
211 put(Permitted_operands, "8b", 0x01);
212
213 put(Permitted_operands, "87", 0x01);
214
215 put(Permitted_operands, "8d", 0x01);
216
217 put(Permitted_operands, "8f", 0x01);
218
219
220
221
222 put(Permitted_operands, "f7", 0x03);
223 put(Permitted_operands, "ff", 0x03);
224
225
226
227
228 put(Permitted_operands, "c7", 0x41);
229
230
231
232
233 put(Permitted_operands, "81", 0x43);
234
235
236 }
237
238 :(code)
239
240
241
242
243 void check_operands(const line& inst, const word& op) {
244 if (!is_hex_byte(op)) return;
245 uint8_t expected_bitvector = get(Permitted_operands, op.data);
246 if (HAS(expected_bitvector, MODRM)) {
247 check_operands_modrm(inst, op);
248 compare_bitvector_modrm(inst, expected_bitvector, op);
249 }
250 else {
251 compare_bitvector(inst, expected_bitvector, op);
252 }
253 }
254
255
256
257 void compare_bitvector(const line& inst, uint8_t expected, const word& op) {
258 if (all_hex_bytes(inst) && has_operands(inst)) return;
259 uint8_t bitvector = compute_operand_bitvector(inst);
260 if (trace_contains_errors()) return;
261 if (bitvector == expected) return;
262 for (int i = 0; i < NUM_OPERAND_TYPES; ++i, bitvector >>= 1, expected >>= 1) {
263
264 if ((bitvector & 0x1) == (expected & 0x1)) continue;
265 const string& optype = Operand_type_name.at(i);
266 if ((bitvector & 0x1) > (expected & 0x1))
267 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": unexpected " << optype << " operand\n" << end();
268 else
269 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": missing " << optype << " operand\n" << end();
270
271 }
272
273 }
274
275 string maybe_name(const word& op) {
276 if (!is_hex_byte(op)) return "";
277 if (!contains_key(Name, op.data)) return "";
278
279 const string& s = get(Name, op.data);
280 return " ("+s.substr(0, s.find(" ("))+')';
281 }
282
283 uint32_t compute_operand_bitvector(const line& inst) {
284 uint32_t bitvector = 0;
285 for (int i = 1; i < SIZE(inst.words); ++i) {
286 bitvector = bitvector | bitvector_for_operand(inst.words.at(i));
287 if (trace_contains_errors()) return INVALID_OPERANDS;
288 }
289 return bitvector;
290 }
291
292 bool has_operands(const line& inst) {
293 return SIZE(inst.words) > first_operand(inst);
294 }
295
296 int first_operand(const line& inst) {
297 if (inst.words.at(0).data == "0f") return 2;
298 if (inst.words.at(0).data == "f2" || inst.words.at(0).data == "f3") {
299 if (inst.words.at(1).data == "0f")
300 return 3;
301 else
302 return 2;
303 }
304 return 1;
305 }
306
307
308
309 uint32_t bitvector_for_operand(const word& w) {
310 uint32_t bv = 0;
311 bool found = false;
312 for (int i = 0; i < SIZE(w.metadata); ++i) {
313 const string& curr = w.metadata.at(i);
314 if (!contains_key(Operand_type, curr)) continue;
315 if (found) {
316 raise << "'" << w.original << "' has conflicting operand types; it should have only one\n" << end();
317 return INVALID_OPERANDS;
318 }
319 bv = (1 << get(Operand_type, curr));
320 found = true;
321 }
322 return bv;
323 }
324
325 :(scenario conflicting_operand_type)
326 % Hide_errors = true;
327 == 0x1
328 cd/software-interrupt 80/imm8/imm32
329 +error: '80/imm8/imm32' has conflicting operand types; it should have only one
330
331
332
333
334 :(scenario check_missing_mod_operand)
335 % Hide_errors = true;
336 == 0x1
337 81 0/add/subop 3/rm32/ebx 1/imm32
338 +error: '81 0/add/subop 3/rm32/ebx 1/imm32' (combine rm32 with imm32 based on subop): missing mod operand
339
340 :(code)
341 void check_operands_modrm(const line& inst, const word& op) {
342 if (all_hex_bytes(inst)) return;
343 check_operand_metadata_present(inst, "mod", op);
344 check_operand_metadata_present(inst, "rm32", op);
345
346 if (op.data == "81" || op.data == "8f" || op.data == "ff") {
347 check_operand_metadata_present(inst, "subop", op);
348 check_operand_metadata_absent(inst, "r32", op, "should be replaced by subop");
349 }
350 if (trace_contains_errors()) return;
351 if (metadata(inst, "rm32").data != "4") return;
352
353 uint8_t mod = hex_byte(metadata(inst, "mod").data);
354 if (mod != 3) {
355 check_operand_metadata_present(inst, "base", op);
356 check_operand_metadata_present(inst, "index", op);
357 }
358 else {
359 check_operand_metadata_absent(inst, "base", op, "direct mode");
360 check_operand_metadata_absent(inst, "index", op, "direct mode");
361 }
362
363 }
364
365
366
367
368 void compare_bitvector_modrm(const line& inst, uint8_t expected, const word& op) {
369 if (all_hex_bytes(inst) && has_operands(inst)) return;
370 uint8_t bitvector = compute_operand_bitvector(inst);
371 if (trace_contains_errors()) return;
372 expected = CLEAR(expected, MODRM);
373 if (bitvector == expected) return;
374 for (int i = 0; i < NUM_OPERAND_TYPES; ++i, bitvector >>= 1, expected >>= 1) {
375
376 if ((bitvector & 0x1) == (expected & 0x1)) continue;
377 const string& optype = Operand_type_name.at(i);
378 if (i == DISP8) {
379 int32_t mod = parse_int(metadata(inst, "mod").data);
380 if (mod != 1)
381 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": unexpected " << optype << " operand\n" << end();
382 continue;
383 }
384 if (i == DISP32) {
385 int32_t mod = parse_int(metadata(inst, "mod").data);
386 int32_t rm32 = parse_int(metadata(inst, "rm32").data);
387 if (mod == 0 && rm32 == 5)
388 ;
389 else if (mod != 2)
390 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": unexpected " << optype << " operand\n" << end();
391 continue;
392 }
393 if ((bitvector & 0x1) > (expected & 0x1))
394 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": unexpected " << optype << " operand\n" << end();
395 else
396 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": missing " << optype << " operand\n" << end();
397
398 }
399
400 }
401
402 void check_operand_metadata_present(const line& inst, const string& type, const word& op) {
403 if (!has_operand_metadata(inst, type))
404 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": missing " << type << " operand\n" << end();
405 }
406
407 void check_operand_metadata_absent(const line& inst, const string& type, const word& op, const string& msg) {
408 if (has_operand_metadata(inst, type))
409 raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": unexpected " << type << " operand (" << msg << ")\n" << end();
410 }
411
412 :(scenarios transform)
413 :(scenario modrm_with_displacement)
414 % Reg[EAX].u = 0x1;
415 == 0x1
416
417 8b/copy 1/mod/lookup+disp8 0/rm32/EAX 2/r32/EDX 4/disp8
418 $error: 0
419 :(scenarios run)
420
421 :(scenario conflicting_operands_in_modrm_instruction)
422 % Hide_errors = true;
423 == 0x1
424 01/add 0/mod 3/mod
425 +error: '01/add 0/mod 3/mod' has conflicting mod operands
426
427 :(scenario conflicting_operand_type_modrm)
428 % Hide_errors = true;
429 == 0x1
430 01/add 0/mod 3/rm32/r32
431 +error: '3/rm32/r32' has conflicting operand types; it should have only one
432
433 :(scenario check_missing_rm32_operand)
434 % Hide_errors = true;
435 == 0x1
436 81 0/add/subop 0/mod 1/imm32
437 +error: '81 0/add/subop 0/mod 1/imm32' (combine rm32 with imm32 based on subop): missing rm32 operand
438
439 :(scenario check_missing_subop_operand)
440 % Hide_errors = true;
441 == 0x1
442 81 0/mod 3/rm32/ebx 1/imm32
443 +error: '81 0/mod 3/rm32/ebx 1/imm32' (combine rm32 with imm32 based on subop): missing subop operand
444
445 :(scenario check_missing_base_operand)
446 % Hide_errors = true;
447 == 0x1
448 81 0/add/subop 0/mod/indirect 4/rm32/use-sib 1/imm32
449 +error: '81 0/add/subop 0/mod/indirect 4/rm32/use-sib 1/imm32' (combine rm32 with imm32 based on subop): missing base operand
450
451 :(scenario check_missing_index_operand)
452 % Hide_errors = true;
453 == 0x1
454 81 0/add/subop 0/mod/indirect 4/rm32/use-sib 0/base 1/imm32
455 +error: '81 0/add/subop 0/mod/indirect 4/rm32/use-sib 0/base 1/imm32' (combine rm32 with imm32 based on subop): missing index operand
456
457 :(scenario check_missing_base_operand_2)
458 % Hide_errors = true;
459 == 0x1
460 81 0/add/subop 0/mod/indirect 4/rm32/use-sib 2/index 3/scale 1/imm32
461 +error: '81 0/add/subop 0/mod/indirect 4/rm32/use-sib 2/index 3/scale 1/imm32' (combine rm32 with imm32 based on subop): missing base operand
462
463 :(scenario check_extra_displacement)
464 % Hide_errors = true;
465 == 0x1
466 89/copy 0/mod/indirect 0/rm32/EAX 1/r32/ECX 4/disp8
467 +error: '89/copy 0/mod/indirect 0/rm32/EAX 1/r32/ECX 4/disp8' (copy r32 to rm32): unexpected disp8 operand
468
469 :(scenario check_base_operand_not_needed_in_direct_mode)
470 == 0x1
471 81 0/add/subop 3/mod/indirect 4/rm32/use-sib 1/imm32
472 $error: 0
473
474
475
476 :(code)
477 void check_operands_0f(const line& inst) {
478 assert(inst.words.at(0).data == "0f");
479 if (SIZE(inst.words) == 1) {
480 raise << "opcode '0f' requires a second opcode\n" << end();
481 return;
482 }
483 word op = preprocess_op(inst.words.at(1));
484 if (!contains_key(Name_0f, op.data)) {
485 raise << "unknown 2-byte opcode '0f " << op.data << "'\n" << end();
486 return;
487 }
488 check_operands_0f(inst, op);
489 }
490
491 void check_operands_f3(const line& ) {
492 raise << "no supported opcodes starting with f3\n" << end();
493 }
494
495 :(scenario check_missing_disp16_operand)
496 % Hide_errors = true;
497 == 0x1
498
499
500
501 0f 84
502 +error: '0f 84' (jump disp16 bytes away if equal, if ZF is set): missing disp16 operand
503
504 :(before "End Globals")
505 map<string, uint8_t> Permitted_operands_0f;
506 :(before "End Init Permitted Operands")
507
508
509
510 put_new(Permitted_operands_0f, "84", 0x08);
511 put_new(Permitted_operands_0f, "85", 0x08);
512 put_new(Permitted_operands_0f, "8c", 0x08);
513 put_new(Permitted_operands_0f, "8d", 0x08);
514 put_new(Permitted_operands_0f, "8e", 0x08);
515 put_new(Permitted_operands_0f, "8f", 0x08);
516
517
518
519
520 put_new(Permitted_operands_0f, "af", 0x01);
521
522 :(code)
523 void check_operands_0f(const line& inst, const word& op) {
524 uint8_t expected_bitvector = get(Permitted_operands_0f, op.data);
525 if (HAS(expected_bitvector, MODRM))
526 check_operands_modrm(inst, op);
527 compare_bitvector_0f(inst, CLEAR(expected_bitvector, MODRM), op);
528 }
529
530 void compare_bitvector_0f(const line& inst, uint8_t expected, const word& op) {
531 if (all_hex_bytes(inst) && has_operands(inst)) return;
532 uint8_t bitvector = compute_operand_bitvector(inst);
533 if (trace_contains_errors()) return;
534 if (bitvector == expected) return;
535 for (int i = 0; i < NUM_OPERAND_TYPES; ++i, bitvector >>= 1, expected >>= 1) {
536
537 if ((bitvector & 0x1) == (expected & 0x1)) continue;
538 const string& optype = Operand_type_name.at(i);
539 if ((bitvector & 0x1) > (expected & 0x1))
540 raise << "'" << to_string(inst) << "'" << maybe_name_0f(op) << ": unexpected " << optype << " operand\n" << end();
541 else
542 raise << "'" << to_string(inst) << "'" << maybe_name_0f(op) << ": missing " << optype << " operand\n" << end();
543
544 }
545
546 }
547
548 string maybe_name_0f(const word& op) {
549 if (!is_hex_byte(op)) return "";
550 if (!contains_key(Name_0f, op.data)) return "";
551
552 const string& s = get(Name_0f, op.data);
553 return " ("+s.substr(0, s.find(" ("))+')';
554 }
555
556 string tolower(const char* s) {
557 ostringstream out;
558 for (; *s; ++s)
559 out << static_cast<char>(tolower(*s));
560 return out.str();
561 }
562
563
564
565
566
567 :(before "End Includes")
568