imagick icon indicating copy to clipboard operation
imagick copied to clipboard

Release memory in Imagick::getConfigureOptions()

Open mikhainin opened this issue 1 year ago • 3 comments

A few memleaks reported by ASAN.

it seems like MagickQueryConfigureOptions() returns not-constant memory and we will have to clean that up

$ ZEND_DONT_UNLOAD_MODULES=1 ./tests/254_getConfigureOptions.sh
Ok
==24023==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 182504 byte(s) in 44 object(s) allocated from:
    #0 0x7f53e933fbb8 in __interceptor_malloc (/lib64/libasan.so.5+0xefbb8)
    #1 0x7f53e3b2c436 in AcquireString MagickCore/string.c:107

Direct leak of 688 byte(s) in 2 object(s) allocated from:
    #0 0x7f53e933fbb8 in __interceptor_malloc (/lib64/libasan.so.5+0xefbb8)
    #1 0x7f53e39e1c89 in GetConfigureList MagickCore/configure.c:554

Indirect leak of 480 byte(s) in 44 object(s) allocated from:
    #0 0x7f53e933fbb8 in __interceptor_malloc (/lib64/libasan.so.5+0xefbb8)
    #1 0x7f53e3b2cb93 in ConstantString MagickCore/string.c:691

SUMMARY: AddressSanitizer: 183672 byte(s) leaked in 90 allocation(s).

mikhainin avatar Mar 16 '23 14:03 mikhainin

Hi,

I appear to have already committed these in 2b0ecc4e1b429236c19a428de49403b27b7c3576 and d521b3ce9dfc98d1e304a073a67b1fd7fd760235 but forgot to say anything here.

If you're able to, please can you give me (or point me to) some instruction to how to use ASAN so that I can avoid these types of errors in the future?

Danack avatar Jun 25 '24 17:06 Danack

Ah, ASAN is actually easy: https://github.com/google/sanitizers/wiki/AddressSanitizer You just need to build all the participating components (PHP, the extenstion, and ideally ImageMagick as well) with CFLAGS="-fsanitize=address -g -fno-omit-frame-pointer" LDFLAGS="-fsanitize=address". I tend to add -O0 -ggdb3 as well.

PHP - I make a separate build (working at Bumble we had a build scenario that was building all the required extensions and other components, although I don't work there anymore):

git clone https://github.com/php/php-src.git
cd php-src
./buildconf
./configure --enable-address-sanitizer --prefix=/opt/php-asan
make -j40
make install

The extension

git clone https://github.com/Imagick/imagick.git
cd imagick
/opt/php-asan/bin/phpize
./configure --with-php-config=/opt/php-asan/bin/php-config CFLAGS="-fsanitize=address -g" LDFLAGS="-fsanitize=address"
make -j10

And then run the tests (or whatever you want).

It's better to use ZEND_DONT_UNLOAD_MODULES=1: ASAN dumps stack-trace on termination, if PHP has unloaded the module, the trace will be not readable.

ZEND_DONT_UNLOAD_MODULES=1 TESTS='-j10' make test

mikhainin avatar Jul 14 '24 15:07 mikhainin

Probably, the main problem atm is that it doesn't work on ARMs (newest macbooks)

mikhainin avatar Jul 14 '24 15:07 mikhainin