Safe/ForkedSafe.pm

filter: Statementstmt 86.4% 204 / 236 Branchbran 64.7% 57 / 88 Conditioncond 45.7% 16 / 35 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 76.4% 39 / 51 Podpod 0.0% 0 / 23 totaltotal 70.4% 319 / 453 CC 57 SCAR 16.6
CC
57
Cov
77%
share_from
16.3
share_redo
12.0
_find_code_refs
9.3
CRAP 5.3
↑ Summary Safe.pm »
1 # This module is a fork of the standard Perl module Safe 2.40. This fork intend
2 # to fix the following bug: https://rt.perl.org/Ticket/Display.html?id=134459
3 #
4 # This module is originally designed and implemented by Malcolm Beattie.
5 # Reworked to use the Opcode module and other changes added by Tim Bunce.
6 # Currently maintained by the Perl 5 Porters, <perl5-porters@perl.org>.
7 #
8 # This fork and all the bug that it adds is by Mathias Kende <mathias@cpan.org>.
9 #
10 # The code here is released under the Perl Artistic Licence:
11 # https://dev.perl.org/licenses/artistic.html
12 #
13 # And the diff between this module and the original Safe one is released in the
14 # public domain (or free to use by anyone in any way whatsoever).
15 #
16 # Changelog, compared to Safe 2.40:
17 # - in wrap_code_ref, wrap the wrapped_code in an eval to catch exceptions;
18 # - do not raise these exceptions, let the user read them in $@;
19 # - recursively wrap code returned by wrapped code;
20 # - propagate an undef wantarray when running wrapped code;
21 # - rename the package to Eval::Safe::ForkedSafe (adapt the `isa` tests as
22 # well as the default root namespace).
23 # - bump the version to 2.41.
24
25 package Eval::Safe::ForkedSafe;
26
27 20 ▶use 5.003_11;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 20 ▶use Scalar::Util qw(reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
29
30 13 $Safe::VERSION = "2.41";
31
32 # *** Don't declare any lexicals above this point ***
33 #
34 # This function should return a closure which contains an eval that can't
35 # see any lexicals in scope (apart from __ExPr__ which is unavoidable)
36
37 43 ▶sub lexless_anon_sub {
Subroutine lexless_anon_sub: called CC=2 SCAR=6.9
Pod undocumented
38 # $_[0] is package;
39 # $_[1] is strict flag;
40 43 my $__ExPr__ = $_[2]; # must be a lexical to create the closure that
41 # can be used to pass the value into the safe
42 # world
43
44 # Create anon sub ref in root of compartment.
45 # Uses a closure (on $__ExPr__) to pass in the code to be executed.
46 # (eval on one line to keep line numbers as expected by caller)
47 43 ▶ eval sprintf
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 43 43
48 'package %s; %s sub { @_=(); eval q[local *SIG; my $__ExPr__;] . $__ExPr__; }',
49 $_[0], $_[1] ? 'use strict;' : '';
50 }
51
52 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 15 ▶BEGIN { eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
55 use Carp::Heavy;
56 } }
57
58 14 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59 13 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.5
60 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 14 ▶ if (defined &B::sub_generation) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &B::sub_generation) { } 13 0 13
62 14 *sub_generation = \&B::sub_generation;
63 }
64 else {
65 # fake sub generation changing for perls < 5.8.9
66 0 ▶ my $sg; *sub_generation = sub { ++$sg };
Subroutine __ANON__: not called CC=1 SCAR=2.6
67 }
68 }
69
70 13 ▶use Opcode 1.01, qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
71 opset opset_to_ops opmask_add
72 empty_opset full_opset invert_opset verify_opset
73 opdesc opcodes opmask define_optag opset_to_hex
74 13 );
75
76 13 *ops_to_opset = \&opset; # Temporary alias for old Penguins
77
78 # Regular expressions and other unicode-aware code may need to call
79 # utf8->SWASHNEW (via perl's utf8.c). That will fail unless we share the
80 # SWASHNEW method.
81 # Sadly we can't just add utf8::SWASHNEW to $default_share because perl's
82 # utf8.c code does a fetchmethod on SWASHNEW to check if utf8.pm is loaded,
83 # and sharing makes it look like the method exists.
84 # The simplest and most robust fix is to ensure the utf8 module is loaded when
85 # Safe is loaded. Then we can add utf8::SWASHNEW to $default_share.
86 13 require utf8;
87 # we must ensure that utf8_heavy.pl, where SWASHNEW is defined, is loaded
88 # but without depending on too much knowledge of that implementation detail.
89 # This code (//i on a unicode string) should ensure utf8 is fully loaded
90 # and also loads the ToFold SWASH, unless things change so that these
91 # particular code points don't cause it to load.
92 # (Swashes are cached internally by perl in PL_utf8_* variables
93 # independent of being inside/outside of Safe. So once loaded they can be)
94 13 do { my $a = pack('U',0x100); my $b = chr 0x101; utf8::upgrade $b; $a =~ /$b/i };
95 # now we can safely include utf8::SWASHNEW in $default_share defined below.
96
97 13 my $default_root = 0;
98 # share *_ and functions defined in universal.c
99 # Don't share stuff like *UNIVERSAL:: otherwise code from the
100 # compartment can 0wn functions in UNIVERSAL
101 13 ▶my $default_share = [qw[
Condition40%6/15
$] < 5.01 && '&utf8::SWASHGET'
!ll
130
$] >= 5.008001 && '&Regexp::DESTROY'
!ll
013
$] >= 5.01 && ('&re::is_regexp', '&re::regname', '&re::regnames', '&re::regnames_count', '&UNIVERSAL::DOES', '&version::()', '&version::new', '&version::(""', '&version::stringify', '&version::(0+', '&version::numify', '&version::normal', '&version::(cmp', '&version::(<=>', '&version::vcmp', '&version::(bool', '&version::boolean', '&version::(nomethod', '&version::noop', '&version::is_alpha', '&version::qv', '&version::vxs::declare', '&version::vxs::qv', '&version::vxs::_VERSION', '&version::vxs::stringify', '&version::vxs::new', '&version::vxs::parse', '&version::vxs::VCMP')
!ll&&!rl&&r
0013
$] >= 5.011 && '&re::regexp_pattern'
!ll
013
$] >= 5.01 && $] < 5.014 && ('&Tie::Hash::NamedCapture::FETCH', '&Tie::Hash::NamedCapture::STORE', '&Tie::Hash::NamedCapture::DELETE', '&Tie::Hash::NamedCapture::CLEAR', '&Tie::Hash::NamedCapture::EXISTS', '&Tie::Hash::NamedCapture::FIRSTKEY', '&Tie::Hash::NamedCapture::NEXTKEY', '&Tie::Hash::NamedCapture::SCALAR', '&Tie::Hash::NamedCapture::flags')
!ll&&!rl&&r
1300
$] >= "5.01" and $] < "5.014"
!ll&&!rl&&r
0130
MC/DC0%0/1
$] < 5.01 && '&utf8::SWASHGET'
$] < 5.01
MC/DC0%0/1
$] >= 5.008001 && '&Regexp::DESTROY'
$] >= 5.008001
MC/DC0%0/2
$] >= 5.01 && ('&re::is_regexp', '&re::regname', '&re::regnames', '&re::regnames_count', '&UNIVERSAL::DOES', '&version::()', '&version::new', '&version::(""', '&version::stringify', '&version::(0+', '&version::numify', '&version::normal', '&version::(cmp', '&version::(<=>', '&version::vcmp', '&version::(bool', '&version::boolean', '&version::(nomethod', '&version::noop', '&version::is_alpha', '&version::qv', '&version::vxs::declare', '&version::vxs::qv', '&version::vxs::_VERSION', '&version::vxs::stringify', '&version::vxs::new', '&version::vxs::parse', '&version::vxs::VCMP')
$] >= 5.01 ('&re::is_regexp', '&re::regname', '&re::regnames', '&re::regnames_count', '&UNIVERSAL::DOES', '&version::()', '&version::new', '&version::(""', '&version::stringify', '&version::(0+', '&version::numify', '&version::normal', '&version::(cmp', '&version::(<=>', '&version::vcmp', '&version::(bool', '&version::boolean', '&version::(nomethod', '&version::noop', '&version::is_alpha', '&version::qv', '&version::vxs::declare', '&version::vxs::qv', '&version::vxs::_VERSION', '&version::vxs::stringify', '&version::vxs::new', '&version::vxs::parse', '&version::vxs::VCMP')
MC/DC0%0/1
$] >= 5.011 && '&re::regexp_pattern'
$] >= 5.011
MC/DC0%0/3
$] >= 5.01 && $] < 5.014 && ('&Tie::Hash::NamedCapture::FETCH', '&Tie::Hash::NamedCapture::STORE', '&Tie::Hash::NamedCapture::DELETE', '&Tie::Hash::NamedCapture::CLEAR', '&Tie::Hash::NamedCapture::EXISTS', '&Tie::Hash::NamedCapture::FIRSTKEY', '&Tie::Hash::NamedCapture::NEXTKEY', '&Tie::Hash::NamedCapture::SCALAR', '&Tie::Hash::NamedCapture::flags')
$] >= "5.01" $] < "5.014" ('&Tie::Hash::NamedCapture::FETCH', '&Tie::Hash::NamedCapture::STORE', '&Tie::Hash::NamedCapture::DELETE', '&Tie::Hash::NamedCapture::CLEAR', '&Tie::Hash::NamedCapture::EXISTS', '&Tie::Hash::NamedCapture::FIRSTKEY', '&Tie::Hash::NamedCapture::NEXTKEY', '&Tie::Hash::NamedCapture::SCALAR', '&Tie::Hash::NamedCapture::flags')
Truth table50%1/2
A
A= $] < 5.01
Aresult
00
11
Truth table50%1/2
A
A= $] >= 5.008001
Aresult
00
11
Truth table33%1/3
A && B
A= $] >= 5.01B= ('&re::is_regexp', '&re::regname', '&re::regnames', '&re::regnames_count', '&UNIVERSAL::DOES', '&version::()', '&version::new', '&version::(""', '&version::stringify', '&version::(0+', '&version::numify', '&version::normal', '&version::(cmp', '&version::(<=>', '&version::vcmp', '&version::(bool', '&version::boolean', '&version::(nomethod', '&version::noop', '&version::is_alpha', '&version::qv', '&version::vxs::declare', '&version::vxs::qv', '&version::vxs::_VERSION', '&version::vxs::stringify', '&version::vxs::new', '&version::vxs::parse', '&version::vxs::VCMP')
ABresult
0X0
100
111
Truth table50%1/2
A
A= $] >= 5.011
Aresult
00
11
Truth table25%1/4
A and B && C
A= $] >= "5.01"B= $] < "5.014"C= ('&Tie::Hash::NamedCapture::FETCH', '&Tie::Hash::NamedCapture::STORE', '&Tie::Hash::NamedCapture::DELETE', '&Tie::Hash::NamedCapture::CLEAR', '&Tie::Hash::NamedCapture::EXISTS', '&Tie::Hash::NamedCapture::FIRSTKEY', '&Tie::Hash::NamedCapture::NEXTKEY', '&Tie::Hash::NamedCapture::SCALAR', '&Tie::Hash::NamedCapture::flags')
ABCresult
0XX0
10X0
1100
1111
102 *_
103 &PerlIO::get_layers
104 &UNIVERSAL::isa
105 &UNIVERSAL::can
106 &UNIVERSAL::VERSION
107 &utf8::is_utf8
108 &utf8::valid
109 &utf8::encode
110 &utf8::decode
111 &utf8::upgrade
112 &utf8::downgrade
113 &utf8::native_to_unicode
114 &utf8::unicode_to_native
115 &utf8::SWASHNEW
116 $version::VERSION
117 $version::CLASS
118 $version::STRICT
119 $version::LAX
120 @version::ISA
121 ], ($] < 5.010 && qw[
122 &utf8::SWASHGET
123 ]), ($] >= 5.008001 && qw[
124 &Regexp::DESTROY
125 ]), ($] >= 5.010 && qw[
126 &re::is_regexp
127 &re::regname
128 &re::regnames
129 &re::regnames_count
130 &UNIVERSAL::DOES
131 &version::()
132 &version::new
133 &version::(""
134 &version::stringify
135 &version::(0+
136 &version::numify
137 &version::normal
138 &version::(cmp
139 &version::(<=>
140 &version::vcmp
141 &version::(bool
142 &version::boolean
143 &version::(nomethod
144 &version::noop
145 &version::is_alpha
146 &version::qv
147 &version::vxs::declare
148 &version::vxs::qv
149 &version::vxs::_VERSION
150 &version::vxs::stringify
151 &version::vxs::new
152 &version::vxs::parse
153 &version::vxs::VCMP
154 ]), ($] >= 5.011 && qw[
155 &re::regexp_pattern
156 ]), ($] >= 5.010 && $] < 5.014 && qw[
157 &Tie::Hash::NamedCapture::FETCH
158 &Tie::Hash::NamedCapture::STORE
159 &Tie::Hash::NamedCapture::DELETE
160 &Tie::Hash::NamedCapture::CLEAR
161 &Tie::Hash::NamedCapture::EXISTS
162 &Tie::Hash::NamedCapture::FIRSTKEY
163 &Tie::Hash::NamedCapture::NEXTKEY
164 &Tie::Hash::NamedCapture::SCALAR
165 &Tie::Hash::NamedCapture::flags
166 ])];
167 13 ▶if (defined $Devel::Cover::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Devel::Cover::VERSION) 13 0 13
168 13 push @$default_share, '&Devel::Cover::use_file';
169 }
170
171 48 ▶sub new {
Subroutine new: called CC=6 SCAR=18.4
Pod undocumented
172 48 my($class, $root, $mask) = @_;
173 48 my $obj = {};
174 48 bless $obj, $class;
175
176 48 ▶ if (defined($root)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 5 43 48
177 5 ▶ croak "Can't use \"$root\" as root name"
Branch: 1 branch
BranchTrueFalseTotal
if $root =~ /^main\b/ or not $root =~ /^\w[:\w]*$/ 0 5 5
Condition33%1/3
$root =~ /^main\b/ or not $root =~ /^\w[:\w]*$/
l!l&&r!l&&!r
005
MC/DC0%0/2
$root =~ /^main\b/ or not $root =~ /^\w[:\w]*$/
$root =~ /^main\b/ not $root =~ /^\w[:\w]*$/
Truth table33%1/3
A or not(B)
A= $root =~ /^main\b/B= not $root =~ /^\w[:\w]*$/
ABresult
1X1
011
000
178 if $root =~ /^main\b/ or $root !~ /^\w[:\w]*$/;
179 5 $obj->{Root} = $root;
180 5 $obj->{Erase} = 0;
181 }
182 else {
183 43 $obj->{Root} = "Eval::Safe::ForkedSafe::Root".$default_root++;
184 43 $obj->{Erase} = 1;
185 }
186
187 # use permit/deny methods instead till interface issues resolved
188 # XXX perhaps new Safe 'Root', mask => $mask, foo => bar, ...;
189 48 ▶ croak "Mask parameter to new no longer supported" if defined $mask;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mask 0 48 48
190 48 $obj->permit_only(':default');
191
192 # We must share $_ and @_ with the compartment or else ops such
193 # as split, length and so on won't default to $_ properly, nor
194 # will passing argument to subroutines work (via @_). In fact,
195 # for reasons I don't completely understand, we need to share
196 # the whole glob *_ rather than $_ and @_ separately, otherwise
197 # @_ in non default packages within the compartment don't work.
198 48 $obj->share_from('main', $default_share);
199
200 48 ▶ Opcode::_safe_pkg_prep($obj->{Root}) if($Opcode::VERSION > 1.04);
Branch: 1 branch
BranchTrueFalseTotal
if $Opcode::VERSION > "1.04" 48 0 48
201
202 48 return $obj;
203 }
204
205 48 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
206 48 my $obj = shift;
207 48 ▶ $obj->erase('DESTROY') if $obj->{Erase};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"Erase"} 43 5 48
208 }
209
210 43 ▶sub erase {
Subroutine erase: called CC=3 SCAR=11.1
Pod undocumented
211 43 my ($obj, $action) = @_;
212 43 my $pkg = $obj->root();
213 43 my ($stem, $leaf);
214
215 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 43 $pkg = "main::$pkg\::"; # expand to full symbol table name
217 43 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
218
219 # The 'my $foo' is needed! Without it you get an
220 # 'Attempt to free unreferenced scalar' warning!
221 43 my $stem_symtab = *{$stem}{HASH};
222
223 #warn "erase($pkg) stem=$stem, leaf=$leaf";
224 #warn " stem_symtab hash ".scalar(%$stem_symtab)."\n";
225 # ", join(', ', %$stem_symtab),"\n";
226
227 # delete $stem_symtab->{$leaf};
228
229 43 my $leaf_glob = $stem_symtab->{$leaf};
230 43 my $leaf_symtab = *{$leaf_glob}{HASH};
231 # warn " leaf_symtab ", join(', ', %$leaf_symtab),"\n";
232 43 %$leaf_symtab = ();
233 #delete $leaf_symtab->{'__ANON__'};
234 #delete $leaf_symtab->{'foo'};
235 #delete $leaf_symtab->{'main::'};
236 # my $foo = undef ${"$stem\::"}{"$leaf\::"};
237
238 43 ▶ if ($action and $action eq 'DESTROY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action and $action eq 'DESTROY') { } 43 0 43
Condition33%1/3
$action and $action eq "DESTROY"
!ll&&!rl&&r
0043
MC/DC0%0/2
$action and $action eq "DESTROY"
$action $action eq "DESTROY"
Truth table33%1/3
A and B
A= $actionB= $action eq "DESTROY"
ABresult
0X0
100
111
239 43 delete $stem_symtab->{$leaf};
240 } else {
241 0 $obj->share_from('main', $default_share);
242 }
243 43 1;
244 }
245
246
247 0 ▶sub reinit {
Subroutine reinit: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my $obj= shift;
249 0 $obj->erase;
250 0 $obj->share_redo;
251 }
252
253 134 ▶sub root {
Subroutine root: called CC=2 SCAR=7.1
Pod undocumented
254 134 my $obj = shift;
255 134 ▶ croak("Safe root method now read-only") if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 134 134
256 134 return $obj->{Root};
257 }
258
259
260 0 ▶sub mask {
Subroutine mask: not called CC=2 SCAR=17.9
Pod undocumented
261 0 my $obj = shift;
262 0 ▶ return $obj->{Mask} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
263 0 $obj->deny_only(@_);
264 }
265
266 # v1 compatibility methods
267 0 ▶sub trap { shift->deny(@_) }
Subroutine trap: not called CC=1 SCAR=6.9
Pod undocumented
268 0 ▶sub untrap { shift->permit(@_) }
Subroutine untrap: not called CC=1 SCAR=6.9
Pod undocumented
269
270 0 ▶sub deny {
Subroutine deny: not called CC=1 SCAR=6.9
Pod undocumented
271 0 my $obj = shift;
272 0 $obj->{Mask} |= opset(@_);
273 }
274 48 ▶sub deny_only {
Subroutine deny_only: called CC=1 SCAR=0.0
Pod undocumented
275 48 my $obj = shift;
276 48 $obj->{Mask} = opset(@_);
277 }
278
279 0 ▶sub permit {
Subroutine permit: not called CC=1 SCAR=6.9
Pod undocumented
280 0 my $obj = shift;
281 # XXX needs testing
282 0 $obj->{Mask} &= invert_opset opset(@_);
283 }
284 48 ▶sub permit_only {
Subroutine permit_only: called CC=1 SCAR=0.0
Pod undocumented
285 48 my $obj = shift;
286 48 $obj->{Mask} = invert_opset opset(@_);
287 }
288
289
290 0 ▶sub dump_mask {
Subroutine dump_mask: not called CC=1 SCAR=6.9
Pod undocumented
291 0 my $obj = shift;
292 0 print opset_to_hex($obj->{Mask}),"\n";
293 }
294
295
296 0 ▶sub share {
Subroutine share: not called CC=1 SCAR=6.9
Pod undocumented
297 0 my($obj, @vars) = @_;
298 0 $obj->share_from(scalar(caller), \@vars);
299 }
300
301
302 48 ▶sub share_from {
Subroutine share_from: called CC=15 SCAR=27.9
Pod undocumented
303 48 my $obj = shift;
304 48 my $pkg = shift;
305 48 my $vars = shift;
306 48 ▶ my $no_record = shift || 0;
Condition50%1/2
shift() || 0
l!l
048
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
307 48 my $root = $obj->root();
308 48 ▶ croak("vars not an array ref") unless ref $vars eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vars eq "ARRAY" 0 48 48
309 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
310 # Check that 'from' package actually exists
311 croak("Package \"$pkg\" does not exist")
312 48 ▶ unless keys %{"$pkg\::"};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{"${pkg}::";} 0 48 48
313 48 my $arg;
314 48 foreach $arg (@$vars) {
315 # catch some $safe->share($var) errors:
316 2496 my ($var, $type);
317 2496 ▶ $type = $1 if ($var = $arg) =~ s/^(\W)//;
Branch: 1 branch
BranchTrueFalseTotal
if ($var = $arg) =~ s/^(\W)// 2400 96 2496
318 # warn "share_from $pkg $type $var";
319 2496 for (1..2) { # assign twice to avoid any 'used once' warnings
320 192 ▶ *{$root."::$var"} = (!$type) ? \&{$pkg."::$var"}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '&' ? : 4224 576 4800
321 4224 ▶ : ($type eq '&') ? \&{$pkg."::$var"}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '$' ? : 384 192 576
322 384 ▶ : ($type eq '$') ? \${$pkg."::$var"}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '@' ? : 96 96 192
323 96 ▶ : ($type eq '@') ? \@{$pkg."::$var"}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '%' ? : 0 96 96
324 0 ▶ : ($type eq '%') ? \%{$pkg."::$var"}
Branch: 1 branch
BranchTrueFalseTotal
$type eq '*' ? : 96 0 96
325 4992 ▶ : ($type eq '*') ? *{$pkg."::$var"}
Branch: 1 branch
BranchTrueFalseTotal
!$type ? : 192 4800 4992
326 : croak(qq(Can't share "$type$var" of unknown type));
327 }
328 }
329 48 ▶ $obj->share_record($pkg, $vars) unless $no_record or !$vars;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_record or not $vars 48 0 48
Condition33%1/3
$no_record or not $vars
l!l&&r!l&&!r
0048
MC/DC0%0/2
$no_record or not $vars
$no_record not $vars
Truth table33%1/3
A or not(B)
A= $no_recordB= not $vars
ABresult
1X1
011
000
330 }
331
332
333 48 ▶sub share_record {
Subroutine share_record: called CC=3 SCAR=11.2
Pod undocumented
334 48 my $obj = shift;
335 48 my $pkg = shift;
336 48 my $vars = shift;
337 48 ▶ my $shares = \%{$obj->{Shares} ||= {}};
Condition50%1/2
$obj->{"Shares"} ||= {}
l!l
048
MC/DC0%0/1
$obj->{"Shares"} ||= {}
$obj->{"Shares"}
Truth table50%1/2
A
A= $obj->{"Shares"}
Aresult
11
00
338 # Record shares using keys of $obj->{Shares}. See reinit.
339 48 ▶ @{$shares}{@$vars} = ($pkg) x @$vars if @$vars;
Branch: 1 branch
BranchTrueFalseTotal
if @$vars 48 0 48
340 }
341
342
343 0 ▶sub share_redo {
Subroutine share_redo: not called CC=3 SCAR=24.8
Pod undocumented
344 0 my $obj = shift;
345 0 ▶ my $shares = \%{$obj->{Shares} ||= {}};
Condition0%0/2
$obj->{"Shares"} ||= {}
l!l
00
MC/DC0%0/1
$obj->{"Shares"} ||= {}
$obj->{"Shares"}
Truth table0%0/2
A
A= $obj->{"Shares"}
Aresult
11
00
346 0 my($var, $pkg);
347 0 ▶ while(($var, $pkg) = each %$shares) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$shares) 0 0 0
348 # warn "share_redo $pkg\:: $var";
349 0 $obj->share_from($pkg, [ $var ], 1);
350 }
351 }
352
353
354 0 ▶sub share_forget {
Subroutine share_forget: not called CC=1 SCAR=6.9
Pod undocumented
355 0 delete shift->{Shares};
356 }
357
358
359 0 ▶sub varglob {
Subroutine varglob: not called CC=1 SCAR=1.2
Pod undocumented
360 0 my ($obj, $var) = @_;
361 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
362 0 return *{$obj->root()."::$var"};
363 }
364
365 746 ▶sub _clean_stash {
Subroutine _clean_stash: called CC=5 SCAR=16.1
366 746 my ($root, $saved_refs) = @_;
367 746 ▶ $saved_refs ||= [];
Condition100%2/2
$saved_refs ||= []
l!l
69254
MC/DC100%1/1
$saved_refs ||= []
$saved_refs
Truth table100%2/2
A
A= $saved_refs
Aresult
11
00
368 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
369 746 foreach my $hook (qw(DESTROY AUTOLOAD), grep /^\(/, keys %$root) {
370 1758 push @$saved_refs, \*{$root.$hook};
371 1758 delete ${$root}{$hook};
372 }
373
374 746 for (grep /::$/, keys %$root) {
375 746 ▶ next if \%{$root.$_} eq \%$root;
Branch: 1 branch
BranchTrueFalseTotal
if \%{$root . $_;} eq \%$root 54 692 746
376 692 _clean_stash($root.$_, $saved_refs);
377 }
378 }
379
380 43 ▶sub reval {
Subroutine reval: called CC=6 SCAR=18.0
Pod undocumented
381 43 my ($obj, $expr, $strict) = @_;
382 43 ▶ die "Bad Safe object" unless $obj->isa('Eval::Safe::ForkedSafe');
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("Eval::Safe::ForkedSafe") 0 43 43
383
384 43 my $root = $obj->{Root};
385
386 43 my $evalsub = lexless_anon_sub($root, $strict, $expr);
387 # propagate context
388 43 my $sg = sub_generation();
389 43 my @subret;
390 43 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 29 14 43
391 @subret = (wantarray)
392 ? Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub)
393 29 ▶ : scalar Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 25 29
394 }
395 else {
396 14 Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
397 }
398 43 ▶ _clean_stash($root.'::') if $sg != sub_generation();
Branch: 1 branch
BranchTrueFalseTotal
if $sg != sub_generation() 43 0 43
399 43 $obj->wrap_code_refs_within(@subret);
400 43 ▶ return (wantarray) ? @subret : $subret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 39 43
401 }
402
403 13 my %OID;
404
405 54 ▶sub wrap_code_refs_within {
Subroutine wrap_code_refs_within: called CC=1 SCAR=0.0
Pod undocumented
406 54 my $obj = shift;
407
408 54 %OID = ();
409 54 $obj->_find_code_refs('wrap_code_ref', @_);
410 }
411
412
413 54 ▶sub _find_code_refs {
Subroutine _find_code_refs: called CC=8 SCAR=22.3
414 54 my $obj = shift;
415 54 my $visitor = shift;
416
417 54 for my $item (@_) {
418 43 ▶ my $reftype = $item && reftype $item
Branch: 1 branch
BranchTrueFalseTotal
my $reftype = $item && builtin::reftype($item) or next 8 35 43
Condition100%3/3
$item && builtin::reftype($item)
!ll&&!rl&&r
9268
MC/DC100%2/2
$item && builtin::reftype($item)
$item builtin::reftype($item)
Truth table100%3/3
A && B
A= $itemB= builtin::reftype($item)
ABresult
0X0
100
111
419 or next;
420
421 # skip references already seen
422 8 ▶ next if ++$OID{refaddr $item} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ++$OID{builtin::refaddr($item)} > 1 0 8 8
423
424 8 ▶ if ($reftype eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 0 8 8
elsif ($reftype eq 'HASH') { } 0 8 8
elsif ($reftype eq 'CODE') { } 8 0 8
425 0 $obj->_find_code_refs($visitor, @$item);
426 }
427 elsif ($reftype eq 'HASH') {
428 0 $obj->_find_code_refs($visitor, values %$item);
429 }
430 # XXX GLOBs?
431 elsif ($reftype eq 'CODE') {
432 8 $item = $obj->$visitor($item);
433 }
434 }
435 }
436
437
438 8 ▶sub wrap_code_ref {
Subroutine wrap_code_ref: called CC=3 SCAR=11.0
Pod undocumented
439 8 my ($obj, $sub) = @_;
440 8 ▶ die "Bad safe object" unless $obj->isa('Eval::Safe::ForkedSafe');
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("Eval::Safe::ForkedSafe") 0 8 8
441
442 # wrap code ref $sub with _safe_call_sv so that, when called, the
443 # execution will happen with the compartment fully 'in effect'.
444
445 8 ▶ croak "Not a CODE reference"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($sub) ne "CODE" 0 8 8
446 if reftype $sub ne 'CODE';
447
448 8 ▶ my $ret = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
449 8 my @args = @_; # lexical to close over
450 8 ▶ my $sub_with_args = sub { eval { $sub->(@args) } };
Subroutine __ANON__: called CC=1 SCAR=0.0
451
452 8 my @subret;
453 my $error;
454 8 do {
455 8 my $sg = sub_generation();
456 8 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 6 2 8
457 @subret = (wantarray)
458 ? Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args)
459 6 ▶ : scalar Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 5 6
460 } else {
461 2 Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args);
462 }
463 8 ▶ _clean_stash($obj->{Root}.'::') if $sg != sub_generation();
Branch: 1 branch
BranchTrueFalseTotal
if $sg != sub_generation() 8 0 8
464 };
465 8 $obj->wrap_code_refs_within(@subret);
466 8 ▶ return (wantarray) ? @subret : $subret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 7 8
467 8 };
468
469 8 return $ret;
470 }
471
472
473 3 ▶sub rdo {
Subroutine rdo: called CC=5 SCAR=16.7
Pod undocumented
474 3 my ($obj, $file) = @_;
475 3 ▶ die "Bad Safe object" unless $obj->isa('Eval::Safe::ForkedSafe');
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("Eval::Safe::ForkedSafe") 0 3 3
476
477 3 my $root = $obj->{Root};
478
479 3 my $sg = sub_generation();
480 3 my $evalsub = eval
481 sprintf('package %s; sub { @_ = (); do $file }', $root);
482 my @subret = (wantarray)
483 ? Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub)
484 3 ▶ : scalar Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
485 3 ▶ _clean_stash($root.'::') if $sg != sub_generation();
Branch: 1 branch
BranchTrueFalseTotal
if $sg != sub_generation() 3 0 3
486 3 $obj->wrap_code_refs_within(@subret);
487 3 ▶ return (wantarray) ? @subret : $subret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
488 }
489
490
491 13 1;
↑ Summary Safe.pm »