^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * x86 decoder sanity test - based on test_get_insn.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) IBM Corporation, 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) Hitachi, Ltd., 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define unlikely(cond) (cond)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/insn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <inat.c>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <insn.c>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Test of instruction analysis against tampering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Feed random binary to instruction decoder and ensure not to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * access out-of-instruction-buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define DEFAULT_MAX_ITER 10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define INSN_NOP 0x90
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static const char *prog; /* Program name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int verbose; /* Verbosity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int x86_64; /* x86-64 bit mode flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static unsigned int seed; /* Random seed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static unsigned long iter_start; /* Start of iteration number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static unsigned long iter_end = DEFAULT_MAX_ITER; /* End of iteration number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static FILE *input_file; /* Input file name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void usage(const char *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fprintf(stderr, "%s: Error: %s\n\n", prog, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) fprintf(stderr, "Usage: %s [-y|-n|-v] [-s seed[,no]] [-m max] [-i input]\n", prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) fprintf(stderr, "\t-y 64bit mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) fprintf(stderr, "\t-n 32bit mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) fprintf(stderr, "\t-v Verbosity(-vv dumps any decoded result)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) fprintf(stderr, "\t-s Give a random seed (and iteration number)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) fprintf(stderr, "\t-m Give a maximum iteration number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) fprintf(stderr, "\t-i Give an input file with decoded binary\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void dump_field(FILE *fp, const char *name, const char *indent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct insn_field *field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) fprintf(fp, "%s.%s = {\n", indent, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) fprintf(fp, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) indent, field->value, field->bytes[0], field->bytes[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) field->bytes[2], field->bytes[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) fprintf(fp, "%s\t.got = %d, .nbytes = %d},\n", indent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) field->got, field->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static void dump_insn(FILE *fp, struct insn *insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fprintf(fp, "Instruction = {\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dump_field(fp, "prefixes", "\t", &insn->prefixes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dump_field(fp, "rex_prefix", "\t", &insn->rex_prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dump_field(fp, "vex_prefix", "\t", &insn->vex_prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dump_field(fp, "opcode", "\t", &insn->opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dump_field(fp, "modrm", "\t", &insn->modrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dump_field(fp, "sib", "\t", &insn->sib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dump_field(fp, "displacement", "\t", &insn->displacement);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dump_field(fp, "immediate1", "\t", &insn->immediate1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dump_field(fp, "immediate2", "\t", &insn->immediate2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) fprintf(fp, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) insn->attr, insn->opnd_bytes, insn->addr_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) fprintf(fp, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) insn->length, insn->x86_64, insn->kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void dump_stream(FILE *fp, const char *msg, unsigned long nr_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned char *insn_buff, struct insn *insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) fprintf(fp, "%s:\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dump_insn(fp, insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) fprintf(fp, "You can reproduce this with below command(s);\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Input a decoded instruction sequence directly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) fprintf(fp, " $ echo ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) for (i = 0; i < MAX_INSN_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) fprintf(fp, " %02x", insn_buff[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) fprintf(fp, " | %s -i -\n", prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!input_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) fprintf(fp, "Or \n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* Give a seed and iteration number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) fprintf(fp, " $ %s -s 0x%x,%lu\n", prog, seed, nr_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void init_random_seed(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) fd = open("/dev/urandom", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (read(fd, &seed, sizeof(seed)) != sizeof(seed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) usage("Failed to open /dev/urandom");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Read given instruction sequence from the input file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int read_next_insn(unsigned char *insn_buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) char buf[256] = "", *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) tmp = fgets(buf, ARRAY_SIZE(buf), input_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (tmp == NULL || feof(input_file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) for (i = 0; i < MAX_INSN_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) insn_buff[i] = (unsigned char)strtoul(tmp, &tmp, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (*tmp != ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int generate_insn(unsigned char *insn_buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (input_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return read_next_insn(insn_buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* Fills buffer with random binary up to MAX_INSN_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) for (i = 0; i < MAX_INSN_SIZE - 1; i += 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) *(unsigned short *)(&insn_buff[i]) = random() & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) while (i < MAX_INSN_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) insn_buff[i++] = random() & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void parse_args(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) char *tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int set_seed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) prog = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) while ((c = getopt(argc, argv, "ynvs:m:i:")) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) case 'y':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) x86_64 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) x86_64 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) case 'v':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) verbose++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) case 'i':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (strcmp("-", optarg) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) input_file = stdin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) input_file = fopen(optarg, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!input_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) usage("Failed to open input file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) seed = (unsigned int)strtoul(optarg, &tmp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (*tmp == ',') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) optarg = tmp + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) iter_start = strtoul(optarg, &tmp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (*tmp != '\0' || tmp == optarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) usage("Failed to parse seed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) set_seed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) case 'm':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) iter_end = strtoul(optarg, &tmp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (*tmp != '\0' || tmp == optarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) usage("Failed to parse max_iter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) usage(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /* Check errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (iter_end < iter_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) usage("Max iteration number must be bigger than iter-num");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (set_seed && input_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) usage("Don't use input file (-i) with random seed (-s)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* Initialize random seed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!input_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!set_seed) /* No seed is given */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) init_random_seed();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) srand(seed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct insn insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int insns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) unsigned char insn_buff[MAX_INSN_SIZE * 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) parse_args(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* Prepare stop bytes with NOPs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) memset(insn_buff + MAX_INSN_SIZE, INSN_NOP, MAX_INSN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) for (i = 0; i < iter_end; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (generate_insn(insn_buff) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (i < iter_start) /* Skip to given iteration number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* Decode an instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) insn_init(&insn, insn_buff, sizeof(insn_buff), x86_64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) insn_get_length(&insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (insn.next_byte <= insn.kaddr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) insn.kaddr + MAX_INSN_SIZE < insn.next_byte) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* Access out-of-range memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) dump_stream(stderr, "Error: Found an access violation", i, insn_buff, &insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) } else if (verbose && !insn_complete(&insn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) dump_stream(stdout, "Info: Found an undecodable input", i, insn_buff, &insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) else if (verbose >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) dump_insn(stdout, &insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) insns++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) fprintf((errors) ? stderr : stdout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) "%s: %s: decoded and checked %d %s instructions with %d errors (seed:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) (errors) ? "Failure" : "Success",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) insns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) (input_file) ? "given" : "random",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) errors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) seed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return errors ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }