Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: XS Error: Segfault with B::HooksAtRuntime

by dave_the_m (Monsignor)
on Aug 07, 2022 at 12:52 UTC ( [id://11145995] : note . print w/replies, xml ) Need Help??

in reply to Re: XS Error: Segfault with B::HooksAtRuntime
in thread XS Error: Segfault with B::HooksAtRuntime

I've now fully diagnosed the issue and have a small reproducible test case. The issue is the context stack being reallocated when deeply nests sub calls are made from after_runtime(), which is called from a destructor while exiting the eval scope from the require. That exiting code isn't expecting the context stack to be reallocated from under it. Normally when perl itself calls out to code from things like destructors, tied method calls etc, it uses a new temporary set of stacks (argument, context etc). after_runtime() needs to do something similar. This is achieved via the PUSHSTACKi() macro. Look at a distribution like Async-Interrupt for an example.

Here's the reproducible code. The recursive sub is called enough times to trigger a context stack grow/realloc.

------------------------- use lib '.'; use Foo; ------------------------- package Foo; use Bar; 1; ------------------------- package Bar; use B::Hooks::AtRuntime 'after_runtime'; sub recurse { my $depth = shift; return if $depth < 0; recurse($depth -1); } sub import { after_runtime { recurse(20); } }
If this is run against a perl that has been built with -DDEBUGGING, you'll see the following assertion failure, which is Perl_cx_popeval() detecting that the current cx pointer has changed underneath it.
perl: inline.h:2921: Perl_cx_popeval: Assertion `CxTYPE(cx) == CXt_EVA +L' failed.


Replies are listed 'Best First'.
Re^3: XS Error: Segfault with B::HooksAtRuntime
by Ovid (Cardinal) on Aug 08, 2022 at 07:04 UTC