Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 1f22ad9af2b490a99185ada7611ab8302c7f5fef
Ticket: 3418547fffffffffffffffffffffffffffffffff
cmd literal sharing vs. cmd resolvers
User & Date: foxcruiser on 2011-10-04 16:15:17
Changes

  1. assignee changed to: "msofer"
  2. cmimetype changed to: "text/plain"
  3. 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
    
  4. foundin changed to: "obsolete: 8.6b2"
  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: "foxcruiser"
  10. subsystem changed to: "47. Bytecode Compiler"
  11. title changed to: "cmd literal sharing vs. cmd resolvers"
  12. type changed to: "Bug"