Artifacts Associated With Ticket a3fb3356b76ec4a8
Ticket change [23b5544ff3] (rid 153958) by victorb on 2017-05-29 12:23:18:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
In GrowEvaluationStack(ExecEnv *eePtr, int growth,int move), the "growth" argument is of the signed int type which means that this API cannot be used to allocate more than 2GB of memory. There is no range check either, so if a Tcl command tries to allocate more than 2GB, the API just silently corrupts memory. Specifically, the bug was found by trying to do lsort on a very large list. a) create a very large list (e.g. 108707840 entries) of string, called, for example, $names. b) sort and uniquify the list: set sorted_unique_names [lsort -unique $names] # This will cause a memory error The top of the stacktrace is: #0 0x0000000014700aa1 in GrowEvaluationStack (eePtr=0x1e2c9a80, growth=-210747392, move=0) at /global/sde_apps/tcl/m/prod/tcl/tcl8.6/generic/tclExecute.c:1124 #1 0x000000001470172f in StackAllocWords () at /global/sde_apps/tcl/m/prod/tcl/tcl8.6/generic/tclExecute.c:1252 #2 0x0000000014702302 in TclStackAlloc (interp=0x1e2c6c00, numBytes=-1685979136) at /global/sde_apps/tcl/m/prod/tcl/tcl8.6/generic/tclExecute.c:1349 #3 Tcl_LsortObjCmd (clientData=0x0, interp=0x1e2c6c00, objc=3, objv=0x1e2d4e70) at /global/sde_apps/tcl/m/prod/tcl/tcl8.6/generic/tclCmdIL.c:3960 - foundin initialized to: "8.6"
- is_private initialized to: "0"
- login: "victorb"
- priority initialized to: "5 Medium"
- private_contact initialized to: "6c72cad08b0f1956fbe1a370500e97a6c84dc157"
- resolution initialized to: "None"
- severity initialized to: "Severe"
- status initialized to: "Open"
- submitter initialized to: "victorb"
- subsystem initialized to: "41. Memory Allocation"
- title initialized to:
Segmentation fault due to int overflow in GrowEvaluationStack
- type initialized to: "Bug"
Ticket change [b6b145860f] (rid 153996) by sebres on 2017-05-29 13:59:29:
- foundin changed to: ">= 8.5"
- icomment:
Thx for reporting! It should be indeed rewritten using ptrdiff_t resp. offset_t (no idea which of both will be supported by all platforms/compilers). size_t would be also a variant if "growth" used only to enlarge the stack (to be verified). But I did not yet understand what some large list has concertedly with the evaluation stack... Should the "names" be just not a variable name in such case? According to: <code><pre> % tcl::unsupported::disassemble script {lsort -unique $names} ByteCode 0x004E7F90, refCt 1, epoch 16, interp 0x004CAF58 (epoch 16) Source "lsort -unique $names" Cmds 1, src 20, inst 10, litObjs 3, aux 0, stkDepth 3, code/src 0.00 Commands 1: 1: pc 0-8, src 0-19 Command 1: "lsort -unique $names" (0) push1 0 # "lsort" (2) push1 1 # "-unique" (4) push1 2 # "names" (6) loadStk (7) invokeStk1 3 (9) done </pre></code> it does at all no matter how large the list is. - login: "sebres"
- mimetype: "text/x-fossil-wiki"
Ticket change [ec956ab8e6] (rid 153997) by sebres on 2017-05-29 14:35:16:
- icomment:
This going to the array of indices that will be used for sorting, allocated via "TclStackAlloc(interp, sizeof(int) * sortInfo.indexc)" which indeed very doubtful by lsort, that can sort really large lists. Instead of the rewriting of GrowEvaluationStack/TclStackAlloc rather the ckalloc should be used here instead (at least by large size of the sorting list). I want fix it right away... BTW If someone nevertheless want to rewrite "int" as "ptrdiff_t", then the "int numBytes" for pair TclStackAlloc/TclStackRealloc should be then also rewritten as "size_t numBytes".
- login: "sebres"
- mimetype: "text/plain"
- subsystem changed to: "17. Commands I-L"
- icomment:
Ticket change [ed859d829e] (rid 154008) by sebres on 2017-05-29 16:53:41:
- icomment:
Just as small test-case to reproduce: <code><pre> % set l {1}; time {lappend l {*}$l} 27; puts "[llength $l] elements" 134217728 elements % lsort -unique $l; # should return "1" Segmentation fault (core dumped) </pre></code> - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [a3de7e1b7e] (rid 154014) by sebres on 2017-05-29 19:47:07:
- closedate changed to: "2457903.32439622"
- closer changed to: "sebres"
- foundin changed to: ">= 8.6"
- icomment: "fixed now in 8.6 and trunk."
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"