-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathverify-sandbox.mjs
More file actions
179 lines (154 loc) · 3.75 KB
/
verify-sandbox.mjs
File metadata and controls
179 lines (154 loc) · 3.75 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { spawn } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, "..");
const verificationDir = path.join(repoRoot, "verification");
const imageName = process.env.BUGFIND_VERIFY_IMAGE || "bugfind15-verifier";
const containerName = process.env.BUGFIND_VERIFY_CONTAINER || "bugfind15-verifier-service";
const sandboxPort = process.env.BUGFIND_SANDBOX_PORT || "4010";
function run(command, args, options = {}) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: options.cwd ?? repoRoot,
stdio: "inherit",
env: {
...process.env,
...(options.env ?? {})
}
});
child.on("error", reject);
child.on("close", (code) => {
if (code === 0) {
resolve();
return;
}
reject(new Error(`${command} exited with code ${code}`));
});
});
}
function runOptional(command, args, options = {}) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: options.cwd ?? repoRoot,
stdio: options.stdio ?? "ignore",
env: {
...process.env,
...(options.env ?? {})
}
});
child.on("error", reject);
child.on("close", (code) => {
resolve(code === 0);
});
});
}
async function buildImage() {
await run("docker", ["build", "-t", imageName, "-f", "Dockerfile", "."], {
cwd: verificationDir
});
}
async function imageExists() {
return runOptional("docker", ["image", "inspect", imageName]);
}
async function containerExists() {
return runOptional("docker", ["container", "inspect", containerName]);
}
async function ensureImageBuilt() {
if (await imageExists()) {
return;
}
console.log(`Docker image "${imageName}" not found. Building it now...`);
await buildImage();
}
async function runVerifier(extraArgs) {
await run("docker", [
"run",
"--rm",
"--entrypoint",
"node",
"--network",
"none",
"--read-only",
"--tmpfs",
"/tmp:exec,mode=1777",
"--cap-drop",
"ALL",
"--security-opt",
"no-new-privileges",
"--pids-limit",
"256",
"--memory",
"1024m",
"--cpus",
"2",
imageName,
"/opt/verification/runner.mjs",
...extraArgs
]);
}
async function serveVerifier() {
await ensureImageBuilt();
if (await containerExists()) {
await run("docker", ["rm", "-f", containerName]);
}
await run("docker", [
"run",
"--rm",
"--name",
containerName,
"-p",
`${sandboxPort}:4010`,
"--read-only",
"--tmpfs",
"/tmp:exec,mode=1777",
"--cap-drop",
"ALL",
"--security-opt",
"no-new-privileges",
"--pids-limit",
"256",
"--memory",
"1024m",
"--cpus",
"2",
imageName
]);
}
async function stopVerifier() {
if (!(await containerExists())) {
console.log(`No running container named "${containerName}".`);
return;
}
await run("docker", ["rm", "-f", containerName]);
}
async function main() {
const [command = "run", ...rest] = process.argv.slice(2);
if (command === "build") {
await buildImage();
return;
}
if (command === "run") {
await runVerifier(rest);
return;
}
if (command === "rebuild") {
await buildImage();
await runVerifier(rest);
return;
}
if (command === "serve") {
await serveVerifier();
return;
}
if (command === "stop") {
await stopVerifier();
return;
}
throw new Error(`Unknown command "${command}". Use build, run, rebuild, serve, or stop.`);
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
});