Commit graph

233 commits

Author SHA1 Message Date
Niels Dossche
d20e3e6cb1
Simplify handling of inheritance in SplFixedArray
After the loop, `parent` will for sure be ce_SplFixedArray, and
inherited will be true; for inherited cases.
2025-03-31 19:39:32 +02:00
Niels Dossche
9e52d1698a
Use specialised functions in SplFixedArray dimension handlers
This is more efficient than manually dealing with a garbage copy.
2025-03-31 19:38:40 +02:00
Niels Dossche
e034b69fa6
Optimize SplFixedArray::fromArray() for packed arrays (#18196)
If the array is packed, then we don't have to loop to get the highest
index.

For this script:
```php
$array = range(1, 100);
for ($i=0;$i<1000000;$i++) {
    SplFixedArray::fromArray($array);
}
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php spl.php
  Time (mean ± σ):     376.5 ms ±   2.0 ms    [User: 372.1 ms, System: 2.6 ms]
  Range (min … max):   373.7 ms … 379.5 ms    10 runs

Benchmark 2: ./sapi/cli/php_old spl.php
  Time (mean ± σ):     511.6 ms ±   1.9 ms    [User: 508.0 ms, System: 2.3 ms]
  Range (min … max):   509.2 ms … 515.1 ms    10 runs

Summary
  ./sapi/cli/php spl.php  ran
    1.36 ± 0.01 times faster than ./sapi/cli/php_old spl.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php spl.php
  Time (mean ± σ):     250.4 ms ±   3.5 ms    [User: 246.6 ms, System: 2.6 ms]
  Range (min … max):   247.0 ms … 258.5 ms    11 runs

Benchmark 2: ./sapi/cli/php_old spl.php
  Time (mean ± σ):     328.4 ms ±   1.0 ms    [User: 324.4 ms, System: 3.8 ms]
  Range (min … max):   327.5 ms … 331.0 ms    10 runs

Summary
  ./sapi/cli/php spl.php  ran
    1.31 ± 0.02 times faster than ./sapi/cli/php_old spl.php
```

Bonus: this also decreases the code size of the function.
2025-03-30 20:07:49 +02:00
Niels Dossche
f056636086
Avoid rebuilding the property table when possible in SplFixedArray's gc handler (#18195)
If there is not yet a dynamic property, and there are no class properties,
then we know that we don't have to build a properties table.

For this (micro-bench) script:
```php
function x() {
    $fa = new SplFixedArray(1);
    $fa[0] = $fa;
}
for ($i=0;$i<1000000;$i++)
    x();
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php spl.php
  Time (mean ± σ):     140.9 ms ±   1.2 ms    [User: 137.5 ms, System: 2.7 ms]
  Range (min … max):   138.9 ms … 144.9 ms    21 runs

Benchmark 2: ./sapi/cli/php_old spl.php
  Time (mean ± σ):     162.0 ms ±   3.8 ms    [User: 157.7 ms, System: 3.2 ms]
  Range (min … max):   158.5 ms … 175.0 ms    17 runs

Summary
  ./sapi/cli/php spl.php  ran
    1.15 ± 0.03 times faster than ./sapi/cli/php_old spl.php
```
2025-03-30 18:36:38 +02:00
Niels Dossche
ce5d2f6d01
Make SplFixedArray::jsonSerialize() an implementation alias of SplFixedArray::toArray() (#18191)
This reduces code duplication and can then use the optimized version.
2025-03-30 18:09:11 +02:00
Niels Dossche
d13d9b3c24
Optimize SplFixedArray::toArray() (#18190)
We can use the optimized packed filling code instead of going through
all the logic of the zend_hash update APIs.

For this script:
```php
$test = new SplFixedArray(4);
$test[0] = 0;
$test[1] = 1;
$test[2] = 2;
$test[3] = 3;

for ($i = 0 ; $i< 5000000; $i++)
	$test->toArray();
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php toarray.php
  Time (mean ± σ):     170.0 ms ±   1.8 ms    [User: 167.3 ms, System: 2.2 ms]
  Range (min … max):   166.9 ms … 173.0 ms    17 runs

Benchmark 2: ./sapi/cli/php_old toarray.php
  Time (mean ± σ):     215.7 ms ±   3.6 ms    [User: 211.9 ms, System: 3.0 ms]
  Range (min … max):   211.3 ms … 222.0 ms    13 runs

Summary
  ./sapi/cli/php toarray.php ran
    1.27 ± 0.02 times faster than ./sapi/cli/php_old toarray.php
```

On an i7-1185G7:
```

Benchmark 1: ./sapi/cli/php toarray.php
  Time (mean ± σ):     112.6 ms ±   1.4 ms    [User: 109.6 ms, System: 2.9 ms]
  Range (min … max):   111.1 ms … 116.4 ms    25 runs

Benchmark 2: ./sapi/cli/php_old toarray.php
  Time (mean ± σ):     145.3 ms ±   2.8 ms    [User: 141.8 ms, System: 3.4 ms]
  Range (min … max):   142.6 ms … 151.8 ms    20 runs

Summary
  ./sapi/cli/php toarray.php  ran
    1.29 ± 0.03 times faster than ./sapi/cli/php_old toarray.php
```
2025-03-30 18:09:01 +02:00
Niels Dossche
335c0b39a2
Optimize SplFixedArray dimension performance (#18184)
This patch optimizes reading and writing from SplFixedArray with the
dimension operators. It accomplishes this due to the following
optimizations:
* Fast-path for long keys (inlined).
* Optimization hints (UNEXPECTED + assertion)
* Using an unsigned index so we can do a single length comparison

For the following script:
```php
$test = new SplFixedArray(4);

for ($i = 0 ; $i< 5000000; $i++)
	$test[1] += $i;
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):      95.4 ms ±   1.6 ms    [User: 91.5 ms, System: 3.2 ms]
  Range (min … max):    93.7 ms … 100.8 ms    31 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):     119.1 ms ±   1.3 ms    [User: 114.7 ms, System: 3.6 ms]
  Range (min … max):   117.6 ms … 123.1 ms    24 runs

Summary
  ./sapi/cli/php x.php ran
    1.25 ± 0.03 times faster than ./sapi/cli/php_old x.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):      67.9 ms ±   1.1 ms    [User: 64.8 ms, System: 3.2 ms]
  Range (min … max):    66.6 ms …  72.8 ms    43 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      84.8 ms ±   1.1 ms    [User: 81.0 ms, System: 3.9 ms]
  Range (min … max):    82.6 ms …  88.0 ms    34 runs

Summary
  ./sapi/cli/php x.php  ran
    1.25 ± 0.03 times faster than ./sapi/cli/php_old x.php
```
2025-03-30 13:16:33 +02:00
Niels Dossche
5f13c62c77
Fix GH-17198: SplFixedArray assertion failure with get_object_vars
Because the properties table contains both a numeric index and a string
index that map to 0 in a symbol table, this causes an assertion failure.
Looking at the manual page of get_object_vars(), it seems that only real
properties must be included. Given that SplFixedArray's elements are not
accessible like properties, they should be excluded. This restores PHP
8.3 behaviour. The reason that this didn't cause problems on 8.3 is
because it used a different handler (get_properties).

Closes GH-17206.
2024-12-17 23:22:33 +01:00
Ilija Tovilo
c82cea0c34
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix uaf in SplFixedArray::unset()
2024-10-17 18:25:56 +02:00
Ilija Tovilo
0932b76d02
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix uaf in SplFixedArray::unset()
2024-10-17 18:25:33 +02:00
Ilija Tovilo
7fe168d855
Fix uaf in SplFixedArray::unset()
Fixes GH-16478
Closes GH-16481
2024-10-17 18:23:55 +02:00
Gina Peter Banyard
c8b45aa59a
ext/spl: Follow-up on GH-9704 (#15295)
Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>
2024-08-08 19:31:51 +01: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
Gina Peter Banyard
54047c1090 ext/spl: Remove some useless header includes and clarify usages 2024-06-15 01:33:09 +01:00
Gina Peter Banyard
a5cacba6d8
ext/spl: Remove spl_engine.h header (#14418)
And convert calls to spl_instantiate_arg_* to the new object_init_with_constructor() API
2024-06-08 23:46:34 +01:00
Gina Peter Banyard
fd2d869642
Clean-up some more headers (#14416)
Remove unused headers (such as php_ini.h for extensions that don't define INI settings)
Use more specific headers when possible
2024-06-08 17:15:36 +01:00
Peter Kokot
e93d23a9cb
Remove unused ext/spl SPL_API code (#13985)
The ext/spl is always enabled and building it as shared is no longer
relevant.
2024-04-19 22:50:36 +02:00
Gina Peter Banyard
0de88dfb81
ext/spl: mark all zend_object_handlers as static (#13547) 2024-02-28 15:31:28 +00:00
Niels Dossche
1d20fc5fc5 Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-13531: Unable to resize SplfixedArray after being unserialized in PHP 8.2.15
2024-02-27 23:05:34 +01:00
Niels Dossche
0285395126 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-13531: Unable to resize SplfixedArray after being unserialized in PHP 8.2.15
2024-02-27 23:05:26 +01:00
Niels Dossche
8494058a1f Fix GH-13531: Unable to resize SplfixedArray after being unserialized in PHP 8.2.15
When unserializing, the cached_resize field was not reset to -1
correctly, causing the setSize() method to think we were inside of a
resize operation.

Closes GH-13543.
2024-02-27 23:04:23 +01:00
David CARLIER
9726721560
general signatures discrepencies fixes (#13122) 2024-01-10 22:19:23 +00:00
Niels Dossche
4cfc8f6d7a
Remove dead stores from ext/spl (#12550) 2023-10-29 14:42:23 +00:00
Niels Dossche
009d48da1c
Convert bounds exception in SplFixedArray to OutOfBoundsException instead of RuntimeException (#12383) 2023-10-08 18:57:57 +02:00
Niels Dossche
63a7f225ea Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix #81992: SplFixedArray::setSize() causes use-after-free
2023-08-14 21:38:36 +02:00
Niels Dossche
0b516aea25 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix #81992: SplFixedArray::setSize() causes use-after-free
2023-08-14 21:34:04 +02:00
Niels Dossche
b71c6b2c6c Fix #81992: SplFixedArray::setSize() causes use-after-free
Upon resizing, the elements are destroyed from lower index to higher
index. When an element refers to an object with a destructor, it can
refer to a lower (i.e. already destroyed) element, causing a uaf.
Set refcounted zvals to NULL after destroying them to avoid a uaf.

Closes GH-11959.
2023-08-14 21:32:22 +02:00
George Peter Banyard
d5ad75108e
More usage of known zend_str instead of C string (#11381) 2023-06-08 13:03:29 +01:00
George Peter Banyard
99fa740acb
Use common function for TypeError on illegal offset access (#10544)
This merges all usages of emitting an offset TypeError into a new ZEND_API function
zend_illegal_container_offset(const zend_string* container, const zval *offset, int type);

Where the container should represent the type on which the access is attempted (e.g. string, array)
The offset zval that is used, where the error message will display its type
The type of access, which should be a BP_VAR_* constant, to get special message for isset/empty/unset
2023-06-06 11:28:19 +01:00
Niels Dossche
930db2b2d3 Merge branch 'PHP-8.2'
* PHP-8.2:
  Handle indirect zvals and use up-to-date properties in SplFixedArray::__serialize
  [ci skip] NEWS
2023-03-30 22:07:48 +02:00
Niels Dossche
47b3fe4710 Handle indirect zvals and use up-to-date properties in SplFixedArray::__serialize
Closes GH-10925.
2023-03-30 21:43:39 +02:00
Niels Dossche
42ecd8858b Merge branch 'PHP-8.2'
* PHP-8.2:
  Revert "Handle indirect zvals in SplFixedArray::__serialize"
2023-03-27 21:54:08 +02:00
Niels Dossche
0d524eda94 Revert "Handle indirect zvals in SplFixedArray::__serialize"
This reverts commit e698938229.
2023-03-27 21:47:02 +02:00
Niels Dossche
abcb88e594 Merge branch 'PHP-8.2'
* PHP-8.2:
  Handle indirect zvals in SplFixedArray::__serialize
  Fix GH-10908: Bus error with PDO Firebird on RPI with 64 bit kernel and 32 bit userland
2023-03-27 21:21:37 +02:00
Niels Dossche
e698938229 Handle indirect zvals in SplFixedArray::__serialize
Closes GH-10925.
2023-03-27 21:02:29 +02:00
Niels Dossche
bb9480a8be Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-10907: Unable to serialize processed SplFixedArrays in PHP 8.2.4
  Fix GH-8979: Possible Memory Leak with SSL-enabled MySQL connections
2023-03-24 18:09:05 +01:00
Niels Dossche
a082696699 Fix GH-10907: Unable to serialize processed SplFixedArrays in PHP 8.2.4
The properties table can also contain numeric entries after a rebuild of
the table based on the array. Since the array can only contain numeric
entries, and the properties table can contain a mix of both, we'll add
the numeric entries from the array and only the string entries from the
properties table. To implement this we simply check if the key from the
properties table is a string.

Closes GH-10921.
2023-03-24 18:08:32 +01:00
Marcos Marcolin
9004725367
chore: standardize the visibility of functions. (#10708)
Co-authored-by: Marcos Marcolin <marcos@ixcsoft.com.br>
2023-02-26 14:08:33 +00:00
Marcos Marcolin
641fe23e3a
Improve illegal offset error messages (#10504)
Co-authored-by: Marcos Marcolin <marcos@ixcsoft.com.br>
2023-02-08 12:11:41 +00:00
Tyson Andre
c4ecd82f93
Make inspecting SplFixedArray instances more memory efficient/consistent, change print_r null props handling (#9757)
* Make handling of SplFixedArray properties more consistent

Create a brand new reference counted array every time in SplFixedArray
to be freed by the callers (or return null).
Switch from overriding `get_properties` to overriding `get_properties_for` handler

* Print objects with null hash table like others in print_r

Noticed when working on subsequent commits for SplFixedArray.
Make whether zend_get_properties_for returns null or an empty array
invisible to the end user - it would be always be a non-null array for
user-defined classes.
Always print newlines with `\n\s*(\n\s*)` after objects

Noticed when working on SplFixedArray changes, e.g. in
ext/spl/tests/SplFixedArray__construct_param_null.phpt
2022-10-24 08:33:25 -04:00
Nikita Popov
1d28a7be24 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix serialization of empty SplFixedArray
2022-09-15 22:36:40 +02:00
Nikita Popov
70ad93dd6e Fix serialization of empty SplFixedArray
Avoid null pointer deref.
2022-09-15 22:36:19 +02:00
Bob Weinand
d1fc0017c9 Revert "Fix compilation on MacOS"
This reverts commit 800c6672e5.

Reverted along with a01dd9feda.
2022-09-14 11:28:06 +02:00
Bob Weinand
800c6672e5 Fix compilation on MacOS
memrchr has an always available equivalent under the name of zend_memrchr.

Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
2022-08-31 16:45:27 +02:00
Máté Kocsis
adb45a63c0
Fix GH-9186 @strict-properties can be bypassed using unserialization (#9354)
* Emit deprecation warnings when adding dynamic properties to classes during unserialization - this will become an Error in php 9.0.
  (Adding dynamic properties in other contexts was already a deprecation warning - the use case of unserialization was overlooked)
* Throw an error when attempting to add a dynamic property to a `readonly` class when unserializing
* Add new serialization methods `__serialize`/`__unserialize` for SplFixedArray to avoid creating deprecated dynamic
  properties that would then be added to the backing fixed-size array
* Don't add named dynamic/declared properties (e.g. $obj->foo) of SplFixedArray to the backing array when unserializing
* Update tests to declare properties or to expect the deprecation warning
* Add news entry

Co-authored-by: Tyson Andre <tysonandre775@hotmail.com>
2022-08-30 07:46:32 -04:00
Ilija Tovilo
3b92a96610
Convert return type of various object handlers from int to zend_result (#8755) 2022-06-26 01:00:19 +02:00
George Peter Banyard
cfc38a6014
SPL: minor refactoring (#8341)
Use more appropriate types and return macros
2022-04-13 20:34:23 +01:00
Tyson Andre
b50315e73c Merge branch 'PHP-8.1'
Conflicts:
	ext/spl/tests/fixedarray_023.phpt
	ext/spl/spl_fixedarray.c (fix compile error)
2022-02-23 19:52:59 -05:00
Tyson Andre
5d907dfcee Merge branch 'PHP-8.0' into PHP-8.1 2022-02-23 19:26:24 -05:00
Tyson Andre
cd1c6f0b81
Fixes infinite recursion introduced by patch to SplFixedArray (#8105)
Closes GH-8079

Track whether the spl_fixedarray was modified since the last call to
get_properties
2022-02-23 19:23:00 -05:00