One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
243 lines
36 KiB
Python
243 lines
36 KiB
Python
import base64
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
SUPPORT = {'Makefile': 'CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -Wpedantic -Iinclude\nSOURCES = src/borrowed_reader.c src/record.c src/stream_port.c src/demo.c\nHEADERS = include/borrowed_reader.h include/stream_port.h\n.PHONY: all clean demo\nall: borrowed-reader\nborrowed-reader: $(SOURCES) $(HEADERS)\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\ndemo: borrowed-reader\n\t./borrowed-reader examples/default.br\nclean:\n\trm -f borrowed-reader\n', 'README.md': '# Borrowed record reader\n\nA small telemetry import pipeline with a deterministic in-memory stream port.\nBuild with `make` and run the examples described in docs/contract.md.\nThe port is a software stand-in for a Linux file descriptor. It keeps resource\nvalidity observable with integers; it does not dereference stale heap objects.\nThe public contract and all supported input and option ranges are in\n[docs/contract.md](docs/contract.md).\n', 'docs/contract.md': '# Record reader contract\n\n1. Build and public reproduction\n\nThe project uses C11, gcc and make, with no downloads. Run `make`, then\n`./borrowed-reader examples/default.br`. A conforming default import reports\nstatus=ok, accepted=2, rejected=0, sum=1, minimum=-2, maximum=3. Run\n`./borrowed-reader examples/recovery.br 1 -1 0 0` for tolerant recovery,\n`./borrowed-reader examples/recovery.br 64 -1 1 0` for strict mode, and\n`./borrowed-reader examples/default.br 2 12 0 0` for an input fault.\n\nThe CLI is `./borrowed-reader FILE [CHUNK [FAIL_AT [STRICT [LIMIT]]]]`.\nCHUNK is 1..64 (default 7), FAIL_AT is -1 or a byte offset 0..input length\n(default -1), STRICT is 0 or 1 (default 0), LIMIT is 0..4096 (default 0).\nInputs have 0..16384 bytes. Only these valid argument ranges are graded.\n\n2. Public API and lifetime\n\n`rb_collect(source, options, report)` is declared in include/borrowed_reader.h.\nIt returns the same rb_status that it writes in report.status. The caller\nsupplies nonnull pointers, an already open source, and the documented options.\nThe source is borrowed: rb_collect and its helpers must never call close,\nincluding successful reads, header rejection, strict rejection, limit stops,\nand input failures. The caller retains the same valid handle after return,\ncan perform a zero-capacity read, and closes it exactly once. No read may be\nattempted with an invalid handle. The reader must not store a source or handle\nfor use by a later rb_collect call. Calls may reuse port slots with new handles.\n\n3. Stream port semantics\n\nstream_port.h provides safe integer handles tagged with a generation. A close\ninvalidates only that live handle. Invalid reads/closes return -9. Read returns\n0 for EOF, a positive byte count for data, or -5 for an input failure. A valid\nzero-capacity read always returns 0 and consumes nothing, even at FAIL_AT.\nPositive reads return at most min(capacity,CHUNK,bytes before EOF or FAIL_AT).\nWith FAIL_AT >= 0, a positive read at that offset returns -5, including when\nthe offset equals input length. FAIL_AT=-1 disables the fault. The reader may\nbuffer or read one byte at a time; read call counts and read-ahead positions\nare not prescribed. The port never accesses arbitrary memory through handles.\n\n4. Header and line boundaries\n\nThe first line must be exactly the three bytes BR1 followed by LF. Missing,\nunterminated, different, or overlong headers return RB_BAD_HEADER with an\nempty report. A read failure before the terminating LF returns RB_IO_ERROR.\nAfter the header, LF separates records. A final nonempty line without LF is\nprocessed as a record. A final empty EOF does not create a record. Payloads\nlonger than RB_LINE_MAX=79 bytes are drained to LF/EOF and rejected once.\nAn input failure while collecting or draining a line returns RB_IO_ERROR;\nthat unfinished line is discarded, with no accepted/rejected increment.\n\n5. Record grammar and recovery\n\nA blank line, or a line whose first byte is #, is ignored when its length is\nat most 79; an overlong comment is rejected. Other lines must be NAME,VALUE.\nNAME has 1..12 bytes: first is a..z, and the rest a..z or underscore. VALUE\nhas an optional + or - followed by one or more ASCII decimal digits, with\nmagnitude <=1000000; leading zeros and signed zero are valid. No whitespace,\nCR, embedded NUL, extra comma, or trailing bytes are permitted in a record.\nA malformed or overlong record increments rejected. STRICT=0 skips it and\ncontinues; STRICT=1 immediately returns RB_BAD_RECORD. Ignored lines do not\nincrement either count. Header bytes are never treated as records.\n\n6. Reports, limits and errors\n\nEach call starts an empty report: accepted/rejected/sum/minimum/maximum=0,\nchecksum=2166136261. Each accepted record increments accepted, adds its signed\nvalue to int64 sum, and updates minimum/maximum over accepted values. When\nthere are no accepted records minimum/maximum remain 0. Checksum is FNV-1a\n32: for each accepted record feed its original payload bytes followed by LF,\neach byte applying hash=(hash XOR byte)*16777619 modulo 2^32. A missing final\nLF is normalized to one LF for hashing. Ignored/rejected/header bytes do not\ncontribute. LIMIT=0 is unlimited; otherwise immediately after accepting the\nLIMITth record return RB_LIMIT, even if it was the last record. No later\nline is processed. Reaching EOF normally returns RB_OK. IO failure returns\nRB_IO_ERROR. Every stop retains the report for fully processed earlier lines;\nno incomplete line contributes. Every public report field is defined. There\nis no requirement on struct padding, unused buffers, diagnostics on stderr,\nor the exact number/size of reads.\n\n7. Submission and observable diagnostics\n\nSubmit only src/borrowed_reader.c. Other public files are fixed support and\nare rebuilt cleanly for grading. The CLI prints its report and then raw port\nobservations: open=1, probe=0, reader_closes=0, invalid_reads=0, owner_close=0,\nlive=0 for every conforming run. Its exit status is 0 for this correct owner\nhandoff, including parsing/input failures reported in the report; malformed\nCLI arguments return 2. Grading calls the same API on multiple streams in a\nsingle process, and compares report values and these lifetime observations.\n', 'examples/default.br': 'BR1\nalpha,3\nbeta,-2\n', 'examples/recovery.br': 'BR1\nalpha,3\nbad record\nbeta,-2\ngamma,+7\n', 'include/borrowed_reader.h': '#ifndef BORROWED_READER_H\n#define BORROWED_READER_H\n#include <stddef.h>\n#include <stdint.h>\n#define RB_LINE_MAX 79\n#define RB_NAME_MAX 12\n#define RB_VALUE_MAX 1000000\n#define RB_INPUT_MAX 16384\n#define RB_FNV_START UINT32_C(2166136261)\nenum rb_status {\n RB_OK = 0,\n RB_LIMIT = 1,\n RB_BAD_HEADER = 2,\n RB_BAD_RECORD = 3,\n RB_IO_ERROR = 4\n};\ntypedef int (*rb_read_fn)(void *, int, unsigned char *, size_t);\ntypedef int (*rb_close_fn)(void *, int);\nstruct rb_source {\n void *context;\n int handle;\n rb_read_fn read;\n rb_close_fn close;\n};\nstruct rb_options {\n int strict;\n unsigned limit;\n};\nstruct rb_report {\n enum rb_status status;\n unsigned accepted;\n unsigned rejected;\n int64_t sum;\n int minimum;\n int maximum;\n uint32_t checksum;\n};\nstruct rb_record {\n char name[RB_NAME_MAX + 1];\n int value;\n};\nvoid rb_report_init(struct rb_report *report);\nint rb_record_parse(const unsigned char *line, size_t length,\n struct rb_record *record);\nvoid rb_report_accept(struct rb_report *report, const struct rb_record *record,\n const unsigned char *line, size_t length);\nint rb_line_ignored(const unsigned char *line, size_t length);\nconst char *rb_status_name(enum rb_status status);\nenum rb_status rb_collect(const struct rb_source *source,\n const struct rb_options *options,\n struct rb_report *report);\n#endif\n', 'include/stream_port.h': '#ifndef STREAM_PORT_H\n#define STREAM_PORT_H\n#include <stddef.h>\n#include "borrowed_reader.h"\n#define SP_SLOTS 4\n#define SP_BAD_HANDLE (-9)\n#define SP_IO_ERROR (-5)\nstruct sp_statistics {\n unsigned close_attempts;\n unsigned invalid_reads;\n unsigned successful_closes;\n};\nvoid sp_reset(void);\nvoid sp_clear_statistics(void);\nint sp_open(const unsigned char *data, size_t length, size_t chunk, long fail_at);\nint sp_read(void *context, int handle, unsigned char *destination, size_t capacity);\nint sp_close(void *context, int handle);\nint sp_is_open(int handle);\nunsigned sp_live_count(void);\nstruct sp_statistics sp_statistics(void);\nstruct rb_source sp_source(int handle);\n#endif\n', 'src/demo.c': '#include "borrowed_reader.h"\n#include "stream_port.h"\n#include <errno.h>\n#include <inttypes.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nstatic int parse_long(const char *text, long minimum, long maximum, long *result)\n{\n char *end;\n long value;\n errno = 0;\n value = strtol(text, &end, 10);\n if (errno || end == text || *end || value < minimum || value > maximum) {\n return 0;\n }\n *result = value;\n return 1;\n}\n\nstatic int read_input(const char *path, unsigned char *data, size_t *length)\n{\n FILE *input = fopen(path, "rb");\n size_t size;\n int failed;\n if (input == NULL) {\n perror(path);\n return 0;\n }\n size = fread(data, 1, RB_INPUT_MAX + 1, input);\n failed = ferror(input);\n if (fclose(input) != 0) {\n failed = 1;\n }\n if (failed || size > RB_INPUT_MAX) {\n fprintf(stderr, "input unavailable or larger than %d bytes\\n", RB_INPUT_MAX);\n return 0;\n }\n *length = size;\n return 1;\n}\n\nstatic void print_report(const struct rb_report *report)\n{\n printf("status=%s accepted=%u rejected=%u sum=%" PRId64 " ",\n rb_status_name(report->status), report->accepted,\n report->rejected, report->sum);\n printf("minimum=%d maximum=%d checksum=%" PRIu32 "\\n",\n report->minimum, report->maximum, report->checksum);\n}\n\nint main(int argc, char **argv)\n{\n unsigned char data[RB_INPUT_MAX + 1];\n size_t length;\n long chunk = 7;\n long fail_at = -1;\n long strict = 0;\n long limit = 0;\n int handle;\n int probe;\n int closed;\n int was_open;\n unsigned char dummy = 0;\n struct rb_source source;\n struct rb_options options;\n struct rb_report report;\n struct sp_statistics observed;\n if (argc < 2 || argc > 6) {\n fprintf(stderr, "usage: %s FILE [CHUNK [FAIL_AT [STRICT [LIMIT]]]]\\n", argv[0]);\n return 2;\n }\n if ((argc > 2 && !parse_long(argv[2], 1, 64, &chunk)) ||\n (argc > 3 && !parse_long(argv[3], -1, RB_INPUT_MAX, &fail_at)) ||\n (argc > 4 && !parse_long(argv[4], 0, 1, &strict)) ||\n (argc > 5 && !parse_long(argv[5], 0, 4096, &limit))) {\n fprintf(stderr, "invalid option\\n");\n return 2;\n }\n if (!read_input(argv[1], data, &length) ||\n (fail_at >= 0 && (unsigned long)fail_at > length)) {\n return 2;\n }\n sp_reset();\n handle = sp_open(data, length, (size_t)chunk, fail_at);\n if (handle < 0) {\n fprintf(stderr, "cannot open stream\\n");\n return 2;\n }\n source = sp_source(handle);\n options.strict = (int)strict;\n options.limit = (unsigned)limit;\n sp_clear_statistics();\n rb_collect(&source, &options, &report);\n observed = sp_statistics();\n was_open = sp_is_open(handle);\n probe = sp_read(NULL, handle, &dummy, 0);\n closed = sp_close(NULL, handle);\n print_report(&report);\n printf("open=%d probe=%d reader_closes=%u invalid_reads=%u owner_close=%d live=%u\\n",\n was_open, probe, observed.close_attempts, observed.invalid_reads,\n closed, sp_live_count());\n return closed == 0 && was_open && probe == 0 ? 0 : 1;\n}\n', 'src/record.c': '#include "borrowed_reader.h"\n#include <string.h>\n\nvoid rb_report_init(struct rb_report *report)\n{\n report->status = RB_OK;\n report->accepted = 0;\n report->rejected = 0;\n report->sum = 0;\n report->minimum = 0;\n report->maximum = 0;\n report->checksum = RB_FNV_START;\n}\n\nstatic int name_start(unsigned char value)\n{\n return value >= \'a\' && value <= \'z\';\n}\n\nstatic int name_continue(unsigned char value)\n{\n return name_start(value) || value == \'_\';\n}\n\nstatic int parse_number(const unsigned char *text, size_t length, int *result)\n{\n size_t position = 0;\n unsigned magnitude = 0;\n int sign = 1;\n if (length == 0) {\n return 0;\n }\n if (text[position] == \'-\' || text[position] == \'+\') {\n sign = text[position] == \'-\' ? -1 : 1;\n position++;\n }\n if (position == length) {\n return 0;\n }\n for (; position < length; position++) {\n unsigned digit;\n if (text[position] < \'0\' || text[position] > \'9\') {\n return 0;\n }\n digit = (unsigned)(text[position] - \'0\');\n if (magnitude > ((unsigned)RB_VALUE_MAX - digit) / 10) {\n return 0;\n }\n magnitude = magnitude * 10 + digit;\n }\n *result = sign * (int)magnitude;\n return 1;\n}\n\nint rb_record_parse(const unsigned char *line, size_t length,\n struct rb_record *record)\n{\n size_t comma = 0;\n int value;\n if (length == 0 || length > RB_LINE_MAX || !name_start(line[0])) {\n return 0;\n }\n while (comma < length && line[comma] != \',\') {\n if (!name_continue(line[comma]) || comma >= RB_NAME_MAX) {\n return 0;\n }\n comma++;\n }\n if (comma == 0 || comma == length || comma > RB_NAME_MAX) {\n return 0;\n }\n if (!parse_number(line + comma + 1, length - comma - 1, &value)) {\n return 0;\n }\n memcpy(record->name, line, comma);\n record->name[comma] = \'\\0\';\n record->value = value;\n return 1;\n}\n\nstatic uint32_t hash_byte(uint32_t hash, unsigned char value)\n{\n return (hash ^ (uint32_t)value) * UINT32_C(16777619);\n}\n\nvoid rb_report_accept(struct rb_report *report, const struct rb_record *record,\n const unsigned char *line, size_t length)\n{\n size_t position;\n if (report->accepted == 0 || record->value < report->minimum) {\n report->minimum = record->value;\n }\n if (report->accepted == 0 || record->value > report->maximum) {\n report->maximum = record->value;\n }\n report->accepted++;\n report->sum += record->value;\n for (position = 0; position < length; position++) {\n report->checksum = hash_byte(report->checksum, line[position]);\n }\n report->checksum = hash_byte(report->checksum, \'\\n\');\n}\n\nint rb_line_ignored(const unsigned char *line, size_t length)\n{\n return length == 0 || line[0] == \'#\';\n}\n\nconst char *rb_status_name(enum rb_status status)\n{\n switch (status) {\n case RB_OK:\n return "ok";\n case RB_LIMIT:\n return "limit";\n case RB_BAD_HEADER:\n return "bad_header";\n case RB_BAD_RECORD:\n return "bad_record";\n case RB_IO_ERROR:\n return "io_error";\n default:\n return "unknown";\n }\n}\n', 'src/stream_port.c': '#include "stream_port.h"\n#include <limits.h>\n#include <string.h>\n\nstruct stream_slot {\n const unsigned char *data;\n size_t length;\n size_t cursor;\n size_t chunk;\n long fail_at;\n unsigned generation;\n int live;\n};\nstatic struct stream_slot slots[SP_SLOTS];\nstatic struct sp_statistics statistics;\n\nstatic int encode_handle(unsigned index, unsigned generation)\n{\n return (int)(generation * 8 + index + 1);\n}\n\nstatic struct stream_slot *lookup(int handle)\n{\n unsigned encoded;\n unsigned index;\n unsigned generation;\n if (handle <= 0) {\n return NULL;\n }\n encoded = (unsigned)handle;\n index = encoded % 8;\n generation = encoded / 8;\n if (index == 0 || index > SP_SLOTS || generation == 0) {\n return NULL;\n }\n index--;\n if (!slots[index].live || slots[index].generation != generation) {\n return NULL;\n }\n return &slots[index];\n}\n\nvoid sp_clear_statistics(void)\n{\n statistics.close_attempts = 0;\n statistics.invalid_reads = 0;\n statistics.successful_closes = 0;\n}\n\nvoid sp_reset(void)\n{\n unsigned index;\n for (index = 0; index < SP_SLOTS; index++) {\n slots[index].data = NULL;\n slots[index].length = 0;\n slots[index].cursor = 0;\n slots[index].chunk = 1;\n slots[index].fail_at = -1;\n slots[index].generation = 0;\n slots[index].live = 0;\n }\n sp_clear_statistics();\n}\n\nint sp_open(const unsigned char *data, size_t length, size_t chunk, long fail_at)\n{\n unsigned index;\n if ((data == NULL && length != 0) || length > RB_INPUT_MAX ||\n chunk == 0 || chunk > 64 || fail_at < -1 ||\n (fail_at >= 0 && (unsigned long)fail_at > length)) {\n return -1;\n }\n for (index = 0; index < SP_SLOTS; index++) {\n struct stream_slot *slot = &slots[index];\n if (slot->live) {\n continue;\n }\n if (slot->generation >= (unsigned)INT_MAX / 8 - 1) {\n return -1;\n }\n slot->generation++;\n slot->data = data;\n slot->length = length;\n slot->cursor = 0;\n slot->chunk = chunk;\n slot->fail_at = fail_at;\n slot->live = 1;\n return encode_handle(index, slot->generation);\n }\n return -1;\n}\n\nint sp_read(void *context, int handle, unsigned char *destination, size_t capacity)\n{\n struct stream_slot *slot = lookup(handle);\n size_t amount;\n (void)context;\n if (slot == NULL) {\n statistics.invalid_reads++;\n return SP_BAD_HANDLE;\n }\n if (capacity == 0) {\n return 0;\n }\n if (destination == NULL) {\n return SP_IO_ERROR;\n }\n if (slot->fail_at >= 0 && slot->cursor >= (size_t)slot->fail_at) {\n return SP_IO_ERROR;\n }\n if (slot->cursor == slot->length) {\n return 0;\n }\n amount = slot->length - slot->cursor;\n if (amount > slot->chunk) {\n amount = slot->chunk;\n }\n if (amount > capacity) {\n amount = capacity;\n }\n if (slot->fail_at >= 0 && amount > (size_t)slot->fail_at - slot->cursor) {\n amount = (size_t)slot->fail_at - slot->cursor;\n }\n memcpy(destination, slot->data + slot->cursor, amount);\n slot->cursor += amount;\n return (int)amount;\n}\n\nint sp_close(void *context, int handle)\n{\n struct stream_slot *slot;\n (void)context;\n statistics.close_attempts++;\n slot = lookup(handle);\n if (slot == NULL) {\n return SP_BAD_HANDLE;\n }\n slot->live = 0;\n slot->data = NULL;\n statistics.successful_closes++;\n return 0;\n}\n\nint sp_is_open(int handle)\n{\n return lookup(handle) != NULL;\n}\n\nunsigned sp_live_count(void)\n{\n unsigned index;\n unsigned count = 0;\n for (index = 0; index < SP_SLOTS; index++) {\n if (slots[index].live) {\n count++;\n }\n }\n return count;\n}\n\nstruct sp_statistics sp_statistics(void)\n{\n return statistics;\n}\n\nstruct rb_source sp_source(int handle)\n{\n struct rb_source source;\n source.context = NULL;\n source.handle = handle;\n source.read = sp_read;\n source.close = sp_close;\n return source;\n}\n'}
|
|
HARNESS = '#include "borrowed_reader.h"\n#include "stream_port.h"\n#include <inttypes.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic int unhex(unsigned char c)\n{\n if (c >= \'0\' && c <= \'9\') return c - \'0\';\n if (c >= \'a\' && c <= \'f\') return c - \'a\' + 10;\n return -1;\n}\nint main(void)\n{\n unsigned count;\n unsigned index;\n if (scanf("%u", &count) != 1) return 2;\n sp_reset();\n for (index = 0; index < count; index++) {\n char hex[RB_INPUT_MAX * 2 + 2];\n unsigned char data[RB_INPUT_MAX];\n unsigned chunk, strict, limit;\n long fault;\n size_t bytes = 0;\n size_t j;\n int handle, result, live, probe, closed;\n unsigned char dummy = 0;\n struct rb_source source;\n struct rb_options options;\n struct rb_report report;\n struct sp_statistics stats;\n if (scanf("%u %ld %u %u %32769s", &chunk, &fault, &strict, &limit, hex) != 5) return 2;\n if (strcmp(hex, "-")) {\n size_t n = strlen(hex);\n if (n % 2 || n / 2 > RB_INPUT_MAX) return 2;\n for (j = 0; j < n; j += 2) {\n int a = unhex((unsigned char)hex[j]);\n int b = unhex((unsigned char)hex[j+1]);\n if (a < 0 || b < 0) return 2;\n data[bytes++] = (unsigned char)(a * 16 + b);\n }\n }\n handle = sp_open(data, bytes, chunk, fault);\n if (handle < 0) return 2;\n source = sp_source(handle);\n options.strict = (int)strict;\n options.limit = limit;\n sp_clear_statistics();\n result = rb_collect(&source, &options, &report);\n stats = sp_statistics();\n live = sp_is_open(handle);\n probe = sp_read(NULL, handle, &dummy, 0);\n closed = sp_close(NULL, handle);\n printf("%d %d %u %u %" PRId64 " %d %d %" PRIu32 " %d %d %u %u %d %u\\n",\n result, report.status, report.accepted, report.rejected,\n report.sum, report.minimum, report.maximum, report.checksum,\n live, probe, stats.close_attempts, stats.invalid_reads,\n closed, sp_live_count());\n }\n return 0;\n}\n'
|
|
|
|
def cases():
|
|
suite = []
|
|
def add(data, chunk=7, fault=-1, strict=0, limit=0):
|
|
assert len(data) <= 16384
|
|
suite.append((data, chunk, fault, strict, limit))
|
|
default = b'BR1\nalpha,3\nbeta,-2\n'
|
|
for chunk in (1, 2, 7, 64):
|
|
add(default, chunk)
|
|
for data in (b'', b'BR1', b'BR2\nx,1\n', b'BR1\n', b'BR1\nx,0',
|
|
b'BR1\nx,-0\n', b'BR1\nx,+0001\n', b'BR1\n\n#comment\ny,-1000000\nz,1000000\n',
|
|
b'X'*80+b'\nalpha,3\n', b'BR1\n'+b'#'*80+b'\nx,3\n'):
|
|
for strict in (0, 1):
|
|
add(data, 1, strict=strict)
|
|
malformed = [b',1', b'A,1', b'a1,1', b'_a,1', b'a,', b'a,+', b'a,--2',
|
|
b'a,1000001', b'a,-1000001', b'a,9999999999999999999999999999',
|
|
b'abcdefghijklmn,1', b'abcdefghijkl,1', b'a,1,2', b'a, 1',
|
|
b'a,1\r', b'a,1\x00', b'a,1 ', b'x'*80, b'#'+b'x'*78, b'']
|
|
for bad in malformed:
|
|
for strict in (0, 1):
|
|
add(b'BR1\nfirst,4\n'+bad+b'\nlast,-2\n', 3, strict=strict)
|
|
for fault in range(len(default)+1):
|
|
add(default, (fault % 9)+1, fault)
|
|
for limit in (1, 2, 3, 4096):
|
|
for strict in (0, 1):
|
|
add(b'BR1\na,3\nb,-8\nc,4\n', 64, strict=strict, limit=limit)
|
|
for fault in (4, 10, 80, 100):
|
|
data = b'BR1\n'+b'x'*100+b'\na,2\n'
|
|
add(data, 11, fault)
|
|
add(b'BR1\n'+b'#'+b'x'*78+b'\na,1000000\n',64)
|
|
add(b'BR1\n'+b'a,00000000000000000000000000000000000000000000000000000000000000000000001\n', 4)
|
|
add(b'BR1\n'+b'a,1\n'*4095,64,limit=4096)
|
|
add(b'BR1\n'+b'a,1\n'*4095,1,limit=4095)
|
|
rng=random.Random(522409)
|
|
for _ in range(90):
|
|
lines=[]
|
|
for j in range(rng.randrange(1,30)):
|
|
mode=rng.randrange(7)
|
|
if mode < 4:
|
|
name=''.join(rng.choice('abcdef_') for _ in range(rng.randrange(1,12)))
|
|
name=rng.choice('abcdef')+name[1:]
|
|
value=rng.randrange(-1000000,1000001)
|
|
lines.append((name+','+str(value)).encode())
|
|
elif mode == 4: lines.append(rng.choice(malformed))
|
|
elif mode == 5: lines.append(b'#note')
|
|
else: lines.append(b'')
|
|
data=b'BR1\n'+b'\n'.join(lines)+(b'\n' if rng.randrange(2) else b'')
|
|
fault=rng.randrange(len(data)+1) if rng.randrange(4)==0 else -1
|
|
add(data,rng.randrange(1,65),fault,rng.randrange(2),rng.choice([0,0,1,2,5]))
|
|
return suite
|
|
|
|
def expected(case):
|
|
data, chunk, fault, strict, limit=case
|
|
report=[0,0,0,0,0,0,0,2166136261]
|
|
position=0
|
|
def line():
|
|
nonlocal position
|
|
payload=bytearray()
|
|
while True:
|
|
if fault >= 0 and position >= fault: return -1,bytes(payload)
|
|
if position == len(data): return (2 if payload else 0),bytes(payload)
|
|
byte=data[position];position+=1
|
|
if byte == 10: return 1,bytes(payload)
|
|
payload.append(byte)
|
|
def status(value):
|
|
report[0]=value;report[1]=value
|
|
return report+[1,0,0,0,0,0]
|
|
rc,header=line()
|
|
if rc == -1: return status(4)
|
|
if rc != 1 or header != b'BR1': return status(2)
|
|
while True:
|
|
rc,payload=line()
|
|
if rc == -1: return status(4)
|
|
if rc == 0: return status(0)
|
|
ignored=len(payload)<=79 and (not payload or payload.startswith(b'#'))
|
|
if not ignored:
|
|
valid=(len(payload)<=79 and re.fullmatch(rb'[a-z][a-z_]{0,11},[+-]?[0-9]+',payload))
|
|
value=int(payload.split(b',')[1]) if valid else 0
|
|
valid=valid and abs(value)<=1000000
|
|
if not valid:
|
|
report[3]+=1
|
|
if strict: return status(3)
|
|
else:
|
|
if report[2]==0:
|
|
report[5]=report[6]=value
|
|
else:
|
|
report[5]=min(report[5],value);report[6]=max(report[6],value)
|
|
report[2]+=1;report[4]+=value
|
|
for byte in payload+b'\n': report[7]=((report[7]^byte)*16777619)&0xffffffff
|
|
if limit and report[2]>=limit: return status(1)
|
|
if rc == 2: return status(0)
|
|
|
|
|
|
EXECUTOR = "\nimport base64,ctypes,json,os,pathlib,signal,subprocess,sys,time\nspec=json.loads(sys.argv[1])\nmeta={'uid':os.geteuid(),'gid':os.getegid(),'groups':os.getgroups(),\n 'supervisor_pid':os.getpid(),'deadline_seconds':spec['timeout'],\n 'timed_out':False,'reaped':False,'group_absent':False,'remaining_children':[]}\nprocess=None\nout=b''\nerr=b''\ncode=125\nfailure=None\nstarted=time.monotonic()\ndef children(pid):\n try:\n return [int(value) for value in pathlib.Path('/proc/%d/task/%d/children'%(pid,pid)).read_text().split()]\n except FileNotFoundError:\n return []\ndef descendants():\n found=set()\n pending=children(os.getpid())\n while pending:\n pid=pending.pop()\n if pid not in found:\n found.add(pid)\n pending.extend(children(pid))\n return found\ndef kill(pid):\n try:os.kill(pid,signal.SIGKILL)\n except ProcessLookupError:pass\ndef contain():\n end=time.monotonic()+4\n observed=set()\n while True:\n owned=descendants()\n observed.update(owned)\n if process is not None:\n try:os.killpg(process.pid,signal.SIGKILL)\n except ProcessLookupError:pass\n for pid in owned:kill(pid)\n if process is not None:process.poll()\n if process is None or process.returncode is not None:\n while True:\n try:\n pid,status=os.waitpid(-1,os.WNOHANG)\n if pid==0:break\n except ChildProcessError:break\n remaining=descendants()\n if not remaining and (process is None or process.returncode is not None):break\n if time.monotonic()>=end:break\n time.sleep(0.01)\n meta['observed_owned_pids']=sorted(observed)\n meta['remaining_children']=sorted(descendants())\n meta['reaped']=not meta['remaining_children'] and (process is None or process.returncode is not None)\n if process is None:\n meta['group_absent']=True\n else:\n try:os.killpg(process.pid,0)\n except ProcessLookupError:meta['group_absent']=True\ntry:\n libc=ctypes.CDLL(None,use_errno=True)\n if libc.prctl(36,1,0,0,0)!=0:\n raise OSError(ctypes.get_errno(),'PR_SET_CHILD_SUBREAPER')\n meta['subreaper']=True\n payload=sys.stdin.buffer.read()\n process=subprocess.Popen(spec['argv'],cwd=spec['cwd'],stdin=subprocess.PIPE,\n stdout=subprocess.PIPE,stderr=subprocess.PIPE,\n start_new_session=True)\n meta['child_pid']=process.pid\n try:\n out,err=process.communicate(payload,timeout=spec['timeout'])\n code=process.returncode\n except subprocess.TimeoutExpired:\n meta['timed_out']=True\n code=124\nexcept BaseException as error:\n failure=type(error).__name__+': '+str(error)\nfinally:\n try:\n contain()\n if process is not None:\n out,err=process.communicate(timeout=1)\n if not meta['reaped'] or not meta['group_absent']:\n raise RuntimeError('owned processes not fully contained')\n except BaseException as error:\n failure=type(error).__name__+': '+str(error)\n if failure is not None:code=125\n meta['failure']=failure\n meta['elapsed_seconds']=time.monotonic()-started\n print(json.dumps({'returncode':code,'stdout_b64':base64.b64encode(out).decode('ascii'),\n 'stderr_b64':base64.b64encode(err).decode('ascii'),'meta':meta}),flush=True)\n"
|
|
CLEANUP = 'import json,os,pathlib,shutil,sys\np=pathlib.Path(sys.argv[1])\nshutil.rmtree(p)\nprint(json.dumps(dict(path=str(p),uid=os.geteuid(),gid=os.getegid(),groups=os.getgroups(),exists=os.path.lexists(p))),flush=True)\nsys.exit(1 if os.path.lexists(p) else 0)\n'
|
|
ABSENCE = 'import json,os,sys\npath=sys.argv[1]\nexists=os.path.lexists(path)\nprint(json.dumps(dict(path=path,uid=os.geteuid(),gid=os.getegid(),groups=os.getgroups(),exists=exists)),flush=True)\nsys.exit(1 if exists else 0)\n'
|
|
|
|
def execute_child(argv, cwd, payload, timeout):
|
|
specification=json.dumps({'argv':[str(value) for value in argv],'cwd':str(cwd),'timeout':timeout})
|
|
child=subprocess.run([sys.executable,'-I','-c',EXECUTOR,specification],
|
|
input=payload,capture_output=True)
|
|
if child.returncode != 0:
|
|
raise RuntimeError('execution supervisor exit '+str(child.returncode)+': '+child.stderr.decode('utf-8','replace')[-500:])
|
|
result=json.loads(child.stdout.decode('utf-8'))
|
|
meta=result['meta']
|
|
if not meta['reaped'] or not meta['group_absent'] or meta['failure'] is not None:
|
|
raise RuntimeError('execution containment: '+str(meta))
|
|
result['stdout']=base64.b64decode(result.pop('stdout_b64'),validate=True)
|
|
result['stderr']=base64.b64decode(result.pop('stderr_b64'),validate=True).decode('utf-8','replace')
|
|
return result
|
|
|
|
def error_text(error):
|
|
return type(error).__name__+': '+str(error)
|
|
|
|
def main():
|
|
scratch=None
|
|
passed=False
|
|
stage='preparation'
|
|
log={'compile_rc':None,'candidate_rc':None,'controlled_rejection':None,
|
|
'first_observation_difference':None,'scratch_path':None,
|
|
'cleanup_rc':None,'cleanup_identity':None,'absence_rc':None,
|
|
'absence_identity':None,'execution':{}}
|
|
try:
|
|
candidate=Path(sys.argv[1])
|
|
source=(candidate/'src/borrowed_reader.c').read_bytes()
|
|
suite=cases()
|
|
want=[expected(case) for case in suite]
|
|
input_text=str(len(suite))+'\n'+''.join('%d %d %d %d %s\n'%(chunk,fault,strict,limit,data.hex() or '-')
|
|
for data,chunk,fault,strict,limit in suite)
|
|
input_bytes=input_text.encode('utf-8')
|
|
log['evidence_rows']=len(want)
|
|
scratch=Path(tempfile.mkdtemp(prefix='borrowed-reader-'))
|
|
log['scratch_path']=str(scratch)
|
|
project=scratch/'project'
|
|
project.mkdir(mode=0o777)
|
|
for name,contents in SUPPORT.items():
|
|
path=project/name
|
|
path.parent.mkdir(parents=True,exist_ok=True)
|
|
path.write_text(contents)
|
|
path.chmod(0o644)
|
|
(project/'src/borrowed_reader.c').write_bytes(source)
|
|
(project/'src/borrowed_reader.c').chmod(0o644)
|
|
(project/'observe.c').write_text(HARNESS)
|
|
(project/'observe.c').chmod(0o644)
|
|
for path in [project]+[p for p in project.rglob('*') if p.is_dir()]:
|
|
path.chmod(0o777)
|
|
compile_argv=['gcc','-std=c11','-O2','-Wall','-Wextra','-Wpedantic','-Iinclude',
|
|
'src/borrowed_reader.c','src/record.c','src/stream_port.c','observe.c','-o','observe']
|
|
candidate_argv=[str(project/'observe')]
|
|
cleanup_argv=[sys.executable,'-I','-c',CLEANUP,str(scratch)]
|
|
absence_argv=[sys.executable,'-I','-c',ABSENCE,str(scratch)]
|
|
stage='compile'
|
|
compiled=execute_child(compile_argv,project,b'',30)
|
|
log['execution']['compile']=compiled['meta']
|
|
log['compile_rc']=compiled['returncode']
|
|
if compiled['returncode']:
|
|
log['controlled_rejection']='compile_failed'
|
|
sys.stderr.write(compiled['stderr'][-1500:])
|
|
else:
|
|
stage='candidate'
|
|
completed=execute_child(candidate_argv,project,input_bytes,15)
|
|
log['execution']['candidate']=completed['meta']
|
|
log['candidate_rc']=completed['returncode']
|
|
if completed['returncode']:
|
|
log['controlled_rejection']='candidate_exit'
|
|
sys.stderr.write(completed['stderr'][-1500:])
|
|
else:
|
|
rows=completed['stdout'].decode('utf-8').splitlines()
|
|
if len(rows)!=len(want):
|
|
log['controlled_rejection']='observation_row_count'
|
|
log['first_observation_difference']={'row_count':len(rows),'expected_count':len(want)}
|
|
else:
|
|
passed=True
|
|
for index,(row,expected_row) in enumerate(zip(rows,want)):
|
|
try: actual=[int(part) for part in row.split()]
|
|
except ValueError: actual=None
|
|
if actual!=expected_row:
|
|
passed=False
|
|
log['controlled_rejection']='behavior_difference'
|
|
log['first_observation_difference']={'row':index,'actual':actual,'expected':expected_row}
|
|
break
|
|
except BaseException as error:
|
|
passed=False
|
|
log['controlled_rejection']=stage+'_execution_error'
|
|
log['execution_error']=error_text(error)
|
|
finally:
|
|
if scratch is not None:
|
|
# These argv use only trusted constants and the pre-child exact path;
|
|
# preparation failures also reach this same-identity cleanup.
|
|
cleanup_argv=[sys.executable,'-I','-c',CLEANUP,str(scratch)]
|
|
absence_argv=[sys.executable,'-I','-c',ABSENCE,str(scratch)]
|
|
try:
|
|
cleaned=execute_child(cleanup_argv,'/',b'',10)
|
|
log['execution']['cleanup']=cleaned['meta']
|
|
log['cleanup_rc']=cleaned['returncode']
|
|
if cleaned['returncode'] != 0:
|
|
passed=False
|
|
log['cleanup_error']='cleanup nonzero: '+str(cleaned['returncode'])+' '+cleaned['stderr'][-500:]
|
|
else:
|
|
identity=json.loads(cleaned['stdout'].decode('utf-8'))
|
|
log['cleanup_identity']=identity
|
|
if identity['path']!=str(scratch) or identity['exists']:
|
|
passed=False
|
|
log['cleanup_error']='invalid cleanup receipt'
|
|
except BaseException as error:
|
|
passed=False
|
|
log['cleanup_error']=error_text(error)
|
|
try:
|
|
absent=execute_child(absence_argv,'/',b'',5)
|
|
log['execution']['absence']=absent['meta']
|
|
log['absence_rc']=absent['returncode']
|
|
if absent['returncode'] != 0:
|
|
passed=False
|
|
log['absence_error']='absence nonzero: '+str(absent['returncode'])
|
|
else:
|
|
identity=json.loads(absent['stdout'].decode('utf-8'))
|
|
log['absence_identity']=identity
|
|
if identity['path']!=str(scratch) or identity['exists']:
|
|
passed=False
|
|
log['absence_error']='invalid absence receipt'
|
|
except BaseException as error:
|
|
passed=False
|
|
log['absence_error']=error_text(error)
|
|
if log['cleanup_rc']!=0 or log['absence_rc']!=0:passed=False
|
|
print(json.dumps(log,sort_keys=True),file=sys.stderr)
|
|
print('PASS' if passed else 'FAIL: '+str(log['controlled_rejection'] or 'cleanup_failed'))
|
|
return 0 if passed else 1
|
|
|
|
if __name__=='__main__':
|
|
raise SystemExit(main())
|