Repository navigation
Expand file tree
/
Copy pathexecution.ts
More file actions
259 lines (247 loc) · 14.3 KB
/
Copy pathexecution.ts
File metadata and controls
259 lines (247 loc) · 14.3 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*---------------------------------------------------------------------------------------------
* Copyright (c) BeCoder contributors. All rights reserved.
* Licensed under GPL-3.0-or-later. See LICENSE in the project root.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import * as path from 'path';
import * as net from 'net';
import { randomUUID } from 'crypto';
import { spawn } from 'child_process';
import type { CompilationPlan, CompilationSourceSnapshot, RuntimeEvidence } from './toolchain';
import { ImportedProblem } from './problem';
import { judgeSample, SampleExecution, SampleVerdict } from './judge';
const outputLimit = 16 * 1024 * 1024;
export interface ExecutionPreferences {
timeOut: number;
ignoreSTDERROR: boolean;
pythonCommand: string;
}
export interface CheckerRun extends SampleExecution { readonly command: string }
export interface CphExecutionOptions {
readonly evidence?: () => RuntimeEvidence | undefined;
readonly prepareCompilation: (source: string, output: string, session: string) => Promise<CompilationPlan> | CompilationPlan;
readonly dataRoot: string;
/** Build-time helper; its inherited stdout/stderr stay separate pipes here. */
readonly inputHelper: string;
readonly onSampleStarted?: (index: number) => void;
readonly onCheckerStarted?: () => void;
readonly onCompileStarted?: () => void;
readonly onCompileFinished?: () => void;
readonly onSampleFinished?: (index: number, sample: JudgeRun['samples'][number]) => void;
readonly preferences?: (source: string) => ExecutionPreferences;
}
export interface JudgeRun {
readonly sourceSnapshot?: CompilationSourceSnapshot;
readonly compile: SampleExecution;
readonly samples: readonly { readonly result: SampleExecution; readonly verdict: SampleVerdict; readonly checkerRun?: CheckerRun }[];
}
/** One request owns one private binary; source-adjacent Runner artifacts are never read. */
export class CphExecutor {
private controller: AbortController | undefined;
private disposed = false;
constructor(private readonly options: CphExecutionOptions) { }
async judge(problem: ImportedProblem & { customCheckerPath?: string }, sourcePath: string, compileOnly = false): Promise<JudgeRun> {
if (this.controller || this.disposed) { throw new Error('CPH is busy or disposed.'); }
if (!/\.(c|cpp|cc|cxx)$/i.test(sourcePath) || problem.interactive || problem.input.type !== 'stdin' || problem.output.type !== 'stdout') {
throw new Error('Only non-interactive C/C++ stdin/stdout problems are supported.');
}
if (!compileOnly && !problem.tests.length) { throw new Error('There are no sample tests.'); }
const controller = new AbortController();
this.controller = controller;
let session: string | undefined;
let evidence: RuntimeEvidence | undefined;
let outcome: unknown = { status: 'preparation-failed' };
try {
problem = structuredClone(problem);
const preferences = this.options.preferences?.(sourcePath);
const timeout = preferences?.timeOut ?? problem.timeLimit;
if (!Number.isInteger(timeout) || timeout < 1 || timeout > 2147483647) { throw new Error('Invalid CPH timeout.'); }
fs.mkdirSync(this.options.dataRoot, { recursive: true });
if (fs.lstatSync(this.options.dataRoot).isSymbolicLink()) { throw new Error('Linked CPH session root.'); }
session = fs.mkdtempSync(path.join(fs.realpathSync(this.options.dataRoot), 'request-'));
fs.mkdirSync(path.join(session, 'tmp'));
fs.mkdirSync(path.join(session, 'user'));
const executable = path.join(session, 'program.exe');
const toolchain = await this.options.prepareCompilation(sourcePath, executable, session);
if (controller.signal.aborted) { throw new Error('CPH compilation cancelled.'); }
for (const name of ['TEMP', 'USERPROFILE', 'LOCALAPPDATA', 'APPDATA']) {
fs.mkdirSync(toolchain.environment[name], { recursive: true });
}
this.options.onCompileStarted?.();
evidence = this.options.evidence?.();
if (toolchain.sourceSnapshot) { evidence?.start(toolchain.sourceSnapshot, { compiler: toolchain.compiler, args: toolchain.args, cwd: path.dirname(sourcePath), problem, preferences, timeout, compileOnly, localHeadersSnapshotted: false, programStream: 'separate-pipes' }); }
const rawCompile = await runProcess(toolchain.compiler, [...toolchain.args], path.dirname(sourcePath), toolchain.environment, 120000, controller.signal, true, undefined,
(stream, bytes) => evidence?.event({ type: 'stream', phase: 'compiling', stream, bytesBase64: bytes.toString('base64') }));
const sourceSnapshot = toolchain.sourceSnapshot;
const paths = sourceSnapshot ? [...new Set([sourceSnapshot.path, sourceSnapshot.path.replace(/\\/g, '/')])] : [];
const mapPath = (text: string) => paths.reduce((value, from) => value.split(from).join(sourceSnapshot!.originalPath), text);
const compile = { ...rawCompile, stdout: mapPath(rawCompile.stdout), stderr: mapPath(rawCompile.stderr) };
evidence?.event({ type: 'compiled', exitCode: compile.exitCode, signal: compile.signal, timedOut: compile.timedOut, cancelled: compile.cancelled, outputLimitExceeded: compile.outputLimitExceeded, launchError: compile.launchError, durationMs: compile.durationMs });
this.options.onCompileFinished?.();
const samples: { result: SampleExecution; verdict: SampleVerdict; checkerRun?: CheckerRun }[] = [];
if (compile.exitCode !== 0 || compile.launchError || compile.cancelled || compile.timedOut || compile.outputLimitExceeded) {
outcome = { status: compile.cancelled ? 'cancelled' : 'compile-error' };
return { compile, samples, sourceSnapshot };
}
if (!fs.statSync(executable).isFile()) { throw new Error('Compiler produced no executable.'); }
if (compileOnly) { outcome = { status: 'compile-only' }; return { compile, samples, sourceSnapshot }; }
for (const [index, sample] of problem.tests.entries()) {
if (controller.signal.aborted) { break; }
const input = path.join(session, 'input');
fs.writeFileSync(input, sample.input, 'utf8');
const result = await runWithInput(this.options.inputHelper, executable, input, session, toolchain.environment, timeout, controller.signal,
() => { evidence?.event({ type: 'sample-started', index, at: Date.now() }); this.options.onSampleStarted?.(index); },
(stream, bytes) => evidence?.event({ type: 'stream', phase: 'running', index, stream, bytesBase64: bytes.toString('base64') }));
let verdict = judgeSample(sample.output, result);
let checkerRun: CheckerRun | undefined;
if (preferences?.ignoreSTDERROR === false && result.stderr && (verdict === 'passed' || verdict === 'wrong-answer')) { verdict = 'runtime-error'; }
if (problem.customCheckerPath?.trim() && (verdict === 'passed' || verdict === 'wrong-answer')) {
const checker = path.resolve(path.dirname(sourcePath), problem.customCheckerPath.trim());
if (!fs.statSync(checker).isFile()) { throw new Error(`Custom checker script not found: ${checker}`); }
const output = path.join(session, 'output');
fs.writeFileSync(output, result.stdout, 'utf8');
const configured = preferences?.pythonCommand || 'python3';
const python = process.platform === 'win32' && configured === 'python3' ? 'python' : configured;
const args = [checker, input, output];
// Owner-approved exception: CPH Python checkers use the user's Python environment.
const pending = runProcess(python, args, path.dirname(checker), { ...process.env, DEBUG: 'true', CPH: 'true' }, timeout, controller.signal, true);
this.options.onCheckerStarted?.();
const checked = await pending;
checkerRun = { ...checked, command: [python, ...args].map(value => JSON.stringify(value)).join(' ') };
verdict = checked.cancelled ? 'cancelled' : checked.timedOut ? 'time-limit' : checked.outputLimitExceeded ? 'output-limit'
: checked.launchError || checked.signal ? 'runtime-error' : checked.exitCode === 0 ? 'passed' : 'wrong-answer';
}
samples.push({ result, verdict, checkerRun });
evidence?.event({ type: 'sample-finished', index, result, verdict, checkerRun });
this.options.onSampleFinished?.(index, { result, verdict, checkerRun });
}
outcome = { status: controller.signal.aborted ? 'cancelled' : 'completed', completedSamples: samples.length, requestedSamples: problem.tests.length };
return { compile, samples, sourceSnapshot };
} catch (error) {
outcome = { status: controller.signal.aborted ? 'cancelled' : 'failed', message: error instanceof Error ? error.message : String(error) };
throw error;
} finally {
try {
await evidence?.finish(outcome);
// Private random request directory; never published to the user project.
if (session) { fs.rmSync(session, { recursive: true }); }
} finally { this.controller = undefined; }
}
}
cancel(): void { this.controller?.abort(); }
dispose(): void { this.disposed = true; this.cancel(); }
}
async function runProcess(file: string, args: string[], cwd: string, env: NodeJS.ProcessEnv, timeoutMs: number, signal: AbortSignal, compiler = false, clockReady?: (reset: (milliseconds: number) => void) => void, onOutput?: (stream: 'stdout' | 'stderr', bytes: Buffer) => void): Promise<SampleExecution> {
return new Promise(resolve => {
let startedAt = Date.now();
const child = spawn(file, args, { cwd, env, windowsHide: true, shell: false, stdio: 'pipe' });
const systemRoot = env.SystemRoot ?? env.SYSTEMROOT ?? env.windir ?? process.env.SystemRoot ?? process.env.SYSTEMROOT ?? process.env.windir;
const output: Buffer[][] = [[], []];
const sizes = [0, 0];
let timedOut = false;
let cancelled = false;
let outputLimitExceeded = false;
let launchError: string | undefined;
let stopping: Promise<void> | undefined;
const stop = () => {
if (stopping) { return; }
stopping = new Promise<void>(done => {
if (compiler && child.pid && child.exitCode === null && systemRoot) {
const killer = spawn(path.join(systemRoot, 'System32', 'taskkill.exe'), ['/PID', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore', shell: false });
killer.once('error', () => { child.kill(); done(); });
killer.once('close', () => { child.kill(); done(); });
} else {
// Helper owns a KILL_ON_JOB_CLOSE job, including program descendants.
child.kill();
done();
}
});
};
const abort = () => { cancelled = true; stop(); };
let timer = setTimeout(() => { timedOut = true; stop(); }, timeoutMs);
clockReady?.(milliseconds => {
startedAt = Date.now();
clearTimeout(timer);
timer = setTimeout(() => { timedOut = true; stop(); }, milliseconds);
});
for (const [index, stream] of [child.stdout, child.stderr].entries()) {
stream.on('data', (chunk: Buffer) => {
onOutput?.(index === 0 ? 'stdout' : 'stderr', chunk);
const remaining = outputLimit - sizes[index];
if (chunk.length > remaining) { outputLimitExceeded = true; stop(); }
if (remaining > 0) {
const accepted = chunk.subarray(0, remaining);
output[index].push(accepted);
sizes[index] += accepted.length;
}
});
}
child.stdin.on('error', () => undefined);
child.stdin.end();
child.once('error', error => { launchError = error.message; });
child.once('close', async (exitCode, exitSignal) => {
clearTimeout(timer);
signal.removeEventListener('abort', abort);
await stopping;
resolve({ durationMs: Date.now() - startedAt, stdout: Buffer.concat(output[0]).toString('utf8'), stderr: Buffer.concat(output[1]).toString('utf8'), exitCode,
signal: exitSignal, timedOut, cancelled, outputLimitExceeded, launchError });
});
signal.addEventListener('abort', abort, { once: true });
if (signal.aborted) { abort(); }
});
}
async function runWithInput(helper: string, executable: string, input: string, cwd: string, env: NodeJS.ProcessEnv, timeoutMs: number, signal: AbortSignal, onStarted: () => void, onOutput?: (stream: 'stdout' | 'stderr', bytes: Buffer) => void): Promise<SampleExecution> {
const pipe = `\\\\.\\pipe\\becoder-cph-${process.pid}-${randomUUID()}`;
let socket: net.Socket | undefined;
let code: number | undefined;
let failure: string | undefined;
let started = false;
let resetClock: (milliseconds: number) => void = () => undefined;
let settle: () => void = () => undefined;
const drained = new Promise<void>(resolve => { settle = resolve; });
const server = net.createServer(connection => {
if (socket) { connection.destroy(); return; }
socket = connection;
let buffer = '';
connection.setEncoding('utf8');
connection.on('close', settle);
connection.on('error', error => { failure = error.message; });
connection.on('data', text => {
buffer += text;
if (buffer.length > 4096) { failure = 'Oversized helper message.'; connection.destroy(); return; }
let newline: number;
while ((newline = buffer.indexOf('\n')) >= 0) {
const line = buffer.slice(0, newline);
buffer = buffer.slice(newline + 1);
try {
const message = JSON.parse(line);
if (message.type === 'started' && !started && Number.isSafeInteger(message.pid) && message.pid > 0) {
started = true;
resetClock(timeoutMs);
connection.write('G');
onStarted();
} else if (message.type === 'exit' && started && code === undefined && Number.isInteger(message.code) && message.code >= 0 && message.code <= 0xFFFFFFFF) {
code = message.code;
} else { throw new Error(`Helper failure: ${line}`); }
} catch (error) { failure = String(error); connection.destroy(); }
}
});
});
try {
await new Promise<void>((resolve, reject) => { server.once('error', reject); server.listen(pipe, resolve); });
const result = await runProcess(helper, [executable, input, pipe], cwd, env, 15000, signal, false, reset => { resetClock = reset; }, onOutput);
if (socket) {
let timer: NodeJS.Timeout | undefined;
try {
await Promise.race([drained, new Promise<void>(resolve => { timer = setTimeout(resolve, 1000); })]);
} finally {
if (timer) { clearTimeout(timer); }
}
}
return { ...result, timedOut: result.timedOut && started, exitCode: code ?? result.exitCode,
launchError: result.launchError ?? failure ?? (!started && !result.cancelled ? 'Helper failed to start the program.' : code === undefined && !result.cancelled && !result.timedOut && !result.outputLimitExceeded ? 'Helper returned no result.' : undefined) };
} finally {
socket?.destroy();
await new Promise<void>(resolve => server.close(() => resolve()));
}
}