Skip to content

Commit 73172a6

Browse files
committed
util.c: Perl_xs_handshake print API ver mismatch before interp mismatch
-this fatal error is much more common by general users than I (orig author) anticipated when I added this check in 5.21.6/2014. I assumed Unix land never had ABI/SEGVing or upgrade problems previous. I wrote the code for my dev style, and my personal setup as test cases, and test cases with Win32-isms. If other OSes get bad-ABI caught, its a plus, but I thought they wouldn't. -the hexadecimal handshake keys were intended to be a debug tool for core devs hacking on something and for XS authors with very complicated Makefile.PL s. To catch -D CCFLAGS arg dropouts on the way to the final cmd line invocation of the CC. -I say the handshake keys are a terrible UI for general "power users" and non-coder sys admins -the Perl API version strings ARE available, even with mismatched interp struct sizes, and those are much more user friendly to print as a error. It should be obvious that from now on, non-power users can figure out on their own (no community help) that a way to "fix" XS boot handshake is to force "reinstall" the "left side perl" or "right side perl" through the OS Pkg Manager. -after this commit, much more often! but not always, users will see a "Perl API 5.X.Y against 5.X+1.Y is incompatible" fatal message instead of the those Core-dev only undocumented hex handshake keys. Sadly the technical P5P debug info is now gone/lost/hidden if "Perl API 5.X.Y against 5.X+1.Y is incompatible" fatal message executes. -core devs, obv will have v5.X.Y matching v5.X.Y in blead perl, so they will still get the handshake keys hex numbers. Since API strings are same. -Package name will get downgraded to "Foo.c" if interp size is wrong, or 2 libperls in 1 proc happens. But the major improvement is showing left and right side Perl API version info. -The POD text is very wordy and detailed, since it has been observed over time, some Perl users, do not know Perl's backend implementation is written in the ISO C language. Or other Perl users on various internet forums or social media, do not know what the term XS code is. While they can sucessful write and debug private personal Perl 5 code, they only read the POD of CPAN modules and only use public documented APIs of CPAN modules, and rarely or never look at "private source" of CPAN modules. Therefore this group of users truly do not know MANY MANY p5p core modules and CPAN modules, make call outs to another language (C), and are unable to troubleshoot a .so file on their filing system is responsible for the error. Since they do not know about XS code concept, their troubleshooting goes very wrong as they keeping looking and keep incorrect diagnosing the problem to ASCII text somewhere in the Perl ecosystem. Either Perl source code, they wrote, or CPAN Perl source code, or a CSV, YAML or JSON file related to Perl. Make it clear, that some "Perl 5 modules" written in ASCII text in Perl 5 lang, depend on "foreign" C code and "foreign" .so/.dll files at runtime. First time Perl coders, can mistakenly assume the Perl 5 interpreter has JIT and self bootstraps to OS binaries from only Perl 5 source like Google Chrome V8 and Raku. So they guess, as first time users, Perl 5 also does it and has no dependency on legacy technologies like C or C++. This commit was specifically written for #16654 but there are dozens or 100s of them #19112
1 parent df4d5e8 commit 73172a6

File tree

4 files changed

+155
-19
lines changed

4 files changed

+155
-19
lines changed

ext/XS-APItest/APItest.xs

+59
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,65 @@ xsreturn_empty()
19411941
PPCODE:
19421942
XSRETURN_EMPTY;
19431943

1944+
void
1945+
test_mismatch_xs_handshake_api_ver(...)
1946+
ALIAS:
1947+
test_mismatch_xs_handshake_bad_struct = 1
1948+
test_mismatch_xs_handshake_bad_struct_and_ver = 2
1949+
PPCODE:
1950+
if(ix == 0) {
1951+
#ifdef MULTIPLICITY
1952+
Perl_xs_handshake(HS_KEYp(sizeof(PerlInterpreter),
1953+
TRUE, NULL, FALSE,
1954+
sizeof("v1.1337.0")-1,
1955+
sizeof("")-1),
1956+
HS_CXT, __FILE__, items, ax,
1957+
"v1.1337.0");
1958+
#else
1959+
Perl_xs_handshake(HS_KEYp(sizeof(struct PerlHandShakeInterpreter),
1960+
FALSE, NULL, FALSE,
1961+
sizeof("v1.1337.0")-1,
1962+
sizeof("")-1),
1963+
HS_CXT, __FILE__, items, ax,
1964+
"v1.1337.0");
1965+
#endif
1966+
}
1967+
else if(ix == 1) {
1968+
#ifdef MULTIPLICITY
1969+
Perl_xs_handshake(HS_KEYp(sizeof(PerlInterpreter)+1,
1970+
TRUE, NULL, FALSE,
1971+
sizeof("v" PERL_API_VERSION_STRING)-1,
1972+
sizeof("")-1),
1973+
HS_CXT, __FILE__, items, ax,
1974+
"v" PERL_API_VERSION_STRING);
1975+
#else
1976+
Perl_xs_handshake(HS_KEYp(sizeof(struct PerlHandShakeInterpreter)+1,
1977+
FALSE, NULL, FALSE,
1978+
sizeof("v" PERL_API_VERSION_STRING)-1,
1979+
sizeof("")-1),
1980+
HS_CXT, __FILE__, items, ax,
1981+
"v" PERL_API_VERSION_STRING);
1982+
#endif
1983+
}
1984+
else {
1985+
#ifdef MULTIPLICITY
1986+
Perl_xs_handshake(HS_KEYp(sizeof(PerlInterpreter)+1,
1987+
TRUE, NULL, FALSE,
1988+
sizeof("v1.1337.0")-1,
1989+
sizeof("")-1),
1990+
HS_CXT, __FILE__, items, ax,
1991+
"v1.1337.0");
1992+
#else
1993+
Perl_xs_handshake(HS_KEYp(sizeof(struct PerlHandShakeInterpreter)+1,
1994+
FALSE, NULL, FALSE,
1995+
sizeof("v1.1337.0")-1,
1996+
sizeof("")-1),
1997+
HS_CXT, __FILE__, items, ax,
1998+
"v1.1337.0");
1999+
#endif
2000+
}
2001+
2002+
19442003
MODULE = XS::APItest:Hash PACKAGE = XS::APItest::Hash
19452004

19462005
void

ext/XS-APItest/t/call.t

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use strict;
1111

1212
BEGIN {
1313
require '../../t/test.pl';
14-
plan(544);
14+
plan(547);
1515
use_ok('XS::APItest')
1616
};
1717
use Config;
@@ -385,3 +385,13 @@ eval { my @a = sort f 2, 1; $x++};
385385
print "x=$x\n";
386386
EOF
387387
}
388+
389+
fresh_perl_like('use XS::APItest;'
390+
.'XS::APItest::XSUB::test_mismatch_xs_handshake_api_ver("Dog");'
391+
, qr/\QPerl API version v1.1337.0 of Dog does not match\E/);
392+
fresh_perl_like('use XS::APItest;'
393+
.'XS::APItest::XSUB::test_mismatch_xs_handshake_bad_struct("Dog");'
394+
, qr/\Q loadable library and perl binaries are mismatched (got first handshake\E/);
395+
fresh_perl_like('use XS::APItest;'
396+
.'XS::APItest::XSUB::test_mismatch_xs_handshake_bad_struct_and_ver("Dog");'
397+
, qr/\QPerl API version v1.1337.0 of APItest.xs does not match\E/);

pod/perldiag.pod

+28-2
Original file line numberDiff line numberDiff line change
@@ -3637,7 +3637,9 @@ does when displayed.
36373637
(P) A dynamic loading library C<.so> or C<.dll> was being loaded into the
36383638
process that was built against a different build of perl than the
36393639
said library was compiled against. Reinstalling the XS module will
3640-
likely fix this error.
3640+
likely fix this error. This error is a less commonly seen subset of
3641+
L<Perl API version|perldiag/"Perl API version %s of %s does not match %s">
3642+
error.
36413643

36423644
=item Locale '%s' contains (at least) the following characters which
36433645
have unexpected meanings: %s The Perl program will use the expected
@@ -5216,7 +5218,31 @@ redirected it with select().)
52165218
=item Perl API version %s of %s does not match %s
52175219

52185220
(F) The XS module in question was compiled against a different incompatible
5219-
version of Perl than the one that has loaded the XS module.
5221+
version of Perl than the one that has loaded the XS module. If the internal
5222+
differences between the 2 incompatible Perl versions are large enough to
5223+
prevent obtaining the full module name causing this error message, a
5224+
C<.c> file name will be shown in this error message instead of the full module
5225+
name. The C<.c> file name serves as a hint, to help identify the module
5226+
causing this error.
5227+
5228+
The term XS module, does not mean, a C<.pm> file. This error is not directly
5229+
caused by Perl code inside a particular C<.pm> file or C<.pl> file.
5230+
Instead this error is only caused by OS and CPU specific, "shared library"
5231+
files created by a C or C++ compiler. This file format is called a
5232+
C<.so>, C<.dll>, C<.dylib>, C<.bundle> or C<.sl> on Perl's most popular
5233+
operating systems. These shared library files are a part of the XS API
5234+
documented in L<perlxs|perlxs>.
5235+
5236+
Each OS has a different file extension or no extension for shared libraries.
5237+
But shared library files on all OSes are non-text, unprintable, binary file
5238+
formats with raw machine code inside of them created by a C or C++ compiler.
5239+
5240+
The C<.so> or C<.dll> or equivalent is usually loaded by a sequence of a
5241+
C<.pm> or C<.pl> file making a call to L<DynaLoader|DynaLoader> or
5242+
L<XSLoader|XSLoader>. Which then calls OS specific mechanisms to load the
5243+
shared library file into the Perl process. The OS specific mechanisms
5244+
then calls a function or subroutine inside the particular C<.so> or C<.dll>
5245+
file. That particular C<.so> or C<.dll> file then throws this error message.
52205246

52215247
=item Perl folding rules are not up-to-date for 0x%X; please use the perlbug
52225248
utility to report; in regex; marked by S<<-- HERE> in m/%s/

util.c

+57-16
Original file line numberDiff line numberDiff line change
@@ -5546,6 +5546,7 @@ Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...)
55465546
void * got;
55475547
void * need;
55485548
const char *stage = "first";
5549+
bool in_abi_mismatch = FALSE;
55495550
#ifdef MULTIPLICITY
55505551
dTHX;
55515552
tTHX xs_interp;
@@ -5585,10 +5586,10 @@ Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...)
55855586
stage = "second";
55865587
if(UNLIKELY(got != need)) {
55875588
bad_handshake:/* recycle branch and string from above */
5588-
if(got != (void *)HSf_NOCHK)
5589-
noperl_die("%s: loadable library and perl binaries are mismatched"
5590-
" (got %s handshake key %p, needed %p)\n",
5591-
file, stage, got, need);
5589+
if(got != (void *)HSf_NOCHK) {
5590+
in_abi_mismatch = TRUE;
5591+
goto die_mismatched_rmv_c_args;
5592+
}
55925593
}
55935594

55945595
if(key & HSf_SETXSUBFN) { /* this might be called from a module bootstrap */
@@ -5600,31 +5601,71 @@ Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...)
56005601
(void)gv_fetchfile(file); */
56015602
}
56025603

5604+
die_mismatched_rmv_c_args:
56035605
if(key & HSf_POPMARK) {
5604-
ax = POPMARK;
5605-
{ SV **mark = PL_stack_base + ax++;
5606-
{ dSP;
5607-
items = (Stack_off_t)(SP - MARK);
5608-
}
5606+
/* Don't touch the local unthreaded or threaded Perl stack if mismatched
5607+
ABI. The pointers inside the mark stack vars and @_ vars are
5608+
are uninitialized data if we are executing in an unexpected second
5609+
libperl.{so,dll} with a different major version. The second libperl
5610+
possibly was auto-loaded by the OS, as a dependency of the out of
5611+
date XS shared library file. */
5612+
if(in_abi_mismatch) {
5613+
ax = Stack_off_t_MAX; /* silence CC & poison */
5614+
items = Stack_off_t_MAX;
5615+
}
5616+
else {
5617+
ax = POPMARK;
5618+
SV **mark = PL_stack_base + ax++;
5619+
dSP;
5620+
items = (Stack_off_t)(SP - MARK);
56095621
}
56105622
} else {
56115623
items = va_arg(args, Stack_off_t);
56125624
ax = va_arg(args, Stack_off_t);
56135625
}
5614-
assert(ax >= 0);
5615-
assert(items >= 0);
5626+
5627+
if(!in_abi_mismatch) {
5628+
assert(ax >= 0);
5629+
assert(items >= 0);
5630+
}
5631+
56165632
{
56175633
U32 apiverlen;
56185634
assert(HS_GETAPIVERLEN(key) <= UCHAR_MAX);
56195635
if((apiverlen = HS_GETAPIVERLEN(key))) {
56205636
char * api_p = va_arg(args, char*);
56215637
if(apiverlen != sizeof("v" PERL_API_VERSION_STRING)-1
56225638
|| memNE(api_p, "v" PERL_API_VERSION_STRING,
5623-
sizeof("v" PERL_API_VERSION_STRING)-1))
5624-
Perl_croak_nocontext("Perl API version %s of %" SVf " does not match %s",
5625-
api_p, SVfARG(PL_stack_base[ax + 0]),
5626-
"v" PERL_API_VERSION_STRING);
5627-
}
5639+
sizeof("v" PERL_API_VERSION_STRING)-1)) {
5640+
if(in_abi_mismatch)
5641+
noperl_die("Perl API version %s of %s does not match %s",
5642+
api_p, file, "v" PERL_API_VERSION_STRING);
5643+
else {/* use %s for SV * for string literal reuse with abv */
5644+
SV * package_sv = PL_stack_base[ax + 0];
5645+
Perl_croak_nocontext("Perl API version %s of %s does not match %s",
5646+
api_p, SvPV_nolen(package_sv),
5647+
"v" PERL_API_VERSION_STRING);
5648+
}
5649+
} /* memcmp() */
5650+
} /* if user wants API Ver Check (xsubpp default is on ) */
5651+
5652+
/* The gentler error above couldn't be shown. Maybe the 2 API ver strings DID
5653+
str eq match. So its a interp build time/Configure problem, or 3rd party patches
5654+
by OS vendors. Or system perl vs /home "local perl" battles.
5655+
No choice but to show the full hex debugging info and die.
5656+
5657+
On Unix, the 1st correct original libperl/perl.bin, on ELF, is irreverisbly
5658+
corrupted now. B/c new Perl API C func bodies have already been
5659+
linked/injected into the 1st perl.bin from the 2nd incompatible "surprise"
5660+
new libperl.so/.dll in the same proc.
5661+
5662+
A quick process exit using only libc APIs, no perl APIs, is only fool proof,
5663+
cross platform way to prevent a SEGV.
5664+
*/
5665+
if(in_abi_mismatch)
5666+
noperl_die("%s: loadable library and perl binaries are mismatched"
5667+
" (got %s handshake key %p, needed %p)\n",
5668+
file, stage, got, need);
56285669
}
56295670
{
56305671
U32 xsverlen = HS_GETXSVERLEN(key);

0 commit comments

Comments
 (0)