void memobj_init(void) { unsigned long min_bytes, max_bytes; unsigned long mem_bytes = GetPhysicalPages() * 0.90 * TILT_PAGESIZE; unsigned long cache_bytes = GetBcacheSize() * 2; min_bytes = 2048 * 1024; min_bytes = Max(min_bytes, cache_bytes); max_bytes = (unsigned long)INT_MAX; max_bytes = Min(max_bytes, mem_bytes); max_bytes = Min(max_bytes, rlimit(RLIMIT_DATA)); max_bytes = Min(max_bytes, rlimit(RLIMIT_AS)); #ifdef RLIMIT_VMEM max_bytes = Min(max_bytes, rlimit(RLIMIT_VMEM)); #endif init_int(&MinHeapByte, min_bytes); init_int(&MaxHeapByte, 0.40 * max_bytes); assert(MinHeapByte <= MaxHeapByte); #ifdef sparc assert(TILT_PAGESIZE == sysconf(_SC_PAGESIZE)); #else assert(TILT_PAGESIZE == sysconf(_SC_PAGE_SIZE)); #endif StackInitialize(); HeapInitialize(); GuardStackletSize = TILT_PAGESIZE / kilobyte; stackletOffset = (GuardStackletSize + MLStackletSize + CStackletSize) * kilobyte; primaryStackletOffset = 0; replicaStackletOffset = stackletOffset; /* So we don't pay mmap for first thread - general case? XXXX */ { int i; Stacklet_t *temp[5]; for (i=0; i<5; i++) temp[i] = Stacklet_Alloc(NULL); for (i=0; i<5; i++) Stacklet_Dealloc(temp[i]); } }
BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch(ul_reason_for_call) { case DLL_PROCESS_ATTACH: { ghPluginModule = (HMODULE)hModule; //ghWorkingModule = (u64)hModule; HeapInitialize(); #ifdef SHOW_STARTED_MSGBOX if (!IsDebuggerPresent()) MessageBoxA(NULL, "ConEmuLn*.dll loaded", "ConEmuLn plugin", 0); #endif bool lbExportsChanged = false; if (LoadFarVersion()) { if (gFarVersion.dwVerMajor == 3) { lbExportsChanged = ChangeExports( Far3Func, ghPluginModule ); if (!lbExportsChanged) { _ASSERTE(lbExportsChanged); } } } } break; case DLL_PROCESS_DETACH: HeapDeinitialize(); break; } return TRUE; }
bool ProcessCommandLine(int& iRc, HMODULE& hConEmu) { LPCWSTR pszCmdLine = GetCommandLineW(); // If there is '-new_console' or '-cur_console' switches... if (IsNewConsoleArg(pszCmdLine) || IsNewConsoleArg(pszCmdLine, L"-cur_console")) return false; HeapInitialize(); bool bProcessed = false; // Loop through switches to find supported { CEStr lsArg; int iCount = 0; bool bHelpRequested = false; bool bFirst = true; while (NextArg(&pszCmdLine, lsArg) == 0) { if ((lsArg.ms_Val[0] == L'-') && lsArg.ms_Val[1] && !wcspbrk(lsArg.ms_Val+1, L"\\//|.&<>^")) { // Seems this is to be the "switch" too lsArg.ms_Val[0] = L'/'; } bool bWasFirst = bFirst; bFirst = false; if ((lsArg.ms_Val[0] != L'/') && bWasFirst) { LPCWSTR pszName = PointToName(lsArg.ms_Val); if (pszName && (lstrcmpi(pszName, WIN3264TEST(L"ConEmuC.exe",L"ConEmuC64.exe")) == 0)) continue; } iCount++; if ((lsArg.ms_Val[0] != L'/') && (iCount > 1)) { // Some unknown (here) switch, goto full version break; } if (lstrcmpi(lsArg, L"/Download") == 0) { iRc = DoDownload(pszCmdLine); // Return '0' on download success for compatibility if (iRc == CERR_DOWNLOAD_SUCCEEDED) iRc = 0; bProcessed = true; break; } if ((lstrcmpi(lsArg, L"/Args") == 0) || (lstrcmpi(lsArg, L"/ParseArgs") == 0)) { iRc = DoParseArgs(pszCmdLine); bProcessed = true; break; } if ((lstrcmpi(lsArg, L"/?") == 0) || (lstrcmpi(lsArg, L"/h") == 0) || (lstrcmpi(lsArg, L"/help") == 0) || (lstrcmpi(lsArg, L"/-help") == 0) ) { bHelpRequested = true; break; }; // ToDo: /IsConEmu may be processed partially? // TODO: Inject remote and standard, DefTerm } if (bHelpRequested || (iCount == 0)) { if (!hConEmu) { // Prefere Help from ConEmuCD.dll because ConEmuC.exe may be outdated (due to stability preference) hConEmu = LoadLibrary(WIN3264TEST(L"ConEmuCD.dll",L"ConEmuCD64.dll")); // Show internal Help variant if only ConEmuCD.dll was failed to load if (hConEmu == NULL) { Help(); iRc = CERR_HELPREQUESTED; bProcessed = true; } } } } HeapDeinitialize(); return bProcessed; }
BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { BOOL lbAllow = TRUE; switch(ul_reason_for_call) { case DLL_PROCESS_ATTACH: { gnDllState = ds_DllProcessAttach; #ifdef _DEBUG HANDLE hProcHeap = GetProcessHeap(); #endif HeapInitialize(); ghOurModule = (HMODULE)hModule; ghConWnd = GetConsoleWindow(); if (ghConWnd) GetConsoleTitle(gsInitConTitle, countof(gsInitConTitle)); gnSelfPID = GetCurrentProcessId(); ghWorkingModule = (u64)hModule; gfGetRealConsoleWindow = GetConsoleWindow; user = (UserImp*)calloc(1, sizeof(*user)); GetMainThreadId(); // Инициализировать gnHookMainThreadId gcchLastWriteConsoleMax = 4096; gpszLastWriteConsole = (wchar_t*)calloc(gcchLastWriteConsoleMax,sizeof(*gpszLastWriteConsole)); gInQueue.Initialize(512, NULL); #ifdef _DEBUG gAllowAssertThread = am_Pipe; #endif #ifdef _DEBUG #ifdef UseDebugExceptionFilter gfnPrevFilter = SetUnhandledExceptionFilter(HkExceptionFilter); #endif #endif #ifdef SHOW_STARTED_MSGBOX if (!IsDebuggerPresent()) { ::MessageBox(ghConEmuWnd, L"ConEmuHk*.dll loaded", L"ConEmu hooks", MB_SYSTEMMODAL); } #endif #ifdef _DEBUG DWORD dwConMode = -1; GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConMode); #endif //_ASSERTE(ghHeap == NULL); //ghHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 200000, 0); wchar_t szEvtName[64]; msprintf(szEvtName, countof(szEvtName), CECONEMUROOTPROCESS, gnSelfPID); HANDLE hRootProcessFlag = OpenEvent(SYNCHRONIZE|EVENT_MODIFY_STATE, FALSE, szEvtName); DWORD nWaitRoot = -1; if (hRootProcessFlag) { nWaitRoot = WaitForSingleObject(hRootProcessFlag, 0); gbSelfIsRootConsoleProcess = (nWaitRoot == WAIT_OBJECT_0); } SafeCloseHandle(hRootProcessFlag); #ifdef HOOK_USE_DLLTHREAD _ASSERTEX(FALSE && "Hooks starting in background thread?"); //HANDLE hEvents[2]; //hEvents[0] = CreateEvent(NULL, FALSE, FALSE, NULL); //hEvents[1] = ghStartThread = CreateThread(NULL, 0, DllStart, NULL/*(LPVOID)(hEvents[0])*/, 0, &gnStartThreadID); if (ghStartThread == NULL) { //_ASSERTE(ghStartThread!=NULL); wchar_t szMsg[128]; DWORD nErrCode = GetLastError(); msprintf(szMsg, countof(szMsg), L"Failed to start DllStart thread!\nErrCode=0x%08X\nPID=%u", nErrCode, GetCurrentProcessId()); GuiMessageBox(ghConEmuWnd, szMsg, L"ConEmu hooks", 0); } else { DWORD nThreadWait = WaitForSingleObject(ghStartThread, 5000); DllThreadClose(); } //DWORD nThreadWait = WaitForMultipleObjects(hEvents, countof(hEvents), FALSE, INFINITE); //CloseHandle(hEvents[0]); #else DllStart(NULL); #endif user->setAllowLoadLibrary(); } break; case DLL_THREAD_ATTACH: { gnDllThreadCount++; if (gbHooksWasSet) InitHooksRegThread(); } break; case DLL_THREAD_DETACH: { #ifdef SHOW_SHUTDOWN_STEPS gnDbgPresent = 0; ShutdownStep(L"DLL_THREAD_DETACH"); #endif if (gbHooksWasSet) DoneHooksRegThread(); // DLL_PROCESS_DETACH зовется как выяснилось не всегда if (gnHookMainThreadId && (GetCurrentThreadId() == gnHookMainThreadId) && !gbDllDeinitialized) { gbDllDeinitialized = true; //WARNING!!! OutputDebugString must NOT be used from ConEmuHk::DllMain(DLL_PROCESS_DETACH). See Issue 465 DllStop(); } gnDllThreadCount--; ShutdownStep(L"DLL_THREAD_DETACH done, left=%i", gnDllThreadCount); } break; case DLL_PROCESS_DETACH: { ShutdownStep(L"DLL_PROCESS_DETACH"); gnDllState = ds_DllProcessDetach; if (gbHooksWasSet) lbAllow = FALSE; // Иначе свалимся, т.к. FreeLibrary перехвачена // Уже могли дернуть в DLL_THREAD_DETACH if (!gbDllDeinitialized) { gbDllDeinitialized = true; //WARNING!!! OutputDebugString must NOT be used from ConEmuHk::DllMain(DLL_PROCESS_DETACH). See Issue 465 DllStop(); } // -- free не нужен, т.к. уже вызван HeapDeinitialize() //free(user); ShutdownStep(L"DLL_PROCESS_DETACH done"); } break; } return lbAllow; }
int main(int argc, char** argv) { HeapInitialize(); for (int i=0; i<argc; i++) { if (strcmp(argv[i], "-verbose")==0 || strcmp(argv[i], "--verbose")==0) gbVerifyVerbose = true; } // Tests { Verify_Step("ls1(`Test`)"); CEStr ls1(L"Test"); Verify0((ls1.ms_Val && 0==wcscmp(ls1.ms_Val,L"Test")),"ls1==`Test`"); Verify_Step("ls12 = ls1.Detach()"); /* Store ptr for Verify test result */ LPCWSTR pszPtr = ls1.ms_Val; CEStr ls2 = ls1.Detach(); Verify2((ls2.ms_Val && !ls1.ms_Val && ls2.ms_Val==pszPtr),"ls2.ms_Val{x%p}==pszPtr{x%p}",ls2.ms_Val,pszPtr); } { Verify_Step("ls3 = `Test3`"); CEStr ls3 = L"Test3"; Verify0((ls3.ms_Val && 0==wcscmp(ls3.ms_Val,L"Test3")),"ls3==`Test3`"); Verify_Step("ls4 = (LPCWSTR)ls3.ms_Val"); CEStr ls4 = static_cast<LPCWSTR>(ls3.ms_Val); Verify2((ls4.ms_Val && ls4.ms_Val != ls3.ms_Val),"ls4.ms_Val{x%p}!=ls3.ms_Val{x%p}",ls4.ms_Val,ls3.ms_Val); Verify_Step("ls5 = lstrdup(ls3)"); CEStr ls5 = lstrdup(ls3); Verify0((ls5.ms_Val && 0==wcscmp(ls5.ms_Val,L"Test3")),"ls5==`Test3`"); Verify_Step("ls6(lstrdup(ls3))"); CEStr ls6(lstrdup(ls3)); Verify0((ls6.ms_Val && 0==wcscmp(ls6.ms_Val,L"Test3")),"ls6==`Test3`"); } { Verify_Step("-new_console parser tests"); LPCWSTR pszTest = L"-new_console:a \\\"-new_console:c\\\" `-new_console:d:C:\\` -cur_console:b"; LPCWSTR pszCmp = L"\\\"-new_console:c\\\" `-new_console:d:C:\\`"; RConStartArgs arg; arg.pszSpecialCmd = lstrdup(pszTest); arg.ProcessNewConArg(); int iCmp = lstrcmp(arg.pszSpecialCmd, pszCmp); Verify0((iCmp==0),"arg.pszSpecialCmd==\\\"-new_console:c\\\" `-new_console:d:C:\\`"); Verify_Step("RConStartArgs::RunArgTests()"); RConStartArgs::RunArgTests(); Verify0(!gbVerifyFailed,"RConStartArgs tests passed"); } { Verify_Step("msprintf tests"); wchar_t szBuffer[200]; msprintf(szBuffer, countof(szBuffer), L"%u %03u %03u %i %x %02X %02X %04x %08X", 123, 98, 4567, -234, 0x12AB, 0x0A, 0xABC, 0x01A0, 0x0765ABCD); const wchar_t szStd[] = L"123 098 4567 -234 12ab 0A ABC 01a0 0765ABCD"; int iCmp = lstrcmp(szBuffer, szStd); WVerify2((iCmp==0),L"`%s` (msprintf[W])\n `%s` (standard)", szBuffer, szStd); char szBufA[200]; msprintf(szBufA, countof(szBufA), "%u %i %x %02X %02X %04x %08X", 123, -234, 0x12AB, 0x0A, 0xABC, 0x01A0, 0x0765ABCD); const char szStdA[] = "123 -234 12ab 0A ABC 01a0 0765ABCD"; iCmp = lstrcmpA(szBufA, szStdA); Verify2((iCmp==0),"`%s` (msprintf[A])\n `%s` (standard)", szBufA, szStdA); } if (gbVerifyFailed) Verify_MsgFail("Some tests failed!"); else Verify_MsgOk("All done"); return gbVerifyFailed ? 99 : 0; }
int main(int argc, char** argv) { HeapInitialize(); for (int i=0; i<argc; i++) { if (strcmp(argv[i], "-verbose")==0 || strcmp(argv[i], "--verbose")==0) gbVerifyVerbose = true; } // Tests { Verify_Step("ls1(`Test`)"); CEStr ls1(L"Test"); Verify0((ls1.ms_Val && 0==wcscmp(ls1.ms_Val,L"Test")),"ls1==`Test`"); Verify_Step("ls12 = ls1.Detach()"); /* Store ptr for Verify test result */ LPCWSTR pszPtr = ls1.ms_Val; CEStr ls2 = ls1.Detach(); Verify2((ls2.ms_Val && !ls1.ms_Val && ls2.ms_Val==pszPtr),"ls2.ms_Val{x%p}==pszPtr{x%p}",ls2.ms_Val,pszPtr); } { Verify_Step("ls3 = `Test3`"); CEStr ls3 = L"Test3"; Verify0((ls3.ms_Val && 0==wcscmp(ls3.ms_Val,L"Test3")),"ls3==`Test3`"); Verify_Step("ls4 = (LPCWSTR)ls3.ms_Val"); CEStr ls4 = static_cast<LPCWSTR>(ls3.ms_Val); Verify2((ls4.ms_Val && ls4.ms_Val != ls3.ms_Val),"ls4.ms_Val{x%p}!=ls3.ms_Val{x%p}",ls4.ms_Val,ls3.ms_Val); Verify_Step("ls5 = lstrdup(ls3)"); CEStr ls5 = lstrdup(ls3); Verify0((ls5.ms_Val && 0==wcscmp(ls5.ms_Val,L"Test3")),"ls5==`Test3`"); Verify_Step("ls6(lstrdup(ls3))"); CEStr ls6(lstrdup(ls3)); Verify0((ls6.ms_Val && 0==wcscmp(ls6.ms_Val,L"Test3")),"ls6==`Test3`"); } { Verify_Step("NextArg and Switch comparison"); LPCWSTR pszCmd = L"conemu.exe /c/dir -run -inside=0x800 /cmdlist \"-inside=\\eCD /d %1\" -bad|switch "; CEStr ls; Verify0((0==NextArg(&pszCmd,ls)),"NextArg conemu.exe"); Verify0((!ls.IsPossibleSwitch()),"!IsPossibleSwitch()"); Verify0((0==NextArg(&pszCmd,ls)),"NextArg /c/dir"); Verify0((!ls.IsPossibleSwitch()),"!IsPossibleSwitch()"); Verify0((0==NextArg(&pszCmd,ls)),"NextArg -run"); Verify0((ls.OneOfSwitches(L"/cmd",L"/run")),"OneOfSwitches(/cmd,/run)"); Verify0((!ls.OneOfSwitches(L"/cmd",L"/cmdlist")),"!OneOfSwitches(/cmd,/cmdlist)"); Verify0((ls.IsSwitch(L"-run")),"IsSwitch(-run)"); Verify0((0==NextArg(&pszCmd,ls)),"NextArg -inside=0x800"); Verify0((ls.IsSwitch(L"-inside=")),"IsSwitch(-inside=)"); Verify0((ls.OneOfSwitches(L"-inside",L"-inside=")),"OneOfSwitches(-inside,-inside=)"); Verify0((!ls.IsSwitch(L"-inside")),"!IsSwitch(-inside)"); Verify0((0==NextArg(&pszCmd,ls)),"NextArg /cmdlist"); Verify0((ls.IsSwitch(L"-cmdlist")),"IsSwitch(-cmdlist)"); Verify0((0==NextArg(&pszCmd,ls)),"NextArg \"-inside=\\eCD /d %1\""); Verify0((ls.IsSwitch(L"-inside:")),"IsSwitch(-inside=)"); Verify0((0==NextArg(&pszCmd,ls)),"NextArg -bad|switch"); Verify0((ls.Compare(L"-bad|switch")==0),"Compare(-bad|switch)"); Verify0((!ls.IsPossibleSwitch()),"!IsPossibleSwitch"); } { Verify_Step("-new_console parser tests"); LPCWSTR pszTest = L"-new_console:a \\\"-new_console:c\\\" `-new_console:d:C:\\` -cur_console:b"; LPCWSTR pszCmp = L"\\\"-new_console:c\\\" `-new_console:d:C:\\`"; RConStartArgs arg; arg.pszSpecialCmd = lstrdup(pszTest); arg.ProcessNewConArg(); int iCmp = lstrcmp(arg.pszSpecialCmd, pszCmp); Verify0((iCmp==0),"arg.pszSpecialCmd==\\\"-new_console:c\\\" `-new_console:d:C:\\`"); Verify_Step("RConStartArgs::RunArgTests()"); RConStartArgs::RunArgTests(); Verify0(!gbVerifyFailed,"RConStartArgs tests passed"); } { Verify_Step("msprintf tests"); wchar_t szBuffer[200]; msprintf(szBuffer, countof(szBuffer), L"%u %03u %03u %i %x %02X %02X %04x %08X", 123, 98, 4567, -234, 0x12AB, 0x0A, 0xABC, 0x01A0, 0x0765ABCD); const wchar_t szStd[] = L"123 098 4567 -234 12ab 0A ABC 01a0 0765ABCD"; int iCmp = lstrcmp(szBuffer, szStd); WVerify2((iCmp==0),L"`%s` (msprintf[W])\n `%s` (standard)", szBuffer, szStd); char szBufA[200]; msprintf(szBufA, countof(szBufA), "%u %i %x %02X %02X %04x %08X", 123, -234, 0x12AB, 0x0A, 0xABC, 0x01A0, 0x0765ABCD); const char szStdA[] = "123 -234 12ab 0A ABC 01a0 0765ABCD"; iCmp = lstrcmpA(szBufA, szStdA); Verify2((iCmp==0),"`%s` (msprintf[A])\n `%s` (standard)", szBufA, szStdA); } if (gbVerifyFailed) Verify_MsgFail("Some tests failed!"); else Verify_MsgOk("All done"); return gbVerifyFailed ? 99 : 0; }