Bug->Doc #79439 [Nab->ReO]: ZTS chdir() does not really change the CWD

From: Date: Wed, 01 Apr 2020 11:22:21 +0000
Subject: Bug->Doc #79439 [Nab->ReO]: ZTS chdir() does not really change the CWD
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17452@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79439&edit=1 ID: 79439 Updated by: cmb@php.net Reported by: nesk at xakep dot ru -Summary: chdir function does not change the working directory for LoadLibrary inside lib +Summary: ZTS chdir() does not really change the CWD -Status: Not a bug +Status: Re-Opened -Type: Bug +Type: Documentation Problem -Package: ffi +Package: Documentation problem Operating System: Win32/Linux/MacOS PHP Version: 7.4.4 Assigned To: cmb Block user comment: N Private report: N New Comment: > However, it may make sense to change the working directory > during calls to the FFI methods? I don't think so. Another thread may change the CWD for another FFI call, and FFI calls may take very long (an FFI call may start an event loop which calls back to PHP functions). > Maybe its worthwhile to correct the documentation, adding > information that the FFI and the ZTS are incompatible? The CWD behavior is not specific to FFI, but affects PHP extensions as well. I agree, though, that it might be worthwhile to be documented. Previous Comments: ------------------------------------------------------------------------ [2020-04-01 11:09:48] nesk at xakep dot ru P.S. You say "dont use such libraries", yes, this is an argument. But in this case, ZTS is simply impossible to use when working with the FFI, because almost every library has dependencies that we should be able to manage. Maybe its worthwhile to correct the documentation, adding information that the FFI and the ZTS are incompatible? Or add a warning that by having ZTS you lose the ability to influence the behavior of FFI? ------------------------------------------------------------------------ [2020-04-01 11:04:02] nesk at xakep dot ru I understand that the ZTS isolates the global state. However, it may make sense to change the working directory during calls to the FFI methods? Thus, before any call to the FFI method, we will change it, and after the call, return it back. And because ZTS mode uses a local variable to store the working directory, so we won’t break anything, because getcwd() will return a value from a local variable instead of "GetCurrentDirectory". ------------------------------------------------------------------------ [2020-03-31 16:54:30] cmb@php.net In ZTS mode, chdir() does not actually change the current working directory, because there is only one CWD per process (not per thread). Instead it merely stores the directory path internally, and uses that whenever a relative file path is passed to a PHP function. Functions in an external library are usually not aware of that; especially, GetCurrentDirectory() or getcwd(3) don't know about PHP's virtual CWD. There is nothing we can do about that. So either don't use such libraries, or use NTS builds. ------------------------------------------------------------------------ [2020-03-31 14:32:29] nesk at xakep dot ru P.S. Related issue: https://github.com/SerafimArts/opengl-demo/issues/3 ------------------------------------------------------------------------ [2020-03-31 14:29:04] nesk at xakep dot ru Description: ------------ In the case that PHP ZTS mode is used, "chdir" function does not change working directory inside the library. That is, ZTS is not able to use libraries that use dynamic linking through the LoadLibrary function. Test script: --------------- <?php // A complete example is not possible, because library // with an example is required. Like: // library.so // // void fun() { // char buff[MAX_PATH]; // GetCurrentDirectoryA(MAX_PATH, buff); // printf("Directory [%s]", buff); // } chdir('path/to/working/directory'); $sdl = FFI::cdef('extern void fun(void)', 'path/to/library.so'); // In PHP 7.4 NTS >> Directory [~/path/to/working/directory] // In PHP 7.4 ZTS >> Directory [~] Expected result: ---------------- Like "Directory [~/path/to/working/directory]" Actual result: -------------- Like "Directory [~]" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79439&edit=1

« previous php.doc.bugs (#17452) next »