What is EXT_NOP and EXT_STMT, PHP Opcode - php

I'm trying to understand more deep and php internals by just executing native php functions against to my implementations.
But in every opcode dump I see these two following opcodes :
EXT_NOP : http://php.net/manual/tr/internals2.opcodes.ext-nop.php
EXT_STMT : http://php.net/manual/tr/internals2.opcodes.ext-stmt.php
as you see in docs there is no detailed explanation.
Even in following example which given in the doc my dumps are different than the doc's spesification. I really want to know why are these two stands in every dump ? What is their functionality ?
<?php
/*
* no operation
* opcode number: 0
*/
function A(){};
?>
Env Spesification :
LXC
Linux web 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u5 (2015-10-09) x86_64 GNU/Linux
PHP 5.6.15-1~dotdeb+7.1 (cli) (built: Nov 3 2015 16:29:58)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
OpCode Dump :
➜ php -d vld.active=1 -d vld.execute=0 -f nop.php
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename: /root/web/www/optest/nop.php
function name: (null)
number of ops: 5
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > EXT_STMT
1 NOP
2 NOP
4 3 EXT_STMT
4 > RETURN 1
branch: # 0; line: 2- 4; sop: 0; eop: 4; out1: -2
path #1: 0,
Function a:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename: /root/web/www/optest/nop.php
function name: A
number of ops: 3
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > EXT_NOP
1 EXT_STMT
2 > RETURN null
branch: # 0; line: 2- 2; sop: 0; eop: 2; out1: -2
path #1: 0,
End of function a

EXT_NOP is used in the cases where previously there was something to do (ie a function declaration), but the engine has internally already taken care of this, and replaced the original opcode with EXT_NOP. NOP stands for "no operation". NOP is slightly different from EXT_NOP, as it's generated at different times, but it does the same thing: Nothing.
EXT_STMT is created in between statements, and allows debuggers (such as Xdebug) to stop in safe locations. Xdebug uses a "statement handler" (https://github.com/derickr/xdebug/blob/master/xdebug.c#L2534) to hook into the Zend Engine. The Zend Engine calls this handler for each EXT_STMT opcode it encounters.

Related

ip2long("012.012.012.012") returns false on Linux

Suppose there are leading zeros in IPv4 address representation such as 012.012.012.012.
To suppress the leading zeros, I simply write the following code, and it works as I expected on my Mac:
% php -r 'var_dump(long2ip(ip2long("012.012.012.012")));'
Command line code:1:
string(11) "12.12.12.12"
% php --version
PHP 7.3.25 (cli) (built: Dec 25 2020 22:03:38) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.25, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.25, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans
But this simple piece of code doesn't work on CentOS 7, it seems like ip2long("012.012.012.012") returns false value:
$ php -r 'var_dump(long2ip(ip2long("012.012.012.012")));'
string(7) "0.0.0.0"
$ php --version
PHP 7.3.25 (cli) (built: Nov 24 2020 11:10:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.25, Copyright (c) 1998-2018 Zend Technologies
$ cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
Both PHP versions are identical and I would expect them to return exact same values. I also read the ip2long docs and tried to find if there is any special options/configurations for this behaviour, but no luck.
Could someone lead me in the right direction?
What makes them different? And what should I do?
OK, I found the culprit. Different implementation of inet_pton(3) between LLVM and GCC leads this behaviour, as Russ-san commented. There was a bug report about the exact same problem 6 years ago. It seems PHP v5.2.10 had started using inet_pton instead of inet_addr according to this patch, that's why older versions of PHP treat each chunks as octal.
test.c
#include <stdio.h>
#include <arpa/inet.h>
#define INADDR "012.012.012.012"
int main() {
struct in_addr inaddr;
if (inet_pton(AF_INET, INADDR, &inaddr) == 0) {
printf("Invalid: %s\n", INADDR);
}
else {
printf("Valid: %s\n", INADDR);
}
return 0;
}
Mac
% cc test.cc && ./a.out
Valid: 012.012.012.012
Linux
$ cc test.cc && ./a.out
Invalid: 012.012.012.012
I gave up using ip2long / long2ip built-in functions and now trying to use https://github.com/mlocati/ip-lib
>>> \IPLib\Address\IPv4::fromString("1.2.3.12")->toString(true);
=> "001.002.003.012"
>>> \IPLib\Address\IPv4::fromString("001.002.003.012")->toString();
=> "1.2.3.12"
No problems so far.

Assert is not working in PHP. So simple. What am I doing wrong?

It's like assert isn't even being called. I am confused.
The version
php -v
PHP 7.0.11-1+deb.sury.org~xenial+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.11-1+deb.sury.org~xenial+1, Copyright (c) 1999-2016, by Zend Technologies
The script:
x.php
<?php
print ("Hello\n");
assert_options(ASSERT_ACTIVE,true);
assert_options(ASSERT_BAIL,true);
assert(false);
assert(true);
print ("Bye\n");
when I run it
php x.php
Hello
Bye
I would have expected the program to terminate with an exception. Am I going crazy?
It looks like assertions are OFF out of the box on 7.0. In my php.ini file zend.assertions was set to -1, which means they are ignored. I have changed the setting to 1.
[Assertion]
; Switch whether to compile assertions at all (to have no overhead at run-time)
; -1: Do not compile at all
; 0: Jump over assertion at run-time
; 1: Execute assertions
; Changing from or to a negative value is only possible in php.ini! (For turning assertions on and off at run-time, see assert.active, when zend.assertions = 1)
; Default Value: 1
; Development Value: 1
; Production Value: -1
; http://php.net/zend.assertions
zend.assertions = 1
The script now works as expected.
php x.php
Hello
PHP Warning: assert(): assert(false) failed in /home/ubuntu/code/x/test/x.php on line 8

PHP vld shows file output instead of opcode

I'm trying to use vld to view opcode of a php file
prep
I've installed vld with:
pecl install channel://pecl.php.net/vld-0.12.0
To get familiar with VLD, I'm trying to compare to php files (echo1 and echo2)
echo1.php
<?php
echo "Hello"." "."World";
echo2.php
<?php
echo "Hello"," ","World";
phpinfo() shows that vld seems to be enabled:
vld support enabled
Directive Local Value Master Value
vld.active 0 0
vld.col_sep
vld.dump_paths 1 1
vld.execute 1 1
vld.format 0 0
vld.save_dir /tmp /tmp
vld.save_paths 0 0
vld.skip_append 0 0
vld.skip_prepend 0 0
vld.verbosity 1 1
problem
running php files shows output instead of opcode
# php -dvld.active=1 -f echo1.php
Hello World
# php -dvld.active=1 -dvld.execute=0 -f echo1.php
Hello World
# php -dvld.active=1 -f echo2.php
Hello World
# php -dvld.active=1 -dvld.execute=0 -f echo2.php
Hello World
Obviously I'm missing something :)
versions running
php version
# php -v
PHP 5.4.4-14+deb7u14 (cli) (built: Aug 21 2014 08:36:44)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
Running Debian in Virtualbox with Apache 2.2.22
After updating to PHP 5.6.2 I gave it another go and it is running :)
php version
# php -v
PHP 5.6.2 (cli) (built: Oct 17 2014 07:22:10)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
install pear
yum install php56w-pear
installing vld
pecl install vld
add extension to /etc/php.ini (or in file in /etc/php.d/__.ini)
#/etc/php.d/vld.ini
extension=vld.so
restart httpd service and test again (fingers crossed)
# php -dvld.active=1 -f echo1.php
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename: /var/www/html/echo1.php
function name: (null)
number of ops: 4
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > CONCAT ~0 'Hello', '+'
1 CONCAT ~1 ~0, 'World'
2 ECHO ~1
3 3 > RETURN 1
branch: # 0; line: 2- 3; sop: 0; eop: 3; out1: -2
path #1: 0,
Hello World
Boom Baby!
I don't know why, but it is working as expected :)
Now the OPCODE adventures can begin (also a love story)
You might be add extension.so to wrong php.ini file
You might be add the extension to php.ini that use by your webserver instead of cli version
What you can do is run locate php.ini, and choose the cli version of php.ini and add extension.so to that file
Normally cli version of php.ini located at /etc/php5/cli/php.ini
Dont forget to restart your console too

Is there a PHP setting that sets whether you can index the result of a function?

I have two servers. They are both running php 5.3.3. This code works on one server and returns a syntax error on the other. Is there a php ini setting that affects this behaviour? I can't find anything related in the PHP documentation, but I may be looking in the wrong place.
Server 1
> php -v
PHP 5.3.3 (cli) (built: Sep 23 2010 14:15:16)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.0.3, Copyright (c) 2002-2007, by Derick Rethans
php > echo explode(" ", " foo ")[1];
foo
Server 2
> php -v
PHP 5.3.3 (cli) (built: Jan 31 2011 15:57:29)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
php > echo explode(" ", " foo ")[1];
Parse error: syntax error, unexpected '[', expecting ',' or ';' in php shell code on line 1
Another idea: PHP on both servers is custom-compiled, so it could also be a different compile flag.
No.
PHP doesn't support this syntax. It's on the trunk, but not yet released (as of PHP 5.3.3).
I have no idea how it's working on your first server, but perhaps this "Xdebug" is making a difference?
Aha! I figured it out.
We installed Facebook's XHP to profile our development server. This syntax (which is quite elegant) was added in in the PHP module. Here's a diff of the php.ini file between server 1 and 2:
> ; XHP https://github.com/facebook/xhp/wiki/Building-XHP
> extension=xhp.so
> ; adds support for the [] operator on the return value of a function
> xhp.idx_expr = 1
> ; Tracking errors in XHP applications is very difficult without annotations.
> xhp.include_debug = 1
I like this syntax, so I'll probably install XHP on the other server. Thanks for the help Michas for suggesting I diff the ini files.

How to compare large number in PHP?

I confuse that how I aseert quite large number by PHPUnit.
I wrote fibonacci number. But in case 100, I wrote below.
http://github.com/sanemat/kata-fibonacci-phpunit/commit/b6b2945d2eff1b95e7e25b8be8c7bff11098d44d
I expect this return TRUE.
It failed.
http://www.php.net/manual/en/language.types.float.php
I understood 'never compare for equality.' and so how to aseert it?
At the end, I compare with to_string each other.
How to compare large number in PHP ?
http://github.com/sanemat/kata-fibonacci-phpunit/commit/70a0e47bc0737be47da8fe8726634edd851d940d
I feel it as bad smell. But I do not know better way.
Please tell me how do you assert it?
====
php > $a = 354224848179261915075;
php > echo $a;
3.5422484817926E+20
php > $b = 3.5422484817926E+20;
php > echo $b;
3.5422484817926E+20
php > echo ($a == $b) ? 'TRUE' : 'FALSE';
FALSE
php > echo gmp_cmp($a, $b);
PHP Warning: gmp_cmp(): Unable to convert variable to GMP - wrong type in php shell code on line 1
php > echo (strval($a) == strval($b)) ? 'TRUE' : 'FALSE';
TRUE
====
environment
$ uname -a
Linux localhost.localdomain 2.6.33.6-147.2.4.fc13.x86_64 #1 SMP Fri Jul 23 17:14:44 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux
$ php --version
PHP 5.3.3 (cli) (built: Jul 22 2010 15:57:00)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
To work with large numbers, there is BCMath libray of PHP.

Categories