Changeset 2940 in kBuild for trunk/src/kWorker
- Timestamp:
- Sep 19, 2016 6:34:43 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kWorker/kWorker.c
r2938 r2940 815 815 /** The current directory (referenced). */ 816 816 static PKFSOBJ g_pCurDirObj = NULL; 817 #ifdef KBUILD_OS_WINDOWS 818 /** The windows system32 directory (referenced). */ 819 static PKFSDIR g_pWinSys32 = NULL; 820 #endif 817 821 818 822 /** Verbosity level. */ … … 2114 2118 2115 2119 /** 2120 * Lazily initializes the g_pWinSys32 variable. 2121 */ 2122 static PKFSDIR kwLdrResolveWinSys32(void) 2123 { 2124 KFSLOOKUPERROR enmError; 2125 PKFSDIR pWinSys32; 2126 2127 /* Get the path first. */ 2128 char szSystem32[MAX_PATH]; 2129 if (GetSystemDirectoryA(szSystem32, sizeof(szSystem32)) >= sizeof(szSystem32)) 2130 { 2131 kwErrPrintf("GetSystemDirectory failed: %u\n", GetLastError()); 2132 strcpy(szSystem32, "C:\\Windows\\System32"); 2133 } 2134 2135 /* Look it up and verify it. */ 2136 pWinSys32 = (PKFSDIR)kFsCacheLookupA(g_pFsCache, szSystem32, &enmError); 2137 if (pWinSys32) 2138 { 2139 if (pWinSys32->Obj.bObjType == KFSOBJ_TYPE_DIR) 2140 { 2141 g_pWinSys32 = pWinSys32; 2142 return pWinSys32; 2143 } 2144 2145 kwErrPrintf("System directory '%s' isn't of 'DIR' type: %u\n", szSystem32, g_pWinSys32->Obj.bObjType); 2146 } 2147 else 2148 kwErrPrintf("Failed to lookup system directory '%s': %u\n", szSystem32, enmError); 2149 return NULL; 2150 } 2151 2152 2153 /** 2116 2154 * Whether we can load this DLL natively or not. 2117 2155 * … … 2119 2157 * @param pszFilename The filename (no path). 2120 2158 * @param enmLocation The location. 2121 */ 2122 static KBOOL kwLdrModuleCanLoadNatively(const char *pszFilename, KWLOCATION enmLocation) 2159 * @param pszFullPath The full filename and path. 2160 */ 2161 static KBOOL kwLdrModuleCanLoadNatively(const char *pszFilename, KWLOCATION enmLocation, const char *pszFullPath) 2123 2162 { 2124 2163 if (enmLocation == KWLOCATION_SYSTEM32) … … 2126 2165 if (enmLocation == KWLOCATION_UNKNOWN_NATIVE) 2127 2166 return K_TRUE; 2167 2168 /* If the location is unknown, we must check if it's some dynamic loading 2169 of a SYSTEM32 DLL with a full path. We do not want to load these ourselves! */ 2170 if (enmLocation == KWLOCATION_UNKNOWN) 2171 { 2172 PKFSDIR pWinSys32 = g_pWinSys32; 2173 if (!pWinSys32) 2174 pWinSys32 = kwLdrResolveWinSys32(); 2175 if (pWinSys32) 2176 { 2177 KFSLOOKUPERROR enmError; 2178 PKFSOBJ pFsObj = kFsCacheLookupA(g_pFsCache, pszFullPath, &enmError); 2179 if (pFsObj) 2180 { 2181 KBOOL fInWinSys32 = pFsObj->pParent == pWinSys32; 2182 kFsCacheObjRelease(g_pFsCache, pFsObj); 2183 if (fInWinSys32) 2184 return K_TRUE; 2185 } 2186 } 2187 } 2188 2128 2189 return kHlpStrNICompAscii(pszFilename, TUPLE("msvc")) == 0 2129 2190 || kHlpStrNICompAscii(pszFilename, TUPLE("msdis")) == 0 … … 2218 2279 */ 2219 2280 pszName = kHlpGetFilename(szNormPath); 2220 if (kwLdrModuleCanLoadNatively(pszName, enmLocation ))2281 if (kwLdrModuleCanLoadNatively(pszName, enmLocation, szNormPath)) 2221 2282 pMod = kwLdrModuleCreateNative(szNormPath, uHashPath, 2222 2283 kwLdrModuleShouldDoNativeReplacements(pszName, enmLocation)); … … 4841 4902 switch (cwcExt) 4842 4903 { 4843 case 3: kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], pwszExt[2], fAttrQuery);4844 case 2: kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], 0, fAttrQuery);4845 case 1: kwFsIsCacheableExtensionCommon(pwszExt[0], 0, 0, fAttrQuery);4846 case 0: kwFsIsCacheableExtensionCommon(0, 0, 0, fAttrQuery);4904 case 3: return kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], pwszExt[2], fAttrQuery); 4905 case 2: return kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], 0, fAttrQuery); 4906 case 1: return kwFsIsCacheableExtensionCommon(pwszExt[0], 0, 0, fAttrQuery); 4907 case 0: return kwFsIsCacheableExtensionCommon(0, 0, 0, fAttrQuery); 4847 4908 } 4848 4909 return K_FALSE; … … 4906 4967 { 4907 4968 KU32 cbCache = (KU32)cbFile.QuadPart; 4969 #if 0 4908 4970 KU8 *pbCache = (KU8 *)kHlpAlloc(cbCache); 4909 4971 if (pbCache) … … 4917 4979 if (SetFilePointerEx(hFile, offZero, NULL /*poffNew*/, FILE_BEGIN)) 4918 4980 { 4981 #else 4982 HANDLE hMapping = CreateFileMappingW(hFile, NULL /*pSecAttrs*/, PAGE_READONLY, 4983 0 /*cbMaxLow*/, 0 /*cbMaxHigh*/, NULL /*pwszName*/); 4984 if (hMapping != NULL) 4985 { 4986 KU8 *pbCache = (KU8 *)MapViewOfFile(hMapping, FILE_MAP_READ, 0 /*offFileHigh*/, 0 /*offFileLow*/, cbCache); 4987 CloseHandle(hMapping); 4988 if (pbCache) 4989 { 4990 #endif 4919 4991 /* 4920 4992 * Create the cached file object. … … 4932 5004 kFsCacheObjGetFullPathA(pFsObj, pCachedFile->szPath, cbPath, '/'); 4933 5005 kFsCacheObjRetain(pFsObj); 5006 KWFS_LOG(("Cached '%s': %p LB %#x, hCached=%p\n", pCachedFile->szPath, pbCache, cbCache, hFile)); 4934 5007 return pCachedFile; 4935 5008 } 4936 5009 4937 5010 KWFS_LOG(("Failed to allocate KFSWCACHEDFILE structure!\n")); 5011 #if 1 5012 } 5013 else 5014 KWFS_LOG(("Failed to cache file: MapViewOfFile failed: %u\n", GetLastError())); 5015 } 5016 else 5017 KWFS_LOG(("Failed to cache file: CreateFileMappingW failed: %u\n", GetLastError())); 5018 #else 4938 5019 } 4939 5020 else … … 4947 5028 else 4948 5029 KWFS_LOG(("Failed to allocate %#x bytes for cache!\n", cbCache)); 5030 #endif 4949 5031 } 4950 5032 else … … 5024 5106 { 5025 5107 HANDLE hFile; 5108 5109 /* 5110 * Check for include files and similar that we do read-only caching of. 5111 */ 5026 5112 if (dwCreationDisposition == FILE_OPEN_IF) 5027 5113 { … … 5103 5189 5104 5190 #ifdef WITH_TEMP_MEMORY_FILES 5105 /* First check for temporary files (cl.exe only). */ 5191 /* 5192 * Check for temporary files (cl.exe only). 5193 */ 5106 5194 if ( g_Sandbox.pTool->u.Sandboxed.enmHint == KWTOOLHINT_VISUAL_CPP_CL 5107 5195 && !(dwFlagsAndAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE | FILE_FLAG_BACKUP_SEMANTICS)) … … 5115 5203 #endif 5116 5204 5117 /* Then check for include files and similar. */ 5205 /* 5206 * Check for include files and similar that we do read-only caching of. 5207 */ 5118 5208 if (dwCreationDisposition == FILE_OPEN_IF) 5119 5209 { … … 5129 5219 if (kwFsIsCacheablePathExtensionW(pwszFilename, K_FALSE /*fAttrQuery*/)) 5130 5220 { 5131 /** @todo rewrite in pure UTF-16. */ 5132 char szTmp[2048]; 5133 KSIZE cch = kwUtf16ToStr(pwszFilename, szTmp, sizeof(szTmp)); 5134 if (cch < sizeof(szTmp)) 5135 return kwSandbox_Kernel32_CreateFileA(szTmp, dwDesiredAccess, dwShareMode, pSecAttrs, 5136 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); 5221 KFSLOOKUPERROR enmError; 5222 PKFSOBJ pFsObj; 5223 kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread); 5224 5225 pFsObj = kFsCacheLookupNoMissingW(g_pFsCache, pwszFilename, &enmError); 5226 if (pFsObj) 5227 { 5228 KBOOL fRc = kwFsObjCacheCreateFile(pFsObj, dwDesiredAccess, pSecAttrs && pSecAttrs->bInheritHandle, 5229 &hFile); 5230 kFsCacheObjRelease(g_pFsCache, pFsObj); 5231 if (fRc) 5232 { 5233 KWFS_LOG(("CreateFileW(%ls) -> %p [cached]\n", pwszFilename, hFile)); 5234 return hFile; 5235 } 5236 } 5237 /* These are for nasm and yasm style header searching. Cache will 5238 already have checked the directories for the file, no need to call 5239 CreateFile to do it again. */ 5240 else if (enmError == KFSLOOKUPERROR_NOT_FOUND) 5241 { 5242 KWFS_LOG(("CreateFileW(%ls) -> INVALID_HANDLE_VALUE, ERROR_FILE_NOT_FOUND\n", pwszFilename)); 5243 return INVALID_HANDLE_VALUE; 5244 } 5245 else if ( enmError == KFSLOOKUPERROR_PATH_COMP_NOT_FOUND 5246 || enmError == KFSLOOKUPERROR_PATH_COMP_NOT_DIR) 5247 { 5248 KWFS_LOG(("CreateFileW(%ls) -> INVALID_HANDLE_VALUE, ERROR_PATH_NOT_FOUND\n", pwszFilename)); 5249 return INVALID_HANDLE_VALUE; 5250 } 5251 5252 /* fallback */ 5253 hFile = CreateFileW(pwszFilename, dwDesiredAccess, dwShareMode, pSecAttrs, 5254 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); 5255 KWFS_LOG(("CreateFileW(%ls) -> %p (err=%u) [fallback]\n", pwszFilename, hFile, GetLastError())); 5256 return hFile; 5137 5257 } 5138 5258 } … … 5976 6096 DWORD dwMaximumSizeLow, LPCWSTR pwszName) 5977 6097 { 6098 HANDLE hMapping; 5978 6099 KUPTR const idxHandle = KW_HANDLE_TO_INDEX(hFile); 5979 6100 kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread); … … 5995 6116 && pwszName == NULL) 5996 6117 { 5997 HANDLEhMapping = kwFsTempFileCreateHandle(pHandle->u.pTempFile, GENERIC_READ, K_TRUE /*fMapping*/);6118 hMapping = kwFsTempFileCreateHandle(pHandle->u.pTempFile, GENERIC_READ, K_TRUE /*fMapping*/); 5998 6119 KWFS_LOG(("CreateFileMappingW(%p, %u) -> %p [temp]\n", hFile, fProtect, hMapping)); 5999 6120 return hMapping; … … 6004 6125 return INVALID_HANDLE_VALUE; 6005 6126 } 6006 } 6007 } 6008 } 6009 6010 KWFS_LOG(("CreateFileMappingW(%p)\n", hFile)); 6011 return CreateFileMappingW(hFile, pSecAttrs, fProtect, dwMaximumSizeHigh, dwMaximumSizeLow, pwszName); 6127 6128 /** @todo read cached memory mapped files too for moc. */ 6129 } 6130 } 6131 } 6132 6133 hMapping = CreateFileMappingW(hFile, pSecAttrs, fProtect, dwMaximumSizeHigh, dwMaximumSizeLow, pwszName); 6134 KWFS_LOG(("CreateFileMappingW(%p, %p, %#x, %#x, %#x, %p) -> %p\n", 6135 hFile, pSecAttrs, fProtect, dwMaximumSizeHigh, dwMaximumSizeLow, pwszName, hMapping)); 6136 return hMapping; 6012 6137 } 6013 6138 6014 6139 /** Kernel32 - MapViewOfFile */ 6015 static HANDLE WINAPI kwSandbox_Kernel32_MapViewOfFile(HANDLE hSection, DWORD dwDesiredAccess, 6016 DWORD offFileHigh, DWORD offFileLow, SIZE_T cbToMap) 6017 { 6140 static PVOID WINAPI kwSandbox_Kernel32_MapViewOfFile(HANDLE hSection, DWORD dwDesiredAccess, 6141 DWORD offFileHigh, DWORD offFileLow, SIZE_T cbToMap) 6142 { 6143 PVOID pvRet; 6018 6144 KUPTR const idxHandle = KW_HANDLE_TO_INDEX(hSection); 6019 6145 kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread); … … 6093 6219 } 6094 6220 6095 KWFS_LOG(("MapViewOfFile(%p)\n", hSection)); 6096 return MapViewOfFile(hSection, dwDesiredAccess, offFileHigh, offFileLow, cbToMap); 6221 pvRet = MapViewOfFile(hSection, dwDesiredAccess, offFileHigh, offFileLow, cbToMap); 6222 KWFS_LOG(("MapViewOfFile(%p, %#x, %#x, %#x, %#x) -> %p\n", 6223 hSection, dwDesiredAccess, offFileHigh, offFileLow, cbToMap, pvRet)); 6224 return pvRet; 6097 6225 } 6098 6226 /** @todo MapViewOfFileEx */
Note:
See TracChangeset
for help on using the changeset viewer.