Skip to content

Commit 7b74480

Browse files
committed
Close GH-16718: GH-Add SAPI module folder to DLL search path
On Windows, it is customary to put dependency DLLs in the main PHP folder, i.e. right besides php.exe. If the SAPI is implemented as .exe, dependency lookup works fine, since DLLs are always searched in the folder of the .exe. However, for Webserver modules, this usually does not work, since the Webserver's executable likely resides in a different folder. While there are obviously solutions to this, the Windows error messages if a module can't be loaded are confusing, so users and even popular AMP distributions often choose to put the DLLs in the folder of the Webserver (e.g. right besides httpd.exe). That can easily cause more confusion later, when users update to a newer PHP version, which requires more recent dependencies. Thus, to simplify setup and to bring the behavior more in line with CLI and CGI SAPIs, we add the SAPI module folder to the DLL search path. We keep that simple, and do not cater to long paths, and silently don't change the DLL search order in case of unexpected failures. Since we are using `SetDllDirectory()` for all SAPIs, that effectively also prevents DLLs being looked up in the current folder, what might break some scripts, but can also avoid confusion regarding ZTS builds, where the current directory of the process is not necessarily what is reported by `getcwd()`. It is also a small security measure, even stronger than safe DLL search mode[1]. Note that this certainly does not solve all problems related to DLL lookup, e.g. that the application folder is still searched first, and that already loaded DLLs will not be searched and loaded again. But it appears to be a sensible improvement, and hopefully prevents others to put DLLs in folders where they don't belong, in the future. [1] <https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order>
1 parent 17187c4 commit 7b74480

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

main/SAPI.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,31 @@ static void sapi_globals_dtor(sapi_globals_struct *sapi_globals)
6565
SAPI_API sapi_module_struct sapi_module;
6666

6767

68+
#ifdef PHP_WIN32
69+
static void add_sapi_module_folder_to_dll_search_path(sapi_module_struct *sf)
70+
{
71+
const DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
72+
HMODULE module;
73+
wchar_t filename[MAX_PATH];
74+
if (GetModuleHandleEx(flags, (LPCTSTR) sf, &module)) {
75+
DWORD len = GetModuleFileNameW(module, filename, MAX_PATH);
76+
if (len > 0 && len < MAX_PATH) {
77+
wchar_t *slash = wcsrchr(filename, L'\\');
78+
slash[0] = L'\0';
79+
SetDllDirectoryW(filename);
80+
}
81+
}
82+
}
83+
#endif
84+
6885
SAPI_API void sapi_startup(sapi_module_struct *sf)
6986
{
7087
sf->ini_entries = NULL;
7188
sapi_module = *sf;
7289

90+
#ifdef PHP_WIN32
91+
add_sapi_module_folder_to_dll_search_path(sf);
92+
#endif
7393
#ifdef ZTS
7494
ts_allocate_fast_id(&sapi_globals_id, &sapi_globals_offset, sizeof(sapi_globals_struct), (ts_allocate_ctor) sapi_globals_ctor, (ts_allocate_dtor) sapi_globals_dtor);
7595
# ifdef PHP_WIN32

0 commit comments

Comments
 (0)