#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qual.h"
static int g_checked;
static int replay_one(const char *path, const uint8_t *buf, size_t len) {
q_op ops[Q_MAX_OPS];
size_t n = q_parse_ops(buf, len, ops, Q_MAX_OPS);
q_ctx *c;
q_trace t;
const q_record *r;
const q_effect *e;
size_t nr, ne;
char detail[160];
q_verdict v;
if (n == SIZE_MAX) {
/* Unparseable input is an expected refusal, not a crash. */
return 1;
}
if (n == 0) {
return 1;
}
c = q_new(1);
if (c == NULL) {
fprintf(stderr, "replay: %s: allocation failed\n", path);
return 0;
}
q_run_ops(c, ops, n, &t);
r = q_records(c, &nr);
e = q_effects(c, &ne);
detail[0] = '\0';
v = q_oracle_check(ops, n, &t, r, nr, e, ne, detail, sizeof detail);
q_free(c);
if (v != Q_OK) {
fprintf(stderr, "replay: %s: %s (%s)\n", path,
q_verdict_name(v), detail);
return 0;
}
g_checked++;
return 1;
}
static int read_file(const char *path, uint8_t *buf, size_t cap, size_t *len) {
FILE *f = fopen(path, "rb");
size_t got;
if (f == NULL) {
return 0;
}
got = fread(buf, 1, cap, f);
if (ferror(f)) {
fclose(f);
return 0;
}
fclose(f);
*len = got;
return got <= cap;
}
int main(int argc, char **argv) {
uint8_t buf[4096];
int files = 0;
if (argc < 2) {
fprintf(stderr, "usage: %s CORPUS_FILE...\n", argv[0]);
return 2;
}
for (int i = 1; i < argc; i++) {
size_t len = 0;
if (!read_file(argv[i], buf, sizeof buf, &len)) {
fprintf(stderr, "replay: %s: read failure or oversize\n", argv[i]);
return 1;
}
if (!replay_one(argv[i], buf, len)) {
return 1;
}
files++;
}
printf("replay: %d files, %d sequences checked\n", files, g_checked);
return 0;
}