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>
330 lines
9.0 KiB
C
330 lines
9.0 KiB
C
#include "owner.h"
|
|
#include "planner.h"
|
|
#include "protocol.h"
|
|
|
|
#include <inttypes.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
typedef struct Session {
|
|
OwnerBank owners;
|
|
View views[OWNER_SLOTS];
|
|
unsigned view_present[OWNER_SLOTS];
|
|
size_t view_alignment[OWNER_SLOTS];
|
|
size_t view_requested[OWNER_SLOTS];
|
|
unsigned char fill_seed[OWNER_SLOTS];
|
|
unsigned lines;
|
|
unsigned failures;
|
|
} Session;
|
|
|
|
typedef struct ViewFacts {
|
|
int owner_ok;
|
|
int aligned;
|
|
int in_bounds;
|
|
int usable;
|
|
} ViewFacts;
|
|
|
|
static void session_init(Session *session)
|
|
{
|
|
unsigned index;
|
|
memset(session, 0, sizeof(*session));
|
|
owner_bank_init(&session->owners);
|
|
for (index = 0; index < OWNER_SLOTS; ++index) {
|
|
clear_view(&session->views[index]);
|
|
}
|
|
}
|
|
|
|
static int exact_owner(const Owner *owner, const View *view)
|
|
{
|
|
if (owner == NULL || view == NULL || !owner_integrity(owner)) {
|
|
return 0;
|
|
}
|
|
if (view->owner != owner) {
|
|
return 0;
|
|
}
|
|
if (view->original != owner->original) {
|
|
return 0;
|
|
}
|
|
if (view->original_capacity != owner->capacity) {
|
|
return 0;
|
|
}
|
|
if (view->generation != owner->generation) {
|
|
return 0;
|
|
}
|
|
return view->id == owner->id;
|
|
}
|
|
|
|
static int address_matches_offset(const Owner *owner, const View *view)
|
|
{
|
|
uintptr_t base;
|
|
uintptr_t address;
|
|
if (owner == NULL || view == NULL || !owner->live) {
|
|
return 0;
|
|
}
|
|
if (!owner_range_inside(owner, view->offset, 0)) {
|
|
return 0;
|
|
}
|
|
base = owner_address(owner);
|
|
address = (uintptr_t)view->begin;
|
|
if (address < base) {
|
|
return 0;
|
|
}
|
|
return address - base == view->offset;
|
|
}
|
|
|
|
static int view_is_aligned(const Owner *owner, const View *view, size_t alignment)
|
|
{
|
|
uintptr_t base;
|
|
if (alignment == 0 || !address_matches_offset(owner, view)) {
|
|
return 0;
|
|
}
|
|
base = owner_address(owner);
|
|
if (view->offset > UINTPTR_MAX - base) {
|
|
return 0;
|
|
}
|
|
return (base + view->offset) % alignment == 0;
|
|
}
|
|
|
|
static ViewFacts inspect_view(const Owner *owner, const View *view,
|
|
size_t alignment, size_t requested)
|
|
{
|
|
ViewFacts facts = {0, 0, 0, 0};
|
|
if (owner == NULL || view == NULL || view->status != 0) {
|
|
return facts;
|
|
}
|
|
facts.owner_ok = exact_owner(owner, view);
|
|
facts.aligned = view_is_aligned(owner, view, alignment);
|
|
facts.in_bounds = owner_range_inside(owner, view->offset, view->span);
|
|
if (!address_matches_offset(owner, view)) {
|
|
facts.in_bounds = 0;
|
|
}
|
|
facts.usable = facts.owner_ok && facts.aligned && facts.in_bounds
|
|
&& requested <= view->span;
|
|
return facts;
|
|
}
|
|
|
|
static int saved_view_usable(Session *session, unsigned id)
|
|
{
|
|
Owner *owner;
|
|
View *view;
|
|
if (id >= OWNER_SLOTS || !session->view_present[id]) {
|
|
return 0;
|
|
}
|
|
owner = owner_lookup(&session->owners, id);
|
|
view = &session->views[id];
|
|
if (!exact_owner(owner, view)) {
|
|
return 0;
|
|
}
|
|
if (!owner_range_inside(owner, view->offset, view->span)) {
|
|
return 0;
|
|
}
|
|
if (!view_is_aligned(owner, view, session->view_alignment[id])) {
|
|
return 0;
|
|
}
|
|
if (session->view_requested[id] > view->span) {
|
|
return 0;
|
|
}
|
|
return address_matches_offset(owner, view);
|
|
}
|
|
|
|
static unsigned char pattern_byte(unsigned seed, size_t index)
|
|
{
|
|
return (unsigned char)((seed + index * 17u) % 256u);
|
|
}
|
|
|
|
static void fill_owner(Owner *owner, unsigned seed)
|
|
{
|
|
size_t index;
|
|
unsigned char *pointer = owner_live_pointer(owner);
|
|
size_t capacity = owner_live_capacity(owner);
|
|
for (index = 0; pointer != NULL && index < capacity; ++index) {
|
|
pointer[index] = pattern_byte(seed, index);
|
|
}
|
|
}
|
|
|
|
static unsigned checksum_bytes(const unsigned char *data, size_t count)
|
|
{
|
|
size_t index;
|
|
unsigned accumulator = 0;
|
|
for (index = 0; index < count; ++index) {
|
|
accumulator = (accumulator * 33u + data[index]) % 65521u;
|
|
}
|
|
return accumulator;
|
|
}
|
|
|
|
static void handle_fill(Session *session, const Command *command)
|
|
{
|
|
Owner *owner = owner_lookup(&session->owners, command->id);
|
|
if (owner == NULL || !owner_integrity(owner)) {
|
|
printf("F %u 0\n", command->id);
|
|
return;
|
|
}
|
|
fill_owner(owner, (unsigned)command->first);
|
|
session->fill_seed[command->id] = (unsigned char)command->first;
|
|
printf("F %u 1\n", command->id);
|
|
}
|
|
|
|
static void handle_probe(Session *session, const Command *command)
|
|
{
|
|
View *view = &session->views[command->id];
|
|
if (!saved_view_usable(session, command->id)
|
|
|| command->first > view->span) {
|
|
printf("T %u -1 0\n", command->id);
|
|
return;
|
|
}
|
|
printf("T %u 0 %u\n", command->id,
|
|
checksum_bytes(view->begin, command->first));
|
|
}
|
|
|
|
static int copy_allowed(Session *session, unsigned source_id,
|
|
unsigned destination_id, size_t count)
|
|
{
|
|
Owner *destination = owner_lookup(&session->owners, destination_id);
|
|
View *source = &session->views[source_id];
|
|
if (!saved_view_usable(session, source_id)) {
|
|
return 0;
|
|
}
|
|
if (destination == NULL || !owner_integrity(destination)) {
|
|
return 0;
|
|
}
|
|
if (count > source->span || count > owner_live_capacity(destination)) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static void handle_copy(Session *session, const Command *command)
|
|
{
|
|
unsigned source_id = command->id;
|
|
unsigned destination_id = (unsigned)command->first;
|
|
size_t count = command->second;
|
|
Owner *destination;
|
|
View *source;
|
|
if (!copy_allowed(session, source_id, destination_id, count)) {
|
|
printf("X %u %u 0\n", source_id, destination_id);
|
|
return;
|
|
}
|
|
destination = owner_lookup(&session->owners, destination_id);
|
|
source = &session->views[source_id];
|
|
memmove(owner_live_pointer(destination), source->begin, count);
|
|
printf("X %u %u 1\n", source_id, destination_id);
|
|
}
|
|
|
|
static void handle_reset(Session *session)
|
|
{
|
|
unsigned index;
|
|
unsigned released = 0;
|
|
for (index = 0; index < OWNER_SLOTS; ++index) {
|
|
if (owner_drop(&session->owners, index)) {
|
|
++released;
|
|
}
|
|
}
|
|
printf("Z %u\n", released);
|
|
}
|
|
|
|
static void handle_open(Session *session, const Command *command)
|
|
{
|
|
Owner *owner;
|
|
if (!owner_open(&session->owners, command->id, command->first)) {
|
|
puts("E");
|
|
++session->failures;
|
|
return;
|
|
}
|
|
owner = owner_lookup(&session->owners, command->id);
|
|
session->fill_seed[command->id] = 0;
|
|
printf("O %u %zu %u\n", command->id, owner->capacity, owner->generation);
|
|
}
|
|
|
|
static void handle_plan(Session *session, const Command *command)
|
|
{
|
|
Owner *owner = owner_lookup(&session->owners, command->id);
|
|
View *view = &session->views[command->id];
|
|
ViewFacts facts;
|
|
size_t residue = owner_residue(owner, command->first);
|
|
int status;
|
|
clear_view(view);
|
|
session->view_present[command->id] = 0;
|
|
status = plan_view(owner, command->first, command->second, view);
|
|
session->view_alignment[command->id] = command->first;
|
|
session->view_requested[command->id] = command->second;
|
|
if (status != 0) {
|
|
printf("P %u %zu -1 0 0 0 0 0 0\n", command->id, residue);
|
|
return;
|
|
}
|
|
session->view_present[command->id] = 1;
|
|
facts = inspect_view(owner, view, command->first, command->second);
|
|
printf("P %u %zu 0 %zu %zu %d %d %d %d\n",
|
|
command->id, residue, view->offset, view->span,
|
|
facts.owner_ok, facts.aligned, facts.in_bounds, facts.usable);
|
|
}
|
|
|
|
static void handle_check(Session *session, const Command *command)
|
|
{
|
|
printf("C %u %d\n", command->id,
|
|
saved_view_usable(session, command->id));
|
|
}
|
|
|
|
static void handle_drop(Session *session, const Command *command)
|
|
{
|
|
int released = owner_drop(&session->owners, command->id);
|
|
printf("D %u %d\n", command->id, released);
|
|
}
|
|
|
|
static void handle_command(Session *session, const Command *command)
|
|
{
|
|
switch (command->kind) {
|
|
case COMMAND_OPEN:
|
|
handle_open(session, command);
|
|
break;
|
|
case COMMAND_PLAN:
|
|
handle_plan(session, command);
|
|
break;
|
|
case COMMAND_CHECK:
|
|
handle_check(session, command);
|
|
break;
|
|
case COMMAND_DROP:
|
|
handle_drop(session, command);
|
|
break;
|
|
case COMMAND_FILL:
|
|
handle_fill(session, command);
|
|
break;
|
|
case COMMAND_PROBE:
|
|
handle_probe(session, command);
|
|
break;
|
|
case COMMAND_COPY:
|
|
handle_copy(session, command);
|
|
break;
|
|
case COMMAND_RESET:
|
|
handle_reset(session);
|
|
break;
|
|
default:
|
|
puts("E");
|
|
++session->failures;
|
|
break;
|
|
}
|
|
}
|
|
|
|
static int run_session(FILE *input)
|
|
{
|
|
Session session;
|
|
InputReader reader;
|
|
Command command;
|
|
session_init(&session);
|
|
reader_init(&reader, input);
|
|
while (reader_next(&reader, &command)) {
|
|
handle_command(&session, &command);
|
|
++session.lines;
|
|
}
|
|
printf("I %llu %u\n", reader.bytes, reader.digest);
|
|
owner_bank_finish(&session.owners);
|
|
if (ferror(input) || ferror(stdout)) {
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
return run_session(stdin);
|
|
}
|