Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions kernelguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
# Pattern 1: Timer monkey-patching
RE_CUDA_EVENT_ASSIGN = re.compile(r'torch\.cuda\.Event\s*=\s*')
RE_CUDA_EVENT_SETATTR = re.compile(r'setattr\s*\(\s*torch\.cuda\s*,\s*[\'"]Event[\'"]\s*,')
RE_TIME_MODULE_ATTR_ASSIGN = re.compile(
r'\btime\.(perf_counter|perf_counter_ns|monotonic|monotonic_ns|process_time|time)\s*=\s*'
)
RE_TIME_MODULE_SETATTR = re.compile(
r'setattr\s*\(\s*time\s*,\s*[\'"](perf_counter|perf_counter_ns|monotonic|monotonic_ns|process_time|time)[\'"]\s*,'
)
RE_ELAPSED_TIME_METHOD_PATCH = re.compile(r'torch\.cuda\.Event\.elapsed_time\s*=')
RE_CUDA_SYNCHRONIZE_PATCH = re.compile(r'torch\.cuda\.synchronize\s*=')
RE_DUMMY_FAKE_EVENT = re.compile(r'class\s+_?(?:Dummy|Fake|Mock|Stub|Noop)\w*Event', re.IGNORECASE)
Expand Down Expand Up @@ -586,6 +592,20 @@ def detect_timer_monkeypatch(code: str | SubmissionFacts) -> list[dict]:
"severity": "critical",
"evidence": "torch.cuda.synchronize patched",
})
if RE_TIME_MODULE_ATTR_ASSIGN.search(code):
m = RE_TIME_MODULE_ATTR_ASSIGN.search(code)
matches.append({
"pattern": "TIMER_MONKEYPATCH",
"severity": "critical",
"evidence": f"time.{m.group(1)} reassigned (host-clock monkeypatch)",
})
if RE_TIME_MODULE_SETATTR.search(code):
m = RE_TIME_MODULE_SETATTR.search(code)
matches.append({
"pattern": "TIMER_MONKEYPATCH",
"severity": "critical",
"evidence": f"setattr(time, '{m.group(1)}', ...) patching",
})
if RE_DUMMY_FAKE_EVENT.search(code):
matches.append({
"pattern": "TIMER_MONKEYPATCH",
Expand Down