69 lines
18 KiB
JSON
69 lines
18 KiB
JSON
{
|
|
"mode": "flags",
|
|
"seed": 18501,
|
|
"support": {
|
|
"device.h": "#ifndef DEVICE_PROTOCOL_H\n#define DEVICE_PROTOCOL_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#define DEVICE_CHANNELS 8\n#define DEVICE_CAPACITY 32\n#define DEVICE_HISTORY 64\n#define DEVICE_LINE 2048\n#ifndef APP_FEATURE\n#define APP_FEATURE 0\n#endif\n#ifndef APP_VERSION\n#define APP_VERSION 1\n#endif\n#ifndef BUILD_PROFILE\n#define BUILD_PROFILE 1\n#endif\ntypedef enum {\n REQUEST_RESET,\n REQUEST_PUT,\n REQUEST_DRAIN,\n REQUEST_DROP,\n REQUEST_STATE,\n REQUEST_INVALID\n} RequestKind;\ntypedef struct {\n uint32_t sequence;\n uint32_t channel;\n int32_t value;\n} DeviceRecord;\ntypedef struct {\n RequestKind kind;\n uint32_t argument;\n DeviceRecord record;\n} Request;\ntypedef struct {\n int status;\n uint32_t epoch;\n uint32_t accepted;\n uint32_t dropped;\n uint32_t pending;\n uint32_t digest;\n int backend;\n int feature;\n int version;\n size_t count;\n int64_t values[DEVICE_CAPACITY];\n} Response;\ntypedef struct {\n DeviceRecord records[DEVICE_CAPACITY];\n size_t count;\n} Journal;\ntypedef struct {\n unsigned char bytes[128];\n size_t cursor;\n size_t available;\n int ended;\n int failed;\n} InputBuffer;\ntypedef enum {\n INPUT_END,\n INPUT_READY,\n INPUT_REJECTED,\n INPUT_FAILED\n} InputResult;\nvoid input_init(InputBuffer *input);\nInputResult input_next(FILE *stream, InputBuffer *input, char *line, size_t capacity);\nint text_request(const char *line, Request *out);\nvoid text_response(const Response *response);\nuint32_t bytes_crc(const unsigned char *raw, size_t size);\nvoid record_encode(const DeviceRecord *record, unsigned char out[16]);\nint record_decode(const unsigned char *raw, size_t size, DeviceRecord *out);\nuint32_t read_le32(const unsigned char *raw);\nvoid write_le32(unsigned char *raw, uint32_t value);\nint parse_u32(const char *text, uint32_t *out);\nint parse_i32(const char *text, int32_t *out);\nint hex_decode(const char *text, unsigned char *out, size_t limit, size_t *size);\nvoid journal_init(Journal *journal);\nint journal_append(Journal *journal, DeviceRecord record);\nint journal_remove(Journal *journal, size_t index, DeviceRecord *out);\nsize_t journal_discard(Journal *journal, uint32_t channel);\nuint32_t journal_digest(const Journal *journal);\nint journal_find(const Journal *journal, uint32_t sequence);\nvoid engine_reset(uint32_t epoch);\nvoid engine_execute(const Request *request, Response *response);\nint64_t calibration_apply(int32_t value, uint32_t channel);\nint calibration_identity(void);\n#endif\n",
|
|
"protocol.c": "#include \"device.h\"\n#include <ctype.h>\n#include <errno.h>\n#include <inttypes.h>\n#include <limits.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nuint32_t read_le32(const unsigned char *raw)\n{\n uint32_t value = (uint32_t)raw[0];\n value |= (uint32_t)raw[1] << 8;\n value |= (uint32_t)raw[2] << 16;\n value |= (uint32_t)raw[3] << 24;\n return value;\n}\n\nvoid write_le32(unsigned char *raw, uint32_t value)\n{\n raw[0] = (unsigned char)value;\n raw[1] = (unsigned char)(value >> 8);\n raw[2] = (unsigned char)(value >> 16);\n raw[3] = (unsigned char)(value >> 24);\n}\n\nuint32_t bytes_crc(const unsigned char *raw, size_t size)\n{\n uint32_t crc = UINT32_C(0xffffffff);\n for (size_t i = 0; i < size; ++i) {\n crc ^= raw[i];\n for (unsigned bit = 0; bit < 8; ++bit) {\n uint32_t mask = 0u - (crc & 1u);\n crc = (crc >> 1) ^ (UINT32_C(0xedb88320) & mask);\n }\n }\n return ~crc;\n}\n\nvoid record_encode(const DeviceRecord *record, unsigned char out[16])\n{\n write_le32(out, record->sequence);\n write_le32(out + 4, record->channel);\n write_le32(out + 8, (uint32_t)record->value);\n write_le32(out + 12, bytes_crc(out, 12));\n}\n\nint record_decode(const unsigned char *raw, size_t size, DeviceRecord *out)\n{\n if (size != 16) {\n return 0;\n }\n if (read_le32(raw + 12) != bytes_crc(raw, 12)) {\n return 0;\n }\n uint32_t channel = read_le32(raw + 4);\n if (channel >= DEVICE_CHANNELS) {\n return 0;\n }\n out->sequence = read_le32(raw);\n out->channel = channel;\n out->value = (int32_t)read_le32(raw + 8);\n return 1;\n}\n\nint parse_u32(const char *text, uint32_t *out)\n{\n char *end = NULL;\n if (text == NULL || *text == '\\0' || *text == '-') {\n return 0;\n }\n errno = 0;\n unsigned long long value = strtoull(text, &end, 10);\n if (errno != 0 || *end != '\\0' || value > UINT32_MAX) {\n return 0;\n }\n *out = (uint32_t)value;\n return 1;\n}\n\nint parse_i32(const char *text, int32_t *out)\n{\n char *end = NULL;\n if (text == NULL || *text == '\\0') {\n return 0;\n }\n errno = 0;\n long long value = strtoll(text, &end, 10);\n if (errno != 0 || *end != '\\0' || value < INT32_MIN || value > INT32_MAX) {\n return 0;\n }\n *out = (int32_t)value;\n return 1;\n}\n\nstatic int hex_nibble(char digit)\n{\n if (digit >= '0' && digit <= '9') {\n return digit - '0';\n }\n if (digit >= 'a' && digit <= 'f') {\n return digit - 'a' + 10;\n }\n if (digit >= 'A' && digit <= 'F') {\n return digit - 'A' + 10;\n }\n return -1;\n}\n\nint hex_decode(const char *text, unsigned char *out, size_t limit, size_t *size)\n{\n size_t length = strlen(text);\n if (length % 2 || length / 2 > limit) {\n return 0;\n }\n for (size_t i = 0; i < length / 2; ++i) {\n int high = hex_nibble(text[i * 2]);\n int low = hex_nibble(text[i * 2 + 1]);\n if (high < 0 || low < 0) {\n return 0;\n }\n out[i] = (unsigned char)((high << 4) | low);\n }\n *size = length / 2;\n return 1;\n}\n\nstatic size_t tokens_copy(const char *line, char storage[DEVICE_LINE], char **tokens)\n{\n size_t length = strlen(line);\n if (length >= DEVICE_LINE) {\n return 9;\n }\n memcpy(storage, line, length + 1);\n char *next = storage;\n size_t count = 0;\n while (*next != '\\0') {\n while (isspace((unsigned char)*next)) {\n ++next;\n }\n if (*next == '\\0') {\n break;\n }\n if (count == 8) {\n return 9;\n }\n tokens[count++] = next;\n while (*next != '\\0' && !isspace((unsigned char)*next)) {\n ++next;\n }\n if (*next != '\\0') {\n *next++ = '\\0';\n }\n }\n return count;\n}\n\nint text_request(const char *line, Request *out)\n{\n char storage[DEVICE_LINE];\n char *token[8];\n size_t count = tokens_copy(line, storage, token);\n memset(out, 0, sizeof(*out));\n out->kind = REQUEST_INVALID;\n if (count == 0 || count > 8) {\n return 0;\n }\n if (strcmp(token[0], \"RESET\") == 0 && count == 2) {\n out->kind = REQUEST_RESET;\n return parse_u32(token[1], &out->argument);\n }\n if (strcmp(token[0], \"PUT\") == 0 && count == 4) {\n out->kind = REQUEST_PUT;\n return parse_u32(token[1], &out->record.sequence) &&\n parse_u32(token[2], &out->record.channel) &&\n parse_i32(token[3], &out->record.value);\n }\n if (strcmp(token[0], \"DRAIN\") == 0 && count == 2) {\n out->kind = REQUEST_DRAIN;\n return parse_u32(token[1], &out->argument);\n }\n if (strcmp(token[0], \"DROP\") == 0 && count == 2) {\n out->kind = REQUEST_DROP;\n return parse_u32(token[1], &out->argument);\n }\n if (strcmp(token[0], \"STATE\") == 0 && count == 1) {\n out->kind = REQUEST_STATE;\n return 1;\n }\n if (strcmp(token[0], \"WIRE\") == 0 && count == 2) {\n unsigned char raw[32];\n size_t size = 0;\n if (!hex_decode(token[1], raw, sizeof(raw), &size) ||\n !record_decode(raw, size, &out->record)) {\n return 0;\n }\n out->kind = REQUEST_PUT;\n return 1;\n }\n return 0;\n}\n\nvoid text_response(const Response *response)\n{\n printf(\"R %d %\" PRIu32 \" %\" PRIu32 \" %\" PRIu32 \" %\" PRIu32,\n response->status, response->epoch, response->accepted,\n response->dropped, response->pending);\n printf(\" %\" PRIu32 \" %d %d %d %zu\", response->digest,\n response->backend, response->feature, response->version,\n response->count);\n for (size_t i = 0; i < response->count; ++i) {\n printf(\" %\" PRId64, response->values[i]);\n }\n putchar('\\n');\n}\n",
|
|
"journal.c": "#include \"device.h\"\n#include <string.h>\n\nstatic int journal_valid(const Journal *journal)\n{\n if (journal == NULL) {\n return 0;\n }\n if (journal->count > DEVICE_CAPACITY) {\n return 0;\n }\n for (size_t i = 0; i < journal->count; ++i) {\n if (journal->records[i].channel >= DEVICE_CHANNELS) {\n return 0;\n }\n }\n return 1;\n}\n\nstatic size_t journal_snapshot(const Journal *journal, unsigned char *raw,\n size_t capacity)\n{\n if (!journal_valid(journal)) {\n return 0;\n }\n size_t needed = journal->count * 16;\n if (needed > capacity) {\n return 0;\n }\n size_t offset = 0;\n for (size_t i = 0; i < journal->count; ++i) {\n record_encode(&journal->records[i], raw + offset);\n offset += 16;\n }\n return offset;\n}\n\nvoid journal_init(Journal *journal)\n{\n memset(journal, 0, sizeof(*journal));\n}\n\nint journal_append(Journal *journal, DeviceRecord record)\n{\n if (!journal_valid(journal) || journal->count >= DEVICE_CAPACITY) {\n return 0;\n }\n journal->records[journal->count] = record;\n journal->count += 1;\n return 1;\n}\n\nint journal_remove(Journal *journal, size_t index, DeviceRecord *out)\n{\n if (!journal_valid(journal) || index >= journal->count) {\n return 0;\n }\n *out = journal->records[index];\n for (size_t i = index + 1; i < journal->count; ++i) {\n journal->records[i - 1] = journal->records[i];\n }\n journal->count -= 1;\n memset(&journal->records[journal->count], 0,\n sizeof(journal->records[journal->count]));\n return 1;\n}\n\nsize_t journal_discard(Journal *journal, uint32_t channel)\n{\n size_t retained = 0;\n size_t removed = 0;\n for (size_t i = 0; i < journal->count; ++i) {\n DeviceRecord record = journal->records[i];\n if (record.channel == channel) {\n removed += 1;\n } else {\n journal->records[retained] = record;\n retained += 1;\n }\n }\n for (size_t i = retained; i < journal->count; ++i) {\n memset(&journal->records[i], 0, sizeof(journal->records[i]));\n }\n journal->count = retained;\n return removed;\n}\n\nuint32_t journal_digest(const Journal *journal)\n{\n unsigned char raw[DEVICE_CAPACITY * 16];\n size_t size = journal_snapshot(journal, raw, sizeof(raw));\n return bytes_crc(raw, size);\n}\n\nint journal_find(const Journal *journal, uint32_t sequence)\n{\n for (size_t i = 0; i < journal->count; ++i) {\n if (journal->records[i].sequence == sequence) {\n return (int)i;\n }\n }\n return -1;\n}\n",
|
|
"input.c": "#include \"device.h\"\n#include <string.h>\n\nvoid input_init(InputBuffer *input)\n{\n memset(input, 0, sizeof(*input));\n}\n\nstatic int input_refill(FILE *stream, InputBuffer *input)\n{\n if (input->failed || input->ended) {\n return 0;\n }\n input->cursor = 0;\n input->available = fread(input->bytes, 1, sizeof(input->bytes), stream);\n if (input->available != 0) {\n return 1;\n }\n if (ferror(stream)) {\n input->failed = 1;\n } else {\n input->ended = 1;\n }\n return 0;\n}\n\nstatic int input_byte(FILE *stream, InputBuffer *input, unsigned char *byte)\n{\n if (input->cursor == input->available) {\n if (!input_refill(stream, input)) {\n return 0;\n }\n }\n *byte = input->bytes[input->cursor];\n input->cursor += 1;\n return 1;\n}\n\nstatic InputResult input_finish(char *line, size_t size, int rejected)\n{\n line[size] = '\\0';\n if (rejected) {\n return INPUT_REJECTED;\n }\n return INPUT_READY;\n}\n\nInputResult input_next(FILE *stream, InputBuffer *input, char *line, size_t capacity)\n{\n if (capacity < 2) {\n return INPUT_FAILED;\n }\n size_t size = 0;\n int rejected = 0;\n int seen = 0;\n for (;;) {\n unsigned char byte;\n if (!input_byte(stream, input, &byte)) {\n if (input->failed) {\n return INPUT_FAILED;\n }\n if (!seen) {\n return INPUT_END;\n }\n return input_finish(line, size, rejected);\n }\n seen = 1;\n if (byte == '\\n') {\n return input_finish(line, size, rejected);\n }\n if (byte == 0) {\n rejected = 1;\n continue;\n }\n if (size + 1 == capacity) {\n rejected = 1;\n continue;\n }\n if (!rejected) {\n line[size] = (char)byte;\n size += 1;\n }\n }\n}\n",
|
|
"main.c": "#include \"device.h\"\n#include <stdio.h>\n#include <string.h>\n\nstatic int consume_line(char *line)\n{\n Request request;\n Response response;\n size_t size = strlen(line);\n if (size && line[size - 1] == '\\n') {\n line[size - 1] = '\\0';\n }\n if (!text_request(line, &request)) {\n memset(&request, 0, sizeof(request));\n request.kind = REQUEST_INVALID;\n }\n engine_execute(&request, &response);\n text_response(&response);\n return ferror(stdout) ? 0 : 1;\n}\n\nint main(void)\n{\n char line[DEVICE_LINE];\n InputBuffer input;\n input_init(&input);\n engine_reset(0);\n for (;;) {\n InputResult result = input_next(stdin, &input, line, sizeof(line));\n if (result == INPUT_END) {\n break;\n }\n if (result == INPUT_FAILED) {\n return 2;\n }\n if (result == INPUT_REJECTED) {\n strcpy(line, \"INVALID\");\n }\n if (!consume_line(line)) {\n return 2;\n }\n }\n return ferror(stdin) ? 2 : 0;\n}\n"
|
|
},
|
|
"candidate_files": [
|
|
"Makefile",
|
|
"engine.c"
|
|
],
|
|
"configs": [
|
|
{
|
|
"make": [
|
|
"clean",
|
|
"all"
|
|
],
|
|
"feature": 0,
|
|
"version": 17,
|
|
"env": {
|
|
"CFLAGS": "-std=c11 -O1 -DAPP_FEATURE=0 -DAPP_VERSION=17"
|
|
}
|
|
},
|
|
{
|
|
"make": [
|
|
"clean",
|
|
"all"
|
|
],
|
|
"feature": 1,
|
|
"version": 23,
|
|
"env": {
|
|
"CFLAGS": "-std=c11 -O1 -DAPP_FEATURE=1 -DAPP_VERSION=23"
|
|
}
|
|
},
|
|
{
|
|
"make": [
|
|
"clean",
|
|
"all"
|
|
],
|
|
"feature": 1,
|
|
"version": 91,
|
|
"env": {
|
|
"CFLAGS": "-std=c11 -O1 -DAPP_FEATURE=1 -DAPP_VERSION=91"
|
|
}
|
|
},
|
|
{
|
|
"make": [
|
|
"clean",
|
|
"all",
|
|
"CC=gcc",
|
|
"CFLAGS=-std=c11 -O0",
|
|
"LDFLAGS=-static",
|
|
"LDLIBS=-lm"
|
|
],
|
|
"env": {
|
|
"CPPFLAGS": "-DAPP_FEATURE=1 -DAPP_VERSION=61"
|
|
},
|
|
"feature": 1,
|
|
"version": 61,
|
|
"static": 1,
|
|
"observe": "static"
|
|
}
|
|
],
|
|
"driver": "import atexit\nimport json\nimport os\nfrom pathlib import Path\nimport re\nimport shutil\nimport subprocess\nimport sys\n\nroot = Path(__file__).resolve().parent\natexit.register(shutil.rmtree, root, ignore_errors=True)\nsteps = json.loads((root / \"steps.json\").read_text())\nfor index, step in enumerate(steps):\n work = root / step[\"directory\"]\n env = os.environ.copy()\n env.update(step.get(\"env\", {}))\n build = subprocess.run([\"make\", \"--no-print-directory\", *step[\"make\"]],\n cwd=work, env=env, capture_output=True, text=True)\n if build.returncode:\n sys.stderr.write(build.stdout + build.stderr)\n raise SystemExit(build.returncode)\n print(\"BEGIN\", index, flush=True)\n if step.get(\"observe\") == \"static\":\n elf = subprocess.run([\"readelf\", \"-d\", \"app\"], cwd=work,\n capture_output=True, text=True)\n print(\"DYNAMIC\", int(\"(NEEDED)\" in elf.stdout), flush=True)\n target = root / (\"deployed-\" + str(index))\n target.mkdir()\n shutil.copyfile(work / \"app\", target / \"app\")\n (target / \"app\").chmod(0o755)\n run_at = target\n else:\n run_at = work\n if step.get(\"observe\") == \"exports\":\n symbols = subprocess.run([\"readelf\", \"--dyn-syms\", \"--wide\", \"libdevice.so\"],\n cwd=work, capture_output=True, text=True)\n names = set()\n for line in symbols.stdout.splitlines():\n fields = line.split()\n if len(fields) >= 8 and fields[3] == \"FUNC\" and fields[4] == \"GLOBAL\" and fields[6] != \"UND\":\n if fields[7].startswith(\"engine_\"):\n names.add(fields[7])\n for name in sorted(names):\n print(\"EXPORT\", name, flush=True)\n target = root / (\"deployed-\" + str(index))\n work.rename(target)\n run_at = target\n stdin = (root / step[\"input\"]).read_text()\n result = subprocess.run([str(run_at / \"app\")], cwd=run_at, input=stdin,\n capture_output=True, text=True, timeout=10)\n sys.stdout.write(result.stdout)\n sys.stderr.write(result.stderr)\n if result.returncode:\n raise SystemExit(result.returncode)\n print(\"END\", index, flush=True)\n"
|
|
}
|