Skip to content

Commit a742fa0

Browse files
committed
allow building with high-water mark to be independent of -DDEBUGGING
This allows a debugging perl to be built with the high water mark checks disabled, or a non-debugging perl to be built with the high water marks enabled. This should allow Debian, the reporter for #16607 to build both their normal perl and debugperl with the same state of high water mark checks and avoid the mismatch between a debugperl and non-debug dynamic extension. Fixes #16607
1 parent a33729f commit a742fa0

File tree

12 files changed

+65
-16
lines changed

12 files changed

+65
-16
lines changed

cop.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ MSVC_DIAG_RESTORE
4444

4545
typedef struct jmpenv JMPENV;
4646

47-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
47+
#if defined PERL_USE_HWM
4848
# define JE_OLD_STACK_HWM_zero PL_start_env.je_old_stack_hwm = 0
4949
# define JE_OLD_STACK_HWM_save(je) \
5050
(je).je_old_stack_hwm = PL_curstackinfo->si_stack_hwm
@@ -1271,7 +1271,7 @@ struct stackinfo {
12711271
I32 si_stack_nonrc_base;
12721272
#endif
12731273

1274-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
1274+
#ifdef PERL_USE_HWM
12751275
/* high water mark: for checking if the stack was correctly extended /
12761276
* tested for extension by each pp function */
12771277
SSize_t si_stack_hwm;
@@ -1298,7 +1298,7 @@ typedef struct stackinfo PERL_SI;
12981298
# define SET_MARK_OFFSET NOOP
12991299
#endif
13001300

1301-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
1301+
#ifdef PERL_USE_HWM
13021302
# define PUSHSTACK_INIT_HWM(si) ((si)->si_stack_hwm = 0)
13031303
#else
13041304
# define PUSHSTACK_INIT_HWM(si) NOOP

dump.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,7 +2808,7 @@ Perl_hv_dump(pTHX_ HV *hv)
28082808
int
28092809
Perl_runops_debug(pTHX)
28102810
{
2811-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
2811+
#ifdef PERL_USE_HWM
28122812
SSize_t orig_stack_hwm = PL_curstackinfo->si_stack_hwm;
28132813

28142814
PL_curstackinfo->si_stack_hwm = PL_stack_sp - PL_stack_base;
@@ -2829,7 +2829,7 @@ Perl_runops_debug(pTHX)
28292829
#ifdef PERL_TRACE_OPS
28302830
++PL_op_exec_cnt[PL_op->op_type];
28312831
#endif
2832-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
2832+
#ifdef PERL_USE_HWM
28332833
if (PL_curstackinfo->si_stack_hwm < PL_stack_sp - PL_stack_base)
28342834
Perl_croak_nocontext(
28352835
"panic: previous op failed to extend arg stack: "
@@ -2867,7 +2867,7 @@ Perl_runops_debug(pTHX)
28672867
DEBUG_l(Perl_deb(aTHX_ "leaving RUNOPS level\n"));
28682868
PERL_ASYNC_CHECK();
28692869

2870-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
2870+
#ifdef PERL_USE_HWM
28712871
if (PL_curstackinfo->si_stack_hwm < orig_stack_hwm)
28722872
PL_curstackinfo->si_stack_hwm = orig_stack_hwm;
28732873
#endif

ext/XS-APItest/APItest.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use strict;
44
use warnings;
55
use Carp;
66

7-
our $VERSION = '1.35';
7+
our $VERSION = '1.36';
88

99
require XSLoader;
1010

ext/XS-APItest/APItest.xs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,12 @@ destruct_test(pTHX_ void *p) {
16011601
warn("In destruct_test: %" SVf "\n", (SV*)p);
16021602
}
16031603

1604+
#ifdef PERL_USE_HWM
1605+
# define hwm_checks_enabled() true
1606+
#else
1607+
# define hwm_checks_enabled() false
1608+
#endif
1609+
16041610
MODULE = XS::APItest PACKAGE = XS::APItest
16051611

16061612
INCLUDE: const-xs.inc
@@ -2670,6 +2676,17 @@ PPCODE:
26702676
*PL_stack_max = NULL;
26712677

26722678

2679+
void
2680+
bad_EXTEND()
2681+
PPCODE:
2682+
/* testing failure to extend the stack, do not extend the stack */
2683+
PUSHs(&PL_sv_yes);
2684+
PUSHs(&PL_sv_no);
2685+
XSRETURN(2);
2686+
2687+
bool
2688+
hwm_checks_enabled()
2689+
26732690
void
26742691
call_sv_C()
26752692
PREINIT:

ext/XS-APItest/t/extend.t

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use Test::More;
1515
use Config;
16-
use XS::APItest qw(test_EXTEND);
16+
use XS::APItest qw(test_EXTEND hwm_checks_enabled bad_EXTEND);
1717

18-
plan tests => 48;
18+
plan tests => 50;
1919

2020
my $uvsize = $Config::Config{uvsize}; # sizeof(UV)
2121
my $sizesize = $Config::Config{sizesize}; # sizeof(Size_t)
@@ -66,3 +66,15 @@ for my $offset (-1, 0, 1) {
6666
}
6767
}
6868
}
69+
70+
SKIP:
71+
{
72+
# we've extended the stack a fair bit above so the actual bad_EXTEND*() should
73+
# be safe in terms of UB *here*
74+
skip "HWM checks not enabled", 2
75+
unless hwm_checks_enabled();
76+
77+
ok(!eval { bad_EXTEND(); 1 }, "bad_EXTEND() should throw");
78+
like($@, qr/^panic: XSUB XS::APItest::bad_EXTEND \(APItest\.c\) failed to extend arg stack/,
79+
"check panic message");
80+
}

op.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5146,7 +5146,7 @@ S_gen_constant_list(pTHX_ OP *o)
51465146

51475147
switch (ret) {
51485148
case 0:
5149-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
5149+
#ifdef PERL_USE_HWM
51505150
PL_curstackinfo->si_stack_hwm = 0; /* stop valgrind complaining */
51515151
#endif
51525152
Perl_pp_pushmark(aTHX);

perl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4514,7 +4514,7 @@ Perl_init_stacks(pTHX)
45144514
REASONABLE(8192/sizeof(PERL_CONTEXT) - 1),
45154515
make_real);
45164516
PL_curstackinfo->si_type = PERLSI_MAIN;
4517-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
4517+
#ifdef PERL_USE_HWM
45184518
PL_curstackinfo->si_stack_hwm = 0;
45194519
#endif
45204520
PL_curstack = PL_curstackinfo->si_stack;

perl.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,26 @@ violations are fatal.
10871087
*/
10881088
#define PERL_USE_SAFE_PUTENV
10891089

1090+
/* Control whether we set and test the stack high water mark.
1091+
*
1092+
* When enabled this checks that pp funcs and XSUBs properly EXTEND()
1093+
* the stack.
1094+
*
1095+
* Debugging builds have HWM checks on by default, you can add
1096+
* -DPERL_NO_HWM to ccflags to prevent those checks, or add
1097+
* -DPERL_USE_HWM to ccflags to perform HWM checks even on
1098+
* non-debugging builds.
1099+
*/
1100+
1101+
#if defined PERL_NO_HWM
1102+
# undef PERL_USE_HWM
1103+
#elif defined PERL_USE_HWM
1104+
/* nothing to do here */
1105+
#elif defined DEBUGGING && !defined DEBUGGING_RE_ONLY
1106+
# define PERL_USE_HWM
1107+
#endif
1108+
1109+
10901110
/* HP-UX 10.X CMA (Common Multithreaded Architecture) insists that
10911111
pthread.h must be included before all other header files.
10921112
*/
@@ -5204,7 +5224,7 @@ typedef Sighandler_t Sigsave_t;
52045224
#define SCAN_TR 1
52055225
#define SCAN_REPL 2
52065226

5207-
#ifdef DEBUGGING
5227+
#if defined DEBUGGING || defined PERL_USE_HWM
52085228
# ifndef register
52095229
# define register
52105230
# endif

pp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ Does not use C<TARG>. See also C<L</XPUSHu>>, C<L</mPUSHu>> and C<L</PUSHu>>.
387387
/* EXTEND_HWM_SET: note the high-water-mark to which the stack has been
388388
* requested to be extended (which is likely to be less than PL_stack_max)
389389
*/
390-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
390+
#ifdef PERL_USE_HWM
391391
# define EXTEND_HWM_SET(p, n) \
392392
STMT_START { \
393393
SSize_t extend_hwm_set_ix = (p) - PL_stack_base + (n); \

pp_hot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6494,7 +6494,7 @@ PP(pp_entersub)
64946494

64956495
rpp_invoke_xs(cv);
64966496

6497-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
6497+
#ifdef PERL_USE_HWM
64986498
/* This duplicates the check done in runops_debug(), but provides more
64996499
* information in the common case of the fault being with an XSUB.
65006500
*

scope.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
5656
Perl_croak(aTHX_ "Out of memory during stack extend");
5757

5858
av_extend(PL_curstack, current + n + extra);
59-
#ifdef DEBUGGING
59+
#ifdef PERL_USE_HWM
6060
PL_curstackinfo->si_stack_hwm = current + n + extra;
6161
#endif
6262

sv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15159,7 +15159,7 @@ Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
1515915159
#ifdef PERL_RC_STACK
1516015160
nsi->si_stack_nonrc_base = si->si_stack_nonrc_base;
1516115161
#endif
15162-
#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
15162+
#ifdef PERL_USE_HWM
1516315163
nsi->si_stack_hwm = 0;
1516415164
#endif
1516515165

0 commit comments

Comments
 (0)