Skip to content

Commit 14148aa

Browse files
committed
RC_STACK asserts without DEBUG_LEAKING_SCALARS
An earlier commit added some asserts to the old-style and new-style stack push/pop functions such as PUSHs() or rpp_popfree_1(). These assert that the stack is reference-counted or not as appropriate. These asserts were enabled only if perl was built with all of DEBUGGING && PERL_RC_STACK && DEBUG_LEAKING_SCALARS. This commit makes the asserts available under just DEBUGGING && PERL_RC_STACK Initially I was worried about the performance implications (you expect DEBUGGING builds to be slow, but not *too* slow), but they don't seem too bad. So by making them more likely to be enabled, they're more likely to help people spot code that needs fixing (e.g. code still doing POPs when the stack is reference counted and the function hasn't been wrapped).
1 parent e96dd01 commit 14148aa

File tree

3 files changed

+8
-27
lines changed

3 files changed

+8
-27
lines changed

inline.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,7 @@ Perl_rpp_popfree_to(pTHX_ SV **sp)
439439

440440
assert(sp <= PL_stack_sp);
441441
#ifdef PERL_RC_STACK
442-
# ifdef DEBUG_LEAKING_SCALARS
443442
assert(rpp_stack_is_rc());
444-
# endif
445443
while (PL_stack_sp > sp) {
446444
SV *sv = *PL_stack_sp--;
447445
SvREFCNT_dec(sv);
@@ -466,9 +464,7 @@ Perl_rpp_popfree_1(pTHX)
466464
PERL_ARGS_ASSERT_RPP_POPFREE_1;
467465

468466
#ifdef PERL_RC_STACK
469-
# ifdef DEBUG_LEAKING_SCALARS
470467
assert(rpp_stack_is_rc());
471-
# endif
472468
SV *sv = *PL_stack_sp--;
473469
SvREFCNT_dec(sv);
474470
#else
@@ -493,9 +489,7 @@ Perl_rpp_popfree_2(pTHX)
493489
PERL_ARGS_ASSERT_RPP_POPFREE_2;
494490

495491
#ifdef PERL_RC_STACK
496-
# ifdef DEBUG_LEAKING_SCALARS
497492
assert(rpp_stack_is_rc());
498-
# endif
499493
for (int i = 0; i < 2; i++) {
500494
SV *sv = *PL_stack_sp--;
501495
SvREFCNT_dec(sv);
@@ -534,9 +528,7 @@ Perl_rpp_pop_1_norc(pTHX)
534528
#ifndef PERL_RC_STACK
535529
SvREFCNT_inc(sv);
536530
#else
537-
# ifdef DEBUG_LEAKING_SCALARS
538531
assert(rpp_stack_is_rc());
539-
# endif
540532
#endif
541533
return sv;
542534
}
@@ -563,9 +555,7 @@ Perl_rpp_push_1(pTHX_ SV *sv)
563555

564556
*++PL_stack_sp = sv;
565557
#ifdef PERL_RC_STACK
566-
# ifdef DEBUG_LEAKING_SCALARS
567558
assert(rpp_stack_is_rc());
568-
# endif
569559
SvREFCNT_inc_simple_void_NN(sv);
570560
#endif
571561
}
@@ -578,9 +568,7 @@ Perl_rpp_push_2(pTHX_ SV *sv1, SV *sv2)
578568
*++PL_stack_sp = sv1;
579569
*++PL_stack_sp = sv2;
580570
#ifdef PERL_RC_STACK
581-
# ifdef DEBUG_LEAKING_SCALARS
582571
assert(rpp_stack_is_rc());
583-
# endif
584572
SvREFCNT_inc_simple_void_NN(sv1);
585573
SvREFCNT_inc_simple_void_NN(sv2);
586574
#endif
@@ -624,9 +612,7 @@ Perl_rpp_push_1_norc(pTHX_ SV *sv)
624612

625613
*++PL_stack_sp = sv;
626614
#ifdef PERL_RC_STACK
627-
# ifdef DEBUG_LEAKING_SCALARS
628615
assert(rpp_stack_is_rc());
629-
# endif
630616
#else
631617
sv_2mortal(sv);
632618
#endif
@@ -649,9 +635,7 @@ Perl_rpp_replace_1_1(pTHX_ SV *sv)
649635
PERL_ARGS_ASSERT_RPP_REPLACE_1_1;
650636

651637
#ifdef PERL_RC_STACK
652-
# ifdef DEBUG_LEAKING_SCALARS
653638
assert(rpp_stack_is_rc());
654-
# endif
655639
SV *oldsv = *PL_stack_sp;
656640
*PL_stack_sp = sv;
657641
SvREFCNT_inc_simple_void_NN(sv);
@@ -678,9 +662,7 @@ Perl_rpp_replace_2_1(pTHX_ SV *sv)
678662
PERL_ARGS_ASSERT_RPP_REPLACE_2_1;
679663

680664
#ifdef PERL_RC_STACK
681-
# ifdef DEBUG_LEAKING_SCALARS
682665
assert(rpp_stack_is_rc());
683-
# endif
684666
/* replace PL_stack_sp[-1] first; leave PL_stack_sp[0] in place while
685667
* we free [-1], so if an exception occurs, [0] will still be freed.
686668
*/
@@ -767,14 +749,14 @@ context.
767749
PERL_STATIC_INLINE bool
768750
Perl_rpp_is_lone(pTHX_ SV *sv)
769751
{
770-
#if defined(PERL_RC_STACK) && defined(DEBUG_LEAKING_SCALARS)
752+
#ifdef PERL_RC_STACK
771753
/* note that rpp_is_lone() can be used in wrapped pp functions,
772754
* where technically the stack is no longer ref-counted; but because
773755
* the args are non-RC copies of RC args further down the stack, we
774756
* can't be in a *completely* non-ref stack.
775757
*/
776758
assert(AvREAL(PL_curstack));
777-
# endif
759+
#endif
778760

779761
return SvREFCNT(sv) <= cBOOL(SvTEMP(sv))
780762
#ifdef PERL_RC_STACK

pod/perlguts.pod

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4477,8 +4477,7 @@ API.
44774477
A reference-counted perl can be built using the PERL_RC_STACK define.
44784478
For development and debugging purposes, it is best to enable leaking
44794479
scalar debugging too, as that displays extra information about scalars
4480-
that have leaked or been prematurely freed, and it also enables extra
4481-
assertions in the macros and functions which manipulate the stack:
4480+
that have leaked or been prematurely freed.
44824481

44834482
Configure -DDEBUGGING \
44844483
-Accflags='-DPERL_RC_STACK -DDEBUG_LEAKING_SCALARS'
@@ -4896,9 +4895,9 @@ call_sv() is called from a standard PP function (rpp_stack_is_rc() is
48964895
true) or from a wrapped PP or XS function (rpp_stack_is_rc() is false).
48974896
Note that you're unlikely to need to use this function, as in most places,
48984897
such as PP or XS functions, it is always RC or non-RC respectively. In
4899-
fact, under C<DEBUG_LEAKING_SCALARS>, PUSHs() and similar macros include
4900-
an C<assert(!rpp_stack_is_rc())>, while rpp_push_1() and similar functions
4901-
have C<assert(rpp_stack_is_rc())>.
4898+
fact on debugging builds under C<PERL_RC_STACK>, PUSHs() and similar
4899+
macros include an C<assert(!rpp_stack_is_rc())>, while rpp_push_1() and
4900+
similar functions have C<assert(rpp_stack_is_rc())>.
49024901

49034902
The macros for pushing new stackinfos have been replaced with inline
49044903
functions which don't rely on C<dSP> being in scope, and which have less

pp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Pops an unsigned long off the stack.
210210
#define RETURNOP(o) return (PUTBACK, o)
211211
#define RETURNX(x) return (x, PUTBACK, NORMAL)
212212

213-
#if defined(PERL_RC_STACK) && defined(DEBUG_LEAKING_SCALARS)
213+
#ifdef PERL_RC_STACK
214214
# define POPs (assert(!rpp_stack_is_rc()), *sp--)
215215
#else
216216
# define POPs (*sp--)
@@ -543,7 +543,7 @@ Does not use C<TARG>. See also C<L</XPUSHu>>, C<L</mPUSHu>> and C<L</PUSHu>>.
543543
sv_setnv_mg(targ, TARGn_nv); \
544544
} STMT_END
545545

546-
#if defined(PERL_RC_STACK) && defined(DEBUG_LEAKING_SCALARS)
546+
#ifdef PERL_RC_STACK
547547
# define PUSHs(s) (assert(!rpp_stack_is_rc()), *++sp = (s))
548548
#else
549549
# define PUSHs(s) (*++sp = (s))

0 commit comments

Comments
 (0)