VirtualBox

Changeset 2940 in kBuild for trunk/src/kWorker


Ignore:
Timestamp:
Sep 19, 2016 6:34:43 PM (8 years ago)
Author:
bird
Message:

kWorker: Fixed busted read-only caching, checking out using memory mapped files for it. Made the CreateFileW interceptor not callCreateFileA for read-only cached files. Deal with
?\ prefixed paths (long paths) and other moc.exe related fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kWorker/kWorker.c

    r2938 r2940  
    815815/** The current directory (referenced). */
    816816static PKFSOBJ      g_pCurDirObj = NULL;
     817#ifdef KBUILD_OS_WINDOWS
     818/** The windows system32 directory (referenced). */
     819static PKFSDIR      g_pWinSys32 = NULL;
     820#endif
    817821
    818822/** Verbosity level. */
     
    21142118
    21152119/**
     2120 * Lazily initializes the g_pWinSys32 variable.
     2121 */
     2122static 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/**
    21162154 * Whether we can load this DLL natively or not.
    21172155 *
     
    21192157 * @param   pszFilename         The filename (no path).
    21202158 * @param   enmLocation         The location.
    2121  */
    2122 static KBOOL kwLdrModuleCanLoadNatively(const char *pszFilename, KWLOCATION enmLocation)
     2159 * @param   pszFullPath         The full filename and path.
     2160 */
     2161static KBOOL kwLdrModuleCanLoadNatively(const char *pszFilename, KWLOCATION enmLocation, const char *pszFullPath)
    21232162{
    21242163    if (enmLocation == KWLOCATION_SYSTEM32)
     
    21262165    if (enmLocation == KWLOCATION_UNKNOWN_NATIVE)
    21272166        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
    21282189    return kHlpStrNICompAscii(pszFilename, TUPLE("msvc"))   == 0
    21292190        || kHlpStrNICompAscii(pszFilename, TUPLE("msdis"))  == 0
     
    22182279             */
    22192280            pszName = kHlpGetFilename(szNormPath);
    2220             if (kwLdrModuleCanLoadNatively(pszName, enmLocation))
     2281            if (kwLdrModuleCanLoadNatively(pszName, enmLocation, szNormPath))
    22212282                pMod = kwLdrModuleCreateNative(szNormPath, uHashPath,
    22222283                                               kwLdrModuleShouldDoNativeReplacements(pszName, enmLocation));
     
    48414902    switch (cwcExt)
    48424903    {
    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);
    48474908    }
    48484909    return K_FALSE;
     
    49064967            {
    49074968                KU32 cbCache = (KU32)cbFile.QuadPart;
     4969#if 0
    49084970                KU8 *pbCache = (KU8 *)kHlpAlloc(cbCache);
    49094971                if (pbCache)
     
    49174979                        if (SetFilePointerEx(hFile, offZero, NULL /*poffNew*/, FILE_BEGIN))
    49184980                        {
     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
    49194991                            /*
    49204992                             * Create the cached file object.
     
    49325004                                kFsCacheObjGetFullPathA(pFsObj, pCachedFile->szPath, cbPath, '/');
    49335005                                kFsCacheObjRetain(pFsObj);
     5006                                KWFS_LOG(("Cached '%s': %p LB %#x, hCached=%p\n", pCachedFile->szPath, pbCache, cbCache, hFile));
    49345007                                return pCachedFile;
    49355008                            }
    49365009
    49375010                            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
    49385019                        }
    49395020                        else
     
    49475028                else
    49485029                    KWFS_LOG(("Failed to allocate %#x bytes for cache!\n", cbCache));
     5030#endif
    49495031            }
    49505032            else
     
    50245106{
    50255107    HANDLE hFile;
     5108
     5109    /*
     5110     * Check for include files and similar that we do read-only caching of.
     5111     */
    50265112    if (dwCreationDisposition == FILE_OPEN_IF)
    50275113    {
     
    51035189
    51045190#ifdef WITH_TEMP_MEMORY_FILES
    5105     /* First check for temporary files (cl.exe only). */
     5191    /*
     5192     * Check for temporary files (cl.exe only).
     5193     */
    51065194    if (   g_Sandbox.pTool->u.Sandboxed.enmHint == KWTOOLHINT_VISUAL_CPP_CL
    51075195        && !(dwFlagsAndAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE | FILE_FLAG_BACKUP_SEMANTICS))
     
    51155203#endif
    51165204
    5117     /* Then check for include files and similar. */
     5205    /*
     5206     * Check for include files and similar that we do read-only caching of.
     5207     */
    51185208    if (dwCreationDisposition == FILE_OPEN_IF)
    51195209    {
     
    51295219                    if (kwFsIsCacheablePathExtensionW(pwszFilename, K_FALSE /*fAttrQuery*/))
    51305220                    {
    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;
    51375257                    }
    51385258                }
     
    59766096                                                           DWORD dwMaximumSizeLow, LPCWSTR pwszName)
    59776097{
     6098    HANDLE      hMapping;
    59786099    KUPTR const idxHandle = KW_HANDLE_TO_INDEX(hFile);
    59796100    kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread);
     
    59956116                        && pwszName == NULL)
    59966117                    {
    5997                         HANDLE hMapping = kwFsTempFileCreateHandle(pHandle->u.pTempFile, GENERIC_READ, K_TRUE /*fMapping*/);
     6118                        hMapping = kwFsTempFileCreateHandle(pHandle->u.pTempFile, GENERIC_READ, K_TRUE /*fMapping*/);
    59986119                        KWFS_LOG(("CreateFileMappingW(%p, %u) -> %p [temp]\n", hFile, fProtect, hMapping));
    59996120                        return hMapping;
     
    60046125                    return INVALID_HANDLE_VALUE;
    60056126                }
    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;
    60126137}
    60136138
    60146139/** Kernel32 - MapViewOfFile  */
    6015 static HANDLE WINAPI kwSandbox_Kernel32_MapViewOfFile(HANDLE hSection, DWORD dwDesiredAccess,
    6016                                                       DWORD offFileHigh, DWORD offFileLow, SIZE_T cbToMap)
    6017 {
     6140static PVOID WINAPI kwSandbox_Kernel32_MapViewOfFile(HANDLE hSection, DWORD dwDesiredAccess,
     6141                                                     DWORD offFileHigh, DWORD offFileLow, SIZE_T cbToMap)
     6142{
     6143    PVOID       pvRet;
    60186144    KUPTR const idxHandle = KW_HANDLE_TO_INDEX(hSection);
    60196145    kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread);
     
    60936219    }
    60946220
    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;
    60976225}
    60986226/** @todo MapViewOfFileEx */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette