core&judge&ui: hack submission

This commit is contained in:
undefined committed 2022-09-06 13:02:46 +08:00
1 parent 07ab4f1bbd
commit 4e0c9bb00a
17 files changed
+256 -128

No files matched your search

+10 -2
View File
@@ -6,8 +6,10 @@ import { max, sum } from 'lodash';
import readYamlCases, { convertIniConfig } from '@hydrooj/utils/lib/cases';
import { normalizeSubtasks, readSubtasksFromFiles } from '@hydrooj/utils/lib/common';
import { changeErrorType } from '@hydrooj/utils/lib/utils';
import { ProblemConfigFile } from 'hydrooj';
import { getConfig } from './config';
import { FormatError, SystemError } from './error';
import { NextFunction, ParsedConfig } from './interface';
import { ensureFile, parseMemoryMB } from './utils';
function isValidConfig(config) {
@@ -62,7 +64,13 @@ export async function processTestdata(folder: string) {
}));
}
export default async function readCases(folder: string, cfg: Record<string, any> = {}, args) {
interface Args {
next: NextFunction;
key: string;
isSelfSubmission: boolean;
}
export default async function readCases(folder: string, cfg: ProblemConfigFile = {}, args: Args): Promise<ParsedConfig> {
const iniConfig = path.resolve(folder, 'config.ini');
const yamlConfig = path.resolve(folder, 'config.yaml');
const ymlConfig = path.resolve(folder, 'config.yml');
@@ -93,7 +101,7 @@ export default async function readCases(folder: string, cfg: Record<string, any>
try {
result.subtasks = readSubtasksFromFiles(await collectFiles(folder), cfg);
result.count = Math.sum(result.subtasks.map((i) => i.cases.length));
if (cfg.isSelfSubmission) args.next?.({ message: { message: 'Found {0} testcases.', params: [result.count] } });
if (args.isSelfSubmission) args.next?.({ message: { message: 'Found {0} testcases.', params: [result.count] } });
} catch (e) {
throw new SystemError('Cannot parse testdata.', [e.message, ...(e.params || [])]);
}
+1 -7
View File
@@ -1,7 +1,7 @@
import Queue from 'p-queue';
import { STATUS } from '@hydrooj/utils/lib/status';
import { getConfig } from './config';
import { CompileError, FormatError } from './error';
import { FormatError } from './error';
import { Context, ContextSubTask } from './judge/interface';
import { NormalizedCase, NormalizedSubtask } from './utils';
@@ -60,12 +60,6 @@ function judgeSubtask(subtask: NormalizedSubtask, sid: string, judgeCase: Task['
export const runFlow = async (ctx: Context, task: Task) => {
if (!ctx.config.subtasks.length) throw new FormatError('Problem data not found.');
ctx.next({ status: STATUS.STATUS_COMPILING });
if (ctx.config.template && 'content' in ctx.code) {
if (ctx.config.template[ctx.lang]) {
const tpl = ctx.config.template[ctx.lang];
ctx.code.content = tpl[0] + ctx.code.content + tpl[1];
} else throw new CompileError('Language not supported by provided templates');
}
await task.compile();
ctx.next({ status: STATUS.STATUS_JUDGING, progress: 0 });
const tasks = [];
+1
View File
@@ -20,6 +20,7 @@ const logger = new Logger('judge');
const session = {
config: { detail: system.get('hydrojudge.detail') },
async fetchFile(name: string) {
name = name.split('#')[0];
const target = path.join('/tmp/hydro/judge', name.replace(/\//g, '_'));
await storage.get(`submission/${name}`, target);
return target;
+1
View File
@@ -110,6 +110,7 @@ export default class Hydro {
}
async fetchFile(name: string) {
name = name.split('#')[0];
const res = await this.post('judge/code', { id: name });
const target = path.join('/tmp/hydro/judge', name.replace(/\//g, '_'));
const w = fs.createWriteStream(target);
+11
View File
@@ -1,3 +1,5 @@
import { NormalizedSubtask } from '@hydrooj/utils/lib/utils';
import { JudgeResultBody, ProblemConfigFile } from 'hydrooj';
import { CopyInFile } from './sandbox';
export interface Execute {
@@ -6,3 +8,12 @@ export interface Execute {
copyIn: Record<string, CopyInFile>;
time?: number;
}
export type NextFunction = (body: Partial<JudgeResultBody>) => Promise<void> | void;
export interface ParsedConfig extends Omit<ProblemConfigFile, 'time' | 'memory' | 'subtasks'> {
count: number;
time: number;
memory: number;
subtasks: NormalizedSubtask[];
}
+9 -8
View File
@@ -23,23 +23,24 @@ export async function judge(ctx: Context) {
compile(session.getLang(ctx.lang), ctx.code, userExtraFiles, ctx.next).then(markCleanup),
compileChecker(session.getLang, config.checker_type, config.checker, judgeExtraFiles).then(markCleanup),
compileValidator(session.getLang, config.validator, judgeExtraFiles).then(markCleanup),
ctx.session.fetchFile(ctx.input),
ctx.session.fetchFile(ctx.files.hack),
]);
ctx.next({ status: STATUS.STATUS_JUDGING, progress: 0 });
const validateResult = await run(
validator.execute,
{
stdin: { src: input },
copyIn: judgeExtraFiles,
copyIn: { ...validator.copyIn, ...judgeExtraFiles },
time: parseTimeMS(ctx.config.time || '1s') * execute.time,
memory: parseMemoryMB(ctx.config.memory || '256m'),
},
);
console.log(validateResult);
if (validateResult.status !== STATUS.STATUS_ACCEPTED) {
const message = `${validateResult.files.stdout || ''}\n${validateResult.files.stderr || ''}`.trim();
const message = `${validateResult.stdout || ''}\n${validateResult.stderr || ''}`.trim();
return ctx.end({ status: STATUS.STATUS_FORMAT_ERROR, message });
}
const copyIn = { ...ctx.execute.copyIn };
const copyIn = { ...execute.copyIn };
const { filename } = ctx.config;
if (filename) copyIn[`${filename}.in`] = { src: input };
const res = await run(
@@ -47,7 +48,7 @@ export async function judge(ctx: Context) {
{
stdin: filename ? null : { src: input },
copyIn,
copyOutCached: [filename ? `${filename}.out?` : 'stdout'],
copyOutCached: [filename ? `${filename}.out?` : 'stdout', 'stderr'],
time: parseTimeMS(ctx.config.time || '1s') * execute.time,
memory: parseMemoryMB(ctx.config.memory || '256m'),
},
@@ -55,7 +56,7 @@ export async function judge(ctx: Context) {
const { code, time, memory } = res;
let { status } = res;
let stdout: CmdFile = { fileId: res.fileIds[filename ? `${filename}.out` : 'stdout'] };
const stderr = { fileId: res.fileIds['stderr'] };
console.log(res);
if (!stdout.fileId) stdout = { content: '' };
let message: any = '';
if (status === STATUS.STATUS_ACCEPTED) {
@@ -68,9 +69,9 @@ export async function judge(ctx: Context) {
execute: checker.execute,
copyIn: checker.copyIn || {},
input: { src: input },
output: null,
output: { content: '' },
user_stdout: stdout,
user_stderr: stderr,
user_stderr: { fileId: res.fileIds['stderr'] },
score: 100,
detail: ctx.config.detail ?? true,
env: { ...ctx.env, HYDRO_TESTCASE: '0' },
+22 -41
View File
@@ -4,18 +4,18 @@ import { noop } from 'lodash';
import { LangConfig } from '@hydrooj/utils/lib/lang';
import { STATUS } from '@hydrooj/utils/lib/status';
import type {
FileInfo, JudgeRequest, JudgeResultBody, ProblemConfigFile,
FileInfo, JudgeMeta, JudgeRequest, JudgeResultBody,
} from 'hydrooj';
import { Logger } from 'hydrooj/src/logger';
import readCases from './cases';
import { getConfig } from './config';
import { CompileError, FormatError, SystemError } from './error';
import { CompileError, FormatError } from './error';
import { NextFunction, ParsedConfig } from './interface';
import judge from './judge';
import { CopyInFile } from './sandbox';
import * as tmpfs from './tmpfs';
import { compilerText, Lock, md5 } from './utils';
type NextFunction = (data: Partial<JudgeResultBody>) => void;
interface Session {
getLang: (name: string) => LangConfig;
getNext: (task: JudgeTask) => NextFunction;
@@ -38,7 +38,9 @@ export class JudgeTask {
clean: (() => Promise<any>)[];
data: FileInfo[];
folder: string;
config: ProblemConfigFile & { problemOwner: number };
config: ParsedConfig;
meta: JudgeMeta;
files?:Record<string, string>;
next: (data: Partial<JudgeResultBody>) => void;
end: (data: Partial<JudgeResultBody>) => void;
env: Record<string, string>;
@@ -58,7 +60,8 @@ export class JudgeTask {
this.code = { content: this.request.code };
this.data = this.request.data;
this.source = this.request.source;
this.config = this.request.config;
this.meta = this.request.meta;
this.files = this.request.files;
this.input = this.request.input;
let tid = this.request.contest?.toString() || '';
if (tid === '000000000000000000000000') tid = '';
@@ -77,9 +80,7 @@ export class JudgeTask {
fs.ensureDirSync(this.tmpdir);
tmpfs.mount(this.tmpdir, getConfig('tmpfs_size'));
logger.info('Submission: %s/%s/%s', host, this.source, this.rid);
this.stat.cache_start = new Date();
if (tid === '000000000000000000000001') await this.doHack();
else await this.doSubmission();
await this.doSubmission();
} catch (e) {
if (e instanceof CompileError) {
this.next({ compilerText: compilerText(e.stdout, e.stderr) });
@@ -109,11 +110,10 @@ export class JudgeTask {
}
async doSubmission() {
this.stat.cache_start = new Date();
this.folder = await this.session.cacheOpen(this.source, this.data, this.next);
if ((this.code as any).content.startsWith('@@hydro_submission_file@@')) {
const id = (this.code as any).content.split('@@hydro_submission_file@@')[1]?.split('#')?.[0];
if (!id) throw new SystemError('Submission File Not Found');
const target = await this.session.fetchFile(id);
if (this.files?.code) {
const target = await this.session.fetchFile(this.files?.code);
this.code = { src: target };
this.clean.push(() => fs.remove(target));
}
@@ -122,39 +122,20 @@ export class JudgeTask {
this.folder,
{
detail: this.session.config.detail,
isSelfSubmission: this.config.problemOwner === this.request.uid,
...this.config,
...this.request.config,
},
{
next: this.next,
isSelfSubmission: this.meta.problemOwner === this.request.uid,
key: md5(`${this.source}/${getConfig('secret')}`),
},
{ next: this.next, key: md5(`${this.source}/${getConfig('secret')}`) },
);
this.stat.judge = new Date();
const type = typeof this.input === 'string' ? 'run' : this.config.type || 'default';
const type = this.request.contest?.toString() === '000000000000000000000000' ? 'run'
: this.files?.hack
? 'hack'
: this.config.type || 'default';
if (!judge[type]) throw new FormatError('Unrecognized problemType: {0}', [type]);
await judge[type].judge(this);
}
async doHack() {
this.folder = await this.session.cacheOpen(this.source, this.data, this.next);
if ((this.code as any).content.startsWith('@@hydro_submission_file@@')) {
const id = (this.code as any).content.split('@@hydro_submission_file@@')[1]?.split('#')?.[0];
if (!id) throw new SystemError('Submission File Not Found');
const target = await this.session.fetchFile(id);
this.code = { src: target };
this.clean.push(() => fs.remove(target));
}
this.stat.read_cases = new Date();
this.config = await readCases(
this.folder,
{
detail: this.session.config.detail,
isSelfSubmission: this.config.problemOwner === this.request.uid,
...this.config,
},
{ next: this.next, key: md5(`${this.source}/${getConfig('secret')}`) },
);
this.stat.judge = new Date();
const type = typeof this.input === 'string' ? 'run' : this.config.type || 'default';
if (type !== 'default') throw new FormatError('Cannot hack: only type=default allowed');
await judge.hack.judge(this);
}
}