Testing: Add --enable-asan configure option to compile with AddressSanitizer (#6404)

This commit is contained in:
Geza Lore 2025-09-09 08:55:49 +01:00 committed by GitHub
parent c6ffd22c45
commit 056c3ee331
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 79 additions and 5 deletions

View File

@ -203,10 +203,23 @@ sub aslr_off {
sub ulimit_stack_unlimited {
return "" if !$opt_unlimited_stack;
system("ulimit -s unlimited 2>/dev/null");
my $limit = "unlimited";
# AddressSanitizer doesn't work with 'ulimit -s unlimted'
if (`${\(verilator_bin())} --get-supported ASAN` eq "1\n") {
# Use host 'physical memory / #cores / 8' instead
open(my $fh, "<", "/proc/meminfo") || die "Can't read host memory for asan";
while (<$fh>) {
if (m/MemTotal:\s+(\d+)\s+kB/) {
$limit = int(int($1)/`nproc`/8);
last;
}
}
close($fh);
}
system("ulimit -s $limit 2>/dev/null");
my $status = $?;
if ($status == 0) {
return "ulimit -s unlimited 2>/dev/null; exec ";
return "ulimit -s $limit 2>/dev/null; exec ";
} else {
return "";
}

View File

@ -57,6 +57,22 @@ AC_ARG_ENABLE([partial-static],
CFG_ENABLE_PARTIAL_STATIC=yes)
AC_MSG_RESULT($CFG_ENABLE_PARTIAL_STATIC)
# Flag to enable compiling with AddressSanitizer
AC_MSG_CHECKING(whether to use AddressSanitizer)
AC_ARG_ENABLE([asan],
[AS_HELP_STRING([--enable-asan],
[Enable compiling Verilator with ASAN
AddressSanitizer for memory error detection.
This disables tcmalloc. Does not affect
Verilated models using ASAN.])],
[case "${enableval}" in
yes) CFG_WITH_ASAN=yes ;;
no) CFG_WITH_ASAN=no ;;
*) AC_MSG_ERROR([bad value '${enableval}' for --enable-asan]) ;;
esac],
CFG_WITH_ASAN=no)
AC_MSG_RESULT($CFG_WITH_ASAN)
# Flag to enable linking Verilator with tcmalloc if available
AC_MSG_CHECKING(whether to use tcmalloc)
AC_ARG_ENABLE([tcmalloc],
@ -69,8 +85,12 @@ AC_ARG_ENABLE([tcmalloc],
*) AC_MSG_ERROR([bad value '${enableval}' for --enable-tcmalloc]) ;;
esac],
[CFG_WITH_TCMALLOC=check;])
AC_SUBST(CFG_WITH_TCMALLOC)
AC_MSG_RESULT($CFG_WITH_TCMALLOC)
if test "$CFG_WITH_ASAN" = "yes"; then
CFG_WITH_TCMALLOC=no
AC_MSG_RESULT("disabled by --enable-asan")
else
AC_MSG_RESULT($CFG_WITH_TCMALLOC)
fi
# Flag to enable coverage build
AC_MSG_CHECKING(whether to build for coverage collection)
@ -433,6 +453,14 @@ AC_SUBST(CFG_CXXFLAGS_COROUTINES)
AC_SUBST(HAVE_COROUTINES)
# Flags for compiling Verilator internals including parser always
if test "$CFG_WITH_ASAN" = "yes"; then
_MY_CXX_CHECK_IFELSE(-fsanitize=address -DVL_ASAN,
[CFG_CXXFLAGS_SRC="$CFG_CXXFLAGS_SRC -fsanitize=address -DVL_ASAN"
CFG_LDFLAGS_SRC="$CFG_LDFLAGS_SRC -fsanitize=address"
AC_DEFINE([HAVE_ASAN],[1],[Defined if built with AddresSanitizer])]
)
fi
AC_SUBST(HAVE_ASAN)
_MY_CXX_CHECK_OPT(CFG_CXXFLAGS_SRC,-Qunused-arguments)
_MY_CXX_CHECK_OPT(CFG_CXXFLAGS_SRC,-Wno-shadow)
_MY_CXX_CHECK_OPT(CFG_CXXFLAGS_SRC,-Wno-unused-parameter)

View File

@ -772,7 +772,7 @@ Summary:
be useful in makefiles. See also :vlopt:`-V`, and the various
:file:`*.mk` files.
Feature may be one of the following: COROUTINES, SYSTEMC.
Feature may be one of the following: COROUTINES, SYSTEMC, ASAN.
.. option:: --getenv <variable>

View File

@ -29,6 +29,21 @@
VL_DEFINE_DEBUG_FUNCTIONS;
//######################################################################
// AddressSanitizer default options
#ifdef VL_ASAN
extern "C" const char* __asan_default_options() {
// See https://github.com/google/sanitizers/wiki/SanitizerCommonFlags
// or 'env ASAN_OPTIONS=help=1 ./verilator_bin'
return ""
#ifndef VL_LEAK_CHECKS
":detect_leaks=0"
#endif
;
}
#endif
//######################################################################
// V3Global

View File

@ -817,6 +817,8 @@ string V3Options::getSupported(const string& var) {
// cppcheck-suppress knownConditionTrueFalse
} else if (var == "SYSTEMC" && systemCFound()) {
return "1";
} else if (var == "ASAN" && builtWithAsan()) {
return "1";
} else {
return "";
}
@ -843,6 +845,14 @@ bool V3Options::coroutineSupport() {
#endif
}
bool V3Options::builtWithAsan() {
#ifdef HAVE_ASAN
return true;
#else
return false;
#endif
}
//######################################################################
// V3 Options notification methods

View File

@ -821,6 +821,7 @@ public:
static bool systemCSystemWide();
static bool systemCFound(); // SystemC installed, or environment points to it
static bool coroutineSupport(); // Compiler supports coroutines
static bool builtWithAsan(); // Compiler built with AddressSanitizer
// METHODS (file utilities using these options)
string fileExists(const string& filename);

View File

@ -44,6 +44,9 @@ PACKAGE_VERSION_STRING_CHAR
// Define if compiled with tcmalloc
#undef HAVE_TCMALLOC
// Define if compiled with AddressSanitizer
#undef HAVE_ASAN
//**********************************************************************
//**** This file sometimes gets truncated, so check in consumers
#define HAVE_CONFIG_PACKAGE

View File

@ -1807,6 +1807,8 @@ class VlTest:
if not fails and status:
firstline = self._error_log_summary(logfile)
# Strip ANSI escape sequences
firstline = re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', firstline)
self.error("Exec of " + self._error_cmd_simplify(cmd) + " failed: " + firstline)
if fails and status:
print("(Exec expected to fail, and did.)")

View File

@ -11,6 +11,8 @@ import vltest_bootstrap
test.scenarios('vlt')
os.environ["ASAN_OPTIONS"] = "handle_segv=0"
test.lint(v_flags=["--debug-sigsegv"], fails=True, sanitize=0)
test.file_grep(test.compile_log_filename,