Commit Graph

735 Commits

Author SHA1 Message Date
Peter Kokot
e9254494ff
Autotools: Simplify POSIX Threads check (#14855)
The PTHREADS_CHECK is using cache variables so it is run only once early
during the configuration phase, before including SAPI and other M4
files. This removes the TSRM_CHECK_PTHREADS macro and moves the POSIX
Threads check after the PHP_THREAD_SAFETY variable is set in
configure.ac.

The check and error throw in PHP_BUILD_THREAD_SAFE is also joing into
this single check.

This removes the redundant tsrm.m4 file and PHP_CONFIGURE_PART call for
TSRM as it doesn't run any configuration checks anymore.
2024-07-07 22:44:44 +02:00
Peter Kokot
2e756e0086
Remove unused PTHREADS CPP macro definition (#14759) 2024-07-02 05:40:18 +02:00
Niels Dossche
d883996326
Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-14537: shmop Windows 11 crashes the process
2024-06-28 20:14:26 +02:00
Niels Dossche
c19affc567
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-14537: shmop Windows 11 crashes the process
2024-06-28 20:14:20 +02:00
Niels Dossche
39a3266576
Fix GH-14537: shmop Windows 11 crashes the process
The error handling code isn't entirely right in two places.
One of the code blocks is dead because of an always-false condition, and
another code block is missing the assignment of a NULL pointer.

Getting the exact same behaviour is not entirely possible because you
can't extend the size of a shared memory region after it was made with
the Windows APIs we use, unless we destroy the region and recreate it,
but that has other consequences.
However, it certainly shouldn't crash.

Closes GH-14707.
2024-06-28 20:13:47 +02:00
Arnaud Le Blanc
11accb5cdf
Preferably include from build dir (#13516)
* Include from build dir first

This fixes out of tree builds by ensuring that configure artifacts are included
from the build dir.

Before, out of tree builds would preferably include files from the src dir, as
the include path was defined as follows (ignoring includes from ext/ and sapi/) :

    -I$(top_builddir)/main
    -I$(top_srcdir)
    -I$(top_builddir)/TSRM
    -I$(top_builddir)/Zend
    -I$(top_srcdir)/main
    -I$(top_srcdir)/Zend
    -I$(top_srcdir)/TSRM
    -I$(top_builddir)/

As a result, an out of tree build would include configure artifacts such as
`main/php_config.h` from the src dir.

After this change, the include path is defined as follows:

    -I$(top_builddir)/main
    -I$(top_builddir)
    -I$(top_srcdir)/main
    -I$(top_srcdir)
    -I$(top_builddir)/TSRM
    -I$(top_builddir)/Zend
    -I$(top_srcdir)/Zend
    -I$(top_srcdir)/TSRM

* Fix extension include path for out of tree builds

* Include config.h with the brackets form

`#include "config.h"` searches in the directory containing the including-file
before any other include path. This can include the wrong config.h when building
out of tree and a config.h exists in the source tree.

Using `#include <config.h>` uses exclusively the include path, and gives
priority to the build dir.
2024-06-26 00:26:43 +02:00
Niels Dossche
8c92b5f7ae TSRM: change uses of sprintf into snprintf 2024-06-14 08:12:03 -07:00
Peter Kokot
a82d86479c
Replace WIN32 conditions with _WIN32 or PHP_WIN32 (#14462)
* Replace WIN32 conditions with _WIN32 or PHP_WIN32

WIN32 is defined by the SDK and not defined all the time on Windows by
compilers or the environment. _WIN32 is defined as 1 when the
compilation target is 32-bit ARM, 64-bit ARM, x86, or x64. Otherwise,
undefined.

This syncs these usages one step further.

Upstream libgd has replaced WIN32 with _WIN32 via
c60d9fe577

PHP_WIN32 is added to ext/sockets/sockets.stub.php as done in other
*.stub.php files at this point.

* Use PHP_WIN32 in ext/random

* Use PHP_WIN32 in ext/sockets

* Use _WIN32 in xxhash.h as done upstream

See https://github.com/Cyan4973/xxHash/pull/931

* Update end comment with PHP_WIN32
2024-06-10 21:59:41 +02:00
Niels Dossche
89c4e4c4cb
Fix GH-11188: Error when building TSRM in ARM64 (for IR JIT) (#14459)
This is GH-11236 for IR JIT.
2024-06-04 19:47:07 +02:00
Niels Dossche
3e8752169c
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-11188: Error when building TSRM in ARM64
2024-06-03 21:11:20 +02:00
nielsdos
644d3628e3
Fix GH-11188: Error when building TSRM in ARM64
Although the issue mentioned FreeBSD, this is a broader problem:
the current ARM64 code to load the TLS offset assumes a setup with
the non-default TLS model. This problem can also apply on some
configurations on other platforms.

Closes GH-11236.
2024-06-03 20:28:55 +02:00
Paul Ripke
2d6bd1644d Fixed GH-13581: threaded mod_php fails on NetBSD with No space available for Thread Local Storage.
Close GH-13832
2024-03-29 15:16:51 +00:00
David Carlier
868257a3de Fix GH-13727: macro generating invalid call test prototypes fixes.
autoconf/libtool generating code to test features missed `void` for
C calls prototypes w/o arguments.
Note that specific changes related to libtool have to be upstreamed.

Co-authored-by: Peter Kokot <petk@php.net>

close GH-13732
2024-03-18 06:53:39 +00:00
Peter Kokot
42a4e50513
Sync logical operators in shell scripting code (#13560)
This updates the obsolescent `-a` and `-o` binary primaries to `&&` and
`||`.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
2024-03-01 20:40:16 +01:00
Niels Dossche
5941cdaaad
Fix ZTS crashes with persistent resources in modules (#13381)
On shutdown in ZTS the following happens:
- https://github.com/php/php-src/blob/master/Zend/zend.c#L1124-L1125
  gets executed. This destroys global persistent resources and destroys
  the modules. Furthermore, the modules are unloaded too.
- Further down, `ts_free_id(executor_globals_id)` gets executed, which
  calls `executor_globals_dtor`. This function destroys persistent
  resources for each thread.

Notice that in the last step, the modules that the persistent resource
belong to may already have been destroyed. This means that accessing
globals will cause a crash (I previously fixed this with ifdef magic),
or when the module is dynamically loaded we'll try jumping to a
destructor that is no longer loaded in memory. These scenarios cause
crashes.

It's not possible to move the `ts_free_id` call upwards, because that
may break assumptions of callers, and furthermore this would deallocate
the executor globals structure, which means that any access to those
will cause a segfault.

This patch adds a new API to the TSRM that allows running a callback on
a certain resource type. We use this API to destroy the persistent
resources in all threads prior to the module destruction, and keep the
rest of the resource dtor intact.

I verified this fix on Apache with postgres, both dynamically and
statically.

Fixes GH-12974.
2024-02-13 21:43:03 +01:00
Peter Kokot
d657d5592e
Check host_alias on one place (#13042) 2023-12-29 22:44:39 +01:00
Peter Kokot
5c523edffe
Remove obsolete IRIX checks (#12737)
IRIX is a discontinued and closed-source OS with last release in 2006.
2023-11-21 02:56:53 +00:00
Niels Dossche
115afeedac Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-10737: PHP 8.1.16 segfaults on line 597 of sapi/apache2handler/sapi_apache2.c
2023-04-08 16:47:05 +02:00
Niels Dossche
9261ff7ba9 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-10737: PHP 8.1.16 segfaults on line 597 of sapi/apache2handler/sapi_apache2.c
2023-04-08 16:42:52 +02:00
Niels Dossche
51faf04dbd Fix GH-10737: PHP 8.1.16 segfaults on line 597 of sapi/apache2handler/sapi_apache2.c
The TSRM keeps a hashtable mapping the thread IDs to the thread resource pointers.
It's possible that the thread disappears without us knowing, and then another thread
gets spawned some time later with the same ID as the disappeared thread.
Note that since it's a new thread the TSRM key pointer and cached pointer will be NULL.

The Apache request handler `php_handler()` will try to fetch some fields from the SAPI globals.
It uses a lazy thread resource allocation by calling `ts_resource(0);`.
This allocates a thread resource and sets up the TSRM pointers if they haven't been set up yet.

At least, that's what's supposed to happen. But since we are in a situation where the thread ID
still has the resources of the *old* thread associated in the hashtable,
the loop in `ts_resource_ex` will find that thread resource and assume the thread has been setup
already. But this is not the case since this thread is actually a new thread, just reusing the ID
of the old one, without any relation whatsoever to the old thread.
Because of this assumption, the TSRM pointers will not be setup, leading to a
NULL pointer dereference when trying to access the SAPI globals.

We can easily detect this scenario: if we're in the fallback path, and the pointer is NULL,
and we're looking for our own thread resource, we know we're actually reusing a thread ID.
In that case, we'll free up the old thread resources gracefully (gracefully because
there might still be resources open like database connection which need to be
shut down cleanly). After freeing the resources, we'll create the new resources for
this thread as if the stale resources never existed in the first place.
From that point forward, it is as if that situation never occurred.
The fact that this situation happens isn't that bad because a child process containing
threads will eventually be respawned anyway by the SAPI, so the stale thread resources
won't remain forever.

Note that we can't simply assign our own TSRM pointers to the existing
thread resource for our ID, since it was actually from a different thread
(just with the same ID!). Furthermore, the dynamically loaded extensions
have their own pointer, which is only set when their constructor is
called, so we'd have to call their constructor anyway...
I also tried to call the dtor and then the ctor again for those resources
on the pre-existing thread resource to reuse storage, but that didn't work properly
because other code doesn't expect something like that to happen, which breaks assumptions,
and this in turn caused Valgrind to (rightfully) complain about memory bugs.

Note 2: I also had to fix a bug in the core globals destruction because it
always assumed that the thread destroying them was the owning thread,
which on TSRM shutdown isn't always the case. A similar bug was fixed
recently with the JIT globals.

Closes GH-10863.
2023-04-08 16:34:07 +02:00
Michael Orlitzky
fa65873502 */*.m4: update main() signatures.
The next generation of C compilers is going to enforce the C standard
more strictly:

  https://wiki.gentoo.org/wiki/Modern_C_porting

One warning that will soon become an error is -Wstrict-prototypes.
This is relatively easy to catch in most code (it will fail to
compile), but inside of autoconf tests it can go unnoticed because
many feature-test compilations fail by design. For example,

  $ export CFLAGS="$CFLAGS -Werror=strict-prototypes"
  $ ./configure
  ...
  checking if iconv supports errno... no
  configure: error: iconv does not support errno

(this is on a system where iconv *does* support errno). If errno
support were optional, that test would have "silently" disabled
it. The underlying issue here, from config.log, is

  conftest.c:211:5: error: function declaration isn't a prototype
  [-Werror=strict-prototypes]
    211 | int main() {

This commit goes through all of our autoconf tests, replacing main()
with main(void). Up to equivalent types and variable renamings, that's
one of the two valid signatures, and satisfies the compiler (gcc-12 in
this case).

Fixes GH-10751
2023-03-07 15:32:30 +00:00
Ilija Tovilo
7c3b92fc91
Fix strict prototypes warnings 2023-02-17 15:14:30 +01:00
Dmitry Stogov
bf809a918e Merge branch 'PHP-8.2'
* PHP-8.2:
  ext/opcache/zend_jit: cast function to fix -Wincompatible-pointer-types (#10527)
2023-02-07 13:38:52 +03:00
Dmitry Stogov
8ad81c0e8c Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  ext/opcache/zend_jit: cast function to fix -Wincompatible-pointer-types (#10527)
2023-02-07 13:38:39 +03:00
Max Kellermann
afbb28dfb7
ext/opcache/zend_jit: cast function to fix -Wincompatible-pointer-types (#10527)
* ext/opcache/zend_jit: cast function to fix -Wincompatible-pointer-types

Regression by commit a21195650e

* TSRM/win32: fix ts_allocate_dtor cast

The dtor was casted to ts_allocate_ctor; luckily, ts_allocate_dtor and
ts_allocate_ctor just happen to be the same type.
2023-02-07 13:38:17 +03:00
Christoph M. Becker
f8ff105420
Merge branch 'PHP-8.2'
* PHP-8.2:
  shmget() with IPC_CREAT must not create 0 size SHM
2022-12-13 19:43:47 +01:00
Christoph M. Becker
4631e9de2b
shmget() with IPC_CREAT must not create 0 size SHM
The recently committed fix for GH-9944 did only indirectly cater to
that, namely because in this case `CreateFileMapping()` with a zero
size couldn't be created.  As of PHP 8.2.0, the mappings of the actual
SHM and the info segment have been merged, so creating a zero size SHM
would be possible unless we explicitly prohibit this.
2022-12-13 19:43:13 +01:00
Christoph M. Becker
b593b53910
Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix Windows shmget() wrt. IPC_PRIVATE
2022-12-13 15:51:07 +01:00
Christoph M. Becker
9089e15940
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix Windows shmget() wrt. IPC_PRIVATE
2022-12-13 15:49:55 +01:00
Tyson Andre
7a983e281c
Fix Windows shmget() wrt. IPC_PRIVATE
Fixes #9944

https://man7.org/linux/man-pages/man2/shmget.2.html notes

   The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW
   would more clearly show its function.

Closes GH-9946.
2022-12-13 15:46:40 +01:00
Christoph M. Becker
d672c0430f
Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix potential NULL pointer dereference Windows shm*() functions
2022-11-02 14:55:34 +01:00
Christoph M. Becker
79d4fdad52
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix potential NULL pointer dereference Windows shm*() functions
2022-11-02 14:54:48 +01:00
Christoph M. Becker
8bf6266e65
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix potential NULL pointer dereference Windows shm*() functions
2022-11-02 14:53:30 +01:00
Christoph M. Becker
d1c9ff5642
Fix potential NULL pointer dereference Windows shm*() functions
`shm_get()` (not to be confused with `shmget()`) returns `NULL` if
reallocation fails; we need to cater to that when calling the function.

Closes GH-9872.
2022-11-02 14:51:59 +01:00
Christoph M. Becker
a27c41a98f
Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-9829: Bug in refactoring Windows shmat() function
2022-11-02 14:05:54 +01:00
Christoph M. Becker
1e3915c8c7
Fix GH-9829: Bug in refactoring Windows shmat() function
`shm->addr` cannot be `NULL` here, so the whole check is completely
pointless.

Given that this is merely a clean-up and not an actual bug fix, we do
without a NEWS entry.

Closes GH-9873.
2022-11-02 14:05:36 +01:00
Kévin Dunglas
9da75d0c63
fix: no-op when signal handlers are called on threads not managed by PHP (#9766) 2022-10-22 11:17:27 +02:00
Christoph M. Becker
d7bdc040b4
Join shm_pair.info and .segment of Windows shm implementation
There is not much point in having two distinct file mappings; since the
info mapping is very small and of fixed size, we can put it at the
beginning of a single mapping.  Besides the obvious resource savings,
that also simplifies the error handling.

Closes GH-8648.
2022-06-10 17:18:27 +02:00
George Peter Banyard
d08451b2ca
Replace php_stdint.h header with standard headers (#8613) 2022-05-29 11:20:56 +01:00
George Peter Banyard
5ba6ecd523
Minor refactoring of main/main.c and TSRM (#8608) 2022-05-24 08:34:55 +01:00
Christoph M. Becker
4c0639df1c
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Haiku fix ZTS build disabling `tsrm_ls_cache` usage.
2022-04-26 14:18:02 +02:00
David Carlier
4bb0dd49d8
Haiku fix ZTS build disabling tsrm_ls_cache usage. 2022-04-26 14:16:38 +02:00
Christoph M. Becker
097c9271e1
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Move locks in TSRM.c to prevent races
2022-04-11 17:19:39 +02:00
ryancaicse
1a75269c8e
Move locks in TSRM.c to prevent races
Closes GH-8298.
2022-04-11 17:16:40 +02:00
Christoph M. Becker
0b1bca6b9c
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix GH-8160: ZTS support on Alpine is broken
2022-03-11 14:41:46 +01:00
Michael Voříšek
2b2aeb989e
Fix GH-8160: ZTS support on Alpine is broken
We need to export `__MUSL__` so that phpize builds can see the proper
macro, and also need to fix "_tsrm_ls_cache" usage for musl.

Closes GH-8180.
2022-03-11 14:39:39 +01:00
Christoph M. Becker
188a6ccd90
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Haiku fix ZTS build, disabling tls model
2022-02-07 13:07:12 +01:00
David Carlier
bea542a953
Haiku fix ZTS build, disabling tls model
Closes GH-8047.
2022-02-07 13:05:54 +01:00
Joe Watkins
f9bf5f4877
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix TSRM ignoring done
2022-01-11 03:52:40 +01:00
Joe Watkins
d7db5f6e21
Fix TSRM ignoring done 2022-01-11 03:51:06 +01:00