This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
panic if a freed op is called
authorDavid Mitchell <davem@iabyn.com>
Wed, 4 Oct 2017 10:29:40 +0000 (11:29 +0100)
committerDavid Mitchell <davem@iabyn.com>
Tue, 31 Oct 2017 15:31:26 +0000 (15:31 +0000)
On debugging builds, when freeing an op, set its op_ppaddr to point to
a function which dies. That way if a freed op accidentally remains in the
execution path, you'll know immediately, rather than crashing sometime
later with a stack underflow or whatever.

op.c

diff --git a/op.c b/op.c
index bcf1cba..c88a8f8 100644 (file)
--- a/op.c
+++ b/op.c
@@ -419,6 +419,15 @@ Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
 #    define PerlMemShared PerlMem
 #endif
 
+/* make freed ops die if they're inadvertently executed */
+#ifdef DEBUGGING
+static OP *
+S_pp_freed(pTHX)
+{
+    DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op);
+}
+#endif
+
 void
 Perl_Slab_Free(pTHX_ void *op)
 {
@@ -427,6 +436,10 @@ Perl_Slab_Free(pTHX_ void *op)
 
     PERL_ARGS_ASSERT_SLAB_FREE;
 
+#ifdef DEBUGGING
+    o->op_ppaddr = S_pp_freed;
+#endif
+
     if (!o->op_slabbed) {
         if (!o->op_static)
            PerlMemShared_free(op);