Commit Graph

147 Commits

Author SHA1 Message Date
Ilija Tovilo
0b1d750d91
Allow arbitrary expressions in static variable initializer
Closes GH-9301
2023-05-24 20:17:31 +02:00
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
Ilija Tovilo
02bd52b5a8
Implement dynamic class const fetch
https://wiki.php.net/rfc/dynamic_class_constant_fetch

Closes GH-9793
2023-01-26 16:46:34 +01:00
Dmitry Stogov
233b659223 Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix memory leak
2022-02-01 16:26:29 +03:00
Dmitry Stogov
8e2406c59b Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix memory leak
2022-02-01 16:25:03 +03:00
Dmitry Stogov
a60a9b4a89 Fix memory leak
Fixes oss-fuzz #44222
2022-02-01 16:24:22 +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
b63e4cf727 Handle holes in zend_get_opcode_id()
Some opcodes may currently not be allocated.
2021-12-30 16:56:03 +01:00
Joe Watkins
d0b09a7be4 Add first-class callables
Support acquiring a Closure to a callable using the syntax
func(...), $obj->method(...), etc. This is essentially a
shortcut for Closure::fromCallable().

RFC: https://wiki.php.net/rfc/first_class_callable_syntax

Closes GH-7019.

Co-Authored-By: Nikita Popov <nikita.ppv@gmail.com>
2021-07-14 14:37:25 +02:00
Matt Brown
6cd0b48cac Implement never return type
The never type can be used to indicate that a function never
returns, for example because it always unwinds.

RFC: https://wiki.php.net/rfc/noreturn_type

Closes GH-6761.
2021-04-19 11:27:29 +02:00
twosee
cb8f39f247
Add zend_get_opcode_id() to get opcode id from name 2021-04-06 11:44:10 +08: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
3c68f38fda Restrict allowed usages of $GLOBALS
This restricts allowed usage of $GLOBALS, with the effect that
plain PHP arrays can no longer contain INDIRECT elements.

RFC: https://wiki.php.net/rfc/restrict_globals_usage

Closes GH-6487.
2021-01-06 12:46:24 +01:00
Nikita Popov
d92229d8c7 Implement named parameters
From an engine perspective, named parameters mainly add three
concepts:

 * The SEND_* opcodes now accept a CONST op2, which is the
   argument name. For now, it is looked up by linear scan and
   runtime cached.
 * This may leave UNDEF arguments on the stack. To avoid having
   to deal with them in other places, a CHECK_UNDEF_ARGS opcode
   is used to either replace them with defaults, or error.
 * For variadic functions, EX(extra_named_params) are collected
   and need to be freed based on ZEND_CALL_HAS_EXTRA_NAMED_PARAMS.

RFC: https://wiki.php.net/rfc/named_params

Closes GH-5357.
2020-07-31 15:53:36 +02:00
Ilija Tovilo
9bf119832d
Implement nullsafe ?-> operator
RFC: https://wiki.php.net/rfc/nullsafe_operator

Closes GH-5619.

Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>
2020-07-24 10:05:03 +02:00
Ilija Tovilo
9fa1d13301
Implement match expression
RFC: https://wiki.php.net/rfc/match_expression_v2

Closes GH-5371.
2020-07-09 23:52:17 +02:00
Nikita Popov
df79277de3 Revert "Fetch for read in nested property assignments"
This reverts commit bb43a3822e.

After thinking about this a bit more, this is now going to be
a complete solution for the "readonly properties" case, for example:

    unset($foo->readOnly->bar);

should also be legal and

    $foo->readOnly['bar'] = 42;

should also be legal if $foo->readOnly is not an array but an
ArrayAccess object.

I think it may be better to distinguish better on the BP_VAR flag
level. Reverting for now.
2020-03-18 14:54:43 +01:00
Nikita Popov
bb43a3822e Fetch for read in nested property assignments
$a->b->c = 'd';

is now compiled the same way as

    $b = $a->b;
    $b->c = 'd';

That is, we perform a read fetch on $a->b, rather than a write
fetch.

This is possible, because PHP 8 removed auto-vivification support
for objects, so $a->b->c = 'd' may no longer modify $a->b proper
(i.e. not counting interior mutability of the object).

Closes GH-5250.
2020-03-18 12:08:06 +01:00
Nikita Popov
d933591674 Add support for $obj::class
This allows $obj::class, which gives the same result as get_class($obj).
Anything other than an object results in TypeError.

RFC: https://wiki.php.net/rfc/class_name_literal_on_object

Closes GH-5065.
2020-02-11 12:16:30 +01:00
Dmitry Stogov
77bf144d52 Inline hot parts of bitwise instructions into hybrid VM 2020-01-14 13:47:10 +03:00
Nikita Popov
9c9a833f9c Actually remove the YIELD key specialization
I only adjusted the code before, without switching to TMPVAR.
2019-10-09 14:11:07 +02:00
Nikita Popov
da3b245715 Reduce YIELD_FROM specialization
Generator delegation is a complex compound operation, it does not
make a lot of sense to optimize refcounting here to this degree.
2019-10-09 13:53:07 +02:00
Nikita Popov
2aba10be4a Reduce ZEND_THROW specialization
Throwing is very expensive due to the need of gathering the backtrace,
so it makes little sense to optimize refcounting to this degree.
2019-10-09 13:17:25 +02:00
Dmitry Stogov
6e226c188c Change ZEND_RECV and ZEND_RECV_VARIADIC to use extended_value for cache slot (instead of op2), to be consistent with ZEND_RECV_INIT. 2019-09-23 22:28:56 +03:00
Dmitry Stogov
853b426ecf Avoid over-specialization 2019-07-24 19:51:56 +03:00
Nikita Popov
1eb706179f Avoid references in TMP var
Make sure we deref the OBJ_IS result, because we store it in a TMP
var, which is not allowed to contain references and will cause
assertion failures in the unspecialized VM.

This also partially reverts fd463a9a60,
which merged the TMP and VAR specializations of COALESCE to work
around this bug.

An alternative would be to change the result type of OBJ_IS back
to VAR.
2019-07-24 10:07:26 +02:00
Dmitry Stogov
d5943f5a11 Use run-time cache to avoid repeatable hash lookups when creating anonymous functions and classes 2019-07-19 10:43:49 +03:00
Dmitry Stogov
b065fbde19 ZEND_DECLARE_ANON_CLASS doesn't need to skip anything now. It's immediatelly followed by ZEND_NEW. 2019-07-19 10:09:26 +03:00
Dmitry Stogov
ef1a1a0698 Separate "cold" parts of comparison instructions 2019-07-11 20:44:39 +03:00
Dmitry Stogov
be94c0c3c6 Separate "cold" parts of binary op instructions 2019-07-11 18:23:08 +03:00
Dmitry Stogov
1305d9c0d2 Fixed opcode description 2019-07-08 11:20:46 +03:00
Dmitry Stogov
48ca5a1e17 Replace ZEND_ASSIGN_ADD (and others) by ZEND_ASSIGN_OP, ZEND_ASSIGN_DIM_OP, ZEND_ASSGIN_OBJ_OP and ZEND_ASSIGN_STATIC_PROP_OP 2019-07-05 12:03:25 +03:00
Dmitry Stogov
ef05eab432 Improve zend_binary_assign_op helpers.
Reorder opcode numbers to make ADD-POW and ASSIGN_ADD-ASSIGN_POW opcodes sequencional.
2019-07-04 17:25:43 +03:00
Dmitry Stogov
56b8b165f8 Optimization of INC/DEC helpers 2019-07-03 10:33:03 +03:00
Dmitry Stogov
e8f1f70101 Reduce overhead of delayed early binding 2019-06-25 14:20:41 +03:00
Nikita Popov
89b2d88659 Register class before fetching parent
We want the class declaration to be available while compiling the
parent class.
2019-06-11 13:09:33 +02:00
Dmitry Stogov
1814308e49 Don't specialize "cold" handlers 2019-05-31 00:31:57 +03:00
Dmitry Stogov
4e567ed1da Removed useless specialization. Specialized handlers called not specialized helpers. 2019-05-31 00:15:25 +03:00
CHU Zhaowei
e829d08729 Implement spread operator in arrays
RFC: https://wiki.php.net/rfc/spread_operator_for_array

Closes GH-3640.
2019-05-13 14:42:43 +02:00
Dmitry Stogov
d4bef4ce7b Avoid useless code duplication, because of unused specialization 2019-02-15 17:49:39 +03:00
Zeev Suraski
a81202ac49 Adios, yearly copyright ranges 2019-01-30 11:48:28 +01:00
Nikita Popov
a50198d0fe Implement ??= operator
RFC: https://wiki.php.net/rfc/null_coalesce_equal_operator

$a ??= $b is $a ?? ($a = $b), with the difference that $a is only
evaluated once, to the degree that this is possible. In particular
in $a[foo()] ?? $b function foo() is only ever called once.
However, the variable access themselves will be reevaluated.
2019-01-22 11:12:04 +01:00
Nikita Popov
e219ec144e Implement typed properties
RFC: https://wiki.php.net/rfc/typed_properties_v2

This is a squash of PR #3734, which is a squash of PR #3313.

Co-authored-by: Bob Weinand <bobwei9@hotmail.com>
Co-authored-by: Joe Watkins <krakjoe@php.net>
Co-authored-by: Dmitry Stogov <dmitry@zend.com>
2019-01-11 15:49:06 +01:00
Michael Moravec
f5044a12dd Implement ZEND_ARRAY_KEY_EXISTS opcode to speed up array_key_exists() 2018-12-26 23:54:11 +03:00
Zeev Suraski
54dc07f3dc Update email addresses. We're still @Zend, but future proofing it... 2018-11-01 17:20:07 +02:00
Dmitry Stogov
d140df58e6 Keep information about unresolved interfaces in zend_class_entry->interface_names.
Move interface implementation code into ZEND_DECLARE_*CLASS opcodes.
Remove ZEND_ADD_INTERFACE and ZEND_VERIFY_ABSTRACT_CLASS opcodes.
2018-08-23 17:16:28 +03:00
Dmitry Stogov
67397970b2 Replace zend_class_entry->traits by persistent zend_class_entry->trait_names.
Move trait binding code into ZEND_DECLARE_*CLASS opcodes.
Remove ZEND_ADD_TRIAIT and ZEND_BIND_TRAITS opcodes.
2018-08-23 02:02:26 +03:00
Dmitry Stogov
6d88e1ccd6 Don't use second operand of BIND_STATIC instruction. 2018-08-21 12:22:04 +03:00
Xinchen Hui
fd463a9a60 Fixed bug #76752 (Crash in ZEND_COALESCE_SPEC_TMP_HANDLER - assertion in _get_zval_ptr_tmp failed). 2018-08-17 12:19:31 +08:00
Dmitry Stogov
f950128cd6 Encode parent class name as IS_CONST operand in DECLARE_INHERITED_CLASS and DECLARE_ANON_INHERITED_CLASS opcodes (eliminate FETCH_CLAS
S opcode).
2018-07-25 13:40:47 +03:00