Note: This method is designed for Linux systems where LD_PRELOAD is available.
AddressSanitizer (ASan) is a powerful tool for detecting memory corruption in C/C++ code. When developing Perl XS modules, you can integrate ASan support directly into your Makefile.PL to make debugging seamless.
Add the 'asan-on-linux' Option
First, use GetOptions to provide a dedicated flag for ASan. This allows users to enable memory sanitization explicitly. We also set up a temporary directory to store ASan's detailed log files.
use Getopt::Long; use File::Path 'mkpath';
GetOptions('asan-on-linux' => \my $asan_on_linux);
my @ccflags; my @ldflags; my $asan_logs_dir = ".tmp/asan_logs";
if ($asan_on_linux) { push @ccflags, "-fsanitize=address", "-fno-omit-frame-pointer"; push @ldflags, "-fsanitize=address"; mkpath $asan_logs_dir unless -d $asan_logs_dir; }
Automate Library Preloading via Makefile Macros
Since the standard perl binary isn't typically compiled with ASan, we must ensure libasan.so is correctly preloaded…