1#!./perl 2 3BEGIN { 4 chdir 't' if -d 't'; 5 require './test.pl'; 6 set_up_inc( qw(. ../lib) ); 7} 8 9use strict qw(refs subs); 10 11plan(254); 12 13# Test this first before we extend the stack with other operations. 14# This caused an asan failure due to a bad write past the end of the stack. 15eval { die 1..127, $_=\() }; 16 17# Test glob operations. 18 19$bar = "one"; 20$foo = "two"; 21{ 22 local(*foo) = *bar; 23 is($foo, 'one'); 24} 25is ($foo, 'two'); 26 27$baz = "three"; 28$foo = "four"; 29{ 30 local(*foo) = 'baz'; 31 is ($foo, 'three'); 32} 33is ($foo, 'four'); 34 35$foo = "global"; 36{ 37 local(*foo); 38 is ($foo, undef); 39 $foo = "local"; 40 is ($foo, 'local'); 41} 42is ($foo, 'global'); 43 44{ 45 no strict 'refs'; 46# Test fake references. 47 48 $baz = "valid"; 49 $bar = 'baz'; 50 $foo = 'bar'; 51 is ($$$foo, 'valid'); 52} 53 54# Test real references. 55 56$FOO = \$BAR; 57$BAR = \$BAZ; 58$BAZ = "hit"; 59is ($$$FOO, 'hit'); 60 61# Test references to real arrays. 62 63my $test = curr_test(); 64@ary = ($test,$test+1,$test+2,$test+3); 65$ref[0] = \@a; 66$ref[1] = \@b; 67$ref[2] = \@c; 68$ref[3] = \@d; 69for $i (3,1,2,0) { 70 push(@{$ref[$i]}, "ok $ary[$i]\n"); 71} 72print @a; 73print ${$ref[1]}[0]; 74print @{$ref[2]}[0]; 75{ 76 no strict 'refs'; 77 print @{'d'}; 78} 79curr_test($test+4); 80 81# Test references to references. 82 83$refref = \\$x; 84$x = "Good"; 85is ($$$refref, 'Good'); 86 87# Test nested anonymous arrays. 88 89$ref = [[],2,[3,4,5,]]; 90is (scalar @$ref, 3); 91is ($$ref[1], 2); 92is (${$$ref[2]}[2], 5); 93is (scalar @{$$ref[0]}, 0); 94 95is ($ref->[1], 2); 96is ($ref->[2]->[0], 3); 97 98# Test references to hashes of references. 99 100$refref = \%whatever; 101$refref->{"key"} = $ref; 102is ($refref->{"key"}->[2]->[0], 3); 103 104# Test to see if anonymous subarrays spring into existence. 105 106$spring[5]->[0] = 123; 107$spring[5]->[1] = 456; 108push(@{$spring[5]}, 789); 109is (join(':',@{$spring[5]}), "123:456:789"); 110 111# Test to see if anonymous subhashes spring into existence. 112 113@{$spring2{"foo"}} = (1,2,3); 114$spring2{"foo"}->[3] = 4; 115is (join(':',@{$spring2{"foo"}}), "1:2:3:4"); 116 117# Test references to subroutines. 118 119{ 120 my $called; 121 sub mysub { $called++; } 122 $subref = \&mysub; 123 &$subref; 124 is ($called, 1); 125} 126is ref eval {\&{""}}, "CODE", 'reference to &{""} [perl #94476]'; 127delete $My::{"Foo::"}; 128is ref \&My::Foo::foo, "CODE", 129 'creating stub with \&deleted_stash::foo [perl #128532]'; 130 131 132# Test references to return values of operators (TARGs/PADTMPs) 133{ 134 my @refs; 135 for("a", "b") { 136 push @refs, \"$_" 137 } 138 is join(" ", map $$_, @refs), "a b", 'refgen+PADTMP'; 139} 140 141$subrefref = \\&mysub2; 142is ($$subrefref->("GOOD"), "good"); 143sub mysub2 { lc shift } 144 145# Test REGEXP assignment 146 147SKIP: { 148 skip_if_miniperl("no dynamic loading on miniperl, so can't load re", 5); 149 require re; 150 my $x = qr/x/; 151 my $str = "$x"; # regex stringification may change 152 153 my $y = $$x; 154 is ($y, $str, "bare REGEXP stringifies correctly"); 155 ok (eval { "x" =~ $y }, "bare REGEXP matches correctly"); 156 157 my $z = \$y; 158 ok (re::is_regexp($z), "new ref to REXEXP passes is_regexp"); 159 is ($z, $str, "new ref to REGEXP stringifies correctly"); 160 ok (eval { "x" =~ $z }, "new ref to REGEXP matches correctly"); 161} 162{ 163 my ($x, $str); 164 { 165 my $y = qr/x/; 166 $str = "$y"; 167 $x = $$y; 168 } 169 is ($x, $str, "REGEXP keeps a ref to its mother_re"); 170 ok (eval { "x" =~ $x }, "REGEXP with mother_re still matches"); 171} 172 173# test dereferencing errors 174{ 175 format STDERR = 176. 177 my $ref; 178 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) { 179 eval q/ $$ref /; 180 like($@, qr/Not a SCALAR reference/, "Scalar dereference"); 181 eval q/ @$ref /; 182 like($@, qr/Not an ARRAY reference/, "Array dereference"); 183 eval q/ %$ref /; 184 like($@, qr/Not a HASH reference/, "Hash dereference"); 185 eval q/ &$ref /; 186 like($@, qr/Not a CODE reference/, "Code dereference"); 187 } 188 189 $ref = *STDERR{FORMAT}; 190 eval q/ *$ref /; 191 like($@, qr/Not a GLOB reference/, "Glob dereference"); 192 193 $ref = *STDOUT{IO}; 194 eval q/ *$ref /; 195 is($@, '', "Glob dereference of PVIO is acceptable"); 196 197 is($ref, *{$ref}{IO}, "IO slot of the temporary glob is set correctly"); 198} 199 200# Test the ref operator. 201 202sub PVBM () { 'foo' } 203{ my $dummy = index 'foo', PVBM } 204 205my $pviv = 1; "$pviv"; 206my $pvnv = 1.0; "$pvnv"; 207my $x; 208 209# we don't test 210# tied lvalue => SCALAR, as we haven't tested tie yet 211# BIND, 'cos we can't create them yet 212# REGEXP, 'cos that requires overload or Scalar::Util 213 214for ( 215 [ 'undef', SCALAR => \undef ], 216 [ 'constant IV', SCALAR => \1 ], 217 [ 'constant NV', SCALAR => \1.0 ], 218 [ 'constant PV', SCALAR => \'f' ], 219 [ 'scalar', SCALAR => \$x ], 220 [ 'PVIV', SCALAR => \$pviv ], 221 [ 'PVNV', SCALAR => \$pvnv ], 222 [ 'PVMG', SCALAR => \$0 ], 223 [ 'PVBM', SCALAR => \PVBM ], 224 [ 'scalar @array', SCALAR => \scalar @array ], 225 [ 'scalar %hash', SCALAR => \scalar %hash ], 226 [ 'vstring', VSTRING => \v1 ], 227 [ 'ref', REF => \\1 ], 228 [ 'substr lvalue', LVALUE => \substr($x, 0, 0) ], 229 [ 'pos lvalue', LVALUE => \pos ], 230 [ 'vec lvalue', LVALUE => \vec($x,0,1) ], 231 [ 'named array', ARRAY => \@ary ], 232 [ 'anon array', ARRAY => [ 1 ] ], 233 [ 'named hash', HASH => \%whatever ], 234 [ 'anon hash', HASH => { a => 1 } ], 235 [ 'named sub', CODE => \&mysub, ], 236 [ 'anon sub', CODE => sub { 1; } ], 237 [ 'glob', GLOB => \*foo ], 238 [ 'format', FORMAT => *STDERR{FORMAT} ], 239) { 240 my ($desc, $type, $ref) = @$_; 241 is (ref $ref, $type, "ref() for ref to $desc"); 242 like ("$ref", qr/^$type\(0x[0-9a-f]+\)$/, "stringify for ref to $desc"); 243} 244 245is (ref *STDOUT{IO}, 'IO::File', 'IO refs are blessed into IO::File'); 246like (*STDOUT{IO}, qr/^IO::File=IO\(0x[0-9a-f]+\)$/, 247 'stringify for IO refs'); 248 249{ # Test re-use of ref's TARG [perl #101738] 250 my $obj = bless [], '____'; 251 my $uniobj = bless [], chr 256; 252 my $get_ref = sub { ref shift }; 253 my $dummy = &$get_ref($uniobj); 254 $dummy = &$get_ref($obj); 255 ok exists { ____ => undef }->{$dummy}, 'ref sets UTF8 flag correctly'; 256} 257 258# Test anonymous hash syntax. 259 260$anonhash = {}; 261is (ref $anonhash, 'HASH'); 262$anonhash2 = {FOO => 'BAR', ABC => 'XYZ',}; 263is (join('', sort values %$anonhash2), 'BARXYZ'); 264 265# Test bless operator. 266 267package MYHASH; 268{ 269 no warnings qw(syntax deprecated); 270 $object = bless $main'anonhash2; 271} 272main::is (ref $object, 'MYHASH'); 273main::is ($object->{ABC}, 'XYZ'); 274 275$object2 = bless {}; 276main::is (ref $object2, 'MYHASH'); 277 278# Test ordinary call on object method. 279 280&mymethod($object,"argument"); 281 282sub mymethod { 283 local($THIS, @ARGS) = @_; 284 die 'Got a "' . ref($THIS). '" instead of a MYHASH' 285 unless ref $THIS eq 'MYHASH'; 286 main::is ($ARGS[0], "argument"); 287 main::is ($THIS->{FOO}, 'BAR'); 288} 289 290# Test automatic destructor call. 291 292$string = "bad"; 293$object = "foo"; 294$string = "good"; 295{ 296 no warnings qw(syntax deprecated); 297 $main'anonhash2 = "foo"; 298} 299$string = ""; 300 301DESTROY { 302 return unless $string; 303 main::is ($string, 'good'); 304 305 # Test that the object has not already been "cursed". 306 main::isnt (ref shift, 'HASH'); 307} 308 309# Now test inheritance of methods. 310 311package OBJ; 312 313@ISA = ('BASEOBJ'); 314 315{ 316 no warnings qw(syntax deprecated); 317 $main'object = bless {FOO => 'foo', BAR => 'bar'}; 318} 319 320package main; 321 322# Test arrow-style method invocation. 323 324is ($object->doit("BAR"), 'bar'); 325 326# Test indirect-object-style method invocation. 327 328$foo = doit $object "FOO"; 329main::is ($foo, 'foo'); 330 331{ 332 no warnings qw(syntax deprecated); 333 sub BASEOBJ'doit { 334 local $ref = shift; 335 die "Not an OBJ" unless ref $ref eq 'OBJ'; 336 $ref->{shift()}; 337 } 338} 339 340package UNIVERSAL; 341@ISA = 'LASTCHANCE'; 342 343package LASTCHANCE; 344sub foo { main::is ($_[1], 'works') } 345 346package WHATEVER; 347foo WHATEVER "works"; 348 349# 350# test the \(@foo) construct 351# 352package main; 353@foo = \(1..3); 354@bar = \(@foo); 355@baz = \(1,@foo,@bar); 356is (scalar (@bar), 3); 357is (scalar grep(ref($_), @bar), 3); 358is (scalar (@baz), 3); 359 360my(@fuu) = \(1..2,3); 361my(@baa) = \(@fuu); 362my(@bzz) = \(1,@fuu,@baa); 363is (scalar (@baa), 3); 364is (scalar grep(ref($_), @baa), 3); 365is (scalar (@bzz), 3); 366 367# also, it can't be an lvalue 368# (That’s what *you* think! --sprout) 369eval '\\($x, $y) = (1, 2);'; 370like ($@, qr/Can\'t modify.*ref.*in.*assignment(?x: 371 )|Experimental aliasing via reference not enabled/); 372 373# test for proper destruction of lexical objects 374$test = curr_test(); 375sub larry::DESTROY { print "# larry\nok $test\n"; } 376sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; } 377sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; } 378 379{ 380 my ($joe, @curly, %larry); 381 my $moe = bless \$joe, 'moe'; 382 my $curly = bless \@curly, 'curly'; 383 my $larry = bless \%larry, 'larry'; 384 print "# leaving block\n"; 385} 386 387print "# left block\n"; 388curr_test($test + 3); 389 390# another glob test 391 392 393$foo = "garbage"; 394{ local(*bar) = "foo" } 395$bar = "glob 3"; 396local(*bar) = *bar; 397is ($bar, "glob 3"); 398 399$var = "glob 4"; 400$_ = \$var; 401is ($$_, 'glob 4'); 402 403 404# test if reblessing during destruction results in more destruction 405$test = curr_test(); 406{ 407 package A; 408 sub new { bless {}, shift } 409 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" } 410 package _B; 411 sub new { bless {}, shift } 412 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' } 413 package main; 414 my $b = _B->new; 415} 416curr_test($test + 2); 417 418# test if $_[0] is properly protected in DESTROY() 419 420{ 421 my $test = curr_test(); 422 my $i = 0; 423 local $SIG{'__DIE__'} = sub { 424 my $m = shift; 425 if ($i++ > 4) { 426 print "# infinite recursion, bailing\nnot ok $test\n"; 427 exit 1; 428 } 429 like ($m, qr/^Modification of a read-only/); 430 }; 431 package C; 432 sub new { bless {}, shift } 433 DESTROY { $_[0] = 'foo' } 434 { 435 print "# should generate an error...\n"; 436 my $c = C->new; 437 } 438 print "# good, didn't recurse\n"; 439} 440 441# test that DESTROY is called on all objects during global destruction, 442# even those without hard references [perl #36347] 443 444is( 445 runperl( 446 stderr => 1, prog => 'sub DESTROY { print qq-aaa\n- } bless \$a[0]' 447 ), 448 "aaa\n", 'DESTROY called on array elem' 449); 450is( 451 runperl( 452 stderr => 1, 453 prog => '{ bless \my@x; *a=sub{@x}}sub DESTROY { print qq-aaa\n- }' 454 ), 455 "aaa\n", 456 'DESTROY called on closure variable' 457); 458 459# But cursing objects must not result in double frees 460# This caused "Attempt to free unreferenced scalar" in 5.16. 461fresh_perl_is( 462 'bless \%foo::, bar::; bless \%bar::, foo::; print "ok\n"', "ok\n", 463 { stderr => 1 }, 464 'no double free when stashes are blessed into each other'); 465 466 467# test if refgen behaves with autoviv magic 468{ 469 my @a; 470 $a[1] = "good"; 471 my $got; 472 for (@a) { 473 $got .= ${\$_}; 474 $got .= ';'; 475 } 476 is ($got, ";good;"); 477} 478 479# This test is the reason for postponed destruction in sv_unref 480$a = [1,2,3]; 481$a = $a->[1]; 482is ($a, 2); 483 484# This test used to coredump. The BEGIN block is important as it causes the 485# op that created the constant reference to be freed. Hence the only 486# reference to the constant string "pass" is in $a. The hack that made 487# sure $a = $a->[1] would work didn't work with references to constants. 488 489 490foreach my $lexical ('', 'my $a; ') { 491 my $expect = "pass\n"; 492 my $result = runperl (switches => ['-wl'], stderr => 1, 493 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a'); 494 495 is ($?, 0); 496 is ($result, $expect); 497} 498 499$test = curr_test(); 500sub x::DESTROY {print "ok ", $test + shift->[0], "\n"} 501{ my $a1 = bless [3],"x"; 502 my $a2 = bless [2],"x"; 503 { my $a3 = bless [1],"x"; 504 my $a4 = bless [0],"x"; 505 567; 506 } 507} 508curr_test($test+4); 509 510is (runperl (switches=>['-l'], 511 prog=> 'print 1; print qq-*$\*-;print 1;'), 512 "1\n*\n*\n1\n"); 513 514# bug #21347 515 516runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' ); 517is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//'); 518 519runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1); 520is ($?, 0, 'warn called inside UNIVERSAL::DESTROY'); 521 522 523# bug #22719 524 525runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();'); 526is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)'); 527 528# bug #27268: freeing self-referential typeglobs could trigger 529# "Attempt to free unreferenced scalar" warnings 530 531is (runperl( 532 prog => 'use Symbol;my $x=bless \gensym,q{t}; print;*$$x=$x', 533 stderr => 1 534), '', 'freeing self-referential typeglob'); 535 536# using a regex in the destructor for STDOUT segfaulted because the 537# REGEX pad had already been freed (ithreads build only). The 538# object is required to trigger the early freeing of GV refs to STDOUT 539 540TODO: { 541 local $TODO = "works but output through pipe is mangled" if $^O eq 'VMS'; 542 like (runperl( 543 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_=q{bad};s/bad/ok/;print}', 544 stderr => 1 545 ), qr/^(ok)+$/, 'STDOUT destructor'); 546} 547 548{ 549 no strict 'refs'; 550 $name8 = chr 163; 551 $name_utf8 = $name8 . chr 256; 552 chop $name_utf8; 553 554 is ($$name8, undef, 'Nothing before we start'); 555 is ($$name_utf8, undef, 'Nothing before we start'); 556 $$name8 = "Pound"; 557 is ($$name8, "Pound", 'Accessing via 8 bit symref works'); 558 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works'); 559} 560 561{ 562 no strict 'refs'; 563 $name_utf8 = $name = chr 9787; 564 utf8::encode $name_utf8; 565 566 is (length $name, 1, "Name is 1 char"); 567 is (length $name_utf8, 3, "UTF8 representation is 3 chars"); 568 569 is ($$name, undef, 'Nothing before we start'); 570 is ($$name_utf8, undef, 'Nothing before we start'); 571 $$name = "Face"; 572 is ($$name, "Face", 'Accessing via Unicode symref works'); 573 is ($$name_utf8, undef, 574 'Accessing via the UTF8 byte sequence gives nothing'); 575} 576 577{ 578 no strict 'refs'; 579 $name1 = "\0Chalk"; 580 $name2 = "\0Cheese"; 581 582 isnt ($name1, $name2, "They differ"); 583 584 is ($$name1, undef, 'Nothing before we start (scalars)'); 585 is ($$name2, undef, 'Nothing before we start'); 586 $$name1 = "Yummy"; 587 is ($$name1, "Yummy", 'Accessing via the correct name works'); 588 is ($$name2, undef, 589 'Accessing via a different NUL-containing name gives nothing'); 590 # defined uses a different code path 591 ok (defined $$name1, 'defined via the correct name works'); 592 ok (!defined $$name2, 593 'defined via a different NUL-containing name gives nothing'); 594 595 is ($name1->[0], undef, 'Nothing before we start (arrays)'); 596 is ($name2->[0], undef, 'Nothing before we start'); 597 $name1->[0] = "Yummy"; 598 is ($name1->[0], "Yummy", 'Accessing via the correct name works'); 599 is ($name2->[0], undef, 600 'Accessing via a different NUL-containing name gives nothing'); 601 ok (defined $name1->[0], 'defined via the correct name works'); 602 ok (!defined$name2->[0], 603 'defined via a different NUL-containing name gives nothing'); 604 605 my (undef, $one) = @{$name1}[2,3]; 606 my (undef, $two) = @{$name2}[2,3]; 607 is ($one, undef, 'Nothing before we start (array slices)'); 608 is ($two, undef, 'Nothing before we start'); 609 @{$name1}[2,3] = ("Very", "Yummy"); 610 (undef, $one) = @{$name1}[2,3]; 611 (undef, $two) = @{$name2}[2,3]; 612 is ($one, "Yummy", 'Accessing via the correct name works'); 613 is ($two, undef, 614 'Accessing via a different NUL-containing name gives nothing'); 615 ok (defined $one, 'defined via the correct name works'); 616 ok (!defined $two, 617 'defined via a different NUL-containing name gives nothing'); 618 619 is ($name1->{PWOF}, undef, 'Nothing before we start (hashes)'); 620 is ($name2->{PWOF}, undef, 'Nothing before we start'); 621 $name1->{PWOF} = "Yummy"; 622 is ($name1->{PWOF}, "Yummy", 'Accessing via the correct name works'); 623 is ($name2->{PWOF}, undef, 624 'Accessing via a different NUL-containing name gives nothing'); 625 ok (defined $name1->{PWOF}, 'defined via the correct name works'); 626 ok (!defined $name2->{PWOF}, 627 'defined via a different NUL-containing name gives nothing'); 628 629 my (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'}; 630 my (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'}; 631 is ($one, undef, 'Nothing before we start (hash slices)'); 632 is ($two, undef, 'Nothing before we start'); 633 @{$name1}{'SNIF', 'BEEYOOP'} = ("Very", "Yummy"); 634 (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'}; 635 (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'}; 636 is ($one, "Yummy", 'Accessing via the correct name works'); 637 is ($two, undef, 638 'Accessing via a different NUL-containing name gives nothing'); 639 ok (defined $one, 'defined via the correct name works'); 640 ok (!defined $two, 641 'defined via a different NUL-containing name gives nothing'); 642 643 $name1 = "Left"; $name2 = "Left\0Right"; 644 my $glob2 = *{$name2}; 645 646 is ($glob1, undef, "We get different typeglobs. In fact, undef"); 647 648 *{$name1} = sub {"One"}; 649 *{$name2} = sub {"Two"}; 650 651 is (&{$name1}, "One"); 652 is (&{$name2}, "Two"); 653} 654 655# test derefs after list slice 656 657is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' ); 658is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' ); 659is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' ); 660is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' ); 661is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' ); 662is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' ); 663 664# deref on empty list shouldn't autovivify 665{ 666 local $@; 667 eval { ()[0]{foo} }; 668 like ( "$@", qr/Can't use an undefined value as a HASH reference/, 669 "deref of undef from list slice fails" ); 670} 671 672# these will segfault if they fail 673 674my $pvbm = PVBM; 675my $rpvbm = \$pvbm; 676 677ok (!eval { *$rpvbm }, 'PVBM ref is not a GLOB ref'); 678ok (!eval { *$pvbm }, 'PVBM is not a GLOB ref'); 679ok (!eval { $$pvbm }, 'PVBM is not a SCALAR ref'); 680ok (!eval { @$pvbm }, 'PVBM is not an ARRAY ref'); 681ok (!eval { %$pvbm }, 'PVBM is not a HASH ref'); 682ok (!eval { $pvbm->() }, 'PVBM is not a CODE ref'); 683ok (!eval { $rpvbm->foo }, 'PVBM is not an object'); 684 685# bug 24254 686is( runperl(stderr => 1, prog => 'map eval qq(exit),1 for 1'), ""); 687is( runperl(stderr => 1, prog => 'eval { for (1) { map { die } 2 } };'), ""); 688is( runperl(stderr => 1, prog => 'for (125) { map { exit } (213)}'), ""); 689my $hushed = $^O eq 'VMS' ? 'use vmsish qw(hushed);' : ''; 690is( runperl(stderr => 1, prog => $hushed . 'map die,4 for 3'), "Died at -e line 1.\n"); 691is( runperl(stderr => 1, prog => $hushed . 'grep die,4 for 3'), "Died at -e line 1.\n"); 692is( runperl(stderr => 1, prog => $hushed . 'for $a (3) {@b=sort {die} 4,5}'), "Died at -e line 1.\n"); 693 694# bug 57564 695is( runperl(stderr => 1, prog => 'my $i;for $i (1) { for $i (2) { } }'), ""); 696 697# The mechanism for freeing objects in globs used to leave dangling 698# pointers to freed SVs. To test this, we construct this nested structure: 699# GV => blessed(AV) => RV => GV => blessed(SV) 700# all with a refcnt of 1, and hope that the second GV gets processed first 701# by do_clean_named_objs. Then when the first GV is processed, it mustn't 702# find anything nasty left by the previous GV processing. 703# The eval is stop things in the main body of the code holding a reference 704# to a GV, and the print at the end seems to bee necessary to ensure 705# the correct freeing order of *x and *y (no, I don't know why - DAPM). 706 707is (runperl( 708 prog => 'eval q[bless \@y; bless \$x; $y[0] = \*x; $z = \*y; ]; ' 709 . 'delete $::{x}; delete $::{y}; print qq{ok\n};', 710 stderr => 1), 711 "ok\n", 'freeing freed glob in global destruction'); 712 713 714# Test undefined hash references as arguments to %{} in boolean context 715# [perl #81750] 716{ 717 no strict 'refs'; 718 eval { my $foo; %$foo; }; ok !$@, '%$undef'; 719 eval { my $foo; scalar %$foo; }; ok !$@, 'scalar %$undef'; 720 eval { my $foo; !%$foo; }; ok !$@, '!%$undef'; 721 eval { my $foo; if ( %$foo) {} }; ok !$@, 'if ( %$undef) {}'; 722 eval { my $foo; if (!%$foo) {} }; ok !$@, 'if (!%$undef) {}'; 723 eval { my $foo; unless ( %$foo) {} }; ok !$@, 'unless ( %$undef) {}'; 724 eval { my $foo; unless (!%$foo) {} }; ok !$@, 'unless (!%$undef) {}'; 725 eval { my $foo; 1 if %$foo; }; ok !$@, '1 if %$undef'; 726 eval { my $foo; 1 if !%$foo; }; ok !$@, '1 if !%$undef'; 727 eval { my $foo; 1 unless %$foo; }; ok !$@, '1 unless %$undef;'; 728 eval { my $foo; 1 unless ! %$foo; }; ok !$@, '1 unless ! %$undef'; 729 eval { my $foo; %$foo ? 1 : 0; }; ok !$@, ' %$undef ? 1 : 0'; 730 eval { my $foo; !%$foo ? 1 : 0; }; ok !$@, '!%$undef ? 1 : 0'; 731} 732 733# RT #88330 734# Make sure that a leaked thinggy with multiple weak references to 735# it doesn't trigger a panic with multiple rounds of global cleanup 736# (Perl_sv_clean_all). 737 738{ 739 local $ENV{PERL_DESTRUCT_LEVEL} = 2; 740 741 # we do all permutations of array/hash, 1ref/2ref, to account 742 # for the different way backref magic is stored 743 744 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 1 weak ref'); 745no warnings 'experimental::builtin'; 746use builtin qw(weaken); 747my $r = []; 748Internals::SvREFCNT(@$r, 9); 749my $r1 = $r; 750weaken($r1); 751print "ok"; 752EOF 753 754 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 2 weak refs'); 755no warnings 'experimental::builtin'; 756use builtin qw(weaken); 757my $r = []; 758Internals::SvREFCNT(@$r, 9); 759my $r1 = $r; 760weaken($r1); 761my $r2 = $r; 762weaken($r2); 763print "ok"; 764EOF 765 766 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 1 weak ref'); 767no warnings 'experimental::builtin'; 768use builtin qw(weaken); 769my $r = {}; 770Internals::SvREFCNT(%$r, 9); 771my $r1 = $r; 772weaken($r1); 773print "ok"; 774EOF 775 776 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 2 weak refs'); 777no warnings 'experimental::builtin'; 778use builtin qw(weaken); 779my $r = {}; 780Internals::SvREFCNT(%$r, 9); 781my $r1 = $r; 782weaken($r1); 783my $r2 = $r; 784weaken($r2); 785print "ok"; 786EOF 787 788} 789 790{ 791 my $error; 792 *hassgropper::DESTROY = sub { 793 no warnings 'experimental::builtin'; 794 use builtin qw(weaken); 795 eval { weaken($_[0]) }; 796 $error = $@; 797 # This line caused a crash before weaken refused to weaken a 798 # read-only reference: 799 $do::not::overwrite::this = $_[0]; 800 }; 801 my $xs = bless [], "hassgropper"; 802 undef $xs; 803 like $error, qr/^Modification of a read-only/, 804 'weaken refuses to weaken a read-only ref'; 805 # Now that the test has passed, avoid sabotaging global destruction: 806 undef *hassgropper::DESTROY; 807 undef $do::not::overwrite::this; 808} 809 810 811is ref( bless {}, "nul\0clean" ), "nul\0clean", "ref() is nul-clean"; 812 813# Test constants and references thereto. 814for (3) { 815 eval { $_ = 4 }; 816 like $@, qr/^Modification of a read-only/, 817 'assignment to value aliased to literal number'; 818 eval { ${\$_} = 4 }; 819 like $@, qr/^Modification of a read-only/, 820 'refgen does not allow assignment to value aliased to literal number'; 821} 822for ("4eounthouonth") { 823 eval { $_ = 4 }; 824 like $@, qr/^Modification of a read-only/, 825 'assignment to value aliased to literal string'; 826 eval { ${\$_} = 4 }; 827 like $@, qr/^Modification of a read-only/, 828 'refgen does not allow assignment to value aliased to literal string'; 829} 830{ 831 my $aref = \123; 832 is \$$aref, $aref, 833 '[perl #109746] referential identity of \literal under threads+mad' 834} 835 836# ref in boolean context 837{ 838 my $false = 0; 839 my $true = 1; 840 my $plain = []; 841 my $obj = bless {}, "Foo"; 842 my $objnull = bless [], ""; 843 my $obj0 = bless [], "0"; 844 my $obj00 = bless [], "00"; 845 my $obj1 = bless [], "1"; 846 847 is !ref $false, 1, '!ref $false'; 848 is !ref $true, 1, '!ref $true'; 849 is !ref $plain, "", '!ref $plain'; 850 is !ref $obj, "", '!ref $obj'; 851 is !ref $objnull, "", '!ref $objnull'; 852 is !ref $obj0 , 1, '!ref $obj0'; 853 is !ref $obj00, "", '!ref $obj00'; 854 is !ref $obj1, "", '!ref $obj1'; 855 856 is ref $obj || 0, "Foo", 'ref $obj || 0'; 857 is ref $obj // 0, "Foo", 'ref $obj // 0'; 858 is $true && ref $obj, "Foo", '$true && ref $obj'; 859 is ref $obj ? "true" : "false", "true", 'ref $obj ? "true" : "false"'; 860 861 my $r = 2; 862 if (ref $obj) { $r = 1 }; 863 is $r, 1, 'if (ref $obj)'; 864 865 $r = 2; 866 if (ref $obj0) { $r = 1 }; 867 is $r, 2, 'if (ref $obj0)'; 868 869 $r = 2; 870 if (ref $obj) { $r = 1 } else { $r = 0 }; 871 is $r, 1, 'if (ref $obj) else'; 872 873 $r = 2; 874 if (ref $obj0) { $r = 1 } else { $r = 0 }; 875 is $r, 0, 'if (ref $obj0) else'; 876} 877 878{ 879 # RT #78288 880 # if an op returns &PL_sv_zero rather than newSViv(0), the 881 # value should be mutable. So ref (via the PADTMP flag) should 882 # make a mutable copy 883 884 my @a = (); 885 my $r = \ scalar grep $_ == 1, @a; 886 $$r += 10; 887 is $$r, 10, "RT #78288 - mutable PL_sv_zero copy"; 888} 889 890 891# RT#130861: heap-use-after-free in pp_rv2sv, from asan fuzzing 892SKIP: { 893 skip_if_miniperl("no dynamic loading on miniperl, so can't load arybase", 1); 894 # this value is critical - its just enough so that the stack gets 895 # grown which loading/calling arybase 896 my $n = 125; 897 898 my $code = <<'EOF'; 899$ary = '['; 900my @a = map $$ary, 1..NNN; 901print "@a\n"; 902EOF 903 $code =~ s/NNN/$n/g; 904 my @exp = ("0") x $n; 905 fresh_perl_is($code, "@exp", { stderr => 1 }, 906 'rt#130861: heap uaf in pp_rv2sv'); 907} 908 909# Bit of a hack to make test.pl happy. There are 3 more tests after it leaves. 910$test = curr_test(); 911curr_test($test + 3); 912# test global destruction 913 914my $test1 = $test + 1; 915my $test2 = $test + 2; 916 917package FINALE; 918 919{ 920 $ref3 = bless ["ok $test2\n"]; # package destruction 921 my $ref2 = bless ["ok $test1\n"]; # lexical destruction 922 local $ref1 = bless ["ok $test\n"]; # dynamic destruction 923 1; # flush any temp values on stack 924} 925 926DESTROY { 927 print $_[0][0]; 928} 929 930