Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault when calling a PHP built-in function #421

Open
z80maniac opened this issue Jun 19, 2019 · 8 comments
Open

Segfault when calling a PHP built-in function #421

z80maniac opened this issue Jun 19, 2019 · 8 comments

Comments

@z80maniac
Copy link

When an extension calls any built-in PHP function, it crashes, i.e. this code crashes:

Php::call("var_export", "function call");

Note: it only happens when PHP-CPP is built in release mode (with make release).

Full example source code:

#include <phpcpp.h>
#include <iostream>

void myFunction() {
    Php::call("var_export", "function call");
    Php::out << std::endl << "php out" << std::endl;
}

extern "C" PHPCPP_EXPORT void *get_module() {
    static Php::Extension extension("my_extension", "1.0");
    extension.add<myFunction>("myFunction");
    return extension;
}

Build:

g++ -shared -fPIC -o crashtest.so crashtest.cpp -lphpcpp

Test:

php -dextension=./crashtest.so -r 'myFunction();'

Output:

'function call'Segmentation fault (core dumped)

As mentioned above, the crash only happens when PHP-CPP is built in release mode. When PHP-CPP is built in debug mode, the output is as follows:

'function call'
php out

Which is the expected output.

Note: the crash does not happen in Php::out. You can remove it and the extension will still crash. I just added it to check if the test reaches that point in code.

Tested on a clean Ubuntu 18.04 with PHP v7.2.

@scorninpc
Copy link

Works fine for me

Seleção_099

What did you mean with release and debug mode? -g flag, what?

@z80maniac
Copy link
Author

I mean, the PHP-CPP was built with make release, not just make.

@scorninpc
Copy link

scorninpc commented Jun 19, 2019

Same, no problem

Seleção_109

Seleção_108

release parameter add -02 flag only, nothing more them tell to compiler to optimize the code

@z80maniac
Copy link
Author

Here's a fully self-contained example that can be run via Docker:

#!/usr/bin/env bash
set -e
cd "$(dirname -- "$(readlink -f -- "$0")")"

apt update
DEBIAN_FRONTEND=noninteractive apt install -yq php-dev git

mkdir ../build
cd ../build

git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.git
pushd PHP-CPP && make release && make install && popd

cat << EOF > crashtest.cpp
#include <phpcpp.h>
#include <iostream>

void myFunction() {
    Php::call("var_export", "function call");
    Php::out << std::endl << "php out" << std::endl;
}

extern "C" PHPCPP_EXPORT void *get_module() {
    static Php::Extension extension("my_extension", "1.0");
    extension.add<myFunction>("myFunction");
    return extension;
}
EOF

g++ -shared -fPIC -o crashtest.so crashtest.cpp -lphpcpp

echo "Starting the test..."
php -dextension=./crashtest.so -r 'myFunction();'
echo "The test has been passed."

Name this file run.sh and run Docker in the same directory:

docker run --rm -v"$(pwd)":/test ubuntu:18.04 bash test/run.sh

The last lines will be:

Starting the test...
'function call'test/run.sh: line 33: 10602 Segmentation fault      (core dumped) php -dextension=./crashtest.so -r 'myFunction();'

Now, if you remove the release, the test will be passed correctly:

Starting the test...
'function call'
php out
The test has been passed.

Maybe it's a problem with PHP 7.2 only, and not with PHP 7.3.

@scorninpc
Copy link

Yes, can be the 7.2 problem, but as I saw, release only optimize the code, there is no way can be difference.

Sorry for I cannot test docker test

If you see Makefile, you can run in production with "make" only

@z80maniac
Copy link
Author

can be the 7.2 problem

In fact, it may be Ubuntu problem. I ran the run.sh against php:7.3.6, php:7.2.19 and php:7.1.30 docker images - and they all passed the test. But when I run it in Ubuntu, it crashes. And the crash is reproducible everywhere: in Docker, in LXC and on my host machine with Kubuntu 19.04.

So, may be it's not a problem in PHP-CPP. Though, I can't say for sure.

there is no way can be difference

There can be a difference if a code is written incorrectly (e.g. produces undefined behavior according to the standard). I encountered this situation many times before in different projects.

Sorry for I cannot test docker test

OK. I just wanted to show that the problem is really not on my side. Instead of Docker, you can try LXC, virtual machine or a live disk - anything that gives you a clean Ubuntu install.

you can run in production with "make" only

As a last resort, yes. But I found out that if PHP-CPP is compiled inside a php:* Docker container, then it can be used to compile extensions that don't crash on Ubuntu. So, there's a semi-decent workaround at least.

@scorninpc
Copy link

What the exactly php7.2 version do you have?

@z80maniac
Copy link
Author

On my host machine (Kubuntu 19.04):

$ php -v
PHP 7.2.19-0ubuntu0.19.04.1 (cli) (built: Jun  4 2019 14:44:42) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.19-0ubuntu0.19.04.1, Copyright (c) 1999-2018, by Zend Technologies

In LXC and Docker containers (Ubuntu 18.04):

# php -v
PHP 7.2.19-0ubuntu0.18.04.1 (cli) (built: Jun  4 2019 14:48:12) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.19-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

However, as I said earlier, the test is passed successfully on php:7.2.19 Docker image, which is Debian-based (Stretch).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants