75 lines
18 KiB
JSON

{
"id": "diag-spool-progress",
"seed": 202610174,
"candidate_files": [
"engine.c",
"policy.c"
],
"readonly": {
"wire.h": "#ifndef WIRE_H\n#define WIRE_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#define WIRE_ARGS 10\n#define WIRE_TOKEN 521\n#define WIRE_EVENTS 128\n#define WIRE_STATE 64\ntypedef struct {\n char name;\n size_t count;\n char arg[WIRE_ARGS][WIRE_TOKEN];\n} Command;\ntypedef struct {\n const char *name;\n int64_t a;\n int64_t b;\n int64_t c;\n} Event;\ntypedef struct {\n Event event[WIRE_EVENTS];\n size_t events;\n int64_t state[WIRE_STATE];\n size_t states;\n int overflow;\n} Observation;\ntypedef struct {\n FILE *input;\n unsigned long physical;\n unsigned long commands;\n int failed;\n char line[8192];\n} WireReader;\nvoid wire_reader_init(WireReader *reader, FILE *input);\nint wire_next(WireReader *reader, Command *command);\nint wire_integer(const Command *command, size_t index, int64_t *value);\nint64_t wire_number(const Command *command, size_t index);\nint wire_hex(const char *text, unsigned char *output, size_t capacity, size_t *length);\nvoid observe_begin(Observation *observation);\nvoid observe_event(Observation *observation, const char *name, int64_t a, int64_t b, int64_t c);\nvoid observe_value(Observation *observation, int64_t value);\nint observe_write(FILE *output, unsigned long sequence, const Observation *observation);\nint wire_arguments(const Command *command, size_t minimum, size_t maximum);\n#endif\n",
"wire.c": "#include \"wire.h\"\n#include <ctype.h>\n#include <errno.h>\n#include <inttypes.h>\n#include <limits.h>\n#include <stdlib.h>\n#include <string.h>\n\nvoid wire_reader_init(WireReader *reader, FILE *input)\n{\n memset(reader, 0, sizeof(*reader));\n reader->input = input;\n}\n\nstatic int finish_line(WireReader *reader)\n{\n size_t length = strlen(reader->line);\n if (length && reader->line[length - 1] == '\\n') {\n reader->line[--length] = '\\0';\n } else if (!feof(reader->input)) {\n int value;\n do {\n value = fgetc(reader->input);\n } while (value != '\\n' && value != EOF);\n reader->failed = 1;\n return 0;\n }\n if (length && reader->line[length - 1] == '\\r') {\n reader->line[--length] = '\\0';\n }\n return 1;\n}\n\nstatic int split_line(char *line, Command *command)\n{\n const unsigned char *cursor = (const unsigned char *)line;\n memset(command, 0, sizeof(*command));\n while (*cursor && isspace(*cursor)) {\n cursor++;\n }\n if (*cursor == '\\0' || *cursor == '#') {\n return 0;\n }\n command->name = (char)*cursor++;\n if (*cursor && !isspace(*cursor)) {\n return -1;\n }\n for (;;) {\n size_t length = 0;\n while (*cursor && isspace(*cursor)) {\n cursor++;\n }\n if (*cursor == '\\0' || *cursor == '#') {\n return 1;\n }\n if (command->count == WIRE_ARGS) {\n return -1;\n }\n while (*cursor && !isspace(*cursor)) {\n if (length + 1 == WIRE_TOKEN) {\n return -1;\n }\n command->arg[command->count][length++] = (char)*cursor++;\n }\n command->arg[command->count][length] = '\\0';\n command->count++;\n }\n}\n\nint wire_next(WireReader *reader, Command *command)\n{\n while (fgets(reader->line, sizeof(reader->line), reader->input)) {\n int parsed;\n reader->physical++;\n if (!finish_line(reader)) {\n return -1;\n }\n parsed = split_line(reader->line, command);\n if (parsed < 0) {\n reader->failed = 1;\n return -1;\n }\n if (parsed > 0) {\n reader->commands++;\n return 1;\n }\n }\n if (ferror(reader->input)) {\n reader->failed = 1;\n return -1;\n }\n return 0;\n}\n\nstatic int decimal_value(const char *text, int64_t *result)\n{\n size_t index = 0;\n int negative = 0;\n uint64_t magnitude = 0;\n uint64_t limit;\n if (text[index] == '+' || text[index] == '-') {\n negative = text[index] == '-';\n index++;\n }\n if (text[index] == '\\0') {\n return 0;\n }\n limit = negative ? (uint64_t)INT64_MAX + 1 : (uint64_t)INT64_MAX;\n for (; text[index] != '\\0'; index++) {\n unsigned char character = (unsigned char)text[index];\n unsigned digit;\n if (character < '0' || character > '9') {\n return 0;\n }\n digit = character - '0';\n if (magnitude > (limit - digit) / 10) {\n return 0;\n }\n magnitude = magnitude * 10 + digit;\n }\n if (negative && magnitude == (uint64_t)INT64_MAX + 1) {\n *result = INT64_MIN;\n } else if (negative) {\n *result = -(int64_t)magnitude;\n } else {\n *result = (int64_t)magnitude;\n }\n return 1;\n}\n\nint wire_integer(const Command *command, size_t index, int64_t *value)\n{\n if (index >= command->count) {\n return 0;\n }\n return decimal_value(command->arg[index], value);\n}\n\nint64_t wire_number(const Command *command, size_t index)\n{\n int64_t value = 0;\n (void)wire_integer(command, index, &value);\n return value;\n}\n\nstatic int hex_digit(unsigned char value)\n{\n if (value >= '0' && value <= '9') {\n return value - '0';\n }\n if (value >= 'a' && value <= 'f') {\n return value - 'a' + 10;\n }\n if (value >= 'A' && value <= 'F') {\n return value - 'A' + 10;\n }\n return -1;\n}\n\nint wire_hex(const char *text, unsigned char *output, size_t capacity, size_t *length)\n{\n size_t source = strlen(text);\n size_t index;\n if (strcmp(text, \"-\") == 0) {\n *length = 0;\n return 1;\n }\n if (source % 2 || source / 2 > capacity) {\n return 0;\n }\n for (index = 0; index < source / 2; index++) {\n int high = hex_digit((unsigned char)text[index * 2]);\n int low = hex_digit((unsigned char)text[index * 2 + 1]);\n if (high < 0 || low < 0) {\n return 0;\n }\n output[index] = (unsigned char)(high * 16 + low);\n }\n *length = source / 2;\n return 1;\n}\n\nvoid observe_begin(Observation *observation)\n{\n memset(observation, 0, sizeof(*observation));\n}\n\nvoid observe_event(Observation *observation, const char *name, int64_t a, int64_t b, int64_t c)\n{\n Event *event;\n if (observation->events == WIRE_EVENTS) {\n observation->overflow = 1;\n return;\n }\n event = &observation->event[observation->events++];\n event->name = name;\n event->a = a;\n event->b = b;\n event->c = c;\n}\n\nvoid observe_value(Observation *observation, int64_t value)\n{\n if (observation->states == WIRE_STATE) {\n observation->overflow = 1;\n return;\n }\n observation->state[observation->states++] = value;\n}\n\nstatic int write_unsigned(FILE *output, uint64_t value)\n{\n char digits[32];\n size_t count = 0;\n do {\n unsigned digit = (unsigned)(value % 10);\n digits[count++] = (char)('0' + digit);\n value /= 10;\n } while (value != 0);\n while (count > 0) {\n count--;\n if (fputc(digits[count], output) == EOF) {\n return 0;\n }\n }\n return 1;\n}\n\nstatic int write_signed(FILE *output, int64_t value)\n{\n uint64_t magnitude;\n if (value < 0) {\n if (fputc('-', output) == EOF) {\n return 0;\n }\n magnitude = (uint64_t)(-(value + 1)) + 1;\n } else {\n magnitude = (uint64_t)value;\n }\n return write_unsigned(output, magnitude);\n}\n\nstatic int write_field(FILE *output, int64_t value)\n{\n if (fputc(' ', output) == EOF) {\n return 0;\n }\n return write_signed(output, value);\n}\n\nstatic int write_prefix(FILE *output, char kind, unsigned long sequence)\n{\n if (fputc(kind, output) == EOF) {\n return 0;\n }\n if (fputc(' ', output) == EOF) {\n return 0;\n }\n return write_unsigned(output, sequence);\n}\n\nstatic int write_event(FILE *output, unsigned long sequence, const Event *event)\n{\n if (!write_prefix(output, 'E', sequence)) {\n return 0;\n }\n if (fputc(' ', output) == EOF || fputs(event->name, output) == EOF) {\n return 0;\n }\n if (!write_field(output, event->a)) {\n return 0;\n }\n if (!write_field(output, event->b)) {\n return 0;\n }\n if (!write_field(output, event->c)) {\n return 0;\n }\n return fputc('\\n', output) != EOF;\n}\n\nint observe_write(FILE *output, unsigned long sequence, const Observation *observation)\n{\n size_t index;\n if (observation->overflow) {\n return 0;\n }\n for (index = 0; index < observation->events; index++) {\n if (!write_event(output, sequence, &observation->event[index])) {\n return 0;\n }\n }\n if (!write_prefix(output, 'S', sequence)) {\n return 0;\n }\n for (index = 0; index < observation->states; index++) {\n if (!write_field(output, observation->state[index])) {\n return 0;\n }\n }\n return fputc('\\n', output) != EOF;\n}\n\nint wire_arguments(const Command *command, size_t minimum, size_t maximum)\n{\n size_t index;\n if (command->count < minimum || command->count > maximum) {\n return 0;\n }\n for (index = 0; index < command->count; index++) {\n int64_t ignored;\n if (!wire_integer(command, index, &ignored)) {\n return 0;\n }\n }\n return 1;\n}\n",
"model.h": "#ifndef MODEL_H\n#define MODEL_H\n#include \"wire.h\"\n#include <stdint.h>\ntypedef struct {\n int ids[3];\n int lengths[3];\n int seeds[3];\n int offsets[3];\n int head;\n int count;\n unsigned queued;\n unsigned done;\n unsigned dropped;\n unsigned attempts;\n unsigned pauses;\n unsigned bytes;\n uint32_t checksum;\n unsigned rejected;\n} Model;\nvoid model_init(Model *);\nvoid model_command(Model *, const Command *, Observation *);\nvoid model_snapshot(const Model *, Observation *);\nvoid spool_put(Model *, int, int, int, Observation *);\nvoid spool_step(Model *, int, int, Observation *);\nvoid spool_result(Model *, int, Observation *);\nvoid spool_pop(Model *);\nvoid spool_bytes(Model *, int, int, Observation *);\n#endif\n",
"dispatch.c": "#include \"model.h\"\n#include <string.h>\n\nvoid model_init(Model *m)\n{\n memset(m, 0, sizeof(*m));\n}\n\nstatic void reject(Model *m, const Command *c, Observation *o)\n{\n m->rejected++;\n observe_event(o, \"REJECT\", (unsigned char)c->name, 0, 0);\n}\n\nstatic int command_P(Model *m, const Command *c, Observation *o)\n{\n int64_t id;\n int64_t length;\n int64_t seed;\n if (!wire_arguments(c, 3, 3)) {\n return 0;\n }\n id = wire_number(c, 0);\n if (id < 0 || id > 999) {\n return 0;\n }\n length = wire_number(c, 1);\n if (length < 0 || length > 32) {\n return 0;\n }\n seed = wire_number(c, 2);\n if (seed < 0 || seed > 255) {\n return 0;\n }\n if (!(m->count < 3)) {\n return 0;\n }\n spool_put(m, id, length, seed, o);\n return 1;\n}\n\nstatic int command_W(Model *m, const Command *c, Observation *o)\n{\n int64_t allowance;\n int64_t error;\n if (!wire_arguments(c, 2, 2)) {\n return 0;\n }\n allowance = wire_number(c, 0);\n if (allowance < 1 || allowance > 32) {\n return 0;\n }\n error = wire_number(c, 1);\n if (error < 0 || error > 4) {\n return 0;\n }\n if (!(1)) {\n return 0;\n }\n spool_step(m, allowance, error, o);\n return 1;\n}\n\nvoid model_command(Model *m, const Command *c, Observation *o)\n{\n int accepted = 0;\n switch (c->name) {\n case 'R':\n if (c->count == 0) {\n model_init(m);\n accepted = 1;\n }\n break;\n case 'P':\n accepted = command_P(m, c, o);\n break;\n case 'W':\n accepted = command_W(m, c, o);\n break;\n default:\n break;\n }\n if (!accepted) {\n reject(m, c, o);\n }\n}\n\nvoid model_snapshot(const Model *m, Observation *o)\n{\n observe_value(o, m->head);\n observe_value(o, m->count);\n observe_value(o, m->queued);\n observe_value(o, m->done);\n observe_value(o, m->dropped);\n observe_value(o, m->attempts);\n observe_value(o, m->pauses);\n observe_value(o, m->bytes);\n observe_value(o, m->checksum);\n observe_value(o, m->rejected);\n observe_value(o, m->ids[0]);\n observe_value(o, m->lengths[0]);\n observe_value(o, m->seeds[0]);\n observe_value(o, m->offsets[0]);\n observe_value(o, m->ids[1]);\n observe_value(o, m->lengths[1]);\n observe_value(o, m->seeds[1]);\n observe_value(o, m->offsets[1]);\n observe_value(o, m->ids[2]);\n observe_value(o, m->lengths[2]);\n observe_value(o, m->seeds[2]);\n observe_value(o, m->offsets[2]);\n}\n",
"main.c": "#include \"model.h\"\n#include <stdio.h>\n#include \"cli.h\"\n\nint main(int argc, char **argv)\n{\n ReplayCli cli;\n WireReader reader;\n Model model;\n Command command;\n int status;\n unsigned long sequence = 0;\n if (!replay_cli_open(&cli, argc, argv)) {\n return 2;\n }\n if (cli.help) {\n return 0;\n }\n wire_reader_init(&reader, cli.input);\n model_init(&model);\n while ((status = wire_next(&reader, &command)) > 0) {\n Observation observation;\n observe_begin(&observation);\n model_command(&model, &command, &observation);\n model_snapshot(&model, &observation);\n if (!observe_write(cli.output, ++sequence, &observation)) {\n return 3;\n }\n }\n if (status < 0) {\n fprintf(stderr, \"input framing failed at %lu\\n\", reader.physical);\n return 2;\n }\n return replay_cli_close(&cli);\n}\n",
"Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -pedantic\nSOURCES = main.c cli.c wire.c dispatch.c engine.c policy.c\nall: replay\nreplay: $(SOURCES) wire.h cli.h model.h\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\nclean:\n\trm -f replay\n.PHONY: all clean\n",
"cli.h": "#ifndef REPLAY_CLI_H\n#define REPLAY_CLI_H\n#include <stdio.h>\ntypedef struct {\n FILE *input;\n FILE *output;\n int own_input;\n int own_output;\n int help;\n} ReplayCli;\nint replay_cli_open(ReplayCli *, int, char **);\nint replay_cli_close(ReplayCli *);\n#endif\n",
"cli.c": "#include \"cli.h\"\n#include <errno.h>\n#include <string.h>\n\nstatic void report_open(const char *kind, const char *path)\n{\n fprintf(stderr, \"%s %s: %s\\n\", kind, path, strerror(errno));\n}\n\nint replay_cli_close(ReplayCli *cli)\n{\n int failed = 0;\n if (cli->own_input) {\n if (fclose(cli->input) != 0) {\n failed = 1;\n }\n cli->own_input = 0;\n }\n if (cli->own_output) {\n if (fclose(cli->output) != 0) {\n failed = 1;\n }\n cli->own_output = 0;\n } else if (fflush(cli->output) != 0) {\n failed = 1;\n }\n return failed ? 3 : 0;\n}\n\nint replay_cli_open(ReplayCli *cli, int argc, char **argv)\n{\n int index;\n const char *input_path = NULL;\n const char *output_path = NULL;\n memset(cli, 0, sizeof(*cli));\n cli->input = stdin;\n cli->output = stdout;\n for (index = 1; index < argc; index++) {\n if (strcmp(argv[index], \"--help\") == 0 && argc == 2) {\n cli->help = 1;\n puts(\"usage: replay [--input FILE] [--output FILE]\");\n return 1;\n }\n if (index + 1 >= argc) {\n fprintf(stderr, \"missing path argument\\n\");\n return 0;\n }\n if (strcmp(argv[index], \"--input\") == 0 && input_path == NULL) {\n input_path = argv[++index];\n } else if (strcmp(argv[index], \"--output\") == 0 && output_path == NULL) {\n output_path = argv[++index];\n } else {\n fprintf(stderr, \"unknown or repeated option: %s\\n\", argv[index]);\n return 0;\n }\n }\n if (input_path != NULL && output_path != NULL &&\n strcmp(input_path, output_path) == 0) {\n fprintf(stderr, \"input and output must be different files\\n\");\n return 0;\n }\n if (input_path != NULL) {\n cli->input = fopen(input_path, \"r\");\n if (cli->input == NULL) {\n report_open(\"input\", input_path);\n return 0;\n }\n cli->own_input = 1;\n }\n if (output_path != NULL) {\n cli->output = fopen(output_path, \"w\");\n if (cli->output == NULL) {\n report_open(\"output\", output_path);\n cli->output = stdout;\n replay_cli_close(cli);\n return 0;\n }\n cli->own_output = 1;\n }\n return 1;\n}\n"
},
"initial": {
"ids": [
0,
0,
0
],
"lengths": [
0,
0,
0
],
"seeds": [
0,
0,
0
],
"offsets": [
0,
0,
0
],
"head": 0,
"count": 0,
"queued": 0,
"done": 0,
"dropped": 0,
"attempts": 0,
"pauses": 0,
"bytes": 0,
"checksum": 0,
"rejected": 0
},
"snapshot": [
"head",
"count",
"queued",
"done",
"dropped",
"attempts",
"pauses",
"bytes",
"checksum",
"rejected",
"ids[0]",
"lengths[0]",
"seeds[0]",
"offsets[0]",
"ids[1]",
"lengths[1]",
"seeds[1]",
"offsets[1]",
"ids[2]",
"lengths[2]",
"seeds[2]",
"offsets[2]"
]
}