Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2006-10-16
09:20 • Closed ticket [730244ffff]: missing refCount management in Tcl_EvalObjv plus... artifact: fbf38336c3 user: sf-robot
2006-10-01
20:35 • Pending ticket [730244ffff]. artifact: a915da6354 user: msofer
09:39 • Open ticket [730244ffff]. artifact: 0742b7871f user: dgp
02:37 • Pending ticket [730244ffff]. artifact: 0271e67592 user: msofer
2005-02-10
08:26 • Ticket [730244ffff]: 4 changes artifact: 9ea0d48912 user: dgp
08:18 • Ticket [730244ffff]: 4 changes artifact: 9cbdafa0bd user: msofer
08:15 • Ticket [730244ffff]: 4 changes artifact: 38bcb7b153 user: msofer
2003-05-13
05:36 • Ticket [730244ffff]: 4 changes artifact: 453efcaa01 user: dgp
04:41 • Ticket [730244ffff]: 4 changes artifact: cd5788d4bd user: dkf
2003-05-01
00:58 • Ticket [730244ffff]: 4 changes artifact: d1ec20146b user: dgp
2003-04-30
23:49 • Ticket [730244ffff]: 4 changes artifact: 628979c900 user: msofer
23:26 • Ticket [730244ffff]: 4 changes artifact: 99d677221b user: dgp
22:51 • Ticket [730244ffff]: 4 changes artifact: c7860f769b user: dgp
22:40 • Ticket [730244ffff]: 4 changes artifact: 021a050bcd user: msofer
15:34 • New ticket [730244ffff]. artifact: cc65b7873f user: dgp

missing refCount management in Tcl_EvalObjv

Closed730244ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 20 years ago
Descriptionplain2003-04-30 15:34:56
This script panics:

interp create i
proc test args {puts $args}
trace add execution test enter {interp alias i alias {} ;#}
interp alias i alias {} test this
i eval alias

The problem is that TEOVI does not use
refCounting to protect the elements of
the objv array.  So if a trace frees an
argument value, that value is not available
for command procedure execution.

Most TEOVI callers manage refCounts,
but Tcl_EvalObjv does not; thus interp alias
calls do not.

A fix is to add objv refCounting to Tcl_EvalObjv,
at an unknown performance cost.

Thoughts?
msoferplain2003-04-30 22:40:40
Logged In: YES 
user_id=148712

Another fix is to document that TEOV requires that the
caller hold a counted reference to its arguments, and insure
that all core calls respect this. No performance cost for
bytecodes (which do not suffer this problem), more
complexity ...
dgpplain2003-04-30 22:51:35
Logged In: YES 
user_id=80530


there's any number of solutions, but it's best
not to get too tricky, and expecially best not
to let the trickiness escape to the public interface.

Would be best if each routine in the core managed
refCounts according to its own needs rather than
serving callers, or called routines.

TEOVI is what needs refCounts, to preserve objs
across trace and normal execution calls.

Tcl_EvalEx, for example, currently does incr/decr
of refcounts, even though it doesn't really need to.
It does only one thing with objv, pass it in to TEOVI.
dgpplain2003-04-30 23:26:28
Logged In: YES 
user_id=80530


this issue combined with
TclInterpReady checks
and numLevels management
suggest that the TEOVI callers
in tclBasic.c really have different
needs from the caller in tclExecute.c.

Another level of wrapping could help here.
msoferplain2003-04-30 23:49:58
Logged In: YES 
user_id=148712

The TclInterpReady difference may actually be a bug in TEBC:
[Bug 495830]
dgpplain2003-05-01 00:58:48
Logged In: YES 
user_id=80530


my comment about Tcl_EvalEx's need
for refCounts was incorrect.  Since it
pulls values from the interp result, it
needs to claim each one before moving
on to do substitutions for the next one.
dkfplain2003-05-13 04:41:03
Logged In: YES 
user_id=79902

Looks like a bug in alias handling to me.The problem is that
the deleting of the alias makes the refcounts on the
(leading) arguments decrease at an unexpected time,
resulting in the refcounts for the command obj dropping to
zero before the return of Tcl_EvalObjv (so violating the
general contractural principle of Tcl_Eval* functions.)
Traces are the most obvious way of triggering this, yes, but
I'd hate to swear that that's the only way it could happen.
Two ways to fix this:
    1) incr the refcounts of the prefix part of the alias
command for the duration of the alias call
    2) decouple the Alias structure from the alias command
lifespan, and use refcounting to delete *that* only when the
last reference to it (whether from the command or from the
executing instances) goes away.

I prefer the second way, but the first would do too.  I
don't think either will be too expensive in practice.
dgpplain2003-05-13 05:36:17
Logged In: YES 
user_id=80530

Fix committed for 8.4.3 and 
to the HEAD for the panic
in the example script.  Fix
does refCounting on the words
of the target of an interp alias,
to make up for the fact that
Tcl_EvalObjv doesn't take care
of itself.

Leaving this report open until
we get Tcl_EvalObjv itself changed,
or good documentation on what 
callers must do to protect themselves.
msoferplain2005-02-10 08:15:54
Logged In: YES 
user_id=148712

I tend to agree with donal: refcount the Alias structure.
msoferplain2005-02-10 08:18:26
Logged In: YES 
user_id=148712

The general rule should be: any objv array passed to a Tcl
function should be guaranteed (by the caller) to be valid
until the function returns.  

Thisbug is related to 1119369, as noted by dgp
dgpplain2005-02-10 08:26:16
Logged In: YES 
user_id=80530

That judgment is reasonable;  from
that perspective, this is a documentation
bug.
msoferplain2006-10-01 02:37:23
Logged In: YES 
user_id=148712

Doc bug seems reasonable. What's the place to document these
general features? Should we add a special man page or README
file for extenders?
dgpplain2006-10-01 09:39:42
Logged In: YES 
user_id=80530


Unless I'm missing something,
it's callers of Tcl_EvalObjv()
that need to take precautions
so the docs for Tcl_EvalObjv()
are where the issue needs to be
made clear.
msoferplain2006-10-01 20:35:39
Logged In: YES 
user_id=148712

Added the comment to doc/Eval.3.

My point was: the elements of every objv array passed to Tcl
should be valid until return, not only in the case of TEOVI.
Where are such "general rules" documented?
sf-robotplain2006-10-16 09:20:05
Logged In: YES 
user_id=1312539

This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
Log in (anonymous is fine) to add a comment.