Skip to content

Commit 7f0f1ab

Browse files
committed
fix(security): restore cooperative cancellation and bound the derived-view cache
Addresses both blocking findings on this PR. Both reproduced first; both were real. Cancellation. Seeking the next candidate jumps over the `offset % 4096` checkpoints a character-by-character walk would hit, so a long scan could not be cancelled. On the reviewer's reproducer -- `'aᅟa ' * 8192` with a callback raising on its second call -- main aborts and this branch called the callback once. `_check_skipped_checkpoints` now runs one check per checkpoint crossed, reproducing the walk's cadence exactly: verified equal to `len // 4096` at 0, 4095, 4096, 10k, 32k and 100k characters. The whole-string early-return path had the same gap and is covered too. Memory. The derived-view cache was bounded by entry count, which bounds nothing when normalization expands its input -- NFKC turns one U+FDFA into 18 characters, each carrying a four-byte offset. It is now bounded by stored characters (4M budget) and declines to retain any single view larger than the whole budget. Forty expanding inputs settle at 2.88M stored characters instead of growing without limit. Retention after a scan. clear_security_text_caches() releases the view cache and the predicate caches, and is called from cleanup_result alongside the existing clear_python_ast_cache. That also releases the text keys the predicate caches hold, so a long-lived scanner process no longer keeps a scanned file's content alive after the scan that produced it. Performance is preserved. Full suite, 901 real skills, two alternating runs per arm against upstream main: p95 -10.3%, p99 -12.7%, total scan time -2.6%. Findings remain byte-identical (8,152 on both arms), as do coverage-ledger outcomes (176) and error counts (0). The mean gain narrows from -4.1% to -2.6%, which is the cost of the restored checkpoints and is the right trade. Thirteen new tests cover the checkpoint cadence at six lengths, cancellation across sparse in-word gaps and on text with no candidates, the size bound, the oversized-single-view case, clearing, teardown via cleanup_result, and that an evicted-then-rebuilt view is identical to the original. Full suite: 5821 passed. Signed-off-by: Steven Moy <[email protected]>
1 parent 9dc3c20 commit 7f0f1ab

3 files changed

Lines changed: 224 additions & 4 deletions

File tree

‎src/skillspector/artifacts.py‎

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import re
1515
import unicodedata
1616
from array import array
17+
from collections import OrderedDict
1718
from collections.abc import Callable, Iterator
1819
from dataclasses import dataclass
1920
from enum import StrEnum
@@ -1577,6 +1578,21 @@ def _is_token_gap_character(ch: str) -> bool:
15771578
return _compute_token_gap_character(ch)
15781579

15791580

1581+
_RUNTIME_CHECKPOINT_STRIDE = 4096
1582+
1583+
1584+
def _check_skipped_checkpoints(check_runtime: Callable[[], None], start: int, end: int) -> None:
1585+
"""Run the cooperative checks a character-by-character walk would have run.
1586+
1587+
Seeking jumps straight to the next candidate, so the ``offset % 4096``
1588+
checkpoints between ``start`` and ``end`` would otherwise never fire and a
1589+
long scan could not be cancelled. Invoke one per checkpoint crossed, which
1590+
matches the cadence of the walk it replaces.
1591+
"""
1592+
for _ in range(start // _RUNTIME_CHECKPOINT_STRIDE + 1, end // _RUNTIME_CHECKPOINT_STRIDE + 1):
1593+
check_runtime()
1594+
1595+
15801596
def _token_bridging_gap_spans(
15811597
text: str,
15821598
*,
@@ -1585,14 +1601,20 @@ def _token_bridging_gap_spans(
15851601
) -> Iterator[tuple[int, int]]:
15861602
"""Yield contextual noise runs in one pass without crossing ASCII spaces."""
15871603
if _TOKEN_GAP_CANDIDATE.search(text) is None:
1604+
if check_runtime is not None:
1605+
_check_skipped_checkpoints(check_runtime, 0, len(text))
15881606
return
15891607
offset = 0
15901608
while offset < len(text):
15911609
if check_runtime is not None and offset % 4096 == 0:
15921610
check_runtime()
15931611
seek = _TOKEN_GAP_SEEK.search(text, offset)
15941612
if seek is None:
1613+
if check_runtime is not None:
1614+
_check_skipped_checkpoints(check_runtime, offset, len(text))
15951615
return
1616+
if check_runtime is not None:
1617+
_check_skipped_checkpoints(check_runtime, offset, seek.start())
15961618
offset = seek.start()
15971619
if not _is_token_gap_character(text[offset]):
15981620
offset += 1
@@ -1747,14 +1769,93 @@ def _next_offset(offsets: Iterator[int]) -> int | None:
17471769
return next(offsets, None)
17481770

17491771

1750-
@lru_cache(maxsize=_TEXT_PREDICATE_CACHE_SIZE)
1772+
# Derived views can be far larger than their input, so this cache is bounded by
1773+
# stored characters rather than entries. The budget is a few files' worth of
1774+
# expanded text -- enough for the analyzers that revisit one file, small enough
1775+
# that a long-lived scanner process cannot accumulate.
1776+
_DERIVED_VIEW_CACHE_BUDGET_CHARS = 4_000_000
1777+
1778+
1779+
class _SizeBoundedViewCache:
1780+
"""A small insertion-ordered cache bounded by total stored characters."""
1781+
1782+
def __init__(self, budget: int) -> None:
1783+
self._budget = budget
1784+
self._entries: OrderedDict[str, SecurityTextView] = OrderedDict()
1785+
self._sizes: dict[str, int] = {}
1786+
self._total = 0
1787+
1788+
def get(self, key: str) -> SecurityTextView | None:
1789+
view = self._entries.get(key)
1790+
if view is not None:
1791+
self._entries.move_to_end(key)
1792+
return view
1793+
1794+
def store(self, key: str, view: SecurityTextView, size: int) -> None:
1795+
if size > self._budget:
1796+
# A single view larger than the whole budget is never worth keeping.
1797+
return
1798+
if key in self._entries:
1799+
return
1800+
self._entries[key] = view
1801+
self._sizes[key] = size
1802+
self._total += size
1803+
while self._total > self._budget and self._entries:
1804+
evicted, _ = self._entries.popitem(last=False)
1805+
self._total -= self._sizes.pop(evicted, 0)
1806+
1807+
def clear(self) -> None:
1808+
self._entries.clear()
1809+
self._sizes.clear()
1810+
self._total = 0
1811+
1812+
@property
1813+
def stored_chars(self) -> int:
1814+
return self._total
1815+
1816+
1817+
_NORMALIZED_VIEW_CACHE = _SizeBoundedViewCache(_DERIVED_VIEW_CACHE_BUDGET_CHARS)
1818+
1819+
1820+
def clear_security_text_caches() -> None:
1821+
"""Release every memoized security-text derivation.
1822+
1823+
Called from scan teardown so a long-lived scanner process does not retain a
1824+
scanned file's content -- the derived views, and the text keys the predicate
1825+
caches hold -- after the scan that produced it has finished.
1826+
"""
1827+
_NORMALIZED_VIEW_CACHE.clear()
1828+
for cached in (
1829+
_has_letter_spacing_run,
1830+
_has_obfuscated_instruction,
1831+
_requires_normalized_security_view,
1832+
_letter_spacing_run_spans_cached,
1833+
):
1834+
cached.cache_clear()
1835+
1836+
17511837
def normalized_security_view(text: str) -> SecurityTextView:
17521838
"""Build an NFKC/UTS #39 ASCII-skeleton view with compact offsets.
17531839
1754-
Memoized: every analyzer reaches this with the same file content. The view
1755-
is a frozen dataclass and its ``source_offsets`` array is only ever read --
1756-
sliced, or copied into a fresh array -- so callers can share one instance.
1840+
Memoized, because every analyzer reaches this with the same file content.
1841+
The view is a frozen dataclass and its ``source_offsets`` array is only ever
1842+
read -- sliced, or copied into a fresh array -- so callers can share one
1843+
instance.
1844+
1845+
The cache is bounded by the *size* of what it stores rather than by entry
1846+
count. Normalization can expand its input several-fold -- NFKC turns a
1847+
single U+FDFA into 18 characters, each carrying a four-byte offset -- so a
1848+
count-based bound places no limit on retained memory.
17571849
"""
1850+
cached = _NORMALIZED_VIEW_CACHE.get(text)
1851+
if cached is not None:
1852+
return cached
1853+
view = _build_normalized_security_view(text)
1854+
_NORMALIZED_VIEW_CACHE.store(text, view, len(view.text))
1855+
return view
1856+
1857+
1858+
def _build_normalized_security_view(text: str) -> SecurityTextView:
17581859
output = StringIO()
17591860
offsets = array("I")
17601861
contextual_spans = iter(_normalization_ignored_spans(text))

‎src/skillspector/cleanup.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
import shutil
77

8+
from skillspector.artifacts import clear_security_text_caches
89
from skillspector.python_ast import clear_python_ast_cache
910

1011

1112
def cleanup_result(result: dict[str, object]) -> None:
1213
"""Release scan-local resources and remove a temp dir if set."""
14+
clear_security_text_caches()
1315
python_ast_cache_key = result.get("python_ast_cache_key")
1416
clear_python_ast_cache(python_ast_cache_key if isinstance(python_ast_cache_key, str) else None)
1517
temp_dir = result.get("temp_dir_for_cleanup")
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Cooperative cancellation and cache-bound tests for the security scan.
5+
6+
Both properties regressed when the scan stopped walking character by character:
7+
seeking jumps over the periodic checkpoints a walk would hit, and memoizing a
8+
derived view retains far more than the entry count suggests because
9+
normalization can expand its input several-fold. These tests pin both.
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import pytest
15+
16+
from skillspector.artifacts import (
17+
_DERIVED_VIEW_CACHE_BUDGET_CHARS,
18+
_NORMALIZED_VIEW_CACHE,
19+
_RUNTIME_CHECKPOINT_STRIDE,
20+
_token_bridging_gap_spans,
21+
clear_security_text_caches,
22+
normalized_security_view,
23+
)
24+
25+
26+
class _AbortError(Exception):
27+
pass
28+
29+
30+
@pytest.fixture(autouse=True)
31+
def _clear():
32+
clear_security_text_caches()
33+
yield
34+
clear_security_text_caches()
35+
36+
37+
@pytest.mark.parametrize("length", [0, 4095, 4096, 10_000, 32_768, 100_000])
38+
def test_checkpoint_cadence_matches_a_character_walk(length: int) -> None:
39+
"""Seeking must fire the checks a character-by-character walk would fire."""
40+
fired = 0
41+
42+
def check() -> None:
43+
nonlocal fired
44+
fired += 1
45+
46+
list(_token_bridging_gap_spans("a" * length, check_runtime=check))
47+
assert fired == length // _RUNTIME_CHECKPOINT_STRIDE
48+
49+
50+
def test_cancellation_is_observed_across_sparse_gaps() -> None:
51+
"""In-word gaps that yield no spans must still reach the runtime check."""
52+
calls = 0
53+
54+
def check() -> None:
55+
nonlocal calls
56+
calls += 1
57+
if calls >= 2:
58+
raise _AbortError
59+
60+
with pytest.raises(_AbortError):
61+
list(_token_bridging_gap_spans("aᅟa " * 8192, check_runtime=check))
62+
63+
64+
def test_cancellation_is_observed_on_text_with_no_candidates() -> None:
65+
"""The whole-string skip must not swallow cancellation either."""
66+
calls = 0
67+
68+
def check() -> None:
69+
nonlocal calls
70+
calls += 1
71+
if calls >= 2:
72+
raise _AbortError
73+
74+
with pytest.raises(_AbortError):
75+
list(_token_bridging_gap_spans("a" * 100_000, check_runtime=check))
76+
77+
78+
def test_derived_view_cache_is_bounded_by_size_not_entry_count() -> None:
79+
"""NFKC expands U+FDFA to 18 characters, so entry count bounds nothing."""
80+
chunk = "ﷺ" * 4000
81+
for index in range(40):
82+
normalized_security_view(f"{index} {chunk}")
83+
assert _NORMALIZED_VIEW_CACHE.stored_chars <= _DERIVED_VIEW_CACHE_BUDGET_CHARS
84+
85+
86+
def test_a_single_oversized_view_is_not_retained() -> None:
87+
oversized = "ﷺ" * (_DERIVED_VIEW_CACHE_BUDGET_CHARS // 10)
88+
normalized_security_view(oversized)
89+
assert _NORMALIZED_VIEW_CACHE.stored_chars == 0
90+
91+
92+
def test_clearing_releases_everything() -> None:
93+
normalized_security_view("ﷺ" * 1000)
94+
assert _NORMALIZED_VIEW_CACHE.stored_chars > 0
95+
clear_security_text_caches()
96+
assert _NORMALIZED_VIEW_CACHE.stored_chars == 0
97+
98+
99+
def test_scan_teardown_clears_the_caches() -> None:
100+
"""cleanup_result is the hook that releases scan-local state."""
101+
from skillspector.cleanup import cleanup_result
102+
103+
normalized_security_view("ﷺ" * 1000)
104+
assert _NORMALIZED_VIEW_CACHE.stored_chars > 0
105+
cleanup_result({})
106+
assert _NORMALIZED_VIEW_CACHE.stored_chars == 0
107+
108+
109+
def test_eviction_does_not_change_results() -> None:
110+
"""A view evicted and rebuilt must be identical to the first one."""
111+
text = "ﷺ" * 500
112+
first = normalized_security_view(text)
113+
for index in range(60):
114+
normalized_security_view(f"{index} " + "ﷺ" * 4000)
115+
rebuilt = normalized_security_view(text)
116+
assert rebuilt.text == first.text
117+
assert list(rebuilt.source_offsets or []) == list(first.source_offsets or [])

0 commit comments

Comments
 (0)