"""Compile the submitted parser and check actual stream behavior.""" import json import os import subprocess import sys import tempfile from pathlib import Path class UnsupportedEnvironment(RuntimeError): pass def candidate_user(): os.setgroups([]) os.setgid(65534) os.setuid(65534) def isolation(): if os.environ.get("ROLLOUT_GRADE_ISOLATION") != "uid65534": return None if sys.platform != "linux" or os.geteuid() != 0: raise UnsupportedEnvironment("isolated grader requires a Linux root supervisor") probe = ( "from pathlib import Path\n" "try: Path('/grader/verify.py').read_bytes()\n" "except PermissionError: raise SystemExit(0)\n" "raise SystemExit(1)\n" ) try: result = subprocess.run([sys.executable, "-I", "-c", probe], capture_output=True, timeout=10, preexec_fn=candidate_user) except subprocess.SubprocessError as error: raise UnsupportedEnvironment("candidate identity could not be isolated") from error if result.returncode != 0: raise UnsupportedEnvironment("candidate can read the private grader or isolation probe failed") return candidate_user def frame(payload): checksum = len(payload) for byte in payload: checksum ^= byte return (bytes([len(payload)]) + payload + bytes([checksum])).hex() def verify(candidate): child_user = isolation() source = candidate / "decoder.c" if source.is_symlink() or not source.is_file(): return {"status": "FAIL", "checks": 1, "reason": "missing decoder.c"} cases = [ ([], "", 0), (["02aabb13"], "aabb\n", 0), (["02", "aa", "bb", "13"], "aabb\n", 0), (["02aa", "", "bb13"], "aabb\n", 0), (["01fffe010706"], "ff\n07\n", 0), (["01ff", "fe01", "07", "06"], "ff\n07\n", 0), (["01ff00010706"], "ERR\n07\n", 0), (["11", "010706"], "ERR\n07\n", 0), (["0000"], "\n", 0), (["00", "00"], "\n", 0), (["02aa"], "INCOMPLETE\n", 1), (["f"], "", 2), (["zz"], "", 2), (["02AABB13"], "aabb\n", 0), (["10000102030405060708090a0b0c0d0e0f10"], "000102030405060708090a0b0c0d0e0f\n", 0), ] for length in range(17): payload = bytes((length + index * 17) % 256 for index in range(length)) encoded = frame(payload) expected = payload.hex() + "\n" bad_crc = encoded[:-2] + "{:02x}".format(int(encoded[-2:], 16) ^ 1) cases.extend([ ([encoded], expected, 0), ([encoded[index:index + 2] for index in range(0, len(encoded), 2)], expected, 0), ([encoded[:-2]], "INCOMPLETE\n", 1), ([bad_crc + encoded], "ERR\n" + expected, 0), ]) cases.extend([(["{:02x}".format(length) + "010706"], "ERR\n07\n", 0) for length in (17, 32, 128, 255)]) with tempfile.TemporaryDirectory(prefix="frame-grade-") as temporary: if child_user: os.chmod(temporary, 0o700) os.chown(temporary, 65534, 65534) executable = Path(temporary) / "decoder" compiled = subprocess.run( ["cc", "-std=c11", "-Wall", "-Wextra", "-Werror", str(source), "-o", str(executable)], capture_output=True, text=True, timeout=30, preexec_fn=child_user, ) if compiled.returncode: return {"status": "FAIL", "checks": 1, "reason": "compile", "stderr": compiled.stderr[-4000:]} for index, (chunks, expected, expected_exit) in enumerate(cases, 1): # Cases take milliseconds; the margin keeps host load from failing a correct decoder. completed = subprocess.run( [str(executable)] + chunks, capture_output=True, text=True, timeout=5, preexec_fn=child_user, ) if (completed.stdout, completed.returncode) != (expected, expected_exit): return {"status": "FAIL", "checks": index, "reason": "case {}".format(index)} return {"status": "PASS", "checks": len(cases)} if __name__ == "__main__": try: result = verify(Path(sys.argv[1])) except FileNotFoundError: result = {"status": "UNVERIFIED", "checks": 0, "reason": "C compiler unavailable"} except UnsupportedEnvironment as error: result = {"status": "UNVERIFIED", "checks": 0, "reason": str(error)} except subprocess.TimeoutExpired: result = {"status": "FAIL", "checks": 1, "reason": "timeout"} except Exception as error: result = {"status": "INFRA_ERROR", "checks": 0, "reason": str(error)} print(json.dumps(result, sort_keys=True)) sys.exit({"PASS": 0, "FAIL": 1}.get(result["status"], 2))