17 lines
13 KiB
JSON

{
"mode": "diag-control-commit",
"seed": 75193,
"candidates": [
"control.c"
],
"context": {
"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",
"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 256\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",
"harness.c": "#include \"engine.h\"\n#include <stdio.h>\n#include <string.h>\n\ntypedef struct {\n WireReader reader;\n Engine engine;\n Observation observation;\n unsigned long completed;\n int input_status;\n int output_status;\n} Application;\n\nstatic void application_init(Application *application, FILE *input)\n{\n memset(application, 0, sizeof(*application));\n wire_reader_init(&application->reader, input);\n engine_init(&application->engine);\n application->output_status = 1;\n}\n\nstatic int application_step(Application *application)\n{\n Command command;\n int status = wire_next(&application->reader, &command);\n application->input_status = status;\n if (status <= 0) {\n return 0;\n }\n observe_begin(&application->observation);\n engine_command(&application->engine, &command, &application->observation);\n engine_snapshot(&application->engine, &application->observation);\n application->completed++;\n application->output_status = observe_write(stdout, application->completed,\n &application->observation);\n return application->output_status;\n}\n\nstatic int application_finish(const Application *application)\n{\n if (application->input_status < 0) {\n fprintf(stderr, \"input framing failed at line %lu\\n\", application->reader.physical);\n return 2;\n }\n if (!application->output_status) {\n fputs(\"observation output failed\\n\", stderr);\n return 3;\n }\n if (fflush(stdout) != 0) {\n return 3;\n }\n return 0;\n}\n\nint main(void)\n{\n Application application;\n application_init(&application, stdin);\n while (application_step(&application)) {\n /* Dispatch only; policy and state belong to the engine. */\n }\n return application_finish(&application);\n}\n",
"engine.h": "#ifndef ENGINE_H\n#define ENGINE_H\n#include \"domain.h\"\n#include \"wire.h\"\ntypedef struct {\n Domain domain;\n unsigned rejected;\n} Engine;\nvoid engine_init(Engine *engine);\nvoid engine_command(Engine *engine, const Command *command, Observation *out);\nvoid engine_snapshot(const Engine *engine, Observation *out);\n#endif\n",
"engine.c": "#include \"engine.h\"\n#include <string.h>\n\nvoid engine_init(Engine *engine)\n{\n memset(engine, 0, sizeof(*engine));\n domain_init(&engine->domain);\n}\n\nvoid engine_command(Engine *engine, const Command *command, Observation *out)\n{\n if (command->name == 'R' && wire_arguments(command, 0, 0)) {\n engine_init(engine);\n return;\n }\n if (!domain_command(&engine->domain, command, out)) {\n engine->rejected++;\n observe_event(out, \"REJECT\", (unsigned char)command->name, 0, 0);\n }\n}\n\nvoid engine_snapshot(const Engine *engine, Observation *out)\n{\n observe_value(out, engine->rejected);\n domain_snapshot(&engine->domain, out);\n}\n",
"domain.h": "#ifndef DOMAIN_H\n#define DOMAIN_H\n#include \"wire.h\"\n#include <stdint.h>\n#define CONTROL_LANES 4\ntypedef struct {\n uint32_t committed;\n uint32_t pending_value;\n int pending;\n unsigned status;\n unsigned writes;\n unsigned drains;\n unsigned reads;\n unsigned jobs;\n} ControlLane;\ntypedef struct {\n ControlLane lane[CONTROL_LANES];\n unsigned accepted;\n unsigned failed;\n} Domain;\nvoid domain_init(Domain *domain);\nint domain_command(Domain *domain, const Command *command, Observation *out);\nvoid domain_snapshot(const Domain *domain, Observation *out);\n#endif\n",
"Makefile": "CC = cc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -pedantic\nSOURCES = wire.c engine.c harness.c control.c\nHEADERS = wire.h engine.h domain.h\n\nall: device-demo\n\ndevice-demo: $(SOURCES) $(HEADERS)\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\n\nclean:\n\trm -f device-demo\n\n.PHONY: all clean\n"
}
}