# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: CC-BY-NC-4.0 """compute-sanitizer memcheck and racecheck on the smallest workload: memory errors and races.""" from __future__ import annotations import os import re import shutil from .. import harness # Memory errors and races are properties of the code, not of the shape, so one small workload # catches them about as well as all of them, in minutes. Errors count only when this run's # trace shows the kernel ran; otherwise the result is "unverified", never "clean". TOOLS = ("memcheck", "racecheck") _RAN = frozenset({"PASSED", "INCORRECT_NUMERICAL", "INCORRECT_SHAPE", "INCORRECT_DTYPE", "RUNTIME_ERROR"}) _COMPLETED = _RAN - {"RUNTIME_ERROR"} _SUMMARY = re.compile(r"ERROR SUMMARY:\s*([\d,]+)\s*error") # The harness's CUPTI timer and the sanitizer get in each other's way, and no timing is needed # here: in the eval process, this turns the harness's timing call into a no-op. NO_TIMING_SNIPPET = ("import sol_execbench.core.bench.timing as _t\n" "_t.time_runnable = lambda *a, **k: 0.0") def tool_result(rc, output, rows) -> dict: """{"errors": n} for one tool run, or {"errors": None, "reason": code} when it is no evidence.""" statuses = [r.get("status") for r in rows] m = _SUMMARY.search(output or "") if "MULTIPLE_SUBSCRIBERS" in (output or "") or "Timing failed" in (output or ""): return {"errors": None, "reason": "cupti_conflict"} if not any(s in _RAN for s in statuses): return {"errors": None, "reason": "kernel_did_not_run"} if m is None: return {"errors": None, "reason": "no_error_summary"} total = int(m.group(1).replace(",", "")) if total == 0 and (rc != 0 or not any(s in _COMPLETED for s in statuses)): return {"errors": None, "reason": "incomplete_run"} return {"errors": total} def check(problem_dir, solution_path, out_dir, gpu_id=None, lock_dir=None, tools=TOOLS, compile_timeout_s=300, eval_timeout_s=900) -> dict: """{"verdict": clean | errors | unverified, : {"errors", "reason"}} for one solution.""" if shutil.which("compute-sanitizer") is None: return {"verdict": "unverified", "reason": "compute_sanitizer_not_found"} w = harness.smallest_workload(problem_dir) if w is None: return {"verdict": "unverified", "reason": "no_workload"} os.makedirs(out_dir, exist_ok=True) wl = harness.write_jsonl(os.path.join(out_dir, "smallest.jsonl"), [w]) env = harness.site_env(None, [NO_TIMING_SNIPPET], os.path.join(out_dir, "site")) out = {} for tool in tools: res = harness.evaluate(problem_dir, solution_path, out_dir, gpu_id, lock_dir, workload_path=wl, config_path=harness.quick_config(out_dir), compile_timeout_s=compile_timeout_s, eval_timeout_s=eval_timeout_s, env=env, prefix=["compute-sanitizer", "--tool", tool, "--error-exitcode", "0"]) out[tool] = tool_result(res.get("rc"), res.get("output"), res.get("rows") or []) errs = [r.get("errors") for r in out.values()] verdict = ("errors" if any(errs) else "clean" if errs and all(e == 0 for e in errs) else "unverified") return dict(out, verdict=verdict)