Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 4b330bae9a9d82e48a7ec50bd4459a8c9acee957
Ticket: 2017946fffffffffffffffffffffffffffffffff
Tailcalls into non-compiled scripts don't work properly
User & Date: tallniel on 2008-07-14 12:53:33
Changes

  1. assignee changed to: "msofer"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    A simple tail-recursive factorial function using NRE bombs out at around N=10,000 due to out of memory panic in Tcl bignum code. Equivalent iterative version completes fine on same input. It seems like the C stack is growing despite NRE, and locals aren't being reclaimed? Is this [if] causing the tailcall to not work? Test proc:
    
    proc fac-tr {n {k 1}} {
        if {$n <= 1} {
            return $k
        } else {
            tailcall fac-tr [- $n 1] [* $n $k]
        }
    }
    fac-tr 10000
    
    (abort reproducible on Windows XP, with 4GB RAM). Stack dump at abort:
    
     tclsh86sg.exe!_chkstk()  Line 91Asm
     tclsh86sg.exe!_NMSG_WRITE(int rterrnum=10)  Line 194 + 0x18C
     tclsh86sg.exe!abort()  Line 44 + 0x7C
     tclsh86sg.exe!Tcl_PanicVA(const char * format=0x005603e8, char * argList=0x00033538)  Line 104C
     tclsh86sg.exe!Tcl_Panic(const char * format=0x005603e8, ...)  Line 131 + 0xdC
     tclsh86sg.exe!Tcl_Realloc(char * ptr=0x148e7fe8, unsigned int size=12944)  Line 1108 + 0xeC
     tclsh86sg.exe!TclBN_mp_grow(mp_int * a=0x000340b4, int size=3236)  Line 35 + 0x13C
     tclsh86sg.exe!TclBN_mp_copy(mp_int * a=0x00033584, mp_int * b=0x000340b4)  Line 31 + 0xfC
     tclsh86sg.exe!TclBN_mp_init_copy(mp_int * a=0x000340b4, mp_int * b=0x00033584)  Line 26 + 0xdC
     tclsh86sg.exe!GetBignumFromObj(Tcl_Interp * interp=0x00000000, Tcl_Obj * objPtr=0x14f92310, int copy=0, mp_int * bignumValue=0x000340b4)  Line 2797 + 0xdC
     tclsh86sg.exe!Tcl_TakeBignumFromObj(Tcl_Interp * interp=0x00000000, Tcl_Obj * objPtr=0x14f92310, mp_int * bignumValue=0x000340b4)  Line 2902 + 0x13C
     tclsh86sg.exe!TclExecuteByteCode(Tcl_Interp * interp=0x00afa8a8, ByteCode * codePtr=0x00bb7fb8)  Line 6428 + 0x15C
     tclsh86sg.exe!Tcl_EvalObjv(Tcl_Interp * interp=0x00afa8a8, int objc=3, Tcl_Obj * const * objv=0x14cb1fe0, int flags=524288)  Line 4116 + 0x10C
     tclsh86sg.exe!TailcallFromTebc(void * * data=0x14f922cc, Tcl_Interp * interp=0x00afa8a8, int result=0)  Line 7838 + 0x16C
    >tclsh86sg.exe!TclEvalObjv_NR2(Tcl_Interp * interp=0x00afa8a8, int result=0, TEOV_record * rootPtr=0x06a2ea10)  Line 4238 + 0x16C
     tclsh86sg.exe!Tcl_EvalObjv(Tcl_Interp * interp=0x00afa8a8, int objc=3, Tcl_Obj * const * objv=0x14cb1f98, int flags=524288)  Line 4207 + 0x11C
     tclsh86sg.exe!TailcallFromTebc(void * * data=0x14f92344, Tcl_Interp * interp=0x00afa8a8, int result=0)  Line 7838 + 0x16C
     tclsh86sg.exe!TclEvalObjv_NR2(Tcl_Interp * interp=0x00afa8a8, int result=0, TEOV_record * rootPtr=0x06a2ea10)  Line 4238 + 0x16C
     tclsh86sg.exe!Tcl_EvalObjv(Tcl_Interp * interp=0x00afa8a8, int objc=3, Tcl_Obj * const * objv=0x14cb1f50, int flags=524288)  Line 4207 + 0x11C
     tclsh86sg.exe!TailcallFromTebc(void * * data=0x14f923bc, Tcl_Interp * interp=0x00afa8a8, int result=0)  Line 7838 + 0x16C
    ... stack then has several hundred repeats of these TailcallFromTebc/Tcl_EvalObjv combinations....
    
    I thought this might be the [if] preventing the tailcall reclaiming the stack, but I get the same abort with the simpler:
    
     proc fac-tr {n {k 1}} {
         if {$n <= 1} { return $k }
         tailcall fac-tr [- $n 1] [* $n $k]
     }
    
    
    
  4. foundin changed to: "obsolete: 8.6a1"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "tallniel"
  10. subsystem changed to: "45. Parsing and Eval"
  11. title changed to: "Mem leak in NRE/lack of trampoline"
  12. type changed to: "Bug"