test_context_conflicts.py¶
tests/test_context_conflicts.py
gh#47 part 2: what an interrupt handler reaches that it may not reach.
test_a_blocking_lock_and_a_blocking_call_in_the_closure_are_conflicts
None test_context_conflicts.test_a_blocking_lock_and_a_blocking_call_in_the_closure_are_conflicts(tmp_path)
Both conflict reasons are found, with the path depth recorded.
tests/test_context_conflicts.py:111
Checks
assert lock[0] == VERDICT_CONFLICTassert lock[2] == 1, f"drain is one hop from the handler, got depth {lock[2]}"assert lock[3] == "medium", f"a one-hop path through an ast edge is medium, got {lock[3]}"assert call[0] == VERDICT_CONFLICTassert "K_FOREVER" in call[1], f"the detail must carry the timeout as written, got {call[1]}"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-011 | measured | test_a_blocking_lock_and_a_blocking_call_in_the_closure_are_conflicts → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-001 | measured | test_a_blocking_lock_and_a_blocking_call_in_the_closure_are_conflicts → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-007 | measured | test_a_blocking_lock_and_a_blocking_call_in_the_closure_are_conflicts → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-011 | measured | test_a_blocking_lock_and_a_blocking_call_in_the_closure_are_conflicts → extract_context_conflicts |
| REQ-DDB-SCHEMA-012 | measured |
Source · 21 lines
def test_a_blocking_lock_and_a_blocking_call_in_the_closure_are_conflicts(tmp_path) -> None:
"""The two arms of the rule, one hop from the handler. `drain` is not itself an interrupt
handler — it is reachable from one, which is the whole question a firmware engineer asks.
@brief Both conflict reasons are found, with the path depth recorded.
@version 1
"""
db = _context_db(tmp_path)
extract_context_conflicts(db)
rows = _rows(db)
lock = rows[("blocking_lock", "bus_lock", 40)]
assert lock[0] == VERDICT_CONFLICT
assert lock[2] == 1, f"drain is one hop from the handler, got depth {lock[2]}"
assert lock[3] == "medium", f"a one-hop path through an ast edge is medium, got {lock[3]}"
call = rows[("blocking_call", "k_sem_take", 41)]
assert call[0] == VERDICT_CONFLICT
assert "K_FOREVER" in call[1], f"the detail must carry the timeout as written, got {call[1]}"
Activity · 10 steps
test_a_deferred_handler_is_not_inside_the_interrupt
None test_context_conflicts.test_a_deferred_handler_is_not_inside_the_interrupt(tmp_path)
A binding edge is not an interrupt-context path.
tests/test_context_conflicts.py:172
Checks
assert ("blocking_call", "k_sem_take", 80) not in rows, ("deferred_work runs on a workqueue thread, where blocking is legal" )assert ("blocking_call", "vTaskDelay", 95) not in rows, "task_loop is not an interrupt path"assert ("blocking_lock", "bus_lock", 90) not in rows, "task_loop is not an interrupt path"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-011 | measured | test_a_deferred_handler_is_not_inside_the_interrupt → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-001 | measured | test_a_deferred_handler_is_not_inside_the_interrupt → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-007 | measured | test_a_deferred_handler_is_not_inside_the_interrupt → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-011 | measured | test_a_deferred_handler_is_not_inside_the_interrupt → _run → extract_context_conflicts |
| REQ-DDB-SCHEMA-012 | measured |
Source · 20 lines
def test_a_deferred_handler_is_not_inside_the_interrupt(tmp_path) -> None:
"""`binding` means "a reference someone else will call later, at a time and on a thread this
edge says nothing about". Getting work OUT of an interrupt by binding a handler is the
pattern every RTOS documents, so traversing that edge would report the correct pattern as
the violation — and thread membership traverses it, which is why this closure does not.
`task_loop` is the control: it takes the same mutex and is not interrupt-reachable at all.
@brief A binding edge is not an interrupt-context path.
@version 1
"""
rows = _rows(_run(tmp_path))
assert ("blocking_call", "k_sem_take", 80) not in rows, (
"deferred_work runs on a workqueue thread, where blocking is legal"
)
assert ("blocking_call", "vTaskDelay", 95) not in rows, "task_loop is not an interrupt path"
assert ("blocking_lock", "bus_lock", 90) not in rows, "task_loop is not an interrupt path"
Activity · 4 steps
test_a_path_through_a_multiply_defined_name_says_so
None test_context_conflicts.test_a_path_through_a_multiply_defined_name_says_so(tmp_path)
An ambiguous hop lowers confidence and is named in the row.
tests/test_context_conflicts.py:251
Checks
assert key in rows, f"the conflict is still reported, got {rows}"assert verdict == VERDICT_CONFLICTassert confidence == "low", (f"a path through a name with several definitions is not exact; got {confidence}" )assert "bus_acquire" in detail and "2" in detail, (f"the row must name the ambiguous hop and how many definitions it has; got {detail!r}" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-011 | measured | test_a_path_through_a_multiply_defined_name_says_so → _ported_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-001 | measured | test_a_path_through_a_multiply_defined_name_says_so → _ported_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-007 | measured | test_a_path_through_a_multiply_defined_name_says_so → _ported_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-011 | measured | test_a_path_through_a_multiply_defined_name_says_so → extract_context_conflicts |
| REQ-DDB-SCHEMA-012 | measured |
Source · 31 lines
def test_a_path_through_a_multiply_defined_name_says_so(tmp_path) -> None:
"""MEASURED ON RIOT: `spi_acquire` has FIVE definitions, one per CPU port, and doxygen
emitted exactly ONE edge for the driver's call — to `cpu/atmega_common/periph/spi.c`,
labelled `exact`. An nRF radio driver was therefore reported as reaching atmega's SPI
implementation, and msp430's USART, with a path that reads as precise and is arbitrary: the
index resolved a name, the LINKER resolves the port, and nothing in the tree says which
build is meant. 251 of 2470 definition names in that index are multiply defined.
The finding is not wrong — every one of those implementations does take a mutex, and the
handler does reach one of them — but the row must say WHICH CLAIM IT IS MAKING. So a path
through a multiply-defined name is reported at low confidence, naming the count.
@brief An ambiguous hop lowers confidence and is named in the row.
@version 1
"""
db = _ported_db(tmp_path)
extract_context_conflicts(db)
rows = _rows(db)
key = ("blocking_call", "mutex_lock", 7)
assert key in rows, f"the conflict is still reported, got {rows}"
verdict, detail, _depth, confidence = rows[key]
assert verdict == VERDICT_CONFLICT
assert confidence == "low", (
f"a path through a name with several definitions is not exact; got {confidence}"
)
assert "bus_acquire" in detail and "2" in detail, (
f"the row must name the ambiguous hop and how many definitions it has; got {detail!r}"
)
Activity · 9 steps
test_the_handler_itself_answers_for_what_its_closure_reaches
None test_context_conflicts.test_the_handler_itself_answers_for_what_its_closure_reaches(tmp_path)
An ISR entry reports the conflicts of the thread it owns.
tests/test_context_conflicts.py:282
Called by none
Calls _context_db, context_conflicts_for_rowids, context_undecidable_for_rowids, extract_context_conflicts
Checks
assert on_handler, "the handler must report what its own closure reaches"assert {c.evidence for c in on_handler} == {c.evidence for c in on_helper}, ("the handler's answer is its thread's findings, not a different set" )assert undecidable == 3, f"and the refusals of that thread too, got {undecidable}"assert on_task == [], "a function outside any interrupt path still answers for itself"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-011 | measured | test_the_handler_itself_answers_for_what_its_closure_reaches → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-001 | measured | test_the_handler_itself_answers_for_what_its_closure_reaches → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-007 | measured | test_the_handler_itself_answers_for_what_its_closure_reaches → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-011 | measured | test_the_handler_itself_answers_for_what_its_closure_reaches → extract_context_conflicts |
| REQ-DDB-SCHEMA-012 | measured |
Source · 35 lines
def test_the_handler_itself_answers_for_what_its_closure_reaches(tmp_path) -> None:
"""THE QUESTION THE FEATURE EXISTS FOR, and it was answered wrongly. A conflict is filed
against the function that HOLDS the site — `spi_acquire`, four calls deep — so a dossier on
the interrupt handler, which is the name a firmware engineer actually types, came back with
an empty `context_conflicts` and `context_undecidable: 0`. That reads as a certificate: this
handler reaches nothing that blocks, and nothing was refused.
MEASURED ON RIOT: `nrf24l01p_rx_cb` had 0 rows against its own rowid and 9 against the
thread it owns — including the mutex path this whole layer was built to find.
A handler answers for its closure. Every other function still answers for itself.
@brief An ISR entry reports the conflicts of the thread it owns.
@version 1
"""
from clew.query.locks import context_conflicts_for_rowids, context_undecidable_for_rowids
db = _context_db(tmp_path)
extract_context_conflicts(db)
conn = sqlite3.connect(str(db))
## rowid 1 is the handler; every conflict is filed against rowid 2, the helper it calls.
on_handler = context_conflicts_for_rowids(conn, [1])
undecidable = context_undecidable_for_rowids(conn, [1])
on_helper = context_conflicts_for_rowids(conn, [2])
## The control: a function that is not interrupt-reachable answers for itself only.
on_task = context_conflicts_for_rowids(conn, [4])
conn.close()
assert on_handler, "the handler must report what its own closure reaches"
assert {c.evidence for c in on_handler} == {c.evidence for c in on_helper}, (
"the handler's answer is its thread's findings, not a different set"
)
assert undecidable == 3, f"and the refusals of that thread too, got {undecidable}"
assert on_task == [], "a function outside any interrupt path still answers for itself"1 more lines, not shown
Activity · 13 steps
test_the_index_says_when_it_cannot_decide
None test_context_conflicts.test_the_index_says_when_it_cannot_decide(tmp_path)
Undecidable sites are counted with their reason.
tests/test_context_conflicts.py:147
Checks
assert opaque[0] == VERDICT_UNDECIDABLEassert opaque[1] == "cfg->wait", f"the operand is kept verbatim, got {opaque[1]}"assert guarded[0] == VERDICT_UNDECIDABLEassert "k_is_in_isr" in guarded[1], f"the guard is shown, not evaluated; got {guarded[1]}"assert unresolved[0] == VERDICT_UNDECIDABLE
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-011 | measured | test_the_index_says_when_it_cannot_decide → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-001 | measured | test_the_index_says_when_it_cannot_decide → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-007 | measured | test_the_index_says_when_it_cannot_decide → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-011 | measured | test_the_index_says_when_it_cannot_decide → _run → extract_context_conflicts |
| REQ-DDB-SCHEMA-012 | measured |
Source · 25 lines
def test_the_index_says_when_it_cannot_decide(tmp_path) -> None:
"""THE THIRD OUTCOME. An opaque timeout and a runtime context guard are recorded as
UNDECIDABLE, never as clean and never as a conflict: promoting either fabricates a finding,
dropping it makes an empty conflict list a claim the index has not earned.
Branch polarity is deliberately not evaluated — `if (k_is_in_isr())` may guard the safe path
or the unsafe one — so the condition is reported and the reader decides.
@brief Undecidable sites are counted with their reason.
@version 1
"""
rows = _rows(_run(tmp_path))
opaque = rows[("undecidable_timeout", "k_sem_take", 43)]
assert opaque[0] == VERDICT_UNDECIDABLE
assert opaque[1] == "cfg->wait", f"the operand is kept verbatim, got {opaque[1]}"
guarded = rows[("guarded_by_isr_predicate", "k_sleep", 45)]
assert guarded[0] == VERDICT_UNDECIDABLE
assert "k_is_in_isr" in guarded[1], f"the guard is shown, not evaluated; got {guarded[1]}"
unresolved = rows[("unresolved_lock_identity", "pthread_mutex_lock", 70)]
assert unresolved[0] == VERDICT_UNDECIDABLE
Activity · 9 steps
test_what_an_interrupt_handler_is_supposed_to_do_is_not_a_conflict
None test_context_conflicts.test_what_an_interrupt_handler_is_supposed_to_do_is_not_a_conflict(tmp_path)
Interrupt-mask, spinlock and K_NO_WAIT sites produce no row.
tests/test_context_conflicts.py:132
Checks
assert ("blocking_lock", "irq_disable", 50) not in rows, "masking interrupts is the point"assert ("blocking_lock", "ring", 60) not in rows, "a spinlock does not park the caller"assert not [key for key in rows if key[2] == 42], "K_NO_WAIT is legal in an interrupt"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-011 | measured | test_what_an_interrupt_handler_is_supposed_to_do_is_not_a_conflict → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-001 | measured | test_what_an_interrupt_handler_is_supposed_to_do_is_not_a_conflict → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-007 | measured | test_what_an_interrupt_handler_is_supposed_to_do_is_not_a_conflict → _run → _context_db → _ensure_threads_tables |
| REQ-DDB-SCHEMA-011 | measured | test_what_an_interrupt_handler_is_supposed_to_do_is_not_a_conflict → _run → extract_context_conflicts |
| REQ-DDB-SCHEMA-012 | measured |
Source · 15 lines
def test_what_an_interrupt_handler_is_supposed_to_do_is_not_a_conflict(tmp_path) -> None:
"""Masking interrupts, taking a spinlock, and taking a semaphore with a zero timeout are all
correct interrupt code. A rule that flags them is worse than no rule: it buries the two real
findings above in noise proportional to the size of the tree.
@brief Interrupt-mask, spinlock and K_NO_WAIT sites produce no row.
@version 1
"""
rows = _rows(_run(tmp_path))
assert ("blocking_lock", "irq_disable", 50) not in rows, "masking interrupts is the point"
assert ("blocking_lock", "ring", 60) not in rows, "a spinlock does not park the caller"
assert not [key for key in rows if key[2] == 42], "K_NO_WAIT is legal in an interrupt"