From ba7461717adc5820a32faa510cea9ddecbb680b9 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 10 May 2021 00:51:58 +0200 Subject: [PATCH 1/2] Add ASan support for VS As of VS 16.9, there is native support for ASan in the MSVC toolchain. Although it is already possible to enable that by adding the respective option to `CFLAGS`, we make it available via the configure option `--enable-sanitizer` which is only used for clang builds on Windows so far. We also disable the manual inclusion of ASan headers, since these are available by default. We use the more general `__SANITIZE_ADDRESS__` for detection of ASan, to avoid a bogus warning, when `_setmaxstdio()` is called. --- Zend/zend_fibers.c | 2 +- ext/exif/exif.c | 2 +- ext/opcache/ZendAccelerator.c | 2 +- win32/build/confutils.js | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Zend/zend_fibers.c b/Zend/zend_fibers.c index aa052c7a66198..dc6add99cf602 100644 --- a/Zend/zend_fibers.c +++ b/Zend/zend_fibers.c @@ -55,7 +55,7 @@ # endif #endif -#ifdef __SANITIZE_ADDRESS__ +#if defined(__SANITIZE_ADDRESS__) && !defined(_MSC_VER) # include #endif diff --git a/ext/exif/exif.c b/ext/exif/exif.c index 74dba4bf6985f..017211bf62cf6 100644 --- a/ext/exif/exif.c +++ b/ext/exif/exif.c @@ -48,7 +48,7 @@ /* needed for ssize_t definition */ #include -#ifdef __SANITIZE_ADDRESS__ +#if defined(__SANITIZE_ADDRESS__) && !defined(_MSC_VER) # include #endif diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index a301888815b25..19bc6d210dd1d 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -3093,7 +3093,7 @@ static int accel_startup(zend_extension *extension) #endif #ifdef ZEND_WIN32 -# if !defined(__has_feature) || !__has_feature(address_sanitizer) +# ifndef __SANITIZE_ADDRESS__ _setmaxstdio(2048); /* The default configuration is limited to 512 stdio files */ # endif #endif diff --git a/win32/build/confutils.js b/win32/build/confutils.js index 01c76b0e1e886..4bef1d3728f95 100644 --- a/win32/build/confutils.js +++ b/win32/build/confutils.js @@ -1241,6 +1241,8 @@ function SAPI(sapiname, file_list, makefiletarget, cflags, obj_dir) if (PHP_SANITIZER == "yes") { if (CLANG_TOOLSET) { add_asan_opts("CFLAGS_" + SAPI, "LIBS_" + SAPI, (is_lib ? "ARFLAGS_" : "LDFLAGS_") + SAPI); + } else if (VS_TOOLSET) { + ADD_FLAG("CFLAGS", "/fsanitize=address"); } } From 30207921ed1918e74d0103404ab19241fa3a3c3f Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sat, 17 Jul 2021 23:51:38 +0200 Subject: [PATCH 2/2] Ignore STATUS_BAD_STACK exit codes under ASan for now These are likely bogus, but that issue should be investigated when there is time; for now we just ignore it to make progress. --- run-tests.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/run-tests.php b/run-tests.php index 787e964571f73..bb888d8d9e41f 100755 --- a/run-tests.php +++ b/run-tests.php @@ -159,7 +159,7 @@ function main(): void $temp_source, $temp_target, $test_cnt, $test_dirs, $test_files, $test_idx, $test_list, $test_results, $testfile, $user_tests, $valgrind, $sum_results, $shuffle, $file_cache, $num_repeats, - $bless; + $bless, $asan; // Parallel testing global $workers, $workerID; global $context_line_count; @@ -363,6 +363,7 @@ function main(): void $workers = null; $context_line_count = 3; $num_repeats = 1; + $asan = false; $cfgtypes = ['show', 'keep']; $cfgfiles = ['skip', 'php', 'clean', 'out', 'diff', 'exp', 'mem']; @@ -567,6 +568,7 @@ function main(): void break; case '--asan': case '--msan': + $asan = true; $environment['USE_ZEND_ALLOC'] = 0; $environment['USE_TRACKED_ALLOC'] = 1; $environment['SKIP_ASAN'] = 1; @@ -1312,7 +1314,10 @@ function system_with_timeout( $data .= "\nTermsig=" . ($stat["exitcode"] - 128) . "\n"; } else if (defined('PHP_WINDOWS_VERSION_MAJOR') && (($stat["exitcode"] >> 28) & 0b1111) === 0b1100) { // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781 - $data .= "\nTermsig=" . $stat["exitcode"] . "\n"; + // ignore STATUS_BAD_STACK exit codes under ASan for now + if (!$asan || $stat["exitcode"] !== -1073741784) { + $data .= "\nTermsig=" . $stat["exitcode"] . "\n"; + } } proc_close($proc);