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: Re-Opened Package: Documentation problem
PHP Version: 7.4.4 OS: Win32/Linux/MacOS
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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:
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jun 01 22:01:25 2020 UTC