uoj_judger.h 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <sstream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <climits>
  11. #include <cmath>
  12. #include <cstdlib>
  13. #include <ctime>
  14. #include <cstring>
  15. #include <string>
  16. #include <cstdarg>
  17. #include <unistd.h>
  18. #include <sys/file.h>
  19. #include <sys/wait.h>
  20. #include "uoj_env.h"
  21. using namespace std;
  22. /*========================== execute ====================== */
  23. string escapeshellarg(const string &arg) {
  24. string res = "'";
  25. for (size_t i = 0; i < arg.size(); i++) {
  26. if (arg[i] == '\'') {
  27. res += "'\\''";
  28. } else {
  29. res += arg[i];
  30. }
  31. }
  32. res += "'";
  33. return res;
  34. }
  35. string realpath(const string &path) {
  36. char real[PATH_MAX + 1];
  37. if (realpath(path.c_str(), real) == NULL) {
  38. return "";
  39. }
  40. return real;
  41. }
  42. int execute(const char *cmd) {
  43. return system(cmd);
  44. }
  45. int executef(const char *fmt, ...) {
  46. const int MaxL = 512;
  47. char cmd[MaxL];
  48. va_list ap;
  49. va_start(ap, fmt);
  50. int res = vsnprintf(cmd, MaxL, fmt, ap);
  51. if (res < 0 || res >= MaxL) {
  52. return -1;
  53. }
  54. res = execute(cmd);
  55. va_end(ap);
  56. return res;
  57. }
  58. /*======================== execute End ==================== */
  59. /*========================= file ====================== */
  60. string file_preview(const string &name, const size_t &len = 100) {
  61. FILE *f = fopen(name.c_str(), "r");
  62. if (f == NULL) {
  63. return "";
  64. }
  65. string res = "";
  66. int c;
  67. while (c = fgetc(f), c != EOF && res.size() < len + 4) {
  68. res += c;
  69. }
  70. if (res.size() > len + 3) {
  71. res.resize(len);
  72. res += "...";
  73. }
  74. fclose(f);
  75. return res;
  76. }
  77. void file_hide_token(const string &name, const string &token) {
  78. executef("cp %s %s.bak", name.c_str(), name.c_str());
  79. FILE *rf = fopen((name + ".bak").c_str(), "r");
  80. FILE *wf = fopen(name.c_str(), "w");
  81. int c;
  82. for (int i = 0; i <= (int)token.length(); i++)
  83. {
  84. c = fgetc(rf);
  85. if (c != (i < (int)token.length() ? token[i] : '\n'))
  86. {
  87. fprintf(wf, "Unauthorized output\n");
  88. fclose(rf);
  89. fclose(wf);
  90. return;
  91. }
  92. }
  93. while (c = fgetc(rf), c != EOF) {
  94. fputc(c, wf);
  95. }
  96. fclose(rf);
  97. fclose(wf);
  98. }
  99. /*======================= file End ==================== */
  100. /*====================== parameter ==================== */
  101. struct RunLimit {
  102. int time;
  103. int real_time;
  104. int memory;
  105. int output;
  106. RunLimit() {
  107. }
  108. RunLimit(const int &_time, const int &_memory, const int &_output)
  109. : time(_time), memory(_memory), output(_output), real_time(-1) {
  110. }
  111. };
  112. const RunLimit RL_DEFAULT = RunLimit(1, 256, 64);
  113. const RunLimit RL_JUDGER_DEFAULT = RunLimit(600, 1024, 128);
  114. const RunLimit RL_CHECKER_DEFAULT = RunLimit(5, 256, 64);
  115. const RunLimit RL_INTERACTOR_DEFAULT = RunLimit(1, 256, 64);
  116. const RunLimit RL_VALIDATOR_DEFAULT = RunLimit(5, 256, 64);
  117. const RunLimit RL_MARKER_DEFAULT = RunLimit(5, 256, 64);
  118. const RunLimit RL_COMPILER_DEFAULT = RunLimit(15, 512, 64);
  119. struct PointInfo {
  120. int num;
  121. int scr;
  122. int ust, usm;
  123. string info, in, out, res;
  124. PointInfo(const int &_num, const int &_scr,
  125. const int &_ust, const int &_usm, const string &_info,
  126. const string &_in, const string &_out, const string &_res)
  127. : num(_num), scr(_scr),
  128. ust(_ust), usm(_usm), info(_info),
  129. in(_in), out(_out), res(_res) {
  130. if (info == "default") {
  131. if (scr == 0) {
  132. info = "Wrong Answer";
  133. } else if (scr == 100) {
  134. info = "Accepted";
  135. } else {
  136. info = "Acceptable Answer";
  137. }
  138. }
  139. }
  140. };
  141. struct CustomTestInfo {
  142. int ust, usm;
  143. string info, exp, out;
  144. CustomTestInfo(const int &_ust, const int &_usm, const string &_info,
  145. const string &_exp, const string &_out)
  146. : ust(_ust), usm(_usm), info(_info),
  147. exp(_exp), out(_out) {
  148. }
  149. };
  150. struct RunResult {
  151. int type;
  152. int ust, usm;
  153. int exit_code;
  154. static RunResult failed_result() {
  155. RunResult res;
  156. res.type = RS_JGF;
  157. res.ust = -1;
  158. res.usm = -1;
  159. return res;
  160. }
  161. static RunResult from_file(const string &file_name) {
  162. RunResult res;
  163. FILE *fres = fopen(file_name.c_str(), "r");
  164. if (fres == NULL || fscanf(fres, "%d %d %d %d", &res.type, &res.ust, &res.usm, &res.exit_code) != 4) {
  165. return RunResult::failed_result();
  166. }
  167. fclose(fres);
  168. return res;
  169. }
  170. };
  171. struct RunCheckerResult {
  172. int type;
  173. int ust, usm;
  174. int scr;
  175. string info;
  176. static RunCheckerResult from_file(const string &file_name, const RunResult &rres) {
  177. RunCheckerResult res;
  178. res.type = rres.type;
  179. res.ust = rres.ust;
  180. res.usm = rres.usm;
  181. if (rres.type != RS_AC) {
  182. res.scr = 0;
  183. } else {
  184. FILE *fres = fopen(file_name.c_str(), "r");
  185. char type[21];
  186. if (fres == NULL || fscanf(fres, "%20s", type) != 1) {
  187. return RunCheckerResult::failed_result();
  188. }
  189. if (strcmp(type, "ok") == 0) {
  190. res.scr = 100;
  191. } else if (strcmp(type, "points") == 0) {
  192. double d;
  193. if (fscanf(fres, "%lf", &d) != 1) {
  194. return RunCheckerResult::failed_result();
  195. } else {
  196. res.scr = (int)floor(100 * d + 0.5);
  197. }
  198. } else {
  199. res.scr = 0;
  200. }
  201. fclose(fres);
  202. }
  203. res.info = file_preview(file_name);
  204. return res;
  205. }
  206. static RunCheckerResult failed_result() {
  207. RunCheckerResult res;
  208. res.type = RS_JGF;
  209. res.ust = -1;
  210. res.usm = -1;
  211. res.scr = 0;
  212. res.info = "Checker Judgement Failed";
  213. return res;
  214. }
  215. };
  216. struct RunValidatorResult {
  217. int type;
  218. int ust, usm;
  219. bool succeeded;
  220. string info;
  221. static RunValidatorResult failed_result() {
  222. RunValidatorResult res;
  223. res.type = RS_JGF;
  224. res.ust = -1;
  225. res.usm = -1;
  226. res.succeeded = 0;
  227. res.info = "Validator Judgement Failed";
  228. return res;
  229. }
  230. };
  231. struct RunCompilerResult {
  232. int type;
  233. int ust, usm;
  234. bool succeeded;
  235. string info;
  236. static RunCompilerResult failed_result() {
  237. RunCompilerResult res;
  238. res.type = RS_JGF;
  239. res.ust = -1;
  240. res.usm = -1;
  241. res.succeeded = false;
  242. res.info = "Compile Failed";
  243. return res;
  244. }
  245. };
  246. // see also: run_simple_interaction
  247. struct RunSimpleInteractionResult {
  248. RunResult res; // prog
  249. RunCheckerResult ires; // interactor
  250. };
  251. int problem_id;
  252. string main_path;
  253. string work_path;
  254. string data_path;
  255. string result_path;
  256. int tot_time = 0;
  257. int max_memory = 0;
  258. int tot_score = 0;
  259. ostringstream details_out;
  260. //vector<PointInfo> points_info;
  261. map<string, string> config;
  262. /*==================== parameter End ================== */
  263. /*====================== config set =================== */
  264. void print_config() {
  265. for (map<string, string>::iterator it = config.begin(); it != config.end(); ++it) {
  266. cerr << it->first << " = " << it->second << endl;
  267. }
  268. }
  269. void load_config(const string &filename) {
  270. ifstream fin(filename.c_str());
  271. if (!fin) {
  272. return;
  273. }
  274. string key;
  275. string val;
  276. while (fin >> key >> val) {
  277. config[key] = val;
  278. }
  279. }
  280. string conf_str(const string &key, int num, const string &val) {
  281. ostringstream sout;
  282. sout << key << "_" << num;
  283. if (config.count(sout.str()) == 0) {
  284. return val;
  285. }
  286. return config[sout.str()];
  287. }
  288. string conf_str(const string &key, const string &val) {
  289. if (config.count(key) == 0) {
  290. return val;
  291. }
  292. return config[key];
  293. }
  294. string conf_str(const string &key) {
  295. return conf_str(key, "");
  296. }
  297. int conf_int(const string &key, const int &val) {
  298. if (config.count(key) == 0) {
  299. return val;
  300. }
  301. return atoi(config[key].c_str());
  302. }
  303. int conf_int(const string &key, int num, const int &val) {
  304. ostringstream sout;
  305. sout << key << "_" << num;
  306. if (config.count(sout.str()) == 0) {
  307. return conf_int(key, val);
  308. }
  309. return atoi(config[sout.str()].c_str());
  310. }
  311. int conf_int(const string &key) {
  312. return conf_int(key, 0);
  313. }
  314. // problem.conf is uploaded by whoever controls the problem, so input_pre /
  315. // input_suf / output_pre / output_suf are attacker-controlled. They get
  316. // concatenated into data_path / work_path by conf_input_file_name() and
  317. // conf_output_file_name(), so a value like "x/../../../../etc/" used to make
  318. // the judger read and write outside the problem's own directory. The language
  319. // side of that bug was remote code execution (the web root was writable); here
  320. // it is "only" arbitrary read/write inside the judge container, but the same
  321. // whitelist applies: a filename component is all that is ever legitimate.
  322. string conf_filename_part(const string &key, const string &val) {
  323. string s = conf_str(key, val);
  324. if (s.find("..") != string::npos) {
  325. return val;
  326. }
  327. for (size_t i = 0; i < s.size(); ++i) {
  328. char c = s[i];
  329. bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  330. (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-';
  331. if (!ok) {
  332. return val;
  333. }
  334. }
  335. return s;
  336. }
  337. string conf_input_file_name(int num) {
  338. ostringstream name;
  339. if (num < 0) {
  340. name << "ex_";
  341. }
  342. name << conf_filename_part("input_pre", "input") << abs(num) << "." << conf_filename_part("input_suf", "txt");
  343. return name.str();
  344. }
  345. string conf_output_file_name(int num) {
  346. ostringstream name;
  347. if (num < 0) {
  348. name << "ex_";
  349. }
  350. name << conf_filename_part("output_pre", "output") << abs(num) << "." << conf_filename_part("output_suf", "txt");
  351. return name.str();
  352. }
  353. RunLimit conf_run_limit(string pre, const int &num, const RunLimit &val) {
  354. if (!pre.empty()) {
  355. pre += "_";
  356. }
  357. RunLimit limit;
  358. limit.time = conf_int(pre + "time_limit", num, val.time);
  359. limit.memory = conf_int(pre + "memory_limit", num, val.memory);
  360. limit.output = conf_int(pre + "output_limit", num, val.output);
  361. return limit;
  362. }
  363. RunLimit conf_run_limit(const int &num, const RunLimit &val) {
  364. return conf_run_limit("", num, val);
  365. }
  366. void conf_add(const string &key, const string &val) {
  367. if (config.count(key)) return;
  368. config[key] = val;
  369. }
  370. bool conf_has(const string &key) {
  371. return config.count(key) != 0;
  372. }
  373. bool conf_is(const string &key, const string &val) {
  374. if (config.count(key) == 0) return false;
  375. return config[key] == val;
  376. }
  377. /*==================== config set End ================= */
  378. /*====================== info print =================== */
  379. template <class T>
  380. inline string vtos(const T &v) {
  381. ostringstream sout;
  382. sout << v;
  383. return sout.str();
  384. }
  385. inline string htmlspecialchars(const string &s) {
  386. string r;
  387. for (int i = 0; i < (int)s.length(); i++) {
  388. switch (s[i]) {
  389. case '&' : r += "&amp;"; break;
  390. case '<' : r += "&lt;"; break;
  391. case '>' : r += "&gt;"; break;
  392. case '"' : r += "&quot;"; break;
  393. case '\0': r += "<b>\\0</b>"; break;
  394. default : r += s[i]; break;
  395. }
  396. }
  397. return r;
  398. }
  399. inline string info_str(int id) {
  400. switch (id) {
  401. case RS_MLE: return "Memory Limit Exceeded";
  402. case RS_TLE: return "Time Limit Exceeded";
  403. case RS_OLE: return "Output Limit Exceeded";
  404. case RS_RE : return "Runtime Error";
  405. case RS_DGS: return "Dangerous Syscalls";
  406. case RS_JGF: return "Judgement Failed";
  407. default : return "Unknown Result";
  408. }
  409. }
  410. inline string info_str(const RunResult &p) {
  411. return info_str(p.type);
  412. }
  413. void add_point_info(const PointInfo &info, bool update_tot_score = true) {
  414. if (info.num >= 0) {
  415. if(info.ust >= 0) {
  416. tot_time += info.ust;
  417. }
  418. if(info.usm >= 0) {
  419. max_memory = max(max_memory, info.usm);
  420. }
  421. }
  422. if (update_tot_score) {
  423. tot_score += info.scr;
  424. }
  425. details_out << "<test num=\"" << info.num << "\""
  426. << " score=\"" << info.scr << "\""
  427. << " info=\"" << htmlspecialchars(info.info) << "\""
  428. << " time=\"" << info.ust << "\""
  429. << " memory=\"" << info.usm << "\">" << endl;
  430. if (conf_str("show_in", "on") == "on") {
  431. details_out << "<in>" << htmlspecialchars(info.in) << "</in>" << endl;
  432. }
  433. if (conf_str("show_out", "on") == "on") {
  434. details_out << "<out>" << htmlspecialchars(info.out) << "</out>" << endl;
  435. }
  436. if (conf_str("show_res", "on") == "on") {
  437. details_out << "<res>" << htmlspecialchars(info.res) << "</res>" << endl;
  438. }
  439. details_out << "</test>" << endl;
  440. }
  441. void add_custom_test_info(const CustomTestInfo &info) {
  442. if(info.ust >= 0) {
  443. tot_time += info.ust;
  444. }
  445. if(info.usm >= 0) {
  446. max_memory = max(max_memory, info.usm);
  447. }
  448. details_out << "<custom-test info=\"" << htmlspecialchars(info.info) << "\""
  449. << " time=\"" << info.ust << "\""
  450. << " memory=\"" << info.usm << "\">" << endl;
  451. if (!info.exp.empty()) {
  452. details_out << info.exp << endl;
  453. }
  454. details_out << "<out>" << htmlspecialchars(info.out) << "</out>" << endl;
  455. details_out << "</custom-test>" << endl;
  456. }
  457. void add_subtask_info(const int &num, const int &scr, const string &info, const vector<PointInfo> &points) {
  458. details_out << "<subtask num=\"" << num << "\""
  459. << " score=\"" << scr << "\""
  460. << " info=\"" << htmlspecialchars(info) << "\">" << endl;
  461. tot_score += scr;
  462. for (vector<PointInfo>::const_iterator it = points.begin(); it != points.end(); it++) {
  463. add_point_info(*it, false);
  464. }
  465. details_out << "</subtask>" << endl;
  466. }
  467. void end_judge_ok() {
  468. FILE *fres = fopen((result_path + "/result.txt").c_str(), "w");
  469. fprintf(fres, "score %d\n", tot_score);
  470. fprintf(fres, "time %d\n", tot_time);
  471. fprintf(fres, "memory %d\n", max_memory);
  472. fprintf(fres, "details\n");
  473. fprintf(fres, "<tests>\n");
  474. fprintf(fres, "%s", details_out.str().c_str());
  475. fprintf(fres, "</tests>\n");
  476. fclose(fres);
  477. exit(0);
  478. }
  479. void end_judge_judgement_failed(const string &info) {
  480. FILE *fres = fopen((result_path + "/result.txt").c_str(), "w");
  481. fprintf(fres, "error Judgement Failed\n");
  482. fprintf(fres, "details\n");
  483. fprintf(fres, "<error>%s</error>\n", htmlspecialchars(info).c_str());
  484. fclose(fres);
  485. exit(1);
  486. }
  487. void end_judge_compile_error(const RunCompilerResult &res) {
  488. FILE *fres = fopen((result_path + "/result.txt").c_str(), "w");
  489. fprintf(fres, "error Compile Error\n");
  490. fprintf(fres, "details\n");
  491. fprintf(fres, "<error>%s</error>\n", htmlspecialchars(res.info).c_str());
  492. fclose(fres);
  493. exit(0);
  494. }
  495. void report_judge_status(const char *status) {
  496. FILE *f = fopen((result_path + "/cur_status.txt").c_str(), "a");
  497. if (f == NULL) {
  498. return;
  499. }
  500. if (flock(fileno(f), LOCK_EX) != -1) {
  501. if (ftruncate(fileno(f), 0) != -1) {
  502. fprintf(f, "%s\n", status);
  503. fflush(f);
  504. }
  505. flock(fileno(f), LOCK_UN);
  506. }
  507. fclose(f);
  508. }
  509. bool report_judge_status_f(const char *fmt, ...) {
  510. const int MaxL = 512;
  511. char status[MaxL];
  512. va_list ap;
  513. va_start(ap, fmt);
  514. int res = vsnprintf(status, MaxL, fmt, ap);
  515. if (res < 0 || res >= MaxL) {
  516. return false;
  517. }
  518. report_judge_status(status);
  519. va_end(ap);
  520. return true;
  521. }
  522. /*==================== info print End ================= */
  523. /*========================== run ====================== */
  524. struct RunProgramConfig {
  525. vector<string> readable_file_names; // other than stdin
  526. string result_file_name;
  527. string input_file_name;
  528. string output_file_name;
  529. string error_file_name;
  530. string type;
  531. string work_path;
  532. RunLimit limit;
  533. vector<string> argv;
  534. RunProgramConfig() {
  535. int p = 1;
  536. while (conf_str("readable", p, "") != "") {
  537. readable_file_names.push_back(conf_str("readable", p, ""));
  538. p++;
  539. }
  540. result_file_name = result_path + "/run_program_result.txt";
  541. type = "default";
  542. work_path = ::work_path;
  543. for (vector<string>::iterator it = readable_file_names.begin(); it != readable_file_names.end(); it++) {
  544. if (!it->empty() && (*it)[0] != '/') {
  545. *it = ::work_path + "/" + *it;
  546. }
  547. }
  548. }
  549. void set_argv(const char *program_name, ...) {
  550. argv.clear();
  551. argv.push_back(program_name);
  552. va_list vl;
  553. va_start(vl, program_name);
  554. for (const char *arg = va_arg(vl, const char *); arg; arg = va_arg(vl, const char *)) {
  555. argv.push_back(arg);
  556. }
  557. va_end(vl);
  558. }
  559. void set_submission_program_name(string name) {
  560. string lang = conf_str(string(name) + "_language");
  561. type = "default";
  562. string program_name = name;
  563. if (lang == "Python2") {
  564. type = "python2";
  565. } else if (lang == "Python3") {
  566. type = "python3";
  567. } else if (lang == "Java8") {
  568. program_name += "." + conf_str(name + "_main_class");
  569. type = "java8";
  570. } else if (lang == "Java11") {
  571. program_name += "." + conf_str(name + "_main_class");
  572. type = "java11";
  573. }
  574. set_argv(program_name.c_str(), NULL);
  575. }
  576. string get_cmd() const {
  577. ostringstream sout;
  578. sout << main_path << "/run/run_program"
  579. << " " << "--res=" << escapeshellarg(result_file_name)
  580. << " " << "--in=" << escapeshellarg(input_file_name)
  581. << " " <<"--out=" << escapeshellarg(output_file_name)
  582. << " " << "--err=" << escapeshellarg(error_file_name)
  583. << " " << "--tl=" << limit.time
  584. << " " << "--ml=" << limit.memory
  585. << " " << "--ol=" << limit.output
  586. << " " << "--type=" << type
  587. << " " << "--work-path=" << work_path
  588. /*<< " " << "--show-trace-details"*/;
  589. for (vector<string>::const_iterator it = readable_file_names.begin(); it != readable_file_names.end(); it++) {
  590. sout << " " << "--add-readable=" << escapeshellarg(*it);
  591. }
  592. for (vector<string>::const_iterator it = argv.begin(); it != argv.end(); it++) {
  593. sout << " " << escapeshellarg(*it);
  594. }
  595. return sout.str();
  596. }
  597. };
  598. struct PipeConfig {
  599. int from, to;
  600. int from_fd, to_fd;
  601. string saving_file_name;
  602. PipeConfig() {
  603. }
  604. PipeConfig(int _from, int _from_fd, int _to, int _to_fd, const string &_saving_file_name = "")
  605. : from(_from), from_fd(_from_fd), to(_to), to_fd(_to_fd), saving_file_name(_saving_file_name) {
  606. }
  607. };
  608. struct RunInteractionConfig {
  609. vector<string> cmds;
  610. vector<PipeConfig> pipes;
  611. string get_cmd() const {
  612. ostringstream sout;
  613. sout << main_path << "/run/run_interaction";
  614. for (int i = 0; i < (int)cmds.size(); i++) {
  615. sout << " " << escapeshellarg(cmds[i]);
  616. }
  617. for (int i = 0; i < (int)pipes.size(); i++) {
  618. sout << " " << "-p";
  619. sout << " " << pipes[i].from << ":" << pipes[i].from_fd;
  620. sout << "-" << pipes[i].to << ":" << pipes[i].to_fd;
  621. if (!pipes[i].saving_file_name.empty()) {
  622. sout << " " << "-s";
  623. sout << " " << escapeshellarg(pipes[i].saving_file_name);
  624. }
  625. }
  626. return sout.str();
  627. }
  628. };
  629. // @deprecated
  630. // will be removed in the future
  631. RunResult vrun_program(
  632. const char *run_program_result_file_name,
  633. const char *input_file_name,
  634. const char *output_file_name,
  635. const char *error_file_name,
  636. const RunLimit &limit,
  637. const vector<string> &rest) {
  638. ostringstream sout;
  639. sout << main_path << "/run/run_program"
  640. << " " << "--res=" << escapeshellarg(run_program_result_file_name)
  641. << " " << "--in=" << escapeshellarg(input_file_name)
  642. << " " <<"--out=" << escapeshellarg(output_file_name)
  643. << " " << "--err=" << escapeshellarg(error_file_name)
  644. << " " << "--tl=" << limit.time
  645. << " " << "--ml=" << limit.memory
  646. << " " << "--ol=" << limit.output
  647. /*<< " " << "--show-trace-details"*/;
  648. for (vector<string>::const_iterator it = rest.begin(); it != rest.end(); it++) {
  649. sout << " " << escapeshellarg(*it);
  650. }
  651. if (execute(sout.str().c_str()) != 0) {
  652. return RunResult::failed_result();
  653. }
  654. return RunResult::from_file(run_program_result_file_name);
  655. }
  656. RunResult run_program(const RunProgramConfig &rpc) {
  657. if (execute(rpc.get_cmd().c_str()) != 0) {
  658. return RunResult::failed_result();
  659. }
  660. return RunResult::from_file(rpc.result_file_name);
  661. }
  662. // @return interaction return value
  663. int run_interaction(const RunInteractionConfig &ric) {
  664. return execute(ric.get_cmd().c_str());
  665. }
  666. RunResult run_program(
  667. const char *run_program_result_file_name,
  668. const char *input_file_name,
  669. const char *output_file_name,
  670. const char *error_file_name,
  671. const RunLimit &limit, ...) {
  672. vector<string> argv;
  673. va_list vl;
  674. va_start(vl, limit);
  675. for (const char *arg = va_arg(vl, const char *); arg; arg = va_arg(vl, const char *)) {
  676. argv.push_back(arg);
  677. }
  678. va_end(vl);
  679. return vrun_program(run_program_result_file_name,
  680. input_file_name,
  681. output_file_name,
  682. error_file_name,
  683. limit,
  684. argv);
  685. }
  686. RunValidatorResult run_validator(
  687. const string &input_file_name,
  688. const RunLimit &limit,
  689. const string &program_name) {
  690. RunResult ret = run_program(
  691. (string(result_path) + "/run_validator_result.txt").c_str(),
  692. input_file_name.c_str(),
  693. "/dev/null",
  694. (string(result_path) + "/validator_error.txt").c_str(),
  695. limit,
  696. program_name.c_str(),
  697. NULL);
  698. RunValidatorResult res;
  699. res.type = ret.type;
  700. res.ust = ret.ust;
  701. res.usm = ret.usm;
  702. if (ret.type != RS_AC || ret.exit_code != 0) {
  703. res.succeeded = false;
  704. res.info = file_preview(result_path + "/validator_error.txt");
  705. } else {
  706. res.succeeded = true;
  707. }
  708. return res;
  709. }
  710. RunCheckerResult run_checker(
  711. const RunLimit &limit,
  712. const string &program_name,
  713. const string &input_file_name,
  714. const string &output_file_name,
  715. const string &answer_file_name) {
  716. RunResult ret = run_program(
  717. (string(result_path) + "/run_checker_result.txt").c_str(),
  718. "/dev/null",
  719. "/dev/null",
  720. (string(result_path) + "/checker_error.txt").c_str(),
  721. limit,
  722. ("--add-readable=" + input_file_name).c_str(),
  723. ("--add-readable=" + output_file_name).c_str(),
  724. ("--add-readable=" + answer_file_name).c_str(),
  725. program_name.c_str(),
  726. realpath(input_file_name).c_str(),
  727. realpath(output_file_name).c_str(),
  728. realpath(answer_file_name).c_str(),
  729. NULL);
  730. return RunCheckerResult::from_file(result_path + "/checker_error.txt", ret);
  731. }
  732. RunCompilerResult run_compiler(const char *path, ...) {
  733. vector<string> argv;
  734. argv.push_back("--type=compiler");
  735. argv.push_back(string("--work-path=") + path);
  736. va_list vl;
  737. va_start(vl, path);
  738. for (const char *arg = va_arg(vl, const char *); arg; arg = va_arg(vl, const char *)) {
  739. argv.push_back(arg);
  740. }
  741. va_end(vl);
  742. RunResult ret = vrun_program(
  743. (result_path + "/run_compiler_result.txt").c_str(),
  744. "/dev/null",
  745. "stderr",
  746. (result_path + "/compiler_result.txt").c_str(),
  747. RL_COMPILER_DEFAULT,
  748. argv);
  749. RunCompilerResult res;
  750. res.type = ret.type;
  751. res.ust = ret.ust;
  752. res.usm = ret.usm;
  753. res.succeeded = ret.type == RS_AC && ret.exit_code == 0;
  754. if (!res.succeeded) {
  755. if (ret.type == RS_AC) {
  756. res.info = file_preview(result_path + "/compiler_result.txt", 500);
  757. } else if (ret.type == RS_JGF) {
  758. res.info = "No Comment";
  759. } else {
  760. res.info = "Compiler " + info_str(ret.type);
  761. }
  762. }
  763. return res;
  764. }
  765. RunResult run_submission_program(
  766. const string &input_file_name,
  767. const string &output_file_name,
  768. const RunLimit &limit,
  769. const string &name,
  770. RunProgramConfig rpc = RunProgramConfig()) {
  771. rpc.result_file_name = result_path + "/run_submission_program.txt";
  772. if (!getenv("ONLINE_JUDGE_UBSAN")) {
  773. rpc.output_file_name = output_file_name;
  774. rpc.error_file_name = "/dev/null";
  775. }
  776. else {
  777. rpc.output_file_name = "/dev/null";
  778. rpc.error_file_name = output_file_name;
  779. }
  780. rpc.input_file_name = input_file_name;
  781. rpc.limit = limit;
  782. rpc.set_submission_program_name(name);
  783. RunResult res = run_program(rpc);
  784. if (res.type == RS_AC && res.exit_code != 0) {
  785. res.type = RS_RE;
  786. }
  787. return res;
  788. }
  789. void prepare_interactor() {
  790. static bool prepared = false;
  791. if (prepared) {
  792. return;
  793. }
  794. string data_path_std = data_path + "/interactor";
  795. string work_path_std = work_path + "/interactor";
  796. executef("cp %s %s", data_path_std.c_str(), work_path_std.c_str());
  797. conf_add("interactor_language", "C++");
  798. prepared = true;
  799. }
  800. // simple: prog <---> interactor <---> data
  801. RunSimpleInteractionResult run_simple_interation(
  802. const string &input_file_name,
  803. const string &answer_file_name,
  804. const string &real_input_file_name,
  805. const string &real_output_file_name,
  806. const RunLimit &limit,
  807. const RunLimit &ilimit,
  808. const string &name,
  809. RunProgramConfig rpc = RunProgramConfig(),
  810. RunProgramConfig irpc = RunProgramConfig()) {
  811. prepare_interactor();
  812. rpc.result_file_name = result_path + "/run_submission_program.txt";
  813. rpc.input_file_name = "stdin";
  814. rpc.output_file_name = "stdout";
  815. rpc.error_file_name = "/dev/null";
  816. rpc.limit = limit;
  817. rpc.set_submission_program_name(name);
  818. irpc.result_file_name = result_path + "/run_interactor_program.txt";
  819. irpc.readable_file_names.push_back(input_file_name);
  820. irpc.readable_file_names.push_back(answer_file_name);
  821. irpc.input_file_name = "stdin";
  822. irpc.output_file_name = "stdout";
  823. irpc.error_file_name = result_path + "/interactor_error.txt";
  824. irpc.limit = ilimit;
  825. irpc.set_submission_program_name("interactor");
  826. irpc.argv.push_back(input_file_name);
  827. irpc.argv.push_back("/dev/stdin");
  828. irpc.argv.push_back(answer_file_name);
  829. irpc.limit.real_time = rpc.limit.real_time = rpc.limit.time + irpc.limit.time + 1;
  830. RunInteractionConfig ric;
  831. ric.cmds.push_back(rpc.get_cmd());
  832. ric.cmds.push_back(irpc.get_cmd());
  833. // from:fd, to:fd
  834. ric.pipes.push_back(PipeConfig(2, 1, 1, 0, real_input_file_name));
  835. ric.pipes.push_back(PipeConfig(1, 1, 2, 0, real_output_file_name));
  836. run_interaction(ric);
  837. RunSimpleInteractionResult rires;
  838. RunResult res = RunResult::from_file(rpc.result_file_name);
  839. RunCheckerResult ires = RunCheckerResult::from_file(irpc.error_file_name, RunResult::from_file(irpc.result_file_name));
  840. if (res.type == RS_AC && res.exit_code != 0) {
  841. res.type = RS_RE;
  842. }
  843. if (ires.type == RS_JGF) {
  844. ires.info = "Interactor Judgement Failed";
  845. }
  846. if (ires.type == RS_TLE) {
  847. ires.type = RS_AC;
  848. res.type = RS_TLE;
  849. }
  850. rires.res = res;
  851. rires.ires = ires;
  852. return rires;
  853. }
  854. void prepare_run_standard_program() {
  855. static bool prepared = false;
  856. if (prepared) {
  857. return;
  858. }
  859. string data_path_std = data_path + "/std";
  860. string work_path_std = work_path + "/std";
  861. executef("cp %s %s", data_path_std.c_str(), work_path_std.c_str());
  862. conf_add("std_language", "C++");
  863. prepared = true;
  864. }
  865. // @deprecated
  866. // will be removed in the future
  867. RunResult run_standard_program(
  868. const string &input_file_name,
  869. const string &output_file_name,
  870. const RunLimit &limit,
  871. RunProgramConfig rpc = RunProgramConfig()) {
  872. prepare_run_standard_program();
  873. rpc.result_file_name = result_path + "/run_standard_program.txt";
  874. return run_submission_program(
  875. input_file_name,
  876. output_file_name,
  877. limit,
  878. "std",
  879. rpc);
  880. }
  881. /*======================== run End ==================== */
  882. /*======================== compile ==================== */
  883. bool is_illegal_keyword(const string &name) {
  884. if (name == "__asm" || name == "__asm__" || name == "asm")
  885. return true;
  886. return false;
  887. }
  888. bool has_illegal_keywords_in_file(const string &name) {
  889. FILE *f = fopen(name.c_str(), "r");
  890. int c;
  891. string key;
  892. while ((c = fgetc(f)) != EOF)
  893. {
  894. if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_')
  895. {
  896. if (key.size() < 20)
  897. key += c;
  898. else
  899. {
  900. if (is_illegal_keyword(key))
  901. return true;
  902. key.erase(key.begin());
  903. key += c;
  904. }
  905. }
  906. else
  907. {
  908. if (is_illegal_keyword(key))
  909. return true;
  910. key.clear();
  911. }
  912. }
  913. if (is_illegal_keyword(key))
  914. return true;
  915. fclose(f);
  916. return false;
  917. }
  918. RunCompilerResult prepare_java_source(const string &name, const string &path = work_path) {
  919. FILE *f = fopen((path + "/" + name + ".code").c_str(), "r");
  920. const int L = 1024;
  921. std::string s;
  922. char buf[L + 1];
  923. int mode = 0;
  924. while (!feof(f)) {
  925. buf[fread(buf, 1, L, f)] = '\0';
  926. for (char *p = buf; *p; p++) {
  927. s += *p;
  928. switch (mode) {
  929. case 0:
  930. switch (*p) {
  931. case '/':
  932. mode = 1;
  933. break;
  934. case '\'':
  935. mode = 5;
  936. break;
  937. case '\"':
  938. mode = 6;
  939. break;
  940. }
  941. break;
  942. case 1:
  943. switch (*p) {
  944. case '/':
  945. mode = 2;
  946. s.resize(s.length() - 2);
  947. break;
  948. case '*':
  949. mode = 3;
  950. s.resize(s.length() - 2);
  951. break;
  952. }
  953. break;
  954. case 2:
  955. s.resize(s.length() - 1);
  956. switch (*p) {
  957. case '\n':
  958. s += '\n';
  959. mode = 0;
  960. break;
  961. }
  962. break;
  963. case 3:
  964. s.resize(s.length() - 1);
  965. switch (*p) {
  966. case '*':
  967. mode = 4;
  968. break;
  969. }
  970. break;
  971. case 4:
  972. s.resize(s.length() - 1);
  973. switch (*p) {
  974. case '/':
  975. s += ' ';
  976. mode = 0;
  977. break;
  978. }
  979. break;
  980. case 5:
  981. switch (*p) {
  982. case '\'':
  983. mode = 0;
  984. break;
  985. case '\\':
  986. mode = 7;
  987. break;
  988. }
  989. case 6:
  990. switch (*p) {
  991. case '\"':
  992. mode = 0;
  993. break;
  994. case '\\':
  995. mode = 8;
  996. break;
  997. }
  998. case 7:
  999. mode = 5;
  1000. break;
  1001. case 8:
  1002. mode = 6;
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. bool valid[256];
  1008. fill(valid, valid + 256, false);
  1009. for (int c = 'a'; c <= 'z'; c++)
  1010. valid[c] = true;
  1011. for (int c = 'A'; c <= 'Z'; c++)
  1012. valid[c] = true;
  1013. valid['.'] = true;
  1014. valid['_'] = true;
  1015. vector<string> tokens;
  1016. for (int p = 0, np = 0; p < (int)s.length(); p = np) {
  1017. while (np < (int)s.length() && valid[(unsigned char)s[np]]) {
  1018. np++;
  1019. }
  1020. if (np == p) {
  1021. np++;
  1022. } else {
  1023. tokens.push_back(s.substr(p, np - p));
  1024. }
  1025. }
  1026. if (tokens.size() > 0 && tokens[0] == "package") {
  1027. RunCompilerResult res;
  1028. res.type = RS_WA;
  1029. res.ust = -1;
  1030. res.usm = -1;
  1031. res.succeeded = false;
  1032. res.info = "Please don't specify the package.";
  1033. return res;
  1034. }
  1035. for (int i = 0; i + 1 < (int)tokens.size(); i++) {
  1036. if (tokens[i] == "class") {
  1037. if (tokens[i + 1].length() <= 100) {
  1038. config[name + "_main_class"] = tokens[i + 1];
  1039. RunCompilerResult res;
  1040. res.type = RS_AC;
  1041. res.ust = 0;
  1042. res.usm = 0;
  1043. res.succeeded = true;
  1044. return res;
  1045. } else {
  1046. RunCompilerResult res;
  1047. res.type = RS_WA;
  1048. res.ust = -1;
  1049. res.usm = -1;
  1050. res.succeeded = false;
  1051. res.info = "The name of the main class is too long.";
  1052. return res;
  1053. }
  1054. }
  1055. }
  1056. RunCompilerResult res;
  1057. res.type = RS_WA;
  1058. res.ust = -1;
  1059. res.usm = -1;
  1060. res.succeeded = false;
  1061. res.info = "Can't find the main class.";
  1062. return res;
  1063. }
  1064. RunCompilerResult compile_c(const string &name, const string &path = work_path) {
  1065. return run_compiler(path.c_str(),
  1066. "/usr/bin/gcc", "-o", name.c_str(), "-x", "c", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
  1067. }
  1068. RunCompilerResult compile_pas(const string &name, const string &path = work_path) {
  1069. return run_compiler(path.c_str(),
  1070. "/usr/bin/fpc", (name + ".code").c_str(), "-O2", NULL);
  1071. }
  1072. RunCompilerResult compile_cpp14(const string &name, const string &path = work_path) {
  1073. return run_compiler(path.c_str(),
  1074. "/usr/bin/g++", "-std=c++14", "-o", name.c_str(), "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
  1075. }
  1076. RunCompilerResult compile_cpp17(const string &name, const string &path = work_path) {
  1077. return run_compiler(path.c_str(),
  1078. "/usr/bin/g++", "-std=c++17", "-o", name.c_str(), "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
  1079. }
  1080. RunCompilerResult compile_cpp17ubsan(const string &name, const string &path = work_path) {
  1081. return run_compiler(path.c_str(),
  1082. "/usr/bin/g++", "-fsanitize=undefined", "-std=c++17", "-o", name.c_str(), "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
  1083. }
  1084. RunCompilerResult compile_python2(const string &name, const string &path = work_path) {
  1085. return run_compiler(path.c_str(),
  1086. "/usr/bin/python2", "-E", "-s", "-B", "-O", "-c",
  1087. ("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);
  1088. }
  1089. RunCompilerResult compile_python3(const string &name, const string &path = work_path) {
  1090. return run_compiler(path.c_str(),
  1091. "/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);
  1092. }
  1093. RunCompilerResult compile_java8(const string &name, const string &path = work_path) {
  1094. RunCompilerResult ret = prepare_java_source(name, path);
  1095. if (!ret.succeeded)
  1096. return ret;
  1097. string main_class = conf_str(name + "_main_class");
  1098. executef("rm %s/%s -rf 2>/dev/null; mkdir %s/%s", path.c_str(), name.c_str(), path.c_str(), name.c_str());
  1099. 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());
  1100. return run_compiler((path + "/" + name).c_str(),
  1101. "/usr/lib/jvm/java-8-openjdk-amd64/bin/javac", (main_class + ".java").c_str(), NULL);
  1102. }
  1103. RunCompilerResult compile_java11(const string &name, const string &path = work_path) {
  1104. RunCompilerResult ret = prepare_java_source(name, path);
  1105. if (!ret.succeeded)
  1106. return ret;
  1107. string main_class = conf_str(name + "_main_class");
  1108. executef("rm %s/%s -rf 2>/dev/null; mkdir %s/%s", path.c_str(), name.c_str(), path.c_str(), name.c_str());
  1109. 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());
  1110. return run_compiler((path + "/" + name).c_str(),
  1111. "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac", (main_class + ".java").c_str(), NULL);
  1112. }
  1113. // ryp
  1114. RunCompilerResult compile(const char *name) {
  1115. string lang = conf_str(string(name) + "_language");
  1116. if (lang == "C++17ubsan")
  1117. setenv ("ONLINE_JUDGE_UBSAN", "1", 1);
  1118. if ((lang == "C++14" || lang == "C++17" || lang == "C++17ubsan" || lang == "C") && has_illegal_keywords_in_file(work_path + "/" + name + ".code"))
  1119. {
  1120. RunCompilerResult res;
  1121. res.type = RS_DGS;
  1122. res.ust = -1;
  1123. res.usm = -1;
  1124. res.succeeded = false;
  1125. res.info = "Compile Failed";
  1126. return res;
  1127. }
  1128. if (lang == "C++14") {
  1129. return compile_cpp14(name);
  1130. }
  1131. if (lang == "C++17") {
  1132. return compile_cpp17(name);
  1133. }
  1134. if (lang == "C++17ubsan") {
  1135. return compile_cpp17ubsan(name);
  1136. }
  1137. if (lang == "Python2") {
  1138. return compile_python2(name);
  1139. }
  1140. if (lang == "Python3") {
  1141. return compile_python3(name);
  1142. }
  1143. if (lang == "Java8") {
  1144. return compile_java8(name);
  1145. }
  1146. if (lang == "Java11") {
  1147. return compile_java11(name);
  1148. }
  1149. if (lang == "C") {
  1150. return compile_c(name);
  1151. }
  1152. if (lang == "Pascal") {
  1153. return compile_pas(name);
  1154. }
  1155. RunCompilerResult res = RunCompilerResult::failed_result();
  1156. res.info = "This language is not supported yet.";
  1157. return res;
  1158. }
  1159. RunCompilerResult compile_c_with_implementer(const string &name, const string &path = work_path) {
  1160. return run_compiler(path.c_str(),
  1161. "/usr/bin/gcc", "-o", name.c_str(), "implementer.c", "-x", "c", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
  1162. }
  1163. RunCompilerResult compile_pas_with_implementer(const string &name, const string &path = work_path) {
  1164. executef("cp %s %s", (path + "/" + name + ".code").c_str(), (path + "/" + conf_str(name + "_unit_name") + ".pas").c_str());
  1165. return run_compiler(path.c_str(),
  1166. "/usr/bin/fpc", "implementer.pas", ("-o" + name).c_str(), "-O2", NULL);
  1167. }
  1168. RunCompilerResult compile_cpp14_with_implementer(const string &name, const string &path = work_path) {
  1169. return run_compiler(path.c_str(),
  1170. "/usr/bin/g++", "-std=c++14", "-o", name.c_str(), "implementer.cpp", "-x", "c++", (name + ".code").c_str(), "-lm", "-O2", "-DONLINE_JUDGE", NULL);
  1171. }
  1172. RunCompilerResult compile_cpp17_with_implementer(const string &name, const string &path = work_path) {
  1173. return run_compiler(path.c_str(),
  1174. "/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);
  1175. }
  1176. /*
  1177. RunCompilerResult compile_python2(const string &name, const string &path = work_path) {
  1178. return run_compiler(path.c_str(),
  1179. "/usr/bin/python2", "-E", "-s", "-B", "-O", "-c",
  1180. ("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);
  1181. }
  1182. RunCompilerResult compile_python3(const string &name, const string &path = work_path) {
  1183. return run_compiler(path.c_str(),
  1184. "/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);
  1185. }
  1186. */
  1187. RunCompilerResult compile_with_implementer(const char *name) {
  1188. string lang = conf_str(string(name) + "_language");
  1189. if (has_illegal_keywords_in_file(work_path + "/" + name + ".code"))
  1190. {
  1191. RunCompilerResult res;
  1192. res.type = RS_DGS;
  1193. res.ust = -1;
  1194. res.usm = -1;
  1195. res.succeeded = false;
  1196. res.info = "Compile Failed";
  1197. return res;
  1198. }
  1199. if (lang == "C++14") {
  1200. return compile_cpp14_with_implementer(name);
  1201. }
  1202. if (lang == "C++17") {
  1203. return compile_cpp17_with_implementer(name);
  1204. }
  1205. if (lang == "C") {
  1206. return compile_c_with_implementer(name);
  1207. }
  1208. if (lang == "Pascal") {
  1209. return compile_pas_with_implementer(name);
  1210. }
  1211. RunCompilerResult res = RunCompilerResult::failed_result();
  1212. res.info = "This language is not supported yet.";
  1213. return res;
  1214. }
  1215. /*====================== compile End ================== */
  1216. /*====================== test ================== */
  1217. struct TestPointConfig {
  1218. int submit_answer;
  1219. int validate_input_before_test;
  1220. string input_file_name;
  1221. string output_file_name;
  1222. string answer_file_name;
  1223. TestPointConfig()
  1224. : submit_answer(-1), validate_input_before_test(-1) {
  1225. }
  1226. void auto_complete(int num) {
  1227. if (submit_answer == -1) {
  1228. submit_answer = conf_is("submit_answer", "on");
  1229. }
  1230. if (validate_input_before_test == -1) {
  1231. validate_input_before_test = conf_is("validate_input_before_test", "on");
  1232. }
  1233. if (input_file_name.empty()) {
  1234. input_file_name = data_path + "/" + conf_input_file_name(num);
  1235. }
  1236. if (output_file_name.empty()) {
  1237. output_file_name = work_path + "/" + conf_output_file_name(num);
  1238. }
  1239. if (answer_file_name.empty()) {
  1240. answer_file_name = data_path + "/" + conf_output_file_name(num);
  1241. }
  1242. }
  1243. };
  1244. PointInfo test_point(const string &name, const int &num, TestPointConfig tpc = TestPointConfig()) {
  1245. tpc.auto_complete(num);
  1246. if (tpc.validate_input_before_test) {
  1247. RunValidatorResult val_ret = run_validator(
  1248. tpc.input_file_name,
  1249. conf_run_limit("validator", 0, RL_VALIDATOR_DEFAULT),
  1250. conf_str("validator"));
  1251. if (val_ret.type != RS_AC) {
  1252. return PointInfo(num, 0, -1, -1,
  1253. "Validator " + info_str(val_ret.type),
  1254. file_preview(tpc.input_file_name), "",
  1255. "");
  1256. } else if (!val_ret.succeeded) {
  1257. return PointInfo(num, 0, -1, -1,
  1258. "Invalid Input",
  1259. file_preview(tpc.input_file_name), "",
  1260. val_ret.info);
  1261. }
  1262. }
  1263. if (!conf_is("interaction_mode", "on")) {
  1264. RunResult pro_ret;
  1265. if (!tpc.submit_answer) {
  1266. pro_ret = run_submission_program(
  1267. tpc.input_file_name.c_str(),
  1268. tpc.output_file_name.c_str(),
  1269. conf_run_limit(num, RL_DEFAULT),
  1270. name);
  1271. if (conf_has("token")) {
  1272. file_hide_token(tpc.output_file_name, conf_str("token", ""));
  1273. }
  1274. if (pro_ret.type != RS_AC) {
  1275. return PointInfo(num, 0, -1, -1,
  1276. info_str(pro_ret.type),
  1277. file_preview(tpc.input_file_name), file_preview(tpc.output_file_name),
  1278. "");
  1279. }
  1280. } else {
  1281. pro_ret.type = RS_AC;
  1282. pro_ret.ust = -1;
  1283. pro_ret.usm = -1;
  1284. pro_ret.exit_code = 0;
  1285. }
  1286. RunCheckerResult chk_ret = run_checker(
  1287. conf_run_limit("checker", num, RL_CHECKER_DEFAULT),
  1288. conf_str("checker"),
  1289. tpc.input_file_name,
  1290. tpc.output_file_name,
  1291. tpc.answer_file_name);
  1292. if (chk_ret.type != RS_AC) {
  1293. return PointInfo(num, 0, -1, -1,
  1294. "Checker " + info_str(chk_ret.type),
  1295. file_preview(tpc.input_file_name), file_preview(tpc.output_file_name),
  1296. "");
  1297. }
  1298. return PointInfo(num, chk_ret.scr, pro_ret.ust, pro_ret.usm,
  1299. "default",
  1300. file_preview(tpc.input_file_name), file_preview(tpc.output_file_name),
  1301. chk_ret.info);
  1302. } else {
  1303. string real_output_file_name = tpc.output_file_name + ".real_input.txt";
  1304. string real_input_file_name = tpc.output_file_name + ".real_output.txt";
  1305. RunSimpleInteractionResult rires = run_simple_interation(
  1306. tpc.input_file_name,
  1307. tpc.answer_file_name,
  1308. real_input_file_name,
  1309. real_output_file_name,
  1310. conf_run_limit(num, RL_DEFAULT),
  1311. conf_run_limit("interactor", num, RL_INTERACTOR_DEFAULT),
  1312. name);
  1313. if (rires.ires.type != RS_AC) {
  1314. return PointInfo(num, 0, -1, -1,
  1315. "Interactor " + info_str(rires.ires.type),
  1316. file_preview(real_input_file_name), file_preview(real_output_file_name),
  1317. "");
  1318. }
  1319. if (rires.res.type != RS_AC) {
  1320. return PointInfo(num, 0, -1, -1,
  1321. info_str(rires.res.type),
  1322. file_preview(real_input_file_name), file_preview(real_output_file_name),
  1323. "");
  1324. }
  1325. return PointInfo(num, rires.ires.scr, rires.res.ust, rires.res.usm,
  1326. "default",
  1327. file_preview(real_input_file_name), file_preview(real_output_file_name),
  1328. rires.ires.info);
  1329. }
  1330. }
  1331. PointInfo test_hack_point(const string &name, TestPointConfig tpc) {
  1332. tpc.submit_answer = false;
  1333. tpc.validate_input_before_test = false;
  1334. tpc.auto_complete(0);
  1335. RunValidatorResult val_ret = run_validator(
  1336. tpc.input_file_name,
  1337. conf_run_limit("validator", 0, RL_VALIDATOR_DEFAULT),
  1338. conf_str("validator"));
  1339. if (val_ret.type != RS_AC) {
  1340. return PointInfo(0, 0, -1, -1,
  1341. "Validator " + info_str(val_ret.type),
  1342. file_preview(tpc.input_file_name), "",
  1343. "");
  1344. } else if (!val_ret.succeeded) {
  1345. return PointInfo(0, 0, -1, -1,
  1346. "Invalid Input",
  1347. file_preview(tpc.input_file_name), "",
  1348. val_ret.info);
  1349. }
  1350. RunLimit default_std_run_limit = conf_run_limit(0, RL_DEFAULT);
  1351. prepare_run_standard_program();
  1352. if (!conf_is("interaction_mode", "on")) {
  1353. RunProgramConfig rpc;
  1354. rpc.result_file_name = result_path + "/run_standard_program.txt";
  1355. RunResult std_ret = run_submission_program(
  1356. tpc.input_file_name,
  1357. tpc.answer_file_name,
  1358. conf_run_limit("standard", 0, default_std_run_limit),
  1359. "std",
  1360. rpc);
  1361. if (std_ret.type != RS_AC) {
  1362. return PointInfo(0, 0, -1, -1,
  1363. "Standard Program " + info_str(std_ret.type),
  1364. file_preview(tpc.input_file_name), "",
  1365. "");
  1366. }
  1367. if (conf_has("token")) {
  1368. file_hide_token(tpc.answer_file_name, conf_str("token", ""));
  1369. }
  1370. } else {
  1371. RunProgramConfig rpc;
  1372. rpc.result_file_name = result_path + "/run_standard_program.txt";
  1373. string real_output_file_name = tpc.answer_file_name;
  1374. string real_input_file_name = tpc.output_file_name + ".real_output.txt";
  1375. RunSimpleInteractionResult rires = run_simple_interation(
  1376. tpc.input_file_name,
  1377. tpc.answer_file_name,
  1378. real_input_file_name,
  1379. real_output_file_name,
  1380. conf_run_limit("standard", 0, default_std_run_limit),
  1381. conf_run_limit("interactor", 0, RL_INTERACTOR_DEFAULT),
  1382. "std",
  1383. rpc);
  1384. if (rires.ires.type != RS_AC) {
  1385. return PointInfo(0, 0, -1, -1,
  1386. "Interactor " + info_str(rires.ires.type) + " (Standard Program)",
  1387. file_preview(real_input_file_name), "",
  1388. "");
  1389. }
  1390. if (rires.res.type != RS_AC) {
  1391. return PointInfo(0, 0, -1, -1,
  1392. "Standard Program " + info_str(rires.res.type),
  1393. file_preview(real_input_file_name), "",
  1394. "");
  1395. }
  1396. }
  1397. PointInfo po = test_point(name, 0, tpc);
  1398. po.scr = po.scr != 100;
  1399. return po;
  1400. }
  1401. CustomTestInfo ordinary_custom_test(const string &name) {
  1402. RunLimit lim = conf_run_limit(0, RL_DEFAULT);
  1403. lim.time += 2;
  1404. string input_file_name = work_path + "/input.txt";
  1405. string output_file_name = work_path + "/output.txt";
  1406. RunResult pro_ret = run_submission_program(
  1407. input_file_name,
  1408. output_file_name,
  1409. lim,
  1410. name);
  1411. if (conf_has("token")) {
  1412. file_hide_token(output_file_name, conf_str("token", ""));
  1413. }
  1414. string info;
  1415. if (pro_ret.type == RS_AC) {
  1416. info = "Success";
  1417. } else {
  1418. info = info_str(pro_ret.type);
  1419. }
  1420. string exp;
  1421. if (pro_ret.type == RS_TLE) {
  1422. exp = "<p>[<strong>time limit:</strong> " + vtos(lim.time) + "s]</p>";
  1423. }
  1424. return CustomTestInfo(pro_ret.ust, pro_ret.usm,
  1425. info, exp, file_preview(output_file_name, 2048));
  1426. }
  1427. int scale_score(int scr100, int full) {
  1428. return scr100 * full / 100;
  1429. }
  1430. /*====================== test End ================== */
  1431. /*======================= conf init =================== */
  1432. void main_judger_init(int argc, char **argv) {
  1433. main_path = UOJ_WORK_PATH;
  1434. work_path = main_path + "/work";
  1435. result_path = string(UOJ_RESULT_PATH);
  1436. load_config(work_path + "/submission.conf");
  1437. problem_id = conf_int("problem_id");
  1438. data_path = string(UOJ_DATA_PATH) + "/" + conf_str("problem_id");
  1439. load_config(data_path + "/problem.conf");
  1440. executef("cp %s/require/* %s 2>/dev/null", data_path.c_str(), work_path.c_str());
  1441. if (conf_is("use_builtin_judger", "on")) {
  1442. config["judger"] = string(UOJ_WORK_PATH) + "/builtin/judger/judger";
  1443. } else {
  1444. config["judger"] = data_path + "/judger";
  1445. }
  1446. }
  1447. void judger_init(int argc, char **argv) {
  1448. if (argc != 5) {
  1449. exit(1);
  1450. }
  1451. main_path = argv[1];
  1452. work_path = argv[2];
  1453. result_path = argv[3];
  1454. data_path = argv[4];
  1455. load_config(work_path + "/submission.conf");
  1456. problem_id = conf_int("problem_id");
  1457. load_config(data_path + "/problem.conf");
  1458. if (config.count("use_builtin_checker")) {
  1459. config["checker"] = main_path + "/builtin/checker/" + config["use_builtin_checker"];
  1460. } else {
  1461. config["checker"] = data_path + "/chk";
  1462. }
  1463. config["validator"] = data_path + "/val";
  1464. }
  1465. /*===================== conf init End ================= */