File size: 4,203 Bytes
d40299b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Standalone trusted entry point; copied into each separate verifier image.

No Repo2RLEnv installation is needed to grade an exported task. Submitted Python
runs as an unprivileged user; this parent owns the result parser and reward file.
"""

from __future__ import annotations

import importlib.util
import json
import os
import signal
import stat
import subprocess
import sys
import tempfile
from pathlib import Path


def main() -> None:
    directory = Path("/tests")
    parser_spec = importlib.util.spec_from_file_location(
        "test_results", directory / "test_results.py"
    )
    parser = importlib.util.module_from_spec(parser_spec)
    sys.modules[parser_spec.name] = parser
    parser_spec.loader.exec_module(parser)
    contract = json.loads((directory / "contract.json").read_text())
    logs = Path("/logs/verifier")
    logs.mkdir(parents=True, exist_ok=True)
    logs.chmod(0o755)
    reward = logs / "reward.txt"
    reward.write_text("0\n")
    reward.chmod(0o644)
    for relative in contract["submitted_files"]:
        path = Path("/workspace") / relative
        # Artifact uploads are data. Refuse link traversal and special files
        # before the child imports any learner-controlled code.
        for component in (path, *path.parents):
            if component.is_symlink():
                raise ValueError("Submitted source contains a symlink")
        if not stat.S_ISREG(path.stat().st_mode) or path.stat().st_size > 8 * 1024 * 1024:
            raise ValueError("Submitted source is not a bounded regular file")
        os.chown(path, 0, 0)
        path.chmod(0o644)
    with tempfile.TemporaryDirectory(prefix="r2e-grade-") as temporary:
        working = Path(temporary)
        os.chown(working, 1001, 1001)
        working.chmod(0o700)
        report = working / "results.xml"
        command = [
            "/usr/local/bin/python",
            "-I",
            "/tests/test_driver.py",
            *contract["test_paths"],
            "-q",
            "--tb=short",
            "-p",
            "no:cacheprovider",
            f"--junitxml={report}",
        ]
        with (logs / "stdout.txt").open("w") as stdout, (logs / "stderr.txt").open("w") as stderr:
            process = subprocess.Popen(
                command,
                cwd="/workspace",
                stdout=stdout,
                stderr=stderr,
                user=1001,
                group=1001,
                extra_groups=[],
                start_new_session=True,
                env={
                    "PATH": "/usr/local/bin:/usr/bin:/bin",
                    "HOME": temporary,
                    "PYTHONDONTWRITEBYTECODE": "1",
                    "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1",
                },
            )
            try:
                code = process.wait(timeout=contract["timeout_sec"])
            finally:
                # Terminate descendants too: they must not keep modifying the
                # child-owned report while the trusted parent checks it.
                try:
                    os.killpg(process.pid, signal.SIGKILL)
                except ProcessLookupError:
                    pass
                process.wait()
        if report.is_symlink() or not report.is_file() or report.stat().st_size > 16 * 1024 * 1024:
            (logs / "result.json").write_text(
                json.dumps({"passed": False, "returncode": code, "reason": "invalid_report"})
            )
            return
        text = report.read_text()
        (logs / "results.xml").write_text(text)
        try:
            parsed = parser.parse_junit(text, returncode=code)
        except ValueError as exc:
            (logs / "result.json").write_text(
                json.dumps({"passed": False, "returncode": code, "reason": str(exc)})
            )
            return
        expected = set(contract["expected_passes"])
        passed = code == 0 and bool(expected) and parsed.passed == expected
        (logs / "result.json").write_text(
            json.dumps({"passed": passed, "returncode": code, "statuses": parsed.statuses})
        )
        reward.write_text("1\n" if passed else "0\n")


if __name__ == "__main__":
    main()