Skip to content

Commit c9cc89c

Browse files
committed
Support --enable-sanitizer for MSVC builds
While it is already possible to enable ASan for MSVC (assuming Visual Studio 2019 16.10 or later) by passing `/fsanitizer=address` in the `CFLAGS`, it is only usable if `ZEND_DEBUG` is also enabled; otherwise there are `STATUS_BACK_STACK` errors all the time. Since it makes some sense to combine ASan instrumentation with debug assertions enabled anyway (typical for fuzzing), we support the configure option `--enable-sanitizer`, which is already supported for Clang builds, also for MSVC builds. Note that MSVC supports only ASan for now; contrary to Clang which additionally supports UBSan on Windows. Since ASan reports can be pretty useless without debug symbol information, we require such builds to also produce PDBs (i.e. `--enable-debug-pack`), but forbid actual debug builds (for performance reasons, and because the way it is implemented it would not make sense; that was already an issue with Clang builds with sanitizers enabled). Closes GH-16999.
1 parent d18b722 commit c9cc89c

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ PHP 8.5 UPGRADE NOTES
166166
PHP_RELEASE_VERSION are now always numbers. Previously, they have been
167167
strings for buildconf builds.
168168

169+
* --enable-sanitzer is now supported for MSVC builds. This enables ASan and
170+
debug assertions, and is supported as of MSVC 16.9 and Windows 10.
171+
169172
* COM:
170173
. The extension is now build shared by default; previously it defaulted to a
171174
static extension, although the official Windows binaries built a shared

win32/build/config.w32

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,20 @@ if (PHP_SECURITY_FLAGS == "yes") {
356356
}
357357

358358
ARG_WITH("uncritical-warn-choke", "Disable some uncritical warnings", "yes");
359-
ARG_ENABLE("sanitizer", "Enable ASan and UBSan extensions", "no");
360-
if (CLANG_TOOLSET) {
359+
ARG_ENABLE("sanitizer", "Enable ASan (and UBSan) extensions", "no");
360+
if (PHP_SANITIZER == "yes" && PHP_DEBUG == "yes") {
361+
ERROR("Use of both --enable-sanitizer and --enable-debug not allowed.");
362+
}
363+
if (PHP_SANITIZER == "yes" && PHP_DEBUG_PACK == "no") {
364+
ERROR("--enable-sanitizer requires --enable-debug-pack");
365+
}
366+
if (VS_TOOLSET) {
367+
if (PHP_SANITIZER == "yes") {
368+
if (COMPILER_NUMERIC_VERSION < 1929) {
369+
ERROR("MSVC at least 16.10 required for sanitation plugins");
370+
}
371+
}
372+
} else if (CLANG_TOOLSET) {
361373
if (PHP_UNCRITICAL_WARN_CHOKE != "no") {
362374
ADD_FLAG("CFLAGS", "-Wno-ignored-attributes -Wno-deprecated-declarations -Wno-missing-braces " +
363375
"-Wno-logical-op-parentheses -Wno-msvc-include -Wno-invalid-source-encoding -Wno-unknown-pragmas " +

win32/build/confutils.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,8 @@ function SAPI(sapiname, file_list, makefiletarget, cflags, obj_dir)
12461246
if (PHP_SANITIZER == "yes") {
12471247
if (CLANG_TOOLSET) {
12481248
add_asan_opts("CFLAGS_" + SAPI, "LIBS_" + SAPI, (is_lib ? "ARFLAGS_" : "LDFLAGS_") + SAPI);
1249+
} else if (VS_TOOLSET) {
1250+
ADD_FLAG("CFLAGS", "/fsanitize=address");
12491251
}
12501252
}
12511253

@@ -3442,8 +3444,12 @@ function toolset_setup_build_mode()
34423444
ADD_FLAG("LDFLAGS", "/incremental:no /debug /opt:ref,icf");
34433445
}
34443446
ADD_FLAG("CFLAGS", "/LD /MD");
3445-
if (PHP_SANITIZER == "yes" && CLANG_TOOLSET) {
3446-
ADD_FLAG("CFLAGS", "/Od /D NDebug /D NDEBUG /D ZEND_WIN32_NEVER_INLINE /D ZEND_DEBUG=0");
3447+
if (PHP_SANITIZER == "yes") {
3448+
if (VS_TOOLSET) {
3449+
ADD_FLAG("CFLAGS", "/Ox /U NDebug /U NDEBUG /D ZEND_DEBUG=1");
3450+
} else if (CLANG_TOOLSET) {
3451+
ADD_FLAG("CFLAGS", "/Od /D NDebug /D NDEBUG /D ZEND_WIN32_NEVER_INLINE /D ZEND_DEBUG=0");
3452+
}
34473453
} else {
34483454
// Equivalent to Release_TSInline build -> best optimization
34493455
ADD_FLAG("CFLAGS", "/Ox /D NDebug /D NDEBUG /GF /D ZEND_DEBUG=0");

0 commit comments

Comments
 (0)