php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Segfault on one-arm match statement

Open Firehed opened this issue 4 years ago • 6 comments

Description

When running the following code with the built-in webserver, I experience a segfault:

https://3v4l.org/KrlXs

With one additional match arm (anything seems fine), it no longer occurs. I do not experience a segfault when running on the command line. I have not yet tested under other server mechanisms (e.g. fpm)

As far as I can tell, it's specific to the array destructuring; I've been able to reproduce with a script as small as this:

$ cat repro.php
<?php

[$a, $b] = match (true) {
    true => ['three', 'whatever'],
};

var_dump($a, $b);


 $ php repro.php
string(5) "three"
string(8) "whatever"


 $ php -S 0.0.0.0:12345 repro.php
[Wed Dec 29 12:02:49 2021] PHP 8.1.0 Development Server (http://0.0.0.0:12345) started
[Wed Dec 29 12:02:50 2021] 127.0.0.1:58865 Accepted
Segmentation fault: 11

(note my local machine is on 8.1.0 but the same happens in 8.1.1)

PHP Version

8.1.1

Operating System

macOS, Alpine Linux (docker)

Firehed avatar Dec 29 '21 20:12 Firehed

Quick update: if I have a more complex value in the match arm, it also doesn't seem to occur.

This is fine:

[$key, $value] = match (true) {
  $entity instanceof Foo => ['foo', ['id' => $entity->getId()]],
};

This is not, and continues to segfault the built-in webserver:

[$key, $value] = match (true) {
  $entity instanceof Foo => ['foo', []],
};

Firehed avatar Dec 29 '21 20:12 Firehed

Any chance you can get a backtrace?

damianwadley avatar Dec 29 '21 22:12 damianwadley

Is this what you're looking for?

$ lldb
(lldb) target create php
Current executable set to 'php' (x86_64).
(lldb) settings set -- target.run-args "-S" "0.0.0.0:12345" "repro.php"
(lldb) run
Process 33410 launched: '/usr/local/bin/php' (x86_64)
[Wed Dec 29 14:48:56 2021] PHP 8.1.0 Development Server (http://0.0.0.0:12345) started
[Wed Dec 29 14:49:00 2021] 127.0.0.1:62525 Accepted
Process 33410 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8)
    frame #0: 0x00000001003e311d php`sccp_visit_instr + 2969
php`sccp_visit_instr:
->  0x1003e311d <+2969>: movb   0x8(%r13), %dl
    0x1003e3121 <+2973>: cmpb   $-0x1, %dl
    0x1003e3124 <+2976>: je     0x1003e32c4               ; <+3392>
    0x1003e312a <+2982>: movb   0x8(%r12), %cl
Target 0: (php) stopped.
(lldb)

I tried to follow what was done here

Firehed avatar Dec 29 '21 22:12 Firehed

I can reproduce on CLI with master branch. Output

In function ::{main} (before dfa):
var op1 of 2 (ZEND_FETCH_LIST_R) does not use/def an ssa var
var op1 of 4 (ZEND_FREE) does not use/def an ssa var

$_main:
     ; (lines=10, args=0, vars=2, tmps=2, ssa_vars=6, no_loops)
     ; (at SSA integrity verification)
     ; C:\php-sdk\phpdev\vs16\x64\gh7854.php:1-8
     ; return  [long] RANGE[1..1]
     ; #0.CV0($a) [undef, ref, any]
     ; #1.CV1($b) [undef, ref, any]
BB0:
     ; start exit lines=[0-9]
     ; level=0
0000 #2.V3 [ind, null, string] = FETCH_LIST_R array(...) int(0)
0001 ASSIGN #0.CV0($a) [undef, ref, any] -> #3.CV0($a) [ind, ref, any] #2.V3 [ind, null, string]
0002 #4.V3 [ind, ref, any] = FETCH_LIST_R T2 int(1)
0003 ASSIGN #1.CV1($b) [undef, ref, any] -> #5.CV1($b) [ind, ref, any] #4.V3 [ind, ref, any]
0004 FREE T2
0005 INIT_FCALL 2 112 string("var_dump")
0006 SEND_VAR #3.CV0($a) [ind, ref, any] 1
0007 SEND_VAR #5.CV1($b) [ind, ref, any] 2
0008 DO_ICALL
0009 RETURN int(1)
Assertion failed: 0 && "SSA integrity verification failed", file C:\php-sdk\phpdev\vs16\x64\php-src\Zend\Optimizer\ssa_integrity.c, line 398

With PHP-8.1 on CLI I get a segfault:

php8_debug.dll!zval_get_type(const _zval_struct * pz) Line 556 (c:\php-sdk\phpdev\vs16\x64\php-src-8.1\Zend\zend_types.h:556)
php8_debug.dll!sccp_visit_instr(_scdf_ctx * scdf, _zend_op * opline, _zend_ssa_op * ssa_op) Line 1655 (c:\php-sdk\phpdev\vs16\x64\php-src-8.1\Zend\Optimizer\sccp.c:1655)
php8_debug.dll!scdf_solve(_scdf_ctx * scdf, const char * name) Line 168 (c:\php-sdk\phpdev\vs16\x64\php-src-8.1\Zend\Optimizer\scdf.c:168)
php8_debug.dll!sccp_optimize_op_array(_zend_optimizer_ctx * ctx, _zend_op_array * op_array, _zend_ssa * ssa, _zend_call_info * * call_map) Line 2501 (c:\php-sdk\phpdev\vs16\x64\php-src-8.1\Zend\Optimizer\sccp.c:2501)
php8_debug.dll!zend_dfa_optimize_op_array(_zend_op_array * op_array, _zend_optimizer_ctx * ctx, _zend_ssa * ssa, _zend_call_info * * call_map) Line 1127 (c:\php-sdk\phpdev\vs16\x64\php-src-8.1\Zend\Optimizer\dfa_pass.c:1127)
php8_debug.dll!zend_optimize_script(_zend_script * script, __int64 optimization_level, __int64 debug_level) Line 1458 (c:\php-sdk\phpdev\vs16\x64\php-src-8.1\Zend\Optimizer\zend_optimizer.c:1458)

No problem with PHP-8.0.

cmb69 avatar Dec 29 '21 23:12 cmb69

This is not particularly related to match statements;

[$a, $b] = true ? ['three', 'whatever'] : [];
var_dump($a, $b);

exhibits the same behavior. The regression has been introduced with 1f19401ffa0edb2b69f5f1bd229776ee34cf693e.

cmb69 avatar Jan 02 '22 22:01 cmb69

This issue should have been fixed in the meantime (PHP 8.1.5).

cmb69 avatar Apr 21 '22 09:04 cmb69