jaredonline / rust-php

7 stars 3 forks source link

Which platform? #1

Open beberlei opened 9 years ago

beberlei commented 9 years ago

Building this on Linux/Ubuntu/64bit with Rust Nightly and PHP 5.6.10 Debug causes a segfault:

Program received signal SIGSEGV, Segmentation fault.
0x000000000064e9fe in zend_register_module_ex (module=0x7fffffffde88) at /root/php-5.6.10/Zend/zend_API.c:1916
1916            while (dep->name) {
(gdb) bt
#0  0x000000000064e9fe in zend_register_module_ex (module=0x7fffffffde88) at /root/php-5.6.10/Zend/zend_API.c:1916
#1  0x0000000000539b69 in php_load_extension (filename=0x7ffff7fae780 "/root/rust-php/target/debug/librust_a_star.so", type=1, start_now=0)
    at /root/php-5.6.10/ext/standard/dl.c:242
#2  0x00000000005c20fe in php_load_php_extension_cb (arg=0xb05b70) at /root/php-5.6.10/main/php_ini.c:351
#3  0x0000000000635d20 in zend_llist_apply (l=0xaf0d98 <extension_lists+56>, func=0x5c20d9 <php_load_php_extension_cb>) at /root/php-5.6.10/Zend/zend_llist.c:191
#4  0x00000000005c2f95 in php_ini_register_extensions () at /root/php-5.6.10/main/php_ini.c:745
#5  0x00000000005b931e in php_module_startup (sf=0xadd1a0 <cli_sapi_module>, additional_modules=0x0, num_additional_modules=0) at /root/php-5.6.10/main/main.c:2306
#6  0x00000000006f15d2 in php_cli_startup (sapi_module=0xadd1a0 <cli_sapi_module>) at /root/php-5.6.10/sapi/cli/php_cli.c:417
#7  0x00000000006f3be4 in main (argc=2, argv=0xaf8a30) at /root/php-5.6.10/sapi/cli/php_cli.c:1358
beberlei commented 9 years ago

The following patch makes it work for me (sort of):

diff --git a/src/lib.rs b/src/lib.rs
index 75329c3..d21ec25 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -72,7 +72,7 @@ pub extern "C" fn zm_generic_rust_a_star(_: c_int, _: c_int) -> c_int {
 pub extern "C" fn zm_info_rust_a_star(_: *const zend_module_entry) { }

 #[no_mangle]
-pub extern "C" fn get_module(_: c_void) -> *const zend_module_entry {
+pub extern "C" fn get_module(_: c_void) -> zend_module_entry {
     let rust_a_star_functions : &[zend_function_entry] = &[zend_function_entry {
         fname:    CString::new("confirm_rust_a_star_compiled").unwrap().as_ptr(),
         handler:  Some(zif_confirm_rust_a_star_compiled),
@@ -93,8 +93,8 @@ pub extern "C" fn get_module(_: c_void) -> *const zend_module_entry {
         zend_api:   20131226,
         zend_debug: 0,
         zts:        0,
-        ini_entry:  0 as *const _zend_ini_entry,
-        deps:       0 as *const _zend_module_dep,
+        ini_entry:  ptr::null(),
+        deps:       ptr::null(),
         name:       CString::new("rust_a_star").unwrap().as_ptr(),
         functions:  &rust_a_star_functions[0],

@@ -116,5 +116,5 @@ pub extern "C" fn get_module(_: c_void) -> *const zend_module_entry {
         module_number:        0,
         build_id:             CString::new("API20131226,NTS,debug").unwrap().as_ptr()
     };
-    &rust_a_star_module_entry
+    rust_a_star_module_entry
 }

PHP now runs, but segfaults on shutdown, it doesnt properly register the functions though, it registers a mangled function name instead of "confirm_rust_a_star_compiled". This was verified with:

php -dextension=rust_a_star.so -r 'var_dump(get_defined_functions());'
jaredonline commented 9 years ago

Heya @beberlei,

This is an experiment that I was never able to get working on any platform. It was a continuation of the though experiment that used Rust and C to write a PHP extension. The idea here was to remove the C part.

I was running into the same segfault that you started with, and using gdb I was able to pinpoint the segfault to the zend engine trying to copy data out of a struct that Rust had de-allocated. I was never able to get Rust to hold onto the allocation long enough.

Is there a way to call the mangled function and see what it outputs?

timglabisch commented 7 years ago

i played with the code and changed a bit.

the module is will be registered and i can call the rust function.

On OSX the code segfaults at this line.

At the moment i've no idea why - seems that i've some issues with the linker.

➜  hello_from_rust git:(master) ~/proj_/php/php-out/bin/php -v
PHP 7.2.0-dev (cli) (built: Nov 20 2016 01:01:51) ( NTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
[1]    67051 segmentation fault  ~/proj_/php/php-out/bin/php -v
➜  hello_from_rust git:(master)
➜  hello_from_rust git:(master) git commit -m "..."
➜  hello_from_rust git:(master) make run
cargo build
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
#cp target/debug/librust_a_star.dylib /Users/tim/proj_/php/php-src/../php-out/lib/php/extensions/debug-non-zts-20160303/librust_a_star.dylib
gdb run --args ~/proj_/php/php-out/bin/php src/rust_a_star.php
GNU gdb (GDB) 7.10.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin14.5.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /Users/tim/proj_/php/php-out/bin/php...done.
(gdb) run
Starting program: /Users/tim/proj_/php/php-out/bin/php src/rust_a_star.php
warning: `/Users/tim/proj_/rust/hello_from_rust/target/debug/deps/rust_a_star.0.o': can't open to read symbols: No such file or directory.

Program received signal SIGSEGV, Segmentation fault.
0x00007fff8c0bf2ab in dyld_stub_binder () from /usr/lib/system/libdyld.dylib
timglabisch commented 7 years ago

the problem seems to be, that the struct in get_module should be static.

timglabisch commented 7 years ago

my fork now compiles and dont segfault: https://github.com/timglabisch/rust_php_extension

jaredonline commented 7 years ago

Nice! That's awesome. I'll have to take a look and see what I can port over. What version of PHP does it work with?

timglabisch commented 7 years ago

=)

➜ php-src git:(master) ✗ ~/proj_/php/php-out/bin/php -v PHP 7.2.0-dev (cli) (built: Nov 20 2016 01:01:51) ( NTS DEBUG ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies

➜ php-src git:(master) ✗ git rev-parse HEAD 40f6438972e6c9b4684ea9476fdaf29857cf5fe2

i think the next really important challenge is figuring out how to create new zvals :)