[gambit-list] non-tail recursion segfaults

Marc Feeley feeley at iro.umontreal.ca
Mon Apr 2 14:44:39 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On 2-Apr-07, at 10:54 AM, Phil Dawes wrote:

> Hi Gambit list,
>
> I've started getting segmentation faults whenever I compile
> srfi-1.scm[1] and use it within my program and was wondering if it was
> to do with non-tail recursion.
>
> I did a quick experiment and found that on my machine the following  
> code
> compiled with gsc segfaults when i is just 20000 (it works fine
> interpreted):
>
> --------------
>
> (define (loop i)
>    (if (zero? i)
>        '()
>        (cons i (loop (- i 1)))))
>
> (loop 20000)
>
> --------------
>
> This seems quite limiting - is there flag I can tweak to improve this?
>
> Many thanks,
>
> Phil

It always amazes me when a bug that causes such "simple" code to fail  
goes unnoticed for so long.  This bug is related to "context merging"  
at join points, which was changed in beta 21.  The call (loop (- i  
1)) is transformed to something like

    (loop (if ... (##fx- i 1) (- i 1)))

Note that (##fx- i 1) is inlined and does not perform a function  
call, whereas (- i 1) performs a function call.  The Gambit compiler  
generates a stack overflow for the call to (- i 1).  For the call to  
loop the compiler thinks that it is unnecessary to check for stack  
overflows because it was checked on the call (- i 1).  Of course this  
is incorrect when the "true" branch of the if is taken.  Apply this  
patch to gsc/_front.scm to fix the problem:

1999c1999,2001
<                                     (context-poll context1)
- ---
 >                                     (poll-merge
 >                                      (context-poll context1)
 >                                      (context-poll context2))

Thanks for the bug report!

Marc

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (Darwin)

iD8DBQFGEU8Y//V9Zc2T/v4RAi1AAJ9uTUUX89FXPRtVRRW2Go7SG7eCswCdHFSc
zgiGrTwBPCbr60GZnQ7r2Ms=
=alWk
-----END PGP SIGNATURE-----



More information about the Gambit-list mailing list