private bool Compile(Task task, Result res, string JudgeTempPath) { if (CodeChecker.CheckUnsafeCode(task) == false) { res.ResultCode = ResultCode.CompileError; res.Detail = "Include unsafe code.If you have any question, please contact the administrator."; return(false); } Process p = new Process(); p.StartInfo.FileName = string.Format("\"{0}\"", wrap_judge_path(_profile.CompilerPath, JudgeTempPath)); p.StartInfo.Arguments = wrap_judge_path(_profile.CompileParameters, JudgeTempPath); p.StartInfo.WorkingDirectory = wrap_judge_path(_profile.CompilerWorkingDirectory, JudgeTempPath); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string error_output = null; bool error_output_readed = false; string standard_output = null; bool standard_output_readed = false; ThreadPool.QueueUserWorkItem((object context) => { while (true) { try { error_output = EncodingUtils.DetectAndReadToEndAndDispose(p.StandardError.BaseStream); error_output_readed = true; break; } catch { Thread.Sleep(100); } } }); ThreadPool.QueueUserWorkItem((object context) => { while (true) { try { standard_output = EncodingUtils.DetectAndReadToEndAndDispose(p.StandardOutput.BaseStream); standard_output_readed = true; break; } catch { Thread.Sleep(100); } } }); p.Start(); using (var usage = AffinityManager.GetUsage(p.ProcessorAffinity)) { try { p.ProcessorAffinity = usage.Affinity; } catch { } if (!p.WaitForExit(_profile.CompilerWaitTime)) { res.ResultCode = ResultCode.UnJudgable; res.Detail = "Compile time too long."; return(false); } else if (p.ExitCode != 0) { while (!error_output_readed || !standard_output_readed) { Thread.Sleep(100); } res.ResultCode = ResultCode.CompileError; res.Detail = grep_directory(error_output, JudgeTempPath); return(false); } return(true); } }
protected string ReadString(HttpWebResponse response) { return(EncodingUtils.DetectAndReadToEndAndDispose(response.GetResponseStream(), Encoding.UTF8)); }
private bool Run(Task task, TestData data, Result res, string JudgeTempPath, char[] Buffer) { Process run = new Process(); run.StartInfo.FileName = wrap_judge_path(_profile.RunnerFileName, JudgeTempPath); run.StartInfo.Arguments = wrap_judge_path(_profile.RunnerParameters, JudgeTempPath); run.StartInfo.WorkingDirectory = wrap_judge_path(_profile.RunnerWorkingDirectory, JudgeTempPath); run.StartInfo.UseShellExecute = false; run.StartInfo.RedirectStandardInput = true; run.StartInfo.RedirectStandardOutput = true; run.StartInfo.RedirectStandardError = true; run.StartInfo.CreateNoWindow = true; //Monitor to measure memory and time usage and kill when memory or time limit exceed. int MemoryCost = 0, TimeCost = 0; var monitor = new System.Timers.Timer(MonitorInterval); monitor.Elapsed += (object sender, System.Timers.ElapsedEventArgs args) => { try { if (run.HasExited) { monitor.Stop(); monitor.Dispose(); } else { try { var c_memory = (int)(run.PeakPagedMemorySize64 / 1024); if (c_memory > MemoryCost) { MemoryCost = c_memory; if (MemoryCost > task.MemoryLimit) { res.ResultCode = ResultCode.MemoryLimitExceeded; res.MemoryCost = MemoryCost; try { run.Kill(); } catch { } } } TimeCost = (int)((DateTime.Now - run.StartTime).TotalMilliseconds / _profile.TimeLimitScale); //TimeCost = (int)(run.TotalProcessorTime.TotalMilliseconds / _profile.TimeLimitScale); if (TimeCost > task.TimeLimit) { res.ResultCode = ResultCode.TimeLimitExceeded; res.TimeCost = task.TimeLimit; try { run.Kill(); } catch { } } } catch { } } } catch (InvalidOperationException) { } }; string error_output = null; bool error_output_readed = false; string standard_output = null; bool standard_output_readed = false; //int error_pro_time = 0; //int output_pro_time = 0; ThreadPool.QueueUserWorkItem((object context) => { while (true) { try { //var start_time = run.TotalProcessorTime.TotalMilliseconds; //Stopwatch sw = Stopwatch.StartNew(); error_output = EncodingUtils.DetectAndReadToEndAndDispose(run.StandardError.BaseStream); //sw.Stop(); //error_pro_time = (int)(sw.ElapsedMilliseconds - run.TotalProcessorTime.TotalMilliseconds + start_time); error_output_readed = true; break; } catch { Thread.Sleep(100); } } }); ThreadPool.QueueUserWorkItem((object context) => { while (true) { try { //var start_time = run.TotalProcessorTime.TotalMilliseconds; //Stopwatch sw = Stopwatch.StartNew(); standard_output = EncodingUtils.DetectAndReadToEndAndDispose(run.StandardOutput.BaseStream); //sw.Stop(); //output_pro_time = (int)(sw.ElapsedMilliseconds - run.TotalProcessorTime.TotalMilliseconds + start_time); standard_output_readed = true; break; } catch { Thread.Sleep(100); } } }); run.Start(); monitor.Start(); using (var usage = AffinityManager.GetUsage(run.ProcessorAffinity)) { try { try { run.ProcessorAffinity = usage.Affinity; run.PriorityClass = ProcessPriorityClass.RealTime; MemoryCost = (int)(run.PeakPagedMemorySize64 / 1024); } catch { } run.StandardInput.WriteLine(data.Input); run.StandardInput.Close(); /*run.StandardInput.AutoFlush = true; * int part_len = BufferSize, start_index, end_index; * for (start_index = 0, end_index = part_len; end_index < data.Input.Length; start_index += part_len, end_index += part_len) * run.StandardInput.Write(data.Input.Substring(start_index, part_len)); * run.StandardInput.WriteLine(data.Input.Substring(start_index, data.Input.Length - start_index));*/ } catch (Exception ex) { if (res.ResultCode != ResultCode.MemoryLimitExceeded && res.ResultCode != ResultCode.TimeLimitExceeded) { throw ex; } } if (!run.WaitForExit((int)(task.TimeLimit * _profile.TimeLimitScale))) { if (res.ResultCode == ResultCode.MemoryLimitExceeded || res.ResultCode == ResultCode.TimeLimitExceeded) { return(false); } try { if (run.ExitCode != 0) { res.ResultCode = ResultCode.RuntimeError; res.Detail = grep_directory( EncodingUtils.DetectAndReadToEndAndDispose(run.StandardError.BaseStream), JudgeTempPath ); return(false); } } catch { } try { run.Kill(); } catch { } res.ResultCode = ResultCode.TimeLimitExceeded; res.TimeCost = task.TimeLimit; return(false); } if (res.ResultCode == ResultCode.MemoryLimitExceeded || res.ResultCode == ResultCode.TimeLimitExceeded) { return(false); } try { MemoryCost = (int)(run.PeakPagedMemorySize64 / 1024); } catch { } res.MemoryCost = MemoryCost; if (res.MemoryCost > task.MemoryLimit) { res.ResultCode = ResultCode.MemoryLimitExceeded; return(false); } try { TimeCost = (int)((run.ExitTime - run.StartTime).TotalMilliseconds / _profile.TimeLimitScale); //TimeCost = (int)(run.TotalProcessorTime.TotalMilliseconds / _profile.TimeLimitScale); } catch { } if (TimeCost > res.TimeCost) { res.TimeCost = TimeCost; if (TimeCost > task.TimeLimit) { res.ResultCode = ResultCode.TimeLimitExceeded; res.TimeCost = task.TimeLimit; return(false); } } while (!error_output_readed || !standard_output_readed) { Thread.Sleep(100); } if (run.ExitCode != 0) { res.ResultCode = ResultCode.RuntimeError; res.Detail = grep_directory(error_output, JudgeTempPath); return(false); } if (standard_output.Length > _profile.OutputLimit) { res.ResultCode = ResultCode.OutputLimitExcceeded; return(false); } var output = standard_output.Replace("\r\n", "\n").Replace("\r", "\n").TrimEnd('\n'); var e_output = data.Output.Replace("\r\n", "\n").Replace("\r", "\n").TrimEnd('\n'); if (output == e_output) { return(true); } else { var output_l = output.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); var e_output_l = e_output.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); if (output_l.Length == e_output_l.Length) { int i; for (i = 0; i < output_l.Length; ++i) { if (output_l[i].TrimEnd() != e_output_l[i].TrimEnd()) { break; } } if (i == output_l.Length) { res.ResultCode = ResultCode.PresentationError; } else { res.ResultCode = ResultCode.WrongAnswer; } } else { res.ResultCode = ResultCode.WrongAnswer; } return(false); } } }
public bool FetchData(string ProblemId) { try { var problem = new Problem(); problem.Id = ProblemId; var res = Request(_profile.DataFetchURL, Encoding.UTF8.GetBytes("pid=" + ProblemId)); if (res.ContentType != "application/zip") { #if DEBUG #else ExceptionManager.Throw(new FetcherException(string.Format( "SDNUFetcher fetch data MIME validate failed, content:\r\n{0}\r\n", ReadString(res)), null)); #endif return(false); } Dictionary <string, TestData> data_dic = new Dictionary <string, TestData>(); MemoryStream ms = new MemoryStream(); using (var s = res.GetResponseStream()) { byte[] buffer = new byte[2048000]; int len; while ((len = s.Read(buffer, 0, 2048000)) > 0) { ms.Write(buffer, 0, len); } } using (var file = new ZipFile(ms)) { foreach (ZipEntry entry in file) { entry.IsUnicodeText = true; var tags = entry.Name.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); string content = EncodingUtils.DetectAndReadToEndAndDispose(file.GetInputStream(entry)); switch (tags[0]) { case "last_modified": problem.Version = content; break; case "input": case "output": if (tags.Length > 1) { var dk = Path.GetFileNameWithoutExtension(tags[1]); if (!data_dic.ContainsKey(dk)) { data_dic.Add(dk, new TestData() { Name = dk }); } if (tags[0] == "input") { data_dic[dk].Input = content; } else { data_dic[dk].Output = content; } } break; } } DataAccessor.Update(problem, (from kvp in data_dic orderby kvp.Key ascending select kvp.Value).GetEnumerator()); } return(true); } catch (Exception ex) { ExceptionManager.Throw(new FetcherException(string.Format( "SDNUFetcher fetch data failed, problem id: {0}.", ProblemId), ex)); return(false); } }