aboutsummaryrefslogtreecommitdiff
path: root/test/test-transform-dis.c
blob: d147a493488e8d5e291731a82a628cb1c7fdbecc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#define TRANSFORM_DIS_VERBOSE 1
#include "transform-dis.c"
#include <stdlib.h>
#include <string.h>
#include <assert.h>

_Noreturn
static void usage() {
    printf("usage: test-transform-dis (manual patch_size | auto) <thumb if arm>\n");
    exit(1);
}

static void do_manual(uint8_t *in, size_t in_size, int patch_size,
                      struct arch_dis_ctx arch) {
    (void) in_size;
    /* on ARM:
     *    patch_size bytes of patch
     *    max 2 bytes of tail
     *    max 12 more bytes of ITted insns
     * on x86:
     *    max 14 bytes of tail
     * everywhere:
     *    1 off-by-one written to simplify the code
     */
    int offsets[patch_size + 15];
    uint8_t out[patch_size * 10];
    void *rewritten_ptr = out;
    printf("\n#if 0\n");
    uint_tptr pc_patch_start = 0x10000;
    uint_tptr pc_patch_end = pc_patch_start + patch_size;
    uint_tptr pc_trampoline = 0xf000;
    int ret = transform_dis_main(
        in,
        &rewritten_ptr,
        pc_patch_start,
        &pc_patch_end,
        pc_trampoline,
        &arch,
        offsets,
        TRANSFORM_DIS_BAN_CALLS);
    printf("=> %d\n", ret);
    printf("#endif\n");
    int print_out_idx = 0;
    int print_in_idx = 0;
    if (!ret) {
        printf("// total length: %zd\n", (uint8_t *) rewritten_ptr - out);
        for(int ii = 0; ii <= (int) (pc_patch_end - pc_patch_start); ii++) {
            int oi = offsets[ii];
            if(oi != -1) {
                int in_size = ii - print_in_idx;
                int out_size = oi - print_out_idx;
                if (in_size != out_size || memcmp(out + print_out_idx, in + print_in_idx, in_size)) {
                    printf("at_%x: nop; nop; nop\n", print_in_idx);
                    printf("   .byte ");
                    while(print_in_idx++ < ii)
                        printf("0x%02x%s", in[print_in_idx-1], print_in_idx == ii ? "" : ", ");
                    printf("\nnop //     -->\n   .byte ");
                    while(print_out_idx++ < oi)
                        printf("0x%02x%s", out[print_out_idx-1], print_out_idx == oi ? "" : ", ");
                    printf("\n");
                }
                print_in_idx = ii;
                print_out_idx = oi;
                printf("/* 0x%x: 0x%x */\n", ii, oi);
            }
        }
    }
}

static void hex_dump(const uint8_t *buf, size_t size) {
    printf("  .byte ");
    for (size_t i = 0; i < size; i++) {
        if (i)
            printf(", ");
        printf("0x%02x", buf[i]);
    }
    printf("\n");
}

static void print_given(const uint8_t *given, size_t given_size) {
    printf("given:\n");
    hex_dump(given, given_size);
}

static void do_auto(uint8_t *in, size_t in_size, struct arch_dis_ctx arch) {
    uint8_t *end = in + in_size;
    assert(!memcmp(in, "GIVEN", 5)); in += 5;
    while (in < end) {
        uint8_t *given = in;
        uint8_t *expect = memmem(in, end - in, "EXPECT", 6);
        assert(expect);
        size_t given_size = expect - given;
        expect += 6;
        in = expect;
        bool expect_err = false;
        size_t expect_size;
        if (!memcmp(expect, "_ERR", 4)) {
            expect_err = true;
            in += 4;
            if (in != end) {
                assert(!memcmp(in, "GIVEN", 5));
                in += 5;
            }
        } else {
            in = memmem(in, end - in, "GIVEN", 5);
            if (in) {
                expect_size = in - expect;
                in += 5;
            } else {
                in = end;
                expect_size = in - expect;
            }
        }
        size_t patch_size = given_size;
        int offsets[patch_size + 15];
        uint8_t out[patch_size * 10];
        void *rewritten_ptr = out;
        uint_tptr pc_patch_start = 0xdead0000;
        uint_tptr pc_patch_end = pc_patch_start + patch_size;
        uint_tptr pc_trampoline = 0xdeac0000;
        int ret = transform_dis_main(
            given,
            &rewritten_ptr,
            pc_patch_start,
            &pc_patch_end,
            pc_trampoline,
            &arch,
            offsets,
            0);//TRANSFORM_DIS_BAN_CALLS);
        if (ret) {
            if (expect_err) {
                printf("OK\n");
            } else {
                print_given(given, given_size);
                printf("got ret %d, expected success\n\n", ret);
            }
        } else {
            if (expect_err) {
                print_given(given, given_size);
                printf("got OK, expected error\n\n");
            } else if (rewritten_ptr != out + expect_size ||
                       memcmp(out, expect, expect_size)) {
                print_given(given, given_size);
                printf("got:\n");
                hex_dump(out, (uint8_t *) rewritten_ptr - out);
                printf("but expected:\n");
                hex_dump(expect, expect_size);
                printf("\n");
            } else {
                printf("OK\n");
            }
        }

    }

}

int main(UNUSED int argc, char **argv) {
    argv++;
    if (!*argv)
        usage();
    enum { MANUAL, AUTO } mode;
    if (!strcmp(*argv, "manual"))
        mode = MANUAL;
    else if (!strcmp(*argv, "auto"))
        mode = AUTO;
    else
        usage();
    argv++;

    int patch_size;
    if (mode == MANUAL) {
        if (!*argv)
            usage();
        patch_size = atoi(*argv++);
    }

    struct arch_dis_ctx arch;
    arch_dis_ctx_init(&arch);
#ifdef TARGET_arm
    if (!*argv)
        usage();
    int thumb = atoi(*argv++);
    arch.pc_low_bit = thumb;
#endif


    static uint8_t in[1048576];
    size_t in_size = fread(in, 1, sizeof(in), stdin);

    if (mode == MANUAL)
        do_manual(in, in_size, patch_size, arch);
    else
        do_auto(in, in_size, arch);
}