16 lines
13 KiB
JSON

{
"kind": "sysfs",
"seed": 360164,
"candidates": [
"client.c"
],
"context": {
"lab.h": "#ifndef LAB_H\n#define LAB_H\n#include <stdint.h>\n#include <stddef.h>\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <limits.h>\n#include <errno.h>\n#define ARG_LIMIT 80\n#define BYTE_LIMIT 64\n#define BANKS 4\n#define TRACE_LIMIT 64\nstruct command {\n char operation;\n int argument[ARG_LIMIT];\n size_t count;\n};\nstruct reply {\n int result;\n unsigned char bytes[BYTE_LIMIT];\n size_t length;\n};\nstruct fault {\n int error;\n unsigned remaining;\n};\nstruct trace_entry {\n const char *operation;\n int a;\n int b;\n int c;\n};\nstruct recorder {\n struct trace_entry entries[TRACE_LIMIT];\n size_t used;\n unsigned long total;\n int overflow;\n};\nstruct backend {\n int selected;\n int value[BANKS][8];\n unsigned char storage[BANKS][256];\n struct fault faults[8];\n struct recorder trace;\n};\nstruct client {\n struct backend *backend;\n int selected;\n int value[BANKS][8];\n};\nint command_read(FILE *stream, struct command *command);\nint command_has(const struct command *command, size_t count);\nint command_byte(const struct command *command, size_t index);\nvoid reply_reset(struct reply *reply);\nvoid reply_copy(struct reply *reply, const unsigned char *data, size_t length);\nvoid reply_print(const struct reply *reply);\nvoid trace_begin(struct recorder *recorder);\nvoid trace_add(struct recorder *recorder, const char *operation, int a, int b, int c);\nvoid trace_print(const struct recorder *recorder);\nvoid fault_set(struct backend *backend, int index, int error, int count);\nint fault_take(struct backend *backend, int index);\nunsigned byte_sum(const unsigned char *bytes, size_t length);\nint bytes_valid(const struct command *command, size_t start, size_t count);\nvoid bytes_from_command(unsigned char *target, const struct command *command,\n size_t start, size_t count);\nvoid backend_init(struct backend *backend);\nvoid backend_control(struct backend *backend, const struct command *command,\n struct reply *reply);\nvoid backend_snapshot(const struct backend *backend);\nint device_call(struct backend *backend, int operation, int a, int b,\n unsigned char *bytes, size_t length);\nvoid client_init(struct client *client, struct backend *backend);\nvoid client_apply(struct client *client, const struct command *command,\n struct reply *reply);\nvoid client_snapshot(const struct client *client);\nint text_integer(const unsigned char *bytes, size_t length, int *value);\nsize_t integer_text(int value, unsigned char *bytes);\n#endif\n",
"wire.c": "#include \"lab.h\"\n#include <ctype.h>\n\nstatic int parse_integer(const char *text, int *value)\n{\n char *end;\n long parsed;\n errno = 0;\n parsed = strtol(text, &end, 10);\n if (errno || *end || parsed < INT_MIN || parsed > INT_MAX) {\n return 0;\n }\n *value = (int)parsed;\n return 1;\n}\n\nint command_read(FILE *stream, struct command *command)\n{\n char line[2048];\n char *cursor;\n char *start;\n size_t length;\n while (fgets(line, sizeof(line), stream)) {\n memset(command, 0, sizeof(*command));\n cursor = line;\n while (isspace((unsigned char)*cursor)) {\n cursor++;\n }\n if (!*cursor || *cursor == '#') {\n continue;\n }\n command->operation = *cursor++;\n if (*cursor && !isspace((unsigned char)*cursor)) {\n return -1;\n }\n while (*cursor) {\n while (isspace((unsigned char)*cursor)) {\n cursor++;\n }\n if (!*cursor) {\n break;\n }\n start = cursor;\n while (*cursor && !isspace((unsigned char)*cursor)) {\n cursor++;\n }\n length = (size_t)(cursor - start);\n if (length > 20 || command->count == ARG_LIMIT) {\n return -1;\n }\n if (*cursor) {\n *cursor++ = 0;\n }\n if (!parse_integer(start, &command->argument[command->count])) {\n return -1;\n }\n command->count++;\n }\n return 1;\n }\n return ferror(stream) ? -1 : 0;\n}\n\nint command_has(const struct command *command, size_t count)\n{\n return command->count == count;\n}\n\nint command_byte(const struct command *command, size_t index)\n{\n return index < command->count && command->argument[index] >= 0 &&\n command->argument[index] <= 255;\n}\n\nint bytes_valid(const struct command *command, size_t start, size_t count)\n{\n size_t index;\n if (count > BYTE_LIMIT || start + count != command->count) {\n return 0;\n }\n for (index = start; index < start + count; index++) {\n if (!command_byte(command, index)) {\n return 0;\n }\n }\n return 1;\n}\n\nvoid bytes_from_command(unsigned char *target, const struct command *command,\n size_t start, size_t count)\n{\n size_t index;\n for (index = 0; index < count; index++) {\n target[index] = (unsigned char)command->argument[start + index];\n }\n}\n\nvoid reply_reset(struct reply *reply)\n{\n memset(reply, 0, sizeof(*reply));\n}\n\nvoid reply_copy(struct reply *reply, const unsigned char *data, size_t length)\n{\n if (length > BYTE_LIMIT) {\n reply->result = -EOVERFLOW;\n return;\n }\n memcpy(reply->bytes, data, length);\n reply->length = length;\n}\n\nvoid reply_print(const struct reply *reply)\n{\n size_t index;\n printf(\"R %d %zu\", reply->result, reply->length);\n for (index = 0; index < reply->length; index++) {\n printf(\" %u\", (unsigned)reply->bytes[index]);\n }\n putchar('\\n');\n}\n\nvoid trace_begin(struct recorder *recorder)\n{\n recorder->used = 0;\n recorder->overflow = 0;\n}\n\nvoid trace_add(struct recorder *recorder, const char *operation, int a, int b, int c)\n{\n struct trace_entry *entry;\n recorder->total++;\n if (recorder->used == TRACE_LIMIT) {\n recorder->overflow = 1;\n return;\n }\n entry = &recorder->entries[recorder->used++];\n entry->operation = operation;\n entry->a = a;\n entry->b = b;\n entry->c = c;\n}\n\nvoid trace_print(const struct recorder *recorder)\n{\n size_t index;\n for (index = 0; index < recorder->used; index++) {\n const struct trace_entry *entry = &recorder->entries[index];\n printf(\"T %s %d %d %d\\n\", entry->operation, entry->a, entry->b, entry->c);\n }\n if (recorder->overflow) {\n puts(\"T OVERFLOW 0 0 0\");\n }\n}\n\nvoid fault_set(struct backend *backend, int index, int error, int count)\n{\n if (index >= 0 && index < 8 && error >= 0 && count >= 0) {\n backend->faults[index].error = error;\n backend->faults[index].remaining = (unsigned)count;\n }\n}\n\nint fault_take(struct backend *backend, int index)\n{\n struct fault *fault = &backend->faults[index];\n if (!fault->remaining) {\n return 0;\n }\n fault->remaining--;\n return -fault->error;\n}\n\nunsigned byte_sum(const unsigned char *bytes, size_t length)\n{\n unsigned sum = 0;\n size_t index;\n for (index = 0; index < length; index++) {\n sum = (sum + bytes[index]) & 65535u;\n }\n return sum;\n}\n",
"console.c": "#include \"lab.h\"\n\nstatic int control(struct backend *backend, const struct command *command,\n struct reply *reply)\n{\n if (command->operation == 'A' && command_has(command, 1) &&\n command->argument[0] >= 0 && command->argument[0] < BANKS) {\n backend->selected = command->argument[0];\n return 1;\n }\n if (command->operation == 'F' && command_has(command, 3) &&\n command->argument[0] >= 0 && command->argument[0] < 8 &&\n command->argument[1] >= 0 && command->argument[2] >= 0) {\n fault_set(backend, command->argument[0], command->argument[1],\n command->argument[2]);\n return 1;\n }\n if (command->operation == 'Z') {\n backend_control(backend, command, reply);\n return 1;\n }\n return 0;\n}\n\nvoid client_snapshot(const struct client *client)\n{\n size_t bank;\n size_t index;\n printf(\"C %d\", client->selected);\n for (bank = 0; bank < BANKS; bank++) {\n for (index = 0; index < 8; index++) {\n printf(\" %d\", client->value[bank][index]);\n }\n }\n putchar('\\n');\n}\n\nvoid backend_snapshot(const struct backend *backend)\n{\n size_t bank;\n size_t index;\n printf(\"B %d %lu\", backend->selected, backend->trace.total);\n for (bank = 0; bank < BANKS; bank++) {\n for (index = 0; index < 8; index++) {\n printf(\" %d\", backend->value[bank][index]);\n }\n printf(\" %u\", byte_sum(backend->storage[bank], 256));\n }\n for (index = 0; index < 8; index++) {\n printf(\" %d %u\", backend->faults[index].error,\n backend->faults[index].remaining);\n }\n putchar('\\n');\n}\n\nint main(void)\n{\n struct command command;\n struct reply reply;\n struct backend backend;\n struct client client;\n unsigned long sequence = 0;\n int read_status;\n backend_init(&backend);\n client_init(&client, &backend);\n while ((read_status = command_read(stdin, &command)) > 0) {\n sequence++;\n reply_reset(&reply);\n trace_begin(&backend.trace);\n if (!control(&backend, &command, &reply)) {\n client_apply(&client, &command, &reply);\n }\n if (command.operation == 'A' && reply.result == 0) {\n client.selected = backend.selected;\n }\n printf(\"N %lu\\n\", sequence);\n trace_print(&backend.trace);\n reply_print(&reply);\n client_snapshot(&client);\n backend_snapshot(&backend);\n }\n if (read_status < 0) {\n fputs(\"invalid console input\\n\", stderr);\n return 2;\n }\n return 0;\n}\n",
"codec.c": "#include \"lab.h\"\n\nint text_integer(const unsigned char *bytes, size_t length, int *value)\n{\n size_t start = 0;\n size_t index;\n int negative = 0;\n unsigned magnitude = 0;\n unsigned limit;\n if (!length || length > 12) {\n return -EINVAL;\n }\n if (bytes[length - 1] == '\\n') {\n length--;\n }\n if (length && (bytes[0] == '-' || bytes[0] == '+')) {\n negative = bytes[0] == '-';\n start = 1;\n }\n if (start == length) {\n return -EINVAL;\n }\n for (index = start; index < length; index++) {\n if (bytes[index] < '0' || bytes[index] > '9') {\n return -EINVAL;\n }\n }\n limit = negative ? 2147483648u : 2147483647u;\n for (index = start; index < length; index++) {\n unsigned digit = (unsigned)(bytes[index] - '0');\n if (magnitude > (limit - digit) / 10u) {\n return -ERANGE;\n }\n magnitude = magnitude * 10u + digit;\n }\n *value = negative ? (magnitude == 2147483648u ? INT_MIN : -(int)magnitude) :\n (int)magnitude;\n return 0;\n}\n\nsize_t integer_text(int value, unsigned char *bytes)\n{\n char text[24];\n int length = snprintf(text, sizeof(text), \"%d\\n\", value);\n memcpy(bytes, text, (size_t)length);\n return (size_t)length;\n}\n",
"backend.c": "#include \"lab.h\"\n\nvoid backend_init(struct backend *backend)\n{\n size_t bank;\n size_t index;\n memset(backend, 0, sizeof(*backend));\n for (bank = 0; bank < BANKS; bank++) {\n for (index = 0; index < 256; index++) {\n backend->storage[bank][index] = (unsigned char)((index * 17 + bank * 29) & 255);\n }\n }\n for (size_t bank = 0; bank < BANKS; bank++) { backend->value[bank][0] = -1; }\n}\n\n\nvoid backend_control(struct backend *backend, const struct command *command,\n struct reply *reply)\n{\n int bank = backend->selected;\n if (command->count != 3 || command->argument[0] < 0 ||\n command->argument[0] >= 8) {\n reply->result = -EINVAL;\n return;\n }\n backend->value[bank][command->argument[0]] = command->argument[1];\n if (command->argument[2] >= 0) {\n size_t index;\n for (index = 0; index < 256; index++) {\n backend->storage[bank][index] = (unsigned char)\n ((index * 17 + command->argument[2]) & 255);\n }\n }\n}\n\nint device_call(struct backend *backend, int operation, int a, int b,\n unsigned char *bytes, size_t length)\n{\n int *state = backend->value[backend->selected];\n int result = fault_take(backend, operation - 1);\n (void)b;\n (void)length;\n if (!result && operation == 1) {\n state[0] = a;\n state[1]++;\n } else if (!result && operation == 2) {\n unsigned value = (unsigned)state[0];\n size_t index;\n for (index = 0; index < 4; index++) {\n bytes[index] = (unsigned char)(value >> (8 * index));\n }\n state[2]++;\n }\n trace_add(&backend->trace, \"ATTR\", operation, a, result);\n return result;\n}\n",
"Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -pedantic\nSOURCES = client.c backend.c wire.c console.c codec.c\n\nobserve: $(SOURCES) lab.h\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\n\nclean:\n\trm -f observe\n.PHONY: clean\n"
}
}