From ef7a02def4468ac7301c120ece586caea7351c4d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 1 Feb 2021 05:34:12 +0000 Subject: [PATCH 2/2] carp: set G_ERR in environ before accessing @DB::args In case accessing @DB::args causes a segfault and core dump; one can open the core dump with gdb(1) run "p environ[$IDX]" to figure out what message and sub_name is tickling Perl5's stack-not-refcounted behavior and causing a segfault. ($IDX is typically the last environment variable index in the environ(7) array, but it could be another number) --- dist/Carp/lib/Carp.pm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dist/Carp/lib/Carp.pm b/dist/Carp/lib/Carp.pm index 109b7fe..c87e373 100644 --- a/dist/Carp/lib/Carp.pm +++ b/dist/Carp/lib/Carp.pm @@ -302,6 +302,8 @@ BEGIN { } } +our $g_err; + sub caller_info { my $i = shift(@_) + 1; my %call_info; @@ -327,6 +329,7 @@ sub caller_info { my $sub_name = Carp::get_subname( \%call_info ); if ( $call_info{has_args} ) { +$ENV{G_ERR} = "$sub_name $g_err"; # Guard our serialization of the stack from stack refcounting bugs # NOTE this is NOT a complete solution, we cannot 100% guard against # these bugs. However in many cases Perl *is* capable of detecting @@ -594,6 +597,7 @@ sub ret_backtrace { $tid_msg = " thread $tid" if $tid; } +$g_err = $err; my %i = caller_info($i); $mess = "$err at $i{file} line $i{line}$tid_msg"; if( $. ) { @@ -635,6 +639,7 @@ sub ret_summary { my $tid = threads->tid; $tid_msg = " thread $tid" if $tid; } +$g_err = $err; my %i = caller_info($i); return "$err at $i{file} line $i{line}$tid_msg\.\n";