SystemWin32.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "CrySystem_precompiled.h"
  9. #include "System.h"
  10. #include <time.h>
  11. #include <IRenderer.h>
  12. #include <IMovieSystem.h>
  13. #include <ILog.h>
  14. #include <AzCore/Debug/StackTracer.h>
  15. #include <AzCore/IO/SystemFile.h> // for AZ_MAX_PATH_LEN
  16. #include <AzCore/std/allocator_stack.h>
  17. #if defined(AZ_RESTRICTED_PLATFORM)
  18. #undef AZ_RESTRICTED_SECTION
  19. #define SYSTEMWIN32_CPP_SECTION_1 1
  20. #define SYSTEMWIN32_CPP_SECTION_2 2
  21. #define SYSTEMWIN32_CPP_SECTION_3 3
  22. #endif
  23. #if defined(LINUX) || defined(APPLE)
  24. #include <unistd.h>
  25. #endif
  26. #ifdef WIN32
  27. #define WIN32_LEAN_AND_MEAN
  28. #include "windows.h"
  29. #include <float.h>
  30. #include <shellapi.h> // Needed for ShellExecute.
  31. #include <Psapi.h>
  32. #include <Aclapi.h>
  33. #include <shlobj.h>
  34. #endif
  35. #include "IDebugCallStack.h"
  36. #if defined(APPLE) || defined(LINUX)
  37. #include <pwd.h>
  38. #endif
  39. #include "XConsole.h"
  40. #include "LocalizedStringManager.h"
  41. #include "XML/XmlUtils.h"
  42. #if defined(WIN32)
  43. __pragma(comment(lib, "wininet.lib"))
  44. __pragma(comment(lib, "Winmm.lib"))
  45. #endif
  46. #if defined(APPLE)
  47. #include <AzCore/Utils/SystemUtilsApple_Platform.h>
  48. #endif
  49. // this is the list of modules that can be loaded into the game process
  50. // Each array element contains 2 strings: the name of the module (case-insensitive)
  51. // and the name of the group the module belongs to
  52. //////////////////////////////////////////////////////////////////////////
  53. const char g_szGroupCore[] = "CryEngine";
  54. const char* g_szModuleGroups[][2] = {
  55. {"Editor.exe", g_szGroupCore},
  56. {"CrySystem.dll", g_szGroupCore}
  57. };
  58. #if defined(WIN32)
  59. #pragma pack(push,1)
  60. struct PEHeader_DLL
  61. {
  62. DWORD signature;
  63. IMAGE_FILE_HEADER _head;
  64. IMAGE_OPTIONAL_HEADER opt_head;
  65. IMAGE_SECTION_HEADER* section_header; // actual number in NumberOfSections
  66. };
  67. #pragma pack(pop)
  68. #endif
  69. //////////////////////////////////////////////////////////////////////////
  70. const char* CSystem::GetUserName()
  71. {
  72. #if defined(WIN32) || defined(WIN64)
  73. static const int iNameBufferSize = 1024;
  74. static char szNameBuffer[iNameBufferSize];
  75. memset(szNameBuffer, 0, iNameBufferSize);
  76. DWORD dwSize = iNameBufferSize;
  77. wchar_t nameW[iNameBufferSize];
  78. ::GetUserNameW(nameW, &dwSize);
  79. AZStd::to_string(szNameBuffer, iNameBufferSize, { nameW, dwSize });
  80. return szNameBuffer;
  81. #else
  82. #if defined(LINUX)
  83. static uid_t uid = geteuid ();
  84. static struct passwd* pw = getpwuid (uid);
  85. if (pw)
  86. {
  87. return (pw->pw_name);
  88. }
  89. else
  90. {
  91. return NULL;
  92. }
  93. #elif defined(APPLE)
  94. static const int iNameBufferSize = 1024;
  95. static char szNameBuffer[iNameBufferSize];
  96. if (auto resultOutcome = AZ::SystemUtilsApple::GetUserName(AZStd::span(szNameBuffer));
  97. resultOutcome)
  98. {
  99. return szNameBuffer;
  100. }
  101. else
  102. {
  103. return "";
  104. }
  105. #else
  106. return "";
  107. #endif
  108. #endif
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. int CSystem::GetApplicationInstance()
  112. {
  113. #ifdef WIN32
  114. // tools that declare themselves as in "tool mode" may not access @user@ and may also not lock it
  115. if (gEnv->IsInToolMode())
  116. {
  117. return 0;
  118. }
  119. // this code below essentially "locks" an instance of the USER folder to a specific running application
  120. if (m_iApplicationInstance == -1)
  121. {
  122. AZStd::wstring suffix;
  123. for (int instance = 0;; ++instance)
  124. {
  125. suffix = AZStd::wstring::format(L"O3DEApplication(%d)", instance);
  126. CreateMutexW(NULL, TRUE, suffix.c_str());
  127. // search for duplicates
  128. if (GetLastError() != ERROR_ALREADY_EXISTS)
  129. {
  130. m_iApplicationInstance = instance;
  131. break;
  132. }
  133. }
  134. }
  135. return m_iApplicationInstance;
  136. #else
  137. return 0;
  138. #endif
  139. }
  140. int CSystem::GetApplicationLogInstance([[maybe_unused]] const char* logFilePath)
  141. {
  142. #if AZ_TRAIT_OS_USE_WINDOWS_MUTEX
  143. AZStd::wstring suffix;
  144. int instance = 0;
  145. for (;; ++instance)
  146. {
  147. suffix = AZStd::wstring::format(L"%s(%d)", logFilePath, instance);
  148. CreateMutexW(NULL, TRUE, suffix.c_str());
  149. if (GetLastError() != ERROR_ALREADY_EXISTS)
  150. {
  151. break;
  152. }
  153. }
  154. return instance;
  155. #else
  156. return 0;
  157. #endif
  158. }
  159. //////////////////////////////////////////////////////////////////////////
  160. struct CryDbgModule
  161. {
  162. HANDLE heap;
  163. WIN_HMODULE handle;
  164. AZStd::string name;
  165. DWORD dwSize;
  166. };
  167. #ifdef WIN32
  168. //////////////////////////////////////////////////////////////////////////
  169. const char* GetModuleGroup (const char* szString)
  170. {
  171. for (unsigned i = 0; i < sizeof(g_szModuleGroups) / sizeof(g_szModuleGroups[0]); ++i)
  172. {
  173. if (azstricmp(szString, g_szModuleGroups[i][0]) == 0)
  174. {
  175. return g_szModuleGroups[i][1];
  176. }
  177. }
  178. return "Other";
  179. }
  180. #endif
  181. // Make system error message string
  182. //////////////////////////////////////////////////////////////////////////
  183. //! \return pointer to the null terminated error string or 0
  184. static const char* GetLastSystemErrorMessage()
  185. {
  186. #ifdef WIN32
  187. DWORD dwError = GetLastError();
  188. static char szBuffer[512]; // function will return pointer to this buffer
  189. if (dwError)
  190. {
  191. LPVOID lpMsgBuf = 0;
  192. if (FormatMessage(
  193. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  194. FORMAT_MESSAGE_FROM_SYSTEM |
  195. FORMAT_MESSAGE_IGNORE_INSERTS,
  196. NULL,
  197. GetLastError(),
  198. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  199. (LPTSTR) &lpMsgBuf,
  200. 0,
  201. NULL))
  202. {
  203. azstrcpy(szBuffer, AZ_ARRAY_SIZE(szBuffer), (char*)lpMsgBuf);
  204. LocalFree(lpMsgBuf);
  205. }
  206. else
  207. {
  208. return 0;
  209. }
  210. return szBuffer;
  211. }
  212. #endif //WIN32
  213. return 0;
  214. }
  215. //////////////////////////////////////////////////////////////////////////
  216. void CSystem::FatalError(const char* format, ...)
  217. {
  218. // Guard against reentrancy - out of memory fatal errors can become reentrant since logging can try to alloc.
  219. static bool currentlyReportingError = false;
  220. if (currentlyReportingError == true)
  221. {
  222. return;
  223. }
  224. currentlyReportingError = true;
  225. // format message
  226. va_list ArgList;
  227. char szBuffer[MAX_WARNING_LENGTH];
  228. const char* sPrefix = "";
  229. azstrcpy(szBuffer, MAX_WARNING_LENGTH, sPrefix);
  230. va_start(ArgList, format);
  231. azvsnprintf(szBuffer + strlen(sPrefix), MAX_WARNING_LENGTH - strlen(sPrefix), format, ArgList);
  232. va_end(ArgList);
  233. // get system error message before any attempt to write into log
  234. const char* szSysErrorMessage = GetLastSystemErrorMessage();
  235. CryLogAlways("=============================================================================");
  236. CryLogAlways("*ERROR");
  237. CryLogAlways("=============================================================================");
  238. // write both messages into log
  239. CryLogAlways("%s", szBuffer);
  240. if (szSysErrorMessage)
  241. {
  242. CryLogAlways("Last System Error: %s", szSysErrorMessage);
  243. }
  244. if (GetUserCallback())
  245. {
  246. GetUserCallback()->OnError(szBuffer);
  247. }
  248. assert(szBuffer[0] >= ' ');
  249. // strcpy(szBuffer,szBuffer+1); // remove verbosity tag since it is not supported by ::MessageBox
  250. AZ::Debug::Platform::OutputToDebugger("CrySystem", szBuffer);
  251. #ifdef WIN32
  252. OnFatalError(szBuffer);
  253. if (!g_cvars.sys_no_crash_dialog)
  254. {
  255. AZStd::wstring szBufferW;
  256. AZStd::to_wstring(szBufferW, szBuffer);
  257. ::MessageBoxW(NULL, szBufferW.c_str(), L"Open 3D Engine Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
  258. }
  259. // Dump callstack.
  260. IDebugCallStack::instance()->FatalError(szBuffer);
  261. #endif
  262. // app can not continue
  263. AZ::Debug::Trace::Instance().Break();
  264. #ifdef _DEBUG
  265. #if defined(WIN32) || defined(WIN64)
  266. _flushall();
  267. // on windows, _exit does all sorts of things which can cause cleanup to fail during a crash, we need to terminate instead.
  268. TerminateProcess(GetCurrentProcess(), 1);
  269. #endif
  270. #if defined(AZ_RESTRICTED_PLATFORM)
  271. #define AZ_RESTRICTED_SECTION SYSTEMWIN32_CPP_SECTION_2
  272. #include AZ_RESTRICTED_FILE(SystemWin32_cpp)
  273. #endif
  274. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  275. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  276. #else
  277. _exit(1);
  278. #endif
  279. #endif
  280. }
  281. void CSystem::ReportBug([[maybe_unused]] const char* format, ...)
  282. {
  283. #if defined (WIN32)
  284. va_list ArgList;
  285. char szBuffer[MAX_WARNING_LENGTH];
  286. const char* sPrefix = "";
  287. azstrcpy(szBuffer, MAX_WARNING_LENGTH, sPrefix);
  288. va_start(ArgList, format);
  289. azvsnprintf(szBuffer + strlen(sPrefix), MAX_WARNING_LENGTH - strlen(sPrefix), format, ArgList);
  290. va_end(ArgList);
  291. IDebugCallStack::instance()->ReportBug(szBuffer);
  292. #endif
  293. }
  294. //////////////////////////////////////////////////////////////////////////
  295. void CSystem::debug_GetCallStack(const char** pFunctions, int& nCount)
  296. {
  297. #if defined(WIN32)
  298. using namespace AZ::Debug;
  299. int nMaxCount = nCount;
  300. StackFrame* frames = (StackFrame*)AZ_ALLOCA(sizeof(StackFrame)*nMaxCount);
  301. unsigned int numFrames = StackRecorder::Record(frames, nMaxCount, 1);
  302. SymbolStorage::StackLine* textLines = (SymbolStorage::StackLine*)AZ_ALLOCA(sizeof(SymbolStorage::StackLine)*nMaxCount);
  303. SymbolStorage::DecodeFrames(frames, numFrames, textLines);
  304. for (unsigned int i = 0; i < numFrames; i++)
  305. {
  306. pFunctions[i] = textLines[i];
  307. }
  308. nCount = numFrames;
  309. #define AZ_RESTRICTED_SECTION_IMPLEMENTED
  310. #elif defined(AZ_RESTRICTED_PLATFORM)
  311. #define AZ_RESTRICTED_SECTION SYSTEMWIN32_CPP_SECTION_3
  312. #include AZ_RESTRICTED_FILE(SystemWin32_cpp)
  313. #endif
  314. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  315. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  316. #else
  317. AZ_UNUSED(pFunctions);
  318. nCount = 0;
  319. #endif
  320. }
  321. //////////////////////////////////////////////////////////////////////////
  322. void CSystem::debug_LogCallStack(int nMaxFuncs, [[maybe_unused]] int nFlags)
  323. {
  324. if (nMaxFuncs > 32)
  325. {
  326. nMaxFuncs = 32;
  327. }
  328. // Print call stack for each find.
  329. const char* funcs[32];
  330. int nCount = nMaxFuncs;
  331. GetISystem()->debug_GetCallStack(funcs, nCount);
  332. for (int i = 1; i < nCount; i++) // start from 1 to skip this function.
  333. {
  334. CryLogAlways(" %02d) %s", i, funcs[i]);
  335. }
  336. }
  337. #if (defined(WIN32) || defined(WIN64))
  338. //////////////////////////////////////////////////////////////////////////
  339. bool CSystem::GetWinGameFolder(char* szMyDocumentsPath, int maxPathSize)
  340. {
  341. bool bSucceeded = false;
  342. // check Vista and later OS first
  343. HMODULE shell32 = LoadLibraryW(L"Shell32.dll");
  344. if (shell32)
  345. {
  346. typedef long (__stdcall * T_SHGetKnownFolderPath)(REFKNOWNFOLDERID rfid, unsigned long dwFlags, void* hToken, wchar_t** ppszPath);
  347. T_SHGetKnownFolderPath SHGetKnownFolderPath = (T_SHGetKnownFolderPath)GetProcAddress(shell32, "SHGetKnownFolderPath");
  348. if (SHGetKnownFolderPath)
  349. {
  350. // We must be running Vista or newer
  351. wchar_t* wMyDocumentsPath;
  352. HRESULT hr = SHGetKnownFolderPath(FOLDERID_SavedGames, KF_FLAG_CREATE | KF_FLAG_DONT_UNEXPAND, NULL, &wMyDocumentsPath);
  353. bSucceeded = SUCCEEDED(hr);
  354. if (bSucceeded)
  355. {
  356. // Convert from UNICODE to UTF-8
  357. AZStd::to_string(szMyDocumentsPath, maxPathSize, wMyDocumentsPath);
  358. CoTaskMemFree(wMyDocumentsPath);
  359. }
  360. }
  361. FreeLibrary(shell32);
  362. }
  363. if (!bSucceeded)
  364. {
  365. // check pre-vista OS if not succeeded before
  366. wchar_t wMyDocumentsPath[AZ_MAX_PATH_LEN];
  367. bSucceeded = SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, wMyDocumentsPath));
  368. if (bSucceeded)
  369. {
  370. AZStd::to_string(szMyDocumentsPath, maxPathSize, wMyDocumentsPath);
  371. }
  372. }
  373. return bSucceeded;
  374. }
  375. #endif
  376. //////////////////////////////////////////////////////////////////////////
  377. void CSystem::DetectGameFolderAccessRights()
  378. {
  379. // This code is trying to figure out if the current folder we are now running under have write access.
  380. // By default assume folder is not writable.
  381. // If folder is writable game.log is saved there, otherwise it is saved in user documents folder.
  382. #if defined(WIN32)
  383. DWORD DesiredAccess = FILE_GENERIC_WRITE;
  384. DWORD GrantedAccess = 0;
  385. DWORD dwRes = 0;
  386. PACL pDACL = NULL;
  387. PSECURITY_DESCRIPTOR pSD = NULL;
  388. HANDLE hClientToken = 0;
  389. PRIVILEGE_SET PrivilegeSet;
  390. DWORD PrivilegeSetLength = sizeof(PrivilegeSet);
  391. BOOL bAccessStatus = FALSE;
  392. // Get a pointer to the existing DACL.
  393. dwRes = GetNamedSecurityInfoW(L".", SE_FILE_OBJECT,
  394. DACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION,
  395. NULL, NULL, &pDACL, NULL, &pSD);
  396. if (ERROR_SUCCESS != dwRes)
  397. {
  398. //
  399. assert(0);
  400. }
  401. if (!ImpersonateSelf(SecurityIdentification))
  402. {
  403. return;
  404. }
  405. if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hClientToken) && hClientToken != 0)
  406. {
  407. return;
  408. }
  409. GENERIC_MAPPING GenMap;
  410. GenMap.GenericRead = FILE_GENERIC_READ;
  411. GenMap.GenericWrite = FILE_GENERIC_WRITE;
  412. GenMap.GenericExecute = FILE_GENERIC_EXECUTE;
  413. GenMap.GenericAll = FILE_ALL_ACCESS;
  414. MapGenericMask(&DesiredAccess, &GenMap);
  415. if (!AccessCheck(pSD, hClientToken, DesiredAccess, &GenMap, &PrivilegeSet, &PrivilegeSetLength, &GrantedAccess, &bAccessStatus))
  416. {
  417. RevertToSelf();
  418. CloseHandle(hClientToken);
  419. return;
  420. }
  421. CloseHandle(hClientToken);
  422. RevertToSelf();
  423. if (bAccessStatus)
  424. {
  425. m_bGameFolderWritable = true;
  426. }
  427. #elif defined(MOBILE)
  428. char cwd[AZ_MAX_PATH_LEN];
  429. if (getcwd(cwd, AZ_MAX_PATH_LEN) != NULL)
  430. {
  431. if (0 == access(cwd, W_OK))
  432. {
  433. m_bGameFolderWritable = true;
  434. }
  435. }
  436. #endif //WIN32
  437. }
  438. /////////////////////////////////`/////////////////////////////////////////
  439. void CSystem::EnableFloatExceptions([[maybe_unused]] int type)
  440. {
  441. #ifndef _RELEASE
  442. #if defined(WIN32)
  443. #if defined(WIN32) && !defined(WIN64)
  444. // Optimization
  445. // Enable DAZ/FZ
  446. // Denormals Are Zeros
  447. // Flush-to-Zero
  448. _controlfp(_DN_FLUSH, _MCW_DN);
  449. #endif //#if defined(WIN32) && !defined(WIN64)
  450. AZ_PUSH_DISABLE_WARNING(4996, "-Wunknown-warning-option")
  451. _controlfp(_DN_FLUSH, _MCW_DN);
  452. if (type == 0)
  453. {
  454. // mask all floating exceptions off.
  455. _controlfp(_EM_INEXACT | _EM_UNDERFLOW | _EM_OVERFLOW | _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE, _MCW_EM);
  456. }
  457. else
  458. {
  459. // Clear pending exceptions
  460. _fpreset();
  461. if (type == 1)
  462. {
  463. // enable just the most important fp-exceptions.
  464. _controlfp(_EM_INEXACT | _EM_UNDERFLOW | _EM_OVERFLOW, _MCW_EM); // Enable floating point exceptions.
  465. }
  466. if (type == 2)
  467. {
  468. // enable ALL floating point exceptions.
  469. _controlfp(_EM_INEXACT, _MCW_EM);
  470. }
  471. }
  472. AZ_POP_DISABLE_WARNING
  473. #endif //#if defined(WIN32) && !defined(WIN64)
  474. #ifdef WIN32
  475. _mm_setcsr(_mm_getcsr() & ~0x280 | (type > 0 ? 0 : 0x280));
  476. #endif
  477. #endif //_RELEASE
  478. }