niner / inline-python-pm

Inline::Python - Write Perl subs and classes in Python.
https://metacpan.org/release/Inline-Python
20 stars 13 forks source link

Coredump when setting an environment variable in python using os.environ #7

Closed dpschmitt closed 9 years ago

dpschmitt commented 9 years ago

Python version is 2.7.8 Perl version is 5.20.1 x86_64-linux-thread-multi Inline::Python version is 0.45 OS: fedora 20, Linux 3.17.4-200.fc20.x86_64 #1 SMP Fri Nov 21 23:26:41 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

I've tried various versions of Perl (5.18.4, 5.26.3) and older versions of Inline::Python (0.39 to present) from backpan.perl.org and all generate a core dump when using os.environ to set an environment variable. Below the core dump backtrace is the example program. The workaround is to set environment variables that can be set ahead of time, and not use python libraries that use it.

Core dump:

THIS IS IN PERL* Error in `perl': munmap_chunk(): invalid pointer: 0x00007fcf9665c40c * ======= Backtrace: ========= /lib64/libc.so.6(+0x75a4f)[0x7fcf95137a4f] /lib64/libc.so.6(+0x7b8a7)[0x7fcf9513d8a7] /home/dschmitt/perl5/perlbrew/perls/perl-5.20.1/lib/5.20.1/x86_64-linux-thread-multi/CORE/libperl.so(perl_destruct+0x1515)[0x7fcf9624bfc5] perl(main+0x111)[0x400e51] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fcf950e3d65]

perl[0x400ec1]

Example program follows:

use Inline Python => <<PYTHON; import os os.environ["TEST_VARIABLE"] = "BOB" PYTHON print "Hi";

niner commented 9 years ago

Unfortunately, I cannot reproduce this bug with Perl 5.20.1 and Python 2.7.8 on openSUSE Tumbleweed. Your test produces the output "Hi" and exits normally. The only thing I noticed is, that environment variables set in Python are not accessible in Perl. I also tried it with Perl 5.16.1 and Python 2.7.3 on openSUSE 12.3 and it runs just fine.

That doesn't leave much room for explanations. Maybe different compilers? openSUSE Tumbleweed uses gcc 4.8, openSUSE 12.3 uses gcc 4.7.

dpschmitt commented 9 years ago

Hmm, that's unfortunate.

I set up a VM and installed centos 7, with perl 5.16.3 and python 2.7.9. I wanted a clean environment to try and reproduce this. The gcc version is 4.8.2.

I received the same core dump with the sample program in it. I thought maybe there was some odd library that I had installed into python that might have been the culprit, but this was a near vanilla install. One note, I had to modify the makefile of python to include -fPIC or Inline::Python would not accept it and I installed to /usr/local prefix. Any ideas on a good way to troubleshoot this?

sjgiri commented 9 years ago

Hi, I'm hitting coredump while setting environment variable using inline python as well. I used Perl 5.10.0 and Python 2.7.1. Any pointers to fix this would be very helpful. thanks!

* glibc detected * /build/toolchain/lin32/perl-5.10.0/bin/perl: free(): invalid pointer: 0x0da50904 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xf751aee2] /build/toolchain/lin32/perl-5.10.0/bin/perl(perl_destruct+0x1d07)[0x80af787] /build/toolchain/lin32/perl-5.10.0/bin/perl(main+0xc0)[0x80606e0] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xf74be4d3]

niner commented 9 years ago

I finally found out why it sometimes works and sometimes not. It does work when perl is configured with -Accflags='-DPERL_USE_SAFE_PUTENV' like openSUSE's system perl apparently is. Without this flag, Perl will modify the environment variables directly. The glibc error occurs, when perl tries to free the environment variable strings during its cleanup. Strings set by Python are not allocated the same way as in perl, so the undiscriminated free() explodes.

I don't know if there is a way to fix this. This problem should really occur with any XS module that sets environment variables to strings allocated by a custom allocator. A workaround for now is to use a perl that's compiled with -Accflags='-DPERL_USE_SAFE_PUTENV'

niner commented 9 years ago

There is actually a way to set the safer behavior at runtime. Implemented it in commit 35db0d3109880b9104c0a9d2562520997825de0c

Please try if this fixes your issues.

dpschmitt commented 9 years ago

I've tried that commit and it does stop the crashing. Thanks!