# php segfaulting



## kalobok (Dec 3, 2009)

After today's 'portupgrade -a' my php 5.2.11 started segfaulting on every run. It looks like the segfault happens on exit, and it's very stable - even 'php --version' gives me the following:


```
PHP 5.2.11 with Suhosin-Patch 0.9.7 (cli) (built: Dec  2 2009 12:17:42)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
Segmentation fault (core dumped)
```

The scripts are executed, webpaged produced by php look fine, but it's very annoying. Yesterday everything was working fine. The ports upgraded today are (the output was produced before portupgrade):


```
cups-base-1.4.2             <  needs updating (port has 1.4.2_1) 
cups-client-1.4.2           <  needs updating (port has 1.4.2_1) 
cups-image-1.4.2            <  needs updating (port has 1.4.2_1) 
gobject-introspection-0.6.5  <  needs updating (port has 0.6.5_1) 
libxml2-2.7.6               <  needs updating (port has 2.7.6_1) 
xf86-video-ati-6.12.2_1     <  needs updating (port has 6.12.4)
```

Nothing else has changed. Oh, yeah, it's 7.0-RELEASE.

Any ideas on what's wrong and how to fix it?


----------



## Voltar (Dec 3, 2009)

I had a similar problem, tt was due to the order that my extensions were loaded.

Just a thought, you might want to see if the solution I found helps you.


----------



## kalobok (Dec 3, 2009)

Thanks for the advice. The problem disappears if I comment both *openssl* and *curl* extensions. Changing the order does not fix it. I'm still looking for a solution.


----------



## Voltar (Dec 3, 2009)

I'd try recompiling the extensions and their dependencies again. Also, if you know how to use gdb (and have a core file) you could try pinpointing the problem there.


----------



## kalobok (Dec 3, 2009)

Rebuilding all extensions is what I'm doing right now.

Using gdb is not a big deal, but I'm probably too lazy for this. 

Anyway, I'll post my results here.


----------



## kalobok (Dec 3, 2009)

Well, rebuilding the extensions did not work. gdb shows that the problem is in libxml2 (which was updated today):


```
#0  0x288e3d50 in ?? ()
#1  0x285b16fb in pthread_once () from /lib/libc.so.7
#2  0x283461d2 in xmlIsMainThread () from /usr/local/lib/libxml2.so.5
#3  0x28345787 in __xmlLastError () from /usr/local/lib/libxml2.so.5
#4  0x282d1847 in xmlResetLastError () from /usr/local/lib/libxml2.so.5
#5  0x282d891f in xmlCleanupParser () from /usr/local/lib/libxml2.so.5
#6  0x080844eb in php_libxml_shutdown ()
#7  0x0808451b in zm_shutdown_libxml ()
#8  0x0814b4ae in module_destructor ()
#9  0x08151804 in zend_hash_apply_deleter ()
#10 0x08151a48 in zend_hash_graceful_reverse_destroy ()
#11 0x08147d2e in zend_shutdown ()
#12 0x0810615f in php_module_shutdown ()
#13 0x081c4100 in main ()
```

I hope the library will get fixed soon. I think there's a new bad tradition in freebsd - too many updates are breaking some functionality.


----------



## DutchDaemon (Dec 3, 2009)

I'm running the same PHP version with the same extensions and all ports are up to date as of now. On two separate systems serving up stuff like wordpress, roundcube, squirrelmail, etc. No problems here. I think your 'bad tradition' statement is a bit too general .. These forums would be swarming with people complaining had this been a general bug, because this setup is very standard and widely used.


----------



## Giraya (Dec 3, 2009)

Hi,

got the same problem here, on FreeBSD 7.0-p12 and 7.2-RELEASE-p3.

In fact there is no problem with PHP in apache mod. The problem only occurs in CLI mod.

```
# php -v
PHP 5.2.11 with Suhosin-Patch 0.9.7 (cli) (built: Oct 14 2009 17:13:44)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
Segmentation fault (core dumped)
```

DutchDaemon, can you just try a "php -v" on your servers ?

Using portdowngrade to switch back to the old version of libxml2 (2.7.6) works.

Thanks


----------



## dennylin93 (Dec 3, 2009)

I've just upgraded all my ports to the latest version. Let's see if I encounter the same problem as well.

If the problem is reproducible, send a PR.

Edit: I don't use CGI though.


----------



## DutchDaemon (Dec 3, 2009)

#1


```
PHP 5.2.11 with Suhosin-Patch 0.9.7 (cli) (built: Nov 16 2009 12:28:30) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
    with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH
```


```
eaccelerator-0.9.5.3 An opcode cache for PHP with encoder and loader
libxml2-2.7.6_1     XML parser library for GNOME
openssl-0.9.8l      SSL and crypto library
php-suhosin-0.9.29  A PHP extension that implements high-level protections
php5-5.2.11_1       PHP Scripting Language
php5-ctype-5.2.11_1 The ctype shared extension for php
php5-dom-5.2.11_1   The dom shared extension for php
php5-extensions-1.3 A "meta-port" to install PHP extensions
php5-filter-5.2.11_1 The filter shared extension for php
php5-gd-5.2.11_2    The gd shared extension for php
php5-hash-5.2.11_1  The hash shared extension for php
php5-iconv-5.2.11_1 The iconv shared extension for php
php5-json-5.2.11_1  The json shared extension for php
php5-mbstring-5.2.11_1 The mbstring shared extension for php
php5-mysql-5.2.11_1 The mysql shared extension for php
php5-pcre-5.2.11_1  The pcre shared extension for php
php5-pdo-5.2.11_1   The pdo shared extension for php
php5-pdo_sqlite-5.2.11_1 The pdo_sqlite shared extension for php
php5-posix-5.2.11_1 The posix shared extension for php
php5-session-5.2.11_1 The session shared extension for php
php5-simplexml-5.2.11_1 The simplexml shared extension for php
php5-spl-5.2.11_1   The spl shared extension for php
php5-sqlite-5.2.11_1 The sqlite shared extension for php
php5-tokenizer-5.2.11_1 The tokenizer shared extension for php
php5-xml-5.2.11_1   The xml shared extension for php
php5-xmlreader-5.2.11_1 The xmlreader shared extension for php
php5-xmlrpc-5.2.11_1 The xmlrpc shared extension for php
php5-xmlwriter-5.2.11_1 The xmlwriter shared extension for php
php5-zip-5.2.11_1   The zip shared extension for php
php5-zlib-5.2.11_1  The zlib shared extension for php
roundcube-0.3.1,1   Fully skinnable XHTML/CSS webmail written in PHP
```

#2

```
PHP 5.2.11 with Suhosin-Patch 0.9.7 (cli) (built: Nov 19 2009 18:08:48)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
    with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH
```


```
eaccelerator-0.9.5.3 An opcode cache for PHP with encoder and loader
libmcrypt-2.5.8     Multi-cipher cryptographic library (used in PHP)
libxml2-2.7.6_1     XML parser library for GNOME
openssl-0.9.8l      SSL and crypto library
php-suhosin-0.9.29  A PHP extension that implements high-level protections
php5-5.2.11_1       PHP Scripting Language
php5-bz2-5.2.11_1   The bz2 shared extension for php
php5-calendar-5.2.11_1 The calendar shared extension for php
php5-ctype-5.2.11_1 The ctype shared extension for php
php5-curl-5.2.11_1  The curl shared extension for php
php5-dom-5.2.11_1   The dom shared extension for php
php5-extensions-1.3 A "meta-port" to install PHP extensions
php5-filter-5.2.11_1 The filter shared extension for php
php5-ftp-5.2.11_1   The ftp shared extension for php
php5-gd-5.2.11_2    The gd shared extension for php
php5-gettext-5.2.11_1 The gettext shared extension for php
php5-hash-5.2.11_1  The hash shared extension for php
php5-iconv-5.2.11_1 The iconv shared extension for php
php5-imap-5.2.11_1  The imap shared extension for php
php5-json-5.2.11_1  The json shared extension for php
php5-mbstring-5.2.11_1 The mbstring shared extension for php
php5-mcrypt-5.2.11_1 The mcrypt shared extension for php
php5-mhash-5.2.11_1 The mhash shared extension for php
php5-mysql-5.2.11_1 The mysql shared extension for php
php5-openssl-5.2.11_1 The openssl shared extension for php
php5-pcre-5.2.11_1  The pcre shared extension for php
php5-pdo-5.2.11_1   The pdo shared extension for php
php5-pdo_sqlite-5.2.11_1 The pdo_sqlite shared extension for php
php5-posix-5.2.11_1 The posix shared extension for php
php5-session-5.2.11_1 The session shared extension for php
php5-simplexml-5.2.11_1 The simplexml shared extension for php
php5-spl-5.2.11_1   The spl shared extension for php
php5-sqlite-5.2.11_1 The sqlite shared extension for php
php5-tokenizer-5.2.11_1 The tokenizer shared extension for php
php5-xml-5.2.11_1   The xml shared extension for php
php5-xmlreader-5.2.11_1 The xmlreader shared extension for php
php5-xmlwriter-5.2.11_1 The xmlwriter shared extension for php
php5-zip-5.2.11_1   The zip shared extension for php
php5-zlib-5.2.11_1  The zlib shared extension for php
phpMyAdmin-3.2.3    A set of PHP-scripts to manage MySQL over the web
phpmailer-5.1       Full Featured Email Transfer Class for PHP
roundcube-0.3.1,1   Fully skinnable XHTML/CSS webmail written in PHP
```


----------



## vivek (Dec 3, 2009)

This is an isolated to your server, otherwise we got more messages on freebsd mailing lists. PHP is widely used. Did you recently upgraded from older version?

```
PHP 5.2.11 with Suhosin-Patch 0.9.7 (cli) (built: Oct 25 2009 09:48:29) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with XCache v1.3.0, Copyright (c) 2005-2009, by mOo
```

This box serving around 4 to 5 million page views per month.


----------



## Giraya (Dec 3, 2009)

No more segmentation fault if I remove theses lines from the "patch-configure" file of the libxml2 port :


```
@@ -20665,6 +20666,8 @@
               fi
           fi
        ;;
+       *freebsd*) THREAD_LIBS=""
+       ;;
     esac
     if test "$WITH_THREADS" = "1" ; then
        THREAD_CFLAGS="$THREAD_CFLAGS -D_REENTRANT"
```


----------



## kalobok (Dec 3, 2009)

Giraya said:
			
		

> No more segmentation fault if I remove theses lines from the "patch-configure" file of the libxml2 port



Thanks, but - nope. Just tried it - my php is still coredumping after removing these lines and rebuilding the library.

I suspect that the problem is not just in the library, but in the combination of php extensions which use it. What helps on one machine may not work on another.


----------



## Firestorm (Dec 3, 2009)

I'm having segfault since today too.. mine disappear when I comment out the mhash, memcached and gearman extensions, which I really need.

I've done everything I can think of, including running portmanager -u to update everything, but the problem remains.

Any help would be very very appreciated. I've been trying to get this sorted all day now..


----------



## Firestorm (Dec 3, 2009)

By the way.. it all started when I upgraded graphics/php5-gd because it was reported to have a vulnerability..


----------



## Giraya (Dec 3, 2009)

kalobok said:
			
		

> What helps on one machine may not work on another.



OK, but it's strange, because I have multiple servers with different configurations (and kernels) and this modification works on each one.


----------



## kalobok (Dec 3, 2009)

Giraya said:
			
		

> OK, but it's strange, because I have multiple servers with different configurations (and kernels) and this modification works on each one.



Something strange is happening. I rebuilt and reinstalled the library again and the segfault is now gone. I'll try to reproduce this again - maybe I missed something during the first try.

Thanks for the fix.


----------



## DutchDaemon (Dec 3, 2009)

The only difference that makes (in config.log) is:


```
< THREAD_LIBS=''
---
> THREAD_LIBS='-pthread'
```

and

```
< XML_LIBS='-lxml2 -lz  -L/usr/local/lib -liconv -lm '
---
> XML_LIBS='-lxml2 -lz -pthread -L/usr/local/lib -liconv -lm '
```

So you're adding -pthread to the compilation. I don't see the immediate impact.

Edit: oh, I see it didn't make a real difference. So that was expected


----------



## kalobok (Dec 3, 2009)

Ok, I checked it again and can confirm: the Giraya's method is working. No more segfaults.

The bug is definitely related to thread-safety (see the stack trace above). Maybe the difference is in php configuration. Mine has "Thread Safety => disabled". Maybe thread-safe php does not have the problem.


----------



## Firestorm (Dec 3, 2009)

Yess.. fixing the patch-configure did it for me too.


----------



## cajunman4life (Dec 3, 2009)

Confirmed that removing the last few lines of patch-configure and rebuilding solves the PHP segfault.

Hmm....


----------



## intosi (Dec 4, 2009)

Removing the THREAD_LIBS="" line from the patch-configure file fixed the PHP (and perl*) problem for me, as well.

* yesterday, not only PHP started to act weirdly, but perl suddenly segfaulting whenever I both created an XML parser and opened a connection to my PostgreSQL database.


----------



## sjorsjuh (Dec 5, 2009)

kalobok said:
			
		

> Well, rebuilding the extensions did not work. gdb shows that the problem is in libxml2 (which was updated today):
> 
> 
> ```
> ...



I have the exact same bug in my core dump.



			
				kalobok said:
			
		

> Ok, I checked it again and can confirm: the Giraya's method is working. No more segfaults.
> 
> The bug is definitely related to thread-safety (see the stack trace above). Maybe the difference is in php configuration. Mine has "Thread Safety => disabled". Maybe thread-safe php does not have the problem.



Giraya's fix works for me to. Finally up and running again


----------



## sjorsjuh (Dec 5, 2009)

I couldn't find a bug-report for this; or doesn't this actually qualify for a bug-report?


----------



## Phonetic (Dec 6, 2009)

I can confirm that downgrading to libxml2 2.7.6 fixed our problems.


----------



## ohauer (Dec 6, 2009)

Normally this is a real case for a PR.

since no one opened a PR, i've done it with a pointer to this thread.
(uhhh, it looks not well if the signes '>' are used in a PR something have reformatted the mail)

http://www.freebsd.org/cgi/query-pr.cgi?pr=141226

luckily I read the thread before updating


----------



## joint (Dec 6, 2009)

'portdowngrade libxml2' to 2.7.6 released 10 Oct 2009 fixed
Segmentation fault (core dumped)


----------



## pwm (Dec 11, 2009)

Had this problem on multiple servers. Giraya's fix solved it. Thanks.


----------



## GreenX (Dec 21, 2009)

But this is not entirely correct, because IMHO changes in libxml in order to catch the wrong ports. For example: today corrected mhash. Now I'm waiting for postgresql.


----------



## ohauer (Jan 22, 2010)

Hi,

since the thread is no longer active and it seems the issues are fixed (at least I don't have any issues with the mix of libxml2, php and postgres) I'm going to close the PR I opened tomorrow.


----------



## sjorsjuh (Mar 23, 2010)

The exact same thing happened on my server again today. I updated my libxml2 port to "libxml2-2.7.6_1" and the php-segfaults started again. This update was pending since March 18th, I figured it was working fine because I didn't see any new posts in this topic over the last few days, but it still isn't working for me.

Again the fix provided by Giraya worked for me, the same code is still causing the same problem on my server.


----------



## soloslinger (Jun 21, 2010)

We've encountered this problem as well and Giraya's work around fixes it for us as well.  The question I have about this then is, the port maintainer thought it wise to include that code in the patch-configure file.  What are the trade offs to my system if I alter how libxml2 behaves with threading?  This problem seems widely replicable and easily fixed, yet someone must have thought it a good idea to build the port as it is built...?

soloslinger


----------

