all exec related PHP functions segfault

喜欢而已 提交于 2019-12-12 06:02:41

问题


I build PHP for android using the NDK. Most functions tested so far run perfectly, except...

All exec related php function (like exec, shell_exec, popen, ..) all sefgault.

php sample code (test.php)

<?php
$s=shell_exec("ls");
echo $s
?>

result:

# php test.php
[1]   Segmentation fault

I added some debug code to the internal shell_exec function

PHP_FUNCTION(shell_exec)
{
    FILE *in;
    size_t total_readbytes;
    zval **cmd;
    char *ret;
    php_stream *stream;

    if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &cmd)==FAILURE) {
            WRONG_PARAM_COUNT;
    }

    if (PG(safe_mode)) {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute using backquotes in Safe Mode");
            RETURN_FALSE;
    }

    convert_to_string_ex(cmd);
#ifdef PHP_WIN32
    if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "rt"))==NULL) {
#else
    if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "r"))==NULL) {
#endif
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", Z_STRVAL_PP(cmd));
            RETURN_FALSE;
    }

    stream = php_stream_fopen_from_pipe(in, "rb");
    total_readbytes = php_stream_copy_to_mem(stream, &ret, PHP_STREAM_COPY_ALL, 0);
    php_stream_close(stream);

    printf("TEST\n%s\n",ret);

    if (total_readbytes > 0) {
            RETURN_STRINGL(ret, total_readbytes, 0);
    } else {
            RETURN_NULL();
    }
}

result

# php test.php
TEST
test.php
[1]   Segmentation fault

I conclude
- the exec of ls is working fine
- the result of the ls is ok

Still something gets wrong.

Question 1:
Can someone point me to the code section where the PHP_FUNCTION(shell_exec) is executed, so i can try to see some more details of where it goes wrong

Question 2:
Any hints on what might be wrong.

Version info
Android 2.3.1
Android NDK 6
PHP 5.2.17


回答1:


Thanx to the anonymous who send me an email with some usefull tips.

As it turns out php uses popen for al its exec related code. A some might know androids bionic version of popen was quite buggy up until ICS and it is know the corrupt the stack.

I modified php to use a modified verion of popen Now php exec/shell_exec etc all run without any problems



来源:https://stackoverflow.com/questions/9922517/all-exec-related-php-functions-segfault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!