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
96 changes: 95 additions & 1 deletion kernelguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,94 @@ def detect_precision_downgrade(code: str | SubmissionFacts) -> list[dict]:
return matches


def detect_class_entrypoint_replay(code: str | SubmissionFacts) -> list[dict]:
"""Pattern: class __call__ entrypoint with stateful replay.

Catches RED #43:
class custom_kernel:
@staticmethod
def __call__(data):
global _NODE
if _NODE is not None and _NODE[0].data_ptr() == data.data_ptr():
return _NODE[1]

No 'def custom_kernel(' exists — entrypoint is a class with __call__.
"""
facts = ensure_submission_facts(code)
tree = facts.ast_tree
if tree is None:
return []

for node in facts._class_defs:
if not is_entrypoint_name(node.name):
continue
for item in node.body:
if not isinstance(item, ast.FunctionDef) or item.name != "__call__":
continue

for child in ast.walk(item):
if not isinstance(child, ast.If):
continue
if not isinstance(child.test, ast.BoolOp):
continue
if _body_has_calls(child.body):
continue

has_is_none = False
has_data_ptr = False

for operand in child.test.values:
if isinstance(operand, ast.Compare):
for op in operand.ops:
if isinstance(op, (ast.Is, ast.IsNot)):
left = operand.left
for right in operand.comparators:
none_side = None
name_side = None
if isinstance(left, ast.Constant) and left.value is None:
none_side = left; name_side = right
elif isinstance(right, ast.Constant) and right.value is None:
none_side = right; name_side = left
if none_side is not None and isinstance(name_side, ast.Name):
has_is_none = True
break
break

if isinstance(operand, ast.Compare):
for op in operand.ops:
if isinstance(op, ast.Eq):
if (
_expr_has_data_ptr_fast(operand.left, facts._nodes_with_data_ptr)
and operand.comparators
and _expr_has_data_ptr_fast(operand.comparators[0], facts._nodes_with_data_ptr)
):
has_data_ptr = True
break

if not (has_is_none and has_data_ptr):
continue

returned = {
_ast_root_name(stmt.value)
for stmt in child.body
if isinstance(stmt, ast.Return) and stmt.value is not None
} - {None}
if not returned:
continue
if not any(r in facts._none_inited for r in returned):
continue

return [{
"pattern": "CLASS_ENTRYPOINT_REPLAY",
"severity": "critical",
"evidence": (
f"class '{node.name}.__call__' entrypoint with "
f"is-None sentinel + data_ptr replay state"
),
}]
return []


# ---------------------------------------------------------------------------
# Score anomaly detection
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -3645,6 +3733,10 @@ class RulePolicy:
"SCORE_EXTREME_SPEEDUP", "score_anomaly", "support", SUSPICIOUS_ONLY, (),
(), "downgrade",
),
"CLASS_ENTRYPOINT_REPLAY": RulePolicy(
"CLASS_ENTRYPOINT_REPLAY", "result_reuse", "hard", AUTO_FILTER, (),
(), "keep",
),
"DUPLICATE_SPAM": RulePolicy(
"DUPLICATE_SPAM", "administrative", "support", SUSPICIOUS_ONLY, (),
(), "downgrade",
Expand Down Expand Up @@ -3790,6 +3882,7 @@ def support_only_patterns(matched_patterns: list[dict]) -> bool:
detect_thread_injection,
detect_lazy_tensor,
detect_precision_downgrade,
detect_class_entrypoint_replay,
]

BASE_DETECTOR_SPECS = [
Expand Down Expand Up @@ -3827,6 +3920,7 @@ def support_only_patterns(matched_patterns: list[dict]) -> bool:
("thread_injection", detect_thread_injection),
("lazy_tensor", detect_lazy_tensor),
("precision_downgrade", detect_precision_downgrade),
("class_entrypoint_replay", detect_class_entrypoint_replay),
]

VALID_RULE_OUTCOMES = {AUTO_FILTER, SUSPICIOUS_ONLY, TELEMETRY_ONLY}
Expand Down Expand Up @@ -4703,7 +4797,7 @@ def _worker_parquet(args: tuple) -> dict:
"HARDCODED_SHAPES", "TRIVIAL_PROBE",
"OBFUSCATED_EXEC", "DYNAMIC_EXECUTION", "MODULE_RELOAD", "THREAD_INJECTION", "LAZY_TENSOR",
"TOKEN_PASTE_CUDA_API", "SEQUENCE_BATCH_GRAPH", "PARTIAL_GRAPH_KEY", "RUNTIME_PACKAGE_INSTALL",
"PRECISION_DOWNGRADE", "SCORE_PHYSICS_FLOOR", "SCORE_IMPOSSIBLE", "SCORE_SUSPECT_FLOOR",
"PRECISION_DOWNGRADE", "CLASS_ENTRYPOINT_REPLAY", "SCORE_PHYSICS_FLOOR", "SCORE_IMPOSSIBLE", "SCORE_SUSPECT_FLOOR",
"SCORE_BROKEN", "SCORE_EXTREME_SPEEDUP", "DUPLICATE_SPAM", "NEAR_CLONE_SPAM",
]

Expand Down