Commit graph

322 commits

Author SHA1 Message Date
Niels Dossche
a142f10aa1
Don't evaluate GMP comparison multiple times (#18321)
ZEND_THREEWAY_COMPARE evaluates its operands multiple times.
2025-04-14 11:19:14 +02:00
Gina Peter Banyard
c5edf94003
ext/gmp: Use zend_result type instead of int type 2025-04-11 22:53:20 +01:00
David CARLIER
05a155782b
ext/gmp: use zend_object_alloc instead to initialize GMP instances. (#17822) 2025-02-15 19:09:36 +00:00
Gina Peter Banyard
f8547b1d97 ext/gmp: Check for valid bit index/start consistently 2024-11-29 16:55:38 +00:00
David Carlier
424ba0f2ff Finish reverting gmp_pow overflow check 2024-11-27 23:34:48 +00:00
Niels Dossche
0833f2376e Use specialised zend_new_pair() function to return array pairs in gmp
This is a bit faster, and more readable.
2024-11-27 23:34:23 +00:00
Gina Peter Banyard
eaffa1150a ext/gmp: Compare object CE to gmp_ce directly
The GMP class is now final, therefore we don't need to use the instanceof function.
2024-11-27 19:08:50 +00:00
Gina Peter Banyard
313886e22b ext/gmp: Refactor op overloading for ZEND_BW_NOT
The only way for the do_operation object handler to be called for unary operations is when the zval is an object.
Therefore we know we have a zval of the correct type and there is no need to check for it.
2024-11-27 19:08:50 +00:00
Gina Peter Banyard
0800c68be6 ext/gmp: Remove redundant parenthesis 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
0b5b9472ff Normalize gmp_cmp() to -1/0/+1 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
601f6cd54d ext/gmp: Use new parsing mechanism in comparison operator overloading
This removes the last usages of the old GMP_FETCH API
2024-11-27 15:10:49 +00:00
Gina Peter Banyard
92ff44da12 ext/gmp: Use new parsing API in shift helper 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
0831608c8b ext/gmp: Add weak mode support for parsing 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
27044108d3 ext/gmp: Convert GMP operator overloading to new parsing mechanism 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
8b2cd8fd2d ext/gmp: Start refactoring operator overloading to use new parsing mechanism 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
2681da4531 ext/gmp: Refactor gmp_fact() to use new ZPP specifier
Not sure it is the best approach to do this one however
2024-11-27 15:10:49 +00:00
Gina Peter Banyard
5199904c57 ext/gmp: Remove now unused FETCH_GMP_ZVAL_DEP_DEP macro 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
fabfb6fe00 ext/gmp: Refactor gmp_divexact() and gmp_mod() to use custom ZPP specifier 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
8e9b944a83 ext/gmp: Refactor gmp_div_(q|r)() to use new ZPP specifier 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
1f5ba59b61 ext/gmp: Refactor gmp_div_qr() to use new ZPP specifier 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
376c148fb8 ext/gmp: Refactor gmp_random_range() to use new ZPP specifier 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
3964efd729 ext/gmp: Use new specifier for gmp_cmp()
It seems that this also now normalizes the return value
2024-11-27 15:10:49 +00:00
Gina Peter Banyard
1e3876039f ext/gmp: Refactor generation of some binary GMP functions 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
a9e2a96e69 ext/gmp: Refactor generation of unary GMP functions 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
10003a3f5d ext/gmp: Use new custom ZPP specifier 2024-11-27 15:10:49 +00:00
Gina Peter Banyard
290f69662a ext/gmp: Create custom Fast ZPP specifier to parse into mpz_ptr
We define module globals to be used with ZPP, which also avoids excessive initializing and clearing of variables,
something recommended by the GMP documentation.
2024-11-27 15:10:49 +00:00
Gina Peter Banyard
2313a465b6 Inline gmp_unary_opl() as it was only used once
That use being gmp_popcount()
2024-11-27 15:10:49 +00:00
Gina Peter Banyard
9ee1265e0c ext/gmp: Use Fast ZPP for GMP functions 2024-11-27 15:10:49 +00:00
Christoph M. Becker
bd6b9df5dd
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-16890: array_sum() with GMP can loose precision (LLP64)
2024-11-25 12:55:58 +01:00
Christoph M. Becker
cfcf5cfde8
Fix GH-16890: array_sum() with GMP can loose precision (LLP64)
We must use `mpz_fits_si_p()` instead of `mpz_fits_slong_p()` since the
latter is not suitable for LLP64 data models.

libgmp, however, does not define `mpz_fits_si_p()` (which is an mpir
addition), so we use `mpz_fits_slong_p()` there which should be fine.

Closes GH-16891.
2024-11-25 12:55:09 +01:00
Gina Peter Banyard
9e2367f29f
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  ext/gmp: Fix segfault when null is encountered on an overloaded operator
  ext/gmp: Add behavioural tests for operator overloading
2024-11-02 17:41:27 +00:00
Gina Peter Banyard
65d42342a1
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  ext/gmp: Fix segfault when null is encountered on an overloaded operator
  ext/gmp: Add behavioural tests for operator overloading
2024-11-02 17:40:41 +00:00
Gina Peter Banyard
5253647500
ext/gmp: Fix segfault when null is encountered on an overloaded operator
And various other issues like inconsistent type errors

Closes GH-16015
2024-11-02 17:36:32 +00:00
David Carlier
9ee204f2e3
Merge branch 'PHP-8.3' into PHP-8.4 2024-10-25 14:05:16 +01:00
David Carlier
aa38bbed40
Merge branch 'PHP-8.2' into PHP-8.3 2024-10-25 14:05:07 +01:00
David Carlier
e0a0e216a9
ext/gmp: gmp_pow fix FPE with large values.
even without sanitizers, it is reproducible but with the following

```
<?php
$g = gmp_init(256);
var_dump(gmp_pow($g, PHP_INT_MAX));
```

we get this

```
AddressSanitizer:DEADLYSIGNAL
=================================================================
==286922==ERROR: AddressSanitizer: FPE on unknown address 0x03e8000460ca (pc 0x7faf6c69de5c bp 0x400000000000004 sp 0x7ffe9843c740 T0)
    #0 0x7faf6c69de5c in __pthread_kill_implementation nptl/pthread_kill.c:44
    #1 0x7faf6c649c81 in __GI_raise ../sysdeps/posix/raise.c:26
    #2 0x7faf6db9386c in __gmp_exception (/lib/x86_64-linux-gnu/libgmp.so.10+0xd86c) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #3 0x7faf6db938d3 in __gmp_overflow_in_mpz (/lib/x86_64-linux-gnu/libgmp.so.10+0xd8d3) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #4 0x7faf6dbac95c in __gmpz_realloc (/lib/x86_64-linux-gnu/libgmp.so.10+0x2695c) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #5 0x7faf6dba9038 in __gmpz_n_pow_ui (/lib/x86_64-linux-gnu/libgmp.so.10+0x23038) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #6 0x5565ae1ccd9f in zif_gmp_pow /home/dcarlier/Contribs/php-src/ext/gmp/gmp.c:1286
    #7 0x5565aee96ea9 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:1312
    #8 0x5565af144320 in execute_ex /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:56075
    #9 0x5565af160f07 in zend_execute /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:60439
    #10 0x5565aed6fafe in zend_execute_scripts /home/dcarlier/Contribs/php-src/Zend/zend.c:1842
    #11 0x5565aeae70a8 in php_execute_script /home/dcarlier/Contribs/php-src/main/main.c:2578
    #12 0x5565af532f4e in do_cli /home/dcarlier/Contribs/php-src/sapi/cli/php_cli.c:964
    #13 0x5565af535877 in main /home/dcarlier/Contribs/php-src/sapi/cli/php_cli.c:1334
    #14 0x7faf6c633d67 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #15 0x7faf6c633e24 in __libc_start_main_impl ../csu/libc-start.c:360
    #16 0x5565adc04040 in _start (/home/dcarlier/Contribs/php-src/sapi/cli/php+0x2604040) (BuildId: 949049955bdf8b7197390b1978a1dfc3ef6fdf38)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: FPE nptl/pthread_kill.c:44 in __pthread_kill_implementation
==286922==ABORTING
```

close GH-16384
2024-10-25 14:04:47 +01:00
David Carlier
bde23d0843
Merge branch 'PHP-8.3' into PHP-8.4 2024-10-20 21:44:04 +01:00
David Carlier
d6bac2f7e8
Merge branch 'PHP-8.2' into PHP-8.3 2024-10-20 21:43:51 +01:00
David Carlier
9ca68e037c
Fix GH-16501: gmp_random_bits overflow.
we do the same calculation in advance as mpz_realloc overflow check to
avoid abort.

close GH-16503
2024-10-20 21:43:29 +01:00
Christoph M. Becker
b1fbdd8a67
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-16411: gmp_export() can cause overflow
2024-10-15 15:59:53 +02:00
Christoph M. Becker
ea6f78eac8
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16411: gmp_export() can cause overflow
2024-10-15 15:58:21 +02:00
Christoph M. Becker
ab595c0764
Fix GH-16411: gmp_export() can cause overflow
We need not only to avoid the signed overflow while calculating
`bits_per_word` (reported issue), but also the unsigned overflow when
calculating `count`.  While the former has a fixed threshold, the
latter does not, since it also depends on the size in base 2.  Thus we
use a somewhat unconventional error message.

Closes GH-16418.
2024-10-15 15:57:17 +02:00
David Carlier
61d34b3ebc
Merge branch 'PHP-8.3' into PHP-8.4 2024-10-13 14:26:59 +01:00
David Carlier
47c81462b8
Merge branch 'PHP-8.2' into PHP-8.3 2024-10-13 14:26:47 +01:00
David Carlier
d70b7811b0
ext/gmp: gmp_pow fix FPE with large values.
even without sanitizers, it is reproducible but with the following

```
<?php
$g = gmp_init(256);
var_dump(gmp_pow($g, PHP_INT_MAX));
```

we get this

```
AddressSanitizer:DEADLYSIGNAL
=================================================================
==286922==ERROR: AddressSanitizer: FPE on unknown address 0x03e8000460ca (pc 0x7faf6c69de5c bp 0x400000000000004 sp 0x7ffe9843c740 T0)
    #0 0x7faf6c69de5c in __pthread_kill_implementation nptl/pthread_kill.c:44
    #1 0x7faf6c649c81 in __GI_raise ../sysdeps/posix/raise.c:26
    #2 0x7faf6db9386c in __gmp_exception (/lib/x86_64-linux-gnu/libgmp.so.10+0xd86c) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #3 0x7faf6db938d3 in __gmp_overflow_in_mpz (/lib/x86_64-linux-gnu/libgmp.so.10+0xd8d3) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #4 0x7faf6dbac95c in __gmpz_realloc (/lib/x86_64-linux-gnu/libgmp.so.10+0x2695c) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #5 0x7faf6dba9038 in __gmpz_n_pow_ui (/lib/x86_64-linux-gnu/libgmp.so.10+0x23038) (BuildId: 1af68a49fe041a5bb48a2915c3d47541f713bb38)
    #6 0x5565ae1ccd9f in zif_gmp_pow /home/dcarlier/Contribs/php-src/ext/gmp/gmp.c:1286
    #7 0x5565aee96ea9 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:1312
    #8 0x5565af144320 in execute_ex /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:56075
    #9 0x5565af160f07 in zend_execute /home/dcarlier/Contribs/php-src/Zend/zend_vm_execute.h:60439
    #10 0x5565aed6fafe in zend_execute_scripts /home/dcarlier/Contribs/php-src/Zend/zend.c:1842
    #11 0x5565aeae70a8 in php_execute_script /home/dcarlier/Contribs/php-src/main/main.c:2578
    #12 0x5565af532f4e in do_cli /home/dcarlier/Contribs/php-src/sapi/cli/php_cli.c:964
    #13 0x5565af535877 in main /home/dcarlier/Contribs/php-src/sapi/cli/php_cli.c:1334
    #14 0x7faf6c633d67 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #15 0x7faf6c633e24 in __libc_start_main_impl ../csu/libc-start.c:360
    #16 0x5565adc04040 in _start (/home/dcarlier/Contribs/php-src/sapi/cli/php+0x2604040) (BuildId: 949049955bdf8b7197390b1978a1dfc3ef6fdf38)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: FPE nptl/pthread_kill.c:44 in __pthread_kill_implementation
==286922==ABORTING
```
2024-10-13 14:26:29 +01:00
Gina Peter Banyard
b039af0120
ext/gmp: Use zend_result for type instead of int
Especially as we are widening a zend_result to int just to immediately narrow it back to a zend_result.
2024-09-23 10:55:51 +01:00
Gina Peter Bnayard
6114379ad2 ext/gmp: Refactor gmp_import_export_validate() 2024-08-12 17:30:10 +02:00
Gina Peter Bnayard
f8626638c4 ext/gmp: Use zend_string to GMP object function directly 2024-08-12 17:30:10 +02:00
Saki Takamachi
60afeb5537
RFC: Change GMP bool cast behavior (#15151)
Implementation of "RFC: Change GMP bool cast behavior"

https://wiki.php.net/rfc/fix_up_bcmath_number_class

Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
2024-07-31 00:41:49 +09: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