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

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

Thread Next
April 28, 2006 16:24
[perl #39017] Segfault in functional streams / bug in closure allocation?
Message ID:
# New Ticket Created by  Christian 
# Please include the string:  [perl #39017]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
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;

sub Cons ( $ $ ) {
    [ @_ ]

sub Car ( $ ) {

sub Cdr ( $ ) {

our $Nil= [];

sub LengthOfStream ( $ ) {
    my ($strm)=@_;
    my $len=0;
    while(1) {
	my $p= Force($strm);
	if ($p eq $Nil) {
	    return $len;
	} else {
	    $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]
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:
    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 -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
    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',
    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/, so=so, useshrplib=true,
  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:

Environment for perl v5.8.7:
    LANGUAGE (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About