| 1 |
#! /usr/bin/perl |
| 2 |
# Run a test given as @ARGV, and then munge its exit code to 77 if it |
| 3 |
# failed because of an "exec format error." |
| 4 |
# |
| 5 |
# Written by Zack Weinberg <zackw at panix.com> in 2017--2020. |
| 6 |
# To the extent possible under law, Zack Weinberg has waived all |
| 7 |
# copyright and related or neighboring rights to this work. |
| 8 |
# |
| 9 |
# See https://creativecommons.org/publicdomain/zero/1.0/ for further |
| 10 |
# details. |
| 11 |
|
| 12 |
use v5.14; # implicit use strict; use feature ':5.14'; |
| 13 |
use warnings FATAL => 'all'; |
| 14 |
use utf8; |
| 15 |
use open qw(:std :utf8); |
| 16 |
no if $] >= 5.022, warnings => 'experimental::re_strict'; |
| 17 |
use if $] >= 5.022, re => 'strict'; |
| 18 |
|
| 19 |
use FindBin (); |
| 20 |
use IPC::Open3 qw(open3); |
| 21 |
use Symbol qw(gensym); |
| 22 |
|
| 23 |
use lib $FindBin::Bin; |
| 24 |
use BuildCommon qw(subprocess_error); |
| 25 |
|
| 26 |
# We cannot simply exec the program and then check $!{ENOEXEC} if that |
| 27 |
# fails, because Perl's `exec` primitive calls execvp(3), and POSIX |
| 28 |
# requires execvp(3) to retry after rewriting the command line as |
| 29 |
# `/bin/sh PROGRAM ARGUMENTS...` if execve(2) fails with ENOEXEC. |
| 30 |
# This is a backward compatibility quirk for the sake of code written |
| 31 |
# before `#!` was a thing; if you ask me, on modern systems it's a |
| 32 |
# misfeature, but the Austin Group seems unwilling to change anything |
| 33 |
# (see https://austingroupbugs.net/view.php?id=1435). Even if they |
| 34 |
# did, we would be stuck with build-system C libraries that |
| 35 |
# implemented the old bad behavior for many years. |
| 36 |
# |
| 37 |
# There is no way to call execve(2) directly from Perl. We could |
| 38 |
# implement this program in C, but then we would need $(CC_FOR_BUILD), |
| 39 |
# which currently we do not. So what we do, is rely on /bin/sh to |
| 40 |
# have some kind of heuristic to detect when it's being asked to |
| 41 |
# interpret a machine-code executable, and to print a recognizable |
| 42 |
# error message when this happens. This is the same thing the old |
| 43 |
# shell-based implementation did, so we can live with it. |
| 44 |
|
| 45 |
my $status = eval { |
| 46 |
my $child_err = gensym; |
| 47 |
my $pid = open3('<&STDIN', '>&STDOUT', $child_err, @ARGV); |
| 48 |
my $saw_enoexec = 0; |
| 49 |
|
| 50 |
local $_; |
| 51 |
while (<$child_err>) { |
| 52 |
$saw_enoexec = 1 |
| 53 |
if m{\b(?: |
| 54 |
[Ex]ec \s+ format \s+ error |
| 55 |
| cannot \s+ execute \s+ (?:ELF \s+)? binary |
| 56 |
)\b}x; |
| 57 |
print {*STDERR} $_; |
| 58 |
} |
| 59 |
close $child_err or die "read error: $!\n"; |
| 60 |
waitpid $pid, 0 or die "waitpid: $!\n"; |
| 61 |
|
| 62 |
if (my $sig = ($? & 0x7F)) { |
| 63 |
# subprocess_error knows how to print symbolic names for signals. |
| 64 |
subprocess_error($ARGV[0]); |
| 65 |
} else { |
| 66 |
return 77 if $saw_enoexec; |
| 67 |
return $? >> 8; |
| 68 |
} |
| 69 |
}; |
| 70 |
exit $status if defined $status; |
| 71 |
|
| 72 |
# We only get here if there was an error. |
| 73 |
my $err = $@; |
| 74 |
$err =~ s/\s+ at \s+ \S+ \s+ line \s+ \d+ \.? \n? \Z//x; |
| 75 |
$err =~ s/^open3: //; |
| 76 |
|
| 77 |
print {*STDERR} "${FindBin::Script}: $err\n"; |
| 78 |
exit(($err =~ /\bExec format error\b/) ? 77 : 99); |