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
is( [ $tree->iter(1)->next_kv(2) ], [ 1, 2, 2, 4 ], 'next_kv' );
which means "starting from element key==1, return the next two key+value pairs, which in this unit test happen to be { 1 => 2, 2 => 4 }

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:

EXTEND(SP, 4 at t/21-iter.t line 167. Accessing ST(0), ST(1) at t/21-iter.t line 167. Accessing ST(2), ST(3) at t/21-iter.t line 167. XSRETURN(4) at t/21-iter.t line 167. not ok 6 - iter_get_multi { ok 1 - next_keys(*) ok 2 - next_keys overshoot ok 3 - reverse next_keys overshoot ok 4 - reverse next_keys for 1 ok 5 - iterate nothing ok 6 - next_values(4) ok 7 - next_nodes(2) } # Failed test 'iter_get_multi' # at t/21-iter.t line 172. # Caught exception in subtest: panic: XSUB Tree::RB::XS::Iter::next_kv + (TreeRBXS.c) failed to extend arg stack: base=56076974fa88, sp=56076 +974faa8, hwm=56076974faa0

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:
perlbrew install -j 10 --as p536-debug --noman --debug --thread perl-5.36.0


In reply to [Solved] XS debugging "failed to extend arg stack" by NERDVANA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.