.github/dzil-binary/lib-gate.pl

filter: Statementstmt 91.4% 268 / 293 Branchbran 68.3% 93 / 136 Conditioncond 56.7% 38 / 67 MC/DCmcdc 31.5% 12 / 38 Subroutinesub 94.7% 36 / 38 Podpod n/a totaltotal 78.1% 447 / 572 CC 102 SCAR 26.6
CC
102
Cov
80%
spawn_win32
110.0
run_tool
15.5
linked
12.4
CRAP 14.3
« blib/lib/Dist/Zilla/Plugin/Author/GETTY/GitHubRelease.pm ↑ Summary .github/dzil-binary/plan.pl »
1 #!/usr/bin/env perl
2 # No foreign shared library may be linked by the loader, any object inside the
3 # binary, or any object its first start extracts into the PAR cache (libperl, the
4 # inner executable); on Linux the highest GLIBC_x.y must stay within the floor.
5 # lib-gate.pl <binary> <linux|macos|windows>
6 package DzilBinary::LibGate;
7 162 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 162 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 162 ▶use Archive::Zip qw( AZ_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 162 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 162 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 162 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 162 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 162 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 162 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 108 our $GLIBC_FLOOR = '2.28';
18 108 our $START_TIMEOUT = 60; # seconds the start may take before it is killed
19  
20 108 my @SYSTEM = (
21 linux => [ 'libc.so*', 'libm.so*', 'libpthread.so*', 'libdl.so*', 'librt.so*',
22 'libutil.so*', 'libgcc_s.so*', 'ld-linux-*', 'linux-vdso*' ],
23 macos => [ '/usr/lib/*', '/System/*' ],
24 windows => [ 'api-ms-*' ],
25 );
26 108 my %SYSTEM = @SYSTEM;
27  
28 # the perl shared library the start must have extracted (matched on the basename)
29 108 my %PERL_LIB = ( linux => 'libperl.so*', macos => 'libperl.dylib', windows => 'perl5*.dll' );
30  
31 1899 ▶sub glob_re {
Subroutine glob_re: called CC=1 SCAR=0.0
32 1899 my ($g) = @_;
33 1899 my $re = join '.*', map { quotemeta } split /\*/, $g, -1;
34 1899 return qr/\A$re\z/i;
35 }
36  
37 # A predicate: true for a library name the allowlist or a packed name covers.
38 407 ▶sub covered {
Subroutine covered: called CC=1 SCAR=0.0
39 407 my ($allowed, $packed) = @_;
40 407 my @re = map { glob_re($_) } @$allowed;
41 407 my %packed = map { lc($_) => 1 } @$packed;
42 1175 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
43 1175 my ($lib) = @_;
44 1175 my ($base) = $lib =~ m{([^/\\]+)\z};
45 1175 ▶ return $packed{lc $lib} || (defined $base && $packed{lc $base}) || scalar grep { $lib =~ $_ } @re;
Condition87%7/8
$packed{lc $lib} || defined $base && $packed{lc $base} || scalar grep({$lib =~ /$_/;} @re)
l!l
221954
$packed{lc $lib} or defined $base and $packed{lc $base}
l!l&&r!l&&!r
18338954
defined $base and $packed{lc $base}
!ll&&!rl&&r
095438
MC/DC66%2/3
$packed{lc $lib} || defined $base && $packed{lc $base} || scalar grep({$lib =~ /$_/;} @re)
$packed{lc $lib} defined $base $packed{lc $base}
Truth table75%3/4
A or B and C
A= $packed{lc $lib}B= defined $baseC= $packed{lc $base}
ABCresult
1XX1
0111
00X0
0100
46 407 };
47 }
48  
49 # %links: object => [library names]; returns sorted "lib (linked by object)" strings.
50 341 ▶sub violations {
Subroutine violations: called CC=4 SCAR=13.9
51 341 my ($links, $allowed, $packed) = @_;
52 341 my $covered = covered($allowed, $packed);
53 341 my @v;
54 341 for my $obj (keys %$links) {
55 459 for my $lib (@{ $links->{$obj} }) {
56 921 ▶ push @v, "$lib (linked by $obj)" unless $covered->($lib);
Branch: 1 branch
BranchTrueFalseTotal
unless &$covered($lib) 223 698 921
57 }
58 }
59 341 return sort @v;
60 }
61  
62 # The distinct library names violations() flags, sorted.
63 64 ▶sub foreign {
Subroutine foreign: called CC=1 SCAR=0.0
64 64 my ($links, $allowed, $packed) = @_;
65 64 my $covered = covered($allowed, $packed);
66 64 my %f = map { $_ => 1 } grep { !$covered->($_) } map { @$_ } values %$links;
67 64 return sort keys %f;
68 }
69  
70 356 ▶sub glibc_cmp {
Subroutine glibc_cmp: called CC=6 SCAR=18.2
71 356 my @a = split /\./, $_[0];
72 356 my @b = split /\./, $_[1];
73 356 ▶ while (@a || @b) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a or @b) 712 58 770
Condition66%2/3
@a or @b
l!l&&r!l&&!r
712058
MC/DC50%1/2
@a or @b
@a @b
Truth table66%2/3
A or B
A= @aB= @b
ABresult
1X1
011
000
74 712 ▶ my ($x, $y) = (shift(@a) // 0, shift(@b) // 0);
Condition50%2/4
shift @a // 0
l!l
7120
shift @b // 0
l!l
7120
MC/DC0%0/1
shift @a // 0
shift @a
MC/DC0%0/1
shift @b // 0
shift @b
Truth table50%1/2
A
A= shift @a
Aresult
11
00
Truth table50%1/2
A
A= shift @b
Aresult
11
00
75 712 ▶ return $x <=> $y if $x != $y;
Branch: 1 branch
BranchTrueFalseTotal
if $x != $y 298 414 712
76 }
77 58 return 0;
78 }
79  
80 150 ▶sub glibc_max {
Subroutine glibc_max: called CC=4 SCAR=13.9
81 150 my $max;
82 150 ▶ for my $v (@_) { $max = $v if !defined $max || glibc_cmp($v, $max) > 0 }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or glibc_cmp($v, $max) > 0 268 88 356
Condition100%3/3
not defined $max or glibc_cmp($v, $max) > 0
l!l&&r!l&&!r
15011888
MC/DC100%2/2
not defined $max or glibc_cmp($v, $max) > 0
not defined $max glibc_cmp($v, $max) > 0
Truth table100%3/3
not(A) or B
A= not defined $maxB= glibc_cmp($v, $max) > 0
ABresult
1X1
011
000
83 150 return $max;
84 }
85  
86 # --- CLI side (needs ldd / otool / objdump and real objects) ---------------
87  
88 153 ▶sub unpack_archive {
Subroutine unpack_archive: called CC=7 SCAR=21.0
89 153 my ($archive, $dest) = @_;
90 153 my $zip = Archive::Zip->new;
91 153 ▶ die "cannot read $archive as an archive\n" unless $zip->read($archive) == AZ_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless $zip->read($archive) == 0 0 153 153
92 153 for my $m ($zip->members) {
93 205 ▶ next if $m->isDirectory;
Branch: 1 branch
BranchTrueFalseTotal
if $m->isDirectory 0 205 205
94 205 my $n = $m->fileName;
95 205 ▶ next if $n =~ m{(?:\A|/)\.\.(?:/|\z)} || $n =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
if $n =~ m[(?:\A|/)\.\.(?:/|\z)] or $n =~ m[\A/] 0 205 205
Condition33%1/3
$n =~ m[(?:\A|/)\.\.(?:/|\z)] or $n =~ m[\A/]
l!l&&r!l&&!r
00205
MC/DC0%0/2
$n =~ m[(?:\A|/)\.\.(?:/|\z)] or $n =~ m[\A/]
$n =~ m[(?:\A|/)\.\.(?:/|\z)] $n =~ m[\A/]
Truth table33%1/3
A or B
A= $n =~ m[(?:\A|/)\.\.(?:/|\z)]B= $n =~ m[\A/]
ABresult
1X1
011
000
96 205 my $to = File::Spec->catfile($dest, split m{/}, $n);
97 205 File::Path::make_path(File::Basename::dirname($to));
98 205 ▶ $m->extractToFileNamed($to) == AZ_OK or die "cannot extract $n\n";
Branch: 1 branch
BranchTrueFalseTotal
$m->extractToFileNamed($to) == 0 or die "cannot extract $n\n" 205 0 205
99 }
100 }
101  
102 286 ▶sub is_native {
Subroutine is_native: called CC=5 SCAR=17.2
103 286 my ($file) = @_;
104 286 ▶ open my $fh, '<:raw', $file or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return 0 286 0 286
105 286 read($fh, my $magic, 4);
106 286 ▶ return 0 unless defined $magic && length $magic >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $magic and length $magic >= 2 0 286 286
Condition33%1/3
defined $magic and length $magic >= 2
!ll&&!rl&&r
00286
MC/DC0%0/2
defined $magic and length $magic >= 2
defined $magic length $magic >= 2
Truth table33%1/3
A and B
A= defined $magicB= length $magic >= 2
ABresult
0X0
100
111
107 286 ▶ return $magic =~ /\A(?:\x7fELF|MZ|\xfe\xed\xfa[\xce\xcf]|[\xce\xcf]\xfa\xed\xfe|\xca\xfe\xba\xbe)/s ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$magic =~ /\A(?:\x7fELF|MZ|\xfe\xed\xfa[\xce\xcf]|[\xce\xcf]\xfa\xed\xfe|\xca\xfe\xba\xbe)/s ? : 133 153 286
108 }
109  
110 # Runs a tool; dies when it cannot be spawned (naming it). Returns (output, exit code).
111 505 ▶sub run_tool {
Subroutine run_tool: called CC=13 SCAR=27.4
112 505 my ($merge_stderr, @cmd) = @_;
113 505 ▶ return run_tool_win32($merge_stderr, @cmd) if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 47 458 505
114 458 ▶ pipe(my $r, my $w) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $r, my $w or die "pipe: $!\n" 458 0 458
115 458 ▶ pipe(my $er, my $ew) or die "pipe: $!\n"; # exec-failure channel, closed on exec
Branch: 1 branch
BranchTrueFalseTotal
pipe my $er, my $ew or die "pipe: $!\n" 458 0 458
116 458 ▶ my $pid = fork // die "fork: $!\n";
Condition50%1/2
fork // die("fork: $!\n")
l!l
4580
MC/DC0%0/1
fork // die("fork: $!\n")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
117 458 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 105 353 458
118 105 close $r; close $er;
119 105 ▶ open STDOUT, '>&', $w or POSIX::_exit(127);
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $w or POSIX::_exit(127) 105 0 105
120 105 ▶ open STDERR, '>&', $w or POSIX::_exit(127) if $merge_stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $merge_stderr 35 70 105
Condition50%3/6
$merge_stderr && (open(STDERR, '>&', $w) || POSIX::_exit(127))
!ll
70350
open STDERR, ">&", $w or POSIX::_exit(127)
l!l
3500
MC/DC0%0/3
$merge_stderr && (open(STDERR, '>&', $w) || POSIX::_exit(127))
$merge_stderr open STDERR, ">&", $w POSIX::_exit(127)
Truth table0%0/4
A && B or C
A= $merge_stderrB= open STDERR, ">&", $wC= POSIX::_exit(127)
ABCresult
0XX0
1000
11X1
1011
121 105 { exec @cmd }
122 0 { my $o = select $ew; $| = 1; select $o }
123 0 print $ew "$!";
124 0 POSIX::_exit(127);
125 }
126 353 close $w; close $ew;
127 353 my $out = do { local $/; <$r> };
128 353 my $failed = do { local $/; <$er> };
129 353 close $r; close $er;
130 353 waitpid $pid, 0;
131 353 ▶ die "cannot run $cmd[0]: $failed\n" if defined $failed && length $failed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $failed and length $failed 6 347 353
Condition66%2/3
defined $failed and length $failed
!ll&&!rl&&r
03476
MC/DC50%1/2
defined $failed and length $failed
defined $failed length $failed
Truth table66%2/3
A and B
A= defined $failedB= length $failed
ABresult
0X0
100
111
132 347 ▶ die "$cmd[0] was killed by signal " . ($? & 127) . "\n" if $? & 127;
Branch: 1 branch
BranchTrueFalseTotal
if $? & 127 6 341 347
133 341 ▶ return ($out // '', $? >> 8);
Condition50%1/2
$out // ""
l!l
3410
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
134 }
135  
136 # Win32: fork is emulated there and pipe handles are not duplicated reliably
137 # into the pseudo-process (perlfork, BUGS), so the pipe + fork + exec above never
138 # sees EOF. A list-form pipe open spawns the tool directly. STDOUT only: the
139 # tool's STDERR stays the gate's, and merge_stderr (only ldd, Linux) dies.
140 # Win32 keeps only the low byte of an exit code and has no signals: a killed or
141 # crashed tool shows as that byte, the signal check never fires there.
142 41 ▶sub run_tool_win32 {
Subroutine run_tool_win32: called CC=6 SCAR=18.0
143 41 my ($merge_stderr, @cmd) = @_;
144 41 ▶ die "cannot run $cmd[0]: merging its STDERR is not supported on MSWin32\n" if $merge_stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $merge_stderr 13 28 41
145 28 ▶ open my $fh, '-|', @cmd or die "cannot run $cmd[0]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @cmd or die "cannot run $cmd[0]: $!\n" 24 4 28
146 24 my $out = do { local $/; <$fh> };
147 24 close $fh; # false on a non-zero exit; $? holds the status either way
148 24 ▶ die "cannot wait for $cmd[0]: $!\n" if $? == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $? == -1 0 24 24
149 24 ▶ die "$cmd[0] was killed by signal " . ($? & 127) . "\n" if $? & 127;
Branch: 1 branch
BranchTrueFalseTotal
if $? & 127 4 20 24
150 20 ▶ return ($out // '', $? >> 8);
Condition50%1/2
$out // ""
l!l
200
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
151 }
152  
153 # Unix: the child leads its own process group, so a timeout takes the loader's
154 # children with it. Dies when the binary cannot be executed (naming it).
155 92 ▶sub spawn_unix {
Subroutine spawn_unix: called CC=10 SCAR=24.9
156 92 my ($binary, $null) = @_;
157 92 ▶ pipe(my $er, my $ew) or die "pipe: $!\n"; # exec-failure channel, closed on exec
Branch: 1 branch
BranchTrueFalseTotal
pipe my $er, my $ew or die "pipe: $!\n" 92 0 92
158 92 ▶ my $pid = fork // die "fork: $!\n";
Condition50%1/2
fork // die("fork: $!\n")
l!l
920
MC/DC0%0/1
fork // die("fork: $!\n")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
159 92 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 23 69 92
160 23 close $er;
161 0 ▶ my $fail = sub { syswrite $ew, $_[0]; POSIX::_exit(127) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
162 23 ▶ setpgrp(0, 0) or $fail->("setpgrp: $!");
Branch: 1 branch
BranchTrueFalseTotal
setpgrp 0, 0 or &$fail("setpgrp: $!") 23 0 23
163 23 ▶ open(STDIN, '<', $null) && open(STDOUT, '>', $null) && open(STDERR, '>', $null)
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $null and open STDOUT, ">", $null and open STDERR, ">", $null or &$fail("cannot open ${null}: $!") 23 0 23
Condition33%2/6
open STDIN, "<", $null and open STDOUT, ">", $null and open STDERR, ">", $null
!ll&&!rl&&r
0023
open STDIN, "<", $null and open STDOUT, ">", $null
!ll&&!rl&&r
0023
MC/DC0%0/3
open STDIN, "<", $null and open STDOUT, ">", $null and open STDERR, ">", $null
open STDIN, "<", $null open STDOUT, ">", $null open STDERR, ">", $null
Truth table25%1/4
A and B and C
A= open STDIN, "<", $nullB= open STDOUT, ">", $nullC= open STDERR, ">", $null
ABCresult
0XX0
10X0
1100
1111
164 or $fail->("cannot open $null: $!");
165 23 { exec { $binary } $binary, '--help' }
166 0 $fail->("$!");
167 }
168 69 close $ew;
169 69 my $failed = do { local $/; <$er> };
170 69 close $er;
171 69 ▶ if (defined $failed && length $failed) { waitpid $pid, 0; die "cannot start $binary: $failed\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $failed and length $failed) 1 68 69
Condition66%2/3
defined $failed and length $failed
!ll&&!rl&&r
0681
MC/DC50%1/2
defined $failed and length $failed
defined $failed length $failed
Truth table66%2/3
A and B
A= defined $failedB= length $failed
ABresult
0X0
100
111
172 68 return $pid;
173 }
174  
175 # Win32 (fork is emulated there): spawn with system 1, STDOUT/STDERR pointed at
176 # the null device for the call and restored after it. The gate never reads
177 # stdin, so it stays on the null device.
178 0 ▶sub spawn_win32 {
Subroutine spawn_win32: not called CC=10 SCAR=47.0
179 0 my ($binary, $null) = @_;
180 0 ▶ open STDIN, '<', $null or die "cannot open $null: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $null or die "cannot open ${null}: $!\n" 0 0 0
181 0 ▶ open my $out, '>&', \*STDOUT or die "cannot dup STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">&", \*STDOUT or die "cannot dup STDOUT: $!\n" 0 0 0
182 0 ▶ open my $err, '>&', \*STDERR or die "cannot dup STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $err, ">&", \*STDERR or die "cannot dup STDERR: $!\n" 0 0 0
183 0 my ($pid, $why);
184 0 ▶ if (open(STDOUT, '>', $null) && open(STDERR, '>', $null)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open STDOUT, '>', $null and open STDERR, '>', $null) { } 0 0 0
Condition0%0/3
open STDOUT, ">", $null and open STDERR, ">", $null
!ll&&!rl&&r
000
MC/DC0%0/2
open STDOUT, ">", $null and open STDERR, ">", $null
open STDOUT, ">", $null open STDERR, ">", $null
Truth table0%0/3
A and B
A= open STDOUT, ">", $nullB= open STDERR, ">", $null
ABresult
0X0
100
111
185 0 $pid = system 1, File::Spec->canonpath($binary), '--help'; # C:/x -> C:\x for CreateProcess
186 0 $why = "$!";
187 }
188 0 else { $why = "cannot open $null: $!" }
189 0 ▶ open STDOUT, '>&', $out or die "cannot restore STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $out or die "cannot restore STDOUT: $!\n" 0 0 0
190 0 ▶ open STDERR, '>&', $err or die "cannot restore STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $err or die "cannot restore STDERR: $!\n" 0 0 0
191 0 ▶ die "cannot start $binary: $why\n" unless defined $pid && $pid > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid > 0 0 0 0
Condition0%0/3
defined $pid and $pid > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pid and $pid > 0
defined $pid $pid > 0
Truth table0%0/3
A and B
A= defined $pidB= $pid > 0
ABresult
0X0
100
111
192 0 return $pid;
193 }
194  
195 # Starts the binary once as the install scripts' warm-up does: <binary> --help,
196 # stdin at EOF, output discarded, exit status ignored, the PAR cache in $cache
197 # (PAR_GLOBAL_TEMP is the exact cache directory). A start still running after
198 # $START_TIMEOUT seconds is killed with its process group (Unix) or process tree
199 # (Win32); a timeout alone is not a failure. Dies when it cannot be started.
200 92 ▶sub start_binary {
Subroutine start_binary: called CC=5 SCAR=16.1
201 92 my ($binary, $cache) = @_;
202 92 local $ENV{PAR_GLOBAL_TEMP} = $cache;
203 92 my $null = File::Spec->devnull;
204 92 ▶ my $pid = $^O eq 'MSWin32' ? spawn_win32($binary, $null) : spawn_unix($binary, $null);
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 92 92
205 68 my $deadline = Time::HiRes::time() + $START_TIMEOUT;
206 68 ▶ while (waitpid($pid, POSIX::WNOHANG()) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($pid, 1) == 0) 308 56 364
207 308 ▶ if (Time::HiRes::time() >= $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Time::HiRes::time() >= $deadline) 12 296 308
208 12 kill -9, $pid; # negative signal: Unix the process group $pid leads, Win32 the process tree
209 12 waitpid $pid, 0;
210 12 ▶ print STDERR "$binary --help timed out after ${START_TIMEOUT}s: killed it with its ",
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 12 12
211 ($^O eq 'MSWin32' ? 'process tree' : 'process group'), "; checking what the cache holds\n";
212 12 return;
213 }
214 296 Time::HiRes::sleep(0.1);
215 }
216 56 return;
217 }
218  
219 # native files anywhere under $dir, whatever their name: [ path, label ], sorted
220 68 ▶sub cache_objects {
Subroutine cache_objects: called CC=1 SCAR=0.0
221 68 my ($dir) = @_;
222 68 my @found;
223 403 ▶ File::Find::find({ no_chdir => 1, wanted => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
224 403 ▶ push @found, $File::Find::name if -f $_ && is_native($_);
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and is_native($_) 133 270 403
Condition66%2/3
-f $_ and is_native($_)
!ll&&!rl&&r
2700133
MC/DC50%1/2
-f $_ and is_native($_)
-f $_ is_native($_)
Truth table66%2/3
A and B
A= -f $_B= is_native($_)
ABresult
0X0
100
111
225 68 } }, $dir);
226 68 return map { [ $_, join '/', 'cache', File::Spec->splitdir(File::Spec->abs2rel($_, $dir)) ] } sort @found;
227 }
228  
229 263 ▶sub capture {
Subroutine capture: called CC=2 SCAR=6.9
230 263 my (@cmd) = @_;
231 263 my ($out, $rc) = run_tool(0, @cmd);
232 192 ▶ die "$cmd[0] failed (exit $rc) on $cmd[-1]\n" if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 1 191 192
233 191 return $out;
234 }
235  
236 # ldd: only "not a dynamic executable" / "statically linked" may fail.
237 169 ▶sub capture_ldd {
Subroutine capture_ldd: called CC=3 SCAR=15.1
238 169 my ($obj) = @_;
239 169 my ($out, $rc) = run_tool(1, 'ldd', $obj);
240 133 ▶ return $out if !$rc;
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 133 0 133
241 0 ▶ return '' if $out =~ /not a dynamic|statically linked/;
Branch: 1 branch
BranchTrueFalseTotal
if $out =~ /not a dynamic|statically linked/ 0 0 0
242 0 die "ldd failed (exit $rc) on $obj\n";
243 }
244  
245 299 ▶sub linked {
Subroutine linked: called CC=11 SCAR=25.2
246 299 my ($platform, $obj) = @_;
247 299 my %seen;
248 299 ▶ if ($platform eq 'linux') {
Branch: 2 branches
BranchTrueFalseTotal
if ($platform eq 'linux') { } 169 130 299
elsif ($platform eq 'macos') { } 19 111 130
249 169 for (split /\n/, capture_ldd($obj)) {
250 168 ▶ if (/^\s*(\S+)\s+=>/) { $seen{ File::Basename::basename($1) } = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*(\S+)\s+=>/) { } 168 0 168
elsif (m[^\s*(/\S+|\S+)\s+\(0x]) { } 0 0 0
251 0 elsif (m{^\s*(/\S+|\S+)\s+\(0x}) { $seen{ File::Basename::basename($1) } = 1 }
252 }
253 }
254 elsif ($platform eq 'macos') {
255 19 my @l = split /\n/, capture('otool', '-L', $obj);
256 12 shift @l;
257 12 for (@l) {
258 18 ▶ next unless /^\s+(\S+)\s+\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s+(\S+)\s+\(/ 0 18 18
259 18 my $n = $1;
260 18 ▶ $n = File::Basename::basename($n) if $n =~ /\A\@/;
Branch: 1 branch
BranchTrueFalseTotal
if $n =~ /\A\@/ 0 18 18
261 18 $seen{$n} = 1;
262 }
263 }
264 else {
265 111 ▶ for (split /\n/, capture('objdump', '-p', $obj)) { $seen{ lc $1 } = 1 if /DLL Name:\s*(\S+)/ }
Branch: 1 branch
BranchTrueFalseTotal
if /DLL Name:\s*(\S+)/ 125 0 125
266 }
267 225 return [ sort keys %seen ];
268 }
269  
270 133 ▶sub glibc_versions {
Subroutine glibc_versions: called CC=2 SCAR=6.9
271 133 my ($obj) = @_;
272 133 my %v;
273 133 my $out = capture('objdump', '-T', $obj);
274 99 ▶ $v{$1} = 1 while $out =~ /GLIBC_(\d+(?:\.\d+)*)/g;
Branch: 1 branch
BranchTrueFalseTotal
$out =~ /GLIBC_(\d+(?:\.\d+)*)/g and do { $v{$1} = 1 } 104 99 203
275 99 return keys %v;
276 }
277  
278 # What the gate judges, without the verdict: the links of the loader, of every
279 # object inside $archive and of every native file its first start extracts into
280 # the PAR cache, and the names of all of those (packed). $report gets the cache
281 # listing before the perl library check. Returns
282 # { links => { label => [names] }, packed => [names], glibc => { version => 1 }, checked => count }.
283 153 ▶sub inspect_binary {
Subroutine inspect_binary: called CC=8 SCAR=20.8
284 153 my ($archive, $platform, $report) = @_;
285 153 ▶ die "unknown platform '$platform' (linux, macos, windows)\n" unless $SYSTEM{$platform};
Branch: 1 branch
BranchTrueFalseTotal
unless $SYSTEM{$platform} 0 153 153
286 153 my $tmp = File::Temp->newdir;
287 153 unpack_archive($archive, "$tmp");
288  
289 153 my @objects;
290 812 ▶ File::Find::find({ no_chdir => 1, wanted => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
291 812 ▶ push @objects, $File::Find::name
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and /\.(?:so(?:\.[\d.]+)?|dylib|bundle|dll|pyd)\z/i 203 609 812
Condition100%3/3
-f $_ and /\.(?:so(?:\.[\d.]+)?|dylib|bundle|dll|pyd)\z/i
!ll&&!rl&&r
6072203
MC/DC100%2/2
-f $_ and /\.(?:so(?:\.[\d.]+)?|dylib|bundle|dll|pyd)\z/i
-f $_ /\.(?:so(?:\.[\d.]+)?|dylib|bundle|dll|pyd)\z/i
Truth table100%3/3
A and B
A= -f $_B= /\.(?:so(?:\.[\d.]+)?|dylib|bundle|dll|pyd)\z/i
ABresult
0X0
100
111
292 if -f $_ && /\.(?:so(?:\.[\d.]+)?|dylib|bundle|dll|pyd)\z/i;
293 153 } }, "$tmp");
294 153 ▶ my $packed_name = sub { $platform eq 'windows' ? lc File::Basename::basename($_[0]) : File::Basename::basename($_[0]) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$platform eq 'windows' ? : 107 151 258
295 153 my @packed = map { $packed_name->($_) } @objects;
296 153 ▶ unshift @objects, $archive if is_native($archive);
Branch: 1 branch
BranchTrueFalseTotal
if is_native($archive) 0 153 153
297 153 ▶ die "no shared objects to check in $archive: the gate proved nothing\n" unless @objects;
Branch: 1 branch
BranchTrueFalseTotal
unless @objects 2 151 153
298  
299 151 my (%links, %glibc);
300 290 ▶ my $inspect = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
301 290 my ($o, $label) = @_;
302 290 $links{$label} = linked($platform, $o);
303 219 ▶ $glibc{$_} = 1 for $platform eq 'linux' ? glibc_versions($o) : ();
Branch: 1 branch
BranchTrueFalseTotal
$platform eq 'linux' ? : 133 86 219
304 151 };
305 151 ▶ $inspect->($_, $_ eq $archive ? "$archive (loader)" : File::Spec->abs2rel($_, "$tmp")) for @objects;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $archive ? : 0 189 189
306  
307 # libperl, the inner executable and PAR's own XS live in the loader, not in the
308 # zip: only a start shows them
309 92 my $cache = File::Temp->newdir;
310 92 start_binary($archive, "$cache");
311 68 my @cached = cache_objects("$cache");
312 68 $report->("PAR cache after the start: " . scalar(@cached) . " object(s)\n", map({ " $_->[1]\n" } @cached));
313 68 my $perl_re = glob_re($PERL_LIB{$platform});
314 die "the start extracted no perl library into PAR_GLOBAL_TEMP: the gate proved nothing about libperl\n"
315 68 ▶ unless grep { File::Basename::basename($_->[0]) =~ $perl_re } @cached;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {File::Basename::basename($_->[0]) =~ /$perl_re/;} @cached 3 65 68
316 65 for my $c (@cached) {
317 101 $inspect->(@$c);
318 55 push @packed, $packed_name->($c->[0]);
319 }
320 19 return { links => \%links, packed => \@packed, glibc => \%glibc, checked => @objects + @cached };
321 }
322  
323 # The system libraries $platform's objects may link; on Windows also every name
324 # in %$links that System32 holds.
325 58 ▶sub allowlist {
Subroutine allowlist: called CC=3 SCAR=11.0
326 58 my ($platform, $links) = @_;
327 58 my @allowed = @{ $SYSTEM{$platform} };
328 58 ▶ if ($platform eq 'windows') {
Branch: 1 branch
BranchTrueFalseTotal
if ($platform eq "windows") 46 12 58
329 46 ▶ my $sys = File::Spec->catdir($ENV{SystemRoot} // 'C:\\Windows', 'System32');
Condition50%1/2
$ENV{"SystemRoot"} // "C:\\Windows"
l!l
460
MC/DC0%0/1
$ENV{"SystemRoot"} // "C:\\Windows"
$ENV{"SystemRoot"}
Truth table50%1/2
A
A= $ENV{"SystemRoot"}
Aresult
11
00
330 46 my %names = map { $_ => 1 } map { @$_ } values %$links;
331 46 push @allowed, map { quotemeta } grep { -e File::Spec->catfile($sys, $_) } sort keys %names;
332 46 @allowed = map { s/\\//gr } @allowed; # names have no '*', plain strings
333 }
334 58 return @allowed;
335 }
336  
337 129 ▶sub run {
Subroutine run: called CC=7 SCAR=19.5
338 129 my ($archive, $platform) = @_;
339 129 local $| = 1; # nothing buffered on STDOUT when run_tool / the start fork
340 129 ▶ my $seen = inspect_binary($archive, $platform, sub { print @_ });
Subroutine __ANON__: called CC=1 SCAR=0.0
341 12 my %links = %{ $seen->{links} };
342 12 my %glibc = %{ $seen->{glibc} };
343  
344 12 my @v = violations(\%links, [allowlist($platform, \%links)], $seen->{packed});
345 12 my $fail = 0;
346 12 ▶ if (@v) { print STDERR "foreign shared libraries:\n", map({ " $_\n" } @v); $fail = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (@v) 4 8 12
347 12 ▶ if ($platform eq 'linux' && !%glibc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($platform eq 'linux' and not %glibc) { } 1 11 12
elsif ($platform eq 'linux') { } 5 6 11
Condition100%3/3
$platform eq "linux" and not %glibc
!ll&&!rl&&r
651
MC/DC100%2/2
$platform eq "linux" and not %glibc
$platform eq "linux" not %glibc
Truth table100%3/3
A and not(B)
A= $platform eq "linux"B= not %glibc
ABresult
0X0
100
111
348 1 print STDERR "glibc check did not run: no GLIBC_ version found in any object\n"; $fail = 1;
349 }
350 elsif ($platform eq 'linux') {
351 5 my $max = glibc_max(keys %glibc);
352 5 ▶ if (glibc_cmp($max, $GLIBC_FLOOR) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (glibc_cmp($max, $DzilBinary::LibGate::GLIBC_FLOOR) > 0) { } 2 3 5
353 2 print STDERR "glibc $max needed, floor is $GLIBC_FLOOR\n"; $fail = 1;
354 }
355 3 else { print "highest glibc symbol version: $max (floor $GLIBC_FLOOR)\n" }
356 }
357 12 ▶ print "lib-gate $platform: $seen->{checked} object(s) checked, " . ($fail ? 'FAILED' : 'ok') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$fail ? : 7 5 12
358 12 return $fail;
359 }
360  
361 129 ▶sub main {
Subroutine main: called CC=3 SCAR=11.0
362 129 my ($archive, $platform) = @_;
363 129 ▶ die "usage: lib-gate.pl <binary> <linux|macos|windows>\n" unless defined $platform;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $platform 0 129 129
364 129 my $rc = eval { run($archive, $platform) };
365 24 ▶ if ($@) { print STDERR $@; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 12 12 24
366 12 return $rc;
367 }
368  
369 108 require File::Path;
370 108 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 108 0 108
371 1;
« blib/lib/Dist/Zilla/Plugin/Author/GETTY/GitHubRelease.pm ↑ Summary .github/dzil-binary/plan.pl »