From 515d2579d305a6127c6c194319a58eac62437e33 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Fri, 5 Apr 2019 13:15:42 +0300 Subject: Replace legacy dynamically-loaded functions with statically linked ones We don't need the dynamic load complexity for these now with baseline Windows version being Windows 7 SP1. Stuff used only for compatibility with older versions was dumped. Change-Id: I810f271796cfd875cfa18a3081c9ad444fe57b3e Reviewed-on: https://gerrit.libreoffice.org/70321 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sal/osl/w32/module.cxx | 211 +++++++---------------------------------------- sal/osl/w32/salinit.cxx | 19 +---- sal/osl/w32/security.cxx | 196 ++++++------------------------------------- sal/osl/w32/signal.cxx | 10 +-- 4 files changed, 61 insertions(+), 375 deletions(-) (limited to 'sal/osl/w32') diff --git a/sal/osl/w32/module.cxx b/sal/osl/w32/module.cxx index 8de791a7a8e1..104a82826b94 100644 --- a/sal/osl/w32/module.cxx +++ b/sal/osl/w32/module.cxx @@ -18,7 +18,7 @@ */ #include "system.h" -#include +#include #include "file_url.hxx" #include "path_helper.hxx" @@ -180,206 +180,57 @@ osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol ) return fncAddr; } -/*****************************************************************************/ -/* Implementation for Windows 95, 98 and Me */ -/*****************************************************************************/ - -/* Undefine because there is no explicit "A" definition */ - -#ifdef MODULEENTRY32 -#undef MODULEENTRY32 -#endif - -#ifdef LPMODULEENTRY32 -#undef LPMODULEENTRY32 -#endif - -/***************************************************************************************/ -/* Implementation for Windows NT, 2K and XP (2K and XP could use the above method too) */ -/***************************************************************************************/ - -#include - -typedef BOOL (WINAPI *SymInitialize_PROC)( - HANDLE hProcess, - LPWSTR UserSearchPath, - BOOL fInvadeProcess - ); - -typedef BOOL (WINAPI *SymCleanup_PROC)( - HANDLE hProcess - ); - -typedef BOOL (WINAPI *SymGetModuleInfo_PROC)( - HANDLE hProcess, - DWORD dwAddr, - PIMAGEHLP_MODULEW ModuleInfo - ); - -/* Seems that IMAGEHLP.DLL is always available on NT 4. But MSDN from Platform SDK says Win 2K is required. MSDN from VS 6.0a says - it's O.K on NT 4 ???!!! - BTW: We are using ANSI function because not all version of IMAGEHLP.DLL contain Unicode support -*/ - -static bool osl_addressGetModuleURL_NT4_( void *pv, rtl_uString **pustrURL ) -{ - bool bSuccess = false; /* Assume failure */ - - /* IMAGEHELP.DLL has a bug that it recursively scans subdirectories of - the root when calling SymInitialize(), so we prefer DBGHELP.DLL - which exports the same symbols and is shipped with OOo */ - - HMODULE hModImageHelp = LoadLibraryW( L"DBGHELP.DLL" ); - - if ( !hModImageHelp ) - hModImageHelp = LoadLibraryW( L"IMAGEHLP.DLL" ); - - if ( hModImageHelp ) - { - SymGetModuleInfo_PROC lpfnSymGetModuleInfo; - SymInitialize_PROC lpfnSymInitialize; - SymCleanup_PROC lpfnSymCleanup; - - lpfnSymInitialize = reinterpret_cast(GetProcAddress( hModImageHelp, "SymInitializeW" )); - lpfnSymCleanup = reinterpret_cast(GetProcAddress( hModImageHelp, "SymCleanup" )); - lpfnSymGetModuleInfo = reinterpret_cast(GetProcAddress( hModImageHelp, "SymGetModuleInfoW" )); - - if ( lpfnSymInitialize && lpfnSymCleanup && lpfnSymGetModuleInfo ) - { - IMAGEHLP_MODULEW ModuleInfo; - ::osl::LongPathBuffer< sal_Unicode > aModuleFileName( MAX_LONG_PATH ); - LPWSTR lpSearchPath = nullptr; - - if ( GetModuleFileNameW( nullptr, o3tl::toW(aModuleFileName), aModuleFileName.getBufSizeInSymbols() ) ) - { - wchar_t *pLastBkSlash = wcsrchr( o3tl::toW(aModuleFileName), L'\\' ); - - if ( - pLastBkSlash && - pLastBkSlash > o3tl::toW(aModuleFileName) - && *(pLastBkSlash - 1) != L':' - && *(pLastBkSlash - 1) != L'\\' - ) - { - *pLastBkSlash = 0; - lpSearchPath = o3tl::toW(aModuleFileName); - } - } - - lpfnSymInitialize( GetCurrentProcess(), lpSearchPath, TRUE ); - - ZeroMemory( &ModuleInfo, sizeof(ModuleInfo) ); - ModuleInfo.SizeOfStruct = sizeof(ModuleInfo); - - bSuccess = !!lpfnSymGetModuleInfo( GetCurrentProcess(), reinterpret_cast(pv), &ModuleInfo ); - - if ( bSuccess ) - { - /* #99182 On localized (non-english) NT4 and XP (!!!) for some libraries the LoadedImageName member of ModuleInfo isn't filled. Because - other members ModuleName and ImageName do not contain the full path we can cast the Member - BaseOfImage to a HMODULE (on NT it's the same) and use GetModuleFileName to retrieve the full - path of the loaded image */ - - if ( ModuleInfo.LoadedImageName[0] || GetModuleFileNameW( reinterpret_cast(ModuleInfo.BaseOfImage), ModuleInfo.LoadedImageName, SAL_N_ELEMENTS(ModuleInfo.LoadedImageName) ) ) - { - rtl_uString *ustrSysPath = nullptr; - - rtl_uString_newFromStr( &ustrSysPath, o3tl::toU(ModuleInfo.LoadedImageName) ); - OSL_ASSERT(ustrSysPath != nullptr); - osl_getFileURLFromSystemPath( ustrSysPath, pustrURL ); - rtl_uString_release( ustrSysPath ); - } - else - bSuccess = false; - } - - lpfnSymCleanup( GetCurrentProcess() ); - } - - FreeLibrary( hModImageHelp ); - } - - return bSuccess; -} - -typedef struct MODULEINFO { - LPVOID lpBaseOfDll; - DWORD SizeOfImage; - LPVOID EntryPoint; -} *LPMODULEINFO; - -typedef BOOL (WINAPI *EnumProcessModules_PROC)( - HANDLE hProcess, // handle to the process - HMODULE * lphModule, // array to receive the module handles - DWORD cb, // size of the array - LPDWORD lpcbNeeded // receives the number of bytes returned -); - -typedef BOOL (WINAPI *GetModuleInformation_PROC)( - HANDLE hProcess, // handle to the process - HMODULE hModule, // handle to the module - LPMODULEINFO lpmodinfo, // structure that receives information - DWORD cb // size of the structure -); - -/* This version can fail because PSAPI.DLL is not always part of NT 4 despite MSDN Library 6.0a say so */ - -static bool osl_addressGetModuleURL_NT_( void *pv, rtl_uString **pustrURL ) +sal_Bool SAL_CALL osl_getModuleURLFromAddress( void *pv, rtl_uString **pustrURL ) { bool bSuccess = false; /* Assume failure */ static HMODULE hModPsapi = LoadLibraryW( L"PSAPI.DLL" ); + static auto lpfnEnumProcessModules = reinterpret_cast( + hModPsapi ? GetProcAddress(hModPsapi, "EnumProcessModules") : nullptr); + static auto lpfnGetModuleInformation = reinterpret_cast( + hModPsapi ? GetProcAddress(hModPsapi, "GetModuleInformation") : nullptr); - if ( hModPsapi ) + if (lpfnEnumProcessModules && lpfnGetModuleInformation) { - EnumProcessModules_PROC lpfnEnumProcessModules = reinterpret_cast(GetProcAddress( hModPsapi, "EnumProcessModules" )); - GetModuleInformation_PROC lpfnGetModuleInformation = reinterpret_cast(GetProcAddress( hModPsapi, "GetModuleInformation" )); + DWORD cbNeeded = 0; + HMODULE* lpModules = nullptr; + DWORD nModules = 0; + UINT iModule = 0; + MODULEINFO modinfo; - if ( lpfnEnumProcessModules && lpfnGetModuleInformation ) - { - DWORD cbNeeded = 0; - HMODULE *lpModules = nullptr; - DWORD nModules = 0; - UINT iModule = 0; - MODULEINFO modinfo; + lpfnEnumProcessModules(GetCurrentProcess(), nullptr, 0, &cbNeeded); - lpfnEnumProcessModules( GetCurrentProcess(), nullptr, 0, &cbNeeded ); + lpModules = static_cast(_alloca(cbNeeded)); + lpfnEnumProcessModules(GetCurrentProcess(), lpModules, cbNeeded, &cbNeeded); - lpModules = static_cast(_alloca( cbNeeded )); - lpfnEnumProcessModules( GetCurrentProcess(), lpModules, cbNeeded, &cbNeeded ); + nModules = cbNeeded / sizeof(HMODULE); - nModules = cbNeeded / sizeof(HMODULE); + for (iModule = 0; !bSuccess && iModule < nModules; iModule++) + { + lpfnGetModuleInformation(GetCurrentProcess(), lpModules[iModule], &modinfo, + sizeof(modinfo)); - for ( iModule = 0; !bSuccess && iModule < nModules; iModule++ ) + if (static_cast(pv) >= static_cast(modinfo.lpBaseOfDll) + && static_cast(pv) + < static_cast(modinfo.lpBaseOfDll) + modinfo.SizeOfImage) { - lpfnGetModuleInformation( GetCurrentProcess(), lpModules[iModule], &modinfo, sizeof(modinfo) ); + ::osl::LongPathBuffer aBuffer(MAX_LONG_PATH); + rtl_uString* ustrSysPath = nullptr; - if ( static_cast(pv) >= static_cast(modinfo.lpBaseOfDll) && static_cast(pv) < static_cast(modinfo.lpBaseOfDll) + modinfo.SizeOfImage ) - { - ::osl::LongPathBuffer< sal_Unicode > aBuffer( MAX_LONG_PATH ); - rtl_uString *ustrSysPath = nullptr; + GetModuleFileNameW(lpModules[iModule], o3tl::toW(aBuffer), + aBuffer.getBufSizeInSymbols()); - GetModuleFileNameW( lpModules[iModule], o3tl::toW(aBuffer), aBuffer.getBufSizeInSymbols() ); + rtl_uString_newFromStr(&ustrSysPath, aBuffer); + osl_getFileURLFromSystemPath(ustrSysPath, pustrURL); + rtl_uString_release(ustrSysPath); - rtl_uString_newFromStr( &ustrSysPath, aBuffer ); - osl_getFileURLFromSystemPath( ustrSysPath, pustrURL ); - rtl_uString_release( ustrSysPath ); - - bSuccess = true; - } + bSuccess = true; } } - } return bSuccess; } -sal_Bool SAL_CALL osl_getModuleURLFromAddress( void *pv, rtl_uString **pustrURL ) -{ - /* Use ..._NT first because ..._NT4 is much slower */ - return osl_addressGetModuleURL_NT_( pv, pustrURL ) || osl_addressGetModuleURL_NT4_( pv, pustrURL ); -} - sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl ) { /* casting a function pointer to a data pointer (void*) is diff --git a/sal/osl/w32/salinit.cxx b/sal/osl/w32/salinit.cxx index 71c14e66a566..77cb5da781e5 100644 --- a/sal/osl/w32/salinit.cxx +++ b/sal/osl/w32/salinit.cxx @@ -33,24 +33,11 @@ extern "C" { void sal_detail_initialize(int argc, char ** argv) { sal_initGlobalTimer(); - HMODULE h = GetModuleHandleW(L"kernel32.dll"); - if (h != nullptr) { - FARPROC p; #ifndef _WIN64 - p = GetProcAddress(h, "SetProcessDEPPolicy"); - if (p != 0) { - reinterpret_cast< BOOL (WINAPI *)(DWORD) >(p)(0x00000001); - } + SetProcessDEPPolicy(PROCESS_DEP_ENABLE); #endif - p = GetProcAddress(h, "SetDllDirectoryW"); - if (p != nullptr) { - reinterpret_cast< BOOL (WINAPI *)(LPCWSTR) >(p)(L""); - } - p = GetProcAddress(h, "SetSearchPathMode"); - if (p != nullptr) { - reinterpret_cast< BOOL (WINAPI *)(DWORD) >(p)(0x8001); - } - } + SetDllDirectoryW(L""); // remove the current directory from the default DLL search order + SetSearchPathMode(BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE | BASE_SEARCH_PATH_PERMANENT); WSADATA wsaData; int error; diff --git a/sal/osl/w32/security.cxx b/sal/osl/w32/security.cxx index f7e14e7e32ae..d0872ef2c19c 100644 --- a/sal/osl/w32/security.cxx +++ b/sal/osl/w32/security.cxx @@ -18,6 +18,7 @@ */ #include "system.h" +#include #include #include @@ -31,45 +32,13 @@ #include #include "secimpl.hxx" -/* Data for use in (un)LoadProfile Functions */ -/* Declarations based on USERENV.H for Windows 2000 Beta 2 */ -#define PI_NOUI 0x00000001 // Prevents displaying of messages - -typedef struct { - DWORD dwSize; // Must be set to sizeof(PROFILEINFO) - DWORD dwFlags; // See flags above - LPWSTR lpUserName; // User name (required) - LPWSTR lpProfilePath; // Roaming profile path - LPWSTR lpDefaultPath; // Default user profile path - LPWSTR lpServerName; // Validating DC name in netbios format - LPWSTR lpPolicyPath; // Path to the NT4 style policy file - HANDLE hProfile; // Registry key handle - filled by function -} PROFILEINFOW, FAR * LPPROFILEINFOW; - -/* Typedefs for function pointers in USERENV.DLL */ -typedef BOOL (STDMETHODCALLTYPE FAR * LPFNLOADUSERPROFILE) ( - HANDLE hToken, - LPPROFILEINFOW lpProfileInfo -); - -typedef BOOL (STDMETHODCALLTYPE FAR * LPFNUNLOADUSERPROFILE) ( - HANDLE hToken, - HANDLE hProfile -); - -typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) ( - HANDLE hToken, - LPWSTR lpProfileDir, - LPDWORD lpcchSize -); - /* To get an impersonation token we need to create an impersonation duplicate so every access token has to be created with duplicate access rights */ #define TOKEN_DUP_QUERY (TOKEN_QUERY|TOKEN_DUPLICATE) -static bool GetSpecialFolder(rtl_uString **strPath,int nFolder); +static bool GetSpecialFolder(rtl_uString **strPath, REFKNOWNFOLDERID rFolder); // We use LPCTSTR here, because we use it with SE_foo_NAME constants // which are defined in winnt.h as UNICODE-dependent TEXT("PrivilegeName") static BOOL Privilege(LPCTSTR pszPrivilege, BOOL bEnable); @@ -399,7 +368,7 @@ sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirect } else { - bSuccess = GetSpecialFolder(&ustrSysDir, CSIDL_PERSONAL) && + bSuccess = GetSpecialFolder(&ustrSysDir, FOLDERID_Documents) && (osl_File_E_None == osl_getFileURLFromSystemPath(ustrSysDir, pustrDirectory)); } } @@ -440,7 +409,7 @@ sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDire rtl_uString *ustrFile = nullptr; sal_Unicode sFile[_MAX_PATH]; - if ( !GetSpecialFolder( &ustrFile, CSIDL_APPDATA) ) + if ( !GetSpecialFolder( &ustrFile, FOLDERID_RoamingAppData) ) { OSL_VERIFY(GetWindowsDirectoryW(o3tl::toW(sFile), _MAX_DIR) > 0); @@ -473,9 +442,6 @@ sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security) if (Privilege(SE_RESTORE_NAME, TRUE)) { - HMODULE hUserEnvLib = nullptr; - LPFNLOADUSERPROFILE fLoadUserProfile = nullptr; - LPFNUNLOADUSERPROFILE fUnloadUserProfile = nullptr; HANDLE hAccessToken = static_cast(Security)->m_hToken; /* try to create user profile */ @@ -492,38 +458,25 @@ sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security) } } - hUserEnvLib = LoadLibraryW(L"userenv.dll"); - - if (hUserEnvLib) - { - fLoadUserProfile = reinterpret_cast(GetProcAddress(hUserEnvLib, "LoadUserProfileW")); - fUnloadUserProfile = reinterpret_cast(GetProcAddress(hUserEnvLib, "UnloadUserProfile")); - - if (fLoadUserProfile && fUnloadUserProfile) - { - rtl_uString *buffer = nullptr; - PROFILEINFOW pi; - - getUserNameImpl(Security, &buffer, false); - - ZeroMemory(&pi, sizeof(pi)); - pi.dwSize = sizeof(pi); - pi.lpUserName = o3tl::toW(rtl_uString_getStr(buffer)); - pi.dwFlags = PI_NOUI; + rtl_uString *buffer = nullptr; + PROFILEINFOW pi; - if (fLoadUserProfile(hAccessToken, &pi)) - { - fUnloadUserProfile(hAccessToken, pi.hProfile); + getUserNameImpl(Security, &buffer, false); - bOk = true; - } + ZeroMemory(&pi, sizeof(pi)); + pi.dwSize = sizeof(pi); + pi.lpUserName = o3tl::toW(rtl_uString_getStr(buffer)); + pi.dwFlags = PI_NOUI; - rtl_uString_release(buffer); - } + if (LoadUserProfileW(hAccessToken, &pi)) + { + UnloadUserProfile(hAccessToken, pi.hProfile); - FreeLibrary(hUserEnvLib); + bOk = true; } + rtl_uString_release(buffer); + if (hAccessToken && (hAccessToken != static_cast(Security)->m_hToken)) CloseHandle(hAccessToken); } @@ -535,8 +488,6 @@ void SAL_CALL osl_unloadUserProfile(oslSecurity Security) { if ( static_cast(Security)->m_hProfile != nullptr ) { - HMODULE hUserEnvLib = nullptr; - LPFNUNLOADUSERPROFILE fUnloadUserProfile = nullptr; HANDLE hAccessToken = static_cast(Security)->m_hToken; if ( !hAccessToken ) @@ -552,20 +503,8 @@ void SAL_CALL osl_unloadUserProfile(oslSecurity Security) } } - hUserEnvLib = LoadLibraryW(L"userenv.dll"); - - if (hUserEnvLib) - { - fUnloadUserProfile = reinterpret_cast(GetProcAddress(hUserEnvLib, "UnloadUserProfile")); - - if (fUnloadUserProfile) - { - /* unloading the user profile */ - fUnloadUserProfile(hAccessToken, static_cast(Security)->m_hProfile); - } - - FreeLibrary(hUserEnvLib); - } + /* unloading the user profile */ + UnloadUserProfile(hAccessToken, static_cast(Security)->m_hProfile); static_cast(Security)->m_hProfile = nullptr; @@ -574,100 +513,15 @@ void SAL_CALL osl_unloadUserProfile(oslSecurity Security) } } -static bool GetSpecialFolder(rtl_uString **strPath, int nFolder) +static bool GetSpecialFolder(rtl_uString **strPath, REFKNOWNFOLDERID rFolder) { bool bRet = false; - HINSTANCE hLibrary = LoadLibraryW(L"shell32.dll"); - - if (hLibrary != nullptr) + PWSTR PathW; + if (SUCCEEDED(SHGetKnownFolderPath(rFolder, KF_FLAG_CREATE, nullptr, &PathW))) { - sal_Unicode PathW[_MAX_PATH]; - BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL) = reinterpret_cast(GetProcAddress(hLibrary, "SHGetSpecialFolderPathW")); - - if (pSHGetSpecialFolderPathW) - { - if (pSHGetSpecialFolderPathW(GetActiveWindow(), o3tl::toW(PathW), nFolder, TRUE)) - { - rtl_uString_newFromStr( strPath, PathW); - bRet = true; - } - } - else - { - HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *) = reinterpret_cast(GetProcAddress(hLibrary, "SHGetSpecialFolderLocation")); - BOOL (WINAPI *pSHGetPathFromIDListW)(LPCITEMIDLIST, LPWSTR) = reinterpret_cast(GetProcAddress(hLibrary, "SHGetPathFromIDListW")); - HRESULT (WINAPI *pSHGetMalloc)(LPMALLOC *) = reinterpret_cast(GetProcAddress(hLibrary, "SHGetMalloc")); - - if (pSHGetSpecialFolderLocation && pSHGetPathFromIDListW && pSHGetMalloc ) - { - LPITEMIDLIST pidl; - LPMALLOC pMalloc; - HRESULT hr; - - hr = pSHGetSpecialFolderLocation(GetActiveWindow(), nFolder, &pidl); - - /* Get SHGetSpecialFolderLocation fails if directory does not exists. */ - /* If it fails we try to create the directory and redo the call */ - if (! SUCCEEDED(hr)) - { - HKEY hRegKey; - - if (RegOpenKeyW(HKEY_CURRENT_USER, - L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", - &hRegKey) == ERROR_SUCCESS) - { - LONG lRet; - DWORD lSize = sizeof(PathW); - DWORD Type = REG_SZ; - - switch (nFolder) - { - case CSIDL_APPDATA: - lRet = RegQueryValueExW(hRegKey, L"AppData", nullptr, &Type, reinterpret_cast(PathW), &lSize); - break; - - case CSIDL_PERSONAL: - lRet = RegQueryValueExW(hRegKey, L"Personal", nullptr, &Type, reinterpret_cast(PathW), &lSize); - break; - - default: - lRet = -1l; - } - - if ((lRet == ERROR_SUCCESS) && (Type == REG_SZ)) - { - if (_waccess(o3tl::toW(PathW), 0) < 0) - CreateDirectoryW(o3tl::toW(PathW), nullptr); - - hr = pSHGetSpecialFolderLocation(GetActiveWindow(), nFolder, &pidl); - } - - RegCloseKey(hRegKey); - } - } - - if (SUCCEEDED(hr)) - { - if (pSHGetPathFromIDListW(pidl, o3tl::toW(PathW))) - { - /* if directory does not exist, create it */ - if (_waccess(o3tl::toW(PathW), 0) < 0) - CreateDirectoryW(o3tl::toW(PathW), nullptr); - - rtl_uString_newFromStr( strPath, PathW); - bRet = true; - } - } - - if (SUCCEEDED(pSHGetMalloc(&pMalloc))) - { - pMalloc->Free(pidl); - pMalloc->Release(); - } - } - } - - FreeLibrary(hLibrary); + rtl_uString_newFromStr(strPath, o3tl::toU(PathW)); + CoTaskMemFree(PathW); + bRet = true; } return bRet; diff --git a/sal/osl/w32/signal.cxx b/sal/osl/w32/signal.cxx index 79d0bcbd4e03..aa5662221c7c 100644 --- a/sal/osl/w32/signal.cxx +++ b/sal/osl/w32/signal.cxx @@ -27,6 +27,7 @@ #include #include +#include namespace { @@ -39,14 +40,7 @@ bool onInitSignal() { pPreviousHandler = SetUnhandledExceptionFilter(signalHandlerFunction); - HMODULE hFaultRep = LoadLibraryW( L"faultrep.dll" ); - if ( hFaultRep ) - { - pfn_ADDEREXCLUDEDAPPLICATIONW pfn = reinterpret_cast(GetProcAddress( hFaultRep, "AddERExcludedApplicationW" )); - if ( pfn ) - pfn( L"SOFFICE.EXE" ); - FreeLibrary( hFaultRep ); - } + WerAddExcludedApplication(L"SOFFICE.EXE", FALSE); return true; } -- cgit