Skip to content

Commit defb77b

Browse files
committed
threads::shared: alloc arenas with correct context
RT #131124 In a couple of places in shared.xs, it calls sv_newmortal() with a perl context different from that currently set by PERL_SET_CONTEXT(). If sv_newmortal() happens to trigger the malloc of a new SV HEAD arena, then under PERL_TRACK_MEMPOOL, this will cause panics when the arena is freed or realloced.
1 parent b28683c commit defb77b

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

dist/threads-shared/lib/threads/shared.pm

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use warnings;
77

88
use Scalar::Util qw(reftype refaddr blessed);
99

10-
our $VERSION = '1.55'; # Please update the pod, too.
10+
our $VERSION = '1.56'; # Please update the pod, too.
1111
my $XS_VERSION = $VERSION;
1212
$VERSION = eval $VERSION;
1313

@@ -195,7 +195,7 @@ threads::shared - Perl extension for sharing data structures between threads
195195
196196
=head1 VERSION
197197
198-
This document describes threads::shared version 1.55
198+
This document describes threads::shared version 1.56
199199
200200
=head1 SYNOPSIS
201201

dist/threads-shared/shared.xs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,9 @@ sharedsv_array_mg_CLEAR(pTHX_ SV *sv, MAGIC *mg)
11041104
if (!sv) continue;
11051105
if ( (SvOBJECT(sv) || (SvROK(sv) && (sv = SvRV(sv))))
11061106
&& SvREFCNT(sv) == 1 ) {
1107-
SV *tmp = Perl_sv_newmortal(caller_perl);
1107+
SV *tmp;
11081108
PERL_SET_CONTEXT((aTHX = caller_perl));
1109+
tmp = sv_newmortal();
11091110
sv_upgrade(tmp, SVt_RV);
11101111
get_RV(tmp, sv);
11111112
PERL_SET_CONTEXT((aTHX = PL_sharedsv_space));
@@ -1384,8 +1385,9 @@ STORESIZE(SV *obj,IV count)
13841385
if ( (SvOBJECT(sv) || (SvROK(sv) && (sv = SvRV(sv))))
13851386
&& SvREFCNT(sv) == 1 )
13861387
{
1387-
SV *tmp = Perl_sv_newmortal(caller_perl);
1388+
SV *tmp;
13881389
PERL_SET_CONTEXT((aTHX = caller_perl));
1390+
tmp = sv_newmortal();
13891391
sv_upgrade(tmp, SVt_RV);
13901392
get_RV(tmp, sv);
13911393
PERL_SET_CONTEXT((aTHX = PL_sharedsv_space));

dist/threads-shared/t/object2.t

+23-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ExtUtils::testlib;
1717

1818
BEGIN {
1919
$| = 1;
20-
print("1..131\n"); ### Number of tests that will be run ###
20+
print("1..133\n"); ### Number of tests that will be run ###
2121
};
2222

2323
use threads;
@@ -445,6 +445,28 @@ ok($destroyed[$ID], 'Scalar object removed from shared scalar');
445445
::ok($count == $n, "remove array object by undef");
446446
}
447447

448+
# RT #131124
449+
# Emptying a shared array creates new temp SVs. If there are no spare
450+
# SVs, a new arena is allocated. shared.xs was mallocing a new arena
451+
# with the wrong perl context set, meaning that when the arena was later
452+
# freed, it would "panic: realloc from wrong pool"
453+
#
454+
455+
{
456+
threads->new(sub {
457+
my @a :shared;
458+
push @a, bless &threads::shared::share({}) for 1..1000;
459+
undef @a; # this creates lots of temp SVs
460+
})->join;
461+
ok(1, "#131124 undef array doesnt panic");
462+
463+
threads->new(sub {
464+
my @a :shared;
465+
push @a, bless &threads::shared::share({}) for 1..1000;
466+
@a = (); # this creates lots of temp SVs
467+
})->join;
468+
ok(1, "#131124 clear array doesnt panic");
469+
}
448470

449471

450472
# EOF

0 commit comments

Comments
 (0)