Skip to content
pi-c Documentation
Login

Documentation

/* Deterministic qualification fixture: a deliberately small admission,
 * commit, effect, and recovery model. Test-only; it is the system under test,
 * not production recovery coverage. Independent of oracle.c. */
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "qual.h"

struct q_ctx {
    uint64_t seed;
    uint64_t rng;
    uint64_t clock;
    unsigned mutant;
    uint32_t next_seq;
    q_record recs[Q_MAX_RECORDS];
    size_t nrec;
    q_effect effs[Q_MAX_EFFECTS];
    size_t neff;
};

static uint64_t splitmix64(uint64_t *state) {
    uint64_t z = (*state += UINT64_C(0x9E3779B97F4A7C15));
    z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
    z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
    return z ^ (z >> 31);
}

static uint32_t rng_next(q_ctx *ctx) {
    return (uint32_t)(splitmix64(&ctx->rng) >> 32);
}

static void emit(q_trace *t, uint8_t kind, uint16_t rid, uint8_t flags,
                 uint32_t aux) {
    if (t == NULL || t->count >= Q_MAX_TRACE) {
        return;
    }
    t->ev[t->count].kind = kind;
    t->ev[t->count].flags = flags;
    t->ev[t->count].request_id = rid;
    t->ev[t->count].aux = aux;
    t->count++;
}

static q_record *find_rec(q_ctx *ctx, uint16_t rid) {
    for (size_t i = 0; i < ctx->nrec; i++) {
        if (ctx->recs[i].request_id == rid) {
            return &ctx->recs[i];
        }
    }
    return NULL;
}

static q_effect *find_effect(q_ctx *ctx, uint16_t rid) {
    for (size_t i = 0; i < ctx->neff; i++) {
        if (ctx->effs[i].request_id == rid) {
            return &ctx->effs[i];
        }
    }
    return NULL;
}

static int has_receipt(q_ctx *ctx, uint16_t rid) {
    q_record *r = find_rec(ctx, rid);
    return r != NULL && r->outcome == (uint8_t)Q_STORED;
}

static uint32_t payload_hash(q_ctx *ctx) {
    q_chunk chunks[Q_MAX_CHUNKS];
    size_t n = 0;
    uint32_t h = UINT32_C(2166136261);
    q_seeded_chunks(ctx->seed ^ ctx->rng, chunks, Q_MAX_CHUNKS, &n);
    for (size_t i = 0; i < n; i++) {
        for (uint8_t j = 0; j < chunks[i].len; j++) {
            h = (h ^ (uint32_t)chunks[i].bytes[j]) * UINT32_C(16777619);
        }
    }
    return h;
}

static void do_submit(q_ctx *ctx, const q_op *op, q_trace *t) {
    q_record *r = find_rec(ctx, op->request_id);
    if (r != NULL) {
        if (ctx->mutant == 2 && ctx->nrec < Q_MAX_RECORDS) {
            /* Mutant: a second record for an existing request key. */
            ctx->recs[ctx->nrec++] = (q_record){op->request_id, op->kind,
                                                (uint8_t)Q_UNKNOWN,
                                                ctx->next_seq++, 0};
            emit(t, Q_EV_SUBMIT, op->request_id, 0, ctx->next_seq);
            return;
        }
        if (r->kind != op->kind) {
            emit(t, Q_EV_REFUSED, op->request_id, op->kind, 0);
            return;
        }
        emit(t, Q_EV_DEDUP, op->request_id, 0, r->seq);
        return;
    }
    if (ctx->nrec >= Q_MAX_RECORDS) {
        emit(t, Q_EV_REFUSED, op->request_id, op->kind, 0);
        return;
    }
    ctx->recs[ctx->nrec++] = (q_record){op->request_id, op->kind,
                                        (uint8_t)Q_UNKNOWN,
                                        ctx->next_seq++, 0};
    emit(t, Q_EV_SUBMIT, op->request_id, 0, ctx->next_seq);
}

static void do_commit(q_ctx *ctx, uint16_t rid, q_trace *t) {
    q_record *r = find_rec(ctx, rid);
    if (r == NULL) {
        emit(t, Q_EV_REFUSED, rid, 0, 0);
        return;
    }
    r->attempts++;
    if (r->outcome == (uint8_t)Q_UNKNOWN) {
        r->outcome = (uint8_t)Q_STORED;
        emit(t, Q_EV_COMMIT, rid, 0, r->seq);
    }
}

static void start_effect(q_ctx *ctx, q_effect *e, q_trace *t) {
    e->attempts++;
    e->status = 1;
    e->payload = payload_hash(ctx);
    emit(t, Q_EV_EFFECT_START, e->request_id, e->safe, e->payload);
}

static void do_exec_effect(q_ctx *ctx, const q_op *op, q_trace *t) {
    uint8_t safe = (uint8_t)(op->arg & 1u);
    int allowed = has_receipt(ctx, op->request_id) || ctx->mutant == 1;
    q_effect *e;
    if (!allowed) {
        return;
    }
    e = find_effect(ctx, op->request_id);
    if (e == NULL) {
        if (ctx->neff >= Q_MAX_EFFECTS) {
            return;
        }
        e = &ctx->effs[ctx->neff++];
        *e = (q_effect){op->request_id, 1, safe, 0, 0};
        start_effect(ctx, e, t);
        return;
    }
    if (e->status == 3) {
        return; /* a completed effect is never repeated */
    }
    if (!e->safe && ctx->mutant != 3) {
        return; /* interrupted replay-unsafe effect is not repeated */
    }
    start_effect(ctx, e, t);
}

static void do_effect_done(q_ctx *ctx, uint16_t rid, q_trace *t) {
    q_effect *e = find_effect(ctx, rid);
    if (e == NULL || e->status == 3) {
        return;
    }
    e->status = 3;
    emit(t, Q_EV_EFFECT_DONE, rid, e->safe, e->attempts);
}

static void do_crash(q_ctx *ctx, uint16_t rid, int after_commit, q_trace *t) {
    q_record *r = find_rec(ctx, rid);
    if (after_commit && r != NULL && r->outcome == (uint8_t)Q_UNKNOWN) {
        r->outcome = (uint8_t)Q_STORED;
        emit(t, Q_EV_COMMIT, rid, 0, r->seq);
    }
    emit(t, Q_EV_CRASH, rid, after_commit ? 1u : 0u, 0);
    /* Volatile, uncommitted admissions and in-flight effect state are lost. */
    {
        size_t keep = 0;
        for (size_t i = 0; i < ctx->nrec; i++) {
            if (ctx->recs[i].outcome == (uint8_t)Q_STORED) {
                ctx->recs[keep++] = ctx->recs[i];
            }
        }
        ctx->nrec = keep;
    }
    for (size_t i = 0; i < ctx->neff; i++) {
        if (ctx->effs[i].status == 1) {
            ctx->effs[i].status = 2; /* unknown, never silently completed */
        }
    }
}

static void do_reopen(q_ctx *ctx, q_trace *t) {
    emit(t, Q_EV_REOPEN, 0, 0, 0);
    {
        size_t keep = 0;
        for (size_t i = 0; i < ctx->nrec; i++) {
            if (ctx->recs[i].outcome == (uint8_t)Q_STORED) {
                ctx->recs[keep++] = ctx->recs[i];
            }
        }
        ctx->nrec = keep;
    }
    for (size_t i = 0; i < ctx->neff; i++) {
        if (ctx->effs[i].status == 1) {
            ctx->effs[i].status = 2;
        }
    }
}

static void do_resolve(q_ctx *ctx, uint16_t rid, q_trace *t) {
    q_effect *e = find_effect(ctx, rid);
    emit(t, Q_EV_RESOLVE, rid, 0, 0);
    if (e == NULL || e->status == 3) {
        return;
    }
    if (e->safe || ctx->mutant == 3) {
        start_effect(ctx, e, t);
        e->status = 3;
        emit(t, Q_EV_EFFECT_DONE, rid, e->safe, e->attempts);
        return;
    }
    if (ctx->mutant == 4) {
        e->status = 3; /* Mutant: fabricate success for an unfinished effect. */
    }
}

q_ctx *q_new(uint64_t seed) {
    q_ctx *ctx = calloc(1, sizeof *ctx);
    if (ctx == NULL) {
        return NULL;
    }
    q_reset(ctx, seed);
    return ctx;
}

void q_free(q_ctx *ctx) { free(ctx); }

void q_reset(q_ctx *ctx, uint64_t seed) {
    if (ctx == NULL) {
        return;
    }
    memset(ctx, 0, sizeof *ctx);
    ctx->seed = seed;
    ctx->rng = seed == 0 ? UINT64_C(0x9E3779B97F4A7C15) : seed;
}

void q_set_mutant(q_ctx *ctx, unsigned mutant) {
    if (ctx != NULL) {
        ctx->mutant = mutant;
    }
}

void q_run_ops(q_ctx *ctx, const q_op *ops, size_t n, q_trace *out) {
    if (ctx == NULL || out == NULL) {
        return;
    }
    memset(out, 0, sizeof *out);
    for (size_t i = 0; i < n; i++) {
        ctx->clock += UINT64_C(1000) + (uint64_t)(rng_next(ctx) % 100u);
        switch (ops[i].opcode) {
        case Q_OP_SUBMIT:
            do_submit(ctx, &ops[i], out);
            break;
        case Q_OP_COMMIT:
            do_commit(ctx, ops[i].request_id, out);
            break;
        case Q_OP_CRASH_BEFORE_COMMIT:
            do_crash(ctx, ops[i].request_id, 0, out);
            break;
        case Q_OP_CRASH_AFTER_COMMIT:
            do_crash(ctx, ops[i].request_id, 1, out);
            break;
        case Q_OP_EXEC_EFFECT:
            do_exec_effect(ctx, &ops[i], out);
            break;
        case Q_OP_EFFECT_DONE:
            do_effect_done(ctx, ops[i].request_id, out);
            break;
        case Q_OP_RESOLVE:
            do_resolve(ctx, ops[i].request_id, out);
            break;
        case Q_OP_REOPEN:
            do_reopen(ctx, out);
            break;
        default:
            emit(out, Q_EV_REFUSED, ops[i].request_id, ops[i].kind, 0);
            break;
        }
    }
}

const q_record *q_records(const q_ctx *ctx, size_t *n) {
    if (n != NULL) {
        *n = ctx == NULL ? 0 : ctx->nrec;
    }
    return ctx == NULL ? NULL : ctx->recs;
}

const q_effect *q_effects(const q_ctx *ctx, size_t *n) {
    if (n != NULL) {
        *n = ctx == NULL ? 0 : ctx->neff;
    }
    return ctx == NULL ? NULL : ctx->effs;
}

uint64_t q_clock_ns(const q_ctx *ctx) { return ctx == NULL ? 0 : ctx->clock; }

uint32_t q_prng_next(q_ctx *ctx) {
    return ctx == NULL ? 0 : rng_next(ctx);
}

void q_seeded_ops(uint64_t seed, q_op *ops, size_t cap, size_t *n) {
    uint64_t s = seed == 0 ? UINT64_C(0x9E3779B97F4A7C15) : seed;
    size_t k = 0;
    unsigned rid = 1;
    if (ops == NULL || n == NULL) {
        return;
    }
    while (k + 7 <= cap && rid <= 6) {
        uint64_t r = splitmix64(&s);
        ops[k++] = (q_op){Q_OP_SUBMIT, (uint16_t)rid, 1, 0};
        if ((r & 1u) != 0) {
            ops[k++] = (q_op){Q_OP_SUBMIT, (uint16_t)rid, 1, 0}; /* dedup */
        }
        ops[k++] = (q_op){Q_OP_COMMIT, (uint16_t)rid, 1, 0};
        if ((r & 2u) != 0) {
            uint8_t safe = (uint8_t)((r >> 2) & 1u);
            ops[k++] = (q_op){Q_OP_EXEC_EFFECT, (uint16_t)rid, 1, safe};
            if ((r & 4u) != 0) {
                ops[k++] = (q_op){Q_OP_CRASH_BEFORE_COMMIT, (uint16_t)rid, 1, 0};
                ops[k++] = (q_op){Q_OP_REOPEN, 0, 0, 0};
                ops[k++] = (q_op){Q_OP_RESOLVE, (uint16_t)rid, 1, 0};
            } else {
                ops[k++] = (q_op){Q_OP_EFFECT_DONE, (uint16_t)rid, 1, 0};
            }
        } else {
            ops[k++] = (q_op){Q_OP_REOPEN, 0, 0, 0};
        }
        rid++;
    }
    *n = k;
}

void q_seeded_chunks(uint64_t seed, q_chunk *chunks, size_t cap, size_t *n) {
    uint64_t s = seed == 0 ? UINT64_C(0x9E3779B97F4A7C15) : seed;
    size_t count;
    if (chunks == NULL || n == NULL || cap == 0) {
        if (n != NULL) {
            *n = 0;
        }
        return;
    }
    count = 1u + (size_t)(splitmix64(&s) % (uint64_t)cap);
    for (size_t i = 0; i < count; i++) {
        uint64_t v = splitmix64(&s);
        size_t len = 1u + (size_t)(v % 8u);
        memset(&chunks[i], 0, sizeof chunks[i]);
        chunks[i].len = (uint8_t)len;
        for (size_t j = 0; j < len; j++) {
            chunks[i].bytes[j] = (uint8_t)(v >> ((j % 8u) * 8u));
        }
    }
    *n = count;
}

size_t q_parse_ops(const uint8_t *buf, size_t len, q_op *ops, size_t cap) {
    size_t count;
    if (buf == NULL || ops == NULL || len % Q_OP_BYTES != 0) {
        return SIZE_MAX;
    }
    count = len / Q_OP_BYTES;
    if (count > cap || count > Q_MAX_OPS) {
        return SIZE_MAX;
    }
    for (size_t i = 0; i < count; i++) {
        const uint8_t *p = &buf[i * Q_OP_BYTES];
        if (p[0] >= (uint8_t)Q_OP_COUNT) {
            return SIZE_MAX;
        }
        ops[i].opcode = p[0];
        ops[i].request_id = (uint16_t)(p[1] | ((uint16_t)p[2] << 8));
        ops[i].kind = p[3];
        ops[i].arg = p[4];
    }
    return count;
}