aboutsummaryrefslogtreecommitdiff
path: root/risu.c
blob: a5d155dd830e5d80d05c751f2d1af66c64cd3d15 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/******************************************************************************
 * Copyright (c) 2010 Linaro Limited
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Peter Maydell (Linaro) - initial implementation
 *****************************************************************************/


/* Random Instruction Sequences for Userspace */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <ucontext.h>
#include <getopt.h>
#include <setjmp.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>

#include "config.h"

#include "risu.h"

void *memblock;

int apprentice_fd, master_fd;
int trace;
size_t signal_count;

#ifdef HAVE_ZLIB
#include <zlib.h>
gzFile gz_trace_file;
#endif

sigjmp_buf jmpbuf;

/* Should we test for FP exception status bits? */
int test_fp_exc;

/* Master functions */

int read_sock(void *ptr, size_t bytes)
{
    return recv_data_pkt(master_fd, ptr, bytes);
}

int write_trace(void *ptr, size_t bytes)
{
    size_t res;

#ifdef HAVE_ZLIB
    if (master_fd == STDOUT_FILENO) {
#endif
        res = write(master_fd, ptr, bytes);
#ifdef HAVE_ZLIB
    } else {
        res = gzwrite(gz_trace_file, ptr, bytes);
    }
#endif
    return (res == bytes) ? 0 : 1;
}

void respond_sock(int r)
{
    send_response_byte(master_fd, r);
}

/* Apprentice function */

int write_sock(void *ptr, size_t bytes)
{
    return send_data_pkt(apprentice_fd, ptr, bytes);
}

int read_trace(void *ptr, size_t bytes)
{
    size_t res;

#ifdef HAVE_ZLIB
    if (apprentice_fd == STDIN_FILENO) {
#endif
        res = read(apprentice_fd, ptr, bytes);
#ifdef HAVE_ZLIB
    } else {
        res = gzread(gz_trace_file, ptr, bytes);
    }
#endif

    return (res == bytes) ? 0 : 1;
}

void respond_trace(int r)
{
    switch (r) {
    case 0: /* test ok */
    case 1: /* end of test */
        break;
    default:
        /* mismatch - if tracing we need to report, otherwise barf */
        if (!trace) {
            abort();
        }
        break;
    }
}

void master_sigill(int sig, siginfo_t *si, void *uc)
{
    int r;
    signal_count++;

    if (trace) {
        r = send_register_info(write_trace, uc);
    } else {
        r = recv_and_compare_register_info(read_sock, respond_sock, uc);
    }

    switch (r) {
    case 0:
        /* match OK */
        advance_pc(uc);
        return;
    default:
        /* mismatch, or end of test */
        siglongjmp(jmpbuf, 1);
    }
}

void apprentice_sigill(int sig, siginfo_t *si, void *uc)
{
    int r;
    signal_count++;

    if (trace) {
        r = recv_and_compare_register_info(read_trace, respond_trace, uc);
    } else {
        r = send_register_info(write_sock, uc);
    }

    switch (r) {
    case 0:
        /* match OK */
        advance_pc(uc);
        return;
    case 1:
        /* end of test */
        exit(0);
    default:
        /* mismatch */
        if (trace) {
            siglongjmp(jmpbuf, 1);
        }
        exit(1);
    }
}

static void set_sigill_handler(void (*fn) (int, siginfo_t *, void *))
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(struct sigaction));

    sa.sa_sigaction = fn;
    sa.sa_flags = SA_SIGINFO;
    sigemptyset(&sa.sa_mask);
    if (sigaction(SIGILL, &sa, 0) != 0) {
        perror("sigaction");
        exit(1);
    }
}

typedef void entrypoint_fn(void);

uintptr_t image_start_address;
entrypoint_fn *image_start;

void load_image(const char *imgfile)
{
    /* Load image file into memory as executable */
    struct stat st;
    fprintf(stderr, "loading test image %s...\n", imgfile);
    int fd = open(imgfile, O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "failed to open image file %s\n", imgfile);
        exit(1);
    }
    if (fstat(fd, &st) != 0) {
        perror("fstat");
        exit(1);
    }
    size_t len = st.st_size;
    void *addr;

    /* Map writable because we include the memory area for store
     * testing in the image.
     */
    addr =
        mmap(0, len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd,
             0);
    if (!addr) {
        perror("mmap");
        exit(1);
    }
    close(fd);
    image_start = addr;
    image_start_address = (uintptr_t) addr;
}

int master(void)
{
    if (sigsetjmp(jmpbuf, 1)) {
#ifdef HAVE_ZLIB
        if (trace && master_fd != STDOUT_FILENO) {
            gzclose(gz_trace_file);
        }
#endif
        close(master_fd);
        if (trace) {
            fprintf(stderr, "trace complete after %zd checkpoints\n",
                    signal_count);
            return 0;
        } else {
            return report_match_status(0);
        }
    }
    set_sigill_handler(&master_sigill);
    fprintf(stderr, "starting master image at 0x%"PRIxPTR"\n",
            image_start_address);
    fprintf(stderr, "starting image\n");
    image_start();
    fprintf(stderr, "image returned unexpectedly\n");
    exit(1);
}

int apprentice(void)
{
    if (sigsetjmp(jmpbuf, 1)) {
#ifdef HAVE_ZLIB
        if (trace && apprentice_fd != STDIN_FILENO) {
            gzclose(gz_trace_file);
        }
#endif
        close(apprentice_fd);
        fprintf(stderr, "finished early after %zd checkpoints\n", signal_count);
        return report_match_status(1);
    }
    set_sigill_handler(&apprentice_sigill);
    fprintf(stderr, "starting apprentice image at 0x%"PRIxPTR"\n",
            image_start_address);
    fprintf(stderr, "starting image\n");
    image_start();
    fprintf(stderr, "image returned unexpectedly\n");
    exit(1);
}

int ismaster;

void usage(void)
{
    fprintf(stderr,
            "Usage: risu [--master] [--host <ip>] [--port <port>] <image file>"
            "\n\n");
    fprintf(stderr,
            "Run through the pattern file verifying each instruction\n");
    fprintf(stderr, "between master and apprentice risu processes.\n\n");
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "  --master          Be the master (server)\n");
    fprintf(stderr, "  -t, --trace=FILE  Record/playback trace file\n");
    fprintf(stderr,
            "  -h, --host=HOST   Specify master host machine (apprentice only)"
            "\n");
    fprintf(stderr,
            "  -p, --port=PORT   Specify the port to connect to/listen on "
            "(default 9191)\n");
}

struct option * setup_options(char **short_opts)
{
    static struct option default_longopts[] = {
        {"help", no_argument, 0, '?'},
        {"master", no_argument, &ismaster, 1},
        {"host", required_argument, 0, 'h'},
        {"port", required_argument, 0, 'p'},
        {"test-fp-exc", no_argument, &test_fp_exc, 1},
        {0, 0, 0, 0}
    };

    *short_opts = "h:p:t:";
    return default_longopts;
}

int main(int argc, char **argv)
{
    /* some handy defaults to make testing easier */
    uint16_t port = 9191;
    char *hostname = "localhost";
    char *imgfile;
    char *trace_fn = NULL;
    struct option *longopts;
    char *shortopts;

    longopts = setup_options(&shortopts);

    for (;;) {
        int optidx = 0;
        int c = getopt_long(argc, argv, shortopts, longopts, &optidx);
        if (c == -1) {
            break;
        }

        switch (c) {
        case 0:
        {
            /* flag set by getopt_long, do nothing */
            break;
        }
        case 't':
        {
            trace_fn = optarg;
            trace = 1;
            break;
        }
        case 'h':
        {
            hostname = optarg;
            break;
        }
        case 'p':
        {
            /* FIXME err handling */
            port = strtol(optarg, 0, 10);
            break;
        }
        case '?':
        {
            usage();
            exit(1);
        }
        default:
            abort();
        }
    }

    imgfile = argv[optind];
    if (!imgfile) {
        fprintf(stderr, "Error: must specify image file name\n\n");
        usage();
        exit(1);
    }

    load_image(imgfile);

    if (ismaster) {
        if (trace) {
            if (strcmp(trace_fn, "-") == 0) {
                master_fd = STDOUT_FILENO;
            } else {
                master_fd = open(trace_fn, O_WRONLY | O_CREAT, S_IRWXU);
#ifdef HAVE_ZLIB
                gz_trace_file = gzdopen(master_fd, "wb9");
#endif
            }
        } else {
            fprintf(stderr, "master port %d\n", port);
            master_fd = master_connect(port);
        }
        return master();
    } else {
        if (trace) {
            if (strcmp(trace_fn, "-") == 0) {
                apprentice_fd = STDIN_FILENO;
            } else {
                apprentice_fd = open(trace_fn, O_RDONLY);
#ifdef HAVE_ZLIB
                gz_trace_file = gzdopen(apprentice_fd, "rb");
#endif
            }
        } else {
            fprintf(stderr, "apprentice host %s port %d\n", hostname, port);
            apprentice_fd = apprentice_connect(hostname, port);
        }
        return apprentice();
    }
}