/* Independent declarative oracle for the qualification fixture.
*
* Written separately from fixture.c and sharing no predicate or recovery code
* with it. It consumes only the public views (trace, records, effects) and the
* admitted operation sequence, then asserts invariants. Test-only. */
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "qual.h"
typedef struct {
q_verdict verdict;
char detail[128];
const q_op *ops;
size_t nops;
const q_trace *trace;
const q_record *recs;
size_t nrecs;
const q_effect *effs;
size_t neffs;
} q_view;
static int fail(q_view *v, q_verdict verdict, const char *msg) {
if (v->verdict == Q_OK) {
v->verdict = verdict;
(void)snprintf(v->detail, sizeof v->detail, "%s", msg);
}
return 0;
}
/* Evidence helper. Trace flags carry the effect safety bit for START events. */
static size_t count_events_for(const q_view *v, q_evkind kind, uint16_t rid) {
size_t c = 0;
for (size_t i = 0; i < v->trace->count; i++) {
if (v->trace->ev[i].kind == (uint8_t)kind &&
v->trace->ev[i].request_id == rid) {
c++;
}
}
return c;
}
/* I1 — a durable identity persists; an unreceipted admission may be lost. */
static int invariant_unique_admission(q_view *v) {
for (size_t i = 0; i < v->trace->count; i++) {
const q_trace_event *ei = &v->trace->ev[i];
int durable = 0;
if (ei->kind != (uint8_t)Q_EV_SUBMIT) {
continue;
}
for (size_t j = i + 1; j < v->trace->count; j++) {
const q_trace_event *ej = &v->trace->ev[j];
if (ej->kind == (uint8_t)Q_EV_COMMIT && ej->request_id == ei->request_id) {
durable = 1;
}
if (!durable && (ej->kind == (uint8_t)Q_EV_CRASH ||
ej->kind == (uint8_t)Q_EV_REOPEN)) {
break;
}
if (ej->kind == (uint8_t)Q_EV_SUBMIT &&
ej->request_id == ei->request_id) {
return fail(v, Q_FAIL_DUPLICATE_ADMISSION,
"live or durable request key admitted twice");
}
}
}
return 1;
}
/* I2 — an external effect starts only after its durable receipt. */
static int invariant_effect_after_receipt(q_view *v) {
for (size_t i = 0; i < v->trace->count; i++) {
const q_trace_event *e = &v->trace->ev[i];
int receipted = 0;
if (e->kind != (uint8_t)Q_EV_EFFECT_START) {
continue;
}
for (size_t j = 0; j < i; j++) {
if (v->trace->ev[j].kind == (uint8_t)Q_EV_COMMIT &&
v->trace->ev[j].request_id == e->request_id) {
receipted = 1;
break;
}
}
if (!receipted) {
return fail(v, Q_FAIL_EFFECT_BEFORE_RECEIPT,
"effect start without a prior commit receipt");
}
}
return 1;
}
/* Derive initial effect policy from declared input, independently of SUT flags. */
static int input_effect_policy(const q_view *v, uint16_t rid, uint8_t *safe) {
int admitted = 0;
int receipted = 0;
for (size_t i = 0; i < v->nops; i++) {
const q_op *op = &v->ops[i];
if (op->request_id == rid) {
if (op->opcode == Q_OP_SUBMIT) {
admitted = 1;
}
if (admitted && (op->opcode == Q_OP_COMMIT ||
op->opcode == Q_OP_CRASH_AFTER_COMMIT)) {
receipted = 1;
}
if (receipted && op->opcode == Q_OP_EXEC_EFFECT) {
*safe = (uint8_t)(op->arg & 1u);
return 1;
}
}
if (!receipted && (op->opcode == Q_OP_CRASH_BEFORE_COMMIT ||
op->opcode == Q_OP_CRASH_AFTER_COMMIT ||
op->opcode == Q_OP_REOPEN)) {
admitted = 0;
}
}
return 0;
}
/* I3 — an interrupted replay-unsafe effect is never started again. */
static int invariant_unsafe_not_repeated(q_view *v) {
for (size_t i = 0; i < v->trace->count; i++) {
const q_trace_event *e = &v->trace->ev[i];
uint8_t safe = 0;
if (e->kind != (uint8_t)Q_EV_EFFECT_START) {
continue;
}
if (!input_effect_policy(v, e->request_id, &safe)) {
return fail(v, Q_FAIL_STATE_MISMATCH, "effect has no receipted input intent");
}
if (!safe && count_events_for(v, Q_EV_EFFECT_START, e->request_id) > 1) {
return fail(v, Q_FAIL_REPEATED_UNSAFE_EFFECT,
"replay-unsafe effect started more than once");
}
if ((e->flags & 1u) != safe) {
return fail(v, Q_FAIL_STATE_MISMATCH, "effect safety differs from input intent");
}
}
return 1;
}
/* I4 — no effect is reported done without an observed start, and every
* finally-done effect has a matching done event. */
static int invariant_done_implies_start(q_view *v) {
for (size_t i = 0; i < v->trace->count; i++) {
const q_trace_event *e = &v->trace->ev[i];
uint8_t started = 0;
if (e->kind != (uint8_t)Q_EV_EFFECT_DONE) {
continue;
}
for (size_t j = 0; j < i; j++) {
if (v->trace->ev[j].kind == (uint8_t)Q_EV_EFFECT_START &&
v->trace->ev[j].request_id == e->request_id) {
started = 1;
break;
}
}
if (!started) {
return fail(v, Q_FAIL_UNKNOWN_AS_DONE,
"effect done event without a start");
}
}
for (size_t i = 0; i < v->neffs; i++) {
if (v->effs[i].status == 3 &&
count_events_for(v, Q_EV_EFFECT_DONE, v->effs[i].request_id) == 0) {
return fail(v, Q_FAIL_UNKNOWN_AS_DONE,
"final effect state claims completion, no done event");
}
}
return 1;
}
/* I5 — stored state agrees with observable receipts and identities are unique. */
static int invariant_state_consistency(q_view *v) {
for (size_t i = 0; i < v->trace->count; i++) {
const q_trace_event *event = &v->trace->ev[i];
int retained = 0;
if (event->kind != (uint8_t)Q_EV_COMMIT) {
continue;
}
for (size_t j = 0; j < v->nrecs; j++) {
if (v->recs[j].request_id == event->request_id &&
v->recs[j].outcome == (uint8_t)Q_STORED &&
v->recs[j].seq == event->aux) {
retained = 1;
break;
}
}
if (!retained) {
return fail(v, Q_FAIL_STATE_MISMATCH, "receipted record disappeared or changed");
}
}
for (size_t i = 0; i < v->nrecs; i++) {
for (size_t j = i + 1; j < v->nrecs; j++) {
if (v->recs[i].request_id == v->recs[j].request_id) {
return fail(v, Q_FAIL_STATE_MISMATCH,
"duplicate record identity in final state");
}
}
if (v->recs[i].outcome == (uint8_t)Q_STORED &&
count_events_for(v, Q_EV_COMMIT, v->recs[i].request_id) == 0) {
return fail(v, Q_FAIL_STATE_MISMATCH,
"stored record has no commit event");
}
}
for (size_t i = 0; i < v->neffs; i++) {
int stored = 0;
for (size_t j = 0; j < v->nrecs; j++) {
if (v->recs[j].request_id == v->effs[i].request_id &&
v->recs[j].outcome == (uint8_t)Q_STORED) {
stored = 1;
break;
}
}
if (!stored) {
return fail(v, Q_FAIL_STATE_MISMATCH,
"effect exists without a stored admission");
}
}
if (v->nrecs > Q_MAX_RECORDS || v->neffs > Q_MAX_EFFECTS ||
v->trace->count > Q_MAX_TRACE) {
return fail(v, Q_FAIL_STATE_MISMATCH, "bounded capacity exceeded");
}
return 1;
}
typedef int (*q_invariant)(q_view *v);
static const q_invariant g_oracle[] = {
invariant_unique_admission,
invariant_effect_after_receipt,
invariant_unsafe_not_repeated,
invariant_done_implies_start,
invariant_state_consistency,
};
q_verdict q_oracle_check(const q_op *ops, size_t n, const q_trace *trace,
const q_record *recs, size_t nrecs,
const q_effect *effs, size_t neffs,
char *detail, size_t detail_cap) {
q_view v;
memset(&v, 0, sizeof v);
v.verdict = Q_OK;
v.ops = ops;
v.nops = n;
v.trace = trace;
v.recs = recs;
v.nrecs = nrecs;
v.effs = effs;
v.neffs = neffs;
for (size_t i = 0; i < sizeof g_oracle / sizeof g_oracle[0]; i++) {
if (!g_oracle[i](&v)) {
break;
}
}
if (detail != NULL && detail_cap > 0) {
(void)snprintf(detail, detail_cap, "%s", v.detail);
}
return v.verdict;
}
const char *q_verdict_name(q_verdict v) {
switch (v) {
case Q_OK: return "OK";
case Q_FAIL_EFFECT_BEFORE_RECEIPT: return "FAIL_EFFECT_BEFORE_RECEIPT";
case Q_FAIL_DUPLICATE_ADMISSION: return "FAIL_DUPLICATE_ADMISSION";
case Q_FAIL_REPEATED_UNSAFE_EFFECT: return "FAIL_REPEATED_UNSAFE_EFFECT";
case Q_FAIL_UNKNOWN_AS_DONE: return "FAIL_UNKNOWN_AS_DONE";
case Q_FAIL_STATE_MISMATCH: return "FAIL_STATE_MISMATCH";
}
return "FAIL_UNKNOWN_VERDICT";
}