Commit graph

46 commits

Author SHA1 Message Date
Max Kellermann
d5c649b36b
zend_compiler, ...: use uint8_t instead of zend_uchar (#10621)
`zend_uchar` suggests that the value is an ASCII character, but here,
it's about very small integers.  This is misleading, so let's use a
C99 integer instead.

On all architectures currently supported by PHP, `zend_uchar` and
`uint8_t` are identical.  This change is only about code readability.
2023-02-23 14:56:54 +00:00
Niels Dossche
821fc55a68
Implement GH-9826: Make class_alias() work with internal classes (#10483)
We can't increase the refcount of internal classes during request time.
To work around this problem we simply don't refcount aliases anymore and
add a check in the destruction to skip aliases entirely.
There were also some checks which checked for an alias implicitly by
comparing the refcount, these have been replaced by checking the type of
the zval instead.
2023-02-22 11:47:32 +01:00
Niels Dossche
2e78c080c6
Improve the optimizer's check if a function is a prototype or not (#10467)
Currently, a function is considered a prototype if the function is not
marked as final. However, a class marked as final also make it
impossible for a function to be overridden. Therefore, we know in this
case too that the function is not a prototype.
This allows the type inference algorithm to determine some types more
precisely, and can allow for more optimizations of the instructions.
Additionally, place some computation of the flags in their respective
blocks as a micro-optimization.

Note: anonymous classes *can* be extended (see test
Zend/tests/anon/011.phpt). Therefore we don't optimize this case.
2023-02-21 00:34:47 +03:00
Max Kellermann
49c1e6eb33
Make various pointers const in Zend/ (#10608)
* Zend/zend_operators: pass const pointers to zend_is_identical()

* Zend/zend_operators: pass const pointers to zend_get_{long,double}()

* Zend/Optimizer/sccp: make pointers const

* Zend/Optimizer/scdf: make pointers const

* Zend/Optimizer/zend_worklist: make pointers const

* Zend/Optimizer/zend_optimizer: make pointers const

* Zend/zend_compile: make pointers const
2023-02-20 14:00:59 +00:00
Christoph M. Becker
2f4973fd88
Revert GH-10279
Cf. <https://github.com/php/php-src/pull/10220#issuecomment-1383739816>.

This reverts commit 45a128c9de.
This reverts commit 1eb71c3f15.
This reverts commit 492523a779.
This reverts commit c7a4633891.
This reverts commit 308adb915c.
This reverts commit cd27d5e07f.
This reverts commit c5933409b4.
This reverts commit 46371f4eb3.
This reverts commit 623e2e9fc6.
This reverts commit e7434c1247.
This reverts commit d28d323ca2.
This reverts commit 1a067b84ee.
This reverts commit a55c0c5fc3.
This reverts commit b5aeb3a4d4.
This reverts commit f061a035e4.
This reverts commit b088575119.
This reverts commit b1d48774a7.
This reverts commit 94f9a20ce6.
This reverts commit 4831e48708.
This reverts commit cd985de190.
This reverts commit 9521d21681.
This reverts commit d6136151e9.
2023-01-16 12:25:59 +01:00
Max Kellermann
1a067b84ee Zend/Optimizer/zend_optimizer: include cleanup 2023-01-12 15:12:45 +00:00
Bob Weinand
dc5475c191 Save previous observer on the VM stack
This avoids a possible significant performance penalty, when some leaf function was observed, deep in the stack.
As a side effect, we are not iterating over prev_execute_data anymore and thus, non-observed fake frames, possibly on stack, cannot have any impact on the observer anymore (especially within zend_observer_fcall_end_all).

Saving the previous observer happens now directly on the VM stack. If there is any observer, function frames are allocated an extra zval (the last temporary), which will, on observed frames, contain the previous observed frame address.
2022-08-04 17:16:27 +02:00
Arnaud Le Blanc
298c1ab92e Merge branch 'PHP-8.1' 2022-05-13 12:48:51 +02:00
Dmitry Stogov
27efd125ed Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix ISSET_ISEMPTY_VAR missoptimization
2022-04-25 13:31:40 +03:00
Dmitry Stogov
948ef10dd0 Fix ISSET_ISEMPTY_VAR missoptimization
This fixes oss-fuzz #46909
2022-04-25 13:31:01 +03:00
Dmitry Stogov
73514d6e4f Merge branch 'PHP-8.1'
* PHP-8.1:
  Reorder optimization passes to avoid miss-optimization
2022-04-25 13:12:03 +03:00
Dmitry Stogov
1aa5e9392d Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Reorder optimization passes to avoid miss-optimization
2022-04-25 13:09:31 +03:00
Nikita Popov
4543cd32ae Remove JMPZNZ opcode
While JMPZNZ can avoid execution of a separate JMP opcode in some
cases, it also prevents smart branch optimization, so creating
JMPZNZ may actually have a negative effect. It also adds additional
complexity for optimizations.

Drop JMPZNZ in favor of JMPZ+JMP or JMPNZ+JMP.

Closes GH-7857.
2022-01-10 22:07:10 +01:00
Nikita Popov
0698bf794f Add helper for convertion to CHECK_VAR/FREE/NOP
This is a recurring pattern whenever an instruction with an
operand is deleted.
2021-12-30 12:44:57 +01:00
Dmitry Stogov
a066b809de Skip abstract methods 2021-12-27 15:28:46 +03:00
Nikita Popov
e45653c089 Make sure SCCP can evaluate all functions pass1 can
Move evaluation of ini_get() into eval_special_func_call() and
use this helper both in pass1 and sccp.
2021-12-26 15:40:35 +01:00
Nikita Popov
1050edaef8 Extract special function evaluation from pass1
Pass1 handles a number of special functions that can be evaluated
under some circumstances. Move the core logic into a separate
helper, as I believe that SCCP should reuse this.
2021-12-26 15:40:35 +01:00
Nikita Popov
206d80e11a Reuse get_class_entry_from_op1() helper
Export and reuse this helper in places that fetch a class entry
from op1.
2021-12-25 22:18:50 +01:00
Nikita Popov
2cf93032ee Sink op_array scope case into get_class_entry()
This handles references to the current class through its name
rather than self (and for cases where is is not linked yet and
thus not covered by the context lookup). Rather than handling this
only for FETCH_CLASS_CONSTANT optimization, integrate this into
the generic get_class_entry() utility.
2021-12-25 21:51:29 +01:00
Nikita Popov
52676f2b7e Remove unnecessary wrapper function
This seems to date back to a time where zval_ptr_dtor was a macro
implicitly passing additional parameters.
2021-12-25 17:39:05 +01:00
Nikita Popov
0884048401 Don't exclude arrays from constant collection
These are supported as constants nowadays, so we can drop the
string check.

Also fix a potential leak, though I believe this doesn't matter in
current usage, as it will effectively be suppressed during persist.
2021-12-25 17:32:18 +01:00
Nikita Popov
92e7cf5962 Move FETCH_CLASS+INSTANCEOF special case out of update_op1_const()
The generic code was rejecting this to go into a special code path
in SCCP. We should directly do that in SCCP instead, to still allow
the generic (and valid) replacement.
2021-12-25 16:32:02 +01:00
Nikita Popov
4ad9dbbac9 Don't replace SEND opcodes with different by-ref behavior
update_op1_const() implements the right logic here -- these cannot
be replaced by different opcodes, as the by-ref passing behavior
is not the same.
2021-12-25 12:34:02 +01:00
Nikita Popov
46d1e503dd Remove redundant code in zend_optimizer_replace_by_const()
zend_optimizer_update_op1_const() already handles these cases.
2021-12-25 12:18:12 +01:00
Dmitry Stogov
e1c561508d Merge branch 'PHP-8.1'
* PHP-8.1:
  Optimize closures nested in other closures
2021-11-15 13:33:23 +03:00
Dmitry Stogov
f313b65acb Optimize closures nested in other closures 2021-11-15 13:32:42 +03:00
Dmitry Stogov
90b7bde615 Use more compact representation for packed arrays.
- for packed arrays we store just an array of zvals without keys.
- the elements of packed array are accessible throuf as ht->arPacked[i]
  instead of ht->arData[i]
- in addition to general ZEND_HASH_FOREACH_* macros, we introduced similar
  familied for packed (ZEND_HASH_PACKED_FORECH_*) and real hashes
  (ZEND_HASH_MAP_FOREACH_*)
- introduced an additional family of macros to access elements of array
  (packed or real hashes) ZEND_ARRAY_ELEMET_SIZE, ZEND_ARRAY_ELEMET_EX,
  ZEND_ARRAY_ELEMET, ZEND_ARRAY_NEXT_ELEMENT, ZEND_ARRAY_PREV_ELEMENT
- zend_hash_minmax() prototype was changed to compare only values

Because of smaller data set, this patch may show performance improvement
on some apps and benchmarks that use packed arrays. (~1% on PHP-Parser)

TODO:
    - sapi/phpdbg needs special support for packed arrays (WATCH_ON_BUCKET).
    - zend_hash_sort_ex() may require converting packed arrays to hash.
2021-11-03 15:18:26 +03:00
Nikita Popov
e7a5ec5fd9 Merge branch 'PHP-8.1'
* PHP-8.1:
  Handle operand replacement in JMP_NULL
2021-10-19 15:19:39 +02:00
Nikita Popov
1f19401ffa Handle operand replacement in JMP_NULL
In this case it's not sufficient to replace the JMP_NULL operand,
as it keeps the temporary alive and there may be more uses later.
Fix this by generalizing existing handling for other similar opcodes
like CASE/SWITCH and LIST_R.

Fixes oss-fuzz 5820123475214336.
2021-10-19 15:19:02 +02:00
Nikita Popov
6858ad123e Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix cache slot assignment for ASSIGN_OBJ_OP
2021-09-30 14:35:07 +02:00
Nikita Popov
5cdbfa897c Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix cache slot assignment for ASSIGN_OBJ_OP
2021-09-30 14:34:50 +02:00
George Peter Banyard
f345f6d529 Voidify zend_optimize_script()
It always returned 1

As a consequence voidify preload_optimize()
2021-09-21 11:35:53 +01:00
George Peter Banyard
278efe3b64 Voidify zend_build_call_graph()
It always returned SUCCESS
2021-09-21 11:35:53 +01:00
George Peter Banyard
53d5420d49 Use more appropriate types in Optimizer
Mainly using zend_result and bool instead of int
2021-09-21 11:35:53 +01:00
Nikita Popov
fb4405d357 Avoid accessing literal operand on nop
This would have create a NOP with a CONST operand ... probably
harmless in practice, but we should avoid it.
2021-09-15 14:44:54 +02:00
Patrick Allaert
aff365871a Fixed some spaces used instead of tabs 2021-06-29 11:30:26 +02:00
Nikita Popov
0d9269de75 Extract helper for fetching class entry in optimizer
This code is repeated a few time. Two occurrences additionally
contained checks for user classes in CG(class_table) with the
same file name, but as far as I know these should always be in
the script class_table, so I'm omitting the check here.
2021-06-18 10:48:48 +02:00
codinghuang
2f710f5bb2
Support custom passes in Optimizer 2021-06-06 08:10:11 +02:00
Nikita Popov
ba9c0b38cb Export zend_unary_op_produces_error() and use in optimizer
Don't repeat this logic, as it's going to become more complex.
2021-05-27 16:33:30 +02:00
Nikita Popov
446471b99b Remove error suppression from zend_optimizer_eval_binary_op()
All error conditions should be handled by
zend_binary_op_produces_error().

Pointed out by Girgias.
2021-05-18 14:45:55 +02:00
KsaR
01b3fc03c3
Update http->https in license (#6945)
1. Update: http://www.php.net/license/3_01.txt to https, as there is anyway server header "Location:" to https.
2. Update few license 3.0 to 3.01 as 3.0 states "php 5.1.1, 4.1.1, and earlier".
3. In some license comments is "at through the world-wide-web" while most is without "at", so deleted.
4. fixed indentation in some files before |
2021-05-06 12:16:35 +02:00
Nikita Popov
b82242a88d Remove unnecessary php.h includes from Zend/
The Zend/ directory really shouldn't be including php headers.
These particular includes are plain unnecessary.
2021-04-20 12:21:45 +02:00
George Peter Banyard
5caaf40b43
Introduce pseudo-keyword ZEND_FALLTHROUGH
And use it instead of comments
2021-04-07 00:46:29 +01:00
Nikita Popov
2d0e2733c8 Support prototypes in call graph
Even if we don't know the exact method being called, include it
in the call graph with the is_prototype flag. In particular, we
can still make use of return types from prototype methods, as
PHP 8 makes LSP violations a hard error.

Most other places are adjusted to skip calls with !is_prototype.
Maybe some of them would be fine, but ignoring them is conservative.
2021-03-19 10:49:15 +01:00
Nikita Popov
47a2e5c785 Reference dynamic functions through dynamic_defs
Currently, dynamically declared functions and closures are inserted
into the function table under a runtime definition key, and then later
possibly renamed. When opcache is not used and a file containing a
closure is repeatedly included, this leads to a very large memory leak,
as the no longer needed closure declarations will never be freed
(https://bugs.php.net/bug.php?id=76982).

With this patch, dynamic functions are instead stored in a
dynamic_func_defs member on the op_array, which opcodes reference
by index. When the parent op_array is destroyed, the dynamic_func_defs
it contains are also destroyed (unless they are stilled used elsewhere,
e.g. because they have been bound, or are used by a live closure). This
resolves the fundamental part of the leak, though doesn't completely
fix it yet due to some arena allocations.

The main non-obvious change here is to static variable handling:
We can't destroy static_variables_ptr in destroy_op_array, as e.g.
that would clear the static variables in a dynamic function when
the op_array containing it is destroyed. Static variable destruction
is separated out for this reason (we already do static variable
destruction separately for normal functions, so we only need to
handle main scripts).

Closes GH-5595.
2021-03-01 11:35:54 +01:00
Nikita Popov
83be073abe Move optimizer into core
This only moves the files, adjusts the build system, exports APIs
and does minor fixups to make sure the code builds.

This does not yet try to make the optimizer usable independently
of opcache.

Closes GH-6642.
2021-01-28 10:38:25 +01:00
Renamed from ext/opcache/Optimizer/zend_optimizer.c (Browse further)