-
Notifications
You must be signed in to change notification settings - Fork 337
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
Access violation on eval PHP code #320
Comments
This also happens with PHP 7.0.17 |
Do you happen to have a small test case which reproduces the issue? |
Yes here you go:
|
Confirmed.
|
The issue is that you are trying to execute PHP code before executor data are set up. This will work: #include <phpcpp.h>
static void testfunc()
{
Php::eval("echo 'Hello World!';");
}
extern "C" {
void* get_module()
{
static Php::Extension myExtension("php_my_test", "1.0");
myExtension.add<testfunc>("testfunc");
return myExtension;
}
} php -n -dextension_dir=. -dextension=test.so -r 'testfunc();' |
If you absolutely must call PHP code during request startup but before the executor is initialized, you will have to use Zend API: #include <phpcpp.h>
#include <cstring>
#include <Zend/zend.h>
#include <Zend/zend_compile.h>
#include <Zend/zend_execute.h>
extern "C" {
void* get_module()
{
static Php::Extension myExtension("php_my_test", "1.0");
static bool extensionAdded = false;
if (!extensionAdded)
{
extensionAdded = true;
myExtension.onRequest([]()
{
char code[] = "echo \"Hello, world!\\n\";";
char* desc = zend_make_compiled_string_description("eval'd code");
zend_eval_stringl_ex(code, std::strlen(code), nullptr, desc, 1);
efree(desc);
});
}
return myExtension;
}
} Simple wrapper function: void runPhpCode(const std::string& code)
{
char* desc = zend_make_compiled_string_description("eval'd code");
zend_eval_stringl_ex(const_cast<char*>(code.c_str()), code.size(), nullptr, desc, 1);
efree(desc);
} |
@sjinks Thank you very much for your effort and help !! I'm very appreciating it! But I'm wondering why this is not working with Php::eval() because this all worked in the legacy version of PHP-CPP and PHP 5.6 :( Don't get me wrong your code works great but doesn't it make the existing implementation of PHP-CPP obsolete ? I think we should somehow get it running again with the PHP-CPP eval implementation but I'm lacking in knowlege how PHP is working in that way :( |
|
As my underestanding of this library everything will be initialized after onStartup Callback a mentioned in the documentation: "The startup callback is called when the Zend engine has loaded your extension and all functions and classes in it were registered. If you want to initialize additional variables in your extension before the functions are going to get called, you can use the onStartup() function and register a callback to run this initialization code. After the Zend engine is initialized, it is ready to process requests. In the example above we used the onRequest() method to register a callback that is called in front of each request" And as I mentioned before the same function Php::eval() (with nearly exact the same implementation behind) worked with PHP 5.6 and PHP-CPP Lagacy in the onRequest() method. |
Hi, Sorry to bump this post, but @sjinks i'm trying to use your solution, but can't get it to work. I've downloaded php source to import zend libraries, but with no sucess to compile. I keep getting this error:
Could you please help me? |
g++ $(php-config --includes) -Wall -c -O2 -std=c++11 -fpic -o extension.o extension.cpp |
@sjinks hi,I used your way but it turned to error in linking process:
Could you please help me? |
You need to link against libphp as well. Add smth like |
@sjinks it seems that there donnot exists such library |
@sjinks solved by adding |
Hi,
i've compiled the latest version of PHP-CPP (a5ca2f3) with PHP 7.1.3 (64bit) on Win10 (64bit) VS2015. When I call the function Php::eval(phpContent) -> I get following error
script.cpp (Line 85) ->
opcodes.h (Line 76) -> ExecuteState execState(0)
0xC0000005: Access violation reading location 0x0000000000000010
Can you verify this problem ?
Thank you very much for your help in advance!
Greetings,
atvise
The text was updated successfully, but these errors were encountered: