tools/litmus/enumerate.py was written by an independent author from spec 04 §20 alone, with the programs blind (outcome sets removed). check.py compares its allowed sets with tests/litmus/*.lit: 20/20 equal. 21 self-tests derived from the axioms. Both run in CI. The author's spec findings are Q-024.
292 lines
14 KiB
Python
292 lines
14 KiB
Python
"""Self-tests of the axiomatic oracle (tools/litmus/enumerate.py).
|
||
|
||
python3 -m unittest tools/litmus/test_enumerate.py
|
||
|
||
None of these tests uses the expected sets of the litmus corpus. Every
|
||
expected value is derived by hand from spec 04 §20.2-§20.4; the derivation is
|
||
in the comment next to it.
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import unittest
|
||
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
import enumerate as E # noqa: E402
|
||
|
||
AXIOMS = ('coherence', 'atomicity', 'sc', 'no-thin-air')
|
||
|
||
|
||
def allowed(src):
|
||
return {tuple(o) for o in E.enumerate_program(E.parse(src))[0]}
|
||
|
||
|
||
def W(task, loc, o='relaxed'):
|
||
return E.Event('W', task, loc, o)
|
||
|
||
|
||
def R(task, loc, o='relaxed'):
|
||
return E.Event('R', task, loc, o)
|
||
|
||
|
||
def INIT(loc):
|
||
return E.Event('W', None, loc)
|
||
|
||
|
||
def init_sb(events):
|
||
"""RC11 convention used by the oracle: init writes sb-before all events."""
|
||
inits = [i for i, e in enumerate(events) if e.task is None]
|
||
return [(i, j) for i in inits for j in range(len(events)) if j not in inits]
|
||
|
||
|
||
def failing(ex):
|
||
return {k for k, ok in E.axioms(ex).items() if not ok}
|
||
|
||
|
||
class SingleTask(unittest.TestCase):
|
||
"""With one task, hb ⊇ sb and coherence alone forces SC behaviour."""
|
||
|
||
def test_reads_own_store(self):
|
||
# Reading init would give fr(load, store) with sb(store, load):
|
||
# hb;eco reflexive, coherence violated. So only 1.
|
||
self.assertEqual(allowed('init: x=0\nT0:\n store x 1 rlx\n r0 = load x rlx\n'
|
||
'observe: T0.r0\n'), {(1,)})
|
||
|
||
def test_last_store_is_final(self):
|
||
# mo(2-store, 1-store) with sb(1-store, 2-store) is hb;mo reflexive.
|
||
self.assertEqual(allowed('init: x=0\nT0:\n store x 1 rlx\n store x 2 rlx\n'
|
||
'observe: final x\n'), {(2,)})
|
||
|
||
def test_load_before_store_reads_init(self):
|
||
# Reading the sb-later store is an sb ∪ rf cycle (no-thin-air) and
|
||
# also hb;rf reflexive (coherence). So only 0.
|
||
self.assertEqual(allowed('init: x=0\nT0:\n r0 = load x rlx\n store x 1 rlx\n'
|
||
'observe: T0.r0 final x\n'), {(0, 1)})
|
||
|
||
def test_rmw_chain(self):
|
||
# Each add reads its sb-predecessor's write (coherence), wrapping 2^64
|
||
# for sub; xchg returns the old value.
|
||
src = ('init: x=5\nT0:\n r0 = rmw add x 3 rlx\n r1 = rmw sub x 10 rlx\n'
|
||
' r2 = rmw xchg x 7 sc\n r3 = load x acq\nobserve: T0.r0 T0.r1 T0.r2 T0.r3 final x\n')
|
||
self.assertEqual(allowed(src), {(5, 8, (8 - 10) % 2**64, 7, 7)})
|
||
|
||
def test_cas_success_and_failure(self):
|
||
# First cas reads 0 = EXP, writes 1; second reads 1 ≠ 0, fails.
|
||
src = ('init: x=0\nT0:\n r0 = cas x 0 1 rlx rlx\n r1 = cas x 0 2 sc sc\n'
|
||
'observe: T0.r0 T0.r1 final x\n')
|
||
self.assertEqual(allowed(src), {(0, 1, 1)})
|
||
|
||
def test_stated_sets_are_ignored(self):
|
||
# The parser skips allowed:/forbidden: lines, even false ones.
|
||
src = ('init: x=0\nT0:\n store x 1 rlx\n r0 = load x rlx\nobserve: T0.r0\n'
|
||
'allowed: (0) (7)\nforbidden: (1)\n')
|
||
self.assertEqual(allowed(src), {(1,)})
|
||
|
||
|
||
class HandBuiltAxioms(unittest.TestCase):
|
||
"""One hand-built execution per axiom: it violates exactly that axiom."""
|
||
|
||
def test_coherence_corr(self):
|
||
# 0 init x; 1 T0: W x=1; 2,3 T1: R x, R x.
|
||
# rf: 1→2 and 0→3; mo: 0→1. fr(3,1) since 0 mo 1. sb(2,3).
|
||
# hb(2,3) ; eco(3,2) = fr(3,1);rf(1,2): coherence violated.
|
||
# Atomicity: no rmw. SC: no sc event. sb ∪ rf: 0→*, 1→2→3: acyclic.
|
||
ev = [INIT('x'), W('T0', 'x'), R('T1', 'x'), R('T1', 'x')]
|
||
ex = E.make_execution(ev, init_sb(ev) + [(2, 3)], [(1, 2), (0, 3)], [(0, 1)])
|
||
self.assertEqual(failing(ex), {'coherence'})
|
||
|
||
def test_atomicity(self):
|
||
# 0 init x; 1,2 T0 RMW (R,W); 3,4 T1 RMW (R,W); both reads read init.
|
||
# mo: 0→2→4. fr(3,2) (rf(0,3), mo(0,2)); mo(2,4): (3,4) ∈ fr;mo ∩ rmw.
|
||
# Coherence: hb = sb; eco never leads back to an sb-earlier event of
|
||
# the same task (T0's events precede 4 only via eco). sb ∪ rf acyclic.
|
||
ev = [INIT('x'), R('T0', 'x'), W('T0', 'x'), R('T1', 'x'), W('T1', 'x')]
|
||
ex = E.make_execution(ev, init_sb(ev) + [(1, 2), (3, 4)], [(0, 1), (0, 3)],
|
||
[(0, 2), (0, 4), (2, 4)], rmw=[(1, 2), (3, 4)])
|
||
self.assertEqual(failing(ex), {'atomicity'})
|
||
|
||
def test_no_thin_air_lb(self):
|
||
# 0 init x, 1 init y; T0: 2 R x, 3 W y=1; T1: 4 R y, 5 W x=1.
|
||
# rf 5→2, 3→4: sb(2,3) rf(3,4) sb(4,5) rf(5,2) is an sb ∪ rf cycle.
|
||
# All relaxed: sw = ∅, hb = sb. eco relates only same-location
|
||
# events; hb;eco would need hb into a write, then back: 2 hb 3 but
|
||
# eco from 3 (y) only reaches 4, not 2. Coherence holds. No rmw, no sc.
|
||
ev = [INIT('x'), INIT('y'), R('T0', 'x'), W('T0', 'y'), R('T1', 'y'), W('T1', 'x')]
|
||
ex = E.make_execution(ev, init_sb(ev) + [(2, 3), (4, 5)], [(5, 2), (3, 4)],
|
||
[(0, 5), (1, 3)])
|
||
self.assertEqual(failing(ex), {'no-thin-air'})
|
||
|
||
def _sb_execution(self, o):
|
||
# 0 init x, 1 init y; T0: 2 W x=1, 3 R y; T1: 4 W y=1, 5 R x.
|
||
# Both reads read init: fr(3,4), fr(5,2).
|
||
ev = [INIT('x'), INIT('y'), W('T0', 'x', o), R('T0', 'y', o),
|
||
W('T1', 'y', o), R('T1', 'x', o)]
|
||
return E.make_execution(ev, init_sb(ev) + [(2, 3), (4, 5)], [(1, 3), (0, 5)],
|
||
[(0, 2), (1, 4)])
|
||
|
||
def test_psc_cycle_sb_sc(self):
|
||
# All seq_cst: psc ⊇ sb(2,3), fr(3,4), sb(4,5), fr(5,2): a cycle.
|
||
# Reads read the non-release init: no sw, hb = sb; coherence holds
|
||
# (eco never returns to the same task). sb ∪ rf acyclic.
|
||
self.assertEqual(failing(self._sb_execution('seq_cst')), {'sc'})
|
||
|
||
def test_sb_relaxed_consistent(self):
|
||
# Same execution, relaxed: psc is empty; nothing else changes.
|
||
self.assertEqual(failing(self._sb_execution('relaxed')), set())
|
||
|
||
def test_psc_fences(self):
|
||
# SB with relaxed accesses separated by seq_cst fences, both reads
|
||
# reading init. 0 init x, 1 init y; T0: 2 W x, 3 F sc, 4 R y;
|
||
# T1: 5 W y, 6 F sc, 7 R x. psc_b: [F_sc];hb?;scb;hb?;[F_sc]:
|
||
# 3 sb 4 fr 5 sb 6 gives 3 → 6 (scb ∋ fr, hb? = sb on each side), and
|
||
# symmetrically 6 → 3. psc cycle; other axioms hold.
|
||
ev = [INIT('x'), INIT('y'), W('T0', 'x'), E.Event('F', 'T0', None, 'seq_cst'),
|
||
R('T0', 'y'), W('T1', 'y'), E.Event('F', 'T1', None, 'seq_cst'), R('T1', 'x')]
|
||
sb = init_sb(ev) + [(2, 3), (2, 4), (3, 4), (5, 6), (5, 7), (6, 7)]
|
||
ex = E.make_execution(ev, sb, [(1, 4), (0, 7)], [(0, 2), (1, 5)])
|
||
self.assertEqual(failing(ex), {'sc'})
|
||
|
||
def test_release_acquire_sw(self):
|
||
# MP: T0: 2 W x=1 rlx, 3 W y=1 rel; T1: 4 R y acq (reads 3), 5 R x.
|
||
# sw(3,4) by [E⊒rel];rs;rf;[R];[E⊒acq], hence hb(2,5).
|
||
# If 5 reads init: fr(5,2) and hb(2,5): coherence violated.
|
||
ev = [INIT('x'), INIT('y'), W('T0', 'x'), W('T0', 'y', 'release'),
|
||
R('T1', 'y', 'acquire'), R('T1', 'x')]
|
||
sb = init_sb(ev) + [(2, 3), (4, 5)]
|
||
ex = E.make_execution(ev, sb, [(3, 4), (0, 5)], [(0, 2), (1, 3)])
|
||
d = E.derive(ex)
|
||
self.assertTrue(d['sw'].has(3, 4))
|
||
self.assertTrue(d['hb'].has(2, 5))
|
||
self.assertEqual(failing(ex), {'coherence'})
|
||
# Relaxed load of y instead: no sw, and the same rf is consistent.
|
||
ev[4] = R('T1', 'y')
|
||
ex = E.make_execution(ev, sb, [(3, 4), (0, 5)], [(0, 2), (1, 3)])
|
||
self.assertFalse(E.derive(ex)['sw'].has(3, 4))
|
||
self.assertEqual(failing(ex), set())
|
||
|
||
def test_release_sequence_through_rmw(self):
|
||
# 0 init f; T0: 1 W f rel; T1: 2,3 relaxed RMW reading 1;
|
||
# T2: 4 R f acq reading 3. rs ∋ (1,3) = rf(1,2);rmw(2,3), so sw(1,4).
|
||
ev = [INIT('f'), W('T0', 'f', 'release'), R('T1', 'f'), W('T1', 'f'),
|
||
R('T2', 'f', 'acquire')]
|
||
ex = E.make_execution(ev, init_sb(ev) + [(2, 3)], [(1, 2), (3, 4)],
|
||
[(0, 1), (0, 3), (1, 3)], rmw=[(2, 3)])
|
||
d = E.derive(ex)
|
||
self.assertTrue(d['rs'].has(1, 3))
|
||
self.assertTrue(d['sw'].has(1, 4))
|
||
self.assertEqual(failing(ex), set())
|
||
|
||
|
||
class Programs(unittest.TestCase):
|
||
"""Small programs (not from the corpus), outcomes derived by hand."""
|
||
|
||
def test_mutex_two_loads(self):
|
||
# T0 stores x in a critical section; T1 loads x twice in one.
|
||
# Lock order T1 first: unlock(T1) sw lock(T0), so T1's loads hb the
|
||
# store; reading it is hb;rf reflexive → (0,0). T0 first: store hb
|
||
# both loads; reading init gives fr(load, store) with hb(store, load)
|
||
# → (1,1). Without the mutex (0,1) would be allowed (CoRR permits it).
|
||
src = ('init: x=0\nT0:\n lock m\n store x 1 rlx\n unlock m\n'
|
||
'T1:\n lock m\n r0 = load x rlx\n r1 = load x rlx\n unlock m\n'
|
||
'observe: T1.r0 T1.r1\n')
|
||
self.assertEqual(allowed(src), {(0, 0), (1, 1)})
|
||
|
||
def test_channel_fifo(self):
|
||
# FIFO: the first recv gets the first send, the second the second.
|
||
# After recv #1, store x=1 is hb, x=2 may or may not be seen: r0 ∈ {1,2}.
|
||
# After recv #2, store x=2 is hb and mo-last: r1 = 2.
|
||
src = ('init: x=0\nT0:\n store x 1 rlx\n send c\n store x 2 rlx\n send c\n'
|
||
'T1:\n recv c\n r0 = load x rlx\n recv c\n r1 = load x rlx\n'
|
||
'observe: T1.r0 T1.r1\n')
|
||
self.assertEqual(allowed(src), {(1, 2), (2, 2)})
|
||
|
||
def test_spawn_sees_parent_and_join_sees_child(self):
|
||
# The parent's store is hb the child's load (spawn → first event);
|
||
# the child's store is hb the parent's load (last event → join).
|
||
src = ('init: x=0 y=0\nT0:\n store x 1 rlx\n spawn T1\n join T1\n'
|
||
' r1 = load y rlx\nT1 (spawned):\n r0 = load x rlx\n store y 1 rlx\n'
|
||
'observe: T1.r0 T0.r1\n')
|
||
self.assertEqual(allowed(src), {(1, 1)})
|
||
|
||
def test_unspawned_task_does_not_run(self):
|
||
src = ('init: x=0\nT0:\n r0 = load x rlx\nT1 (spawned):\n store x 1 rlx\n'
|
||
'observe: T0.r0 final x\n')
|
||
self.assertEqual(allowed(src), {(0, 0)})
|
||
|
||
def test_three_relaxed_increments(self):
|
||
# Atomicity: every RMW reads its immediate mo-predecessor, so the
|
||
# values read are a permutation of 0,1,2 and the final value is 3.
|
||
src = ('init: x=0\nT0:\n r0 = rmw add x 1 rlx\nT1:\n r1 = rmw add x 1 rlx\n'
|
||
'T2:\n r2 = rmw add x 1 rlx\nobserve: T0.r0 T1.r1 T2.r2 final x\n')
|
||
perms = {(a, b, c, 3) for a in range(3) for b in range(3) for c in range(3)
|
||
if len({a, b, c}) == 3}
|
||
self.assertEqual(allowed(src), perms)
|
||
|
||
def test_invalid_orderings_rejected(self):
|
||
for op in ('store x 1 acq', 'r0 = load x rel', 'fence rlx',
|
||
'r0 = cas x 0 1 rel acq', 'r0 = cas x 0 1 acq sc'):
|
||
with self.assertRaises(ValueError, msg=op):
|
||
E.parse(f'init: x=0\nT0:\n {op}\nobserve: final x\n')
|
||
|
||
|
||
class WellFormedness(unittest.TestCase):
|
||
"""Every consistent execution has well-formed rf and mo (§20.2)."""
|
||
|
||
SOURCES = [
|
||
'init: x=0 y=0\nT0:\n store x 1 rel\n r0 = rmw xchg y 2 ar\nT1:\n'
|
||
' r1 = cas y 0 3 sc acq\n store x 2 sc\nobserve: T0.r0 T1.r1 final x final y\n',
|
||
'init: x=0\nT0:\n r0 = rmw add x 1 rlx\n store x 9 rel\nT1:\n'
|
||
' r1 = load x acq\n r2 = rmw sub x 1 rlx\nobserve: T1.r1 T1.r2\n',
|
||
]
|
||
|
||
def test_rf_mo_well_formed(self):
|
||
for src in self.SOURCES:
|
||
n_cons = 0
|
||
for ex, val, _ in E.candidates(E.parse(src)):
|
||
if not E.consistent(ex):
|
||
continue
|
||
n_cons += 1
|
||
ev = ex.events
|
||
writes = {}
|
||
for i, e in enumerate(ev):
|
||
if e.kind == 'W':
|
||
writes.setdefault(e.loc, []).append(i)
|
||
rf = ex.rf.pairs()
|
||
for i, e in enumerate(ev):
|
||
srcs = [w for w, r in rf if r == i]
|
||
if e.kind == 'R':
|
||
# exactly one source, a write to the same location,
|
||
# whose value the read returns
|
||
self.assertEqual(len(srcs), 1)
|
||
self.assertEqual(ev[srcs[0]].kind, 'W')
|
||
self.assertEqual(ev[srcs[0]].loc, e.loc)
|
||
self.assertEqual(val[i], val[srcs[0]])
|
||
else:
|
||
self.assertEqual(srcs, [])
|
||
mo = ex.mo.pairs()
|
||
for a, b in mo:
|
||
self.assertEqual(ev[a].kind, 'W')
|
||
self.assertEqual(ev[a].loc, ev[b].loc)
|
||
for loc, ws in writes.items():
|
||
init = [w for w in ws if ev[w].task is None]
|
||
self.assertEqual(len(init), 1)
|
||
for a in ws:
|
||
self.assertNotIn((a, a), mo) # strict
|
||
if a != init[0]:
|
||
self.assertIn((init[0], a), mo) # init first
|
||
for b in ws:
|
||
if a != b: # total
|
||
self.assertTrue((a, b) in mo or (b, a) in mo)
|
||
# every rmw read part reads the immediate mo-predecessor of
|
||
# its write part (consequence of atomicity + coherence)
|
||
for r, w in ex.rmw.pairs():
|
||
src_w = next(s for s, x in rf if x == r)
|
||
between = [c for c in writes[ev[w].loc]
|
||
if (src_w, c) in mo and (c, w) in mo]
|
||
self.assertTrue((src_w, w) in mo and not between)
|
||
self.assertGreater(n_cons, 0)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
unittest.main()
|