2 * x86 decoder sanity test - based on test_get_insn.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2009
19 * Copyright (C) Hitachi, Ltd., 2011
27 #include <sys/types.h>
31 #define unlikely(cond) (cond)
32 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
39 * Test of instruction analysis against tampering.
40 * Feed random binary to instruction decoder and ensure not to
41 * access out-of-instruction-buffer.
44 #define DEFAULT_MAX_ITER 10000
47 static const char *prog; /* Program name */
48 static int verbose; /* Verbosity */
49 static int x86_64; /* x86-64 bit mode flag */
50 static unsigned int seed; /* Random seed */
51 static unsigned long iter_start; /* Start of iteration number */
52 static unsigned long iter_end = DEFAULT_MAX_ITER; /* End of iteration number */
53 static FILE *input_file; /* Input file name */
55 static void usage(const char *err)
58 fprintf(stderr, "%s: Error: %s\n\n", prog, err);
59 fprintf(stderr, "Usage: %s [-y|-n|-v] [-s seed[,no]] [-m max] [-i input]\n", prog);
60 fprintf(stderr, "\t-y 64bit mode\n");
61 fprintf(stderr, "\t-n 32bit mode\n");
62 fprintf(stderr, "\t-v Verbosity(-vv dumps any decoded result)\n");
63 fprintf(stderr, "\t-s Give a random seed (and iteration number)\n");
64 fprintf(stderr, "\t-m Give a maximum iteration number\n");
65 fprintf(stderr, "\t-i Give an input file with decoded binary\n");
69 static void dump_field(FILE *fp, const char *name, const char *indent,
70 struct insn_field *field)
72 fprintf(fp, "%s.%s = {\n", indent, name);
73 fprintf(fp, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
74 indent, field->value, field->bytes[0], field->bytes[1],
75 field->bytes[2], field->bytes[3]);
76 fprintf(fp, "%s\t.got = %d, .nbytes = %d},\n", indent,
77 field->got, field->nbytes);
80 static void dump_insn(FILE *fp, struct insn *insn)
82 fprintf(fp, "Instruction = {\n");
83 dump_field(fp, "prefixes", "\t", &insn->prefixes);
84 dump_field(fp, "rex_prefix", "\t", &insn->rex_prefix);
85 dump_field(fp, "vex_prefix", "\t", &insn->vex_prefix);
86 dump_field(fp, "opcode", "\t", &insn->opcode);
87 dump_field(fp, "modrm", "\t", &insn->modrm);
88 dump_field(fp, "sib", "\t", &insn->sib);
89 dump_field(fp, "displacement", "\t", &insn->displacement);
90 dump_field(fp, "immediate1", "\t", &insn->immediate1);
91 dump_field(fp, "immediate2", "\t", &insn->immediate2);
92 fprintf(fp, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
93 insn->attr, insn->opnd_bytes, insn->addr_bytes);
94 fprintf(fp, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
95 insn->length, insn->x86_64, insn->kaddr);
98 static void dump_stream(FILE *fp, const char *msg, unsigned long nr_iter,
99 unsigned char *insn_buf, struct insn *insn)
103 fprintf(fp, "%s:\n", msg);
107 fprintf(fp, "You can reproduce this with below command(s);\n");
109 /* Input a decoded instruction sequence directly */
110 fprintf(fp, " $ echo ");
111 for (i = 0; i < MAX_INSN_SIZE; i++)
112 fprintf(fp, " %02x", insn_buf[i]);
113 fprintf(fp, " | %s -i -\n", prog);
116 fprintf(fp, "Or \n");
117 /* Give a seed and iteration number */
118 fprintf(fp, " $ %s -s 0x%x,%lu\n", prog, seed, nr_iter);
122 static void init_random_seed(void)
126 fd = open("/dev/urandom", O_RDONLY);
130 if (read(fd, &seed, sizeof(seed)) != sizeof(seed))
136 usage("Failed to open /dev/urandom");
139 /* Read given instruction sequence from the input file */
140 static int read_next_insn(unsigned char *insn_buf)
142 char buf[256] = "", *tmp;
145 tmp = fgets(buf, ARRAY_SIZE(buf), input_file);
146 if (tmp == NULL || feof(input_file))
149 for (i = 0; i < MAX_INSN_SIZE; i++) {
150 insn_buf[i] = (unsigned char)strtoul(tmp, &tmp, 16);
158 static int generate_insn(unsigned char *insn_buf)
163 return read_next_insn(insn_buf);
165 /* Fills buffer with random binary up to MAX_INSN_SIZE */
166 for (i = 0; i < MAX_INSN_SIZE - 1; i += 2)
167 *(unsigned short *)(&insn_buf[i]) = random() & 0xffff;
169 while (i < MAX_INSN_SIZE)
170 insn_buf[i++] = random() & 0xff;
175 static void parse_args(int argc, char **argv)
182 while ((c = getopt(argc, argv, "ynvs:m:i:")) != -1) {
194 if (strcmp("-", optarg) == 0)
197 input_file = fopen(optarg, "r");
199 usage("Failed to open input file");
202 seed = (unsigned int)strtoul(optarg, &tmp, 0);
205 iter_start = strtoul(optarg, &tmp, 0);
207 if (*tmp != '\0' || tmp == optarg)
208 usage("Failed to parse seed");
212 iter_end = strtoul(optarg, &tmp, 0);
213 if (*tmp != '\0' || tmp == optarg)
214 usage("Failed to parse max_iter");
222 if (iter_end < iter_start)
223 usage("Max iteration number must be bigger than iter-num");
225 if (set_seed && input_file)
226 usage("Don't use input file (-i) with random seed (-s)");
228 /* Initialize random seed */
230 if (!set_seed) /* No seed is given */
236 int main(int argc, char **argv)
242 unsigned char insn_buf[MAX_INSN_SIZE * 2];
244 parse_args(argc, argv);
246 /* Prepare stop bytes with NOPs */
247 memset(insn_buf + MAX_INSN_SIZE, INSN_NOP, MAX_INSN_SIZE);
249 for (i = 0; i < iter_end; i++) {
250 if (generate_insn(insn_buf) <= 0)
253 if (i < iter_start) /* Skip to given iteration number */
256 /* Decode an instruction */
257 insn_init(&insn, insn_buf, sizeof(insn_buf), x86_64);
258 insn_get_length(&insn);
260 if (insn.next_byte <= insn.kaddr ||
261 insn.kaddr + MAX_INSN_SIZE < insn.next_byte) {
262 /* Access out-of-range memory */
263 dump_stream(stderr, "Error: Found an access violation", i, insn_buf, &insn);
265 } else if (verbose && !insn_complete(&insn))
266 dump_stream(stdout, "Info: Found an undecodable input", i, insn_buf, &insn);
267 else if (verbose >= 2)
268 dump_insn(stdout, &insn);
272 fprintf(stdout, "%s: %s: decoded and checked %d %s instructions with %d errors (seed:0x%x)\n",
274 (errors) ? "Failure" : "Success",
276 (input_file) ? "given" : "random",
280 return errors ? 1 : 0;