binmode + mod_perl 2.0.5 + Parse :: RecDescent = segmentaion fault

Hln*_*Hln 7 perl mod-perl2 segmentation-fault

重要更新:问题与Apache或mod_perl无关.最简单的演示:

> perl -le 'use PerlIO::via::QuotedPrint; binmode(\*STDERR, ":via(PerlIO::via::QuotedPrint):utf8"); open (ERROR, ">&STDERR");'
zsh: segmentation fault  perl -le
Run Code Online (Sandbox Code Playgroud)

实际上binmode是由我的代码和open (ERROR, ">&STDERR");Parse :: RecDescent执行的.


原始问题:

我在mod_perl 2.0.5下遇到了Spreadsheet :: WriteExcel的问题Apache死了有分段错误,我发现它发生 require Parse::RecDescentSpreadsheet::WriteExcel包内的语句中.

strace显示最后发生的事情是重复STDERR:

[pid 31253] dup(2)                      = 8
[pid 31253] ioctl(8, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fffcf66a328) = -1 ENOTTY (Inappropriate ioctl for device)
[pid 31253] lseek(8, 0, SEEK_CUR)       = 0
[pid 31253] --- SIGSEGV (Segmentation fault) @ 0 (0) ---
Run Code Online (Sandbox Code Playgroud)

我仔细阅读了代码Parse::RecDescent和注意到的语句open (ERROR, ">&STDERR");

好吧,经过一些额外的实验,我有这个简约的Plack应用程序来重现段错误:

use strict;
use warnings;

# DANGEROUS
use PerlIO::via::QuotedPrint;
binmode(\*STDERR, ":via(PerlIO::via::QuotedPrint):utf8");

my $app = sub {
    my $env = shift;

    open (ERROR, ">&STDERR");  # segmenatation fault

    return [
        '200',
        [ 'Content-Type' => 'text/plain' ],
        [ "hello world" ],
    ];
};

$app;
Run Code Online (Sandbox Code Playgroud)

(实际上我使用的是除了binmode层PerlIO::via::QuotedPrint,但效果是一样的)

如果我不执行binmode(\*STDERR, ":via(PerlIO...,apache不会出现段错误.

如果我不重复STDERR,apache不会出现段错误.

如果我同时做这两件事,那就是段错误.

作为一种解决方法,我可以避免binmode在STDERR上使用,但它并不好.

关于在哪里以及如何解决的任何建议?

谢谢.

我的环境:

perl -v |grep version 
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi

uname -a
Linux thinkpad 3.2.0-32-generic #51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -c
Codename:       precise

dpkg -l |grep mod-perl
ii  libapache2-mod-perl2  2.0.5-5ubuntu1   Integration of perl with the Apache2 web server
Run Code Online (Sandbox Code Playgroud)

更新:相同的代码在过时的Ubuntu 8.04 + perl 5.8.8 + mod_perl2 2.0.3下运行良好

Upd2:FreeBSD 9.1 + perl 5.14 + mod_perl 2.0.8 - 段错重复

uname -a                              
FreeBSD liruoko.ru 9.1-RELEASE-p5 FreeBSD 9.1-RELEASE-p5 #7 r253740: Sun Jul 28 16:53:08 MSK 2013     roman@thor.cmc.msu.ru:/usr/obj/usr/src/sys/MINI  amd64

pkg info |grep apache                     
apache22-itk-mpm-2.2.25        Version 2.2.x of Apache web server with itk MPM.

pkg info |grep mod_perl               
ap22-mod_perl2-2.0.8,3         Embeds a Perl interpreter in the Apache2 server

perl -v |grep version
This is perl 5, version 14, subversion 4 (v5.14.4) built for amd64-freebsd
Run Code Online (Sandbox Code Playgroud)

G. *_*ito 3

如果它在没有binmode设置的情况下工作,那么也许你有一个解决方案(如果不是真正的答案为什么会发生这种情况)。 参见以下摘录perldoc -f binmode

On some systems (in general, DOS- and Windows-based systems) binmode() is 
necessary when you're not working with a text file.  For the sake of portability 
it is a good idea always to use it when appropriate, and never to use it when it 
isn't appropriate.  Also, people can set their I/O to be by default UTF8-encoded 
Unicode, not bytes.

In other words: regardless of platform, use binmode() on binary data, like 
images, for example.   ...
Run Code Online (Sandbox Code Playgroud)

perldoc认为,以一种独特的风格,这可能建议您可以binmode为某些文件句柄/套接字进行设置,而不是为其他文件句柄/套接字进行调整,直到“错误”(如果有)不出现为止。

编辑

感谢您简单且可重现的错误/测试用例,我认为这将得到解决。我构建了一个调试版本perl来尝试跟踪错误,它位于liberperl.so- 中的某个位置PerlIOBase_dup()。我还在 IRC 上向了解的人提到了这一点,他们得出的结论是这是一个真正的(即可报告的)perl错误。

我是这样跑的gdb

(gdb) run -Dx -le 'use PerlIO::via::QuotedPrint; binmode(\*STDERR, 
":via(PerlIO::via::QuotedPrint):utf8"); open (ERROR,      ">&STDERR");'
Run Code Online (Sandbox Code Playgroud)

事情的结局是这样的:

Program received signal SIGSEGV, Segmentation fault.
PerlIOBase_dup (f=0x0, o=0x801551060, param=0x0, flags=2) at perlio.c:2307 
2307 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
Run Code Online (Sandbox Code Playgroud)

干杯,你变得perl更好了!