Exemplo n.º 1
0
 static void ShowData(OutputData odata)
 {
     if (!string.IsNullOrEmpty(odata.System_Error))
     {
         Console.WriteLine("System error:");
         Console.WriteLine(odata.System_Error);
     }
     else
     {
         Console.WriteLine("Errors:");
         Console.WriteLine(odata.Errors);
         Console.WriteLine("Warnings:");
         Console.WriteLine(odata.Warnings);
         Console.WriteLine("Output:");
         Console.WriteLine(odata.Output);
         Console.WriteLine("Exit status:");
         Console.WriteLine(odata.Exit_Status);
         Console.WriteLine("Stats:");
         Console.WriteLine(odata.Stats);
     }
     Console.ReadLine();
 }
Exemplo n.º 2
0
        static void TestEngineThroughService(string Program, string Input, Languages Lang, string Args)
        {
            OutputData odata;
            bool bytes = true;

            using (var s = new localhost.Service())
            {

                Stopwatch watch = new Stopwatch();
                watch.Start();
                var res = s.DoWork(Program, Input, (localhost.Languages)Lang, GlobalUtils.TopSecret.Service_user, GlobalUtils.TopSecret.Service_pass, Args, bytes, false, false);

                watch.Stop();
                if (res != null)
                {
                    if (string.IsNullOrEmpty(res.Stats))
                        res.Stats = "";
                    else
                        res.Stats += ", ";
                    res.Stats += string.Format("absolute service time: {0} sec", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }

                odata = new OutputData()
                {
                    Errors = res.Errors,
                    Warnings = res.Warnings,
                    Stats = res.Stats,
                    Output = res.Output,
                    Exit_Status = res.Exit_Status,
                    System_Error = res.System_Error
                };
                if (bytes)
                {
                    if (res.Errors_Bytes != null)
                        odata.Errors = System.Text.Encoding.Unicode.GetString(res.Errors_Bytes);
                    if (res.Warnings_Bytes != null)
                        odata.Warnings = System.Text.Encoding.Unicode.GetString(res.Warnings_Bytes);
                    if (res.Output_Bytes != null)
                        odata.Output = System.Text.Encoding.Unicode.GetString(res.Output_Bytes);
                }
            }
            ShowData(odata);
        }
Exemplo n.º 3
0
        OutputData RunSql(InputData idata)
        {
            OutputData odata = new OutputData();
            string     path  = BasePath + @"usercode\" + Utils.RandomString() + ".sql";

            using (TextWriter tw = new StreamWriter(path))
            {
                tw.Write(idata.Program);
            }

            using (Process process = new Process())
            {
                try
                {
                    double TotalMemoryInBytes = 0;
                    double TotalThreadCount   = 0;
                    int    samplesCount       = 0;

                    process.StartInfo.FileName               = BasePath + @"executables\SqlSandbox.exe";
                    process.StartInfo.Arguments              = path.Replace(" ", "|_|");
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;

                    DateTime start = DateTime.Now;
                    process.Start();

                    OutputReader output       = new OutputReader(process.StandardOutput);
                    Thread       outputReader = new Thread(new ThreadStart(output.ReadOutput));
                    outputReader.Start();
                    OutputReader error       = new OutputReader(process.StandardError);
                    Thread       errorReader = new Thread(new ThreadStart(error.ReadOutput));
                    errorReader.Start();


                    do
                    {
                        // Refresh the current process property values.
                        process.Refresh();
                        if (!process.HasExited)
                        {
                            try
                            {
                                var proc = process.TotalProcessorTime;
                                // Update the values for the overall peak memory statistics.
                                var mem1 = process.PagedMemorySize64;
                                var mem2 = process.PrivateMemorySize64;

                                //update stats
                                TotalMemoryInBytes += (mem1 + mem2);
                                TotalThreadCount   += (process.Threads.Count);
                                samplesCount++;

                                if (proc.TotalSeconds > 5 || mem1 + mem2 > 100000000 || process.Threads.Count > 100 || start + TimeSpan.FromSeconds(15) < DateTime.Now)
                                {
                                    var time = proc.TotalSeconds;
                                    var mem  = mem1 + mem2;
                                    process.Kill();
                                    var res = string.Format("Process killed because it exceeded given resources.\nCpu time used {0} sec, absolute running time {1} sec, memory used {2} Mb, nr of threads {3}", time, (int)(DateTime.Now - start).TotalSeconds, (int)(mem / 1048576), process.Threads.Count);
                                    odata.Errors = odata.Errors + "\n" + res;
                                    string partialResult = output.Builder.ToString();
                                    odata.Output = partialResult;
                                    //odata.Stats = string.Format("Absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                                    //Utils.Log.LogCodeToDB(data.Program, data.Input, data.CompilerArgs, res, (int)data.LanguageChoice, data.IsApi);
                                    return(odata);
                                }
                            }
                            catch (InvalidOperationException)
                            {
                                break;
                            }
                        }
                    }while (!process.WaitForExit(10));
                    process.WaitForExit();

                    errorReader.Join(5000);
                    outputReader.Join(5000);

                    if (!string.IsNullOrEmpty(error.Output))
                    {
                        odata.Output  = output.Builder.ToString();
                        odata.Errors += "\n" + error.Output;
                        odata.Stats   = string.Format("Absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                        //Utils.Log.LogCodeToDB(data.Program, data.Input, data.CompilerArgs, error.Output, (int)data.LanguageChoice, data.IsApi);
                        return(odata);
                    }

                    if (File.Exists(path + ".stats"))
                    {
                        using (TextReader tr = new StreamReader(path + ".stats"))
                        {
                            odata.Stats = tr.ReadLine();
                            if (!string.IsNullOrEmpty(odata.Stats))
                            {
                                odata.Stats += ", ";
                            }
                            else
                            {
                                odata.Stats = "";
                            }
                            odata.Stats += string.Format("absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                        }
                    }
                    //else
                    //{
                    //    odata.Stats = string.Format("Absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                    //}

                    odata.Output = output.Output;
                    // Utils.Log.LogCodeToDB(data.Program, data.Input, data.CompilerArgs, "OK", (int)data.LanguageChoice, data.IsApi);
                    return(odata);
                }
                catch (Exception e)
                {
                    if (!process.HasExited)
                    {
                        //reExp.Utils.Log.LogInfo("Process left running " + e.Message, "RunSqlServer");
                    }
                    throw;
                }
                finally
                {
                    try
                    {
                        File.Delete(path);
                    }
                    catch (Exception)
                    { }

                    //SqlServerUtils job = new SqlServerUtils();
                    //Thread t = new Thread(job.DoShrinkJob);
                    //t.Start();
                }
            }
        }
Exemplo n.º 4
0
        OutputData RunVC(InputData idata)
        {
            CompilerData cdata = null;

            try
            {
                OutputData odata = new OutputData();
                cdata = CreateExecutable(idata);
                if (!cdata.Success)
                {
                    odata.Errors   = cdata.Error;
                    odata.Warnings = cdata.Warning;
                    odata.Stats    = string.Format("Compilation time: {0} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2));
                    return(odata);
                }
                if (!string.IsNullOrEmpty(cdata.Warning))
                {
                    odata.Warnings = cdata.Warning;
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                string nr = cdata.Rand;
                if (!string.IsNullOrEmpty(idata.Input))
                {
                    RedisConnection.Strings.Set(1, nr, Encoding.UTF8.GetBytes(idata.Input));
                }

                RedisConnection.Strings.Set(0, nr, new byte[] { (byte)1 });

                for (int i = 400; i > 0; i--)
                {
                    Thread.Sleep(100);
                    bool _break = false;
                    var  res    = RedisConnection.Strings.Get(4, nr).Result;
                    if (res != null)
                    {
                        _break = true;
                        var output = RedisConnection.Strings.Get(2, nr).Result;
                        if (output != null)
                        {
                            odata.Output = Encoding.UTF8.GetString(output);
                            var a = RedisConnection.Keys.Remove(2, nr).Result;
                        }
                        var errors = RedisConnection.Strings.Get(3, nr).Result;
                        if (errors != null)
                        {
                            odata.Errors = Encoding.UTF8.GetString(errors);
                            var a = RedisConnection.Keys.Remove(3, nr).Result;
                        }
                    }
                    if (_break)
                    {
                        break;
                    }
                }
                watch.Stop();


                if (Utils.IsCompiled(idata.Lang))
                {
                    //odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time: {2} sec, memory peak: {3} Mb", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                else
                {
                    //odata.Stats = string.Format("Absolute running time: {0} sec, cpu time: {1} sec, memory peak: {2} Mb", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Absolute running time: {0} sec", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                return(odata);
            }
            catch (Exception ex)
            {
                return(new OutputData()
                {
                    System_Error = ex.Message
                });
            }
            finally
            {
                if (cdata != null)
                {
                    Cleanup(cdata.CleanThis);
                }
            }
        }
Exemplo n.º 5
0
        OutputData RunVC(InputData idata)
        {
            CompilerData cdata = null;
            try
            {
                OutputData odata = new OutputData();
                cdata = CreateExecutable(idata);
                if (!cdata.Success)
                {
                    odata.Errors = cdata.Error;
                    odata.Warnings = cdata.Warning;
                    odata.Stats = string.Format("Compilation time: {0} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2));
                    return odata;
                }
                if (!string.IsNullOrEmpty(cdata.Warning))
                {
                    odata.Warnings = cdata.Warning;
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                string nr = cdata.Rand;
                if (!string.IsNullOrEmpty(idata.Input))
                {
                    RedisConnection.Strings.Set(1, nr, Encoding.UTF8.GetBytes(idata.Input));
                }

                RedisConnection.Strings.Set(0, nr, new byte[] { (byte)1 });

                for (int i = 400; i > 0; i--)
                {
                    Thread.Sleep(100);
                    bool _break = false;
                    var res = RedisConnection.Strings.Get(4, nr).Result;
                    if (res != null)
                    {
                        _break = true;
                        var output = RedisConnection.Strings.Get(2, nr).Result;
                        if (output != null)
                        {
                            odata.Output = Encoding.UTF8.GetString(output);
                            var a = RedisConnection.Keys.Remove(2, nr).Result;
                        }
                        var errors = RedisConnection.Strings.Get(3, nr).Result;
                        if (errors != null)
                        {
                            odata.Errors = Encoding.UTF8.GetString(errors);
                            var a = RedisConnection.Keys.Remove(3, nr).Result;
                        }
                    }
                    if (_break)
                    {
                        break;
                    }
                }
                watch.Stop();

                if (Utils.IsCompiled(idata.Lang))
                {
                    //odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time: {2} sec, memory peak: {3} Mb", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                else
                {
                    //odata.Stats = string.Format("Absolute running time: {0} sec, cpu time: {1} sec, memory peak: {2} Mb", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Absolute running time: {0} sec", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                return odata;
            }
            catch (Exception ex)
            {
                return new OutputData()
                {
                    System_Error = ex.Message
                };
            }
            finally
            {
                if (cdata != null)
                    Cleanup(cdata.CleanThis);
            }
        }
Exemplo n.º 6
0
        OutputData RunSql(InputData idata)
        {
            OutputData odata = new OutputData();
            string path = BasePath + @"usercode\" + Utils.RandomString() + ".sql";
            using (TextWriter tw = new StreamWriter(path))
            {
                tw.Write(idata.Program);
            }

            using (Process process = new Process())
            {
                try
                {
                    double TotalMemoryInBytes = 0;
                    double TotalThreadCount = 0;
                    int samplesCount = 0;

                    process.StartInfo.FileName = BasePath + @"executables\SqlSandbox.exe";
                    process.StartInfo.Arguments = path.Replace(" ", "|_|");
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;

                    DateTime start = DateTime.Now;
                    process.Start();

                    OutputReader output = new OutputReader(process.StandardOutput);
                    Thread outputReader = new Thread(new ThreadStart(output.ReadOutput));
                    outputReader.Start();
                    OutputReader error = new OutputReader(process.StandardError);
                    Thread errorReader = new Thread(new ThreadStart(error.ReadOutput));
                    errorReader.Start();

                    do
                    {
                        // Refresh the current process property values.
                        process.Refresh();
                        if (!process.HasExited)
                        {
                            try
                            {
                                var proc = process.TotalProcessorTime;
                                // Update the values for the overall peak memory statistics.
                                var mem1 = process.PagedMemorySize64;
                                var mem2 = process.PrivateMemorySize64;

                                //update stats
                                TotalMemoryInBytes += (mem1 + mem2);
                                TotalThreadCount += (process.Threads.Count);
                                samplesCount++;

                                if (proc.TotalSeconds > 5 || mem1 + mem2 > 100000000 || process.Threads.Count > 100 || start + TimeSpan.FromSeconds(15) < DateTime.Now)
                                {
                                    var time = proc.TotalSeconds;
                                    var mem = mem1 + mem2;
                                    process.Kill();
                                    var res = string.Format("Process killed because it exceeded given resources.\nCpu time used {0} sec, absolute running time {1} sec, memory used {2} Mb, nr of threads {3}", time, (int)(DateTime.Now - start).TotalSeconds, (int)(mem / 1048576), process.Threads.Count);
                                    odata.Errors = odata.Errors + "\n" + res;
                                    string partialResult = output.Builder.ToString();
                                    odata.Output = partialResult;
                                    //odata.Stats = string.Format("Absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                                    //Utils.Log.LogCodeToDB(data.Program, data.Input, data.CompilerArgs, res, (int)data.LanguageChoice, data.IsApi);
                                    return odata;
                                }
                            }
                            catch (InvalidOperationException)
                            {
                                break;
                            }
                        }
                    }
                    while (!process.WaitForExit(10));
                    process.WaitForExit();

                    errorReader.Join(5000);
                    outputReader.Join(5000);

                    if (!string.IsNullOrEmpty(error.Output))
                    {
                        odata.Output = output.Builder.ToString();
                        odata.Errors += "\n" + error.Output;
                        odata.Stats = string.Format("Absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                        //Utils.Log.LogCodeToDB(data.Program, data.Input, data.CompilerArgs, error.Output, (int)data.LanguageChoice, data.IsApi);
                        return odata;
                    }

                    if (File.Exists(path + ".stats"))
                    {
                        using (TextReader tr = new StreamReader(path + ".stats"))
                        {
                            odata.Stats = tr.ReadLine();
                            if (!string.IsNullOrEmpty(odata.Stats))
                                odata.Stats += ", ";
                            else
                                odata.Stats = "";
                            odata.Stats += string.Format("absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                        }
                    }
                    //else
                    //{
                    //    odata.Stats = string.Format("Absolute service time: {0} sec", Math.Round((double)(DateTime.Now - start).TotalMilliseconds / 1000, 2));
                    //}

                    odata.Output = output.Output;
                   // Utils.Log.LogCodeToDB(data.Program, data.Input, data.CompilerArgs, "OK", (int)data.LanguageChoice, data.IsApi);
                    return odata;
                }
                catch (Exception e)
                {
                    if (!process.HasExited)
                    {
                        //reExp.Utils.Log.LogInfo("Process left running " + e.Message, "RunSqlServer");
                    }
                    throw;
                }
                finally
                {
                    try
                    {
                        File.Delete(path);
                    }
                    catch (Exception)
                    { }

                    //SqlServerUtils job = new SqlServerUtils();
                    //Thread t = new Thread(job.DoShrinkJob);
                    //t.Start();
                }
            }
        }
Exemplo n.º 7
0
        public OutputData DoWork(InputData idata)
        {
            CompilerData cdata = null;

            try
            {
                OutputData odata = new OutputData();
                cdata = CreateExecutable(idata);
                if (!cdata.Success)
                {
                    odata.Errors   = cdata.Error;
                    odata.Warnings = cdata.Warning;
                    odata.Stats    = string.Format("Compilation time: {0} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2));
                    return(odata);
                }
                if (!string.IsNullOrEmpty(cdata.Warning))
                {
                    odata.Warnings = cdata.Warning;
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                using (Process process = new Process())
                    using (var job = new Job())
                    {
                        process.StartInfo.FileName               = cdata.Executor + (string.IsNullOrEmpty(cdata.Executor) ? "" : " ") + cdata.ExecuteThis;
                        process.StartInfo.UseShellExecute        = false;
                        process.StartInfo.CreateNoWindow         = true;
                        process.StartInfo.RedirectStandardError  = true;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardInput  = true;

                        process.Start();
                        job.AddProcess(process.Handle);

                        if (!string.IsNullOrEmpty(idata.Input))
                        {
                            InputWriter input       = new InputWriter(process.StandardInput, idata.Input);
                            Thread      inputWriter = new Thread(new ThreadStart(input.Writeinput));
                            inputWriter.Start();
                        }

                        OutputReader output       = new OutputReader(process.StandardOutput);
                        Thread       outputReader = new Thread(new ThreadStart(output.ReadOutput));
                        outputReader.Start();
                        OutputReader error       = new OutputReader(process.StandardError);
                        Thread       errorReader = new Thread(new ThreadStart(error.ReadOutput));
                        errorReader.Start();

                        var  start  = DateTime.Now;
                        bool killed = false;
                        do
                        {
                            // Refresh the current process property values.
                            process.Refresh();
                            if (!process.HasExited)
                            {
                                try
                                {
                                    if (start + TimeSpan.FromSeconds(10) < DateTime.Now)
                                    {
                                        process.Kill();
                                        var res = string.Format("Process killed because it ran longer than 10 seconds");
                                        odata.Errors = res;
                                        odata.Output = output.Builder.ToString();
                                        killed       = true;
                                    }
                                }
                                catch (InvalidOperationException)
                                {
                                    break;
                                }
                            }
                        }while (!process.WaitForExit(10));
                        process.WaitForExit();

                        if (!killed)
                        {
                            errorReader.Join(5000);
                            outputReader.Join(5000);

                            if (process.ExitCode != 0)
                            {
                                error.Output = string.Format("Process exit code is not 0: {0}\n", process.ExitCode) + error.Output;
                            }

                            odata.Errors = error.Output;
                            odata.Output = output.Output;
                        }
                    }
                watch.Stop();


                if (Utils.IsCompiled(idata.Lang))
                {
                    //odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time: {2} sec, memory peak: {3} Mb", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                else
                {
                    //odata.Stats = string.Format("Absolute running time: {0} sec, cpu time: {1} sec, memory peak: {2} Mb", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Absolute running time: {0} sec", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                return(odata);
            }
            catch (Exception ex)
            {
                return(new OutputData()
                {
                    System_Error = ex.Message
                });
            }
            finally
            {
                if (cdata != null)
                {
                    Cleanup(cdata.CleanThis);
                }
            }
        }