This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #24940] "sub foo :unique" segfaults
[perl5.git] / ext / threads / t / problems.t
CommitLineData
da46a8d0
AB
1
2BEGIN {
3 chdir 't' if -d 't';
974ec8aa 4 push @INC, '../lib';
da46a8d0
AB
5 require Config; import Config;
6 unless ($Config{'useithreads'}) {
7 print "1..0 # Skip: no useithreads\n";
8 exit 0;
9 }
10}
11
997c206d 12use warnings;
da46a8d0
AB
13use strict;
14use threads;
15use threads::shared;
997c206d
DM
16
17# Note that we can't use Test::More here, as we would need to
18# call is() from within the DESTROY() function at global destruction time,
19# and parts of Test::* may have already been freed by then
20
371fce9b 21print "1..10\n";
997c206d
DM
22
23my $test : shared = 1;
24
25sub is($$$) {
26 my ($got, $want, $desc) = @_;
27 unless ($got eq $want) {
28 print "# EXPECTED: $want\n";
29 print "# GOT: got\n";
30 print "not ";
31 }
32 print "ok $test - $desc\n";
33 $test++;
34}
da46a8d0
AB
35
36
37#
38# This tests for too much destruction
39# which was caused by cloning stashes
40# on join which led to double the dataspace
41#
42#########################
43
44$|++;
da46a8d0
AB
45
46{
da46a8d0
AB
47 sub Foo::DESTROY {
48 my $self = shift;
49 my ($package, $file, $line) = caller;
997c206d
DM
50 is(threads->tid(),$self->{tid},
51 "In destroy[$self->{tid}] it should be correct too" )
da46a8d0
AB
52 }
53 my $foo;
54 $foo = bless {tid => 0}, 'Foo';
55 my $bar = threads->create(sub {
997c206d 56 is(threads->tid(),1, "And tid be 1 here");
da46a8d0
AB
57 $foo->{tid} = 1;
58 return $foo;
59 })->join();
60 $bar->{tid} = 0;
da46a8d0 61}
ad4404a3
EM
62
63#
64# This tests whether we can call Config::myconfig after threads have been
65# started (interpreter cloned). 5.8.1 and 5.8.2 contained a bug that would
66# disallow that too be done, because an attempt was made to change a variable
67# with the : unique attribute.
68#
69#########################
70
71threads->new( sub {1} )->join;
72my $not = eval { Config::myconfig() } ? '' : 'not ';
73print "${not}ok $test - Are we able to call Config::myconfig after clone\n";
74$test++;
75
53c33732
DM
76# bugid 24383 - :unique hashes weren't being made readonly on interpreter
77# clone; check that they are.
78
79our $unique_scalar : unique;
80our @unique_array : unique;
81our %unique_hash : unique;
82threads->new(
83 sub {
84 eval { $unique_scalar = 1 };
85 print $@ =~ /read-only/ ? '' : 'not ', "ok $test - unique_scalar\n";
86 $test++;
87 eval { $unique_array[0] = 1 };
88 print $@ =~ /read-only/ ? '' : 'not ', "ok $test - unique_array\n";
89 $test++;
90 eval { $unique_hash{abc} = 1 };
91 print $@ =~ /disallowed/ ? '' : 'not ', "ok $test - unique_hash\n";
92 $test++;
93 }
94)->join;
95
371fce9b
DM
96# bugid #24940 :unique should fail on my and sub declarations
97
98for my $decl ('my $x : unique', 'sub foo : unique') {
99 eval $decl;
100 print $@ =~
101 /^The 'unique' attribute may only be applied to 'our' variables/
102 ? '' : 'not ', "ok $test - $decl\n";
103 $test++;
104}
105
106
b23f1a86
DM
107# Returing a closure from a thread caused problems. If the last index in
108# the anon sub's pad wasn't for a lexical, then a core dump could occur.
109# Otherwise, there might be leaked scalars.
110
a6144651
DM
111# XXX DAPM 9-Jan-04 - backed this out for now - returning a closure from a
112# thread seems to crash win32
113
114# sub f {
115# my $x = "foo";
116# sub { $x."bar" };
117# }
118#
119# my $string = threads->new(\&f)->join->();
120# print $string eq 'foobar' ? '' : 'not ', "ok $test - returning closure\n";
121# $test++;
b23f1a86 122
da46a8d0 1231;