-
-
Notifications
You must be signed in to change notification settings - Fork 361
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
Doubly freed objects with SWIG 4.1 and PHPNG Mapscript #6433
Comments
If any of the PHP Mapscript methods mapObj::getLayer(), layerObj::getClass() or classObj::getStyle() are called more than once, the PHP process will crash during cleanup. This happens in both PHP7 and PHP8 when compiled using SWIG-4.1. Likely related to the new PHP object handling in swig/swig/pull/1982, not to the PHP8 support. Tested on Linux RHEL7.9, with PHP 7.2.24 and PHP 8.0.12 (CLI), Mapserver 7.6.2 and SWIG 4.1.0. Example script:
Result of running script with PHP8:
gdb backtrace:
|
@bragef I cannot reproduce this on Windows (MapServer-main, PHP 8.0.12, SWIG 4.1.0-master). Can you also compile MapServer-main and test against this as well? thanks. |
Same crash with MapServer-main (7.7-dev):
|
It could be failing silently on Windows too. Thanks for this report, will test more here... |
I believe the issue here is that these functions always return a reference to an existing object, but are declared as If I remove
|
Those Although interestingly the RFC also states:
https://mapserver.org/fr/development/rfc/ms-rfc-24.html#always-give-object-ownership-to-swig |
I cannot see why they are necessary from RFC-24, the allocated memory is already owned by the containing mapobj (or by a layerobj). A %newobject directive here will just add conflict over which objects owns the memory (if I understand it correctly). |
@bragef - yes the code update seems in contradiction to the RFC. Maybe try removing and adding a pull request to see if the Python MapScript test suite continues to pass (it will run on both Windows and Linux). |
I'm not knowledgeable of PHP by any means, but I do reproduce the crash with SWIG master and PHP 7.4.3 and 8.1.0 Removing the %newobject directive would be incorrect (RFC24 text is not consistent with implementation, but implementation looks correct to me. As we increase the native reference count in methods returning %newobject, things are consistent) and would cause memory leaks for other languages. My impression is that the fix should be in SWIG itself. I've filed swig/swig#2108 about that |
Side note: beyond the blocking issue with SWIG, I think PHPNG Mapscript itself might be a bit brittle, as there's no implementation of RFC 24 in mapscript/phpng/php7module.i, that is getLayer() should for example make sure that the PHP layer object keeps a reference to the belonging PHP map object (like what is done in javamodule.i, plmodule.i, pymodule.i and csmodule.i), but such issues would appear in more complex scenarios, like getting a layer from a map, dropping references to the map and calling methods on the layer that will access the map |
That seems to be it. I tested with updated swig with swig/swig/pull/2112 applied, and the crashes are gone. Also valgrind reports no lost memory. |
This also works fine with today's SWIG-master (4.1.0-dev) and PHP 8.1.0, on Windows. (calling the layer a second time, and doing stuff on that, works well) Closing. |
MapScript builds fine with SWIG-master (4.1.0-dev) and PHP 8.0.12 but chrashes when running a simple script:
`<?php
$map = new mapObj("/var/gis/mapfiles/demo.map");
$a = $map->getLayer(1);
//to this point is all right
$b = $map->getLayer(1);
//crashing when calling getLayer a second time`
/var/log/apache2/error.log
double free or corruption (!prev) [Wed Nov 03 14:39:22.650920 2021] [core:notice] [pid 19637] AH00051: child pid 19638 exit signal Aborted (6), possible coredump in /etc/apache2
Originally posted by @bjoernboldt in #6201 (comment)
The text was updated successfully, but these errors were encountered: