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>
231 lines
31 KiB
Python
231 lines
31 KiB
Python
"""Independent mathematical oracle. All support and input frozen before children."""
|
|
import base64
|
|
import json
|
|
from pathlib import Path
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
SUPPORT = {'include/flag_audit.h': '#ifndef FLAG_AUDIT_H\n#define FLAG_AUDIT_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#define CFG_MAX_FIELDS 4\n#define CFG_LINE_CAP 1024\n#define CFG_TOKEN_CAP 10\n#define CFG_NAME_CAP 16\n\ntypedef enum {\n CFG_OK = 0,\n CFG_WIDTH,\n CFG_FIELD,\n CFG_RANGE,\n CFG_LAYOUT,\n CFG_INPUT\n} cfg_status;\n\ntypedef struct {\n uint64_t value;\n uint64_t mask;\n unsigned ones;\n unsigned zeros;\n int low;\n int high;\n unsigned all;\n unsigned none;\n unsigned parity;\n unsigned runs;\n} cfg_word;\n\ntypedef struct {\n uint64_t after;\n uint64_t changed;\n uint64_t gained;\n uint64_t lost;\n} cfg_patch;\n\ntypedef struct {\n uint64_t raw;\n int64_t value;\n} cfg_field;\n\ntypedef struct {\n const char *name;\n unsigned offset;\n unsigned bits;\n unsigned is_signed;\n} cfg_field_spec;\n\ntypedef struct {\n const char *name;\n cfg_field field;\n} cfg_named_field;\n\ntypedef struct {\n cfg_named_field entries[CFG_MAX_FIELDS];\n size_t count;\n} cfg_layout_result;\n\ntypedef struct {\n char *items[CFG_TOKEN_CAP];\n size_t count;\n} cfg_tokens;\n\nint cfg_width_valid(unsigned width);\nuint64_t cfg_mask(unsigned width);\ncfg_status cfg_describe(uint64_t raw, unsigned width, cfg_word *out);\ncfg_status cfg_apply(uint64_t old, uint64_t set, uint64_t clear,\n uint64_t toggle, unsigned width, cfg_patch *out);\ncfg_status cfg_extract(uint64_t raw, unsigned width, unsigned offset,\n unsigned bits, unsigned is_signed, cfg_field *out);\ncfg_status cfg_insert(uint64_t raw, unsigned width, unsigned offset,\n unsigned bits, unsigned is_signed, int64_t value,\n uint64_t *out);\ncfg_status cfg_layout_decode(const char *name, uint64_t raw,\n unsigned width, cfg_layout_result *out);\nint cfg_split(char *line, cfg_tokens *out);\nint cfg_parse_u64(const char *text, uint64_t *out);\nint cfg_parse_unsigned(const char *text, unsigned *out);\nint cfg_parse_i64(const char *text, int64_t *out);\nconst char *cfg_status_name(cfg_status status);\nvoid cfg_print_error(FILE *stream, uint64_t id, cfg_status status);\nvoid cfg_print_word(FILE *stream, uint64_t id, const cfg_word *word);\nvoid cfg_print_patch(FILE *stream, uint64_t id, const cfg_patch *patch);\nvoid cfg_print_field(FILE *stream, uint64_t id, const cfg_field *field);\nvoid cfg_print_insert(FILE *stream, uint64_t id, uint64_t value);\nvoid cfg_print_layout(FILE *stream, uint64_t id, const cfg_layout_result *result);\nint cfg_dispatch(const cfg_tokens *tokens, FILE *out);\n#endif\n', 'src/dispatch.c': '#include "flag_audit.h"\n#include <string.h>\n\nstatic cfg_status word_command(const cfg_tokens *tokens, uint64_t id, FILE *out)\n{\n unsigned width;\n uint64_t raw;\n cfg_word result;\n cfg_status status;\n if (tokens->count != 4 || !cfg_parse_unsigned(tokens->items[2], &width) ||\n !cfg_parse_u64(tokens->items[3], &raw)) {\n return CFG_INPUT;\n }\n status = cfg_describe(raw, width, &result);\n if (status == CFG_OK) {\n cfg_print_word(out, id, &result);\n }\n return status;\n}\n\nstatic cfg_status patch_command(const cfg_tokens *tokens, uint64_t id, FILE *out)\n{\n unsigned width;\n uint64_t old;\n uint64_t set;\n uint64_t clear;\n uint64_t toggle;\n cfg_patch result;\n cfg_status status;\n if (tokens->count != 7 || !cfg_parse_unsigned(tokens->items[2], &width) ||\n !cfg_parse_u64(tokens->items[3], &old) ||\n !cfg_parse_u64(tokens->items[4], &set) ||\n !cfg_parse_u64(tokens->items[5], &clear) ||\n !cfg_parse_u64(tokens->items[6], &toggle)) {\n return CFG_INPUT;\n }\n status = cfg_apply(old, set, clear, toggle, width, &result);\n if (status == CFG_OK) {\n cfg_print_patch(out, id, &result);\n }\n return status;\n}\n\nstatic cfg_status field_arguments(const cfg_tokens *tokens, unsigned *width,\n uint64_t *raw, unsigned *offset,\n unsigned *bits, unsigned *is_signed)\n{\n if (!cfg_parse_unsigned(tokens->items[2], width) ||\n !cfg_parse_u64(tokens->items[3], raw) ||\n !cfg_parse_unsigned(tokens->items[4], offset) ||\n !cfg_parse_unsigned(tokens->items[5], bits) ||\n !cfg_parse_unsigned(tokens->items[6], is_signed)) {\n return CFG_INPUT;\n }\n return CFG_OK;\n}\n\nstatic cfg_status field_command(const cfg_tokens *tokens, uint64_t id, FILE *out)\n{\n unsigned width;\n unsigned offset;\n unsigned bits;\n unsigned is_signed;\n uint64_t raw;\n cfg_field result;\n cfg_status status;\n if (tokens->count != 7) {\n return CFG_INPUT;\n }\n status = field_arguments(tokens, &width, &raw, &offset, &bits, &is_signed);\n if (status == CFG_OK) {\n status = cfg_extract(raw, width, offset, bits, is_signed, &result);\n }\n if (status == CFG_OK) {\n cfg_print_field(out, id, &result);\n }\n return status;\n}\n\nstatic cfg_status insert_command(const cfg_tokens *tokens, uint64_t id, FILE *out)\n{\n unsigned width;\n unsigned offset;\n unsigned bits;\n unsigned is_signed;\n uint64_t raw;\n uint64_t result;\n int64_t value;\n cfg_status status;\n if (tokens->count != 8 || !cfg_parse_i64(tokens->items[7], &value)) {\n return CFG_INPUT;\n }\n status = field_arguments(tokens, &width, &raw, &offset, &bits, &is_signed);\n if (status == CFG_OK) {\n status = cfg_insert(raw, width, offset, bits, is_signed, value, &result);\n }\n if (status == CFG_OK) {\n cfg_print_insert(out, id, result);\n }\n return status;\n}\n\nstatic cfg_status layout_command(const cfg_tokens *tokens, uint64_t id, FILE *out)\n{\n unsigned width;\n uint64_t raw;\n cfg_layout_result result;\n cfg_status status;\n if (tokens->count != 5 || !cfg_parse_unsigned(tokens->items[2], &width) ||\n !cfg_parse_u64(tokens->items[3], &raw)) {\n return CFG_INPUT;\n }\n status = cfg_layout_decode(tokens->items[4], raw, width, &result);\n if (status == CFG_OK) {\n cfg_print_layout(out, id, &result);\n }\n return status;\n}\n\nint cfg_dispatch(const cfg_tokens *tokens, FILE *out)\n{\n cfg_status status;\n uint64_t id = 0;\n const char *command;\n if (tokens->count == 0) {\n return 1;\n }\n if (tokens->count < 2 || !cfg_parse_u64(tokens->items[1], &id)) {\n cfg_print_error(out, 0, CFG_INPUT);\n return 1;\n }\n command = tokens->items[0];\n if (strcmp(command, "WORD") == 0) {\n status = word_command(tokens, id, out);\n } else if (strcmp(command, "PATCH") == 0) {\n status = patch_command(tokens, id, out);\n } else if (strcmp(command, "FIELD") == 0) {\n status = field_command(tokens, id, out);\n } else if (strcmp(command, "INSERT") == 0) {\n status = insert_command(tokens, id, out);\n } else if (strcmp(command, "LAYOUT") == 0) {\n status = layout_command(tokens, id, out);\n } else {\n status = CFG_INPUT;\n }\n if (status != CFG_OK) {\n cfg_print_error(out, id, status);\n }\n return 1;\n}\n', 'src/layout.c': '#include "flag_audit.h"\n#include <string.h>\n\nstatic const cfg_field_spec drive_fields[] = {\n {"enable", 0, 1, 0},\n {"mode", 1, 3, 0},\n {"budget", 4, 12, 0},\n {"channel", 16, 8, 0}\n};\nstatic const cfg_field_spec trim_fields[] = {\n {"trim", 0, 6, 1},\n {"slope", 6, 10, 1},\n {"code", 16, 8, 0}\n};\n\nstatic cfg_status fixed_layout(const char *name, unsigned width,\n const cfg_field_spec **fields, size_t *count)\n{\n if (strcmp(name, "drive") == 0) {\n if (width < 24) {\n return CFG_LAYOUT;\n }\n *fields = drive_fields;\n *count = sizeof(drive_fields) / sizeof(drive_fields[0]);\n return CFG_OK;\n }\n if (strcmp(name, "trim") == 0) {\n if (width < 24) {\n return CFG_LAYOUT;\n }\n *fields = trim_fields;\n *count = sizeof(trim_fields) / sizeof(trim_fields[0]);\n return CFG_OK;\n }\n return CFG_LAYOUT;\n}\n\nstatic cfg_status edge_layout(unsigned width, cfg_field_spec *fields,\n size_t *count)\n{\n if (width < 17) {\n return CFG_LAYOUT;\n }\n fields[0].name = "top";\n fields[0].offset = width - 1;\n fields[0].bits = 1;\n fields[0].is_signed = 0;\n fields[1].name = "delta";\n fields[1].offset = width - 9;\n fields[1].bits = 8;\n fields[1].is_signed = 1;\n fields[2].name = "tag";\n fields[2].offset = 0;\n fields[2].bits = 8;\n fields[2].is_signed = 0;\n *count = 3;\n return CFG_OK;\n}\n\ncfg_status cfg_layout_decode(const char *name, uint64_t raw,\n unsigned width, cfg_layout_result *out)\n{\n cfg_field_spec dynamic[CFG_MAX_FIELDS];\n const cfg_field_spec *fields = NULL;\n cfg_layout_result result;\n cfg_status status;\n size_t count = 0;\n size_t index;\n if (!cfg_width_valid(width)) {\n return CFG_WIDTH;\n }\n if (strcmp(name, "edge") == 0) {\n status = edge_layout(width, dynamic, &count);\n fields = dynamic;\n } else {\n status = fixed_layout(name, width, &fields, &count);\n }\n if (status != CFG_OK) {\n return status;\n }\n result.count = count;\n for (index = 0; index < count; ++index) {\n result.entries[index].name = fields[index].name;\n status = cfg_extract(raw, width, fields[index].offset,\n fields[index].bits, fields[index].is_signed,\n &result.entries[index].field);\n if (status != CFG_OK) {\n return status;\n }\n }\n *out = result;\n return CFG_OK;\n}\n', 'src/main.c': '#include "flag_audit.h"\n#include <stdio.h>\n#include <string.h>\n\nstatic int consume_long_line(FILE *stream)\n{\n int value;\n do {\n value = fgetc(stream);\n } while (value != \'\\n\' && value != EOF);\n return !ferror(stream);\n}\n\nint main(void)\n{\n char line[CFG_LINE_CAP];\n cfg_tokens tokens;\n while (fgets(line, sizeof(line), stdin) != NULL) {\n size_t length = strlen(line);\n if (length == sizeof(line) - 1 && line[length - 1] != \'\\n\') {\n if (!consume_long_line(stdin)) {\n return 2;\n }\n cfg_print_error(stdout, 0, CFG_INPUT);\n continue;\n }\n if (!cfg_split(line, &tokens)) {\n cfg_print_error(stdout, 0, CFG_INPUT);\n continue;\n }\n if (!cfg_dispatch(&tokens, stdout)) {\n return 2;\n }\n }\n if (ferror(stdin) || fflush(stdout) != 0) {\n return 2;\n }\n return 0;\n}\n', 'src/parse.c': '#include "flag_audit.h"\n#include <ctype.h>\n#include <errno.h>\n#include <limits.h>\n#include <stdlib.h>\n#include <string.h>\n\nint cfg_split(char *line, cfg_tokens *out)\n{\n char *cursor = line;\n out->count = 0;\n while (*cursor != \'\\0\') {\n while (isspace((unsigned char)*cursor)) {\n ++cursor;\n }\n if (*cursor == \'\\0\' || (*cursor == \'#\' && out->count == 0)) {\n break;\n }\n if (out->count == CFG_TOKEN_CAP) {\n return 0;\n }\n out->items[out->count++] = cursor;\n while (*cursor != \'\\0\' && !isspace((unsigned char)*cursor)) {\n ++cursor;\n }\n if (*cursor != \'\\0\') {\n *cursor++ = \'\\0\';\n }\n }\n return 1;\n}\n\nstatic int numeric_text(const char *text, int allow_negative)\n{\n const unsigned char *cursor = (const unsigned char *)text;\n if (*cursor == \'-\' && allow_negative) {\n ++cursor;\n }\n if (*cursor == \'\\0\' || *cursor == \'+\' || *cursor == \'-\') {\n return 0;\n }\n if (cursor[0] == \'0\' && (cursor[1] == \'x\' || cursor[1] == \'X\')) {\n cursor += 2;\n if (*cursor == \'\\0\') {\n return 0;\n }\n while (*cursor != \'\\0\') {\n if (!isxdigit(*cursor++)) {\n return 0;\n }\n }\n } else {\n while (*cursor != \'\\0\') {\n if (!isdigit(*cursor++)) {\n return 0;\n }\n }\n }\n return 1;\n}\n\nstatic int numeric_base(const char *text)\n{\n if (*text == \'-\') {\n ++text;\n }\n return text[0] == \'0\' && (text[1] == \'x\' || text[1] == \'X\') ? 16 : 10;\n}\n\nint cfg_parse_u64(const char *text, uint64_t *out)\n{\n char *end;\n unsigned long long value;\n if (!numeric_text(text, 0)) {\n return 0;\n }\n errno = 0;\n value = strtoull(text, &end, numeric_base(text));\n if (errno == ERANGE || *end != \'\\0\' || value > UINT64_MAX) {\n return 0;\n }\n *out = (uint64_t)value;\n return 1;\n}\n\nint cfg_parse_unsigned(const char *text, unsigned *out)\n{\n uint64_t value;\n if (!cfg_parse_u64(text, &value) || value > UINT_MAX) {\n return 0;\n }\n *out = (unsigned)value;\n return 1;\n}\n\nint cfg_parse_i64(const char *text, int64_t *out)\n{\n long long value;\n char *end;\n if (!numeric_text(text, 1)) {\n return 0;\n }\n errno = 0;\n value = strtoll(text, &end, numeric_base(text));\n if (errno == ERANGE || *end != \'\\0\' || value < INT64_MIN || value > INT64_MAX) {\n return 0;\n }\n *out = (int64_t)value;\n return 1;\n}\n', 'src/report.c': '#include "flag_audit.h"\n#include <inttypes.h>\n\nconst char *cfg_status_name(cfg_status status)\n{\n switch (status) {\n case CFG_OK: return "OK";\n case CFG_WIDTH: return "WIDTH";\n case CFG_FIELD: return "FIELD";\n case CFG_RANGE: return "RANGE";\n case CFG_LAYOUT: return "LAYOUT";\n case CFG_INPUT: return "INPUT";\n }\n return "INPUT";\n}\n\nvoid cfg_print_error(FILE *stream, uint64_t id, cfg_status status)\n{\n fprintf(stream, "E %" PRIu64 " %s\\n", id, cfg_status_name(status));\n}\n\nvoid cfg_print_word(FILE *stream, uint64_t id, const cfg_word *word)\n{\n fprintf(stream, "W %" PRIu64 " %" PRIu64 " %" PRIu64\n " %u %u %d %d %u %u %u %u\\n", id, word->value,\n word->mask, word->ones, word->zeros, word->low, word->high,\n word->all, word->none, word->parity, word->runs);\n}\n\nvoid cfg_print_patch(FILE *stream, uint64_t id, const cfg_patch *patch)\n{\n fprintf(stream, "P %" PRIu64 " %" PRIu64 " %" PRIu64\n " %" PRIu64 " %" PRIu64 "\\n", id, patch->after,\n patch->changed, patch->gained, patch->lost);\n}\n\nvoid cfg_print_field(FILE *stream, uint64_t id, const cfg_field *field)\n{\n fprintf(stream, "F %" PRIu64 " %" PRIu64 " %" PRId64 "\\n",\n id, field->raw, field->value);\n}\n\nvoid cfg_print_insert(FILE *stream, uint64_t id, uint64_t value)\n{\n fprintf(stream, "I %" PRIu64 " %" PRIu64 "\\n", id, value);\n}\n\nvoid cfg_print_layout(FILE *stream, uint64_t id, const cfg_layout_result *result)\n{\n size_t index;\n fprintf(stream, "L %" PRIu64 " %zu", id, result->count);\n for (index = 0; index < result->count; ++index) {\n fprintf(stream, " %s=%" PRId64, result->entries[index].name,\n result->entries[index].field.value);\n }\n fputc(\'\\n\', stream);\n}\n'}
|
|
CANDIDATES = ('src/word.c', 'src/fields.c')
|
|
|
|
def cases():
|
|
inputs = []
|
|
expected = []
|
|
labels = []
|
|
serial = 0
|
|
def add(command, tail, label):
|
|
nonlocal serial
|
|
serial += 1
|
|
parts = command.split()
|
|
inputs.append(parts[0] + ' ' + str(serial) + ' ' + ' '.join(parts[1:]))
|
|
expected.append(tail[0] + ' ' + str(serial) + (' ' + tail[1] if tail[1] else ''))
|
|
labels.append(label)
|
|
def m(w):
|
|
return 2 ** w - 1
|
|
def field(raw, offset, bits, signed):
|
|
u = (raw // (2 ** offset)) % (2 ** bits)
|
|
return u, u - 2 ** bits if signed and u >= 2 ** (bits - 1) else u
|
|
def word(w, raw):
|
|
x = raw % (2 ** w)
|
|
text = format(x, '0%db' % w)
|
|
ones = text.count('1')
|
|
low = (x & -x).bit_length() - 1 if x else -1
|
|
high = x.bit_length() - 1
|
|
runs = text.count('01') + int(text.startswith('1'))
|
|
values = [x, m(w), ones, w-ones, low, high, int(x == m(w)), int(x == 0), ones % 2, runs]
|
|
add('WORD %d %d' % (w, raw), ('W', ' '.join(map(str, values))), 'word-statistics')
|
|
def extract(w, raw, offset, bits, signed):
|
|
u,v = field(raw, offset, bits, signed)
|
|
add('FIELD %d %d %d %d %d' % (w,raw,offset,bits,signed), ('F', '%d %d' % (u,v)), 'field-value')
|
|
def insert(w,raw,offset,bits,signed,value):
|
|
low = -(2 ** (bits-1)) if signed else 0
|
|
high = 2 ** (bits-1)-1 if signed else m(bits)
|
|
args = 'INSERT %d %d %d %d %d %d' % (w,raw,offset,bits,signed,value)
|
|
if not low <= value <= high:
|
|
add(args, ('E','RANGE'), 'insert-range')
|
|
return
|
|
before = raw % (2 ** w)
|
|
old = (before // (2 ** offset)) % (2 ** bits)
|
|
after = before + ((value % (2 ** bits)) - old) * (2 ** offset)
|
|
add(args, ('I',str(after)), 'insert-preserves-neighbors')
|
|
def layout(w,raw,name):
|
|
if name == 'drive':
|
|
fields = [('enable',0,1,0),('mode',1,3,0),('budget',4,12,0),('channel',16,8,0)]
|
|
elif name == 'trim':
|
|
fields = [('trim',0,6,1),('slope',6,10,1),('code',16,8,0)]
|
|
else:
|
|
fields = [('top',w-1,1,0),('delta',w-9,8,1),('tag',0,8,0)]
|
|
values = ['%s=%d' % (name,field(raw,offset,bits,signed)[1]) for name,offset,bits,signed in fields]
|
|
add('LAYOUT %d %d %s' % (w,raw,name), ('L',str(len(values))+' '+' '.join(values)), 'layout-'+name)
|
|
rng = random.Random(0xF1A62026)
|
|
# Default illustration first; negative narrow fields remain an independent scenario.
|
|
word(32, 0xffffffff)
|
|
layout(32, 0x00ff123f, 'drive')
|
|
for w in range(1,65):
|
|
for raw in (0,m(w),1,2 ** (w-1),2 ** 64-1,rng.getrandbits(64)):
|
|
word(w,raw)
|
|
for unused in range(3):
|
|
old, set_, clear, toggle = [rng.getrandbits(64) for _ in range(4)]
|
|
before = old % (2 ** w)
|
|
after = (((old | set_) & (m(w) ^ (clear & m(w)))) ^ toggle) & m(w)
|
|
values = [after, before ^ after, (m(w) ^ before) & after, before & (m(w) ^ after)]
|
|
add('PATCH %d %d %d %d %d' % (w,old,set_,clear,toggle), ('P',' '.join(map(str,values))), 'patch-order')
|
|
for bits in range(1,33):
|
|
for w in sorted(set([bits,max(32,bits),64])):
|
|
for offset in sorted(set([0,w-bits,(w-bits)//2])):
|
|
for raw_field in sorted(set([0,1,m(bits),2 ** (bits-1),2 ** (bits-1)-1])):
|
|
raw = ((rng.getrandbits(64) & ~(m(bits) << offset)) | raw_field << offset)
|
|
for signed in (0,1):
|
|
extract(w,raw,offset,bits,signed)
|
|
for signed in (0,1):
|
|
low = -(2 ** (bits-1)) if signed else 0
|
|
high = 2 ** (bits-1)-1 if signed else m(bits)
|
|
for value in sorted(set([low-1,low,0,high,high+1])):
|
|
insert(w,rng.getrandbits(64),offset,bits,signed,value)
|
|
for w in (24,25,31,32,33,48,63,64):
|
|
for name in ('drive','trim','edge'):
|
|
for raw in (0, m(w), 0x3f, 0xff800000000000ff, rng.getrandbits(64)):
|
|
layout(w,raw,name)
|
|
for w in (17,18,23):
|
|
for raw in (0,m(w),rng.getrandbits(64)):
|
|
layout(w,raw,'edge')
|
|
rejection = [
|
|
('WORD 0 0','WIDTH'),('WORD 65 0','WIDTH'),
|
|
('PATCH 0 0 0 0 0','WIDTH'),('PATCH 65 0 0 0 0','WIDTH'),
|
|
('FIELD 0 0 0 0 2','WIDTH'),('FIELD 65 0 0 1 0','WIDTH'),
|
|
('FIELD 32 0 0 0 0','FIELD'),('FIELD 64 0 0 33 0','FIELD'),
|
|
('FIELD 32 0 32 1 0','FIELD'),('FIELD 32 0 31 2 0','FIELD'),
|
|
('FIELD 32 0 0 1 2','FIELD'),('INSERT 0 0 0 0 2 9','WIDTH'),
|
|
('INSERT 32 0 0 0 0 -1','FIELD'),('INSERT 32 0 31 2 0 -1','FIELD'),
|
|
('LAYOUT 0 0 trim','WIDTH'),('LAYOUT 65 0 edge','WIDTH'),
|
|
('LAYOUT 23 0 drive','LAYOUT'),('LAYOUT 23 0 trim','LAYOUT'),
|
|
('LAYOUT 16 0 edge','LAYOUT'),('LAYOUT 32 0 missing','LAYOUT')]
|
|
for command,code in rejection:
|
|
add(command,('E',code),'controlled-rejection')
|
|
# Rejections must not poison the next independent record.
|
|
word(64, 0x8000000000000000)
|
|
extract(64, 0x8000000000000000, 32, 32, 1)
|
|
# Public grammar supports decimal leading zeroes, mixed whitespace and hex.
|
|
add('WORD 08 0Xff',('W','255 255 8 0 0 7 1 0 0 1'),'numeric-grammar')
|
|
return '\n'.join(inputs)+'\n', expected, labels
|
|
|
|
# Pure same-UID executor; no inputs, assertions or behavior model are embedded here.
|
|
EXECUTOR = "import base64\nimport ctypes\nimport json\nimport os\nimport signal\nimport subprocess\nimport sys\nimport time\n\ncommand = json.loads(sys.argv[1])\nworking = sys.argv[2]\ndeadline = float(sys.argv[3])\npayload = sys.stdin.buffer.read()\nprocess = None\nstdout = b''\nstderr = b''\nerrors = []\ntimed_out = False\nreaped = False\ngroup_absent = False\nleader_rc = None\nstarted = time.monotonic()\n\ndef failure(error):\n return type(error).__name__ + ': ' + str(error)\n\ndef children(pid):\n try:\n with open('/proc/%d/task/%d/children' % (pid, pid)) as stream:\n return [int(part) for part in stream.read().split()]\n except FileNotFoundError:\n return []\n\ndef descendants():\n pending = children(os.getpid())\n found = []\n while pending:\n pid = pending.pop()\n if pid not in found:\n found.append(pid)\n pending.extend(children(pid))\n return found\n\ndef kill_owned():\n if process is not None:\n try:\n os.killpg(process.pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n except OSError as error:\n errors.append(failure(error))\n for pid in reversed(descendants()):\n try:\n os.kill(pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n except OSError as error:\n errors.append(failure(error))\n\ndef reap_owned():\n until = time.monotonic() + 3\n while time.monotonic() < until:\n kill_owned()\n no_children = False\n while True:\n try:\n pid, status = os.waitpid(-1, os.WNOHANG)\n except ChildProcessError:\n no_children = True\n break\n if pid == 0:\n break\n if no_children:\n return True\n time.sleep(.01)\n return False\n\ntry:\n libc = ctypes.CDLL(None, use_errno=True)\n if libc.prctl(36, 1, 0, 0, 0) != 0:\n number = ctypes.get_errno()\n raise OSError(number, 'PR_SET_CHILD_SUBREAPER failed')\n process = subprocess.Popen(command, cwd=working, stdin=subprocess.PIPE,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE,\n start_new_session=True)\n try:\n stdout, stderr = process.communicate(payload, timeout=deadline)\n leader_rc = process.returncode\n except subprocess.TimeoutExpired:\n timed_out = True\n kill_owned()\n stdout, stderr = process.communicate(timeout=3)\n leader_rc = process.returncode\nexcept Exception as error:\n errors.append(failure(error))\nfinally:\n if process is not None:\n if process.poll() is None:\n kill_owned()\n try:\n process.wait(timeout=3)\n except Exception as error:\n errors.append(failure(error))\n reaped = reap_owned()\n try:\n os.killpg(process.pid, 0)\n except ProcessLookupError:\n group_absent = True\n except OSError as error:\n errors.append(failure(error))\n else:\n reaped = True\n group_absent = True\n remaining = descendants()\n if remaining or not reaped or not group_absent:\n errors.append('ContainmentError: owned children or process group remain')\ncode = 125 if errors else 124 if timed_out else leader_rc\nif code is None:\n code = 125\n errors.append('ExecutionError: no leader return code')\nmeta = {'uid': os.getuid(), 'gid': os.getgid(), 'deadline_seconds': deadline,\n 'leader_rc': leader_rc, 'returncode': code, 'timed_out': timed_out,\n 'reaped': reaped, 'group_absent': group_absent,\n 'remaining_children': remaining, 'errors': errors,\n 'elapsed_seconds': time.monotonic() - started}\nprint(json.dumps({'returncode': code, 'stdout': base64.b64encode(stdout).decode('ascii'),\n 'stderr': base64.b64encode(stderr).decode('ascii'), 'meta': meta}))\n"
|
|
|
|
EXECUTIONS = []
|
|
|
|
def execute(argv, cwd, timeout, input_bytes=b'', phase='child'):
|
|
transported = subprocess.run(
|
|
[sys.executable, '-I', '-c', EXECUTOR, json.dumps([str(x) for x in argv]),
|
|
str(cwd), str(timeout)], input=input_bytes, capture_output=True)
|
|
if transported.returncode != 0:
|
|
raise RuntimeError('executor_transport_rc=%d stderr=%s' %
|
|
(transported.returncode, transported.stderr.decode('utf-8', 'replace')[-1000:]))
|
|
envelope = json.loads(transported.stdout.decode('utf-8', 'strict'))
|
|
meta = envelope['meta']
|
|
code = envelope['returncode']
|
|
if type(code) is not int or code != meta['returncode']:
|
|
raise RuntimeError('executor_invalid_returncode')
|
|
meta['phase'] = phase
|
|
EXECUTIONS.append({k:meta[k] for k in ('phase','uid','gid','deadline_seconds','leader_rc','returncode','timed_out','reaped','group_absent','remaining_children')})
|
|
print('FLAG_EXEC_META ' + json.dumps(meta, sort_keys=True), file=sys.stderr)
|
|
if not meta['reaped'] or not meta['group_absent'] or meta['remaining_children']:
|
|
code = 125
|
|
output = base64.b64decode(envelope['stdout'], validate=True)
|
|
error = base64.b64decode(envelope['stderr'], validate=True)
|
|
return subprocess.CompletedProcess(argv, code, output, error)
|
|
|
|
def main():
|
|
scratch = None
|
|
result = 'FAIL checker-incomplete'
|
|
phase = 'prepare'
|
|
cleanup_rc = None
|
|
cleanup_error = None
|
|
absence_rc = None
|
|
absence_error = None
|
|
try:
|
|
candidate = Path(sys.argv[1])
|
|
input_text, expected, labels = cases()
|
|
input_bytes = input_text.encode('utf-8')
|
|
frozen = {name:(candidate/name).read_bytes() for name in CANDIDATES}
|
|
sources = ['src/main.c','src/dispatch.c','src/parse.c','src/report.c','src/layout.c',*CANDIDATES]
|
|
scratch = Path(tempfile.mkdtemp(prefix='flag-width-'))
|
|
print('scratch_created=%s' % scratch, file=sys.stderr)
|
|
for name,text in SUPPORT.items():
|
|
path = scratch/name
|
|
path.parent.mkdir(parents=True,exist_ok=True)
|
|
path.write_text(text)
|
|
for name,content in frozen.items():
|
|
path = scratch/name
|
|
path.parent.mkdir(parents=True,exist_ok=True)
|
|
path.write_bytes(content)
|
|
# Only these controller-owned subdirectories are chmodded before any child.
|
|
# The isolation hook already owns the scratch root as UID65534.
|
|
for name in ('src','include'):
|
|
(scratch/name).chmod(0o777)
|
|
phase = 'compile'
|
|
compiled = execute(['gcc','-std=c11','-O2','-Wall','-Wextra','-Wpedantic','-Iinclude',*sources,'-o','audit'], scratch, 25, phase='compile')
|
|
print('compile_rc=%d' % compiled.returncode,file=sys.stderr)
|
|
if compiled.returncode != 0:
|
|
print(compiled.stderr.decode('utf-8', 'replace')[-1500:],file=sys.stderr)
|
|
result = 'FAIL compile_rc=%d' % compiled.returncode
|
|
else:
|
|
phase = 'candidate'
|
|
observed = execute([str(scratch/'audit')], scratch, 12, input_bytes, phase='candidate')
|
|
print('candidate_rc=%d' % observed.returncode,file=sys.stderr)
|
|
if observed.stderr:
|
|
print('candidate_stderr=' + observed.stderr.decode('utf-8', 'replace')[-1000:],file=sys.stderr)
|
|
if observed.returncode != 0:
|
|
result = 'FAIL candidate_rc=%d' % observed.returncode
|
|
else:
|
|
actual = [line.split() for line in observed.stdout.decode('utf-8', 'strict').splitlines()]
|
|
wanted = [line.split() for line in expected]
|
|
difference = next((index for index,(a,b) in enumerate(zip(actual,wanted)) if a != b),None)
|
|
if difference is not None:
|
|
detail = {'row':difference+1,'case':labels[difference],'observed':actual[difference],'expected':wanted[difference]}
|
|
print('first_raw_difference='+json.dumps(detail,sort_keys=True),file=sys.stderr)
|
|
result = 'FAIL normal_exit_first_difference row=%d case=%s' % (difference+1,labels[difference])
|
|
elif len(actual) != len(wanted):
|
|
print('raw_row_count observed=%d expected=%d' % (len(actual),len(wanted)),file=sys.stderr)
|
|
result = 'FAIL normal_exit_row_count observed=%d expected=%d' % (len(actual),len(wanted))
|
|
else:
|
|
print('observations=%d all_matched' % len(wanted),file=sys.stderr)
|
|
result = 'PASS'
|
|
except Exception as error:
|
|
result = 'FAIL %s_exception=%s: %s' % (phase,type(error).__name__,str(error))
|
|
print(result,file=sys.stderr)
|
|
finally:
|
|
if scratch is not None:
|
|
# Both cleanup and independent absence run as candidate-identity argv
|
|
# children. Parent does not access scratch after the first child.
|
|
cleanup = 'import os,shutil,sys; p=sys.argv[1]; shutil.rmtree(p) if os.path.lexists(p) else None'
|
|
absence = 'import os,sys; exists=os.path.lexists(sys.argv[1]); print("scratch_absent="+str(not exists)); sys.exit(1 if exists else 0)'
|
|
try:
|
|
removed = execute([sys.executable,'-I','-c',cleanup,str(scratch)], '/', 12, phase='cleanup')
|
|
cleanup_rc = removed.returncode
|
|
print('candidate_identity_cleanup_rc=%d scratch=%s' % (cleanup_rc,scratch),file=sys.stderr)
|
|
if cleanup_rc != 0:
|
|
print(removed.stderr.decode('utf-8', 'replace')[-1000:],file=sys.stderr)
|
|
except Exception as error:
|
|
cleanup_error = type(error).__name__ + ': ' + str(error)
|
|
print('candidate_identity_cleanup_exception=' + cleanup_error, file=sys.stderr)
|
|
try:
|
|
absent = execute([sys.executable,'-I','-c',absence,str(scratch)], '/', 5, phase='absence')
|
|
absence_rc = absent.returncode
|
|
print('candidate_identity_absence_rc=%d scratch=%s' % (absence_rc,scratch),file=sys.stderr)
|
|
except Exception as error:
|
|
absence_error = type(error).__name__ + ': ' + str(error)
|
|
print('candidate_identity_absence_exception=' + absence_error, file=sys.stderr)
|
|
if cleanup_rc != 0 or cleanup_error is not None:
|
|
result = 'FAIL candidate_identity_cleanup_unconfirmed previous=' + result
|
|
if absence_rc != 0 or absence_error is not None:
|
|
result = 'FAIL candidate_identity_absence_unconfirmed previous=' + result
|
|
print('FLAG_EXEC_SUMMARY ' + json.dumps(EXECUTIONS, sort_keys=True), file=sys.stderr)
|
|
print(result)
|
|
return 0 if result == 'PASS' else 1
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|