1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * x86 decoder sanity test - based on test_get_insn.c
5 * Copyright (C) IBM Corporation, 2009
6 * Copyright (C) Hitachi, Ltd., 2011
14 #include <sys/types.h>
22 * Test of instruction analysis against tampering.
23 * Feed random binary to instruction decoder and ensure not to
24 * access out-of-instruction-buffer.
27 #define DEFAULT_MAX_ITER 10000
30 static const char *prog
; /* Program name */
31 static int verbose
; /* Verbosity */
32 static int x86_64
; /* x86-64 bit mode flag */
33 static unsigned int seed
; /* Random seed */
34 static unsigned long iter_start
; /* Start of iteration number */
35 static unsigned long iter_end
= DEFAULT_MAX_ITER
; /* End of iteration number */
36 static FILE *input_file
; /* Input file name */
38 static void usage(const char *err
)
41 fprintf(stderr
, "%s: Error: %s\n\n", prog
, err
);
42 fprintf(stderr
, "Usage: %s [-y|-n|-v] [-s seed[,no]] [-m max] [-i input]\n", prog
);
43 fprintf(stderr
, "\t-y 64bit mode\n");
44 fprintf(stderr
, "\t-n 32bit mode\n");
45 fprintf(stderr
, "\t-v Verbosity(-vv dumps any decoded result)\n");
46 fprintf(stderr
, "\t-s Give a random seed (and iteration number)\n");
47 fprintf(stderr
, "\t-m Give a maximum iteration number\n");
48 fprintf(stderr
, "\t-i Give an input file with decoded binary\n");
52 static void dump_field(FILE *fp
, const char *name
, const char *indent
,
53 struct insn_field
*field
)
55 fprintf(fp
, "%s.%s = {\n", indent
, name
);
56 fprintf(fp
, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
57 indent
, field
->value
, field
->bytes
[0], field
->bytes
[1],
58 field
->bytes
[2], field
->bytes
[3]);
59 fprintf(fp
, "%s\t.got = %d, .nbytes = %d},\n", indent
,
60 field
->got
, field
->nbytes
);
63 static void dump_insn(FILE *fp
, struct insn
*insn
)
65 fprintf(fp
, "Instruction = {\n");
66 dump_field(fp
, "prefixes", "\t", &insn
->prefixes
);
67 dump_field(fp
, "rex_prefix", "\t", &insn
->rex_prefix
);
68 dump_field(fp
, "vex_prefix", "\t", &insn
->vex_prefix
);
69 dump_field(fp
, "opcode", "\t", &insn
->opcode
);
70 dump_field(fp
, "modrm", "\t", &insn
->modrm
);
71 dump_field(fp
, "sib", "\t", &insn
->sib
);
72 dump_field(fp
, "displacement", "\t", &insn
->displacement
);
73 dump_field(fp
, "immediate1", "\t", &insn
->immediate1
);
74 dump_field(fp
, "immediate2", "\t", &insn
->immediate2
);
75 fprintf(fp
, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
76 insn
->attr
, insn
->opnd_bytes
, insn
->addr_bytes
);
77 fprintf(fp
, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
78 insn
->length
, insn
->x86_64
, insn
->kaddr
);
81 static void dump_stream(FILE *fp
, const char *msg
, unsigned long nr_iter
,
82 unsigned char *insn_buff
, struct insn
*insn
)
86 fprintf(fp
, "%s:\n", msg
);
90 fprintf(fp
, "You can reproduce this with below command(s);\n");
92 /* Input a decoded instruction sequence directly */
93 fprintf(fp
, " $ echo ");
94 for (i
= 0; i
< MAX_INSN_SIZE
; i
++)
95 fprintf(fp
, " %02x", insn_buff
[i
]);
96 fprintf(fp
, " | %s -i -\n", prog
);
100 /* Give a seed and iteration number */
101 fprintf(fp
, " $ %s -s 0x%x,%lu\n", prog
, seed
, nr_iter
);
105 static void init_random_seed(void)
109 fd
= open("/dev/urandom", O_RDONLY
);
113 if (read(fd
, &seed
, sizeof(seed
)) != sizeof(seed
))
119 usage("Failed to open /dev/urandom");
122 /* Read given instruction sequence from the input file */
123 static int read_next_insn(unsigned char *insn_buff
)
125 char buf
[256] = "", *tmp
;
128 tmp
= fgets(buf
, ARRAY_SIZE(buf
), input_file
);
129 if (tmp
== NULL
|| feof(input_file
))
132 for (i
= 0; i
< MAX_INSN_SIZE
; i
++) {
133 insn_buff
[i
] = (unsigned char)strtoul(tmp
, &tmp
, 16);
141 static int generate_insn(unsigned char *insn_buff
)
146 return read_next_insn(insn_buff
);
148 /* Fills buffer with random binary up to MAX_INSN_SIZE */
149 for (i
= 0; i
< MAX_INSN_SIZE
- 1; i
+= 2)
150 *(unsigned short *)(&insn_buff
[i
]) = random() & 0xffff;
152 while (i
< MAX_INSN_SIZE
)
153 insn_buff
[i
++] = random() & 0xff;
158 static void parse_args(int argc
, char **argv
)
165 while ((c
= getopt(argc
, argv
, "ynvs:m:i:")) != -1) {
177 if (strcmp("-", optarg
) == 0)
180 input_file
= fopen(optarg
, "r");
182 usage("Failed to open input file");
185 seed
= (unsigned int)strtoul(optarg
, &tmp
, 0);
188 iter_start
= strtoul(optarg
, &tmp
, 0);
190 if (*tmp
!= '\0' || tmp
== optarg
)
191 usage("Failed to parse seed");
195 iter_end
= strtoul(optarg
, &tmp
, 0);
196 if (*tmp
!= '\0' || tmp
== optarg
)
197 usage("Failed to parse max_iter");
205 if (iter_end
< iter_start
)
206 usage("Max iteration number must be bigger than iter-num");
208 if (set_seed
&& input_file
)
209 usage("Don't use input file (-i) with random seed (-s)");
211 /* Initialize random seed */
213 if (!set_seed
) /* No seed is given */
219 int main(int argc
, char **argv
)
225 unsigned char insn_buff
[MAX_INSN_SIZE
* 2];
227 parse_args(argc
, argv
);
229 /* Prepare stop bytes with NOPs */
230 memset(insn_buff
+ MAX_INSN_SIZE
, INSN_NOP
, MAX_INSN_SIZE
);
232 for (i
= 0; i
< iter_end
; i
++) {
233 if (generate_insn(insn_buff
) <= 0)
236 if (i
< iter_start
) /* Skip to given iteration number */
239 /* Decode an instruction */
240 ret
= insn_decode(&insn
, insn_buff
, sizeof(insn_buff
),
241 x86_64
? INSN_MODE_64
: INSN_MODE_32
);
243 if (insn
.next_byte
<= insn
.kaddr
||
244 insn
.kaddr
+ MAX_INSN_SIZE
< insn
.next_byte
) {
245 /* Access out-of-range memory */
246 dump_stream(stderr
, "Error: Found an access violation", i
, insn_buff
, &insn
);
248 } else if (verbose
&& ret
< 0)
249 dump_stream(stdout
, "Info: Found an undecodable input", i
, insn_buff
, &insn
);
250 else if (verbose
>= 2)
251 dump_insn(stdout
, &insn
);
255 fprintf((errors
) ? stderr
: stdout
,
256 "%s: %s: decoded and checked %d %s instructions with %d errors (seed:0x%x)\n",
258 (errors
) ? "Failure" : "Success",
260 (input_file
) ? "given" : "random",
264 return errors
? 1 : 0;