php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79439 ZTS chdir() does not really change the CWD
Submitted: 2020-03-31 14:29 UTC Modified: 2020-04-01 11:22 UTC
From: nesk at xakep dot ru Assigned:
Status: Closed Package: Documentation problem
PHP Version: 7.4.4 OS: Win32/Linux/MacOS
Private report: No CVE-ID: None
 [2020-03-31 14:29 UTC] 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 [~]"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-31 14:32 UTC] nesk at xakep dot ru
P.S. Related issue: https://github.com/SerafimArts/opengl-demo/issues/3
 [2020-03-31 16:54 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-03-31 16:54 UTC] 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-04-01 11:04 UTC] 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-04-01 11:09 UTC] 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:22 UTC] cmb@php.net
-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
 [2020-04-01 11:22 UTC] cmb@php.net
> 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.
 [2020-04-01 11:22 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2020-06-17 18:34 UTC] alexdowad@php.net
Just opened a PR to doc-en to warn about this behavior in the manual:

https://github.com/php/doc-en/pull/121

I wonder if it should also be documented in the page for chroot()?
 [2020-06-18 08:16 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=27ca68ce995137de25ff8c53b28d382477ade597
Log: Fix #79439: Warn about different behavior of chdir() under ZTS
 [2020-06-18 08:16 UTC] phpdocbot@php.net
-Status: Re-Opened +Status: Closed
 [2020-06-19 20:00 UTC] phpdocbot@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=3b3dffbad091a2e76095b8e93e3deddd788336a7
Log: Fix #79439: Warn about different behavior of chdir() under ZTS
 [2020-12-30 11:59 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=1fb6ecdd37ff64f857c87ac90b9e01c4441d7fc7
Log: Fix #79439: Warn about different behavior of chdir() under ZTS
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Jun 24 18:01:23 2021 UTC