develooper Front page | perl.perl5.porters | Postings from April 2006

[perl #39017] Segfault in functional streams / bug in closure allocation?

Thread Next
From:
Christian
Date:
April 28, 2006 16:24
Subject:
[perl #39017] Segfault in functional streams / bug in closure allocation?
Message ID:
rt-3.0.11-39017-132813.17.9137393371117@perl.org
# New Ticket Created by  Christian 
# Please include the string:  [perl #39017]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=39017 >



This is a bug report for perl from christian@pflanze.mine.nu,
generated with the help of perlbug 1.35 running under perl v5.8.7.


-----------------------------------------------------------------
[Please enter your report here]

See the code below which segfaults.

I don't see any cyclic reference, and running LengthOfStream
(SequenceStream 15000) in an endless loop runs just fine without
leaking, so cyclic references are not the problem.

It is clearly related to the stack size, increasing the stack limit
using ulimit raises the length of streams that can be processed.

It should be possible to create such streams of infinite length, being
built and deallocated while being traversed. This bug means that the
functional stream programming paradigm (or probably anything else
which creates closures in a repeated fashion?) cannot be applied to
perl 5.



use strict;

sub Delay ( & ) {
    [ 0, $_[0] ]
}

sub Force ( $ ) {
    my $p=shift;
    $$p[0] ? $$p[1]
      : do {
	  $$p[1]= &{ $$p[1] };
	  $$p[0]= 1;
	  $$p[1]
      };
}

sub Cons ( $ $ ) {
    [ @_ ]
}

sub Car ( $ ) {
    $_[0][0]
}

sub Cdr ( $ ) {
    $_[0][1]
}

our $Nil= [];

sub LengthOfStream ( $ ) {
    my ($strm)=@_;
    my $len=0;
    while(1) {
	my $p= Force($strm);
	if ($p eq $Nil) {
	    return $len;
	} else {
	    $len++;
	    $strm= Cdr($p);
	}
    }
}

sub SequenceStream ( $ ) {
    my ($n)=@_;
    Delay {
	($n < 0) ? $Nil
	  #: Cons $n, SequenceStream($n-1);
	  : [ $n, SequenceStream($n-1) ];#no change
    };
}

if (0) {
    while(1) {
	print LengthOfStream (SequenceStream 15000),"\n";
    }
}
# above is no problem. but:
if (0) {
    print LengthOfStream (SequenceStream 15000),"\n";
    print LengthOfStream (SequenceStream 15000),"\n";
    print LengthOfStream (SequenceStream 15000),"\n";
    print LengthOfStream (SequenceStream 25000),"\n";
    # now here a segfault happens
    print LengthOfStream (SequenceStream 25000),"\n";
}
if (1) {
    my $s= (SequenceStream 100000);
    print LengthOfStream $s ,"\n";
    print "Still alive\n";
    undef $s;# <-- segfaults
    print "Still alive?\n";
}


[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=medium
---
Site configuration information for perl v5.8.7:

Configured by Debian Project at Thu Jun  9 00:28:22 EST 2005.

Summary of my perl5 (revision 5 version 8 subversion 7) configuration:
  Platform:
    osname=linux, osvers=2.4.27-ti1211, archname=i386-linux-thread-multi
    uname='linux kosh 2.4.27-ti1211 #1 sun sep 19 18:17:45 est 2004 i686 gnulinux '
    config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=i386-linux -Dprefix=/usr -Dprivlib=/usr/share/perl/5.8 -Darchlib=/usr/lib/perl/5.8 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.8.7 -Dsitearch=/usr/local/lib/perl/5.8.7 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Uusesfio -Uusenm -Duseshrplib -Dlibperl=libperl.so.5.8.7 -Dd_dosuid -des'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include'
    ccversion='', gccversion='3.3.6 (Debian 1:3.3.6-6)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
    perllibs=-ldl -lm -lpthread -lc -lcrypt
    libc=/lib/libc-2.3.2.so, so=so, useshrplib=true, libperl=libperl.so.5.8.7
    gnulibc_version='2.3.2'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    

---
@INC for perl v5.8.7:
    /etc/perl
    /usr/local/lib/perl/5.8.7
    /usr/local/share/perl/5.8.7
    /usr/lib/perl5
    /usr/share/perl5
    /usr/lib/perl/5.8
    /usr/share/perl/5.8
    /usr/local/lib/site_perl
    /usr/local/lib/perl/5.8.4
    /usr/local/share/perl/5.8.4
    /usr/local/lib/perl/5.8.3
    /usr/local/share/perl/5.8.3
    .

---
Environment for perl v5.8.7:
    HOME=/home/chris
    LANG=de_CH
    LANGUAGE (unset)
    LC_CTYPE=de_CH
    LC_NUMERIC=C
    LD_LIBRARY_PATH=:/home/chris/lib
    LOGDIR (unset)
    PATH=/usr/local/Gambit-C/bin:/opt/j2sdk_nb/j2sdk1.4.2/bin/:/home/chris/local/bin:/home/chris/bin:/root/local/bin:/root/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11:/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
    PERL_BADLANG (unset)
    SHELL=/bin/bash


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About