xref: /openbsd/gnu/usr.bin/perl/t/comp/hints.t (revision 73471bf0)
1#!./perl
2
3# Tests the scoping of $^H and %^H
4
5BEGIN {
6    @INC = qw(. ../lib ../ext/re);
7    chdir 't' if -d 't';
8}
9
10BEGIN { print "1..31\n"; }
11BEGIN {
12    print "not " if exists $^H{foo};
13    print "ok 1 - \$^H{foo} doesn't exist initially\n";
14    if (${^OPEN}) {
15	print "not " unless $^H & 0x00020000;
16	print "ok 2 - \$^H contains HINT_LOCALIZE_HH initially with ${^OPEN}\n";
17    } else {
18	print "not " if $^H & 0x00020000;
19	print "ok 2 - \$^H doesn't contain HINT_LOCALIZE_HH initially\n";
20    }
21}
22{
23    # simulate a pragma -- don't forget HINT_LOCALIZE_HH
24    BEGIN { $^H |= 0x04020000; $^H{foo} = "a"; }
25    BEGIN {
26	print "not " if $^H{foo} ne "a";
27	print "ok 3 - \$^H{foo} is now 'a'\n";
28	print "not " unless $^H & 0x00020000;
29	print "ok 4 - \$^H contains HINT_LOCALIZE_HH while compiling\n";
30    }
31    {
32	BEGIN { $^H |= 0x00020000; $^H{foo} = "b"; }
33	BEGIN {
34	    print "not " if $^H{foo} ne "b";
35	    print "ok 5 - \$^H{foo} is now 'b'\n";
36	}
37    }
38    BEGIN {
39	print "not " if $^H{foo} ne "a";
40	print "ok 6 - \$^H{foo} restored to 'a'\n";
41    }
42    # The pragma settings disappear after compilation
43    # (test at CHECK-time and at run-time)
44    CHECK {
45	print "not " if exists $^H{foo};
46	print "ok 9 - \$^H{foo} doesn't exist when compilation complete\n";
47	if (${^OPEN}) {
48	    print "not " unless $^H & 0x00020000;
49	    print "ok 10 - \$^H contains HINT_LOCALIZE_HH when compilation complete with ${^OPEN}\n";
50	} else {
51	    print "not " if $^H & 0x00020000;
52	    print "ok 10 - \$^H doesn't contain HINT_LOCALIZE_HH when compilation complete\n";
53	}
54    }
55    print "not " if exists $^H{foo};
56    print "ok 11 - \$^H{foo} doesn't exist at runtime\n";
57    if (${^OPEN}) {
58	print "not " unless $^H & 0x00020000;
59	print "ok 12 - \$^H contains HINT_LOCALIZE_HH at run-time with ${^OPEN}\n";
60    } else {
61	print "not " if $^H & 0x00020000;
62	print "ok 12 - \$^H doesn't contain HINT_LOCALIZE_HH at run-time\n";
63    }
64    # op_entereval should keep the pragmas it was compiled with
65    eval q*
66      BEGIN {
67	print "not " if $^H{foo} ne "a";
68	print "ok 13 - \$^H{foo} is 'a' at eval-\"\" time\n";
69	print "not " unless $^H & 0x00020000;
70	print "ok 14 - \$^H contains HINT_LOCALIZE_HH at eval\"\"-time\n";
71      }
72    *;
73}
74BEGIN {
75    print "not " if exists $^H{foo};
76    print "ok 7 - \$^H{foo} doesn't exist while finishing compilation\n";
77    if (${^OPEN}) {
78	print "not " unless $^H & 0x00020000;
79	print "ok 8 - \$^H contains HINT_LOCALIZE_HH while finishing compilation with ${^OPEN}\n";
80    } else {
81	print "not " if $^H & 0x00020000;
82	print "ok 8 - \$^H doesn't contain HINT_LOCALIZE_HH while finishing compilation\n";
83    }
84}
85
86{
87    BEGIN{$^H{x}=1};
88    for my $tno (15..16) {
89        eval q(
90            BEGIN {
91                print $^H{x}==1 && !$^H{y} ? "ok $tno\n" : "not ok $tno\n";
92            }
93            $^H{y} = 1;
94        );
95        if ($@) {
96            (my $str = $@)=~s/^/# /gm;
97            print "not ok $tno\n$str\n";
98        }
99    }
100}
101
102{
103    BEGIN { $^H |= 0x04000000; $^H{foo} = "z"; }
104
105    our($ri0, $rf0); BEGIN { $ri0 = $^H; $rf0 = $^H{foo}; }
106    print +($ri0 & 0x04000000 ? "" : "not "), "ok 17 - \$^H correct before require\n";
107    print +($rf0 eq "z" ? "" : "not "), "ok 18 - \$^H{foo} correct before require\n";
108
109    our($ra1, $ri1, $rf1, $rfe1);
110    BEGIN { require "comp/hints.aux"; }
111    print +(!($ri1 & 0x04000000) ? "" : "not "), "ok 19 - \$^H cleared for require\n";
112    print +(!defined($rf1) && !$rfe1 ? "" : "not "), "ok 20 - \$^H{foo} cleared for require\n";
113
114    our($ri2, $rf2); BEGIN { $ri2 = $^H; $rf2 = $^H{foo}; }
115    print +($ri2 & 0x04000000 ? "" : "not "), "ok 21 - \$^H correct after require\n";
116    print +($rf2 eq "z" ? "" : "not "), "ok 22 - \$^H{foo} correct after require\n";
117}
118
119# [perl #73174]
120
121{
122    my $res;
123    BEGIN { $^H{73174} = "foo" }
124    BEGIN { $res = ($^H{73174} // "") }
125    # /x{100}/i forces loading of utf8.pm, which used to reset %^H
126    eval '"" =~ /\x{100}/i; 1'
127	# Allow miniperl to fail this regexp compilation (effectively skip
128	# the test) in case tables have not been build, but require real
129	# perl to succeed.
130	or defined &DynaLoader::boot_DynaLoader and die;
131    BEGIN { $res .= '-' . ($^H{73174} // "")}
132    $res .= '-' . ($^H{73174} // "");
133    print $res eq "foo-foo-" ? "" : "not ",
134	"ok 23 - \$^H{foo} correct after /unicode/i (res=$res)\n";
135}
136
137# [perl #106282] Crash when tying %^H
138# Tying %^H should not result in a crash when the hint hash is cloned.
139# Hints should also be copied properly to inner scopes.  See also
140# [rt.cpan.org #73402].
141eval q`
142    # Do something naughty enough, and you get your module mentioned in the
143    # test suite. :-)
144    package namespace::clean::_TieHintHash;
145
146    sub TIEHASH  { bless[] }
147    sub STORE    { $_[0][0]{$_[1]} = $_[2] }
148    sub FETCH    { $_[0][0]{$_[1]} }
149    sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
150    sub NEXTKEY  { each %{$_[0][0]} }
151
152    package main;
153
154    BEGIN {
155	$^H{foo} = "bar"; # activate localisation magic
156	tie( %^H, 'namespace::clean::_TieHintHash' ); # sabotage %^H
157	$^H{foo} = "bar"; # create an element in the tied hash
158    }
159    { # clone the tied hint hash on scope entry
160	BEGIN {
161	    print "not " x ($^H{foo} ne 'bar'),
162		  "ok 24 - tied hint hash is copied to inner scope\n";
163	    %^H = ();
164	    tie( %^H, 'namespace::clean::_TieHintHash' );
165	    $^H{foo} = "bar";
166	}
167	{
168	    BEGIN{
169		print
170		  "not " x ($^H{foo} ne 'bar'),
171		  "ok 25 - tied empty hint hash is copied to inner scope\n"
172	    }
173	}
174	1;
175    }
176    1;
177` or warn $@;
178print "ok 26 - no crash when cloning a tied hint hash\n";
179
180{
181    my $w;
182    local $SIG{__WARN__} = sub { $w = shift };
183    eval q`
184	package namespace::clean::_TieHintHasi;
185
186	sub TIEHASH  { bless[] }
187	sub STORE    { $_[0][0]{$_[1]} = $_[2] }
188	sub FETCH    { $_[0][0]{$_[1]} }
189	sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
190      # Intentionally commented out:
191      #  sub NEXTKEY  { each %{$_[0][0]} }
192
193	package main;
194
195	BEGIN {
196    	    $^H{foo} = "bar"; # activate localisation magic
197    	    tie( %^H, 'namespace::clean::_TieHintHasi' ); # sabotage %^H
198    	    $^H{foo} = "bar"; # create an element in the tied hash
199	}
200	{ ; } # clone the tied hint hash
201    `;
202    print "not " if $w;
203    print "ok 27 - double-freeing explosive tied hints hash\n";
204    print "# got: $w" if $w;
205}
206
207# Setting ${^WARNING_HINTS} to its own value should not change things.
208{
209    my $w;
210    local $SIG{__WARN__} = sub { $w++ };
211    BEGIN {
212	# should have no effect:
213	my $x = ${^WARNING_BITS};
214	${^WARNING_BITS} = $x;
215    }
216    {
217	local $^W = 1;
218	() = 1 + undef;
219    }
220    print "# ", $w//'no', " warnings\nnot " unless $w == 1;
221    print "ok 28 - ",
222          "setting \${^WARNING_BITS} to its own value has no effect\n";
223}
224
225# [perl #112326]
226# this code could cause a crash, due to PL_hints continuing to point to th
227# hints hash currently being freed
228
229{
230    package Foo;
231    my @h = qw(a 1 b 2);
232    BEGIN {
233	$^H{FOO} = bless {};
234    }
235    sub DESTROY {
236	@h = %^H;
237	delete $INC{strict}; require strict; # boom!
238    }
239    my $h = join ':', %h;
240    # this isn't the main point of the test; the main point is that
241    # it doesn't crash!
242    print "not " if $h ne '';
243    print "ok 29 - #112326\n";
244}
245
246
247# [perl #112444]
248# A destructor called while %^H is freed should not be able to stop %^H
249# from being magical (due to *^H{HASH} being undef).
250{
251    BEGIN {
252	# Make sure %^H is clear and not localised, to begin with
253	%^H = ();
254	$^H = 0;
255    }
256    DESTROY { %^H }
257    {
258	{
259	    BEGIN {
260		$^H{foom} = bless[];
261	    }
262	} # scope exit triggers destructor, which autovivifies a non-
263	  # magical %^H
264	BEGIN {
265	    # Here we have the %^H created by DESTROY, which is
266	    # not localised
267	    $^H{112444} = 'baz';
268	}
269    } # %^H leaks on scope exit
270    BEGIN { @keez = keys %^H }
271}
272print "not " if @keez;
273print "ok 30 - %^H does not leak when autovivified in destructor\n";
274print "# keys are: @keez\n" if @keez;
275
276
277# Add new tests above this require, in case it fails.
278require './test.pl';
279
280# bug #27040: hints hash was being double-freed
281my $result = runperl(
282    prog => '$^H |= 0x20000; eval q{BEGIN { $^H |= 0x20000 }}',
283    stderr => 1
284);
285print "not " if length $result;
286print "ok 31 - double-freeing hints hash\n";
287print "# got: $result\n" if length $result;
288
289__END__
290# Add new tests above require 'test.pl'
291