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.

clew/critical_sections.py:175

Show on the map →

Called by _ensure_lock_tables, _section_db

Tested by

1 teststest_the_table_is_created_even_for_a_repo_with_no_locks

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
lines 189–205conn.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); """ )

Requirements

REQ-DDB-SCHEMA-011

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.

clew/critical_sections.py:554

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
lines 571–571inserted = 0lines 572–572◇ for call in callslines 573–573callee_name, call_line = call[0], call[1]lines 576–576bare = bool(call[2]) if len(call) > 2 else Falselines 577–577candidates = name_to_rowids.get(callee_name, [])lines 578–578rowid, resolution = _resolve_callee(candidates, bare=bare)lines 579–584inserted += 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), ).rowcountlines 585–585return inserted

Requirements

REQ-DDB-SCHEMA-011

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.

clew/critical_sections.py:430

Show on the map →

Called by _section_for

Tested by

1 teststest_an_unnamed_operand_yields_no_section

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))

Activity · 15 steps
lines 450–450block = enclosing(acquire, BLOCK_TYPES)lines 456–456◇ if block is None or (not operand and not global_identity)lines 457–457return Section(None, [], EXTENT_UNRESOLVED)lines 458–458floor = acquire.end_bytelines 459–459releases = _releases(block, src, releaser, operand, floor, global_identity)lines 460–460by_block = _by_block(releases)lines 461–461closing = _closing_release(releases, acquire_block=block, by_block=by_block)lines 462–462◇ if releaser is not None and closing is Nonelines 463–463return Section(None, [], EXTENT_UNRESOLVED)lines 464–464◇ if closing is not Nonelines 465–465ceiling, end_line = closing.start_byte, closing.start_point[0] + 1lines 466–466elselines 467–467ceiling, end_line = block.end_byte, block.end_point[0] + 1lines 468–468calls = _member_calls(block, src, (floor, ceiling), by_block, primitives)lines 469–469return Section(end_line, calls, _extent_confidence(releaser, releases))

Requirements

REQ-DDB-SCHEMA-011