Skip to content

Commit 122deea

Browse files
authored
Add ASan and UBSan options (#6825)
When we need to enable ASan/UBSan in PHP extension, we also need to enable it in PHP kernel. Providing these options makes it easier for us to enable ASan/UBSan when compiling PHP. And we use --enable-address-sanitizer and --enable-undefined-sanitizer in azure-pipelines.yml instead of changing CFLAGS manually. We also add compile flag check to MSan.
1 parent 559c3f6 commit 122deea

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

azure-pipelines.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ jobs:
7474
- template: azure/job.yml
7575
parameters:
7676
configurationName: DEBUG_ZTS_ASAN_UBSAN
77-
configurationParameters: >-
78-
--enable-debug --enable-zts
79-
CFLAGS='-fsanitize=undefined,address -DZEND_TRACK_ARENA_ALLOC'
80-
LDFLAGS='-fsanitize=undefined,address'
77+
configurationParameters: '--enable-debug --enable-zts --enable-address-sanitizer --enable-undefined-sanitizer'
8178
runTestsParameters: --asan
8279
timeoutInMinutes: 360
8380
- template: azure/msan_job.yml
@@ -90,9 +87,8 @@ jobs:
9087
parameters:
9188
configurationName: COMMUNITY
9289
configurationParameters: >-
93-
--enable-debug --enable-zts
94-
CFLAGS='-fsanitize=undefined,address -fno-sanitize-recover -DZEND_TRACK_ARENA_ALLOC'
95-
LDFLAGS='-fsanitize=undefined,address'
90+
--enable-debug --enable-zts --enable-address-sanitizer --enable-undefined-sanitizer
91+
CFLAGS='-fno-sanitize-recover'
9692
- template: azure/coverage_job.yml
9793
parameters:
9894
configurationName: COVERAGE_DEBUG_ZTS

configure.ac

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,16 @@ PHP_ARG_ENABLE([memory-sanitizer],,
982982
[Enable memory sanitizer (clang only)])],
983983
[no],
984984
[no])
985+
PHP_ARG_ENABLE([address-sanitizer],,
986+
[AS_HELP_STRING([--enable-address-sanitizer],
987+
[Enable address sanitizer])],
988+
[no],
989+
[no])
990+
PHP_ARG_ENABLE([undefined-sanitizer],,
991+
[AS_HELP_STRING([--enable-undefined-sanitizer],
992+
[Enable undefined sanitizer])],
993+
[no],
994+
[no])
985995

986996
dnl Extension configuration.
987997
dnl ----------------------------------------------------------------------------
@@ -1372,10 +1382,38 @@ if test "$PHP_WERROR" = "yes"; then
13721382
CXXFLAGS="$CXXFLAGS -Werror"
13731383
fi
13741384

1385+
if test "$PHP_MEMORY_SANITIZER" = "yes" &&
1386+
test "$PHP_ADDRESS_SANITIZER" = "yes"; then
1387+
AC_MSG_ERROR([MemorySanitizer and AddressSanitizer are mutually exclusive])
1388+
fi
1389+
13751390
dnl Enable -fsanitize=memory late, because interceptors may break linking detection.
13761391
if test "$PHP_MEMORY_SANITIZER" = "yes"; then
1377-
CFLAGS="$CFLAGS -fsanitize=memory -fsanitize-memory-track-origins"
1378-
CXXFLAGS="$CXXFLAGS -fsanitize=memory -fsanitize-memory-track-origins"
1392+
AX_CHECK_COMPILE_FLAG([-fsanitize=memory -fsanitize-memory-track-origins], [
1393+
CFLAGS="$CFLAGS -fsanitize=memory -fsanitize-memory-track-origins"
1394+
CXXFLAGS="$CXXFLAGS -fsanitize=memory -fsanitize-memory-track-origins"
1395+
], [AC_MSG_ERROR([MemorySanitizer is not available])])
1396+
fi
1397+
1398+
if test "$PHP_ADDRESS_SANITIZER" = "yes"; then
1399+
AX_CHECK_COMPILE_FLAG([-fsanitize=address], [
1400+
CFLAGS="$CFLAGS -fsanitize=address -DZEND_TRACK_ARENA_ALLOC"
1401+
CXXFLAGS="$CXXFLAGS -fsanitize=address -DZEND_TRACK_ARENA_ALLOC"
1402+
], [AC_MSG_ERROR([AddressSanitizer is not available])])
1403+
fi
1404+
1405+
if test "$PHP_UNDEFINED_SANITIZER" = "yes"; then
1406+
AX_CHECK_COMPILE_FLAG([-fsanitize=undefined], [
1407+
CFLAGS="$CFLAGS -fsanitize=undefined"
1408+
CXXFLAGS="$CXXFLAGS -fsanitize=undefined"
1409+
], [AC_MSG_ERROR([UndefinedBehaviorSanitizer is not available])])
1410+
fi
1411+
1412+
if test "$PHP_MEMORY_SANITIZER" = "yes" ||
1413+
test "$PHP_ADDRESS_SANITIZER" = "yes" ||
1414+
test "$PHP_UNDEFINED_SANITIZER" = "yes"; then
1415+
CFLAGS="$CFLAGS -fno-omit-frame-pointer"
1416+
CXXFLAGS="$CXXFLAGS -fno-omit-frame-pointer"
13791417
fi
13801418

13811419
dnl

0 commit comments

Comments
 (0)