• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <inttypes.h>
26 #include "disassemble.h"
27 
28 static inline uint8_t
parse_nibble(const char c)29 parse_nibble(const char c)
30 {
31    return (c >= 'a') ? 10 + (c - 'a') : (c - '0');
32 }
33 
34 /* Given a little endian 8 byte hexdump, parse out the 64-bit value */
35 static uint64_t
parse_hex(const char * in)36 parse_hex(const char *in)
37 {
38    uint64_t v = 0;
39 
40    for (unsigned i = 0; i < 8; ++i) {
41       uint8_t byte = (parse_nibble(in[0]) << 4) | parse_nibble(in[1]);
42       v |= ((uint64_t) byte) << (8 * i);
43 
44       /* Skip the space after the byte */
45       in += 3;
46    }
47 
48    return v;
49 }
50 
51 int
main(int argc,const char ** argv)52 main(int argc, const char **argv)
53 {
54    if (argc < 2) {
55       fprintf(stderr, "Expected case list\n");
56       return 1;
57    }
58 
59    FILE *fp = fopen(argv[1], "r");
60 
61    if (fp == NULL) {
62       fprintf(stderr, "Could not open the case list");
63       return 1;
64    }
65 
66    char line[128];
67    unsigned nr_fail = 0, nr_pass = 0;
68 
69    while (fgets(line, sizeof(line), fp) != NULL) {
70       char *output = NULL;
71       size_t sz = 0;
72       size_t len = strlen(line);
73 
74       /* Skip empty lines */
75       if (len <= 1)
76          continue;
77 
78       /* Check for buffer overflow */
79       if (len < 28) {
80          fprintf(stderr, "Invalid reference %s\n", line);
81          nr_fail++;
82       }
83 
84       uint64_t bin = parse_hex(line);
85       FILE *outputp = open_memstream(&output, &sz);
86       va_disasm_instr(outputp, bin);
87       fprintf(outputp, "\n");
88       fclose(outputp);
89 
90       /* Skip hexdump: 8 bytes * (2 nibbles + 1 space) + 3 spaces */
91       const char *reference = line + 27;
92       bool fail = strcmp(reference, output);
93 
94       if (fail) {
95          printf("Got %sExpected %s\n", output, reference);
96          nr_fail++;
97       } else {
98          nr_pass++;
99       }
100 
101       free(output);
102    }
103 
104    printf("Passed %u/%u tests.\n", nr_pass, nr_pass + nr_fail);
105    fclose(fp);
106 
107    return nr_fail ? 1 : 0;
108 }
109