Skip to content

Commit 663bfd4

Browse files
Add ASAN/UBSan PHP versions
1 parent 0e95675 commit 663bfd4

File tree

13 files changed

+137
-23
lines changed

13 files changed

+137
-23
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
fail-fast: false
7474
matrix: ${{fromJson(needs.get-matrix.outputs.container_os_matrix)}}
7575
env:
76+
ASAN: ${{ matrix.asan }}
7677
BUILD: ${{ matrix.build }}
7778
GITHUB_MESSAGE: ${{ github.event.head_commit.message || github.event.inputs.build-mode }}
7879
GITHUB_REPOSITORY: ${{ github.repository }}
@@ -127,7 +128,7 @@ jobs:
127128
- name: Upload Artifact
128129
uses: actions/upload-artifact@v4
129130
with:
130-
name: php-sapi${{ matrix.php-version }}-${{ matrix.build }}+${{ matrix.dist }}-${{ matrix.dist-version }}
131+
name: php-sapi${{ matrix.php-version }}-${{ matrix.build }}${{ matrix.asan && '-asan' || '' }}+${{ matrix.dist }}-${{ matrix.dist-version }}
131132
path: /tmp/debian/*.zst
132133

133134
merge:
@@ -151,7 +152,7 @@ jobs:
151152

152153
- uses: actions/download-artifact@v5
153154
with:
154-
name: php-sapi${{ matrix.php-version }}-${{ matrix.build }}+${{ matrix.dist }}-${{ matrix.dist-version }}
155+
name: php-sapi${{ matrix.php-version }}-${{ matrix.build }}${{ matrix.asan && '-asan' || '' }}+${{ matrix.dist }}-${{ matrix.dist-version }}
155156
path: /tmp
156157

157158
- name: Stage builds
@@ -166,6 +167,7 @@ jobs:
166167
- name: Build and package
167168
run: bash scripts/build.sh merge
168169
env:
170+
ASAN: ${{ matrix.asan }}
169171
BUILD: ${{ matrix.build }}
170172
SAPI_LIST: ${{ env.SAPI_LIST }}
171173
GITHUB_USER: ${{ github.repository_owner }}
@@ -181,7 +183,7 @@ jobs:
181183
- name: Upload Artifact
182184
uses: actions/upload-artifact@v4
183185
with:
184-
name: php${{ matrix.php-version }}-${{ matrix.build }}+${{ matrix.dist }}-${{ matrix.dist-version }}
186+
name: php${{ matrix.php-version }}-${{ matrix.build }}${{ matrix.asan && '-asan' || '' }}+${{ matrix.dist }}-${{ matrix.dist-version }}
185187
path: |
186188
/tmp/*.xz
187189
/tmp/*.zst
@@ -230,13 +232,13 @@ jobs:
230232

231233
- uses: actions/download-artifact@v5
232234
with:
233-
name: php${{ matrix.php-version }}-${{ matrix.build }}+${{ matrix.dist }}-${{ matrix.dist-version }}
235+
name: php${{ matrix.php-version }}-${{ matrix.build }}${{ matrix.asan && '-asan' || '' }}+${{ matrix.dist }}-${{ matrix.dist-version }}
234236
path: /tmp
235237

236238
- name: Install PHP
237239
run: |
238240
sed -i '/download/d' scripts/install.sh
239-
bash scripts/install.sh ${{ matrix.php-version }} local ${{ matrix.debug }} ${{ matrix.build }}
241+
bash scripts/install.sh ${{ matrix.php-version }} local ${{ matrix.debug }} ${{ matrix.build }} ${{ matrix.asan }}
240242
241243
- name: Test
242244
run: |
@@ -271,13 +273,13 @@ jobs:
271273

272274
- uses: actions/download-artifact@v5
273275
with:
274-
name: php${{ matrix.php-version }}-${{ matrix.build }}+${{ matrix.os }}
276+
name: php${{ matrix.php-version }}-${{ matrix.build }}${{ matrix.asan && '-asan' || '' }}+${{ matrix.os }}
275277
path: /tmp
276278

277279
- name: Install PHP
278280
run: |
279281
sed -i '/download/d' scripts/install.sh
280-
bash scripts/install.sh ${{ matrix.php-version }} github ${{ matrix.debug }} ${{ matrix.build }}
282+
bash scripts/install.sh ${{ matrix.php-version }} github ${{ matrix.debug }} ${{ matrix.build }} ${{ matrix.asan }}
281283
282284
- name: Test
283285
run: |

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Install](#install)
1515
- [Extensions](#extensions)
1616
- [JIT](#jit)
17+
- [ASAN](#asan-addresssanitizer)
1718
- [SAPI Support](#sapi-support)
1819
- [Builds](#builds)
1920
- [Uninstall](#uninstall)
@@ -42,15 +43,16 @@ chmod a+x ./install.sh
4243

4344
The installer takes the following options:
4445
```bash
45-
./install.sh <php-version> <release|debug> <nts|zts>
46+
./install.sh <php-version> [release|debug] [nts|zts] [asan]
4647
```
4748

48-
The `php-version` is required, and `release` and `nts` are the defaults.
49+
The `php-version` is required, and `release` and `nts` are the defaults.
4950

5051
- release: No debugging symbols
5152
- debug: With debugging symbols
5253
- nts: Non Thread Safe
5354
- zts: Thread Safe
55+
- asan: AddressSanitizer build (PHP 8.0+ only)
5456

5557
### Examples
5658

@@ -66,6 +68,12 @@ The `php-version` is required, and `release` and `nts` are the defaults.
6668
./install.sh 8.4 debug zts
6769
```
6870

71+
- or, to install `PHP 8.4` with AddressSanitizer (for memory error detection):
72+
73+
```bash
74+
./install.sh 8.4 asan
75+
```
76+
6977
- Finally, test your PHP version:
7078

7179
```bash
@@ -134,6 +142,35 @@ To disable JIT:
134142
switch_jit -v <php-version> -s <ALL|sapi-name> disable
135143
```
136144

145+
## ASAN (AddressSanitizer)
146+
147+
PHP 8.0 and above versions have builds with AddressSanitizer (ASAN) and UndefinedBehaviorSanitizer (UBSan) enabled. These builds are useful for detecting memory issues.
148+
149+
To install an ASAN build:
150+
151+
```bash
152+
./install.sh 8.4 asan
153+
```
154+
155+
You can combine ASAN with other options:
156+
157+
```bash
158+
# ASAN + Thread Safe
159+
./install.sh 8.4 zts asan
160+
161+
# ASAN + Debug symbols
162+
./install.sh 8.4 debug asan
163+
```
164+
165+
**Notes:**
166+
167+
- ASAN builds are only available for PHP 8.0 and above.
168+
- Running PHP with ASAN will be slower than regular builds due to the instrumentation overhead.
169+
- You can configure ASAN behavior using the `ASAN_OPTIONS` environment variable:
170+
```bash
171+
ASAN_OPTIONS=detect_leaks=1 php your_script.php
172+
```
173+
137174
## SAPI support
138175

139176
These SAPIs are installed by default:
@@ -160,7 +197,7 @@ switch_sapi -v <php-version> -s <sapi|sapi:server>
160197

161198
## Builds
162199

163-
The following releases have `nts` and `zts` builds for the following PHP versions along with builds with and without debugging symbols.
200+
The following releases have `nts` and `zts` builds for the following PHP versions along with builds with and without debugging symbols. PHP 8.0+ versions also include AddressSanitizer (ASAN) builds for memory error detection.
164201

165202
- [PHP 8.6.0-dev](https://github.com/shivammathur/php-builder/releases/tag/8.6)
166203
- [PHP 8.5.x](https://github.com/shivammathur/php-builder/releases/tag/8.5)

config/definitions/8.0

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
# Placeholder for thread-safe build.
130130
ZTS
131131

132+
# Placeholder for ASAN build.
133+
ASAN
134+
132135
# Placeholder for patch commands.
133136
PATCHES
134137

config/definitions/8.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
# Placeholder for thread-safe build.
130130
ZTS
131131

132+
# Placeholder for ASAN build.
133+
ASAN
134+
132135
# Placeholder for patch commands.
133136
PATCHES
134137

config/definitions/8.2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
# Placeholder for thread-safe build.
130130
ZTS
131131

132+
# Placeholder for ASAN build.
133+
ASAN
134+
132135
# Placeholder for patch commands.
133136
PATCHES
134137

config/definitions/8.3

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
# Placeholder for thread-safe build.
130130
ZTS
131131

132+
# Placeholder for ASAN build.
133+
ASAN
134+
132135
# Placeholder for patch commands.
133136
PATCHES
134137

config/definitions/8.4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122
# Placeholder for thread-safe build.
123123
ZTS
124124

125+
# Placeholder for ASAN build.
126+
ASAN
127+
125128
# Placeholder for patch commands.
126129
PATCHES
127130

config/definitions/8.5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@
121121
# Placeholder for thread-safe build.
122122
ZTS
123123

124+
# Placeholder for ASAN build.
125+
ASAN
126+
124127
# Placeholder for patch commands.
125128
PATCHES
126129

config/definitions/8.6

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@
121121
# Placeholder for thread-safe build.
122122
ZTS
123123

124+
# Placeholder for ASAN build.
125+
ASAN
126+
124127
# Placeholder for patch commands.
125128
PATCHES
126129

scripts/build.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,26 @@ build_php() {
5151
echo "::group::$1"
5252
SAPI=$1
5353

54+
# Force disable LTO for ASAN builds (incompatible)
55+
if [ "${ASAN:-}" = "asan" ]; then
56+
lto="-lto"
57+
fi
58+
5459
# Set and export FLAGS
55-
CFLAGS="$(get_buildflags CFLAGS "$lto") $(getconf LFS_CFLAGS)"
60+
CFLAGS="$(get_buildflags CFLAGS "$lto") $(getconf LFS_CFLAGS)"
5661
CFLAGS=$(echo "$CFLAGS" | sed -E 's/-Werror=implicit-function-declaration//g')
5762
CFLAGS="$CFLAGS -DOPENSSL_SUPPRESS_DEPRECATED"
5863

5964
CPPFLAGS="$(get_buildflags CPPFLAGS "$lto")"
6065
CXXFLAGS="$(get_buildflags CXXFLAGS "$lto")"
6166
LDFLAGS="$(get_buildflags LDFLAGS "$lto") -Wl,-z,now -Wl,--as-needed"
67+
68+
# Add ASAN/UBSan flags
69+
if [ "${ASAN:-}" = "asan" ]; then
70+
CFLAGS="$CFLAGS -fsanitize=address,undefined -fno-omit-frame-pointer"
71+
CXXFLAGS="$CXXFLAGS -fsanitize=address,undefined -fno-omit-frame-pointer"
72+
LDFLAGS="$LDFLAGS -fsanitize=address,undefined"
73+
fi
6274

6375
if [[ "$PHP_VERSION" =~ 5.6|7.[0-4]|8.0 ]]; then
6476
EXTRA_CFLAGS="-fpermissive -Wno-deprecated -Wno-deprecated-declarations"
@@ -273,6 +285,12 @@ if [ "${BUILD:?}" = "zts" ]; then
273285
export PHP_PKG_SUFFIX=-zts
274286
fi
275287

288+
# Set ASAN options.
289+
if [ "${ASAN:-}" = "asan" ]; then
290+
PHP_PKG_SUFFIX="${PHP_PKG_SUFFIX:-}-asan"
291+
export PHP_PKG_SUFFIX
292+
fi
293+
276294
# Import OS information to the environment.
277295
. /etc/os-release
278296

0 commit comments

Comments
 (0)