Tcl Source Code

View Ticket
Login
Ticket UUID: 3392070
Title: literal leak with delayed compile [subst]
Type: Bug Created on: 2011-08-15 17:19:18
Submitter: dgp Assigned to: dgp
Subsystem: 47. Bytecode Compiler Severity:
Priority: 5 Medium Last modified: 2011-08-17 19:40:21
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2011-08-17 12:40:21
Version: obsolete: 8.6b2
Description:
This exercise of the test suite leaks:

$ make valgrind TESTFLAGS='-file "regexpComp.test subst.test" -match "regexpComp-12.1 subst-3.2"'
...
==4233== 292 (48 direct, 244 indirect) bytes in 1 blocks are definitely lost in loss record 23 of 24
==4233==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==4233==    by 0x498654: Tcl_Alloc (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==4233==    by 0x4FB2CA: TclCreateLiteral (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==4233==    by 0x44821F: InitLocalCache (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==4233==    by 0x4488F0: TclNRInterpProcCore (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==4233==    by 0x48E315: TclNRRunCallbacks (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==4233==    by 0x493E03: TclEvalEx (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==4233==    by 0x494135: Tcl_EvalEx (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)

Looking at the tests, I suspect the literal value "j" is leaking,
but don't have anything more concrete than that yet.
User Comments:
dgp added on 2011-08-16 02:39:24:
Reduced leak demonstration...

$ cat a.tcl
source b.tcl

$ cat b.tcl
proc foo {} {
    regexp . x j
    return $j
}
foo
subst \j

$ valgrind ./tcltest a.tcl
...
==6105== 292 (48 direct, 244 indirect) bytes in 1 blocks are definitely lost in loss record 23 of 24
==6105==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==6105==    by 0x498654: Tcl_Alloc (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x4FB2CA: TclCreateLiteral (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x4FB501: TclRegisterLiteral (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x4CF24A: TclCompileTokens (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x4CE6A6: TclCompileScript (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x4D0A6B: TclSetByteCodeFromAny (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x4D0BB6: SetByteCodeFromAny (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x4E5065: TclCompileObj (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)
==6105==    by 0x494338: TclNREvalObjEx (in /local/tmp/dgp/fossil/tcl-only/unix/tcltest)


Debugging prints show the literal "j" is not released by
TclDeleteLiteralTable() due to a surplus refcount.

hobbs added on 2011-08-16 02:43:17:
Perhaps the hard-coded compiled regexp cache is never freed?  This is process wide and size limited though, so not a big deal.

dgp added on 2011-08-16 23:04:44:
Simpler, single-file demo that exonerates [regexp]:

if 1 {
  proc foo {} {
    scan x %c j
    return $j
  }
  foo
  set subst subst
  $subst j
}

ferrieux added on 2011-08-17 00:26:45:
The leaking case seems to be correlated with shimmering from localVarName to substCode:


set repr ::tcl::unsupported::representation
if 1 {
proc foo {} {
scan x %c j
return $j
}
foo
puts [$repr j]
set subst subst
$subst j
puts [$repr j]
}

=>


value is a localVarName with a refcount of 5, object pointer at 0x4237408, internal representation (nil):(nil), string representation "j".
value is a substcode with a refcount of 6, object pointer at 0x4237408, internal representation 0x423cad8:0x7, string representation "j".
==16322==    definitely lost: 24 bytes in 1 blocks

While (shimmering to List in between):


set repr ::tcl::unsupported::representation
if 1 {
proc foo {} {
scan x %c j
return $j
}
proc bar x {
llength $x
}
foo
puts [$repr j]
bar j
puts [$repr j]

set subst subst
$subst j
puts [$repr j]
}

=>


value is a localVarName with a refcount of 5, object pointer at 0x4237878, internal representation (nil):(nil), string representation "j".
value is a list with a refcount of 5, object pointer at 0x4237878, internal representation 0x423d848:(nil), string representation "j".
value is a substcode with a refcount of 5, object pointer at 0x4237878, internal representation 0x423dea0:0x7, string representation "j".
==16479==    definitely lost: 0 bytes in 0 blocks

dgp added on 2011-08-17 00:44:44:
Lookie what DupLocalVarName does!
Look familiar?  The copied value keeps
a reference to the original, so taking a dup
to stop cycles doesn't get the job done.

dgp added on 2011-08-17 01:04:28:
This is suggesting that the "Dup to avoid a cycle"
strategy is not sufficient.  If we've got these issues
in multiple internal Tcl_ObjTypes it's a near certainty
that some extension Tcl_ObjTypes do as well, and
we haven't had any explicit docs against the practice,
have we?

dgp added on 2011-08-17 02:13:28:
Confirmed that if I disable the
"localVarName" type completely,
the leak goes away while adding
no failed tests.  No idea what that
does to performance or other matters
though.  The type must be there for
a reason.

dgp added on 2011-08-17 02:50:42:
Branch bug-3392070 holds a proposed fix.

msofer added on 2011-08-17 04:42:51:
Note that this code should be unchanged from Tcl8.5

dgp added on 2011-08-17 19:40:21:

allow_comments - 1