| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613 |
- #include <iostream>
- #include <algorithm>
- #include <sstream>
- #include <fstream>
- #include <vector>
- #include <set>
- #include <map>
- #include <cstdio>
- #include <cstdlib>
- #include <climits>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <cstring>
- #include <string>
- #include <cstdarg>
- #include <unistd.h>
- #include <sys/file.h>
- #include <sys/wait.h>
- #include "uoj_env.h"
- using namespace std;
- /*========================== execute ====================== */
- string escapeshellarg(const string &arg) {
- string res = "'";
- for (size_t i = 0; i < arg.size(); i++) {
- if (arg[i] == '\'') {
- res += "'\\''";
- } else {
- res += arg[i];
- }
- }
- res += "'";
- return res;
- }
- string realpath(const string &path) {
- char real[PATH_MAX + 1];
- if (realpath(path.c_str(), real) == NULL) {
- return "";
- }
- return real;
- }
- int execute(const char *cmd) {
- return system(cmd);
- }
- int executef(const char *fmt, ...) {
- const int MaxL = 512;
- char cmd[MaxL];
- va_list ap;
- va_start(ap, fmt);
- int res = vsnprintf(cmd, MaxL, fmt, ap);
- if (res < 0 || res >= MaxL) {
- return -1;
- }
- res = execute(cmd);
- va_end(ap);
- return res;
- }
- /*======================== execute End ==================== */
- /*========================= file ====================== */
- string file_preview(const string &name, const size_t &len = 100) {
- FILE *f = fopen(name.c_str(), "r");
- if (f == NULL) {
- return "";
- }
- string res = "";
- int c;
- while (c = fgetc(f), c != EOF && res.size() < len + 4) {
- res += c;
- }
- if (res.size() > len + 3) {
- res.resize(len);
- res += "...";
- }
- fclose(f);
- return res;
- }
- void file_hide_token(const string &name, const string &token) {
- executef("cp %s %s.bak", name.c_str(), name.c_str());
- FILE *rf = fopen((name + ".bak").c_str(), "r");
- FILE *wf = fopen(name.c_str(), "w");
- int c;
- for (int i = 0; i <= (int)token.length(); i++)
- {
- c = fgetc(rf);
- if (c != (i < (int)token.length() ? token[i] : '\n'))
- {
- fprintf(wf, "Unauthorized output\n");
- fclose(rf);
- fclose(wf);
- return;
- }
- }
- while (c = fgetc(rf), c != EOF) {
- fputc(c, wf);
- }
- fclose(rf);
- fclose(wf);
- }
- /*======================= file End ==================== */
- /*====================== parameter ==================== */
- struct RunLimit {
- int time;
- int real_time;
- int memory;
- int output;
- RunLimit() {
- }
- RunLimit(const int &_time, const int &_memory, const int &_output)
- : time(_time), memory(_memory), output(_output), real_time(-1) {
- }
- };
- const RunLimit RL_DEFAULT = RunLimit(1, 256, 64);
- const RunLimit RL_JUDGER_DEFAULT = RunLimit(600, 1024, 128);
- const RunLimit RL_CHECKER_DEFAULT = RunLimit(5, 256, 64);
- const RunLimit RL_INTERACTOR_DEFAULT = RunLimit(1, 256, 64);
- const RunLimit RL_VALIDATOR_DEFAULT = RunLimit(5, 256, 64);
- const RunLimit RL_MARKER_DEFAULT = RunLimit(5, 256, 64);
- const RunLimit RL_COMPILER_DEFAULT = RunLimit(15, 512, 64);
- struct PointInfo {
- int num;
- int scr;
- int ust, usm;
- string info, in, out, res;
- PointInfo(const int &_num, const int &_scr,
- const int &_ust, const int &_usm, const string &_info,
- const string &_in, const string &_out, const string &_res)
- : num(_num), scr(_scr),
- ust(_ust), usm(_usm), info(_info),
- in(_in), out(_out), res(_res) {
- if (info == "default") {
- if (scr == 0) {
- info = "Wrong Answer";
- } else if (scr == 100) {
- info = "Accepted";
- } else {
- info = "Acceptable Answer";
- }
- }
- }
- };
- struct CustomTestInfo {
- int ust, usm;
- string info, exp, out;
- CustomTestInfo(const int &_ust, const int &_usm, const string &_info,
- const string &_exp, const string &_out)
- : ust(_ust), usm(_usm), info(_info),
- exp(_exp), out(_out) {
- }
- };
- struct RunResult {
- int type;
- int ust, usm;
- int exit_code;
- static RunResult failed_result() {
- RunResult res;
- res.type = RS_JGF;
- res.ust = -1;
- res.usm = -1;
- return res;
- }
- static RunResult from_file(const string &file_name) {
- RunResult res;
- FILE *fres = fopen(file_name.c_str(), "r");
- if (fres == NULL || fscanf(fres, "%d %d %d %d", &res.type, &res.ust, &res.usm, &res.exit_code) != 4) {
- return RunResult::failed_result();
- }
- fclose(fres);
- return res;
- }
- };
- struct RunCheckerResult {
- int type;
- int ust, usm;
- int scr;
- string info;
- static RunCheckerResult from_file(const string &file_name, const RunResult &rres) {
- RunCheckerResult res;
- res.type = rres.type;
- res.ust = rres.ust;
- res.usm = rres.usm;
- if (rres.type != RS_AC) {
- res.scr = 0;
- } else {
- FILE *fres = fopen(file_name.c_str(), "r");
- char type[21];
- if (fres == NULL || fscanf(fres, "%20s", type) != 1) {
- return RunCheckerResult::failed_result();
- }
- if (strcmp(type, "ok") == 0) {
- res.scr = 100;
- } else if (strcmp(type, "points") == 0) {
- double d;
- if (fscanf(fres, "%lf", &d) != 1) {
- return RunCheckerResult::failed_result();
- } else {
- res.scr = (int)floor(100 * d + 0.5);
- }
- } else {
- res.scr = 0;
- }
- fclose(fres);
- }
- res.info = file_preview(file_name);
- return res;
- }
-
- static RunCheckerResult failed_result() {
- RunCheckerResult res;
- res.type = RS_JGF;
- res.ust = -1;
- res.usm = -1;
- res.scr = 0;
- res.info = "Checker Judgement Failed";
- return res;
- }
- };
- struct RunValidatorResult {
- int type;
- int ust, usm;
- bool succeeded;
- string info;
-
- static RunValidatorResult failed_result() {
- RunValidatorResult res;
- res.type = RS_JGF;
- res.ust = -1;
- res.usm = -1;
- res.succeeded = 0;
- res.info = "Validator Judgement Failed";
- return res;
- }
- };
- struct RunCompilerResult {
- int type;
- int ust, usm;
- bool succeeded;
- string info;
- static RunCompilerResult failed_result() {
- RunCompilerResult res;
- res.type = RS_JGF;
- res.ust = -1;
- res.usm = -1;
- res.succeeded = false;
- res.info = "Compile Failed";
- return res;
- }
- };
- // see also: run_simple_interaction
- struct RunSimpleInteractionResult {
- RunResult res; // prog
- RunCheckerResult ires; // interactor
- };
- int problem_id;
- string main_path;
- string work_path;
- string data_path;
- string result_path;
- int tot_time = 0;
- int max_memory = 0;
- int tot_score = 0;
- ostringstream details_out;
- //vector<PointInfo> points_info;
- map<string, string> config;
- /*==================== parameter End ================== */
- /*====================== config set =================== */
- void print_config() {
- for (map<string, string>::iterator it = config.begin(); it != config.end(); ++it) {
- cerr << it->first << " = " << it->second << endl;
- }
- }
- void load_config(const string &filename) {
- ifstream fin(filename.c_str());
- if (!fin) {
- return;
- }
- string key;
- string val;
- while (fin >> key >> val) {
- config[key] = val;
- }
- }
- string conf_str(const string &key, int num, const string &val) {
- ostringstream sout;
- sout << key << "_" << num;
- if (config.count(sout.str()) == 0) {
- return val;
- }
- return config[sout.str()];
- }
- string conf_str(const string &key, const string &val) {
- if (config.count(key) == 0) {
- return val;
- }
- return config[key];
- }
- string conf_str(const string &key) {
- return conf_str(key, "");
- }
- int conf_int(const string &key, const int &val) {
- if (config.count(key) == 0) {
- return val;
- }
- return atoi(config[key].c_str());
- }
- int conf_int(const string &key, int num, const int &val) {
- ostringstream sout;
- sout << key << "_" << num;
- if (config.count(sout.str()) == 0) {
- return conf_int(key, val);
- }
- return atoi(config[sout.str()].c_str());
- }
- int conf_int(const string &key) {
- return conf_int(key, 0);
- }
- // problem.conf is uploaded by whoever controls the problem, so input_pre /
- // input_suf / output_pre / output_suf are attacker-controlled. They get
- // concatenated into data_path / work_path by conf_input_file_name() and
- // conf_output_file_name(), so a value like "x/../../../../etc/" used to make
- // the judger read and write outside the problem's own directory. The language
- // side of that bug was remote code execution (the web root was writable); here
- // it is "only" arbitrary read/write inside the judge container, but the same
- // whitelist applies: a filename component is all that is ever legitimate.
- string conf_filename_part(const string &key, const string &val) {
- string s = conf_str(key, val);
- if (s.find("..") != string::npos) {
- return val;
- }
- for (size_t i = 0; i < s.size(); ++i) {
- char c = s[i];
- bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-';
- if (!ok) {
- return val;
- }
- }
- return s;
- }
- string conf_input_file_name(int num) {
- ostringstream name;
- if (num < 0) {
- name << "ex_";
- }
- name << conf_filename_part("input_pre", "input") << abs(num) << "." << conf_filename_part("input_suf", "txt");
- return name.str();
- }
- string conf_output_file_name(int num) {
- ostringstream name;
- if (num < 0) {
- name << "ex_";
- }
- name << conf_filename_part("output_pre", "output") << abs(num) << "." << conf_filename_part("output_suf", "txt");
- return name.str();
- }
- RunLimit conf_run_limit(string pre, const int &num, const RunLimit &val) {
- if (!pre.empty()) {
- pre += "_";
- }
- RunLimit limit;
- limit.time = conf_int(pre + "time_limit", num, val.time);
- limit.memory = conf_int(pre + "memory_limit", num, val.memory);
- limit.output = conf_int(pre + "output_limit", num, val.output);
- return limit;
- }
- RunLimit conf_run_limit(const int &num, const RunLimit &val) {
- return conf_run_limit("", num, val);
- }
- void conf_add(const string &key, const string &val) {
- if (config.count(key)) return;
- config[key] = val;
- }
- bool conf_has(const string &key) {
- return config.count(key) != 0;
- }
- bool conf_is(const string &key, const string &val) {
- if (config.count(key) == 0) return false;
- return config[key] == val;
- }
- /*==================== config set End ================= */
- /*====================== info print =================== */
- template <class T>
- inline string vtos(const T &v) {
- ostringstream sout;
- sout << v;
- return sout.str();
- }
- inline string htmlspecialchars(const string &s) {
- string r;
- for (int i = 0; i < (int)s.length(); i++) {
- switch (s[i]) {
- case '&' : r += "&"; break;
- case '<' : r += "<"; break;
- case '>' : r += ">"; break;
- case '"' : r += """; break;
- case '\0': r += "<b>\\0</b>"; break;
- default : r += s[i]; break;
- }
- }
- return r;
- }
- inline string info_str(int id) {
- switch (id) {
- case RS_MLE: return "Memory Limit Exceeded";
- case RS_TLE: return "Time Limit Exceeded";
- case RS_OLE: return "Output Limit Exceeded";
- case RS_RE : return "Runtime Error";
- case RS_DGS: return "Dangerous Syscalls";
- case RS_JGF: return "Judgement Failed";
- default : return "Unknown Result";
- }
- }
- inline string info_str(const RunResult &p) {
- return info_str(p.type);
- }
- void add_point_info(const PointInfo &info, bool update_tot_score = true) {
- if (info.num >= 0) {
- if(info.ust >= 0) {
- tot_time += info.ust;
- }
- if(info.usm >= 0) {
- max_memory = max(max_memory, info.usm);
- }
- }
- if (update_tot_score) {
- tot_score += info.scr;
- }
- details_out << "<test num=\"" << info.num << "\""
- << " score=\"" << info.scr << "\""
- << " info=\"" << htmlspecialchars(info.info) << "\""
- << " time=\"" << info.ust << "\""
- << " memory=\"" << info.usm << "\">" << endl;
- if (conf_str("show_in", "on") == "on") {
- details_out << "<in>" << htmlspecialchars(info.in) << "</in>" << endl;
- }
- if (conf_str("show_out", "on") == "on") {
- details_out << "<out>" << htmlspecialchars(info.out) << "</out>" << endl;
- }
- if (conf_str("show_res", "on") == "on") {
- details_out << "<res>" << htmlspecialchars(info.res) << "</res>" << endl;
- }
- details_out << "</test>" << endl;
- }
- void add_custom_test_info(const CustomTestInfo &info) {
- if(info.ust >= 0) {
- tot_time += info.ust;
- }
- if(info.usm >= 0) {
- max_memory = max(max_memory, info.usm);
- }
- details_out << "<custom-test info=\"" << htmlspecialchars(info.info) << "\""
- << " time=\"" << info.ust << "\""
- << " memory=\"" << info.usm << "\">" << endl;
- if (!info.exp.empty()) {
- details_out << info.exp << endl;
- }
- details_out << "<out>" << htmlspecialchars(info.out) << "</out>" << endl;
- details_out << "</custom-test>" << endl;
- }
- void add_subtask_info(const int &num, const int &scr, const string &info, const vector<PointInfo> &points) {
- details_out << "<subtask num=\"" << num << "\""
- << " score=\"" << scr << "\""
- << " info=\"" << htmlspecialchars(info) << "\">" << endl;
- tot_score += scr;
- for (vector<PointInfo>::const_iterator it = points.begin(); it != points.end(); it++) {
- add_point_info(*it, false);
- }
- details_out << "</subtask>" << endl;
- }
- void end_judge_ok() {
- FILE *fres = fopen((result_path + "/result.txt").c_str(), "w");
- fprintf(fres, "score %d\n", tot_score);
- fprintf(fres, "time %d\n", tot_time);
- fprintf(fres, "memory %d\n", max_memory);
- fprintf(fres, "details\n");
- fprintf(fres, "<tests>\n");
- fprintf(fres, "%s", details_out.str().c_str());
- fprintf(fres, "</tests>\n");
- fclose(fres);
- exit(0);
- }
- void end_judge_judgement_failed(const string &info) {
- FILE *fres = fopen((result_path + "/result.txt").c_str(), "w");
- fprintf(fres, "error Judgement Failed\n");
- fprintf(fres, "details\n");
- fprintf(fres, "<error>%s</error>\n", htmlspecialchars(info).c_str());
- fclose(fres);
- exit(1);
- }
- void end_judge_compile_error(const RunCompilerResult &res) {
- FILE *fres = fopen((result_path + "/result.txt").c_str(), "w");
- fprintf(fres, "error Compile Error\n");
- fprintf(fres, "details\n");
- fprintf(fres, "<error>%s</error>\n", htmlspecialchars(res.info).c_str());
- fclose(fres);
- exit(0);
- }
- void report_judge_status(const char *status) {
- FILE *f = fopen((result_path + "/cur_status.txt").c_str(), "a");
- if (f == NULL) {
- return;
- }
- if (flock(fileno(f), LOCK_EX) != -1) {
- if (ftruncate(fileno(f), 0) != -1) {
- fprintf(f, "%s\n", status);
- fflush(f);
- }
- flock(fileno(f), LOCK_UN);
- }
- fclose(f);
- }
- bool report_judge_status_f(const char *fmt, ...) {
- const int MaxL = 512;
- char status[MaxL];
- va_list ap;
- va_start(ap, fmt);
- int res = vsnprintf(status, MaxL, fmt, ap);
- if (res < 0 || res >= MaxL) {
- return false;
- }
- report_judge_status(status);
- va_end(ap);
- return true;
- }
- /*==================== info print End ================= */
- /*========================== run ====================== */
- struct RunProgramConfig {
- vector<string> readable_file_names; // other than stdin
- string result_file_name;
- string input_file_name;
- string output_file_name;
- string error_file_name;
- string type;
- string work_path;
- RunLimit limit;
- vector<string> argv;
- RunProgramConfig() {
- int p = 1;
- while (conf_str("readable", p, "") != "") {
- readable_file_names.push_back(conf_str("readable", p, ""));
- p++;
- }
- result_file_name = result_path + "/run_program_result.txt";
- type = "default";
- work_path = ::work_path;
- for (vector<string>::iterator it = readable_file_names.begin(); it != readable_file_names.end(); it++) {
- if (!it->empty() && (*it)[0] != '/') {
- *it = ::work_path + "/" + *it;
- }
- }
- }
- void set_argv(const char *program_name, ...) {
- argv.clear();
- argv.push_back(program_name);
- va_list vl;
- va_start(vl, program_name);
- for (const char *arg = va_arg(vl, const char *); arg; arg = va_arg(vl, const char *)) {
- argv.push_back(arg);
- }
- va_end(vl);
- }
- void set_submission_program_name(string name) {
- string lang = conf_str(string(name) + "_language");
- type = "default";
- string program_name = name;
- if (lang == "Python2") {
- type = "python2";
- } else if (lang == "Python3") {
- type = "python3";
- } else if (lang == "Java8") {
- program_name += "." + conf_str(name + "_main_class");
- type = "java8";
- } else if (lang == "Java11") {
- program_name += "." + conf_str(name + "_main_class");
- type = "java11";
- }
- set_argv(program_name.c_str(), NULL);
- }
- string get_cmd() const {
- ostringstream sout;
- sout << main_path << "/run/run_program"
- << " " << "--res=" << escapeshellarg(result_file_name)
- << " " << "--in=" << escapeshellarg(input_file_name)
- << " " <<"--out=" << escapeshellarg(output_file_name)
- << " " << "--err=" << escapeshellarg(error_file_name)
- << " " << "--tl=" << limit.time
- << " " << "--ml=" << limit.memory
- << " " << "--ol=" << limit.output
- << " " << "--type=" << type
- << " " << "--work-path=" << work_path
- /*<< " " << "--show-trace-details"*/;
- for (vector<string>::const_iterator it = readable_file_names.begin(); it != readable_file_names.end(); it++) {
- sout << " " << "--add-readable=" << escapeshellarg(*it);
- }
- for (vector<string>::const_iterator it = argv.begin(); it != argv.end(); it++) {
- sout << " " << escapeshellarg(*it);
- }
- return sout.str();
- }
- };
- struct PipeConfig {
- int from, to;
- int from_fd, to_fd;
- string saving_file_name;
- PipeConfig() {
- }
- PipeConfig(int _from, int _from_fd, int _to, int _to_fd, const string &_saving_file_name = "")
- : from(_from), from_fd(_from_fd), to(_to), to_fd(_to_fd), saving_file_name(_saving_file_name) {
- }
- };
- struct RunInteractionConfig {
- vector<string> cmds;
- vector<PipeConfig> pipes;
- string get_cmd() const {
- ostringstream sout;
- sout << main_path << "/run/run_interaction";
- for (int i = 0; i < (int)cmds.size(); i++) {
- sout << " " << escapeshellarg(cmds[i]);
- }
- for (int i = 0; i < (int)pipes.size(); i++) {
- sout << " " << "-p";
- sout << " " << pipes[i].from << ":" << pipes[i].from_fd;
- sout << "-" << pipes[i].to << ":" << pipes[i].to_fd;
- if (!pipes[i].saving_file_name.empty()) {
- sout << " " << "-s";
- sout << " " << escapeshellarg(pipes[i].saving_file_name);
- }
- }
- return sout.str();
- }
- };
- // @deprecated
- // will be removed in the future
- RunResult vrun_program(
- const char *run_program_result_file_name,
- const char *input_file_name,
- const char *output_file_name,
- const char *error_file_name,
- const RunLimit &limit,
- const vector<string> &rest) {
- ostringstream sout;
- sout << main_path << "/run/run_program"
- << " " << "--res=" << escapeshellarg(run_program_result_file_name)
- << " " << "--in=" << escapeshellarg(input_file_name)
- << " " <<"--out=" << escapeshellarg(output_file_name)
- << " " << "--err=" << escapeshellarg(error_file_name)
- << " " << "--tl=" << limit.time
- << " " << "--ml=" << limit.memory
- << " " << "--ol=" << limit.output
- /*<< " " << "--show-trace-details"*/;
- for (vector<string>::const_iterator it = rest.begin(); it != rest.end(); it++) {
- sout << " " << escapeshellarg(*it);
- }
- if (execute(sout.str().c_str()) != 0) {
- return RunResult::failed_result();
- }
- return RunResult::from_file(run_program_result_file_name);
- }
- RunResult run_program(const RunProgramConfig &rpc) {
- if (execute(rpc.get_cmd().c_str()) != 0) {
- return RunResult::failed_result();
- }
- return RunResult::from_file(rpc.result_file_name);
- }
- // @return interaction return value
- int run_interaction(const RunInteractionConfig &ric) {
- return execute(ric.get_cmd().c_str());
- }
- RunResult run_program(
- const char *run_program_result_file_name,
- const char *input_file_name,
- const char *output_file_name,
- const char *error_file_name,
- const RunLimit &limit, ...) {
- vector<string> argv;
- va_list vl;
- va_start(vl, limit);
- for (const char *arg = va_arg(vl, const char *); arg; arg = va_arg(vl, const char *)) {
- argv.push_back(arg);
- }
- va_end(vl);
- return vrun_program(run_program_result_file_name,
- input_file_name,
- output_file_name,
- error_file_name,
- limit,
- argv);
- }
- RunValidatorResult run_validator(
- const string &input_file_name,
- const RunLimit &limit,
- const string &program_name) {
- RunResult ret = run_program(
- (string(result_path) + "/run_validator_result.txt").c_str(),
- input_file_name.c_str(),
- "/dev/null",
- (string(result_path) + "/validator_error.txt").c_str(),
- limit,
- program_name.c_str(),
- NULL);
- RunValidatorResult res;
- res.type = ret.type;
- res.ust = ret.ust;
- res.usm = ret.usm;
- if (ret.type != RS_AC || ret.exit_code != 0) {
- res.succeeded = false;
- res.info = file_preview(result_path + "/validator_error.txt");
- } else {
- res.succeeded = true;
- }
- return res;
- }
- RunCheckerResult run_checker(
- const RunLimit &limit,
- const string &program_name,
- const string &input_file_name,
- const string &output_file_name,
- const string &answer_file_name) {
- RunResult ret = run_program(
- (string(result_path) + "/run_checker_result.txt").c_str(),
- "/dev/null",
- "/dev/null",
- (string(result_path) + "/checker_error.txt").c_str(),
- limit,
- ("--add-readable=" + input_file_name).c_str(),
- ("--add-readable=" + output_file_name).c_str(),
- ("--add-readable=" + answer_file_name).c_str(),
- program_name.c_str(),
- realpath(input_file_name).c_str(),
- realpath(output_file_name).c_str(),
- realpath(answer_file_name).c_str(),
- NULL);
- return RunCheckerResult::from_file(result_path + "/checker_error.txt", ret);
- }
- RunCompilerResult run_compiler(const char *path, ...) {
- vector<string> argv;
- argv.push_back("--type=compiler");
- argv.push_back(string("--work-path=") + path);
- va_list vl;
- va_start(vl, path);
- for (const char *arg = va_arg(vl, const char *); arg; arg = va_arg(vl, const char *)) {
- argv.push_back(arg);
- }
- va_end(vl);
- RunResult ret = vrun_program(
- (result_path + "/run_compiler_result.txt").c_str(),
- "/dev/null",
- "stderr",
- (result_path + "/compiler_result.txt").c_str(),
- RL_COMPILER_DEFAULT,
- argv);
- RunCompilerResult res;
- res.type = ret.type;
- res.ust = ret.ust;
- res.usm = ret.usm;
- res.succeeded = ret.type == RS_AC && ret.exit_code == 0;
- if (!res.succeeded) {
- if (ret.type == RS_AC) {
- res.info = file_preview(result_path + "/compiler_result.txt", 500);
- } else if (ret.type == RS_JGF) {
- res.info = "No Comment";
- } else {
- res.info = "Compiler " + info_str(ret.type);
- }
- }
- return res;
- }
- RunResult run_submission_program(
- const string &input_file_name,
- const string &output_file_name,
- const RunLimit &limit,
- const string &name,
- RunProgramConfig rpc = RunProgramConfig()) {
- rpc.result_file_name = result_path + "/run_submission_program.txt";
- if (!getenv("ONLINE_JUDGE_UBSAN")) {
- rpc.output_file_name = output_file_name;
- rpc.error_file_name = "/dev/null";
- }
- else {
- rpc.output_file_name = "/dev/null";
- rpc.error_file_name = output_file_name;
- }
-
- rpc.input_file_name = input_file_name;
- rpc.limit = limit;
- rpc.set_submission_program_name(name);
- RunResult res = run_program(rpc);
- if (res.type == RS_AC && res.exit_code != 0) {
- res.type = RS_RE;
- }
- return res;
- }
- void prepare_interactor() {
- static bool prepared = false;
- if (prepared) {
- return;
- }
- string data_path_std = data_path + "/interactor";
- string work_path_std = work_path + "/interactor";
- executef("cp %s %s", data_path_std.c_str(), work_path_std.c_str());
- conf_add("interactor_language", "C++");
- prepared = true;
- }
- // simple: prog <---> interactor <---> data
- RunSimpleInteractionResult run_simple_interation(
- const string &input_file_name,
- const string &answer_file_name,
- const string &real_input_file_name,
- const string &real_output_file_name,
- const RunLimit &limit,
- const RunLimit &ilimit,
- const string &name,
- RunProgramConfig rpc = RunProgramConfig(),
- RunProgramConfig irpc = RunProgramConfig()) {
- prepare_interactor();
- rpc.result_file_name = result_path + "/run_submission_program.txt";
- rpc.input_file_name = "stdin";
- rpc.output_file_name = "stdout";
- rpc.error_file_name = "/dev/null";
- rpc.limit = limit;
- rpc.set_submission_program_name(name);
- irpc.result_file_name = result_path + "/run_interactor_program.txt";
- irpc.readable_file_names.push_back(input_file_name);
- irpc.readable_file_names.push_back(answer_file_name);
- irpc.input_file_name = "stdin";
- irpc.output_file_name = "stdout";
- irpc.error_file_name = result_path + "/interactor_error.txt";
- irpc.limit = ilimit;
- irpc.set_submission_program_name("interactor");
- irpc.argv.push_back(input_file_name);
- irpc.argv.push_back("/dev/stdin");
- irpc.argv.push_back(answer_file_name);
- irpc.limit.real_time = rpc.limit.real_time = rpc.limit.time + irpc.limit.time + 1;
- RunInteractionConfig ric;
- ric.cmds.push_back(rpc.get_cmd());
- ric.cmds.push_back(irpc.get_cmd());
- // from:fd, to:fd
- ric.pipes.push_back(PipeConfig(2, 1, 1, 0, real_input_file_name));
- ric.pipes.push_back(PipeConfig(1, 1, 2, 0, real_output_file_name));
-
- run_interaction(ric);
- RunSimpleInteractionResult rires;
- RunResult res = RunResult::from_file(rpc.result_file_name);
- RunCheckerResult ires = RunCheckerResult::from_file(irpc.error_file_name, RunResult::from_file(irpc.result_file_name));
- if (res.type == RS_AC && res.exit_code != 0) {
- res.type = RS_RE;
- }
- if (ires.type == RS_JGF) {
- ires.info = "Interactor Judgement Failed";
- }
- if (ires.type == RS_TLE) {
- ires.type = RS_AC;
- res.type = RS_TLE;
- }
- rires.res = res;
- rires.ires = ires;
- return rires;
- }
- void prepare_run_standard_program() {
- static bool prepared = false;
- if (prepared) {
- return;
- }
- string data_path_std = data_path + "/std";
- string work_path_std = work_path + "/std";
- executef("cp %s %s", data_path_std.c_str(), work_path_std.c_str());
- conf_add("std_language", "C++");
- prepared = true;
- }
- // @deprecated
- // will be removed in the future
- RunResult run_standard_program(
- const string &input_file_name,
- const string &output_file_name,
- const RunLimit &limit,
- RunProgramConfig rpc = RunProgramConfig()) {
- prepare_run_standard_program();
- rpc.result_file_name = result_path + "/run_standard_program.txt";
- return run_submission_program(
- input_file_name,
- output_file_name,
- limit,
- "std",
- rpc);
- }
- /*======================== run End ==================== */
- /*======================== compile ==================== */
- bool is_illegal_keyword(const string &name) {
- if (name == "__asm" || name == "__asm__" || name == "asm")
- return true;
- return false;
- }
- bool has_illegal_keywords_in_file(const string &name) {
- FILE *f = fopen(name.c_str(), "r");
- int c;
- string key;
- while ((c = fgetc(f)) != EOF)
- {
- if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_')
- {
- if (key.size() < 20)
- key += c;
- else
- {
- if (is_illegal_keyword(key))
- return true;
- key.erase(key.begin());
- key += c;
- }
- }
- else
- {
- if (is_illegal_keyword(key))
- return true;
- key.clear();
- }
- }
- if (is_illegal_keyword(key))
- return true;
- fclose(f);
- return false;
- }
- RunCompilerResult prepare_java_source(const string &name, const string &path = work_path) {
- FILE *f = fopen((path + "/" + name + ".code").c_str(), "r");
- const int L = 1024;
- std::string s;
- char buf[L + 1];
- int mode = 0;
- while (!feof(f)) {
- buf[fread(buf, 1, L, f)] = '\0';
- for (char *p = buf; *p; p++) {
- s += *p;
- switch (mode) {
- case 0:
- switch (*p) {
- case '/':
- mode = 1;
- break;
- case '\'':
- mode = 5;
- break;
- case '\"':
- mode = 6;
- break;
- }
- break;
- case 1:
- switch (*p) {
- case '/':
- mode = 2;
- s.resize(s.length() - 2);
- break;
- case '*':
- mode = 3;
- s.resize(s.length() - 2);
- break;
- }
- break;
- case 2:
- s.resize(s.length() - 1);
- switch (*p) {
- case '\n':
- s += '\n';
- mode = 0;
- break;
- }
- break;
- case 3:
- s.resize(s.length() - 1);
- switch (*p) {
- case '*':
- mode = 4;
- break;
- }
- break;
- case 4:
- s.resize(s.length() - 1);
- switch (*p) {
- case '/':
- s += ' ';
- mode = 0;
- break;
- }
- break;
- case 5:
- switch (*p) {
- case '\'':
- mode = 0;
- break;
- case '\\':
- mode = 7;
- break;
- }
- case 6:
- switch (*p) {
- case '\"':
- mode = 0;
- break;
- case '\\':
- mode = 8;
- break;
- }
- case 7:
- mode = 5;
- break;
- case 8:
- mode = 6;
- break;
- }
- }
- }
- bool valid[256];
- fill(valid, valid + 256, false);
- for (int c = 'a'; c <= 'z'; c++)
- valid[c] = true;
- for (int c = 'A'; c <= 'Z'; c++)
- valid[c] = true;
- valid['.'] = true;
- valid['_'] = true;
- vector<string> tokens;
- for (int p = 0, np = 0; p < (int)s.length(); p = np) {
- while (np < (int)s.length() && valid[(unsigned char)s[np]]) {
- np++;
- }
- if (np == p) {
- np++;
- } else {
- tokens.push_back(s.substr(p, np - p));
- }
- }
- if (tokens.size() > 0 && tokens[0] == "package") {
- RunCompilerResult res;
- res.type = RS_WA;
- res.ust = -1;
- res.usm = -1;
- res.succeeded = false;
- res.info = "Please don't specify the package.";
- return res;
- }
- for (int i = 0; i + 1 < (int)tokens.size(); i++) {
- if (tokens[i] == "class") {
- if (tokens[i + 1].length() <= 100) {
- config[name + "_main_class"] = tokens[i + 1];
- RunCompilerResult res;
- res.type = RS_AC;
- res.ust = 0;
- res.usm = 0;
- res.succeeded = true;
- return res;
- } else {
- RunCompilerResult res;
- res.type = RS_WA;
- res.ust = -1;
- res.usm = -1;
- res.succeeded = false;
- res.info = "The name of the main class is too long.";
- return res;
- }
- }
- }
- RunCompilerResult res;
- res.type = RS_WA;
- res.ust = -1;
- res.usm = -1;
- res.succeeded = false;
- res.info = "Can't find the main class.";
- return res;
- }
- RunCompilerResult compile_c(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/gcc", "-o", name.c_str(), "-x", "c", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
- }
- RunCompilerResult compile_pas(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/fpc", (name + ".code").c_str(), "-O2", NULL);
- }
- RunCompilerResult compile_cpp14(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/g++", "-std=c++14", "-o", name.c_str(), "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
- }
- RunCompilerResult compile_cpp17(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/g++", "-std=c++17", "-o", name.c_str(), "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
- }
- RunCompilerResult compile_cpp17ubsan(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/g++", "-fsanitize=undefined", "-std=c++17", "-o", name.c_str(), "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
- }
- RunCompilerResult compile_python2(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/python2", "-E", "-s", "-B", "-O", "-c",
- ("import py_compile\nimport sys\ntry:\n py_compile.compile('" + name + ".code'" + ", '" + name + "', doraise=True)\n sys.exit(0)\nexcept Exception as e:\n print e\n sys.exit(1)").c_str(), NULL);
- }
- RunCompilerResult compile_python3(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/python3", "-I", "-B", "-O", "-c", ("import py_compile\nimport sys\ntry:\n py_compile.compile('" + name + ".code'" + ", '" + name + "', doraise=True)\n sys.exit(0)\nexcept Exception as e:\n print(e)\n sys.exit(1)").c_str(), NULL);
- }
- RunCompilerResult compile_java8(const string &name, const string &path = work_path) {
- RunCompilerResult ret = prepare_java_source(name, path);
- if (!ret.succeeded)
- return ret;
- string main_class = conf_str(name + "_main_class");
- executef("rm %s/%s -rf 2>/dev/null; mkdir %s/%s", path.c_str(), name.c_str(), path.c_str(), name.c_str());
- executef("echo package %s\\; | cat - %s/%s.code >%s/%s/%s.java", name.c_str(), path.c_str(), name.c_str(), path.c_str(), name.c_str(), main_class.c_str());
- return run_compiler((path + "/" + name).c_str(),
- "/usr/lib/jvm/java-8-openjdk-amd64/bin/javac", (main_class + ".java").c_str(), NULL);
- }
- RunCompilerResult compile_java11(const string &name, const string &path = work_path) {
- RunCompilerResult ret = prepare_java_source(name, path);
- if (!ret.succeeded)
- return ret;
- string main_class = conf_str(name + "_main_class");
- executef("rm %s/%s -rf 2>/dev/null; mkdir %s/%s", path.c_str(), name.c_str(), path.c_str(), name.c_str());
- executef("echo package %s\\; | cat - %s/%s.code >%s/%s/%s.java", name.c_str(), path.c_str(), name.c_str(), path.c_str(), name.c_str(), main_class.c_str());
- return run_compiler((path + "/" + name).c_str(),
- "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac", (main_class + ".java").c_str(), NULL);
- }
- // ryp
- RunCompilerResult compile(const char *name) {
- string lang = conf_str(string(name) + "_language");
- if (lang == "C++17ubsan")
- setenv ("ONLINE_JUDGE_UBSAN", "1", 1);
- if ((lang == "C++14" || lang == "C++17" || lang == "C++17ubsan" || lang == "C") && has_illegal_keywords_in_file(work_path + "/" + name + ".code"))
- {
- RunCompilerResult res;
- res.type = RS_DGS;
- res.ust = -1;
- res.usm = -1;
- res.succeeded = false;
- res.info = "Compile Failed";
- return res;
- }
- if (lang == "C++14") {
- return compile_cpp14(name);
- }
- if (lang == "C++17") {
- return compile_cpp17(name);
- }
- if (lang == "C++17ubsan") {
- return compile_cpp17ubsan(name);
- }
- if (lang == "Python2") {
- return compile_python2(name);
- }
- if (lang == "Python3") {
- return compile_python3(name);
- }
- if (lang == "Java8") {
- return compile_java8(name);
- }
- if (lang == "Java11") {
- return compile_java11(name);
- }
- if (lang == "C") {
- return compile_c(name);
- }
- if (lang == "Pascal") {
- return compile_pas(name);
- }
- RunCompilerResult res = RunCompilerResult::failed_result();
- res.info = "This language is not supported yet.";
- return res;
- }
- RunCompilerResult compile_c_with_implementer(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/gcc", "-o", name.c_str(), "implementer.c", "-x", "c", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
- }
- RunCompilerResult compile_pas_with_implementer(const string &name, const string &path = work_path) {
- executef("cp %s %s", (path + "/" + name + ".code").c_str(), (path + "/" + conf_str(name + "_unit_name") + ".pas").c_str());
- return run_compiler(path.c_str(),
- "/usr/bin/fpc", "implementer.pas", ("-o" + name).c_str(), "-O2", NULL);
- }
- RunCompilerResult compile_cpp14_with_implementer(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/g++", "-std=c++14", "-o", name.c_str(), "implementer.cpp", "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
- }
- RunCompilerResult compile_cpp17_with_implementer(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/g++", "-std=c++17", "-o", name.c_str(), "implementer.cpp", "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", "-std=c++11", NULL);
- }
- /*
- RunCompilerResult compile_python2(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/python2", "-E", "-s", "-B", "-O", "-c",
- ("import py_compile\nimport sys\ntry:\n py_compile.compile('" + name + ".code'" + ", '" + name + "', doraise=True)\n sys.exit(0)\nexcept Exception as e:\n print e\n sys.exit(1)").c_str(), NULL);
- }
- RunCompilerResult compile_python3(const string &name, const string &path = work_path) {
- return run_compiler(path.c_str(),
- "/usr/bin/python3", "-I", "-B", "-O", "-c", ("import py_compile\nimport sys\ntry:\n py_compile.compile('" + name + ".code'" + ", '" + name + "', doraise=True)\n sys.exit(0)\nexcept Exception as e:\n print(e)\n sys.exit(1)").c_str(), NULL);
- }
- */
- RunCompilerResult compile_with_implementer(const char *name) {
- string lang = conf_str(string(name) + "_language");
- if (has_illegal_keywords_in_file(work_path + "/" + name + ".code"))
- {
- RunCompilerResult res;
- res.type = RS_DGS;
- res.ust = -1;
- res.usm = -1;
- res.succeeded = false;
- res.info = "Compile Failed";
- return res;
- }
- if (lang == "C++14") {
- return compile_cpp14_with_implementer(name);
- }
- if (lang == "C++17") {
- return compile_cpp17_with_implementer(name);
- }
- if (lang == "C") {
- return compile_c_with_implementer(name);
- }
- if (lang == "Pascal") {
- return compile_pas_with_implementer(name);
- }
- RunCompilerResult res = RunCompilerResult::failed_result();
- res.info = "This language is not supported yet.";
- return res;
- }
- /*====================== compile End ================== */
- /*====================== test ================== */
- struct TestPointConfig {
- int submit_answer;
- int validate_input_before_test;
- string input_file_name;
- string output_file_name;
- string answer_file_name;
- TestPointConfig()
- : submit_answer(-1), validate_input_before_test(-1) {
- }
- void auto_complete(int num) {
- if (submit_answer == -1) {
- submit_answer = conf_is("submit_answer", "on");
- }
- if (validate_input_before_test == -1) {
- validate_input_before_test = conf_is("validate_input_before_test", "on");
- }
- if (input_file_name.empty()) {
- input_file_name = data_path + "/" + conf_input_file_name(num);
- }
- if (output_file_name.empty()) {
- output_file_name = work_path + "/" + conf_output_file_name(num);
- }
- if (answer_file_name.empty()) {
- answer_file_name = data_path + "/" + conf_output_file_name(num);
- }
- }
- };
- PointInfo test_point(const string &name, const int &num, TestPointConfig tpc = TestPointConfig()) {
- tpc.auto_complete(num);
- if (tpc.validate_input_before_test) {
- RunValidatorResult val_ret = run_validator(
- tpc.input_file_name,
- conf_run_limit("validator", 0, RL_VALIDATOR_DEFAULT),
- conf_str("validator"));
- if (val_ret.type != RS_AC) {
- return PointInfo(num, 0, -1, -1,
- "Validator " + info_str(val_ret.type),
- file_preview(tpc.input_file_name), "",
- "");
- } else if (!val_ret.succeeded) {
- return PointInfo(num, 0, -1, -1,
- "Invalid Input",
- file_preview(tpc.input_file_name), "",
- val_ret.info);
- }
- }
- if (!conf_is("interaction_mode", "on")) {
- RunResult pro_ret;
- if (!tpc.submit_answer) {
- pro_ret = run_submission_program(
- tpc.input_file_name.c_str(),
- tpc.output_file_name.c_str(),
- conf_run_limit(num, RL_DEFAULT),
- name);
- if (conf_has("token")) {
- file_hide_token(tpc.output_file_name, conf_str("token", ""));
- }
- if (pro_ret.type != RS_AC) {
- return PointInfo(num, 0, -1, -1,
- info_str(pro_ret.type),
- file_preview(tpc.input_file_name), file_preview(tpc.output_file_name),
- "");
- }
- } else {
- pro_ret.type = RS_AC;
- pro_ret.ust = -1;
- pro_ret.usm = -1;
- pro_ret.exit_code = 0;
- }
- RunCheckerResult chk_ret = run_checker(
- conf_run_limit("checker", num, RL_CHECKER_DEFAULT),
- conf_str("checker"),
- tpc.input_file_name,
- tpc.output_file_name,
- tpc.answer_file_name);
- if (chk_ret.type != RS_AC) {
- return PointInfo(num, 0, -1, -1,
- "Checker " + info_str(chk_ret.type),
- file_preview(tpc.input_file_name), file_preview(tpc.output_file_name),
- "");
- }
- return PointInfo(num, chk_ret.scr, pro_ret.ust, pro_ret.usm,
- "default",
- file_preview(tpc.input_file_name), file_preview(tpc.output_file_name),
- chk_ret.info);
- } else {
- string real_output_file_name = tpc.output_file_name + ".real_input.txt";
- string real_input_file_name = tpc.output_file_name + ".real_output.txt";
- RunSimpleInteractionResult rires = run_simple_interation(
- tpc.input_file_name,
- tpc.answer_file_name,
- real_input_file_name,
- real_output_file_name,
- conf_run_limit(num, RL_DEFAULT),
- conf_run_limit("interactor", num, RL_INTERACTOR_DEFAULT),
- name);
- if (rires.ires.type != RS_AC) {
- return PointInfo(num, 0, -1, -1,
- "Interactor " + info_str(rires.ires.type),
- file_preview(real_input_file_name), file_preview(real_output_file_name),
- "");
- }
- if (rires.res.type != RS_AC) {
- return PointInfo(num, 0, -1, -1,
- info_str(rires.res.type),
- file_preview(real_input_file_name), file_preview(real_output_file_name),
- "");
- }
- return PointInfo(num, rires.ires.scr, rires.res.ust, rires.res.usm,
- "default",
- file_preview(real_input_file_name), file_preview(real_output_file_name),
- rires.ires.info);
- }
- }
- PointInfo test_hack_point(const string &name, TestPointConfig tpc) {
- tpc.submit_answer = false;
- tpc.validate_input_before_test = false;
- tpc.auto_complete(0);
- RunValidatorResult val_ret = run_validator(
- tpc.input_file_name,
- conf_run_limit("validator", 0, RL_VALIDATOR_DEFAULT),
- conf_str("validator"));
- if (val_ret.type != RS_AC) {
- return PointInfo(0, 0, -1, -1,
- "Validator " + info_str(val_ret.type),
- file_preview(tpc.input_file_name), "",
- "");
- } else if (!val_ret.succeeded) {
- return PointInfo(0, 0, -1, -1,
- "Invalid Input",
- file_preview(tpc.input_file_name), "",
- val_ret.info);
- }
- RunLimit default_std_run_limit = conf_run_limit(0, RL_DEFAULT);
- prepare_run_standard_program();
- if (!conf_is("interaction_mode", "on")) {
- RunProgramConfig rpc;
- rpc.result_file_name = result_path + "/run_standard_program.txt";
- RunResult std_ret = run_submission_program(
- tpc.input_file_name,
- tpc.answer_file_name,
- conf_run_limit("standard", 0, default_std_run_limit),
- "std",
- rpc);
- if (std_ret.type != RS_AC) {
- return PointInfo(0, 0, -1, -1,
- "Standard Program " + info_str(std_ret.type),
- file_preview(tpc.input_file_name), "",
- "");
- }
- if (conf_has("token")) {
- file_hide_token(tpc.answer_file_name, conf_str("token", ""));
- }
- } else {
- RunProgramConfig rpc;
- rpc.result_file_name = result_path + "/run_standard_program.txt";
- string real_output_file_name = tpc.answer_file_name;
- string real_input_file_name = tpc.output_file_name + ".real_output.txt";
- RunSimpleInteractionResult rires = run_simple_interation(
- tpc.input_file_name,
- tpc.answer_file_name,
- real_input_file_name,
- real_output_file_name,
- conf_run_limit("standard", 0, default_std_run_limit),
- conf_run_limit("interactor", 0, RL_INTERACTOR_DEFAULT),
- "std",
- rpc);
- if (rires.ires.type != RS_AC) {
- return PointInfo(0, 0, -1, -1,
- "Interactor " + info_str(rires.ires.type) + " (Standard Program)",
- file_preview(real_input_file_name), "",
- "");
- }
- if (rires.res.type != RS_AC) {
- return PointInfo(0, 0, -1, -1,
- "Standard Program " + info_str(rires.res.type),
- file_preview(real_input_file_name), "",
- "");
- }
- }
- PointInfo po = test_point(name, 0, tpc);
- po.scr = po.scr != 100;
- return po;
- }
- CustomTestInfo ordinary_custom_test(const string &name) {
- RunLimit lim = conf_run_limit(0, RL_DEFAULT);
- lim.time += 2;
- string input_file_name = work_path + "/input.txt";
- string output_file_name = work_path + "/output.txt";
- RunResult pro_ret = run_submission_program(
- input_file_name,
- output_file_name,
- lim,
- name);
- if (conf_has("token")) {
- file_hide_token(output_file_name, conf_str("token", ""));
- }
- string info;
- if (pro_ret.type == RS_AC) {
- info = "Success";
- } else {
- info = info_str(pro_ret.type);
- }
- string exp;
- if (pro_ret.type == RS_TLE) {
- exp = "<p>[<strong>time limit:</strong> " + vtos(lim.time) + "s]</p>";
- }
- return CustomTestInfo(pro_ret.ust, pro_ret.usm,
- info, exp, file_preview(output_file_name, 2048));
- }
- int scale_score(int scr100, int full) {
- return scr100 * full / 100;
- }
- /*====================== test End ================== */
- /*======================= conf init =================== */
- void main_judger_init(int argc, char **argv) {
- main_path = UOJ_WORK_PATH;
- work_path = main_path + "/work";
- result_path = string(UOJ_RESULT_PATH);
- load_config(work_path + "/submission.conf");
- problem_id = conf_int("problem_id");
- data_path = string(UOJ_DATA_PATH) + "/" + conf_str("problem_id");
- load_config(data_path + "/problem.conf");
- executef("cp %s/require/* %s 2>/dev/null", data_path.c_str(), work_path.c_str());
- if (conf_is("use_builtin_judger", "on")) {
- config["judger"] = string(UOJ_WORK_PATH) + "/builtin/judger/judger";
- } else {
- config["judger"] = data_path + "/judger";
- }
- }
- void judger_init(int argc, char **argv) {
- if (argc != 5) {
- exit(1);
- }
- main_path = argv[1];
- work_path = argv[2];
- result_path = argv[3];
- data_path = argv[4];
- load_config(work_path + "/submission.conf");
- problem_id = conf_int("problem_id");
- load_config(data_path + "/problem.conf");
- if (config.count("use_builtin_checker")) {
- config["checker"] = main_path + "/builtin/checker/" + config["use_builtin_checker"];
- } else {
- config["checker"] = data_path + "/chk";
- }
- config["validator"] = data_path + "/val";
- }
- /*===================== conf init End ================= */
|