Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoLoader: goto considered harmful (especially with AutoLoaded XS) #9103

Open
p5pRT opened this issue Nov 7, 2007 · 4 comments
Open

AutoLoader: goto considered harmful (especially with AutoLoaded XS) #9103

p5pRT opened this issue Nov 7, 2007 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Nov 7, 2007

Migrated from rt.perl.org#47219 (status was 'open')

Searchable as RT47219$

@p5pRT
Copy link
Author

p5pRT commented Nov 7, 2007

From deforest@boulder.swri.edu

The AutoLoader documentation page recommends using "goto &$AUTOLOAD"
to enter the newly declared subroutine. This munges the stack if the
declaration code includes loading XS modules. In particular, if the
subroutine is defined via "bootstrap newmodule" (this most often
happens with declarations that use Inline to autocompile a new XS
module), then a stray '1' is left on the Perl stack by the
declaration step. By using 'goto', the autoload subroutine preserves
the 1, which is then returned as an extra value(!) when the newly
declared subroutine returns.

In scalar context, the '1' is dropped on the floor; in list context,
it is interpolated into the returned list and appears as the first
value in that list.

In this case, the simplest workaround is to use "return &$AUTOLOAD
(@​_)" rather than "goto &$AUTOLOAD". That costs an extra stack
frame, but is more bulletproof.

I have tested this bug in Perl 5.8.6 and 5.8.8 under MacOS X and
5.8.8 under Fedora Core 4.

The bug could be fixed by updating the AutoLoad man page to use the
safer construction, or by looking at the dynamic loader to identify
why it is leaving an extra 1 on the stack. The former is less likely
to stir up trouble in other parts of Perl.

@p5pRT
Copy link
Author

p5pRT commented Nov 15, 2007

From @smpeters

On Tue Nov 06 23​:28​:01 2007, zowie wrote​:

The AutoLoader documentation page recommends using "goto &$AUTOLOAD"
to enter the newly declared subroutine. This munges the stack if the
declaration code includes loading XS modules. In particular, if the
subroutine is defined via "bootstrap newmodule" (this most often
happens with declarations that use Inline to autocompile a new XS
module), then a stray '1' is left on the Perl stack by the
declaration step. By using 'goto', the autoload subroutine preserves
the 1, which is then returned as an extra value(!) when the newly
declared subroutine returns.

In scalar context, the '1' is dropped on the floor; in list context,
it is interpolated into the returned list and appears as the first
value in that list.

In this case, the simplest workaround is to use "return &$AUTOLOAD
(@​_)" rather than "goto &$AUTOLOAD". That costs an extra stack
frame, but is more bulletproof.

I have tested this bug in Perl 5.8.6 and 5.8.8 under MacOS X and
5.8.8 under Fedora Core 4.

The bug could be fixed by updating the AutoLoad man page to use the
safer construction, or by looking at the dynamic loader to identify
why it is leaving an extra 1 on the stack. The former is less likely
to stir up trouble in other parts of Perl.

Do you have some code that demonstrates the problem? Its much easier to
investigate and test the problem using existing code rather than attempt
to devise it ourselves.

Thanks,

Steve Peters

@p5pRT
Copy link
Author

p5pRT commented Nov 15, 2007

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Nov 16, 2007

From deforest@boulder.swri.edu

Hi, Steve,

Happy to supply my test case. It uses the PDL autoloader and Inline,
so you'll have to install those to tickle it.

The below code prints "1, Tickled!" for me with Perl 5.8.6 and 5.8.8
under MacOSX, with the current releases of Inline and PDL available
from CPAN.

Cheers,
Craig

----- write the following to 'tickle_bug.pdl'
sub tickle_bug {
  return tickler();
}

use Inline C=><<'EOF';

char *tickler() {
  return "Tickled!\n";
}
EOF
------ write the following to 'test.pl'
#!/usr/bin/perl
use PDL;
@​a = tickle_bug();
print join(", ",@​a),"\n";


On Nov 15, 2007, at 2​:27 PM, Steve Peters via RT wrote​:

On Tue Nov 06 23​:28​:01 2007, zowie wrote​:

The AutoLoader documentation page recommends using "goto &$AUTOLOAD"
to enter the newly declared subroutine. This munges the stack if the
declaration code includes loading XS modules. In particular, if the
subroutine is defined via "bootstrap newmodule" (this most often
happens with declarations that use Inline to autocompile a new XS
module), then a stray '1' is left on the Perl stack by the
declaration step. By using 'goto', the autoload subroutine preserves
the 1, which is then returned as an extra value(!) when the newly
declared subroutine returns.

In scalar context, the '1' is dropped on the floor; in list context,
it is interpolated into the returned list and appears as the first
value in that list.

In this case, the simplest workaround is to use "return &$AUTOLOAD
(@​_)" rather than "goto &$AUTOLOAD". That costs an extra stack
frame, but is more bulletproof.

I have tested this bug in Perl 5.8.6 and 5.8.8 under MacOS X and
5.8.8 under Fedora Core 4.

The bug could be fixed by updating the AutoLoad man page to use the
safer construction, or by looking at the dynamic loader to identify
why it is leaving an extra 1 on the stack. The former is less likely
to stir up trouble in other parts of Perl.

Do you have some code that demonstrates the problem? Its much
easier to
investigate and test the problem using existing code rather than
attempt
to devise it ourselves.

Thanks,

Steve Peters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants