Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 8a967c449e44ada87b556dde53983dd9007dde4f
Ticket: 3418547fffffffffffffffffffffffffffffffff
cmd literal sharing vs. cmd resolvers
User & Date: dgp on 2016-07-15 15:47:27
Changes

  1. assignee changed to: "dgp"
  2. closedate changed to: "2457585.15795824"
  3. closer changed to: "dgp"
  4. comment changed to:
    When testing our Tcl's per-interp resolvers, in particular for
    resolving commands, against 8.6b2 (and the current
    trunk, as of 20111004), we found an unwanted interaction between
    "global" CmdName obj sharing (i.e. cmd literals) and the cmd resolver
    infrastructure.
    
    In short: Using command resolvers can easily lead to situations in
    which CmdName objs are effectively shared between BC-compiled scripts
    (procs, [namespace eval]) for the scope of a given namespace (global
    and non-global NS alike) while their command references turn out
    obsolete. An invalidation of the CmdName objs intrep (ResolvedCmdName)
    is not performed. The omission of the invalidation is due to several
    reasons, depending on the BC compilation/execution context. For
    instance, for instantly compiled scripts ([namespace eval]) we hit
    TclSetCmdNameObj() which simply returns without touching (existing) CmdName
    obj intreps.
    
    A condensed example:
    
    Consider a cmd resolver which binds "z" to ::y when "z" is being
    resolved in the context of the proc ::x (and in this scope only):
     
    proc ::y {} { return Y }; # the resolver-specific resolution target
      
    proc ::x {} {
        z; # the tcl word being turned into a CmdName obj and shared as cmd literal after having been BC-compiled
      }
    
    This cmd resolution is performed during BC compilation of ::x. The cmd
    (::y) return by the cmd resolver is referenced by the intrep of a
    CmdName obj "z" for the NS scope of the [namespace current] during
    compilation ("::"). Next time, a script is compiled in this sharing
    context, this will work (unexpectedly?):
    
    namespace eval :: {
        z; # returns "Y"?!
    }
    
    However, when requesting a different cmd to become available under the
    token "z", e.g., ::ns1::z:
      
    namespace eval ::ns1 {
        proc z {} { return Z }
        namespace export z
      }
      
    ... by e.g. [namespace import]'ing, this won't show any effect on the shared cmd literal:
    
     namespace eval :: {
        namespace import ::ns1::z
        z; # still returns "Y", though expecting "Z"
      }
     
    One can alter this (mis-)behaviour at various spots, for instance, by
    causing the compiled script to omit cmd literal sharing for the given
    token "z":
    
     namespace eval :: {
        proc z {} { return Z }
        z; # yields "Z", as expected
      }
    
    This "works" simply because there is no cmdname obj emitted. Changing
    the script back to using a qualified token "::z" gives us the unwanted
    behavior again:
    
     namespace eval :: {
        proc ::z {} { return Z }
        z; # yields "Y", not "Z"
      }
    
    (This example is contained as a tcltest (plus comments) lit-1.1 by the patch attached)
    
    In our reading, cmd resolvers are capable of causing yet another form
    of "command shadowing" at the level of cmd literals, given the
    aggressive cmd literal sharing in Tcl 8.6. Command resolution through
    cmd resolvers in Tcl_FindCommand & friends gives birth to cmd literals
    which silently "import" commands from alien contexts (resolver
    specific) into a namespace context without "registering" the commands
    in a manner to make them visible to the various invalidation
    routines. Command epochs do not come into play either because the
    "resolver-imported" command might simply not be touched itself or
    intrep invalidation is never triggered
    (TclSetCmdNameObj()). NS-specific invalidation (cmdRefEpoch) simply
    does not apply because literals are not part of any invalidation
    scheme, e.g., TclResetShadowedCmdRefs, NS path invalidation etc. From
    within the cmd resolvers, there is no way of interfering (in our
    understanding).
    
    The patch attached to this bug report features the following:
    
    a) A minimal infrastructure for testing resolvers (tclTest.c):
    [testinterpresolver up|down]
    
    b) A collection (resolvers.test) of characteristic tcltests which show
    the issue in various settings ([namespace import], [interp alias],
    [interp expose], [proc]). See tcltest lit-1.1 for a step-by-step
    explanation of a prototypical scenario
    
    c) Finally, a suggested patch around a newly introduced
    TclInvalidateCmdLiteral() helper. While this helper addresses the
    issue sufficiently as indicated by the tcltest collection, there might certainly
    be alternative strategies. The primary purpose of this patch is to
    point you into the right direction. TclInvalidateCmdLiteral() simply
    queries the literal tables for candidates using the Tcl_CreateLiteral
    API and, if found, resets the intrep after critical operations such
    as e.g. [namespace import].
    
    d) Included is also a tcltest covering (the unrelated) bug #3383616
    
    Stefan Sobernig
    Gustaf Neumann
    
  5. icomment:
    
    Fate brings me here again.
    
    This bug report was about a real and difficult problem
    with a still under-specified extension mechanism, and
    I believe the remedy we've been living with was offered
    up in good faith and has been effective enough for
    people to move forward.
    
    But it's just wrong.
    
    My suspicions are raised because the TclInvalidateCmdLiteral()
    routine that implements the corrective *only* patches up
    literals that have invalid intreps.  What about other
    Tcl_Obj values that are holding "cmdName" intreps?  How
    are they to be corrected?
    
    Looking at test resolver-1.1 I see more trouble.  The
    testing resolver that is enabled by [testinterpresolver up]
    has a very specific function.  If it sees the command name
    "z" within the proc "::x" and nowhere else it will redirect
    that to the implementation of command "::y".  So as described
    in the test comments,
    
        set r0 [x]
    
    produces result "Y" because of the redirection.  In step 2,
    
        set r1 [z]
    
    The redirection has been cached as the intrep of [z] so it
    is taken again, even though it is not in the context recognized
    by the resolver.  The comments question, but accept this.
    They should not.  This is just wrong.  The resolver conditions
    are not met.  The correct result should be "unknown command 'z'".
    
    The a [namespace import] is done to give [z] a more correct target.
    Before the bug fix, this failed.  Now it succeeds because each thing
    like [namespace import] that could offer a new target also seeks out
    and clears any "cmdName" intreps in literals, and in this case the
    improper behavior is in a literal.  We now get
    
        set r2 [z]
    
    which returns "Z" as we expect.  At this point the test stops before
    taking the next step to see that things are still broken:
    
        set r3 [x]
    
    This takes us back to the first case, where unquestionably the
    resolver ought to be in power, but if you try it you will see
    that the returned value is "Z".  Here the caching is working against
    us again, but in the opposite way.  The context distinctions that
    the interp resolver is making use of are not known and not tested
    in validating the intrep.  In general, it's hard to see how they
    could be.
    
  6. login: "dgp"
  7. mimetype: "text/plain"
  8. severity changed to: "Minor"