Module:
Description
The default typemap lib/ExtUtils/typemap ships with perl and provides a default set of translations from SVs to the types requested by the XS code.
The T_PTRREF INPUT entry, for example:
T_PTRREF
if (SvROK($arg)) {
IV tmp = SvIV((SV*)SvRV($arg));
$var = INT2PTR($type,tmp);
}
else
Perl_croak_nocontext("%s: %s is not a reference",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq["$pname"]},
"$var")
doesn't perform any get magic before checking the ROK flag. If the supplied SV is tied, and SV has not previously been fetched or has changed since last fetched, the ROK flag may no longer be valid.
For the common case where a method is called on a T_PTRREF implemented object, method resolution will call get magic before the XSUB is called, so
I only demonstrate the issue in T_PTRREF here, other INPUT maps have similar problems, T_PTROBJ, T_REFREF seem to, but I haven't looked closely.
Steps to Reproduce
Needs to be run from a built source tree, since XS::Typemap isn't installed. I expect you could see similar results from many CPAN XS modules:
tony@venus:.../git/perl6$ cat ../typemap-magic.pl
use v5.40;
use XS::Typemap qw(T_PTRREF_IN T_PTRREF_OUT);
my $x;
tie my $y, "T";
my $i = 10;
my $p = T_PTRREF_OUT($i);
$x = 0;
my $ignored = $y; # prime $y
$x = $p;
$ignored = $y if @ARGV; # fetch
say T_PTRREF_IN($y);
package T {
sub TIESCALAR ($class) { bless {}, $class }
sub FETCH ($obj) { $x }
}
tony@venus:.../git/perl6$ ./perl -Ilib ../typemap-magic.pl
XS::Typemap::T_PTRREF_IN: ptr is not a reference at ../typemap-magic.pl line 15.
tony@venus:.../git/perl6$ ./perl -Ilib ../typemap-magic.pl FETCH
10
If you supply an argument like the second invocation, the magic is invoked by the assignment and T_PTRREF_IN() sees the correct value.
Expected behavior
The code completes successfully and prints 10 without the pre-fetch.
Perl configuration
# perl -V output goes here
Summary of my perl5 (revision 5 version 45 subversion 1) configuration:
Commit id: f26eeb6cb5fed44b69135f7df58db3f187927356
Platform:
osname=linux
osvers=6.12.85+deb13-amd64
archname=x86_64-linux
uname='linux venus 6.12.85+deb13-amd64 #1 smp preempt_dynamic debian 6.12.85-1 (2026-04-30) x86_64 gnulinux '
config_args='-des -Dusedevel'
hint=recommended
useposix=true
d_sigaction=define
useithreads=undef
usemultiplicity=undef
use64bitint=define
use64bitall=define
uselongdouble=undef
usemymalloc=n
default_inc_excludes_dot=define
Compiler:
cc='cc'
ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2'
optimize='-O2'
cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
ccversion=''
gccversion='14.2.0'
gccosandvers=''
intsize=4
longsize=8
ptrsize=8
doublesize=8
byteorder=12345678
doublekind=3
d_longlong=define
longlongsize=8
d_longdbl=define
longdblsize=16
longdblkind=3
ivtype='long'
ivsize=8
nvtype='double'
nvsize=8
Off_t='off_t'
lseeksize=8
alignbytes=8
prototype=define
Linker and Libraries:
ld='cc'
ldflags =' -fstack-protector-strong -L/usr/local/lib'
libpth=/usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib64
libs=-lpthread -lgdbm -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
perllibs=-lpthread -ldl -lm -lcrypt -lutil -lc
libc=/lib/x86_64-linux-gnu/libc.so.6
so=so
useshrplib=false
libperl=libperl.a
gnulibc_version='2.41'
Dynamic Linking:
dlsrc=dl_dlopen.xs
dlext=so
d_dlsymun=undef
ccdlflags='-Wl,-E'
cccdlflags='-fPIC'
lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector-strong'
Characteristics of this binary (from libperl):
Compile-time options:
HAS_LONG_DOUBLE
HAS_STRTOLD
HAS_TIMES
PERLIO_LAYERS
PERL_COPY_ON_WRITE
PERL_HASH_FUNC_SIPHASH13
PERL_HASH_USE_SBOX32
PERL_MALLOC_WRAP
PERL_OP_PARENT
PERL_PRESERVE_IVUV
PERL_USE_DEVEL
PERL_USE_SAFE_PUTENV
USE_64_BIT_ALL
USE_64_BIT_INT
USE_LARGE_FILES
USE_LOCALE
USE_LOCALE_COLLATE
USE_LOCALE_CTYPE
USE_LOCALE_NUMERIC
USE_LOCALE_TIME
USE_PERLIO
USE_PERL_ATOF
Built under linux
Compiled at Jul 22 2026 10:52:15
%ENV:
PERLBREW_BASHRC_VERSION="0.43"
PERLBREW_HOME="/home/tony/.perlbrew"
PERLBREW_MANPATH=""
PERLBREW_PATH="/home/tony/perl5/perlbrew/bin"
PERLBREW_ROOT="/home/tony/perl5/perlbrew"
PERLBREW_VERSION="0.67"
@INC:
lib
/usr/local/lib/perl5/site_perl/5.45.1/x86_64-linux
/usr/local/lib/perl5/site_perl/5.45.1
/usr/local/lib/perl5/5.45.1/x86_64-linux
/usr/local/lib/perl5/5.45.1
Edit: fix a mis-edit left from composition.
Module:
Description
The default typemap
lib/ExtUtils/typemapships with perl and provides a default set of translations from SVs to the types requested by the XS code.The
T_PTRREFINPUT entry, for example:doesn't perform any get magic before checking the ROK flag. If the supplied SV is tied, and SV has not previously been fetched or has changed since last fetched, the ROK flag may no longer be valid.
For the common case where a method is called on a
T_PTRREFimplemented object, method resolution will call get magic before the XSUB is called, soI only demonstrate the issue in
T_PTRREFhere, other INPUT maps have similar problems,T_PTROBJ,T_REFREFseem to, but I haven't looked closely.Steps to Reproduce
Needs to be run from a built source tree, since XS::Typemap isn't installed. I expect you could see similar results from many CPAN XS modules:
If you supply an argument like the second invocation, the magic is invoked by the assignment and T_PTRREF_IN() sees the correct value.
Expected behavior
The code completes successfully and prints
10without the pre-fetch.Perl configuration
Edit: fix a mis-edit left from composition.