Crash in event_free() on windows

Issue #1 resolved
Anatol Belski created an issue

Debugging the crash of the tests\11-gc-cycles.phpt it turned out that the following will cause crash

$t = Event::timer(new EventBase(), function () { }); $t->free();

Even ommiting the explicit freeing it'd crash in event_object_free_storage() as that's using the same snippet. Actually it looks very odd, as the pointer itself is the same in both Event::timer() when created and later when passing to event_free() ... Looking at the code of event_free() itself i can imagine that's an issue of the libevent itself, namely a locking issue in event_del(). Cant prove it yet though.

And that's why i've decided to created a ticket here and ask for assistance. Ruslan, maybe you've an idea where I should dig further?

When I look at the related libevent ext, there's no event_free() call, the event seems to be not passed for freeing to the libevent. So it might be leaked if there's no gc in the libevent itself. Anyway ommiting event_free() could solve that crash on windows.

Thanks.

Comments (12)

  1. Ruslan Osmanov repo owner

    I'm trying to set up build environment on Windows virtual machine. Struggling with weird behaviour of the configure script:

    C:\php-sdk\phpdev\vc18\x64\src>cl
    Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    usage: cl [ option... ] filename... [ /link linkoption... ]
    
    C:\php-sdk\phpdev\vc18\x64\src>echo %path%
    C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Wind
    owsPowerShell\v1.0\;"C:\Program Files (x86)\GnuWin32\bin\";C:\Program Files\Micr
    osoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft Visual Studio
    12.0\VC\bin\;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE;;C:
    \php-sdk\bin\\..\bin;C:\php-sdk\bin\;D:\apps\svn\bin;C:\php-sdk\bin\\..\bin;C:\p
    hp-sdk\bin\;D:\apps\svn\bin
    
    C:\php-sdk\phpdev\vc18\x64\src>buildconf --force
    Rebuilding configure.js
    Now run 'configure --help'
    
    C:\php-sdk\phpdev\vc18\x64\src>configure
    Saving configure options to config.nice.bat
    Checking for cl.exe ...  <in default path>
      Detected compiler undefined
    C:\php-sdk\phpdev\vc18\x64\src\configure.js(1829, 9) Microsoft JScript runtime e
    rror: 'length' is null or not an object
    

    I'll continue digging into the painful Windows stuff when I find some free time again.

  2. Anatol Belski reporter

    We don't work with VS2013, that's the issue. It's even not added to the config scripts. For 5.5 it's VS2012, for earlier VS2008.

  3. Anatol Belski reporter

    Yep, VS2012 will for 5.5 will be probably the easiest, as the SDK there isn't separated like in VS2008. I'll be testing on VS2008 then anyway. There's a wiki page about it https://wiki.php.net/internals/windows/stepbystepbuild . VS2013 will be added somewhen soon to master only, i presume.

    Btw. great you've decided to setup on windows. As you're much deeper into libevent yet than me :)

  4. Ruslan Osmanov repo owner

    Once again I tried to compile PHP x86 under Windows 7 x86_64. Still no luck. Unfortunately, I lack experience in Windows programming. Last time I worked on Windows was five years ago.

    The following is output from "VS2012 x86 Native Tools Command Prompt":

    c:\php-sdk\phpdev\vc11\x86\src>configure --disable-all --enable-cli --enable-so
    kets  --with-event=C:\Users\Ruslan\src\libevent-2.0.21-stable\ --disable-zts
    Saving configure options to config.nice.bat
    Checking for cl.exe ...  <in default path>
      Detected compiler MSVC11 (Visual C++ 2012)
      Detected 32-bit compiler
    Checking for link.exe ...  C:\Program Files (x86)\Microsoft Visual Studio 11.0\
    C\bin\
    Checking for nmake.exe ...  <in default path>
    Checking for lib.exe ...  <in default path>
    Checking for bison.exe ...  <in default path>
    Checking for re2c.exe ...  <in default path>
      Detected re2c version 0.13.5
    Checking for zip.exe ...  <in default path>
    Checking for lemon.exe ...  <not found>
    Checking for mc.exe ...  <not found>
    Checking for mt.exe ...  <not found>
    
    Build dir: Release
    PHP Core:  php5.dll and php5.lib
    
    Checking for wspiapi.h ...  <not found>
    Checking for wspiapi.h ...  <not found>
    Enabling SAPI sapi\cli
    Enabling extension ext\date
    Enabling extension ext\ereg
    Enabling extension ext\pcre
    Enabling extension ext\reflection
    Checking for library ws2_32.lib ... <not found>
    WARNING: sockets not enabled; libraries and headers not found
    
    Enabling extension ext\spl
    Checking for timelib_config.h ...  ext/date/lib
    Enabling extension ext\standard
    Checking for event2/event.h ...  <in default path>
    Checking for library libevent.lib ... ..\..\..\..\..\Users\Ruslan\src\libevent-
    .0.21-stable\libevent.lib
    Checking for library libevent_core.lib ... ..\..\..\..\..\Users\Ruslan\src\libe
    ent-2.0.21-stable\libevent_core.lib
    Checking for library libevent_extras.lib ... ..\..\..\..\..\Users\Ruslan\src\li
    event-2.0.21-stable\libevent_extras.lib
    Checking for pthread.h ...  C:\Users\Ruslan\src\libevent-2.0.21-stable\
    Checking for sched.h ...  C:\Users\Ruslan\src\libevent-2.0.21-stable\
    Checking for library pthreadVC2.lib ... <in LIB path> pthreadVC2.lib
    Checking for common.h ...  ext\event\src
    Checking for http.h ...  <in default path>
    Checking for php_event.h ...  ext\event
    Enabling extension ext\event [shared]
    
    Creating build dirs...
    Generating files...
    Generating Makefile
    Generating main/internal_functions.c
            [content unchanged; skipping]
    Generating main/config.w32.h
    Generating phpize
    Done.
    
    
    
    Enabled extensions:
    -----------------------
    | Extension  | Mode   |
    -----------------------
    | date       | static |
    | ereg       | static |
    | event      | shared |
    | pcre       | static |
    | reflection | static |
    | spl        | static |
    | standard   | static |
    -----------------------
    
    
    Enabled SAPI:
    -------------
    | Sapi Name |
    -------------
    | cli       |
    -------------
    
    
    --------------------------------------------
    |               |                          |
    --------------------------------------------
    | Build type    | Release                  |
    | Thread Safety | No                       |
    | Compiler      | MSVC11 (Visual C++ 2012) |
    | Architecture  | x86                      |
    --------------------------------------------
    
    
    Type 'nmake' to build PHP
    
    c:\php-sdk\phpdev\vc11\x86\src>nmake
    
    Microsoft (R) Program Maintenance Utility Version 11.00.50727.1
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    Recreating build dirs
            type ext\pcre\php_pcre.def > Release\php5.dll.def
            "" -h win32\ -r Release\ -x Release\ win32\build\wsyslog.mc
    '-h' is not recognized as an internal or external command,
    operable program or batch file.
    NMAKE : fatal error U1077: '"' : return code '0x1'
    Stop.
    
    c:\php-sdk\phpdev\vc11\x86\src>
    
    c:\php-sdk\phpdev\vc11\x86\src>configure --disable-all --enable-cli --enable-so
    kets  --with-event=C:\Users\Ruslan\src\libevent-2.0.21-stable\ --disable-zts
    Saving configure options to config.nice.bat
    Checking for cl.exe ...  <in default path>
      Detected compiler MSVC11 (Visual C++ 2012)
      Detected 32-bit compiler
    Checking for link.exe ...  C:\Program Files (x86)\Microsoft Visual Studio 11.0\
    C\bin\
    Checking for nmake.exe ...  <in default path>
    Checking for lib.exe ...  <in default path>
    Checking for bison.exe ...  <in default path>
    Checking for re2c.exe ...  <in default path>
      Detected re2c version 0.13.5
    Checking for zip.exe ...  <in default path>
    Checking for lemon.exe ...  <not found>
    Checking for mc.exe ...  <not found>
    Checking for mt.exe ...  <not found>
    
    Build dir: Release
    PHP Core:  php5.dll and php5.lib
    
    Checking for wspiapi.h ...  <not found>
    Checking for wspiapi.h ...  <not found>
    Enabling SAPI sapi\cli
    Enabling extension ext\date
    Enabling extension ext\ereg
    Enabling extension ext\pcre
    Enabling extension ext\reflection
    Checking for library ws2_32.lib ... <not found>
    WARNING: sockets not enabled; libraries and headers not found
    
    Enabling extension ext\spl
    Checking for timelib_config.h ...  ext/date/lib
    Enabling extension ext\standard
    Checking for event2/event.h ...  <in default path>
    Checking for library libevent.lib ... ..\..\..\..\..\Users\Ruslan\src\libevent-
    .0.21-stable\libevent.lib
    Checking for library libevent_core.lib ... ..\..\..\..\..\Users\Ruslan\src\libe
    ent-2.0.21-stable\libevent_core.lib
    Checking for library libevent_extras.lib ... ..\..\..\..\..\Users\Ruslan\src\li
    event-2.0.21-stable\libevent_extras.lib
    Checking for pthread.h ...  C:\Users\Ruslan\src\libevent-2.0.21-stable\
    Checking for sched.h ...  C:\Users\Ruslan\src\libevent-2.0.21-stable\
    Checking for library pthreadVC2.lib ... <in LIB path> pthreadVC2.lib
    Checking for common.h ...  ext\event\src
    Checking for http.h ...  <in default path>
    Checking for php_event.h ...  ext\event
    Enabling extension ext\event [shared]
    
    Creating build dirs...
    Generating files...
    Generating Makefile
    Generating main/internal_functions.c
            [content unchanged; skipping]
    Generating main/config.w32.h
    Generating phpize
    Done.
    
    Enabled extensions:
    -----------------------
    | Extension  | Mode   |
    -----------------------
    | date       | static |
    | ereg       | static |
    | event      | shared |
    | pcre       | static |
    | reflection | static |
    | spl        | static |
    | standard   | static |
    -----------------------
    
    
    Enabled SAPI:
    -------------
    | Sapi Name |
    -------------
    | cli       |
    -------------
    
    
    --------------------------------------------
    |               |                          |
    --------------------------------------------
    | Build type    | Release                  |
    | Thread Safety | No                       |
    | Compiler      | MSVC11 (Visual C++ 2012) |
    | Architecture  | x86                      |
    --------------------------------------------
    
    
    Type 'nmake' to build PHP
    
    c:\php-sdk\phpdev\vc11\x86\src>nmake
    
    Microsoft (R) Program Maintenance Utility Version 11.00.50727.1
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    Recreating build dirs
            type ext\pcre\php_pcre.def > Release\php5.dll.def
            "" -h win32\ -r Release\ -x Release\ win32\build\wsyslog.mc
    '-h' is not recognized as an internal or external command,
    operable program or batch file.
    NMAKE : fatal error U1077: '"' : return code '0x1'
    Stop.
    
    c:\php-sdk\phpdev\vc11\x86\src>
    c:\php-sdk\phpdev\vc11\x86\src>echo %LIB%
    C:\Users\Ruslan\src\pthreads\Pre-built.2\lib\x86;%LIB%
    
    c:\php-sdk\phpdev\vc11\x86\src>echo %PATH%
    C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Wind
    owsPowerShell\v1.0\;"C:\Program Files (x86)\GnuWin32\bin\";C:\Program Files\Micr
    osoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft Visual Studio
    11.0\VC\bin\;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE;;c:
    \php-sdk\bin\\..\bin;c:\php-sdk\bin\;D:\apps\svn\bin
    
    c:\php-sdk\phpdev\vc11\x86\src>echo %INCLUDE%
    ext\event\classes;C:\Users\Ruslan\src\libevent-2.0.21-stable\include;%INCLUDE%
    
    c:\php-sdk\phpdev\vc11\x86\src>
    
  5. Anatol Belski reporter

    Looks very odd, the environment seems to be broken. That's already to see in config

    Checking for library ws2_32.lib ... <not found> WARNING: sockets not enabled; libraries and headers not found

    the ws2_32.lib is the basic one, so it should be available.

    Or with nmake

    "" -h win32\ -r Release\ -x Release\ win32\build\wsyslog.mc

    that should look like

    "C:\Program Files (x86)\Windows Kits\8.0\bin\x86\mc.exe" -h win32\ -r Debug_TS\ -x Debug_TS\ win32\build\wsyslog.mc

    Sure you use the native command promt as in wiki?

    Another thing - i almost use --with-extra-libs and --with-extra-includes instead of setting the the path in the --with-* option. Multiple paths there can be separated with semicolon. For now at least, --with-event=shared should be used, as that's only the case which matters.

  6. Ruslan Osmanov repo owner

    Well, I've finally set it up. I had to rebuild it all for x64.

    The crash is caused by early base destruction. The code

    $t = Event::timer(new EventBase(), function () { }); $t->free();
    

    doesn't cause a crash on Linux, however; at least on my amd64 Gentoo box.

    The following code shouldn't crash on Windows, too:

    $b = new EventBase();
    $t = Event::timer($b, function () { });
    $t->free();
    

    Currently I have troubles getting debug info.. One day I'll fix it )

  7. Ruslan Osmanov repo owner

    Ehh, Remi has pushed some fixes, so I should package release ASAP. But this bug haunts me. I should fix it some way :) Maybe just restrict all EventBase arguments to be passed by reference? So we'll avoid getting refcount = 1.

    We talked about implementing custom reference counting for EventBase objects with krakjoe in the IRC. He suggested to make a linked list for our own "GC"... I can scrcely imagine that thing, because I don't know how to properly avoid class object's auto destruction. We certainly can make full copies including underlying "struct event_base"... but it's weird.

    So maybe we should just force EventBase arguments to be passed as reference variables?

  8. Anatol Belski reporter

    Hi,

    if it works, why not. Is this already commited, so i could trigger a snapshot?

    BTW, as you confirm the crash comes from the libevent, I think it would make pretty much sense to report this issue to them.

  9. Ruslan Osmanov repo owner

    Forced passing EventBase arguments by reference. All tests passed on my Windows Home Premium x64. I think we can try to make a snap.

  10. Anatol Belski reporter

    ups, looks like i was too slowly and you've pushed the release already and it's built, so no snapshot needed now :)

  11. Log in to comment