Tk Library Source Code

View Ticket
Login
Ticket UUID: 1998628
Title: Tcl lib and safe interpreter
Type: Bug Version: None
Submitter: nicolascastagne Created on: 2008-06-20 10:13:18
Subsystem: math Assigned To: arjenmarkus
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2009-12-09 01:21:37
Resolution: Fixed Closed By: andreas_kupries
    Closed on: 2009-12-04 17:38:24
Description:
I am working on Linux, with active tcl 852, and tcllib installed through teacup. 
 
************ 
 First question 
 ************ 
 I tried executing the following: 
         > /opt/ActiveTcl-8.5/bin/tclsh8.5 
         % ::safe::interpCreate 
         interp0 
         % interp eval interp0 "package require math" 
         script error 
 
Indeed, as discussed on this comp.lang.tcl thread :
http://groups.google.com/group/comp.lang.tcl/browse_thread/thread/3e15e20b001e6ecf#

If you set a log command for the safe package then you can see the problem: 
 
  % proc debug args { puts "LOG: $args" } 
   % safe::setLogCmd debug 
   debug 
   % $i eval { package require math } 
   LOG: {ERROR for slave interp0 : invalid command name "pwd"} 
 
It seems the math package tries to use [pwd] for some reason, which is not available in a safe slave.

I don't see any true reason that would forbid requiring the math package in a safe interp ?
If 
 you set a log command for the safe package then you can see the problem: 
 
  % proc debug args { puts "LOG: $args" } 
   % safe::setLogCmd debug 
   debug 
   % $i eval { package require math } 
   LOG: {ERROR for slave interp0 : invalid command name "pwd"} 
 It seems the math package tries to use [pwd] for some reason, which is not available in a safe slave.

Perhaps there are, but I don't see any true reason that would forbid requiring the math package in a safe interp.

More generally, I guess most tcllib packages, which are a-priori "safe" should load into a safe slave.
User Comments: andreas_kupries added on 2009-12-09 01:21:37:
Ok. Per talk with dgp on the chat based on his continued investigastions this seems to be a problem in Tcl's package.tcl file. See

https://sourceforge.net/tracker/?func=detail&atid=110894&aid=2910867&group_id=10894

dgp added on 2009-12-08 21:06:05:
FWIW, I see troubles with test math-12.1.
Several messages about inability to read
paths ending in pkgIndex.tcl.  One of the
testing interps is the release of 8.5.8, so I
suspect this is really a problem in the math
package (code or test).

andreas_kupries added on 2009-12-05 00:38:24:

allow_comments - 1

I am accepting this with some modifications. Replaced the join/lreplace/split with file join/dirname. The only command which made trouble was [pwd], not the path manipulation commands. I.e. no reason to drop them as well. The use of / as path separator is also not robust, i.e. portable. On windows it might quite well be \.

Tweak to the testcase: Destruction of the interpreter after we are done with it.

Another tweak to math.tcl, when I am done with the release: Make the whole thing unconditional. No need to distinguish plain and safe interps, just have plain source their files as the safe will do, i.e. directly, not lazy.

Committed to head, version 1.2.5.

arjenmarkus added on 2009-12-04 15:25:12:
I got this one solved - sn error in my test case, rather than something very weird in the package management. As the repository is read-only at the moment, I include the changes 
and the test case here:

-- math.test --
test math-12.1 {Safe Interpreter} {
    #interp create -safe safeInterp
    ::safe::interpCreate safeInterp
    interp alias safeInterp puts {} puts
    set result [interp eval safeInterp {
        package require math
        set result [math::cov 100 100 100 -100]
    }]
} 200.0
----

-- math.tcl --
    # Set up for auto-loading

    if { ![interp issafe {}]} {
        variable home [file join [pwd] [file dirname [info script]]]
        if {[lsearch -exact $::auto_path $home] == -1} {
   lappend ::auto_path $home
        }
    } else {
        source [join [lreplace [split [info script] /] end end misc.tcl] /]
        source [join [lreplace [split [info script] /] end end combinatorics.tcl] /]
    }

arjenmarkus added on 2009-11-20 15:54:30:
I tried to solve this by avoiding auto_path and auto_load for safe interpreters and simply
sourcing the files. This does not work either. My suspicion is that [info script] does not
return a valid file name, but I still have to check that - puts is not very helpful in safe
interpreters, I probably have to alias it.

andreas_kupries added on 2009-11-18 00:05:08:
Note the following in the original description
> LOG: {ERROR for slave interp0 : invalid command name "pwd"} 
pwd is a forbidden command in a safe interp.

The problem is likely

math.tcl:36:    variable home [file join [pwd] [file dirname [info script]]]

and later

    if {[lsearch -exact $::auto_path $home] == -1} {
        lappend ::auto_path $home

I am not sure why this extension of the auto-load path is required.

arjenmarkus added on 2009-11-17 21:42:32:
I have tried a SafeBase interpreter - it works for a package like math::statistics, but it fails for math itself. The error message is uninformative: script error.

This needs some further investigation

andreas_kupries added on 2008-06-21 00:11:29:
Logged In: YES 
user_id=75003
Originator: NO

Safe base means that a number of commands are not allowed, and not provided. pwd and file are among them.
math uses pwd and file to set up old-style lazy auto-loading of procedures.

To use math in the safe base you have to reconfigure it, i.e. make pwd and file available, maybe in restricted form.
Or the package has to be rewritten to not use auto-loading of its pieces.

nicolascastagne added on 2008-06-20 17:14:09:
Logged In: YES 
user_id=1258423
Originator: YES

Note : I did not put any "category", since though I found the pb with the math package, I guess it is perhaps more general in tcllib.