| 1 |
/* |
| 2 |
* Copyright (C) 2021 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <inttypes.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include "disassemble.h" |
| 9 |
|
| 10 |
static inline uint8_t |
| 11 |
parse_nibble(const char c) |
| 12 |
{ |
| 13 |
return (c >= 'a') ? 10 + (c - 'a') : (c - '0'); |
| 14 |
} |
| 15 |
|
| 16 |
/* Given a little endian 8 byte hexdump, parse out the 64-bit value */ |
| 17 |
static uint64_t |
| 18 |
parse_hex(const char *in) |
| 19 |
{ |
| 20 |
uint64_t v = 0; |
| 21 |
|
| 22 |
for (unsigned i = 0; i < 8; ++i) { |
| 23 |
uint8_t byte = (parse_nibble(in[0]) << 4) | parse_nibble(in[1]); |
| 24 |
v |= ((uint64_t)byte) << (8 * i); |
| 25 |
|
| 26 |
/* Skip the space after the byte */ |
| 27 |
in += 3; |
| 28 |
} |
| 29 |
|
| 30 |
return v; |
| 31 |
} |
| 32 |
|
| 33 |
int |
| 34 |
main(int argc, const char **argv) |
| 35 |
{ |
| 36 |
if (argc < 2) { |
| 37 |
fprintf(stderr, "Expected case list\n"); |
| 38 |
return 1; |
| 39 |
} |
| 40 |
|
| 41 |
FILE *fp = fopen(argv[1], "r"); |
| 42 |
|
| 43 |
if (fp == NULL) { |
| 44 |
fprintf(stderr, "Could not open the case list"); |
| 45 |
return 1; |
| 46 |
} |
| 47 |
|
| 48 |
char line[128]; |
| 49 |
unsigned nr_fail = 0, nr_pass = 0; |
| 50 |
|
| 51 |
while (fgets(line, sizeof(line), fp) != NULL) { |
| 52 |
char *output = NULL; |
| 53 |
size_t sz = 0; |
| 54 |
size_t len = strlen(line); |
| 55 |
|
| 56 |
/* Skip empty lines */ |
| 57 |
if (len <= 1) |
| 58 |
continue; |
| 59 |
|
| 60 |
/* Check for buffer overflow */ |
| 61 |
if (len < 28) { |
| 62 |
fprintf(stderr, "Invalid reference %s\n", line); |
| 63 |
nr_fail++; |
| 64 |
} |
| 65 |
|
| 66 |
uint64_t bin = parse_hex(line); |
| 67 |
FILE *outputp = open_memstream(&output, &sz); |
| 68 |
va_disasm_instr(outputp, bin); |
| 69 |
fprintf(outputp, "\n"); |
| 70 |
fclose(outputp); |
| 71 |
|
| 72 |
/* Skip hexdump: 8 bytes * (2 nibbles + 1 space) + 3 spaces */ |
| 73 |
const char *reference = line + 27; |
| 74 |
bool fail = strcmp(reference, output); |
| 75 |
|
| 76 |
if (fail) { |
| 77 |
/* Extra spaces after Got to align with Expected */ |
| 78 |
printf("Got %sExpected %s\n", output, reference); |
| 79 |
nr_fail++; |
| 80 |
} else { |
| 81 |
nr_pass++; |
| 82 |
} |
| 83 |
|
| 84 |
free(output); |
| 85 |
} |
| 86 |
|
| 87 |
printf("Passed %u/%u tests.\n", nr_pass, nr_pass + nr_fail); |
| 88 |
fclose(fp); |
| 89 |
|
| 90 |
return nr_fail ? 1 : 0; |
| 91 |
} |