critical_sections.py¶
clew/critical_sections.py
R1 lock layer, L2: which calls run WHILE a lock is held (task #52).
ensure_section_table
None clew.critical_sections.ensure_section_table(sqlite3.Connection conn)
Create the L2 membership table if it does not exist.
Called by _ensure_lock_tables, _section_db
Tested by
Calls none
Source · 33 lines
def ensure_section_table(conn: sqlite3.Connection) -> None:
"""Created unconditionally alongside the L1 tables — including on a repo
with no locks — so R2 and R4 never branch
on table existence. That is the same contract `locks`/`lock_acquisitions`
already give, and the requirements-table precedent both follow.
`callee_rowid` is NULLABLE and deliberately outside the UNIQUE key: a call
site is a physical location, identified by (acquisition, line, name), and
letting an unresolved rowid participate in dedup would file the same site
twice under two different resolutions.
@brief Create critical_section_calls.
@version 2
"""
conn.executescript(
f"""
CREATE TABLE IF NOT EXISTS critical_section_calls (
id INTEGER PRIMARY KEY,
acquisition_id INTEGER NOT NULL REFERENCES lock_acquisitions(id),
callee_rowid INTEGER REFERENCES memberdef(rowid),
callee_name TEXT NOT NULL,
call_line INTEGER NOT NULL,
resolution TEXT NOT NULL {check("critical_section_calls", "resolution")},
UNIQUE(acquisition_id, call_line, callee_name)
);
CREATE INDEX IF NOT EXISTS idx_section_calls_acq
ON critical_section_calls(acquisition_id);
CREATE INDEX IF NOT EXISTS idx_section_calls_callee
ON critical_section_calls(callee_rowid);
"""
)
Activity · 1 steps
Requirements
insert_section_calls
int clew.critical_sections.insert_section_calls(sqlite3.Connection conn, int acquisition_id, list[list[Any]] calls, dict[str, list[int]] name_to_rowids)
Persist one acquisition's membership rows.
Called by _insert_sites
Tested by 6 tests
test_a_callee_with_no_memberdef_is_external_not_fuzzy, test_a_uniquely_named_BARE_callee_resolves_to_its_rowid, test_a_uniquely_named_MEMBER_callee_does_NOT_resolve, test_an_absent_bare_flag_reads_as_MEMBER_ish, test_an_ambiguous_callee_is_ONE_row_with_no_rowid_not_one_per_candidate, test_the_same_call_site_is_not_recorded_twice
Calls _resolve_callee
Source · 34 lines
def insert_section_calls(
conn: sqlite3.Connection,
acquisition_id: int,
calls: list[list[Any]],
name_to_rowids: dict[str, list[int]],
) -> int:
"""A NAME matching several memberdefs does NOT fan out into several rows the
way `call_edges` does. A call site is one physical call, so N rows would
answer "what runs under this lock" with N functions where one runs, and
nothing would mark the N-1 as invented. The row stays single, `callee_rowid`
stays NULL, and `resolution` records that the name was ambiguous — the same
never-borrow-another-symbol's-rowid rule the thread layer follows.
@brief Insert membership rows, resolving callee names conservatively.
@return Rows inserted.
@version 2
"""
inserted = 0
for call in calls:
callee_name, call_line = call[0], call[1]
## Tolerate a 2-element payload: an index cached before the shape gained the
## bare flag replays as member-ish, which is the conservative reading.
bare = bool(call[2]) if len(call) > 2 else False
candidates = name_to_rowids.get(callee_name, [])
rowid, resolution = _resolve_callee(candidates, bare=bare)
inserted += conn.execute(
"INSERT OR IGNORE INTO critical_section_calls "
"(acquisition_id, callee_rowid, callee_name, call_line, resolution) "
"VALUES (?, ?, ?, ?, ?)",
(acquisition_id, rowid, callee_name, call_line, resolution),
).rowcount
return inserted
Activity · 8 steps
Requirements
resolve_section
Section clew.critical_sections.resolve_section(Any acquire, bytes src, str|None releaser, str operand, frozenset[str] primitives=frozenset(), bool global_identity=False)
Resolve one acquisition's critical section from the AST.
Called by _section_for
Tested by
Calls _by_block, _closing_release, _extent_confidence, _member_calls, _releases, enclosing
Source · 42 lines
def resolve_section(
acquire: Any,
src: bytes,
releaser: str | None,
operand: str,
primitives: frozenset[str] = frozenset(),
global_identity: bool = False,
) -> Section:
"""The single entry point L1 calls, so extent and membership are two outputs
of ONE analysis and cannot drift apart.
An RAII hold ends with its block, by language rule, and is reported 'high'.
An explicit pair ends at its fall-through release: 'high' when nothing had to
be reasoned past, 'medium' when a jump-terminated branch release was walked
over — that inference is load-bearing there and a consumer should be able to
see it. No fall-through release means 'low', a NULL extent and no rows.
@brief Resolve extent + membership for one acquisition.
@version 2
"""
block = enclosing(acquire, BLOCK_TYPES)
## gh#47. AN EMPTY OPERAND IS NOT ALWAYS A FAILURE TO RESOLVE ONE. An operand-less primitive
## has no operand to find, so refusing here reported every `irq_disable()` hold as
## extentless — measured at 21 of 21 across four RIOT `core/` files. The fail-closed rule
## below is untouched: no fall-through release still means no extent, so pairing by name
## cannot become "the rest of the function" on no evidence.
if block is None or (not operand and not global_identity):
return Section(None, [], EXTENT_UNRESOLVED)
floor = acquire.end_byte
releases = _releases(block, src, releaser, operand, floor, global_identity)
by_block = _by_block(releases)
closing = _closing_release(releases, acquire_block=block, by_block=by_block)
if releaser is not None and closing is None:
return Section(None, [], EXTENT_UNRESOLVED)
if closing is not None:
ceiling, end_line = closing.start_byte, closing.start_point[0] + 1
else:
ceiling, end_line = block.end_byte, block.end_point[0] + 1
calls = _member_calls(block, src, (floor, ceiling), by_block, primitives)
return Section(end_line, calls, _extent_confidence(releaser, releases))