oracle / graalpython

A Python 3 implementation built on GraalVM
Other
1.17k stars 101 forks source link

Using weasyprint from from Java results in a segmentation fault #397

Closed msievers closed 1 month ago

msievers commented 1 month ago

When trying to embed the weasyprint html/css to pdf python library in a Java application the application crashes with a SIGSEV on non-trivial input data. It indeed works with very simple html/css inputs.

The error is

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000071c47883ad87, pid=16635, tid=16638
#
# JRE version: Java(TM) SE Runtime Environment Oracle GraalVM 21.0.3+7.1 (21.0.3+7) (build 21.0.3+7-LTS-jvmci-23.1-b37)
# Java VM: Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.3+7.1 (21.0.3+7-LTS-jvmci-23.1-b37, mixed mode, sharing, tiered, jvmci, jvmci compiler, compressed oops, compressed class ptrs, g1 gc, linux-amd64)
# Problematic frame:
# C  [_cffi_backend.graalpy240-310-native-x86_64-linux.so+0x10d87]  _cdata_getslicearg+0x1c

I'm using GraalVM 21.0.3 (also tried with 22.0.1) and Truffle 24.0.1.

I created a reproducer at https://github.com/msievers/graalpy-weasyprint. The README contains the basic setup steps and the application itself should be as minimal as possible.

Information about the weasyprint library can be found here:

If I can provide any other additional information please let me know.

Thanks in advance :sunflower:

msimacek commented 1 month ago

Hi @msievers, thank you for detailed reproducer, I could reproduce the segfault. I'll look into the bug shortly

msimacek commented 1 month ago

I created a fix in https://github.com/oracle/graalpython/pull/398. Since the fix only changes the patch for cffi library, you can manually apply it already if you want. To do that you can run:

source src/main/resources/vfs/venv/bin/activate
graalpy -m pip cache remove cffi
curl -L https://files.pythonhosted.org/packages/68/ce/95b0bae7968c65473e1298efb042e10cafc7bafc14d9e4f154008241c91d/cffi-1.16.0.tar.gz | tar xz
graalpy -m autopatch_capi cffi-1.16.0
sed -i 's/&PyTuple_GET_ITEM(fargs, 0)/PySequence_Fast_ITEMS(fargs)/' cffi-1.16.0/src/c/_cffi_backend.c
curl -L https://raw.githubusercontent.com/oracle/graalpython/0d4fb3ddad881f059bcd369f597d3b9aaf35f0eb/graalpython/lib-graalpython/patches/cffi/cffi-1.16.0.patch | patch -p1 -d cffi-1.16.0
graalpy -m pip install --force-reinstall --no-deps ./cffi-1.16.0
msievers commented 1 month ago

@msimacek I can confirm that it's now working as expected, even with more complex input data as in the reproducer.

I took the invoice sample from weasyprint (https://github.com/CourtBouillon/weasyprint-samples/tree/master/invoice) and even this works now without seg fault.

Thank you very much for the quick reaction and the immediate help :pray:

I think we can close this issue then, right?

msimacek commented 1 month ago

I'm glad it helped. A bot will close the issue when the PR with fix gets merged.