Skip to content
pi-c Artifact [26f19b469f]
Login

Artifact [26f19b469f]

Artifact 26f19b469f2ef135e8e502a901af414bbf9106349e6aa43ae22a4ee734656771:


#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "qual.h"

/* Mutant matrix: every injected defect must be caught, and the correct
 * implementation must not be reported defective. */
static void build(q_op *ops, size_t *n) {
    size_t i = 0;
    ops[i++] = (q_op){Q_OP_SUBMIT, 1, 1, 0};
    ops[i++] = (q_op){Q_OP_COMMIT, 1, 1, 0};
    ops[i++] = (q_op){Q_OP_SUBMIT, 1, 1, 0};       /* dedup */
    ops[i++] = (q_op){Q_OP_EXEC_EFFECT, 1, 1, 0};  /* unsafe */
    ops[i++] = (q_op){Q_OP_CRASH_BEFORE_COMMIT, 1, 1, 0};
    ops[i++] = (q_op){Q_OP_REOPEN, 0, 0, 0};
    ops[i++] = (q_op){Q_OP_RESOLVE, 1, 1, 0};
    ops[i++] = (q_op){Q_OP_SUBMIT, 2, 1, 0};
    ops[i++] = (q_op){Q_OP_EXEC_EFFECT, 2, 1, 0};  /* before receipt */
    *n = i;
}

static q_verdict observe(unsigned mutant, q_op *ops, size_t n, char *d,
                         size_t dcap) {
    q_ctx *c = q_new(7);
    q_trace t;
    const q_record *r;
    const q_effect *e;
    size_t nr, ne;
    q_verdict v;
    q_set_mutant(c, mutant);
    q_run_ops(c, ops, n, &t);
    r = q_records(c, &nr);
    e = q_effects(c, &ne);
    v = q_oracle_check(ops, n, &t, r, nr, e, ne, d, dcap);
    q_free(c);
    return v;
}

/* Safety policy comes from input intent, not the fixture's self-reported flag. */
static void test_forged_safe_flags(q_op *ops, size_t n, unsigned mutant,
                                   q_verdict expected) {
    q_ctx *c = q_new(7);
    q_trace t;
    size_t nr, ne;
    const q_record *r;
    const q_effect *e;
    assert(c != NULL);
    q_set_mutant(c, mutant);
    q_run_ops(c, ops, n, &t);
    for (size_t i = 0; i < t.count; i++) {
        if (t.ev[i].kind == Q_EV_EFFECT_START) {
            t.ev[i].flags = 1;
        }
    }
    r = q_records(c, &nr);
    e = q_effects(c, &ne);
    assert(q_oracle_check(ops, n, &t, r, nr, e, ne, NULL, 0) == expected);
    q_free(c);
}

static void test_committed_record_cannot_disappear(void) {
    q_op ops[] = {{Q_OP_SUBMIT, 1, 1, 0}, {Q_OP_COMMIT, 1, 1, 0}};
    q_ctx *c = q_new(7);
    q_trace t;
    size_t nr;
    assert(c != NULL);
    q_run_ops(c, ops, sizeof ops / sizeof ops[0], &t);
    (void)q_records(c, &nr);
    assert(nr == 1);
    /* Present a recovery that lost the record despite the commit receipt. */
    assert(q_oracle_check(ops, sizeof ops / sizeof ops[0], &t,
                          NULL, 0, NULL, 0, NULL, 0) == Q_FAIL_STATE_MISMATCH);
    q_free(c);
}

int main(void) {
    q_op ops[Q_MAX_OPS];
    size_t n = 0;
    char detail[128];

    build(ops, &n);
    memset(detail, 0, sizeof detail);
    assert(observe(0, ops, n, detail, sizeof detail) == Q_OK);

    /* Mutant 1: execute an effect despite no durable receipt. */
    assert(observe(1, ops, n, detail, sizeof detail) ==
           Q_FAIL_EFFECT_BEFORE_RECEIPT);
    /* Mutant 2: re-admit an existing request key (duplicate identity). */
    assert(observe(2, ops, n, detail, sizeof detail) ==
           Q_FAIL_DUPLICATE_ADMISSION);
    /* Mutant 3: re-run an interrupted replay-unsafe effect. */
    assert(observe(3, ops, n, detail, sizeof detail) ==
           Q_FAIL_REPEATED_UNSAFE_EFFECT);
    /* Mutant 4: report an interrupted effect as completed. */
    assert(observe(4, ops, n, detail, sizeof detail) == Q_FAIL_UNKNOWN_AS_DONE);

    test_forged_safe_flags(ops, n, 3, Q_FAIL_REPEATED_UNSAFE_EFFECT);
    test_forged_safe_flags(ops, n, 0, Q_FAIL_STATE_MISMATCH);
    test_committed_record_cannot_disappear();

    /* Observable trace must itself carry the evidence the oracle cites. */
    {
        q_ctx *c = q_new(7);
        q_trace t;
        size_t starts = 0, reopens = 0, crashes = 0;
        q_run_ops(c, ops, n, &t);
        for (size_t i = 0; i < t.count; i++) {
            if (t.ev[i].kind == Q_EV_EFFECT_START) starts++;
            if (t.ev[i].kind == Q_EV_REOPEN) reopens++;
            if (t.ev[i].kind == Q_EV_CRASH) crashes++;
        }
        assert(starts == 1);
        assert(reopens == 1);
        assert(crashes == 1);
        q_free(c);
    }

    printf("oracle-mutant: 8 verdicts checked\n");
    return 0;
}