more useful options | |
PerlMonks |
[Solved] XS debugging "failed to extend arg stack"by NERDVANA (Deacon) |
on Jun 02, 2022 at 17:27 UTC ( [id://11144355]=perlquestion: print w/replies, xml ) | Need Help?? |
NERDVANA has asked for the wisdom of the Perl Monks concerning the following question:
Hi, my Tree::RB::XS module just recently started throwing an exception on new perls built with debugging enabled. It claims I wrote past the end of the stack (SP) during "$iter->next_kv". I've done a fair amount of debugging on it and just don't see the bug. I'm probably missing some magic XS macro... maybe a second pair of eyes can spot it?
next_kv is a method that returns one or more key+value pairs, as a list. So, the function could return up to 2x the number of nodes in the tree, or zero, or usually just one for most of the ways the function can be called. To make things more complicated, I decided to share the implementation of this method with the 'next_keys', 'next_values', and 'next' methods, which are all basically the same algorithm but return different things to the caller. I used perl's "ix" aliasing mechanism. Here is the XS code. The line of the test that fails is
Here is The C code it generates, with a warning added for every occurrence of EXTEND and XSRETURN and usage of ST(x) in the case where ix == 3 (which is what is failing). My understanding of XS is that in a xsub declared with PPCODE, the SP is pointed at the first argument, so the stack is at least as long as the number of arguments received. If you want to return more elements than the number of arguments passed to you, you need to call EXTEND(SP, n) where n is the number of return values. (in the code, I make the assumption that there was always one argument because it is a method that requires one, and so ST(0) should always be safe to use.) This is the relevant output of the unit test:
If you want to compile it, the source uses Dist::Zilla which I know offends some people. However, you can just grab it from CPAN because the current HEAD of the git repo is exactly what is published right now. Note that the assertion only fails on debug builds of perl. I was unable to reproduce the error until I realized that.
I used:
Back to
Seekers of Perl Wisdom
|
|