From: <Or...@us...> - 2008-12-06 13:12:38
|
Revision: 434 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=434&view=rev Author: Oracle_ Date: 2008-12-06 13:12:31 +0000 (Sat, 06 Dec 2008) Log Message: ----------- Added C++ compiler. Added support of LangIDs. Made some small changes to code - now it is a bit more readable. Fixed bug with case sensitivity in SysCallSecure. Modified Paths: -------------- ACMServer/trunk/sharp tester/SourceTest 2.0/Load/LoaderClass.cs ACMServer/trunk/sharp tester/SourceTest 2.0/Main/TestEnvClass.cs ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/Program.cs ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/AllowedFunc.txt ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/InData.txt ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.cpp ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.h ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.cpp ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.h ACMServer/trunk/sharp tester/SourceTest 2.0/Test/Test.cpp Added Paths: ----------- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/CompIDs.txt ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/D7.bat ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/DCC32.exe here.txt ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/dcc32.cfg ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/rlink32.dll here.txt ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/VS.bat ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/includeVS/ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/includeVS/includes here.txt Removed Paths: ------------- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7.bat ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/DCC32.exe here.txt ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/dcc32.cfg ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/includeD7/ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/rlink32.dll here.txt Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/Load/LoaderClass.cs =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/Load/LoaderClass.cs 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/Load/LoaderClass.cs 2008-12-06 13:12:31 UTC (rev 434) @@ -9,19 +9,19 @@ public class DataLoader { public string path; - public int CodeLimit=65536; - public int CompilationTimeLimit=10000; - public int MemoryLimit=67108864; - public int OutputLimit=20000000; - public int RealTimeLimit=20000; - public int TimeLimit=1000; - public int MaxThreads=2; - public int PerTestCount=2; - public string InputName="in.txt"; - public string OutputName="out.txt"; - public string PointFileName="points.txt"; - public string TestFolderName="test*"; - public string Checker=""; + public int CodeLimit = 65536; + public int CompilationTimeLimit = 10000; + public int MemoryLimit = 67108864; + public int OutputLimit = 20000000; + public int RealTimeLimit = 20000; + public int TimeLimit = 1000; + public int MaxThreads = 2; + public int PerTestCount = 2; + public string InputName = "in.txt"; + public string OutputName = "out.txt"; + public string PointFileName = "points.txt"; + public string TestFolderName = "test*"; + public string Checker = ""; public string CheckPlugin = ""; public DataLoader(string Path) @@ -36,7 +36,7 @@ StreamReader f = File.OpenText(path); while (!f.EndOfStream) { - string[] param=f.ReadLine().Split(' '); + string[] param = f.ReadLine().Split(' '); if (param.Length < 2) continue; try { @@ -88,14 +88,22 @@ OutPath = outname; PointPath = pointname; if (File.Exists(inname)) - input = File.OpenText(inname).ReadToEnd(); + { + input = File.OpenText(inname).ReadToEnd(); + } else + { input = ""; + } if (File.Exists(outname)) + { output = File.OpenText(outname).ReadToEnd(); + } else + { output = ""; + } if (File.Exists(pointname)) { @@ -109,7 +117,9 @@ } } else + { points = 1; + } } } @@ -118,8 +128,10 @@ private string MaskToString(string mask, int numb) { int star; - if ((star=mask.IndexOf('*')) == -1) + if ((star = mask.IndexOf('*')) == -1) + { return mask; + } int cnt=0; while (star < mask.Length && mask[star] == '*') { @@ -127,8 +139,11 @@ cnt++; } string NS = numb.ToString(); - while (NS.Length < cnt) NS = '0' + NS; - return mask.Replace(mask.Substring(star - cnt, cnt), NS); ; + while (NS.Length < cnt) + { + NS = '0' + NS; + } + return mask.Replace(mask.Substring(star - cnt, cnt), NS); } public string InName; @@ -155,8 +170,10 @@ string curInPath = ProblemPath + MaskToString(FolderName,cnt+1)+"\\"+MaskToString(InName,cnt+1); string curOutPath = ProblemPath + MaskToString(FolderName, cnt+1) + "\\" + MaskToString(OutName, cnt+1); string curPointPath = ProblemPath + MaskToString(FolderName, cnt+1) + "\\" + MaskToString(PointName, cnt+1); - if (File.Exists(curInPath)||File.Exists(curOutPath)||File.Exists(curPointPath)) - cnt++; + if (File.Exists(curInPath) || File.Exists(curOutPath) || File.Exists(curPointPath)) + { + cnt++; + } else break; } Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/Main/TestEnvClass.cs =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/Main/TestEnvClass.cs 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/Main/TestEnvClass.cs 2008-12-06 13:12:31 UTC (rev 434) @@ -1,4 +1,9 @@ -using System; +/* + This file includes definition of class TestEnv, that encapsulates all features of Tester. + Only this file should be used by user. +*/ + +using System; using System.IO; using Test; using Load; @@ -16,25 +21,27 @@ { public string ProblemPath; public string TempPath; - public string CompPath; + public int LangID; //language ID, that stores in CompIDs.txt public string Source; public Compiler comp; public Runner run; - public TestEnv(string Source,string CompPath,string TempPath,string ProblemPath) + public TestEnv(string Source,int LangID,string TempPath,string ProblemPath) { try { this.Source = Source; this.TempPath = Path.GetFullPath(TempPath); if (!Directory.Exists(this.TempPath)) + { Directory.CreateDirectory(this.TempPath); - this.CompPath = Path.GetFullPath(CompPath); - if (!File.Exists(this.CompPath)) - throw new FileNotFoundException("Compiler file do not exists!"); + } + this.LangID = LangID; this.ProblemPath = Path.GetFullPath(ProblemPath); if (!Directory.Exists(this.ProblemPath)) + { throw new DirectoryNotFoundException("Problem path do not exists!"); + } } catch (Exception e) { @@ -49,16 +56,24 @@ if (comp == null) { if (!File.Exists(ProblemPath + "ProblemData.txt")) + { throw new FileNotFoundException("ProblemData.txt do not exists"); + } DataLoader dat = new DataLoader(ProblemPath + "ProblemData.txt"); if (!dat.Load()) + { throw new FileLoadException("Can not load data from ProblemData.txt"); + } if (!File.Exists("TesterInData.txt")) + { throw new FileNotFoundException("TesterInData.txt do not exists"); + } string[] InData = System.IO.File.ReadAllLines("TesterInData.txt"); if (InData.Length < 1) + { throw new FileLoadException("Can not load data from ProblemData.txt"); + } int buf; try { @@ -69,7 +84,7 @@ throw new FileLoadException("Can not load sleep time from TesterInData.txt " + e.Message); } - comp = new Compiler(CompPath, Source, TempPath, dat.CompilationTimeLimit, buf); + comp = new Compiler(LangID, Source, TempPath, dat.CompilationTimeLimit, buf); } comp.Compile(); } @@ -83,6 +98,7 @@ { RunAllTests(SecureType.Double,true); } + public void RunAllTests(SecureType SecType) { RunAllTests(SecType,true); @@ -95,7 +111,9 @@ if (run == null) { if (comp == null) + { throw new TesterException("You should first compile source before running tests!"); + } run = new Runner(SecType, ProblemPath, comp.ExeFile, TempPath, comp.CONST_SLEEP); } run.TestAll = TestAll; @@ -119,7 +137,9 @@ if (run == null) { if (comp == null) + { throw new TesterException("You should first compile source before running tests!"); + } run = new Runner(SecType, ProblemPath, comp.ExeFile, TempPath, comp.CONST_SLEEP); } run.ExecuteTest(index); Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/Program.cs =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/Program.cs 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/Program.cs 2008-12-06 13:12:31 UTC (rev 434) @@ -1,4 +1,8 @@ -using System; +/* + This file is just a small program, used to test all features of tester. +*/ + +using System; using System.Collections.Generic; using System.Text; using System.IO; @@ -13,10 +17,10 @@ { Console.SetOut(File.CreateText("D:\\logout.txt")); string[] data=File.ReadAllLines("InData.txt"); - for (int ind = 1; ind <= 5; ind++) + for (int ind = 1; ind <= 1; ind++) { string src = File.ReadAllText("src\\source" + ind.ToString() + ".txt"); - TestEnv test = new TestEnv(src, data[0], data[1], data[2]); + TestEnv test = new TestEnv(src, Convert.ToInt32(data[0]), data[1], data[2]); test.Compile(); Console.WriteLine("------------------BEGIN SOURCE #"+ind.ToString()+"--------------------------"); Console.WriteLine(src); Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/AllowedFunc.txt =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/AllowedFunc.txt 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/AllowedFunc.txt 2008-12-06 13:12:31 UTC (rev 434) @@ -54,4 +54,52 @@ kernel32.dll GetFileSize kernel32.dll GetFileType kernel32.dll CreateFileA -kernel32.dll CloseHandle \ No newline at end of file +kernel32.dll CloseHandle + +kernel32.dll HeapFree +kernel32.dll HeapAlloc +kernel32.dll GetProcessHeap +kernel32.dll FreeEnvironmentStringsA +kernel32.dll GetEnvironmentStrings +kernel32.dll FreeEnvironmentStringsW +kernel32.dll GetEnvironmentStringsW +kernel32.dll SetHandleCount +kernel32.dll TlsAlloc +kernel32.dll TlsFree +kernel32.dll InterlockedIncrement +kernel32.dll SetLastError +kernel32.dll InterlockedDecrement +kernel32.dll HeapDestroy +kernel32.dll HeapCreate +kernel32.dll QueryPerformanceCounter +kernel32.dll GetTickCount +kernel32.dll GetCurrentProcessId +kernel32.dll GetSystemTimeAsFileTime +kernel32.dll TerminateProcess +kernel32.dll GetCurrentProcess +kernel32.dll SetUnhandledExceptionFilter +kernel32.dll IsDebuggerPresent +kernel32.dll LoadLibraryA +kernel32.dll Sleep +kernel32.dll GetOEMCP +kernel32.dll HeapReAlloc +kernel32.dll HeapSize +kernel32.dll MultiByteToWideChar +kernel32.dll LCMapStringA +kernel32.dll LCMapStringW +kernel32.dll GetStringTypeA +kernel32.dll GetStringTypeW + +kernel32.dll InterlockedExchange +kernel32.dll GetUserDefaultLCID +kernel32.dll EnumSystemLocalesA +kernel32.dll IsValidLocale +kernel32.dll IsValidCodePage +kernel32.dll GetConsoleCP +kernel32.dll GetConsoleMode +kernel32.dll FlushFileBuffers +kernel32.dll GetLocaleInfoW +kernel32.dll WriteConsoleA +kernel32.dll GetConsoleOutputCP +kernel32.dll WriteConsoleW +kernel32.dll SetStdHandle \ No newline at end of file Added: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/CompIDs.txt =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/CompIDs.txt (rev 0) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/CompIDs.txt 2008-12-06 13:12:31 UTC (rev 434) @@ -0,0 +1,2 @@ +1 Compiler\VC6\VS.bat +2 Compiler\D7\D7.bat \ No newline at end of file Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/InData.txt =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/InData.txt 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/InData.txt 2008-12-06 13:12:31 UTC (rev 434) @@ -1,3 +1,3 @@ -Compiler\D7.bat +1 Temp\ Problem\ \ No newline at end of file Property changes on: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7 ___________________________________________________________________ Added: tsvn:logminsize + 5 Added: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/D7.bat =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/D7.bat (rev 0) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/D7.bat 2008-12-06 13:12:31 UTC (rev 434) @@ -0,0 +1 @@ +DCC32.EXE -CC %1 -E%2 \ No newline at end of file Added: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/dcc32.cfg =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/dcc32.cfg (rev 0) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7/dcc32.cfg 2008-12-06 13:12:31 UTC (rev 434) @@ -0,0 +1 @@ +-u"includeD7" Deleted: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7.bat =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7.bat 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/D7.bat 2008-12-06 13:12:31 UTC (rev 434) @@ -1 +0,0 @@ -DCC32.EXE -CC %1 -E%2 \ No newline at end of file Property changes on: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6 ___________________________________________________________________ Added: tsvn:logminsize + 5 Added: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/VS.bat =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/VS.bat (rev 0) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/VS.bat 2008-12-06 13:12:31 UTC (rev 434) @@ -0,0 +1 @@ +cl /EHsc /TP /Za /O1 /IincludeVS /D ONLINE_JUDGE /Fe%2source.exe" %1 /link /LIBPATH:includeVS \ No newline at end of file Property changes on: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/VC6/includeVS ___________________________________________________________________ Added: tsvn:logminsize + 5 Deleted: ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/dcc32.cfg =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/dcc32.cfg 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/SourceTest 2.0/bin/Debug/compiler/dcc32.cfg 2008-12-06 13:12:31 UTC (rev 434) @@ -1 +0,0 @@ --u"includeD7" Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.cpp =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.cpp 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.cpp 2008-12-06 13:12:31 UTC (rev 434) @@ -3,9 +3,50 @@ using namespace System::IO; using namespace System::Diagnostics; -Compiler::Compiler(String^ CompPath, String^ Source, String^ TempPath, int CompilationTimeLimit, int WaitForFile) +String^ Compiler::GetCompFromID(int LangID) //resolves ID to compiler's path { - comppath=CompPath; + if (!File::Exists("CompIDs.txt")) + { + throw gcnew FileNotFoundException("Can not find file CompIDs.txt"); + } + StreamReader^ f = File::OpenText("CompIDs.txt"); + String^ res = ""; + while (!f->EndOfStream) + { + array<String^>^ param = f->ReadLine()->Split(' '); + if (param->Length < 2) continue; + try + { + int curID = Convert::ToInt32(param[0]); + if (curID == LangID) + { + res=""; + for (int i=1;i<param->Length;i++) + { + res += param[i]+" "; + } + } + } + catch (System::Exception^) + { + throw gcnew FileLoadException("Can not load data from CompIDs.txt"); + } + } + if (res=="") + { + throw gcnew FileLoadException("Can not load data from CompIDs.txt"); + } + return res; +} + +Compiler::Compiler(int LangID, String^ Source, String^ TempPath, int CompilationTimeLimit, int WaitForFile) +{ + comppath=GetCompFromID(LangID); + comppath=Path::GetFullPath(comppath); + if (!File::Exists(comppath)) + { + throw gcnew FileNotFoundException("Can not find compiler!"); + } source=Source; temp=TempPath; ExeFile=TempPath+"source.exe"; @@ -42,9 +83,15 @@ System::Threading::Thread::Sleep(CONST_SLEEP); for (int i=0;i<CONST_SLEEP;i++) + { if (!File::Exists(srcpath)) - System::Threading::Thread::Sleep(5); else + { + System::Threading::Thread::Sleep(5); + } else + { break; + } + } si->FileName=comppath; si->Arguments="\""+srcpath+"\" \""+temp; @@ -63,9 +110,15 @@ UsedTime=comp->TotalProcessorTime; for (int i=0;i<CONST_SLEEP;i++) + { if (!File::Exists(ExeFile)) - System::Threading::Thread::Sleep(5); else + { + System::Threading::Thread::Sleep(5); + } else + { break; + } + } if (!File::Exists(ExeFile)) //compilation error { Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.h =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.h 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/Test/CompileClass.h 2008-12-06 13:12:31 UTC (rev 434) @@ -22,6 +22,7 @@ String^ comppath; String^ source; String^ temp; + String^ GetCompFromID(int LangID); public: String^ Details; CompRes Result; @@ -31,7 +32,7 @@ int TL; int CONST_SLEEP; - Compiler(String^ CompPath, String^ Source, String^ TempPath, int CompilationTimeLimit, int WaitForFile); + Compiler(int LangID, String^ Source, String^ TempPath, int CompilationTimeLimit, int WaitForFile); void Compile(); }; } \ No newline at end of file Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.cpp =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.cpp 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.cpp 2008-12-06 13:12:31 UTC (rev 434) @@ -8,9 +8,9 @@ UserSecure::UserSecure() { - process=NULL; - thread=NULL; - err=0; + process=NULL; + thread=NULL; + err=0; } BOOL UserSecure::SetUserObjectFullAccess(HANDLE hUserObject) @@ -39,86 +39,92 @@ bool UserSecure::Init(LPWSTR lpApplicationName) { - FILE* f=_wfopen(L"TesterInData.txt",L"r"); - if (f==NULL) - { - Details="Cannot open TesterInData.txt"; - return false; - } - int buf=0; - fwscanf(f,L"%d %s %s",&buf,&USER_NAME,&USER_PASSW); - fclose(f); + FILE* f=_wfopen(L"TesterInData.txt",L"r"); + if (f==NULL) + { + Details="Cannot open TesterInData.txt"; + return false; + } + int buf=0; + fwscanf(f,L"%d %s %s",&buf,&USER_NAME,&USER_PASSW); + fclose(f); - if (!SetUserObjectFullAccess(GetThreadDesktop(GetCurrentThreadId()))) - { - Details="Cannot SetUserObjectFullAccess(GetThreadDesktop(GetCurrentThreadId())). Error#"+ToStr(GetLastError()); - return false; //needed for accessing from CreateProcessAsUser - } - if (!SetUserObjectFullAccess( GetProcessWindowStation())) - { - Details="Cannot SetUserObjectFullAccess( GetProcessWindowStation()). Error#"+ToStr(GetLastError()); - return false; //needed for accessing from CreateProcessAsUser - } + if (!SetUserObjectFullAccess(GetThreadDesktop(GetCurrentThreadId()))) + { + Details="Cannot SetUserObjectFullAccess(GetThreadDesktop(GetCurrentThreadId())). Error#"+ToStr(GetLastError()); + return false; //needed for accessing from CreateProcessAsUser + } + if (!SetUserObjectFullAccess( GetProcessWindowStation())) + { + Details="Cannot SetUserObjectFullAccess(GetProcessWindowStation()). Error#"+ToStr(GetLastError()); + return false; //needed for accessing from CreateProcessAsUser + } - USER_INFO_1 ui; - NET_API_STATUS st; - ui.usri1_comment=NULL; - ui.usri1_flags=UF_SCRIPT|UF_PASSWD_CANT_CHANGE|UF_DONT_EXPIRE_PASSWD; - ui.usri1_home_dir=NULL; - ui.usri1_name=USER_NAME; - ui.usri1_password=USER_PASSW; - ui.usri1_priv=USER_PRIV_USER; - ui.usri1_script_path=NULL; - st=NetUserAdd(NULL,1,(LPBYTE)&ui,NULL); //trying to create new user - bool userex=false; - if (st!=NERR_Success) - { - if (st==NERR_UserExists) - userex=true; else - { - Details="Cannot create user. Error#"+ToStr(GetLastError()); - return false; - } - } - if (!userex) - { - array<DriveInfo^>^ drv=DriveInfo::GetDrives(); - for (int i=0;i<drv->Length;i++) //for all fixed drives set limitation - if (drv[i]->DriveType==DriveType::Fixed) - DenyAccessFolder(drv[i]->Name); - wchar_t buf[MAX_PATH+1]; - GetEnvironmentVariableW(L"ProgramFiles",buf,MAX_PATH); - DenyAccessFolder(gcnew String(buf)); - GetEnvironmentVariable(L"ALLUSERSPROFILE",buf,MAX_PATH); - DenyAccessFolder(gcnew String(buf)); - GetWindowsDirectory(buf,MAX_PATH); - DenyAccessFolder(gcnew String(buf)); - GetSystemDirectory(buf,MAX_PATH); - DenyAccessFolder(gcnew String(buf)); - DWORD size=MAX_PATH; - GetProfilesDirectory(buf,&size); - DenyAccessFolder(gcnew String(buf)); - Details="User created. "; - } else - Details="User exists. "; - return true; + USER_INFO_1 ui; + NET_API_STATUS st; + ui.usri1_comment=NULL; + ui.usri1_flags=UF_SCRIPT|UF_PASSWD_CANT_CHANGE|UF_DONT_EXPIRE_PASSWD; + ui.usri1_home_dir=NULL; + ui.usri1_name=USER_NAME; + ui.usri1_password=USER_PASSW; + ui.usri1_priv=USER_PRIV_USER; + ui.usri1_script_path=NULL; + st=NetUserAdd(NULL,1,(LPBYTE)&ui,NULL); //trying to create new user + bool userex=false; + if (st!=NERR_Success) + { + if (st==NERR_UserExists) + { + userex=true; + } else + { + Details="Cannot create user. Error#"+ToStr(GetLastError()); + return false; + } + } + if (!userex) + { + array<DriveInfo^>^ drv=DriveInfo::GetDrives(); + for (int i=0;i<drv->Length;i++) //for all fixed drives set limitation + { + if (drv[i]->DriveType==DriveType::Fixed) + { + DenyAccessFolder(drv[i]->Name); + } + } + wchar_t buf[MAX_PATH+1]; + GetEnvironmentVariableW(L"ProgramFiles",buf,MAX_PATH); + DenyAccessFolder(gcnew String(buf)); + GetEnvironmentVariable(L"ALLUSERSPROFILE",buf,MAX_PATH); + DenyAccessFolder(gcnew String(buf)); + GetWindowsDirectory(buf,MAX_PATH); + DenyAccessFolder(gcnew String(buf)); + GetSystemDirectory(buf,MAX_PATH); + DenyAccessFolder(gcnew String(buf)); + DWORD size=MAX_PATH; + GetProfilesDirectory(buf,&size); + DenyAccessFolder(gcnew String(buf)); + Details="User created. "; + } else + Details="User exists. "; + return true; } BOOL UserSecure::AddPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege) { -TOKEN_PRIVILEGES tp; -LUID luid; -if (!LookupPrivilegeValue( NULL,lpszPrivilege,&luid)) - return FALSE; -tp.PrivilegeCount = 1; -tp.Privileges[0].Luid = luid; -tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + TOKEN_PRIVILEGES tp; + LUID luid; + if (!LookupPrivilegeValue( NULL,lpszPrivilege,&luid)) + return FALSE; + tp.PrivilegeCount = 1; + tp.Privileges[0].Luid = luid; + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; -if ( !AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),(PTOKEN_PRIVILEGES) NULL,(PDWORD) NULL) ) - return FALSE; -if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) - return FALSE; -return TRUE; + if (!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),(PTOKEN_PRIVILEGES)NULL,(PDWORD)NULL)) + return FALSE; + if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) + return FALSE; + return TRUE; } bool UserSecure::CreateProc(LPWSTR lpApplicationName,BOOL bInheritHandles,DWORD dwCreationFlags,LPSTARTUPINFOW lpStartupInfo) @@ -186,151 +192,153 @@ bool UserSecure::CheckSecure() { - return false; //NoDF + return false; //NoDF } void UserSecure::Finalize() { - if (process!=NULL) - CloseHandle(process); - if (thread!=NULL) - CloseHandle(thread); + if (process!=NULL) + CloseHandle(process); + if (thread!=NULL) + CloseHandle(thread); } SysCallSecure::SysCallSecure() { -process=NULL; -thread=NULL; -err=0; + process=NULL; + thread=NULL; + err=0; } bool SysCallSecure::Init(LPWSTR lpApplicationName) { -HasDF=false; -FILE* f=fopen("AllowedFunc.txt","r"); -if (f==NULL) -{ - Details="Cannot open AllowedFunc.txt"; - return false; -} -vector<TFunc> allowed; -char mod_name[256],func_name[256]; -while (fscanf(f,"%s %s",mod_name,func_name)==2) -{ - TFunc func; - func.Name=func_name; - func.Module=mod_name; - allowed.push_back(func); -} -fclose(f); + HasDF=false; + FILE* f=fopen("AllowedFunc.txt","r"); + if (f==NULL) + { + Details="Cannot open AllowedFunc.txt"; + return false; + } + vector<TFunc> allowed; + char mod_name[256],func_name[256]; + while (fscanf(f,"%s %s",mod_name,func_name)==2) + { + TFunc func; + strcpy_s(func.Name,256,func_name); + strcpy_s(func.Module,256,mod_name); + allowed.push_back(func); + } + fclose(f); -HMODULE hmodCaller=LoadLibrary(lpApplicationName); -if (hmodCaller==NULL) -{ - Details="Can not load module. Error#"+ToStr(err=GetLastError()); - return false; -} -ULONG ulSize; -PIMAGE_IMPORT_DESCRIPTOR pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR) ImageDirectoryEntryToData(hmodCaller,TRUE,IMAGE_DIRECTORY_ENTRY_IMPORT,&ulSize); -if (pImportDesc!=NULL) -{ -for (;pImportDesc->Name; pImportDesc++) -{ - PSTR pszModName = (PSTR)((PBYTE) hmodCaller + pImportDesc->Name); - PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)((PBYTE) hmodCaller + pImportDesc->FirstThunk); - for (; pThunk->u1.Function; pThunk++) - { - if ((pThunk->u1.Function&(1<<31))!=0) - continue; - PSTR NAME=(PSTR)((PIMAGE_IMPORT_BY_NAME)((PBYTE)hmodCaller+pThunk->u1.AddressOfData))->Name; - - bool allow=false; - for (unsigned i=0;i<allowed.size();i++) - if ((allowed[i].Name==string(NAME))&&(allowed[i].Module==string(pszModName))) + HMODULE hmodCaller=LoadLibrary(lpApplicationName); + if (hmodCaller==NULL) { - allow=true; - break; + Details="Can not load module. Error#"+ToStr(err=GetLastError()); + return false; } - if (!allow) - { - HasDF=true; - Details="Import DF. \""+string(NAME)+"\" from module \""+string(pszModName)+"\" "; - FreeLibrary(hmodCaller); - return true; - } - } + ULONG ulSize; + PIMAGE_IMPORT_DESCRIPTOR pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR) ImageDirectoryEntryToData(hmodCaller,TRUE,IMAGE_DIRECTORY_ENTRY_IMPORT,&ulSize); + if (pImportDesc!=NULL) + { + for (;pImportDesc->Name; pImportDesc++) + { + PSTR pszModName = (PSTR)((PBYTE) hmodCaller + pImportDesc->Name); + PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)((PBYTE) hmodCaller + pImportDesc->FirstThunk); + for (; pThunk->u1.Function; pThunk++) + { + if ((pThunk->u1.Function&(1<<31))!=0) + continue; + PSTR NAME=(PSTR)((PIMAGE_IMPORT_BY_NAME)((PBYTE)hmodCaller+pThunk->u1.AddressOfData))->Name; + + bool allow=false; + for (unsigned i=0;i<allowed.size();i++) + { + if ((strcmp(allowed[i].Name,NAME)==0)&&(stricmp(allowed[i].Module,pszModName)==0)) + { + allow=true; + break; + } + } + if (!allow) + { + HasDF=true; + Details="Import DF. \""+string(NAME)+"\" from module \""+string(pszModName)+"\" "; + FreeLibrary(hmodCaller); + return true; + } + } + } + } + FreeLibrary(hmodCaller); + return true; } -} -FreeLibrary(hmodCaller); -return true; -} bool SysCallSecure::CreateProc(LPWSTR lpApplicationName,BOOL bInheritHandles,DWORD dwCreationFlags,LPSTARTUPINFOW lpStartupInfo) { -PROCESS_INFORMATION pi;ZeroMemory(&pi,sizeof(pi)); -if (!CreateProcess(lpApplicationName,NULL,NULL,NULL,bInheritHandles,dwCreationFlags,NULL,NULL,lpStartupInfo,&pi)) -{ - Details+="Cannot create process. Error#"+ToStr(err=GetLastError()); - return false; -} -process=pi.hProcess; -thread=pi.hThread; + PROCESS_INFORMATION pi;ZeroMemory(&pi,sizeof(pi)); + if (!CreateProcess(lpApplicationName,NULL,NULL,NULL,bInheritHandles,dwCreationFlags,NULL,NULL,lpStartupInfo,&pi)) + { + Details+="Cannot create process. Error#"+ToStr(err=GetLastError()); + return false; + } + process=pi.hProcess; + thread=pi.hThread; -Details+="Process created."; -return true; + Details+="Process created."; + return true; } bool SysCallSecure::CheckSecure() { -return HasDF; + return HasDF; } void SysCallSecure::Finalize() { -if (process!=NULL) - CloseHandle(process); -if (thread!=NULL) - CloseHandle(thread); + if (process!=NULL) + CloseHandle(process); + if (thread!=NULL) + CloseHandle(thread); } NoneSecure::NoneSecure() { -process=NULL; -thread=NULL; -err=0; + process=NULL; + thread=NULL; + err=0; } bool NoneSecure::Init(LPWSTR lpApplicationName) { -return true; + return true; } bool NoneSecure::CreateProc(LPWSTR lpApplicationName,BOOL bInheritHandles,DWORD dwCreationFlags,LPSTARTUPINFOW lpStartupInfo) { -PROCESS_INFORMATION pi;ZeroMemory(&pi,sizeof(pi)); -if (CreateProcess(lpApplicationName,NULL,NULL,NULL,bInheritHandles,dwCreationFlags,NULL,NULL,lpStartupInfo,&pi)) -{ - process=pi.hProcess; - thread=pi.hThread; - Details="Process created."; - return true; + PROCESS_INFORMATION pi;ZeroMemory(&pi,sizeof(pi)); + if (CreateProcess(lpApplicationName,NULL,NULL,NULL,bInheritHandles,dwCreationFlags,NULL,NULL,lpStartupInfo,&pi)) + { + process=pi.hProcess; + thread=pi.hThread; + Details="Process created."; + return true; + } + err=GetLastError(); + Details="Cannot create process. Error#"+ToStr(err); + return false; } -err=GetLastError(); -Details="Cannot create process. Error#"+ToStr(err); -return false; -} bool NoneSecure::CheckSecure() { -return false; //No DF + return false; //No DF } void NoneSecure::Finalize() { -if (process!=NULL) - CloseHandle(process); -if (thread!=NULL) - CloseHandle(thread); + if (process!=NULL) + CloseHandle(process); + if (thread!=NULL) + CloseHandle(thread); } DoubleSecure::DoubleSecure() Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.h =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.h 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/Test/SecureClass.h 2008-12-06 13:12:31 UTC (rev 434) @@ -13,16 +13,39 @@ { protected: int err; - string ToStr(int x) {char c[15]; _itoa_s(x,c,10); return string(c);}; + string ToStr(int x) + { + char c[15]; + _itoa_s(x,c,10); + return string(c); + }; + public: string Details; HANDLE process; HANDLE thread; - virtual bool Init(LPWSTR lpApplicationName) {return true;}; - virtual bool CreateProc(LPWSTR lpApplicationName,BOOL bInheritHandles,DWORD dwCreationFlags,LPSTARTUPINFOW lpStartupInfo) {return true;}; - virtual bool CheckSecure() {return true;}; + + virtual bool Init(LPWSTR lpApplicationName) + { + return true; + }; + + virtual bool CreateProc(LPWSTR lpApplicationName,BOOL bInheritHandles,DWORD dwCreationFlags,LPSTARTUPINFOW lpStartupInfo) + { + return true; + }; + + virtual bool CheckSecure() + { + return true; + }; + virtual void Finalize() {}; - virtual int LastError() {return err;}; + + virtual int LastError() + { + return err; + } }; public class UserSecure: public BasicSecure @@ -43,8 +66,8 @@ private struct TFunc { - string Name; - string Module; + char Name[256]; + char Module[256]; }; public class SysCallSecure:public BasicSecure @@ -69,7 +92,10 @@ bool CreateProc(LPWSTR lpApplicationName,BOOL bInheritHandles,DWORD dwCreationFlags,LPSTARTUPINFOW lpStartupInfo); bool CheckSecure(); void Finalize(); - int LastError() {return syssec->LastError(); } + int LastError() + { + return syssec->LastError(); + } }; public class NoneSecure:public BasicSecure Modified: ACMServer/trunk/sharp tester/SourceTest 2.0/Test/Test.cpp =================================================================== --- ACMServer/trunk/sharp tester/SourceTest 2.0/Test/Test.cpp 2008-11-07 10:17:40 UTC (rev 433) +++ ACMServer/trunk/sharp tester/SourceTest 2.0/Test/Test.cpp 2008-12-06 13:12:31 UTC (rev 434) @@ -11,16 +11,22 @@ SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS); //for better testing initlock(); if (!File::Exists(ProblemPath + "ProblemData.txt")) + { throw gcnew FileNotFoundException("ProblemData not found!"); + } data = gcnew DataLoader(ProblemPath + "ProblemData.txt"); if (!data->Load()) + { throw gcnew FileLoadException("Can not load problem data!"); + } testfield=gcnew TestLoader(data->InputName,data->OutputName,data->PointFileName,data->TestFolderName,ProblemPath); testfield->Load(); results=gcnew array<TestRes^>(testfield->tests->Length); plug=gcnew PluginLoader(System::IO::Path::GetFullPath(data->CheckPlugin)); if (!plug->Load()) + { throw gcnew FileLoadException("Can not load Plugin!"); + } exe=ExeFile; prob=ProblemPath; temp=TempPath; @@ -31,288 +37,295 @@ bool Runner::ExecuteTest(int index) //returns false if not AC verdict { -STARTUPINFO si;ZeroMemory(&si,sizeof(si)); -SECURITY_ATTRIBUTES secdesc; -secdesc.bInheritHandle=TRUE; -secdesc.nLength=sizeof(secdesc); -secdesc.lpSecurityDescriptor=NULL; -JOBOBJECT_BASIC_ACCOUNTING_INFORMATION acc; -JOBOBJECT_EXTENDED_LIMIT_INFORMATION mem; -HANDLE job=NULL; -JOBOBJECT_ASSOCIATE_COMPLETION_PORT port; ZeroMemory(&port,sizeof(port)); -JOBOBJECT_BASIC_UI_RESTRICTIONS uilim; -DWORD len=0; -wchar_t* bufW; -TestRes^ result=gcnew TestRes(); -BasicSecure* sec=NULL; -try -{ - if (SecType==SecureType::None) - sec=new NoneSecure(); else - if (SecType==SecureType::User) - sec=new UserSecure(); else - if (SecType==SecureType::SysCall) - sec=new SysCallSecure(); else - if (SecType==SecureType::Double) - sec=new DoubleSecure(); - lock(); - String^ oupath=temp+"out"+index.ToString()+".txt"; - String^ erpath=temp+"err"+index.ToString()+".txt"; - unlock(); - String^ inpath=test->tests[index-1]->InPath; - - si.cb=sizeof(si); - si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; - si.wShowWindow=FALSE; - si.hStdInput= CreateFile(bufW=(wchar_t*)Marshal::StringToHGlobalUni(inpath).ToPointer(),GENERIC_READ,FILE_SHARE_READ,&secdesc,OPEN_EXISTING,0,NULL); Marshal::FreeHGlobal(IntPtr(bufW)); - si.hStdOutput=CreateFile(bufW=(wchar_t*)Marshal::StringToHGlobalUni(oupath).ToPointer(),GENERIC_WRITE,0,&secdesc,CREATE_ALWAYS,0,NULL); Marshal::FreeHGlobal(IntPtr(bufW)); - si.hStdError=CreateFile(bufW=(wchar_t*)Marshal::StringToHGlobalUni(erpath).ToPointer(),GENERIC_WRITE,FILE_SHARE_WRITE,&secdesc,CREATE_ALWAYS,0,NULL); Marshal::FreeHGlobal(IntPtr(bufW)); + STARTUPINFO si;ZeroMemory(&si,sizeof(si)); + SECURITY_ATTRIBUTES secdesc; + secdesc.bInheritHandle=TRUE; + secdesc.nLength=sizeof(secdesc); + secdesc.lpSecurityDescriptor=NULL; + JOBOBJECT_BASIC_ACCOUNTING_INFORMATION acc; + JOBOBJECT_EXTENDED_LIMIT_INFORMATION mem; + HANDLE job=NULL; + JOBOBJECT_ASSOCIATE_COMPLETION_PORT port; ZeroMemory(&port,sizeof(port)); + JOBOBJECT_BASIC_UI_RESTRICTIONS uilim; + DWORD len=0; + wchar_t* bufW; + TestRes^ result=gcnew TestRes(); + BasicSecure* sec=NULL; + try + { + lock(); + if (SecType==SecureType::None) + sec=new NoneSecure(); else + if (SecType==SecureType::User) + sec=new UserSecure(); else + if (SecType==SecureType::SysCall) + sec=new SysCallSecure(); else + if (SecType==SecureType::Double) + sec=new DoubleSecure(); + String^ oupath=temp+"out"+index.ToString()+".txt"; + String^ erpath=temp+"err"+index.ToString()+".txt"; + unlock(); + String^ inpath=test->tests[index-1]->InPath; + + si.cb=sizeof(si); + si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; + si.wShowWindow=FALSE; + si.hStdInput= CreateFile(bufW=(wchar_t*)Marshal::StringToHGlobalUni(inpath).ToPointer(),GENERIC_READ,FILE_SHARE_READ,&secdesc,OPEN_EXISTING,0,NULL); Marshal::FreeHGlobal(IntPtr(bufW)); + si.hStdOutput=CreateFile(bufW=(wchar_t*)Marshal::StringToHGlobalUni(oupath).ToPointer(),GENERIC_WRITE,0,&secdesc,CREATE_ALWAYS,0,NULL); Marshal::FreeHGlobal(IntPtr(bufW)); + si.hStdError=CreateFile(bufW=(wchar_t*)Marshal::StringToHGlobalUni(erpath).ToPointer(),GENERIC_WRITE,FILE_SHARE_WRITE,&secdesc,CREATE_ALWAYS,0,NULL); Marshal::FreeHGlobal(IntPtr(bufW)); - SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX); - lock(); - bufW=(wchar_t*)Marshal::StringToHGlobalUni(exe).ToPointer(); - unlock(); + SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX); + lock(); + bufW=(wchar_t*)Marshal::StringToHGlobalUni(exe).ToPointer(); + bool Succed=sec->Init(bufW); + unlock(); - if (!sec->Init(bufW)) - { - int err=sec->LastError(); - if ((err==193)||(err==1455)) + if (!Succed) { - result->res=TestResult::RuntimeError; - result->Details=gcnew String(sec->Details.c_str())+" Too large array"; - } else + int err=sec->LastError(); + if ((err==193)||(err==1455)) + { + result->res=TestResult::RuntimeError; + result->Details=gcnew String(sec->Details.c_str())+" Too large array"; + } else + { + result->res=TestResult::InternalError; + result->Details=gcnew String(sec->Details.c_str())+" Can not init secure"; + } + Marshal::FreeHGlobal(IntPtr(bufW)); + return false; + } + + if (sec->CheckSecure()) { - result->res=TestResult::InternalError; - result->Details=gcnew String(sec->Details.c_str())+" Can not init secure"; + result->res=TestResult::DestrictedFunction; + result->Details=gcnew String(sec->Details.c_str())+"Destricted function."; + Marshal::FreeHGlobal(IntPtr(bufW)); + return false; } - Marshal::FreeHGlobal(IntPtr(bufW)); - return false; - } - if (sec->CheckSecure()) - { - result->res=TestResult::DestrictedFunction; - result->Details=gcnew String(sec->Details.c_str())+"Destricted function."; - Marshal::FreeHGlobal(IntPtr(bufW)); - return false; - } - - if (!sec->CreateProc(bufW,TRUE,CREATE_SUSPENDED|CREATE_NO_WINDOW|ABOVE_NORMAL_PRIORITY_CLASS,&si)) - { - Marshal::FreeHGlobal(IntPtr(bufW)); - int err=sec->LastError(); - if ((err==193)||(err==1455)) + if (!sec->CreateProc(bufW,TRUE,CREATE_SUSPENDED|CREATE_NO_WINDOW|ABOVE_NORMAL_PRIORITY_CLASS,&si)) { - result->res=TestResult::RuntimeError; - result->Details=gcnew String(sec->Details.c_str())+" Too large array"; - } else - { - if (!sec->CheckSecure()) + Marshal::FreeHGlobal(IntPtr(bufW)); + int err=sec->LastError(); + if ((err==193)||(err==1455)) { - result->res=TestResult::InternalError; - result->Details=gcnew String(sec->Details.c_str())+" Can not start process"; + result->res=TestResult::RuntimeError; + result->Details=gcnew String(sec->Details.c_str())+" Too large array"; } else { - result->res=TestResult::DestrictedFunction; - result->Details=gcnew String(sec->Details.c_str())+" Destricted function"; + if (!sec->CheckSecure()) + { + result->res=TestResult::InternalError; + result->Details=gcnew String(sec->Details.c_str())+" Can not start process"; + } else + { + result->res=TestResult::DestrictedFunction; + result->Details=gcnew String(sec->Details.c_str())+" Destricted function"; + } } + return false; } - return false; - } - Marshal::FreeHGlobal(IntPtr(bufW)); + Marshal::FreeHGlobal(IntPtr(bufW)); - job=CreateJobObject(NULL,NULL); - mem.BasicLimitInformation.LimitFlags=JOB_OBJECT_LIMIT_JOB_MEMORY; - mem.JobMemoryLimit=data->MemoryLimit; - SetInformationJobObject(job,JobObjectExtendedLimitInformation,&mem,sizeof(mem)); - port.CompletionKey=(void*)index; - port.CompletionPort=CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0); - SetInformationJobObject(job,JobObjectAssociateCompletionPortInformation,&port,sizeof(port)); - - uilim.UIRestrictionsClass=JOB_OBJECT_UILIMIT_ALL; - SetInformationJobObject(job,JobObjectBasicUIRestrictions,&uilim,sizeof(uilim)); + job=CreateJobObject(NULL,NULL); + mem.BasicLimitInformation.LimitFlags=JOB_OBJECT_LIMIT_JOB_MEMORY; + mem.JobMemoryLimit=data->MemoryLimit; + SetInformationJobObject(job,JobObjectExtendedLimitInformation,&mem,sizeof(mem)); + port.CompletionKey=(void*)index; + port.CompletionPort=CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0); + SetInformationJobObject(job,JobObjectAssociateCompletionPortInformation,&port,sizeof(port)); + + uilim.UIRestrictionsClass=JOB_OBJECT_UILIMIT_ALL; + SetInformationJobObject(job,JobObjectBasicUIRestrictions,&uilim,sizeof(uilim)); - AssignProcessToJobObject(job,sec->process); + AssignProcessToJobObject(job,sec->process); - ResumeThread(sec->thread); + ResumeThread(sec->thread); - DWORD start=GetTickCount(); - while (true) - { + DWORD start=GetTickCount(); + while (true) + { + if (plug->p->OutputLimit((int)GetFileSize(si.hStdOutput,NULL),data->OutputLimit,result->Details)) + { + TerminateJobObject(job,0); + result->res=TestResult::OutputLimit; + break; + } + QueryInformationJobObject(job,JobObjectBasicAccountingInformation,&acc,sizeof(acc),NULL); + if (plug->p->RealTimeLimit((int)(GetTickCount()-start),data->RealTimeLimit,result->Details)) + { + TerminateJobObject(job,0); + result->res=TestResult::RealTimeLimit; + break; + } + if (plug->p->TimeLimit((int)((acc.TotalUserTime.QuadPart)/10000),data->TimeLimit,result->Details)) + { + TerminateJobObject(job,0); + result->res=TestResult::TimeLimit; + break; + } + + unsigned long len,key; + HANDLE proc; + if (GetQueuedCompletionStatus(port.CompletionPort, &len, &key, (LPOVERLAPPED*)&proc, 1)&&(key==index)) + { + DWORD code=0; + bool needbreak=false; + switch(len) + { + case JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS: + GetExitCodeProcess(sec->process,&code); + if (code==5) + { + result->res=TestResult::DestrictedFunction; + result->Details="Access denied."; + } else + if (code!=0) + { + result->res=TestResult::RuntimeError; + result->Details="Runtime error #"+code.ToString(); + } + needbreak=true; + break; + case JOB_OBJECT_MSG_JOB_MEMORY_LIMIT: + result->res=TestResult::MemoryLimit; + result->Details="Memory Limit"; + needbreak=true; + break; + case JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO: + GetExitCodeProcess(sec->process,&code); + if (code==5) + { + result->res=TestResult::DestrictedFunction; + result->Details="Access denied."; + } else + if (code!=0) + { + result->res=TestResult::RuntimeError; + result->Details="Runtime error #"+code.ToString(); + } + needbreak=true; + break; + } + if (needbreak) + { + TerminateJobObject(job,0); + break; + } + } + Sleep(1); + } + + result->UsedRealTime=GetTickCount()-start; + QueryInformationJobObject(job,JobObjectExtendedLimitInformation,&mem,sizeof(mem),NULL); + QueryInformationJobObject(job,JobObjectBasicAccountingInformation,&acc,sizeof(acc),NULL); + result->UsedMemory=mem.PeakJobMemoryUsed; + result->UsedTime=(int)((acc.TotalUserTime.QuadPart)/10000); + + if (result->res!=TestResult::NotTested) + { + result->Details=gcnew String(sec->Details.c_str())+result->Details; + return false; + } + if (plug->p->OutputLimit((int)GetFileSize(si.hStdOutput,NULL),data->OutputLimit,result->Details)) { - TerminateJobObject(job,0); + result->Details=gcnew String(sec->Details.c_str())+result->Details; result->res=TestResult::OutputLimit; - break; + return false; } - QueryInformationJobObject(job,JobObjectBasicAccountingInformation,&acc,sizeof(acc),NULL); - if (plug->p->RealTimeLimit((int)(GetTickCount()-start),data->RealTimeLimit,result->Details)) + + if (plug->p->TimeLimit(result->UsedTime,data->TimeLimit,result->Details)) { - TerminateJobObject(job,0); + result->Details=gcnew String(sec->Details.c_str())+result->Details; + result->res=TestResult::TimeLimit; + return false; + } + + if (plug->p->RealTimeLimit(result->UsedRealTime,data->RealTimeLimit,result->Details)) + { + result->Details=gcnew String(sec->Details.c_str())+result->Details; result->res=TestResult::RealTimeLimit; - break; + return false; } - if (plug->p->TimeLimit((int)((acc.TotalUserTime.QuadPart)/10000),data->TimeLimit,result->Details)) + + if (plug->p->MemoryLimit(result->UsedMemory,data->MemoryLimit,result->Details)) { - TerminateJobObject(job,0); - result->res=TestResult::TimeLimit; - break; + result->Details=gcnew String(sec->Details.c_str())+result->Details; + result->res=TestResult::MemoryLimit; + return false; } - unsigned long len,key; - HANDLE proc; - if (GetQueuedCompletionStatus(port.CompletionPort, &len, &key, (LPOVERLAPPED*)&proc, 1)&&(key==index)) + CloseHandle(si.hStdOutput);si.hStdOutput=NULL; + CloseHandle(si.hStdInput);si.hStdInput=NULL; + + for (int i=0;i<CONST_SLEEP;i++) { - DWORD code=0; - bool needbreak=false; - switch(len) + if (!File::Exists(oupath)) { - case JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS: - GetExitCodeProcess(sec->process,&code); - if (code==5) - { - result->res=TestResult::DestrictedFunction; - result->Details="Access denied."; - } else - if (code!=0) - { - result->res=TestResult::RuntimeError; - result->Details="Runtime error #"+code.ToString(); - } - needbreak=true; - break; - case JOB_OBJECT_MSG_JOB_MEMORY_LIMIT: - result->res=TestResult::MemoryLimit; - result->Details="Memory Limit"; - needbreak=true; - break; - case JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO: - GetExitCodeProcess(sec->process,&code); - if (code==5) - { - result->res=TestResult::DestrictedFunction; - result->Details="Access denied."; - } else - if (code!=0) - { - result->res=TestResult::RuntimeError; - result->Details="Runtime error #"+code.ToString(); - } - needbreak=true; - break; - } - if (needbreak) + Sleep(5); + } else { - TerminateJobObject(job,0); break; } } - Sleep(1); - } - result->UsedRealTime=GetTickCount()-start; - QueryInformationJobObject(job,JobObjectExtendedLimitInformation,&mem,sizeof(mem),NULL); - QueryInformationJobObject(job,JobObjectBasicAccountingInformation,&acc,sizeof(acc),NULL); - result->UsedMemory=mem.PeakJobMemoryUsed; - result->UsedTime=(int)((acc.TotalUserTime.QuadPart)/10000); + if (!File::Exists(oupath)) + { + result->res=TestResult::WrongAnswer; + result->Details=gcnew String(sec->Details.c_str())+" No output file"; + return false; + } - if (result->res!=TestResult::NotTested) - { - result->Details=gcnew String(sec->Details.c_str())+result->Details; - return false; - } + Checker^ check=gcnew Tester::Checker(Path::GetFullPath(data->Checker),test->tests[index-1]->input,ReadAllFile(oupath),test->tests[index-1]->output); - if (plug->p->OutputLimit((int)GetFileSize(si.hStdOutput,NULL),data->OutputLimit,result->Details)) - { + switch (check->Check()) + { + case CheckResult::OE: + result->res=TestResult::OutputError; + result->Details="Output Error"; + break; + case CheckResult::PE: + result->res=TestResult::PresentationError; + result->Details="Presentation Error"; + break; + case CheckResult::WA: + result->res=TestResult::WrongAnswer; + result->Details="Wrong Answer"; + break; + case CheckResult::AC: + result->res=TestResult::Accepted; + result->Details="Accepted"; + break; + }; result->Details=gcnew String(sec->Details.c_str())+result->Details; - result->res=TestResult::OutputLimit; - return false; - } - - if (plug->p->TimeLimit(result->UsedTime,data->TimeLimit,result->Details)) + if (result->res!=TestResult::Accepted) + return false; + return true; + }finally { - result->Details=gcnew String(sec->Details.c_str())+result->Details; - result->res=TestResult::TimeLimit; - return false; + lock(); + results[index-1]=result; + unlock(); + if (sec!=NULL) + { + sec->Finalize(); + delete sec; + } + if (si.hStdInput!=NULL) + CloseHandle(si.hStdInput); + if (si.hStdOutput!=NULL) + CloseHandle(si.hStdOutput); + if (si.hStdError!=NULL) + CloseHandle(si.hStdError); + if (port.CompletionPort!=NULL) + CloseHandle(port.CompletionPort); + if (job!=NULL) + CloseHandle(job); } - - if (plug->p->RealTimeLimit(result->UsedRealTime,data->RealTimeLimit,result->Details)) - { - result->Details=gcnew String(sec->Details.c_str())+result->Details; - result->res=TestResult::RealTimeLimit; - return false; - } - - if (plug->p->MemoryLimit(result->UsedMemory,data->MemoryLimit,result->Details)) - { - result->Details=gcnew String(sec->Details.c_str())+result->Details; - result->res=TestResult::MemoryLimit; - return false; - } - - CloseHandle(si.hStdOutput);si.hStdOutput=NULL; - CloseHandle(si.hStdInput);si.hStdInput=NULL; - - for (int i=0;i<CONST_SLEEP;i++) - if (!File::Exists(oupath)) - Sleep(5); else - break; - - if (!File::Exists(oupath)) - { - result->res=TestResult::WrongAnswer; - result->Details=gcnew String(sec->Details.c_str())+" No output file"; - return false; - } - - Checker^ check=gcnew Tester::Checker(Path::GetFullPath(data->Checker),test->tests[index-1]->input,ReadAllFile(oupath),test->tests[index-1]->output); - - switch (check->Check()) - { - case CheckResult::OE: - result->res=TestResult::OutputError; - result->Details="Output Error"; - break; - case CheckResult::PE: - result->res=TestResult::PresentationError; - result->Details="Presentation Error"; - break; - case CheckResult::WA: - result->res=TestResult::WrongAnswer; - result->Details="Wrong Answer"; - break; - case CheckResult::AC: - result->res=TestResult::Accepted; - result->Details="Accepted"; - break; - }; - result->Details=gcnew String(sec->Details.c_str())+result->Details; - if (result->res!=TestResult::Accepted) - return false; - return true; -}finally -{ - lock(); - results[index-1]=result; - unlock(); - if (sec!=NULL) - { - sec->Finalize(); - delete sec; - } - if (si.hStdInput!=NULL) - CloseHandle(si.hStdInput); - if (si.hStdOutput!=NULL) - CloseHandle(si.hStdOutput); - if (si.hStdError!=NULL) - CloseHandle(si.hStdError); - if (port.CompletionPort!=NULL) - CloseHandle(port.CompletionPort); - if (job!=NULL) - CloseHandle(job); } -} void Runner::ThreadProc() { @@ -325,7 +338,9 @@ if (n<=test->tests->Length) { if (!ExecuteTest(n)) + { StopTest=true; + } } else break; } @@ -351,7 +366,9 @@ void Runner::TerminateThreads() { for (int i=0;i<threads->Length;i++) + { threads[i]->Abort(); + } } Runner::~Runner() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |