dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.35k stars 201 forks source link

Segmentation fault (core dumped) with Imagick (any code, only static frankenphp sapi) #262

Open DubbleClick opened 9 months ago

DubbleClick commented 9 months ago

A few of my urls, particularly those generating pdf's using Html2pdf/TCPDF crash FrankenPHP. I'll investigate whether this is due to imagick being enabled.

2023/10/12 08:50:45.396 INFO    http.log        server running  {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2023/10/12 08:50:45.396 INFO    http.log        server running  {"name": "remaining_auto_https_redirects", "protocols": ["h1", "h2", "h3"]}
2023/10/12 08:50:45.396 INFO    autosaved config (load with --resume flag)      {"file": "/root/.config/caddy/autosave.json"}
2023/10/12 08:50:45.397 INFO    serving initial configuration
2023/10/12 08:50:50.042 INFO    http.log.access.log0    handled request {"request": {"remote_ip": "192.168.99.99", "remote_port": "58851", "client_ip": "192.168.99.99", "proto": "HTTP/2.0", "method": "GET", "host": "my.secret.url:3000", "uri": "/_profiler/phpinfo", "headers": {"Referer": ["https://my.secret.url/generate_pdf/3922"], "Accept-Encoding": ["gzip, deflate, br"], "Accept-Language": ["en,de;q=0.9"], "Sec-Ch-Ua": ["\"Chromium\";v=\"118\", \"Google Chrome\";v=\"118\", \"Not=A?Brand\";v=\"99\""], "Sec-Fetch-Mode": ["cors"], "Sec-Fetch-Dest": ["empty"], "Sec-Ch-Ua-Platform": ["\"Windows\""], "Sec-Fetch-Site": ["same-origin"], "Cookie": [], "Accept": ["text/html, application/xhtml+xml"], "Sec-Ch-Ua-Mobile": ["?0"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "my.secret.url"}}, "bytes_read": 0, "user_id": "", "duration": 0.45020249, "size": 27250, "status": 200, "resp_headers": {"Alt-Svc": ["h3=\":3000\"; ma=2592000"], "Content-Type": ["text/html; charset=UTF-8"], "Content-Encoding": ["gzip"], "X-Robots-Tag": ["noindex"], "Vary": ["Accept-Encoding"], "Server": ["Caddy"], "X-Powered-By": ["PHP/8.2.11"], "Cache-Control": ["no-cache, private"], "Date": ["Thu, 12 Oct 2023 08:50:50 GMT"]}}
Segmentation fault (core dumped)
dunglas commented 9 months ago

Could you copy the stack trace (using GDB on the core dump)? Thanks!

DubbleClick commented 9 months ago

Sorry, I'm not familiar with using GDB (all my native projects have been centered around windows with MSVC so far) so it'll take a little while.

I've confirmed that it's a problem with Imagick and FrankenPHP though, as using the php binary directly works as expected.

[root@localhost frankenphp]# tail -n100 index.php
<?php

error_reporting(E_ALL);
ini_set( 'display_errors','1');

/* Create a new imagick object */
$im = new Imagick();

/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");

/* Create imagickdraw object */
$draw = new ImagickDraw();

/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);

/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);

/* Close the pattern */
$draw->popPattern();

/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');

/* Set font size to 52 */
$draw->setFontSize(52);

/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");

/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");

/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);

/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);

/* Set the format to PNG */
$canvas->setImageFormat('png');

/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>
[root@localhost frankenphp]# /opt/static-php-cli/buildroot/bin/php index.php
�PNG

...
���I���IE
... compile frankenphp ...
[root@localhost] ./frankenphp php-cli index.php
Segmentation fault (core dumped)
DubbleClick commented 9 months ago

Doesn't say a whole lot to me:

[root@localhost frankenphp]# gdb ./frankenphp
GNU gdb (GDB) Red Hat Enterprise Linux 10.2-10.el9
Copyright (C) 2021 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-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://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 ./frankenphp...
(No debugging symbols found in ./frankenphp)
(gdb) run php-cli index.php
Starting program: /opt/frankenphp-main/caddy/frankenphp/frankenphp php-cli index.php
[New LWP 1998726]
[New LWP 1998727]
[New LWP 1998728]
[New LWP 1998729]
[New LWP 1998730]
[New LWP 1998731]
[New LWP 1998732]
[New LWP 1998733]

Thread 9 "frankenphp" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 1998733]
0x00007ffff31072c3 in ?? ()
(gdb)

How do I compile frankenphp with debug symbols?

dunglas commented 9 months ago

The easiest way is to follow these instructions: https://github.com/dunglas/frankenphp/blob/main/CONTRIBUTING.md

DubbleClick commented 8 months ago
[root@alma frankenphp]# gdb -q frankenphp
Reading symbols from frankenphp...
(gdb) run php-cli index.php
Starting program: /opt/frankenphp/caddy/frankenphp/frankenphp php-cli index.php
[New LWP 1339544]
[New LWP 1339545]
[New LWP 1339546]
[New LWP 1339547]
[New LWP 1339548]
[New LWP 1339549]
[New LWP 1339550]
[New LWP 1339551]
[New LWP 1339552]
[New LWP 1339553]
[New LWP 1339554]
[New LWP 1339555]

Thread 13 "frankenphp" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 1339555]
OpenPixelCache (image=image@entry=0x7fffb065b020, mode=mode@entry=IOMode, exception=exception@entry=0x7fffb0808140)
    at MagickCore/cache.c:3677
3677      if (IsEventLogging() != MagickFalse)
(gdb) info frame
Stack level 0, frame at 0x7fffb0e31360:
 rip = 0x25d55a3 in OpenPixelCache (MagickCore/cache.c:3677); saved rip = 0x437b4d
 called by frame at 0x7fffb0e34850
 source language c.
 Arglist at 0x7fffb0e2cd48, args: image=image@entry=0x7fffb065b020, mode=mode@entry=IOMode,
    exception=exception@entry=0x7fffb0808140
 Locals at 0x7fffb0e2cd48, Previous frame's sp is 0x7fffb0e31360
 Saved registers:
  rbx at 0x7fffb0e31328, rbp at 0x7fffb0e31330, r12 at 0x7fffb0e31338, r13 at 0x7fffb0e31340, r14 at 0x7fffb0e31348,
  r15 at 0x7fffb0e31350, rip at 0x7fffb0e31358
(gdb) info args
image = 0x7fffb065b020
mode = IOMode
exception = 0x7fffb0808140
(gdb) info locals
cache_info = <optimized out>
source_info = {storage_class = UndefinedClass, colorspace = UndefinedColorspace, alpha_trait = UndefinedPixelTrait,
  channels = UndefinedChannel, columns = 0, rows = 0, metacontent_extent = 0, number_channels = 0, channel_map = {{
      channel = UndefinedPixelChannel, traits = UndefinedPixelTrait, offset = 0} <repeats 64 times>},
  type = UndefinedCache, mode = ReadMode, disk_mode = ReadMode, mapped = MagickFalse, offset = 0, length = 0,
  virtual_pixel_method = UndefinedVirtualPixelMethod, virtual_pixel_color = {storage_class = UndefinedClass,
    colorspace = UndefinedColorspace, alpha_trait = UndefinedPixelTrait, fuzz = 0, depth = 0, count = 0, red = 0,
    green = 0, blue = 0, black = 0, alpha = 0, index = 0}, number_threads = 0, nexus_info = 0x0, pixels = 0x0,
  metacontent = 0x0, file = 0, filename = '\000' <repeats 4095 times>, cache_filename = '\000' <repeats 4095 times>,
  methods = {get_virtual_pixel_handler = 0x0, get_virtual_pixels_handler = 0x0,
    get_virtual_metacontent_from_handler = 0x0, get_one_virtual_pixel_from_handler = 0x0,
    get_authentic_pixels_handler = 0x0, get_authentic_metacontent_from_handler = 0x0,
    get_one_authentic_pixel_from_handler = 0x0, get_authentic_pixels_from_handler = 0x0,
    queue_authentic_pixels_handler = 0x0, sync_authentic_pixels_handler = 0x0, destroy_pixel_handler = 0x0},
  random_info = 0x0, server_info = 0x0, synchronize = MagickFalse, debug = MagickFalse, id = 0x0, reference_count = 0,
  semaphore = 0x0, file_semaphore = 0x0, timestamp = 0, signature = 0, opencl = 0x0, composite_mask = MagickFalse,
  width_limit = 0, height_limit = 0}
format = '\000' <repeats 4095 times>
message = '\000' <repeats 4095 times>
hosts = <optimized out>
type = <optimized out>
status = <optimized out>
length = <optimized out>
number_pixels = <optimized out>
columns = <optimized out>
packet_size = <optimized out>
__func__ = "OpenPixelCache"
(gdb) bt
#0  OpenPixelCache (image=image@entry=0x7fffb065b020, mode=mode@entry=IOMode, exception=exception@entry=0x7fffb0808140)
    at MagickCore/cache.c:3677
#1  0x0000000000437b4d in GetImagePixelCache (image=0x7fffb065b020, clone=clone@entry=MagickTrue,
    exception=exception@entry=0x7fffb0808140) at MagickCore/cache.c:1771
#2  0x00000000025d871b in SyncImagePixelCache (image=<optimized out>, exception=exception@entry=0x7fffb0808140)
    at MagickCore/cache.c:5587
#3  0x0000000002663dc8 in SetImageExtent (image=<optimized out>, columns=<optimized out>, rows=<optimized out>,
    exception=exception@entry=0x7fffb0808140) at MagickCore/image.c:2669
#4  0x000000000278fae2 in ReadMIFFImage (image_info=<optimized out>, exception=<optimized out>) at coders/miff.c:1328
#5  0x00000000025f8045 in ReadImage (image_info=image_info@entry=0x7fffb065f020,
    exception=exception@entry=0x7fffb0808140) at MagickCore/constitute.c:736
#6  0x00000000025c9dc3 in BlobToImage (image_info=image_info@entry=0x7fffb0632650, blob=blob@entry=0x7fffb06831d0,
    length=15494, exception=exception@entry=0x7fffb0808140) at MagickCore/blob.c:477
#7  0x00000000025f96a2 in ReadInlineImage (image_info=image_info@entry=0x7fffb062f360,
    content=content@entry=0x7fffb0624d10 "data:image/x-gradient;base64,\n aWQ9SW1hZ2VNYWdpY2sgdmVyc2lvbj0xLjAKY2xhc3M9RGlyZWN0Q2xhc3MgY29sb3JzPTAgYWxw\n aGEtdHJhaXQ9VW5kZWZpbmVkCm51bWJlci1jaGFubmVscz0zIG51bWJlci1tZXRhLWNoYW5uZWxz\n PTAgY2hhbm5lb"..., exception=exception@entry=0x7fffb0808140) at MagickCore/constitute.c:1177
#8  0x000000000261a3ad in DrawPrimitive (image=image@entry=0x7fffb068f3f0, draw_info=0x7fffb0687490,
    primitive_info=primitive_info@entry=0x7fffafc74550, exception=exception@entry=0x7fffb0808140)
    at MagickCore/draw.c:5612
#9  0x0000000002620258 in RenderMVGContent (image=0x7fffb068f3f0, draw_info=draw_info@entry=0x7fffb0687010,
    depth=depth@entry=0, exception=exception@entry=0x7fffb0808140) at MagickCore/draw.c:4488
#10 0x0000000002626352 in DrawPatternPath (image=0x7fffb076e2d0, draw_info=0x7fffb07abb10,
    name=0x7fffb070f260 "gradient", pattern=0x7fffb07aaf50, exception=0x7fffb0808140) at MagickCore/draw.c:4619
#11 0x0000000002622da7 in RenderMVGContent (image=0x7fffb076e2d0, draw_info=draw_info@entry=0x7fffb07abb10,
    depth=depth@entry=0, exception=<optimized out>) at MagickCore/draw.c:3673
#12 0x000000000262680a in DrawImage (image=<optimized out>, draw_info=draw_info@entry=0x7fffb07abb10,
    exception=<optimized out>) at MagickCore/draw.c:4527
#13 0x00000000025a33f3 in MagickDrawImage (wand=0x7fffb07b0110, drawing_wand=<optimized out>)
--Type <RET> for more, q to quit, c to continue without paging--
    at MagickWand/magick-image.c:3102
#14 0x0000000001bc607d in zim_Imagick_drawImage (execute_data=<optimized out>, return_value=0x7fffb0e4c4b0)
    at /opt/static-php-cli/source/php-src/ext/imagick/imagick_class.c:10393
#15 0x0000000001815705 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER ()
    at /opt/static-php-cli/source/php-src/Zend/zend_vm_execute.h:1842
#16 execute_ex (ex=<optimized out>) at /opt/static-php-cli/source/php-src/Zend/zend_vm_execute.h:56077
#17 0x000000000181da73 in zend_execute (op_array=0x7fffb0c7b000, return_value=0x0)
    at /opt/static-php-cli/source/php-src/Zend/zend_vm_execute.h:60409
#18 0x00000000017c783d in zend_execute_scripts (type=type@entry=8, retval=0x7fffb0c87700, retval@entry=0x0,
    file_count=file_count@entry=3) at /opt/static-php-cli/source/php-src/Zend/zend.c:1833
#19 0x000000000178d684 in php_execute_script (primary_file=0x7fffb0e4e8f0)
    at /opt/static-php-cli/source/php-src/main/main.c:2557
#20 0x00000000017863fc in execute_script_cli ()
#21 0x0000000002b3afea in start ()
#22 0x00007fffb0e4ea78 in ?? ()
#23 0x0000000000000000 in ?? ()
(gdb)

Apparently the crash happens here: https://github.com/ImageMagick/ImageMagick/blob/main/MagickCore/cache.c#L3677

But I'm kind of at a loss on how to proceed. Maybe I could upload the binary & php-embed used to compile it?

DubbleClick commented 8 months ago

A slightly different stacktrace after recompiling with a different version of gcc.


[root@localhost frankenphp]# /opt/static-php-cli/buildroot/bin/php index.php
�PNG

IHDR`��/� cHRMz&�����u0�`:�p��Q<bKGD�������3IDATx��yPTW�ǿ͢��.�"� �%@Dd�0F'�I
                                                                                   e2eB&q�I�+���bbb&c2ѱj̸�L�%��J����
                                                                                                                      ����*H���Ⱦ�4���n�o��Gw?������>��{ϻ���s7   �`pL@� >�@ 
                                                      �D"��؅ ���       A$�&� �@ D�8`�@  �       A$�&� �@ D�8`�@  �       A$�&�0�.ٰW$J꯬ԙ��3g��8`�����(e�1w�N���V1�Uib�`}#���jb��Y��`�]ׯ3�]�H�w����0b����^��[�j��do�M���#��i>�c,0LK�d�E����Ft,?���a<���L���320$�3t6���q�:C����~~b�oզ��;�uL
                                                                          ��NW����0��d�^Nc�O���2��p}=�e2�͟�[_Sj*�z���V9`��<�{��r�ט��O�CB���d����黯t��4�ߏ�k��lV:`�4'&Fl������3mC�������HLL`�
                                                                                  p��UU訬du���!�jj�����e7Mz�b�)����ݞN�<DK����O��z�kL|�
v11���(�7��2����y�l�~��������b�M'�զ�|���7U0H
                                               ��t�,�'sX�A8��SG�8psQ���`je�YO]ZE���
��_�-%%諫���;g��,�_�i~�kSB��
6 |NMן_6AA0����khtM7np֡��Q���II
                                     {��7�����M�C|�H
3�ͣ\�������F���I��z1`�H�����,��x8`m����           x��0;L�0H���ͨ�=a���\�vvR��۶
���b�KgL�Y��W�                               l�,�kB���S�ڸ��˘S<��
�uu�kl���̝�`����P,\�scb`<s��\��xfu99P���?;�J9��[��j^Bn}������4���52r\���8u�x�Ĵ�bt%�O��矣��m���#d�nD��7&��6���
                                                                                                                     �wߡ29�UU�w
����`5><�|��yfc���T���{�TA�A8]¶�oxtx7�A�����d|�_]���j =yǎ�r�^�lۆ�-[0����\C\��4��;Q����y�L�V�
                                                                                                          wss�{�(,�����w����~������}�Ϳ֯�MpZ�L뽃�����C����z���� 71��ȯ�j�ŋ�p-��[�r�(R_}u���W_Ū#G�uu�f�Z4P�i�ϧ�=�65�ׇ�w�A�C�3��s�w�@�������w/���#SS��i�����iPRv�F-��)���!��7p<$�UU�ʩ�D���Ӡ H��9�g@Gc#�_~�-_���6>!$���!�C�dd`P-T0"�`A|<`���""(:�\��
���ʁ8����� �[�_�ЄĀ[�����q��m�/�zj�J�iD����A��*.�ۇ
                                                          �vQ��T}�:NDG���}>%&�Xp5\N������i�sZX�����V�:[��AW[>�y�`�pᤱ5�L���|��ߠIÂ�8:�����r���Fsu5������֯������s�D�x�������'�@[]����7#`�F8���}mmh((@�W_��_P�;���8�.+���
                                                                                                            �rr�s���o�E�����f�}�%��_;OO������UW� ��I���R�닋��
                                       / i�ڌ��|�J��X@�Y������[o�rҴ4,ղ�L��Ug�8�Cۚ��R�|���
�O=��{����� $) ����vw�`O���P��(�����ʬ,T��R�I�=`��M                                         YK������S��q�
�q��=�s'���vx��Ix�~zZ������O<�����&%���a��.|����Ds������G���g��'�-|���g�T�%���/|}��+�<rɻvaTm�@�ŋ�=yaII��    �}|`��N������2l4l��ZU����L���_�˖a��z��˥/ju�U��
                                                   �yhq�bؚ�ΥW����h�3����]��x�X:;���ۦR��GmI     �z�[oa��}0V�`�����!r�|�����jeFԜ� �D���䠝��HCi)���B\;~�r���
                                       ��3--��뛐�=��8�t)�y�8s���
<W�`���m�e���?F[S�wL
                        ^<w&ZF�%       ��m���9�|�e�g?�ۇ�͛a�af�<bc��/���,kp��ޯ�Y�X��Q�ի�w��ص�qdHY�j���akz{���?z�6l8F&�_i>m����1d��!b��њϼ�0lIO�G���nld|N� �>��sz�s��#G(e�H$H<~|\�����?�i�����rt��
                                                                                       Aȇ�q��   ��L

                                                                                                   <���Z�:������cך��P�\����x�� �急Y޴�U��怽��`jfƐ�MH�u5��Ѐ��B�2d���QWZJ    ��{x�V×�X�f
                                                                   �@Hb"~��'���Ӧ�O�B���ڒ'�����[�O=�O�~����DB-�D��Ω�h�p�N�z����:s�"����h^�
~�Yx���/]�`Og;�&�~�����۶������AF���}�K|�q`�tG�@ܨ\��K�(�>NVX�z5��/�*i��V޸�!��"�%� ��Yۃ���'�6u��)�
                                                                                                          ##l���U<��anx���34anoO�O}n�t@�ih�N�z�ׯ����"�q����o�L��?4m�
}�Q��ɐ�ؼ�w}\/�{h(EOYv� �p�~�X/X@�PT���.�lUn.��ˏ58`KGG�>E6_CB�2��G���l�!81�����ڦzZ[!��
                                                                                              Z�z5h�z\�x�E^�:V..��fk䞊�<dभ\��E�����������.˨4����U�NNE��� �S�u]���V�..�ж���ec����f����t�F��         ���L
                                                                                             ��2�J�p��WK�W,[�m����l5\۔F^�,�.o���ŵl��B��r�9`�b�������oS6��9�O��rM�7���M�4�����Q�9�Ό��i�ө�A���B�����+{����NO8.��EکS�k�Y��n���]���``B�y�_�74��
       �];vmT.GiNE���l��IJ5[
                              �#$�W~\۔�����N������Ѫ6����Q���t�1�"k����P�xR���v��O���5<���\�N��iG��wY���W�8
                                                                                                                    &��2��N    -��ى۴��4M�'"�VV�S
                           g䥤P��ݴ��b-�1m���m��x�ǵMu*���H$p����.���/�����4s��v)��p;mœ�So_�r�j)rOG�
                                                                                                        fs��Ŝ9
                                                                                                              }�|J��ss���u03'�2O6��%�j����ǚ061�\�\�@�m�����������Q�47��6�A{GL��'4���V� �Ɍ��v���3������֫b_v��A�����)�S3�C3�X6(�%���$�==�T�X&Oy��*���ÎC�/���)�ݭ��=��E,hq�bښ>�N|�ܦhK�Mf�\G��B�q_�}6��?�+�L--1��]����Yy�4

                                                                                                 �]��U�l�lF��5�¹0�����6����A�B�q��^���[�
h+��-Z;WW��Ĵ5��
S+��G�'�s\]!U�Ƴ%�?�m/�Yʟ}*��O~�w;�kn6�>�9��A��g�:�66z�M�ʕ�<���k��^Ujhˏ�lj��p�?�������]�NI���[��m)z�8�?�ik��g":�i�l��Ĩ\.8
       �I�'l��϶�Ƭ     �ژ�<���Y��Qd[[Z
                                       a�-�026�
                                                �O��Tp9k+*��ֳ�qtļ%K(y�Tn�s���f�@��Նl���9B!K�!h��'Ŵ�.N��ڦ�(2C
�KK׳��\P
�i       �u n�
k"���~���٢�7��D9MO=y����>�������h�ɐEs�~��0���7��=egO�p�en��Ų�.0�6�M[�2
M[8@�a����Z��a �9�����s�(���o�Y6E��~�>�\��+��!�J1_�yf.P򲲱��         �0'GP{��Q�<��i<
ɬYR����ŋ�������h���!��'zB
/��;�+?���Qj��� ��d     l��2d��=`�J}�  �u%\Pd$�����x�qW���=8��c)��yV��VN.1`��A˗3l���Ӽ�S#��Ff&EO�c�i]�+B��1bdD���?����Nʵp���f��!h�
����d��ˣ���x��X���~&����>˖Qd/������/�|u���p����8��l�4ՙ�m߰�X�nE���_�Rm��
                                                                         �n�@���=�ZfS�3�r�F���'���e#mm���

                                                                                                            x�E^:�bmc� J��nQ�vv���86��������\
�'�D���lݴ��􍌰i��ف���x���(�==ؾy3�5�K����p�E���;��[���7a,���G��`$���(�@�!H$x��nxb�ڐ�p��M063������8v������b�+��ۛcy��űcc��ٳj=yy({��p�*�;���MDJh'�ҩ���ƄT���Z�q#xyq.���|y�NXҶﻔ���z
mZfz(
���a�dyYv��6LL
              2T�dٞR=E�������ܹ�z.Y;1F1lm����5����0���=��o�����㩸8ܭ���>��7���?�����e�NC,�Hp��)̢M)�]V���0$%&"��ܭ��;�lA���(PnȢJV8p�0o;�R����=ʸ��
                               �x1���ȿy��@ue%�{�����������n6��8"M�GEa��D�����U'��PO�b���=`xm�^xѦ8��;�
                                                                                                                      �����n����22���jRb��pG*���䀅��<�P�gׯW��TYQ�3�_}�C����ޟSS�����$�ٳ�W���ס�)�y���T^���|sB�1�˖)Z��u����2<��LA�MH�wg                                                                                                                      g�j���o��Ӑ������hw|Ҧ*���������(/�\[�o��A�/<(hBm`2@��ƀUĮZ��4X�gpM�nn�.%�EE����B���~�=����!72T�山�)#v��:~R�X�!<��/���C�һ�H���i(b�Z=`.m���/Í�q>���ϟ���9����ƀՉ���m�[�og}Aْ��^޺�����c�˩Kyuv��:ro�B�81U�����O#%=V�G���;Almm�`��q��.�-�S%IJ�>b�\uxy{#�����LMǭ��[�o���l8��0�?@���1yhn~��A�J�����v��
���?�?A#��8`�@�Db��@ �L "A0�@ �q�� ��HL "A0�@ �q�� ��HL "A0�@ �q�� ��HL "A0�@ �q�� ��HL "!�n��$G����I���IECGO_CFLAGS="$(/opt/static-php-cli/buildroot/bin/php-config --includes | sed s#-I/#-I/opt/static-php-cli/buildroot/#g)" CGO_LDFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(/opt/static-php-cli/buildroot/bin/php-config --ldflags) -Wl,--start-group $(/opt/static-php-cli/buildroot/bin/php-config --libs | sed -e 's/-lgcc_s//g') -Wl,--end-group" LIBPHP_VERSION="$(/opt/static-php-cli/buildroot/bin/php-config --version)" go build -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags -static-pie -w -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $LIBPHP_VERSION Caddy'" && ./frankenphp version
FrankenPHP  PHP  Caddy v2.7.4 h1:J8nisjdOxnYHXlorUKXY75Gr6iBfudfoGhrJ8t7/flI=
[root@localhost frankenphp]# ./frankenphp php-cli index.php
Segmentation fault (core dumped)
[root@localhost frankenphp]# gdb -q frankenphp
Reading symbols from frankenphp...
(gdb) run php-cli index.php
Starting program: /opt/frankenphp-main/caddy/frankenphp/frankenphp php-cli index.php
[New LWP 2465459]
[New LWP 2465460]
[New LWP 2465461]
[New LWP 2465462]
[New LWP 2465463]
[New LWP 2465464]
[New LWP 2465465]
[New LWP 2465466]

Thread 9 "frankenphp" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 2465466]
OpenPixelCache (image=image@entry=0x7fffa9232340, mode=mode@entry=IOMode, exception=exception@entry=0x7fffa951d180)
    at MagickCore/cache.c:3680
3680      if (IsEventLogging() != MagickFalse)
(gdb) bt
#0  OpenPixelCache (image=image@entry=0x7fffa9232340, mode=mode@entry=IOMode, exception=exception@entry=0x7fffa951d180)
    at MagickCore/cache.c:3680
#1  0x00007ffff12b38ab in GetImagePixelCache (image=0x7fffa9232340, clone=clone@entry=MagickTrue,
    exception=exception@entry=0x7fffa951d180) at MagickCore/cache.c:1784
#2  0x00007ffff338117b in SyncImagePixelCache (image=<optimized out>, exception=exception@entry=0x7fffa951d180)
    at MagickCore/cache.c:5613
#3  0x00007ffff340d058 in SetImageExtent (image=<optimized out>, columns=<optimized out>, rows=<optimized out>,
    exception=exception@entry=0x7fffa951d180) at MagickCore/image.c:2669
#4  0x00007ffff3536d49 in ReadMIFFImage (image_info=<optimized out>, exception=<optimized out>) at coders/miff.c:1328
#5  0x00007ffff33a088a in ReadImage (image_info=image_info@entry=0x7fffa9239360,
    exception=exception@entry=0x7fffa951d180) at MagickCore/constitute.c:736
#6  0x00007ffff337207e in BlobToImage (image_info=image_info@entry=0x7fffa9236040, blob=blob@entry=0x7fffa953b020,
    length=15494, exception=exception@entry=0x7fffa951d180) at MagickCore/blob.c:477
#7  0x00007ffff33a1f32 in ReadInlineImage (image_info=image_info@entry=0x7fffa9557660,
    content=content@entry=0x7fffa9205e80 "data:image/x-gradient;base64,\n aWQ9SW1hZ2VNYWdpY2sgdmVyc2lvbj0xLjAKY2xhc3M9RGlyZWN0Q2xhc3MgY29sb3JzPTAgYWxw\n aGEtdHJhaXQ9VW5kZWZpbmVkCm51bWJlci1jaGFubmVscz0zIG51bWJlci1tZXRhLWNoYW5uZWxz\n PTAgY2hhbm5lb"..., exception=exception@entry=0x7fffa951d180) at MagickCore/constitute.c:1177
#8  0x00007ffff33c3573 in DrawPrimitive (image=image@entry=0x7fffa955b200, draw_info=draw_info@entry=0x7fffa95c1490,
    primitive_info=0x7fffa8855380, exception=exception@entry=0x7fffa951d180) at MagickCore/draw.c:5612
#9  0x00007ffff33c951d in RenderMVGContent (image=0x7fffa955b200, draw_info=draw_info@entry=0x7fffa95c1010,
    depth=depth@entry=0, exception=exception@entry=0x7fffa951d180) at MagickCore/draw.c:4488
#10 0x00007ffff33cf5eb in DrawPatternPath (image=image@entry=0x7fffa95ea110, draw_info=draw_info@entry=0x7fffa969bb10,
    name=name@entry=0x7fffa955f080 "gradient", pattern=pattern@entry=0x7fffa969af50,
    exception=exception@entry=0x7fffa951d180) at MagickCore/draw.c:4619
#11 0x00007ffff33cce33 in RenderMVGContent (image=0x7fffa95ea110, draw_info=draw_info@entry=0x7fffa969bb10,
    depth=depth@entry=0, exception=<optimized out>) at MagickCore/draw.c:2960
#12 0x00007ffff33cfada in DrawImage (image=<optimized out>, draw_info=draw_info@entry=0x7fffa969bb10,
    exception=<optimized out>) at MagickCore/draw.c:4527
#13 0x00007ffff3349e6a in MagickDrawImage (wand=0x7fffa95ef270, drawing_wand=<optimized out>)
--Type <RET> for more, q to quit, c to continue without paging--
    at MagickWand/magick-image.c:3102
#14 0x00007ffff2a7612a in zim_Imagick_drawImage (execute_data=<optimized out>, return_value=0x7fffa9ddc360)
    at /opt/static-php-cli/source/php-src/ext/imagick/imagick_class.c:10393
#15 0x00007ffff269d8e2 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER ()
    at /opt/static-php-cli/source/php-src/Zend/zend_vm_execute.h:1842
#16 execute_ex (ex=<optimized out>) at /opt/static-php-cli/source/php-src/Zend/zend_vm_execute.h:56077
#17 0x00007ffff26a6641 in zend_execute (op_array=0x7fffa9a7b000, return_value=0x0)
    at /opt/static-php-cli/source/php-src/Zend/zend_vm_execute.h:60409
#18 0x00007ffff264b616 in zend_execute_scripts (type=type@entry=8, retval=0x7fffa9a87700, retval@entry=0x0,
    file_count=file_count@entry=3) at /opt/static-php-cli/source/php-src/Zend/zend.c:1833
#19 0x00007ffff260c278 in php_execute_script (primary_file=0x7fffa9dde7b0)
    at /opt/static-php-cli/source/php-src/main/main.c:2557
#20 0x00007ffff2603a5c in execute_script_cli ()
#21 0x00007ffff38f838b in start ()
#22 0x00007fffa9ddeb38 in ?? ()
#23 0x0000000000000000 in ?? ()
(gdb)```
dunglas commented 8 months ago

Thanks! This looks like a bug in ImageMagick or in the ImageMagick extension with ZTS builds. Could you report this upstream please? (https://github.com/Imagick/imagick)

DubbleClick commented 8 months ago

NTS/ZTS didn't seem to make a difference, got the same stack trace for both. It must somehow be limited to the embed SAPI though, as the CLI binary runs it without a problem. Will report to the imagick repo.

dunglas commented 8 months ago

NTS isn't supported by FrankenPHP and we don't use the embed SAPI, except for the cli tool we provide. Di you try the (official) CLI SAPI compiled with ZTS?

DubbleClick commented 8 months ago

I've used static-php-cli to build php-embed (which I believe FrankenPHP uses for the static build) and php-cli. Using the generated cli binary, Imagick works fine. Using FrankenPHP, both with ./frankenphp php-cli index.php and with trying to serve a request that involves Imagick in any way, leads to the segfault.

DubbleClick commented 8 months ago

Full log from console:

alpine:/opt/static-php-cli# ./bin/spc build imagick --with-libs=libpng,libjpeg,libwebp,freetype --enable-zts --build-embed --build-cli
     _        _   _                 _
 ___| |_ __ _| |_(_) ___      _ __ | |__  _ __
/ __| __/ _` | __| |/ __|____| '_ \| '_ \| '_ \
\__ \ || (_| | |_| | (_|_____| |_) | | | | |_) |
|___/\__\__,_|\__|_|\___|    | .__/|_| |_| .__/   v2.0.0
                             |_|         |_|
[15:48:26] [INFO] [EXEC] echo | gcc -E -x c - -march=corei7 2>/dev/null
[15:48:26] [INFO] [EXEC] echo | gcc -E -x c - -mtune=core-avx2 2>/dev/null
[15:48:26] [INFO] Build target: cli, embed
[15:48:26] [INFO] Enabled extensions: imagick
[15:48:26] [INFO] Required libraries: zlib, libpng, libjpeg, libwebp, freetype, imagemagick
[15:48:28] [INFO] extracting pkg-config source
[15:48:28] [INFO] extracting zlib source
[15:48:28] [INFO] extracting libpng source
[15:48:28] [INFO] extracting libjpeg source
[15:48:28] [INFO] extracting libwebp source
[15:48:28] [INFO] extracting freetype source
[15:48:28] [INFO] extracting imagemagick source
[15:48:29] [INFO] Building required library [pkg-config]
[15:48:29] [INFO] Entering dir: /opt/static-php-cli/source/pkg-config
[15:48:29] [INFO] [EXEC] PKG_CONFIG_PATH="/opt/static-php-cli/buildroot/lib/pkgconfig" CC='gcc' CXX='g++' ./configure --disable-shared --enable-static --with-internal-glib --prefix=/opt/static-php-cli/buildroot --without-sysroot --without-system-include-path --without-system-library-path --without-pc-path
[15:48:37] [INFO] [EXEC] make clean
[15:48:38] [INFO] [EXEC] make -j16
[15:48:41] [INFO] [EXEC] make install
[15:48:41] [INFO] lib [pkg-config] build success
[15:48:41] [INFO] Building required library [zlib]
[15:48:41] [INFO] Entering dir: /opt/static-php-cli/source/zlib
[15:48:41] [INFO] [EXEC] PKG_CONFIG='/opt/static-php-cli/buildroot/bin/pkg-config' PKG_CONFIG_PATH='/opt/static-php-cli/buildroot/lib/pkgconfig' CC='gcc' CXX='g++' PATH='/opt/static-php-cli/buildroot/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin' ./configure --static --prefix=
[15:48:42] [INFO] [EXEC] make clean
[15:48:42] [INFO] [EXEC] make -j16
[15:48:42] [INFO] [EXEC] make install DESTDIR=/opt/static-php-cli/buildroot
[15:48:42] [INFO] Patching library [zlib] pkgconfig
[15:48:42] [INFO] lib [zlib] build success
[15:48:42] [INFO] Building required library [libpng]
[15:48:42] [INFO] Entering dir: /opt/static-php-cli/source/libpng
[15:48:42] [INFO] [EXEC] chmod +x ./configure
[15:48:42] [INFO] [EXEC] chmod +x ./install-sh
[15:48:42] [INFO] [EXEC] PKG_CONFIG='/opt/static-php-cli/buildroot/bin/pkg-config' PKG_CONFIG_PATH='/opt/static-php-cli/buildroot/lib/pkgconfig' CC='gcc' CXX='g++' PATH='/opt/static-php-cli/buildroot/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin' ./configure --host=x86_64-unknown-linux --disable-shared --enable-static --enable-hardware-optimizations --with-zlib-prefix="/opt/static-php-cli/buildroot" --enable-intel-sse --prefix=
[15:48:45] [INFO] [EXEC] make clean
[15:48:45] [INFO] [EXEC] make -j16 DEFAULT_INCLUDES='-I. -I/opt/static-php-cli/buildroot/include' LIBS= libpng16.la
[15:48:46] [INFO] [EXEC] make install-libLTLIBRARIES install-data-am DESTDIR=/opt/static-php-cli/buildroot
[15:48:46] [INFO] Entering dir: /opt/static-php-cli/buildroot/lib
[15:48:46] [INFO] [EXEC] ln -sf libpng16.a libpng.a
[15:48:46] [INFO] Patching library [libpng] pkgconfig
[15:48:46] [INFO] lib [libpng] build success
[15:48:46] [INFO] Building required library [libjpeg]
[15:48:46] [INFO] Entering dir: /opt/static-php-cli/source/libjpeg/build
[15:48:46] [INFO] [EXEC] PKG_CONFIG='/opt/static-php-cli/buildroot/bin/pkg-config' PKG_CONFIG_PATH='/opt/static-php-cli/buildroot/lib/pkgconfig' CC='gcc' CXX='g++' PATH='/opt/static-php-cli/buildroot/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin' cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/ -DCMAKE_INSTALL_LIBDIR=/lib -DCMAKE_INSTALL_INCLUDEDIR=/include -DCMAKE_TOOLCHAIN_FILE=/opt/static-php-cli/source/toolchain.cmake -DENABLE_STATIC=ON -DENABLE_SHARED=OFF ..
[15:48:47] [INFO] [EXEC] cmake --build . -j 16
[15:48:54] [INFO] [EXEC] make install DESTDIR=/opt/static-php-cli/buildroot
[15:48:54] [INFO] Patching library [libjpeg] pkgconfig
[15:48:54] [INFO] lib [libjpeg] build success
[15:48:54] [INFO] Building required library [libwebp]
[15:48:54] [INFO] Entering dir: /opt/static-php-cli/source/libwebp/build
[15:48:54] [INFO] [EXEC] PKG_CONFIG='/opt/static-php-cli/buildroot/bin/pkg-config' PKG_CONFIG_PATH='/opt/static-php-cli/buildroot/lib/pkgconfig' CC='gcc' CXX='g++' PATH='/opt/static-php-cli/buildroot/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin' cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/ -DCMAKE_INSTALL_LIBDIR=/lib -DCMAKE_INSTALL_INCLUDEDIR=/include -DCMAKE_TOOLCHAIN_FILE=/opt/static-php-cli/source/toolchain.cmake -DBUILD_SHARED_LIBS=OFF -DWEBP_BUILD_EXTRAS=ON ..
[15:48:55] [INFO] [EXEC] cmake --build . -j 16
[15:48:59] [INFO] [EXEC] make install DESTDIR=/opt/static-php-cli/buildroot
[15:49:00] [INFO] Patching library [libwebp] pkgconfig
[15:49:00] [INFO] Patching library [libwebp] pkgconfig
[15:49:00] [INFO] lib [libwebp] build success
[15:49:00] [INFO] Building required library [freetype]
[15:49:00] [INFO] Entering dir: /opt/static-php-cli/source/freetype
[15:49:00] [INFO] [EXEC] PKG_CONFIG='/opt/static-php-cli/buildroot/bin/pkg-config' PKG_CONFIG_PATH='/opt/static-php-cli/buildroot/lib/pkgconfig' CC='gcc' CXX='g++' PATH='/opt/static-php-cli/buildroot/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin' ./configure --enable-static --disable-shared --without-harfbuzz --prefix= --with-png --without-bzip2 --without-brotli
[15:49:01] [INFO] [EXEC] make clean
[15:49:01] [INFO] [EXEC] make -j16
[15:49:06] [INFO] [EXEC] make install DESTDIR=/opt/static-php-cli/buildroot
[15:49:06] [INFO] Patching library [freetype] pkgconfig
[15:49:06] [INFO] lib [freetype] build success
[15:49:06] [INFO] Building required library [imagemagick]
[15:49:06] [INFO] Entering dir: /opt/static-php-cli/source/imagemagick
[15:49:06] [INFO] [EXEC] PKG_CONFIG='/opt/static-php-cli/buildroot/bin/pkg-config' PKG_CONFIG_PATH='/opt/static-php-cli/buildroot/lib/pkgconfig' CC='gcc' CXX='g++' PATH='/opt/static-php-cli/buildroot/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin' ./configure --enable-static --disable-shared --without-jxl --without-xml --without-x --disable-openmp --without-zip --with-jpeg --with-png --with-webp --without-zstd --with-freetype --prefix=
[15:49:16] [INFO] [EXEC] make clean
[15:49:16] [INFO] [EXEC] make -j16
[15:49:39] [INFO] [EXEC] make install DESTDIR=/opt/static-php-cli/buildroot
[15:49:39] [INFO] Patching library [imagemagick] pkgconfig
[15:49:39] [INFO] lib [imagemagick] build success
[15:49:39] [INFO] extracting php-src source
[15:49:41] [INFO] extracting micro source
patching file build/order_by_dep.awk
patching file ext/opcache/ZendAccelerator.c
Hunk #1 succeeded at 93 (offset 2 lines).
Hunk #2 succeeded at 4783 (offset -28 lines).
patching file ext/opcache/config.m4
patching file ext/opcache/config.w32
patching file main/main.c
Hunk #1 succeeded at 2012 with fuzz 2 (offset 1 line).
Hunk #2 succeeded at 2269 (offset 4 lines).
patching file win32/build/confutils.js
Hunk #1 succeeded at 1534 (offset -1 lines).
patching file ext/fileinfo/config.w32
patching file ext/openssl/config.w32
patching file TSRM/tsrm_win32.c
Hunk #1 succeeded at 530 (offset -1 lines).
patching file ext/ffi/ffi.c
Hunk #1 succeeded at 5293 (offset 46 lines).
patching file ext/opcache/ZendAccelerator.c
Hunk #1 succeeded at 2845 (offset 15 lines).
Hunk #2 succeeded at 3150 (offset 22 lines).
Hunk #3 succeeded at 3162 with fuzz 2 (offset 22 lines).
patching file ext/pcre/php_pcre.c
Hunk #1 succeeded at 300 (offset 9 lines).
patching file ext/readline/readline_cli.c
patching file ext/sqlite3/sqlite3.c
patching file ext/standard/php_fopen_wrapper.c
patching file ext/standard/proc_open.c
patching file main/main.c
Hunk #2 succeeded at 1354 (offset 14 lines).
patching file win32/console.c
patching file configure.ac
Hunk #1 succeeded at 1114 (offset 109 lines).
patching file win32/winutil.c
patching file win32/build/confutils.js
Hunk #1 succeeded at 3452 (offset -2 lines).
Hunk #2 succeeded at 3464 (offset -2 lines).
patching file Zend/zend_stream.c
[15:49:41] [INFO] Patched source [micro] after extracted
[15:49:41] [INFO] extracting ext-imagick source
[15:49:41] [INFO] Extension [imagick] patched before buildconf
[15:49:41] [INFO] Entering dir: /opt/static-php-cli/source/php-src
[15:49:41] [INFO] [EXEC] ./buildconf --force
[15:49:45] [INFO] Entering dir: /opt/static-php-cli/source/php-src
[15:49:45] [INFO] Using configure: --with-imagick=/opt/static-php-cli/buildroot
[15:49:45] [INFO] [EXEC] ./configure --prefix= --with-valgrind=no --enable-shared=no --enable-static=yes --disable-all --disable-cgi --disable-phpdbg --enable-cli --disable-fpm --enable-embed=static --disable-micro --enable-zts --disable-zend-signals --enable-zend-max-execution-timers --with-imagick=/opt/static-php-cli/buildroot PKG_CONFIG='/opt/static-php-cli/buildroot/bin/pkg-config' PKG_CONFIG_PATH='/opt/static-php-cli/buildroot/lib/pkgconfig' CC='gcc' CXX='g++' CFLAGS='' LIBS='-ldl -lpthread' PATH='/opt/static-php-cli/buildroot/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin'
[15:49:52] [INFO] Extension [imagick] patched before make
[15:49:52] [INFO] cleaning up
[15:49:52] [INFO] Entering dir: /opt/static-php-cli/source/php-src
[15:49:52] [INFO] [EXEC] make clean
[15:49:52] [INFO] building cli
[15:49:52] [INFO] Entering dir: /opt/static-php-cli/source/php-src
[15:49:52] [INFO] [EXEC] sed -i "s|//lib|/lib|g" Makefile
[15:49:52] [INFO] [EXEC] make -j16 EXTRA_CFLAGS='-g -Os -fno-ident -Xcompiler -march=corei7 -Xcompiler -mtune=core-avx2' EXTRA_LIBS='/opt/static-php-cli/buildroot/lib/libMagick++-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickCore-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickWand-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libfreetype.a /opt/static-php-cli/buildroot/lib/libwebp.a /opt/static-php-cli/buildroot/lib/libwebpdecoder.a /opt/static-php-cli/buildroot/lib/libwebpdemux.a /opt/static-php-cli/buildroot/lib/libwebpmux.a /opt/static-php-cli/buildroot/lib/libsharpyuv.a /opt/static-php-cli/buildroot/lib/libjpeg.a /opt/static-php-cli/buildroot/lib/libturbojpeg.a /opt/static-php-cli/buildroot/lib/libpng16.a /opt/static-php-cli/buildroot/lib/libz.a  /opt/static-php-cli/buildroot/lib/libMagick++-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickWand-7.Q16HDRI.a  /opt/static-php-cli/buildroot/lib/libMagickCore-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libjpeg.a /opt/static-php-cli/buildroot/lib/libpng.a /opt/static-php-cli/buildroot/lib/libwebp.a /opt/static-php-cli/buildroot/lib/libfreetype.a  /opt/static-php-cli/buildroot/lib/libMagick++-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickWand-7.Q16HDRI.a  /opt/static-php-cli/buildroot/lib/libMagickCore-7.Q16HDRI.a -lgomp ' EXTRA_LDFLAGS_PROGRAM=' -all-static' cli
[15:50:24] [INFO] Entering dir: /opt/static-php-cli/source/php-src/sapi/cli
[15:50:24] [INFO] [EXEC] strip --strip-all php
[15:50:24] [INFO] Deploying cli file
[15:50:24] [INFO] [EXEC] cp '/opt/static-php-cli/source/php-src/sapi/cli/php' '/opt/static-php-cli/buildroot/bin/'
[15:50:24] [INFO] building embed
[15:50:24] [INFO] Entering dir: /opt/static-php-cli/source/php-src
[15:50:24] [INFO] [EXEC] sed -i "s|//lib|/lib|g" Makefile
[15:50:24] [INFO] [EXEC] make INSTALL_ROOT=/opt/static-php-cli/buildroot -j16 EXTRA_CFLAGS='-g -Os -fno-ident -fPIE -Xcompiler -march=corei7 -Xcompiler -mtune=core-avx2' EXTRA_LIBS='/opt/static-php-cli/buildroot/lib/libMagick++-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickCore-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickWand-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libfreetype.a /opt/static-php-cli/buildroot/lib/libwebp.a /opt/static-php-cli/buildroot/lib/libwebpdecoder.a /opt/static-php-cli/buildroot/lib/libwebpdemux.a /opt/static-php-cli/buildroot/lib/libwebpmux.a /opt/static-php-cli/buildroot/lib/libsharpyuv.a /opt/static-php-cli/buildroot/lib/libjpeg.a /opt/static-php-cli/buildroot/lib/libturbojpeg.a /opt/static-php-cli/buildroot/lib/libpng16.a /opt/static-php-cli/buildroot/lib/libz.a  /opt/static-php-cli/buildroot/lib/libMagick++-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickWand-7.Q16HDRI.a  /opt/static-php-cli/buildroot/lib/libMagickCore-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libjpeg.a /opt/static-php-cli/buildroot/lib/libpng.a /opt/static-php-cli/buildroot/lib/libwebp.a /opt/static-php-cli/buildroot/lib/libfreetype.a  /opt/static-php-cli/buildroot/lib/libMagick++-7.Q16HDRI.a /opt/static-php-cli/buildroot/lib/libMagickWand-7.Q16HDRI.a  /opt/static-php-cli/buildroot/lib/libMagickCore-7.Q16HDRI.a -lgomp ' EXTRA_LDFLAGS_PROGRAM=' -all-static' install
[15:50:27] [INFO] running cli sanity check
[15:50:27] [INFO] [EXEC] /opt/static-php-cli/buildroot/bin/php -r "echo \"hello\";"
[15:50:27] [INFO] Build complete, used 120.783 s !
[15:50:27] [INFO] Static php binary path: /opt/static-php-cli/buildroot/bin/php
[15:50:27] [INFO] License path: /opt/static-php-cli/buildroot/license/
alpine:/opt/static-php-cli# cd /opt/frankenphp/caddy/frankenphp/
alpine:/opt/frankenphp/caddy/frankenphp# CGO_CFLAGS="$(/opt/static-php-cli/buildroot/bin/php-config --includes | sed s#-
I/#-I/opt/static-php-cli/buildroot/#g)"     CGO_LDFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $(/opt/static-php-cli/
buildroot/bin/php-config --ldflags) -Wl,--start-group $(/opt/static-php-cli/buildroot/bin/php-config --libs | sed -e 's/
-lgcc_s//g') -Wl,--end-group"     LIBPHP_VERSION="$(/opt/static-php-cli/buildroot/bin/php-config --version)"     go buil
d -buildmode=pie -tags "cgo netgo osusergo static_build" -ldflags "-linkmode=external -extldflags -static-pie -s -w -X '
github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $LIBPHP_VERSION Caddy'"
alpine:/opt/frankenphp/caddy/frankenphp# /opt/static-php-cli/buildroot/bin/php magick.php
�PNG

IHDR7n�$ cHRMz&�����u0�`:�p��Q<bKGD݊�
alpine:/opt/frankenphp/caddy/frankenphp# ./frankenphp php-cli magick.php
Segmentation fault
alpine:/opt/frankenphp/caddy/frankenphp# tail magick.php
<?php

$image = new Imagick();
$image->newImage(1, 1, new ImagickPixel('#ffffff'));
$image->setImageFormat('png');
$pngData = $image->getImagesBlob();
echo $pngData;

?>
alpine:/opt/frankenphp/caddy/frankenphp#
DubbleClick commented 8 months ago

@dunglas I didn't really make progress tracking the error(s) down, but I'm fairly certain that it's limited to libphp.a embedding/FrankenPHP. Using the php-fpm binary or the php-cli binary produced with the same ./configure options for php works just dandy with imagick.

For what it's worth, I've tried enabling and disabling threading in the imagemagick library and tried limiting the imagick runtime threads to 1. Also tried with and without the libjpeg library and frankenphp segfaults one way or the other.

The imagick extension maintainer doesn't see as any work having to be done with the extension itself, as php-fpm and php-cli work.

dunglas commented 8 months ago

Could you try using the Docker images, which loads libphp as a shared library, to see if it's related to static compilation or not please?

DubbleClick commented 8 months ago

Err, I'm trying, but I can't seem to get the docker image to actually serve files from my current directory?

/opt/frankenphp/caddy/frankenphp/Caddyfile

{
        {$CADDY_GLOBAL_OPTIONS}

        frankenphp {
                #worker /path/to/your/worker.php
                {$FRANKENPHP_CONFIG}
        }
        order php_server before file_server
}

{$CADDY_EXTRA_CONFIG}

{$SERVER_NAME:localhost} {
        log {
                # Redact the authorization query parameter that can be set by Mercure
                format filter {
                        wrap console
                        fields {
                                uri query {
                                        replace authorization REDACTED
                                }
                        }
                }
        }

        root * /opt/frankenphp/caddy/frankenphp

        {$CADDY_SERVER_EXTRA_DIRECTIVES}

        encode zstd gzip

        php_server
}

/opt/frankenphp/caddy/frankenphp/index.php


<?php

$image = new Imagick();
$image->newImage(1, 1, new ImagickPixel('#ffffff'));
$image->setImageFormat('png');
$pngData = $image->getImagesBlob();
echo $pngData;

?>

docker run -v $PWD -p 443:443 dunglas/frankenphp curl https://localhost/magick.php -k

Answers with a phpinfo() response, rather than serving magick.php.

Edit: yeah, nevermind, I forgot to map the directory correctly.

Either way, imagick does not exist in the phpinfo response, so I wouldn't be able to test if it works.

dunglas commented 8 months ago

You can add imagemagick like this:

FROM dunglas/frankenphp

RUN install-php-extensions imagick

Then: docker build -t myimage . and docker run -v $PWD:/app/public -p 443:443 myimage

DubbleClick commented 8 months ago

Sorry, I'm not familiar with docker at all. I suppose I edit one of the dockerfiles in the repo root here, or create one myself. Will get it working and report back tomorrow.

dunglas commented 8 months ago

No worries! Just copy the content of the Dockerfile I provided in a file named Dockerfile in the current directory of your code and run the command. That should be enough (This tiny Dockerfile extends the official image and use a provided script to install extra extensions).

DubbleClick commented 8 months ago

Using

FROM dunglas/frankenphp
RUN install-php-extensions
    gd \
    intl
    imagick

docker run -v $PWD:/app/public -p 443:443 img2

[root@alma frankenphp]# curl -k https://localhost/magick.php --output -
�PNG
⸮
IHDR7n�$gAMA��
              �a cHRMz&�����u0�`:�p��Q<bKGD݊�
IDA�ch���Cj�IEND�B`�[root@alma frankenphp]#

So that works. Seems to be a problem between FrankenPHP and static imagemagick. Either work fine on their own, just not together. What does FrankenPHP do different than php-cli and php-fpm?

For what it's worth, a workaround is to run static-php-cli with --build-embed --build-fpm --enable-zts, build FrankenPHP and then copy both php-fpm and FrankenPHP. In the Caddyfile I define two routes, first one that matches pdf/imagick/calendar (many doctrine entities problem) routes with php_fastcgi to the php-fpm binary and second a FrankenPHP handler for everything else.

dunglas commented 2 months ago

Do you know if the problem still occurs? Static PHP CLI fixed some related issues.

DubbleClick commented 2 months ago

Only one way to find out! Could you quickly add imagick to the extension list and trigger a frankenphp build? I won't be able to until Monday or Tuesday.