public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* PerlIO::eol core dumps (illegal instruction) in a docker alpine container
@ 2023-04-21  6:02 Yuri Kanivetsky
  2023-04-21  7:52 ` Yuri Kanivetsky
  0 siblings, 1 reply; 3+ messages in thread
From: Yuri Kanivetsky @ 2023-04-21  6:02 UTC (permalink / raw)
  To: gcc-help

Hi,

$ cat a.sh
apk add perl-dev perl-utils perl-libwww perl-lwp-protocol-https build-base gdb
cpan -g PerlIO::eol || true
tar xf Perl*
cd Perl*
sed -Ei '/\bWriteMakefile\b/i\\n$WriteMakefileArgs{OPTIMIZE} = "-g3
-gdwarf-4 -O";' Makefile.PL
# sed -Ei '/\bWriteMakefile\b/i\\n$WriteMakefileArgs{OPTIMIZE} =
"-O0";' Makefile.PL
perl Makefile.PL
make install
cd /app
gdb -ex 'break PerlIOEOL_fill' -ex r --args perl a.pl

$ cat a.pl
use strict;
use warnings;
open my $fh, '<:raw:eol(LF)', 'a.txt';
my $l = <$fh>;
print $l, "\n";

$ echo -e 'line 1\nline 2\r\nline 3' > a.txt

$ docker run --rm -itv $PWD:/app alpine:3.17 sh -eux app/a.sh
...
Breakpoint 1, PerlIOEOL_fill (my_perl=0x7f977d80e020,
f=0x7f977d7ea180) at eol.xs:117
117     {
(gdb) c
Continuing.

Program received signal SIGILL, Illegal instruction.
PerlIOEOL_fill (my_perl=<optimized out>, f=<optimized out>) at eol.xs:132
132             Copy(start, ptr, i - start, STDCHAR);
(gdb) backtrace
#0  PerlIOEOL_fill (my_perl=<optimized out>, f=<optimized out>) at eol.xs:132
#1  0x00007f977d970a74 in Perl_PerlIO_fill () from
/usr/lib/perl5/core_perl/CORE/libperl.so
#2  0x00007f977d973353 in PerlIOBase_read () from
/usr/lib/perl5/core_perl/CORE/libperl.so
#3  0x00007f977d9733e9 in PerlIO_getc () from
/usr/lib/perl5/core_perl/CORE/libperl.so
#4  0x00007f977d91a5df in Perl_sv_gets () from
/usr/lib/perl5/core_perl/CORE/libperl.so
#5  0x00007f977d9037de in Perl_do_readline () from
/usr/lib/perl5/core_perl/CORE/libperl.so
#6  0x00007f977d8fe780 in Perl_runops_standard () from
/usr/lib/perl5/core_perl/CORE/libperl.so
#7  0x00007f977d8841ba in perl_run () from
/usr/lib/perl5/core_perl/CORE/libperl.so
#8  0x000055ca41b3b1db in main ()

But with -O0 it supposedly works. What's going on here? How do I debug it?

Regards,
Yuri

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PerlIO::eol core dumps (illegal instruction) in a docker alpine container
  2023-04-21  6:02 PerlIO::eol core dumps (illegal instruction) in a docker alpine container Yuri Kanivetsky
@ 2023-04-21  7:52 ` Yuri Kanivetsky
  2023-04-21  7:57   ` Xi Ruoyao
  0 siblings, 1 reply; 3+ messages in thread
From: Yuri Kanivetsky @ 2023-04-21  7:52 UTC (permalink / raw)
  To: gcc-help

The culprit supposedly found. fortify-headers makes memcpy() assert
that source and destination shouldn't overlap, and when they do it
calls __builtin_trap():

http://git.2f30.org/fortify-headers/file/include/string.h.html#l46

PerlIO::eol in its turn uses memcpy() to remove symbols from a string
(source and destination overlaps). The questions that are left are:

1) Is fortify-headers too strict, or PerlIO::eol should be fixed?
2) What -O0 has to do with this?

Regards,
Yuri

On Fri, Apr 21, 2023 at 9:02 AM Yuri Kanivetsky
<yuri.kanivetsky@gmail.com> wrote:
>
> Hi,
>
> $ cat a.sh
> apk add perl-dev perl-utils perl-libwww perl-lwp-protocol-https build-base gdb
> cpan -g PerlIO::eol || true
> tar xf Perl*
> cd Perl*
> sed -Ei '/\bWriteMakefile\b/i\\n$WriteMakefileArgs{OPTIMIZE} = "-g3
> -gdwarf-4 -O";' Makefile.PL
> # sed -Ei '/\bWriteMakefile\b/i\\n$WriteMakefileArgs{OPTIMIZE} =
> "-O0";' Makefile.PL
> perl Makefile.PL
> make install
> cd /app
> gdb -ex 'break PerlIOEOL_fill' -ex r --args perl a.pl
>
> $ cat a.pl
> use strict;
> use warnings;
> open my $fh, '<:raw:eol(LF)', 'a.txt';
> my $l = <$fh>;
> print $l, "\n";
>
> $ echo -e 'line 1\nline 2\r\nline 3' > a.txt
>
> $ docker run --rm -itv $PWD:/app alpine:3.17 sh -eux app/a.sh
> ...
> Breakpoint 1, PerlIOEOL_fill (my_perl=0x7f977d80e020,
> f=0x7f977d7ea180) at eol.xs:117
> 117     {
> (gdb) c
> Continuing.
>
> Program received signal SIGILL, Illegal instruction.
> PerlIOEOL_fill (my_perl=<optimized out>, f=<optimized out>) at eol.xs:132
> 132             Copy(start, ptr, i - start, STDCHAR);
> (gdb) backtrace
> #0  PerlIOEOL_fill (my_perl=<optimized out>, f=<optimized out>) at eol.xs:132
> #1  0x00007f977d970a74 in Perl_PerlIO_fill () from
> /usr/lib/perl5/core_perl/CORE/libperl.so
> #2  0x00007f977d973353 in PerlIOBase_read () from
> /usr/lib/perl5/core_perl/CORE/libperl.so
> #3  0x00007f977d9733e9 in PerlIO_getc () from
> /usr/lib/perl5/core_perl/CORE/libperl.so
> #4  0x00007f977d91a5df in Perl_sv_gets () from
> /usr/lib/perl5/core_perl/CORE/libperl.so
> #5  0x00007f977d9037de in Perl_do_readline () from
> /usr/lib/perl5/core_perl/CORE/libperl.so
> #6  0x00007f977d8fe780 in Perl_runops_standard () from
> /usr/lib/perl5/core_perl/CORE/libperl.so
> #7  0x00007f977d8841ba in perl_run () from
> /usr/lib/perl5/core_perl/CORE/libperl.so
> #8  0x000055ca41b3b1db in main ()
>
> But with -O0 it supposedly works. What's going on here? How do I debug it?
>
> Regards,
> Yuri

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PerlIO::eol core dumps (illegal instruction) in a docker alpine container
  2023-04-21  7:52 ` Yuri Kanivetsky
@ 2023-04-21  7:57   ` Xi Ruoyao
  0 siblings, 0 replies; 3+ messages in thread
From: Xi Ruoyao @ 2023-04-21  7:57 UTC (permalink / raw)
  To: Yuri Kanivetsky; +Cc: gcc-help

On Fri, 2023-04-21 at 10:52 +0300, Yuri Kanivetsky via Gcc-help wrote:
> The culprit supposedly found. fortify-headers makes memcpy() assert
> that source and destination shouldn't overlap, and when they do it
> calls __builtin_trap():
> 
> http://git.2f30.org/fortify-headers/file/include/string.h.html#l46
> 
> PerlIO::eol in its turn uses memcpy() to remove symbols from a string
> (source and destination overlaps). The questions that are left are:
> 
> 1) Is fortify-headers too strict, or PerlIO::eol should be fixed?

Then you should use memmove instead of memcpy.

> 2) What -O0 has to do with this?

-D_FORTIFY_SOURCE=2 has no effect with -O0:

$ cc hw.c -D_FORTIFY_SOURCE=2 -O0
In file included from /usr/include/bits/libc-header-start.h:33,
                 from /usr/include/stdio.h:27,
                 from hw.c:1:
/usr/include/features.h:413:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
  413 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
      |    ^~~~~~~

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-21  7:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-21  6:02 PerlIO::eol core dumps (illegal instruction) in a docker alpine container Yuri Kanivetsky
2023-04-21  7:52 ` Yuri Kanivetsky
2023-04-21  7:57   ` Xi Ruoyao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).