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>
207 lines
6.7 KiB
C
207 lines
6.7 KiB
C
#include "command_port.h"
|
|
#include <string.h>
|
|
static void record_event(struct command_port *port, unsigned operation, unsigned token, long value)
|
|
{
|
|
if (port->event_count < COMMAND_MAX_EVENTS) {
|
|
struct port_event *event = &port->events[port->event_count++];
|
|
event->operation = operation;
|
|
event->token = token;
|
|
event->value = value;
|
|
} else {
|
|
port->dropped_events++;
|
|
}
|
|
}
|
|
static unsigned token_of(size_t index, unsigned generation)
|
|
{
|
|
return generation * COMMAND_MAX_RECORDS + (unsigned)index;
|
|
}
|
|
static struct port_record *lookup(struct command_port *port, unsigned token)
|
|
{
|
|
unsigned index = token % COMMAND_MAX_RECORDS;
|
|
unsigned generation = token / COMMAND_MAX_RECORDS;
|
|
struct port_record *record = &port->records[index];
|
|
if (!record->active || record->generation != generation) {
|
|
port->protocol_errors++;
|
|
return NULL;
|
|
}
|
|
return record;
|
|
}
|
|
static void retire(struct port_record *record)
|
|
{
|
|
if (!record->stream_open && !record->child_owned) {
|
|
record->active = 0;
|
|
}
|
|
}
|
|
int command_plan_valid(const struct command_plan *plan)
|
|
{
|
|
if (plan == NULL) {
|
|
return 0;
|
|
}
|
|
if (plan->payload_size > COMMAND_MAX_BYTES) {
|
|
return 0;
|
|
}
|
|
if (plan->chunk_size == 0 || plan->chunk_size > 128) {
|
|
return 0;
|
|
}
|
|
if (plan->read_fault_at < -1 || plan->read_fault_at > (long)plan->payload_size) {
|
|
return 0;
|
|
}
|
|
if (plan->read_interrupts > 16 || plan->wait_interrupts > 16) {
|
|
return 0;
|
|
}
|
|
if (plan->child_exit > 255 || plan->child_signal > 64) {
|
|
return 0;
|
|
}
|
|
if (plan->child_signal != 0 && plan->child_exit != 0) {
|
|
return 0;
|
|
}
|
|
if (plan->fail_open != 0 && plan->fail_open != 1) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
void command_port_init(struct command_port *port, unsigned streams, unsigned children)
|
|
{
|
|
memset(port, 0, sizeof(*port));
|
|
port->stream_capacity = streams;
|
|
port->child_capacity = children;
|
|
port->next_generation = 1;
|
|
port->plan.chunk_size = 16;
|
|
port->plan.read_fault_at = -1;
|
|
}
|
|
int command_port_configure(struct command_port *port, const struct command_plan *plan)
|
|
{
|
|
if (!command_plan_valid(plan)) {
|
|
return PORT_ERROR;
|
|
}
|
|
port->plan = *plan;
|
|
return 0;
|
|
}
|
|
int command_port_open(struct command_port *port, unsigned tag, unsigned *token)
|
|
{
|
|
size_t index;
|
|
if (token == NULL || !command_plan_valid(&port->plan)) {
|
|
port->protocol_errors++;
|
|
record_event(port, 1, 0, PORT_ERROR);
|
|
return PORT_ERROR;
|
|
}
|
|
if (port->plan.fail_open || port->streams_live >= port->stream_capacity ||
|
|
port->children_live >= port->child_capacity) {
|
|
port->failures++;
|
|
record_event(port, 1, 0, PORT_ERROR);
|
|
return PORT_ERROR;
|
|
}
|
|
for (index = 0; index < COMMAND_MAX_RECORDS; index++) {
|
|
struct port_record *record = &port->records[index];
|
|
if (record->active) {
|
|
continue;
|
|
}
|
|
memset(record, 0, sizeof(*record));
|
|
record->active = 1;
|
|
record->stream_open = 1;
|
|
record->child_owned = 1;
|
|
record->generation = port->next_generation++;
|
|
record->tag = tag;
|
|
record->read_interrupts = port->plan.read_interrupts;
|
|
record->wait_interrupts = port->plan.wait_interrupts;
|
|
port->streams_live++;
|
|
port->children_live++;
|
|
port->opens++;
|
|
if (port->streams_live > port->streams_peak) {
|
|
port->streams_peak = port->streams_live;
|
|
}
|
|
if (port->children_live > port->children_peak) {
|
|
port->children_peak = port->children_live;
|
|
}
|
|
*token = token_of(index, record->generation);
|
|
record_event(port, 1, *token, tag);
|
|
return 0;
|
|
}
|
|
port->failures++;
|
|
record_event(port, 1, 0, PORT_ERROR);
|
|
return PORT_ERROR;
|
|
}
|
|
long command_port_read(struct command_port *port, unsigned token, unsigned char *data, size_t size)
|
|
{
|
|
struct port_record *record = lookup(port, token);
|
|
size_t amount;
|
|
port->reads++;
|
|
if (record == NULL || !record->stream_open || data == NULL || size == 0) {
|
|
port->protocol_errors++;
|
|
record_event(port, 2, token, PORT_ERROR);
|
|
return PORT_ERROR;
|
|
}
|
|
if (record->read_interrupts != 0) {
|
|
record->read_interrupts--;
|
|
record_event(port, 2, token, PORT_INTERRUPTED);
|
|
return PORT_INTERRUPTED;
|
|
}
|
|
if (port->plan.read_fault_at >= 0 && record->position >= (size_t)port->plan.read_fault_at) {
|
|
record_event(port, 2, token, PORT_ERROR);
|
|
return PORT_ERROR;
|
|
}
|
|
if (record->position == port->plan.payload_size) {
|
|
record_event(port, 2, token, 0);
|
|
return 0;
|
|
}
|
|
amount = port->plan.payload_size - record->position;
|
|
if (amount > size) {
|
|
amount = size;
|
|
}
|
|
if (amount > port->plan.chunk_size) {
|
|
amount = port->plan.chunk_size;
|
|
}
|
|
if (port->plan.read_fault_at >= 0 && amount > (size_t)port->plan.read_fault_at - record->position) {
|
|
amount = (size_t)port->plan.read_fault_at - record->position;
|
|
}
|
|
memcpy(data, port->plan.payload + record->position, amount);
|
|
record->position += amount;
|
|
record_event(port, 2, token, (long)amount);
|
|
return (long)amount;
|
|
}
|
|
int command_port_close(struct command_port *port, unsigned token)
|
|
{
|
|
struct port_record *record = lookup(port, token);
|
|
if (record == NULL || !record->stream_open) {
|
|
port->protocol_errors++;
|
|
record_event(port, 3, token, PORT_ERROR);
|
|
return PORT_ERROR;
|
|
}
|
|
record->stream_open = 0;
|
|
port->streams_live--;
|
|
port->closes++;
|
|
retire(record);
|
|
record_event(port, 3, token, 0);
|
|
return 0;
|
|
}
|
|
int command_port_wait(struct command_port *port, unsigned token, unsigned *exit_code, unsigned *signal_number)
|
|
{
|
|
struct port_record *record = lookup(port, token);
|
|
if (record == NULL || !record->child_owned || exit_code == NULL || signal_number == NULL) {
|
|
port->protocol_errors++;
|
|
record_event(port, 4, token, PORT_ERROR);
|
|
return PORT_ERROR;
|
|
}
|
|
if (record->wait_interrupts != 0) {
|
|
record->wait_interrupts--;
|
|
record_event(port, 4, token, PORT_INTERRUPTED);
|
|
return PORT_INTERRUPTED;
|
|
}
|
|
*exit_code = port->plan.child_exit;
|
|
*signal_number = port->plan.child_signal;
|
|
record->child_owned = 0;
|
|
port->children_live--;
|
|
port->waits++;
|
|
retire(record);
|
|
record_event(port, 4, token, 0);
|
|
return 0;
|
|
}
|
|
size_t command_port_events(const struct command_port *port, struct port_event *events, size_t capacity)
|
|
{
|
|
size_t count = port->event_count < capacity ? port->event_count : capacity;
|
|
if (events != NULL && count != 0) {
|
|
memcpy(events, port->events, count * sizeof(*events));
|
|
}
|
|
return count;
|
|
}
|