ensembles seem to be not compiled in safe interp
JulianNoble (via tclchat) 2024-06-13 07:29:42 UTC:Hi all. Are there some known differences in how compilation works in a safe interp? I am looking into performance differences and notice from tcl::unsupported::disassemble that no jump tables seem to be generated for switch statements within a 'dict for' script in safe base interps - yet they are in a normal interp.
My PoC (see below) shows that it is indeed not compiled...
But I'd use the command directly (without ensemble) it works as expected.
So it looks like ensembles compilation is avoided in safe interpreters in 8.6+ for whatever reasons.
PoC:
% interp create -safe si
% si eval { proc test {d} { dict for {k v} $d { something $k $v } }; tcl::unsupported::disassemble proc test }
...
Command 1: "dict for {k v} $d { something $k $v } "
(0) push1 0 # "dict"
(2) push1 1 # "for"
(4) push1 2 # "k v"
(6) loadScalar1 %v0 # var "d"
(8) push1 3 # " something $k $v "
(10) invokeStk1 5
(12) done
% # test with direct command not ensemble...
% si eval { proc test {d} { tcl::dict::for {k v} $d { something $k $v } }; tcl::unsupported::disassemble proc test }
...
Command 1: "tcl::dict::for {k v} $d { something $k $v } "
(0) loadScalar1 %v0 # var "d"
(2) beginCatch4 0
(7) dictFirst %v3 # temp var 3
(12) jumpTrue4 +42 # pc 54
...
% proc test {d} { dict for {k v} $d { something $k $v } }; tcl::unsupported::disassemble proc test
...
Command 1: "dict for {k v} $d { something $k $v } "
(0) loadScalar1 %v0 # var "d"
(2) beginCatch4 0
(7) dictFirst %v3 # temp var 3
(12) jumpTrue4 +42 # pc 54
...
However it works in 8.5 as expected.
hmm, look like it is intended (fossil blame shows dkf in [0a095f089c8ce09a], I guess to circumvent some issues of not completely safe ensembles like "file")
Fixed in branch bug-1095bf7f756f9aed...
Unfortunately I didn't found any test covering why it shall be disable in safe interpreters and [0a095f089c8ce09a] was about ensembles rewrite (so compilation for non compiled subcommands) only... All tests passed...
But I'll assign it to dkf, anyway.
For instance, all the subcommands in initMap of "file" ensemble are marked as safe (zero at end of each declaration in "unsafe")... I know that the whole file command is not available in safe interp, but if it gets exposed to safe child interp, they are then indirectly "safe", however after the fix it still returns "not allowed to invoke subcommand * of file" as expected and intended in [3445787], if I try this (with and without fix):
So it seems to be not affected by the fix at all, but... I leave the decision for dkf (or TCT).
% interp create -safe si
% interp expose si file
% si eval {file dirname a/b}
not allowed to invoke subcommand dirname of file
% si eval {tcl::file::dirname a/b}
not allowed to invoke subcommand dirname of file
This change looks logical to me. I think the original code was simply a little bit too strict not allowing compilation in the case described here.
So, +1 from me, for merging it to all branches.
Thanks, Sergey, for taking this! Just go ahead.
Merged in all mainline branches.