From cd7a7890403b1a7b049c2a6a7e89ce300c188d39 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Sun, 30 Aug 2026 14:16:02 +0300 Subject: [PATCH] Add large-codebase PCOV 2.0 --- .appveyor.yml | 159 -- .dockerignore | 27 + .gitattributes | 8 + .github/workflows/ci.yml | 51 + .github/workflows/release.yml | 23 + .gitignore | 3 + .travis.yml | 62 - INSTALL.md | 39 - README.md | 349 +++-- benchmark/large-codebase/README.md | 31 + benchmark/large-codebase/e2e-summary.json | 40 + benchmark/large-codebase/generate.php | 24 + benchmark/large-codebase/run.php | 205 +++ benchmark/large-codebase/run.sh | 34 + benchmark/large-codebase/worker.php | 93 ++ cfg/704/zend_cfg.c | 2 +- composer.json | 18 +- config.m4 | 34 +- config.w32 | 10 +- docs/manifest-and-records.md | 103 ++ pcov.c | 275 +++- pcov_dump.c | 1736 +++++++++++++++++++++ pcov_fingerprint.c | 468 ++++++ pcov_fingerprint.h | 57 + php_pcov.h | 118 +- tests/006.phpt | 3 + tests/016.phpt | 45 + tests/022.phpt | 87 ++ tests/023.phpt | 58 + tests/027.phpt | 47 + tests/028.phpt | 96 ++ tests/029.phpt | 140 ++ tests/030.phpt | 132 ++ tests/031.phpt | 113 ++ tests/032.phpt | 75 + tests/033.phpt | 58 + tests/034.phpt | 47 + tests/035.phpt | 62 + tests/036.phpt | 106 ++ tests/037.phpt | 99 ++ tests/038.phpt | 105 ++ tests/039.phpt | 66 + tests/040.phpt | 35 + tools/pcov_manifest.php | 66 + tools/pcov_manifest_tools.php | 568 +++++++ tools/pcov_record_loader.php | 233 +++ 46 files changed, 5759 insertions(+), 451 deletions(-) delete mode 100644 .appveyor.yml create mode 100644 .dockerignore create mode 100644 .gitattributes create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml delete mode 100644 .travis.yml delete mode 100644 INSTALL.md create mode 100644 benchmark/large-codebase/README.md create mode 100644 benchmark/large-codebase/e2e-summary.json create mode 100644 benchmark/large-codebase/generate.php create mode 100644 benchmark/large-codebase/run.php create mode 100755 benchmark/large-codebase/run.sh create mode 100644 benchmark/large-codebase/worker.php create mode 100644 docs/manifest-and-records.md create mode 100644 pcov_dump.c create mode 100644 pcov_fingerprint.c create mode 100644 pcov_fingerprint.h create mode 100644 tests/016.phpt create mode 100644 tests/022.phpt create mode 100644 tests/023.phpt create mode 100644 tests/027.phpt create mode 100644 tests/028.phpt create mode 100644 tests/029.phpt create mode 100644 tests/030.phpt create mode 100644 tests/031.phpt create mode 100644 tests/032.phpt create mode 100644 tests/033.phpt create mode 100644 tests/034.phpt create mode 100644 tests/035.phpt create mode 100644 tests/036.phpt create mode 100644 tests/037.phpt create mode 100644 tests/038.phpt create mode 100644 tests/039.phpt create mode 100644 tests/040.phpt create mode 100644 tools/pcov_manifest.php create mode 100644 tools/pcov_manifest_tools.php create mode 100644 tools/pcov_record_loader.php diff --git a/.appveyor.yml b/.appveyor.yml deleted file mode 100644 index 77054ec..0000000 --- a/.appveyor.yml +++ /dev/null @@ -1,159 +0,0 @@ -version: "{branch}.build.{build}" -skip_tags: true - -branches: - only: - - develop - -clone_folder: c:\projects\pcov - -install: - ps: | - if (-not (Test-Path c:\build-cache)) { - mkdir c:\build-cache - } - $bname = 'php-sdk-' + $env:BIN_SDK_VER + '.zip' - if (-not (Test-Path c:\build-cache\$bname)) { - Invoke-WebRequest "https://github.com/OSTC/php-sdk-binary-tools/archive/$bname" -OutFile "c:\build-cache\$bname" - } - $dname0 = 'php-sdk-binary-tools-php-sdk-' + $env:BIN_SDK_VER - $dname1 = 'php-sdk-' + $env:BIN_SDK_VER - if (-not (Test-Path c:\build-cache\$dname1)) { - & 7z x c:\build-cache\$bname -oc:\build-cache - move c:\build-cache\$dname0 c:\build-cache\$dname1 - } - -cache: - c:\build-cache -> .appveyor.yml - -environment: - BIN_SDK_VER: 2.2.0 - matrix: - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 - ARCH: x64 - VC: vc14 - PHP_VER: 7.1.33 - TS: 1 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 - ARCH: x86 - VC: vc14 - PHP_VER: 7.1.33 - TS: 0 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - ARCH: x64 - VC: vc15 - PHP_VER: 7.2.34 - TS: 1 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - ARCH: x86 - VC: vc15 - PHP_VER: 7.2.34 - TS: 0 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - ARCH: x64 - VC: vc15 - PHP_VER: 7.3.27 - TS: 1 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - ARCH: x86 - VC: vc15 - PHP_VER: 7.3.27 - TS: 0 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - ARCH: x64 - VC: vc15 - PHP_VER: 7.4.16 - TS: 1 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - ARCH: x86 - VC: vc15 - PHP_VER: 7.4.16 - TS: 0 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 - ARCH: x64 - VC: vs16 - PHP_VER: 8.0.3 - TS: 1 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 - ARCH: x86 - VC: vs16 - PHP_VER: 8.0.3 - TS: 0 - -build_script: - ps: | - $ts_part = '' - if ('0' -eq $env:TS) { $ts_part = '-nts' } - $bname = 'php-devel-pack-' + $env:PHP_VER + $ts_part + '-Win32-' + $env:VC.toUpper() + '-' + $env:ARCH + '.zip' - if (-not (Test-Path c:\build-cache\$bname)) { - Invoke-WebRequest "http://windows.php.net/downloads/releases/archives/$bname" -OutFile "c:\build-cache\$bname" - if (-not (Test-Path c:\build-cache\$bname)) { - Invoke-WebRequest "http://windows.php.net/downloads/releases/$bname" -OutFile "c:\build-cache\$bname" - } - } - $dname0 = 'php-' + $env:PHP_VER + '-devel-' + $env:VC.toUpper() + '-' + $env:ARCH - $dname1 = 'php-' + $env:PHP_VER + $ts_part + '-devel-' + $env:VC.toUpper() + '-' + $env:ARCH - if (-not (Test-Path c:\build-cache\$dname1)) { - 7z x c:\build-cache\$bname -oc:\build-cache - move c:\build-cache\$dname0 c:\build-cache\$dname1 - } - cd c:\projects\pcov - $env:PATH = 'c:\build-cache\' + $dname1 + ';' + $env:PATH - #echo "@echo off" | Out-File -Encoding "ASCII" task.bat - #echo "" | Out-File -Encoding "ASCII" -Append task.bat - echo "" | Out-File -Encoding "ASCII" task.bat - echo "call phpize 2>&1" | Out-File -Encoding "ASCII" -Append task.bat - if ('1' -eq $env:UTF32) { $utf32_part = '--enable-pcov-utf32' } - $conf_cmd = "call configure --enable-pcov " + $utf32_part + " --enable-debug-pack 2>&1" - echo $conf_cmd | Out-File -Encoding "ASCII" -Append task.bat - echo "nmake /nologo 2>&1" | Out-File -Encoding "ASCII" -Append task.bat - echo "exit %errorlevel%" | Out-File -Encoding "ASCII" -Append task.bat - $here = (Get-Item -Path "." -Verbose).FullName - $runner = 'c:\build-cache\php-sdk-' + $env:BIN_SDK_VER + '\phpsdk' + '-' + $env:VC + '-' + $env:ARCH + '.bat' - $task = $here + '\task.bat' - & $runner -t $task - -after_build: - ps: | - $ts_part = 'ts' - if ('0' -eq $env:TS) { $ts_part = 'nts' } - $zip_bname = 'php_pcov-' + $env:APPVEYOR_REPO_COMMIT.substring(0, 8) + '-' + $env:PHP_VER.substring(0, 3) + '-' + $ts_part + '-' + $env:VC + '-' + $env:ARCH + '.zip' - $dir = 'c:\projects\pcov\'; - if ('x64' -eq $env:ARCH) { $dir = $dir + 'x64\' } - $dir = $dir + 'Release' - if ('1' -eq $env:TS) { $dir = $dir + '_TS' } - & 7z a c:\$zip_bname $dir\php_pcov.dll $dir\php_pcov.pdb c:\projects\pcov\LICENSE - Push-AppveyorArtifact c:\$zip_bname - -test_script: - ps: | - $ts_part = '' - if ('0' -eq $env:TS) { $ts_part = '-nts' } - $bname = 'php-' + $env:PHP_VER + $ts_part + '-Win32-' + $env:VC.toUpper() + '-' + $env:ARCH + '.zip' - if (-not (Test-Path c:\build-cache\$bname)) { - Invoke-WebRequest "http://windows.php.net/downloads/releases/archives/$bname" -OutFile "c:\build-cache\$bname" - if (-not (Test-Path c:\build-cache\$bname)) { - Invoke-WebRequest "http://windows.php.net/downloads/releases/$bname" -OutFile "c:\build-cache\$bname" - } - } - $dname = 'php-' + $env:PHP_VER + $ts_part + '-' + $env:VC.toUpper() + '-' + $env:ARCH - if (-not (Test-Path c:\build-cache\$dname)) { - 7z x c:\build-cache\$bname -oc:\build-cache\$dname - } - cd c:\projects\pcov - echo "" | Out-File -Encoding "ASCII" task.bat - echo "set REPORT_EXIT_STATUS=1" | Out-File -Encoding "ASCII" -Append task.bat - echo "set TEST_PHP_JUNIT=c:\junit.out.xml" | Out-File -Encoding "ASCII" task.bat - if ('1' -eq $env:UTF32) { $utf32_part = '--enable-pcov-utf32' } - $cmd = 'call configure --enable-pcov ' + $utf32_part + ' --with-prefix=c:\build-cache\' + $dname + " 2>&1" - echo $cmd | Out-File -Encoding "ASCII" -Append task.bat - echo 'nmake /nologo test TESTS="-q --show-diff --set-timeout 120" 2>&1' | Out-File -Encoding "ASCII" -Append task.bat - echo "exit %errorlevel%" | Out-File -Encoding "ASCII" -Append task.bat - $here = (Get-Item -Path "." -Verbose).FullName - $runner = 'c:\build-cache\php-sdk-' + $env:BIN_SDK_VER + '\phpsdk' + '-' + $env:VC + '-' + $env:ARCH + '.bat' - $task = $here + '\task.bat' - & $runner -t $task - $wc = New-Object 'System.Net.WebClient' - $wc.UploadFile("https://ci.appveyor.com/api/testresults/junit/$($env:APPVEYOR_JOB_ID)", "c:\junit.out.xml") - - diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f10d9bd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,27 @@ +.git +.idea +.libs +autom4te.cache +config.cache +config.h +config.log +config.nice +config.status +configure +libtool +Makefile +Makefile.fragments +Makefile.objects +modules +run-tests.php +tests/*.diff +tests/*.exp +tests/*.log +tests/*.out +tests/*.php +tmp-php.ini +benchmark/results +*.lo +*.o +*.so +*.dep diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bc6f797 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +/.idea export-ignore +/.github export-ignore +/.gitignore export-ignore +/.gitattributes export-ignore +/.travis.yml export-ignore +/.appveyor.yml export-ignore +/benchmark/results export-ignore +/.dockerignore export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f083d36 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,51 @@ +name: CI + +on: + push: + pull_request: + +permissions: + contents: read + +jobs: + linux-nts: + name: PHP ${{ matrix.php }} NTS + runs-on: ubuntu-latest + container: php:${{ matrix.php }}-cli + strategy: + fail-fast: false + matrix: + php: ["8.3", "8.4", "8.5"] + + steps: + - name: Install build tools + run: | + apt-get update + apt-get install -y --no-install-recommends autoconf g++ gcc git libc-dev make pkg-config re2c + + - uses: actions/checkout@v4 + + - name: Check whitespace + run: | + if find . -path './.git' -prune -o -type f -print0 | xargs -0 -r grep -IEn '[[:blank:]]+$'; then + echo 'Trailing whitespace found' >&2 + exit 1 + fi + + - name: Validate package metadata + run: | + php -r "\$json = json_decode(file_get_contents('composer.json'), true, flags: JSON_THROW_ON_ERROR); assert(\$json['type'] === 'php-ext');" + + - name: Configure and build with strict warnings + run: | + phpize + CFLAGS="-O2 -Wall -Wextra -Werror" ./configure --enable-pcov + make -j"$(nproc)" + + - name: Run PHPT suite + run: NO_INTERACTION=1 REPORT_EXIT_STATUS=1 make test + + - name: Check PHP and shell syntax + run: | + find tools benchmark -type f -name '*.php' -print0 | xargs -0 -n1 php -l + find benchmark -type f -name '*.sh' -print0 | xargs -0 -r -n1 bash -n diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f04cf34 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,23 @@ +name: Release + +on: + push: + tags: + - "v*" + +permissions: + contents: write + +jobs: + release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Verify tag matches extension version + run: | + version="$(sed -n 's/^#define PHP_PCOV_VERSION "\([^"]*\)"/\1/p' php_pcov.h)" + test "v${version}" = "${GITHUB_REF_NAME}" + - name: Create GitHub release + env: + GH_TOKEN: ${{ github.token }} + run: gh release create "${GITHUB_REF_NAME}" --generate-notes --verify-tag diff --git a/.gitignore b/.gitignore index a3b5d79..7e545c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Object files *.o *.lo +*.dep # Libraries *.lib @@ -53,3 +54,5 @@ run-tests.log tmp-php.ini /vendor/ +.idea +benchmark/results/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index db4dc22..0000000 --- a/.travis.yml +++ /dev/null @@ -1,62 +0,0 @@ -language: php -compiler: - - gcc -addons: - apt: - packages: - - valgrind -php: - - 8.0 - - nightly - -env: - global: - - REPORT_EXIT_STATUS=1 - - RULE="all" - - DEF="-q --show-diff" - - MEMCHECK="-m --show-mem" - matrix: - - OPCACHE=0 TEST_PHP_ARGS="$DEF" - - OPCACHE=1 TEST_PHP_ARGS="$DEF" - - OPCACHE=0 TEST_PHP_ARGS="$DEF $MEMCHECK" - - OPCACHE=1 TEST_PHP_ARGS="$DEF $MEMCHECK" - -matrix: - allow_failures: - - php: nightly - env: OPCACHE=0 TEST_PHP_ARGS="$DEF" - - php: nightly - env: OPCACHE=1 TEST_PHP_ARGS="$DEF" - - php: nightly - env: OPCACHE=0 TEST_PHP_ARGS="$DEF $MEMCHECK" - - php: nightly - env: OPCACHE=1 TEST_PHP_ARGS="$DEF $MEMCHECK" - -before_script: - - echo "{" >> $HOME/default.supp - - echo "String_Equality_Intentionally_Reads_Uninit_Memory" >> $HOME/default.supp - - echo "Memcheck:Cond" >> $HOME/default.supp - - echo "fun:zend_string_equal_val" >> $HOME/default.supp - - echo "}" >> $HOME/default.supp - - echo "#!/bin/bash" >> $HOME/bin/valgrind - - echo "/usr/bin/valgrind --suppressions=$HOME/default.supp \$@" >> $HOME/bin/valgrind - - chmod +x $HOME/bin/valgrind - - export PATH=$HOME/bin:$PATH - - echo "variables_order=EGPCS" >> $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/php.ini - - echo > $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/xdebug.ini - - echo "extension=pcov.so" > $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/pcov.ini - - echo "pcov.enabled=1" >> $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/pcov.ini - - echo "opcache.enable_cli=$OPCACHE" > $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/opcache.ini - - echo "opcache.optimization_level=0x7FFEBFFF" >> $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/opcache.ini - -script: - - phpize - - wget -q -O run-tests.php https://raw.githubusercontent.com/php/php-src/master/run-tests.php - - ./configure --$CONF - - make $RULE - - make install - - php run-tests.php -P - - git clone --depth=1 https://github.com/sebastianbergmann/php-code-coverage - - cd php-code-coverage - - composer install --ignore-platform-reqs - - vendor/bin/phpunit -c phpunit.xml diff --git a/INSTALL.md b/INSTALL.md deleted file mode 100644 index f6daf70..0000000 --- a/INSTALL.md +++ /dev/null @@ -1,39 +0,0 @@ -Requirements -============ - - * PHP 7.1+ - -Installation -============ - -**From sources** - - git clone https://github.com/krakjoe/pcov.git - cd pcov - phpize - ./configure --enable-pcov - make - make test - make install - -Use `develop` branch for development, use `release` branch for last stable released version. - -**From PECL** - - pecl install pcov - -**Binary distributions** - - * **Microsoft Windows**: dll available on [https://pecl.php.net/package/pcov](pecl) - - * **Fedora** 29 and up: use the [php-pecl-pcov](https://packages.fedoraproject.org/pkgs/php-pecl-pcov/php-pecl-pcov/) package - - `dnf install php-pecl-pcov` - -* **Ubuntu** Setup Ubuntu PPA [ppa:ondrej/php](https://launchpad.net/~ondrej/+archive/ubuntu/php/) - - `apt install php-pcov` - -* **Debian** Setup Debian DPA [packages.sury.org/php](https://packages.sury.org/php/) - - `apt install php-pcov` diff --git a/README.md b/README.md index fde9041..7270475 100644 --- a/README.md +++ b/README.md @@ -1,150 +1,277 @@ -PCOV -==== - -[![Build Status](https://travis-ci.org/krakjoe/pcov.svg?branch=develop)](https://travis-ci.org/krakjoe/pcov) -[![Build status](https://ci.appveyor.com/api/projects/status/w265n0w7yk6o3y6m?svg=true)](https://ci.appveyor.com/project/krakjoe/pcov) - -A self contained [CodeCoverage](https://github.com/sebastianbergmann/php-code-coverage) compatible driver for PHP +# PCOV for large PHP codebases + +This is an alternative distribution of [PCOV](https://github.com/krakjoe/pcov) +for aggregate coverage of large PHP test suites. It keeps PCOV's fast opcode +recording and all upstream APIs, and adds a synchronous native export path that +can reuse an immutable executable-line manifest. + +Normal PCOV records positive line hits while PHP executes. `pcov\collect()` +later runs Zend control-flow-graph (CFG) discovery to find executable but +unexecuted lines and builds the nested PHP coverage array. Repeating that work +for many web requests can dominate coverage collection in a large application. + +```text +Normal full collection: +every request repeatedly discovers all executable lines + +Manifest mode: +discover executable lines once +validate that code is unchanged +save only lines actually executed +fall back to full collection whenever validity is uncertain +``` -Requirements and Installation -============================= +Nothing is forked from PHP-FPM, no work is delegated to another process, and +request export remains synchronous. Native full collection is always the +correctness fallback. The optimization is designed for coverage aggregated +across an entire suite, not for a standalone request report. -See [INSTALL.md](INSTALL.md) +## Requirements and installation +Release 2.0 supports Linux, NTS PHP 8.3, 8.4, and 8.5. It uses the extension +name `pcov`, so it replaces and cannot be loaded beside official PCOV. -API -=== +Install with [PIE](https://github.com/php/pie), the PHP extension installer: -```php -/** - * Shall start recording coverage information - */ -function \pcov\start() : void; - -/** - * Shall stop recording coverage information - */ -function \pcov\stop() : void; - -/** - * Shall collect coverage information - * - * @param integer $type define witch type of information should be collected - * \pcov\all shall collect coverage information for all files - * \pcov\inclusive shall collect coverage information for the specified files - * \pcov\exclusive shall collect coverage information for all but the specified files - * @param array $filter path of files (realpath) that should be filtered - * - * @return array - */ -function \pcov\collect(int $type = \pcov\all, array $filter = []) : array; - -/** - * Shall clear stored information - * - * @param bool $files set true to clear file tables - * - * Note: clearing the file tables may have surprising consequences - */ -function \pcov\clear(bool $files = false) : void; - -/** - * Shall return list of files waiting to be collected - */ -function \pcov\waiting() : array; - -/** - * Shall return the current size of the trace and cfg arena - */ -function \pcov\memory() : int; +```sh +pie install vitaliyboyko/pcov ``` -Configuration -============= +Or build from source: -PCOV is configured using PHP.ini: +```sh +phpize +./configure --enable-pcov +make -j"$(getconf _NPROCESSORS_ONLN)" +make test +sudo make install +``` -| Option | Default | Changeable | Description | -|:-----------------------|:-------------------|:--------------:|:------------------------------------------------------| -| `pcov.enabled` | 1 | SYSTEM | enable or disable zend hooks for pcov | -| `pcov.directory` | auto | SYSTEM,PERDIR | restrict collection to files under this path | -| `pcov.exclude` | unused | SYSTEM,PERDIR | exclude files under pcov.directory matching this PCRE | -| `pcov.initial.memory` | 65536 | SYSTEM,PERDIR | shall set initial size of arena | -| `pcov.initial.files` | 64 | SYSTEM,PERDIR | shall set initial size of tables | +Enable it in the dedicated coverage SAPI: -Notes ------ +```ini +extension=pcov.so +pcov.enabled=1 +pcov.large_codebase=1 +pcov.directory=/path/to/project +``` -The recommended defaults for production should be: +`pcov.large_codebase` is the single feature switch and defaults to `1`. Set it +to `0` to retain upstream collection behavior without compile fingerprints or +native export. Application sources must be immutable during a request and an +OPcache generation must not mix deployments; use atomic deployment paths and +reset OPcache when switching generations. - * `pcov.enabled = 0` +## Upstream-compatible API -The recommended defaults for development should be: +The original API and line-coverage semantics are unchanged: - * `pcov.enabled = 1` - * `pcov.directory = /path/to/your/source/directory` +```php +pcov\start(); +// Execute application or test code. +pcov\stop(); -When `pcov.directory` is left unset, PCOV will attempt to find `src`, `lib` or, `app` in the current -working directory, in that order; If none are found the current directory will be used, which may waste resources storing -coverage information for the test suite. +$coverage = pcov\collect(); // array> +pcov\clear(); +``` -If `pcov.directory` contains test code, it's recommended to set `pcov.exclude` to avoid wasting resources. +`pcov\waiting()`, `pcov\memory()`, `pcov\enabled()`, `pcov\all`, +`pcov\inclusive`, and `pcov\exclusive` remain available. -To avoid unnecessary allocation of additional arenas for traces and control flow graphs, `pcov.initial.memory` should be set according to the memory required by the test suite, which may be discovered with `\pcov\memory()`. +## Large-codebase workflow -To avoid reallocation of tables, `pcov.initial.files` should be set to a number higher than the number of files that will be loaded during testing, inclusive of test files. +### 1. Define the deployment identity -*Note that arenas are allocated in chunks: If the chunk size is set to 65536 and pcov require 65537 bytes, the system will allocate two chunks, each 65536 bytes. When setting arena space therefore, be generous in your estimates.* +The identity must change when application code, dependencies, enabled modules, +generated code, or the relevant source inventory changes: -Interoperability -================ +```sh +DEPLOYMENT_ID=$(php -r ' +require "tools/pcov_manifest_tools.php"; +echo pcov_manifest_environment_identity([ + "application_revision" => "release-2026-08-30", + "dependency_lock" => hash_file("sha256", "composer.lock"), + "modules_config" => hash("sha256", "enabled-modules-v1"), + "generated_code" => hash("sha256", "generated-code-v1"), + "source_tree" => hash("sha256", "source-inventory-v1"), +])["id"]; +') +``` -When PCOV is enabled by configuration `pcov.enabled=1`: +Use authoritative build inputs in place of the generic strings. Timestamps +alone are not a deployment identity. - * interoperability with Xdebug is not possible - * interoperability with phpdbg is not possible - * interoperability with Blackfire profiler is not possible +### 2. Generate an immutable manifest -At an internals level, the executor function is overriden by pcov, so any extension or SAPI which does the same will be broken. +During one complete reference suite, export full records tagged with that +identity: -When PCOV is disabled by configuration `pcov.enabled=0`: +```php +$result = pcov\export( + '/path/to/bootstrap/request-1.pcov', + null, + getenv('DEPLOYMENT_ID') ?: throw new RuntimeException('Missing deployment identity') +); + +if ($result === false || $result['mode'] !== 'full') { + throw new RuntimeException('Full PCOV export failed'); +} +``` - * PCOV is zero cost - code runs at full speed - * Xdebug may be loaded - * phpdbg may be executed - * Blackfire probe may be loaded +Build and atomically publish the immutable manifest from all reference records: -At an internals level, the executor function is untouched, and pcov allocates no memory. +```sh +php tools/pcov_manifest.php create \ + /path/to/manifests/manifest-v1.pcov \ + "$DEPLOYMENT_ID" \ + /path/to/bootstrap/*.pcov +``` -Differences in Reporting -======================== +The reference suite must load the complete coverage scope. Keep old manifest +generations while request records still reference them. -There are subtle differences between Xdebug and PCOV in reporting: Both Xdebug and PCOV perform branch analysis in order to detect executable code. Xdebug has custom written (very mature, proven) analysis, while PCOV uses the very well proven control flow graph from Optimizer. They generate comparably accurate reports, while phpdbg uses less robust detection of executable code and generates reports with known defects. One such defect in phpdbg is this: +### 3. Export validated request hits ```php -/* 2 */ function foo($bar) { -/* 3 */ if ($bar) { -/* 4 */ return true; -/* 5 */ } -/* 6 */ } +$result = pcov\export( + '/path/to/records/request-42.pcov', + '/path/to/manifests/manifest-v1.pcov', + getenv('DEPLOYMENT_ID') ?: throw new RuntimeException('Missing deployment identity') +); + +if ($result === false) { + throw new RuntimeException('PCOV publication failed'); +} + +// "hit-only" when validation succeeds, "full-fallback" otherwise. +error_log("PCOV export mode: {$result['mode']} ({$result['reason']})"); ``` -phpdbg will detect that this function is 100% covered when the first control path is taken, `if ($bar)`, because it cannot correctly detect which implicit return paths inserted by Zend at compile time are executable, and so chooses to ignore them all. While this may seem like a trivial difference to some, it means that the reports generated by phpdbg are not completely trustworthy. +PCOV validates the runtime identity, every selected loaded file, SHA-256 source +fingerprints, and every positive hit. New, changed, unreadable, raced, unknown, +or incompatible code synchronously produces a complete full-fallback record. +Temporary files are unique and publication uses atomic rename. -While the accuracy of Xdebug and PCOV are comparable, the reports they generate are not precisely the same, one such example is the `switch` construct: +### 4. Merge into the normal PCOV representation -```php -/* 2 */ switch ($condition) { -/* 3 */ case 1: -/* 4 */ return "PHP rox!"; -/* 5 */ } +```sh +php tools/pcov_manifest.php merge \ + /path/to/coverage.php \ + /path/to/manifests/manifest-v1.pcov \ + /path/to/records/*.pcov ``` -From PHP 7.2.15 and PCOV 1.0, PCOV will detect the executability of the cases inside the switch body correctly, but will not detect line 2 (with the `switch` statement) as executable because Zend didn't output an executable opcode on that line. Xdebug's custom analysis doesn't use the same method and so will show an extra executable line on 2. Pre 7.2.15 and PCOV 1.0, the coverage of some switches is questionable as a result of the way Zend constructs the opcodes in the body of the switch - it may not execute them depending on a jump table optimization. +`coverage.php` returns the normal `file => line => -1|1` array. The merger is +deterministic and idempotent. It rejects corrupt records, mismatched identities, +unknown files or lines, and incomplete fallback cohorts. A fallback is never +silently reported as complete; rebuild the immutable manifest from a complete +full-discovery suite before publishing the next generation. + +See [Manifest and record architecture](docs/manifest-and-records.md) for the +identity, validation, fallback, and binary-format contract. -While Xdebug and PCOV both do the same kind of analysis of code, Xdebug is currently able to do more with that information than just generate accurate line coverage reports, it has path coverage and PCOV does not, although path coverage is not yet implemented (and probably won't be) by CodeCoverage. +## Added API -Differences in Performance -========================== +```php +pcov\export( + string $path, + ?string $manifest = null, + ?string $deploymentId = null, + int $type = pcov\all, + array $filter = [] +): array|false; + +pcov\export_stats(): array; +``` + +With no manifest, `export()` writes complete native coverage. Passing a +deployment ID tags that full record for manifest creation. With a manifest and +deployment ID, it writes validated hits or a synchronous full fallback. +`export_stats()` exposes diagnostics used by the benchmark suite. + +Coverage records use a fixed-width, big-endian, checksummed format (version 1) +with strict bounds checking. Records are intentionally tied to an exact +`PHP_VERSION_ID`; regenerate manifests when changing PHP or PCOV versions. + +## Official PCOV comparison + +### Representative E2E workload + +The primary comparison uses a representative large PHP e-commerce application +under PHP 8.3.31 NTS. One Cypress runner executes 20 read-only E2E tests with +120 cache-busted storefront navigations through a one-child PHP-FPM pool. +Blackfire and SPX are disabled in both otherwise identical images. Three +accepted rounds rotate mode order. + +Official PCOV 1.0.12 uses php-code-coverage's normal `PcovDriver`, PHP object +writer, and one atomic request file. Large-codebase PCOV 2.0 uses the identical +file filter and collection intervals, one prebuilt immutable manifest, +synchronous validation, and atomic hit records. The “finalized” column includes +the post-Cypress deterministic merge. + +| Mode | Cypress wall median / p95 | Finalized wall median / p95 | FPM CPU median / p95 | Request export median / p95 | Output median | +|---|---:|---:|---:|---:|---:| +| Official PCOV 1.0.12 | 316.245 / 317.229 s | 316.766 / 317.717 s | 208.433 / 210.015 s | 519.1 / 1090.6 ms | 59.34 MB | +| Large-codebase PCOV 2.0 | 234.870 / 236.068 s | 236.125 / 237.366 s | 112.699 / 115.070 s | 1.85 / 2.95 ms | 3.10 MB | + +The fork reduced Cypress wall time by **25.7%**, finalized wall time by +**25.5%**, FPM CPU by **45.9%**, pooled per-request export latency by **99.6%**, +peak cgroup memory by **21.9%**, minor faults by **20.9%**, and request-record +output by **94.8%**. Its final merge was slower (1.255 s median versus 0.489 s), +and that cost is included in finalized wall time. + +All six accepted mode runs reconstructed exactly 208 files and 4,341 +executable lines with normalized SHA-256 +`6b1280edc9ecddd14657907a43b0cbed63bebb69dba81b280931c5c43d3c2903`. +All 435 fork exports were validated hit-only matches; no fallback was accepted. +With only three whole-scenario samples, the reported scenario p95 is the +nearest-rank observed maximum, not a high-confidence tail estimate. Detailed +application names, routes, and local infrastructure are intentionally omitted. + +### Reproducible synthetic workload + +The public synthetic benchmark builds both extensions in the same PHP 8.5 +container. It generates 1,500 branch-heavy files, 64,500 executable lines, ten +request-isolated slices, and ten interleaved rounds: + +| Mode | Total wall median / p95 | CPU median / p95 | Request export median / p95 | Output per round | +|---|---:|---:|---:|---:| +| Official PCOV 1.0.12 | 2.031 / 2.166 s | 1.992 / 2.148 s | 344.2 / 361.2 ms | 694,580 B | +| Large-codebase PCOV 2.0 | 2.114 / 2.490 s | 2.006 / 2.339 s | 73.9 / 97.5 ms | 517,240 B | + +Here the fork reduced request export by **78.5%**, output by **25.5%**, and +minor faults by **7.7%**, but the run was too short to amortize validation and +the PHP finalizer: wall was **4.1% higher** and CPU **0.7% higher**. This is why +the real multi-minute workload is the primary result. + +All synthetic rounds reconstructed the same 1,500 files and 64,500 lines with +SHA-256 +`2bf06aab6b79a763167a9c5d05947f6876a0ab5d738f78de1dfe8885bdff3dea`. +Reproduce that cohort with: + +```sh +benchmark/large-codebase/run.sh +``` -The differences in performance of Xdebug and PCOV are not slight. Xdebug is first and foremost a debugging extension, and when you load it, you incur the overhead of a debugger even when it's disabled. PCOV is less than 1000 lines of code (not including CFG) and doesn't have anything like the overhead of a debugger. +Raw JSONL is written under ignored `benchmark/results/`. Both results are +workload-specific. Manifest generation is excluded from warm rows: the +optimization pays off only when an immutable manifest is reused across enough +requests, while code changes or uncertain validation synchronously fall back +to complete discovery. + +## Operational limits + +- Use this extension only in a dedicated coverage SAPI. Like official PCOV, + its executor hooks are not compatible with Xdebug, phpdbg coverage, or other + tools replacing the same hooks. +- Sources and active manifests must be immutable. Publish new generations to a + new path by atomic rename; never truncate an active manifest. +- Compile-side fingerprints are reused only while canonical path, device, + inode, mode, size, nanosecond mtime, and nanosecond ctime still match. A + mismatch rereads and hashes the source; uncertainty falls back fully. +- Eval code, unsupported streams, unreadable files, and source races fail + closed. +- The merger needs an authoritative complete reference suite to account for + files no request loaded and files removed between deployments. +- This release supports Linux NTS only. Export is synchronous and all mutable + coverage state remains process-local. diff --git a/benchmark/large-codebase/README.md b/benchmark/large-codebase/README.md new file mode 100644 index 0000000..9815021 --- /dev/null +++ b/benchmark/large-codebase/README.md @@ -0,0 +1,31 @@ +# Official PCOV versus large-codebase PCOV + +This benchmark generates 1,500 PHP files with 40 source-line statements per +file. Each statement contains an 80-term short-circuit expression so CFG work +is substantial without inflating the executable-line inventory. Ten +interleaved rounds run ten isolated request processes against official PCOV +1.0.12 and this fork; each request loads 150 files and each complete round +covers all files. The official path calls `pcov\collect()`, serializes the PHP +array and publishes one file per request. The fork reuses one immutable +manifest and calls `pcov\export()` for validated hit-only records. + +Run it from the repository root: + +```sh +benchmark/large-codebase/run.sh +``` + +The script builds both extensions with PHP 8.5 in the same container image. +Raw JSONL and a JSON summary are written below `benchmark/results/`, which is +ignored by Git. Every accepted round must produce the same normalized coverage +SHA-256 in both modes. + +The accepted PHP 8.5 cohort produced 1,500 files, 64,500 executable lines, and +normalized SHA-256 +`2bf06aab6b79a763167a9c5d05947f6876a0ab5d738f78de1dfe8885bdff3dea`. +Median request-export time was 344.2 ms for official PCOV and 73.9 ms for this +fork. Median end-to-end benchmark time was 2.031 s and 2.114 s respectively; +the slower PHP final merge offset the request-path saving in this synthetic +cohort. This intentionally short benchmark is not representative of a +multi-minute browser suite. See the root README for the primary real-E2E +comparison, complete tables, and interpretation. diff --git a/benchmark/large-codebase/e2e-summary.json b/benchmark/large-codebase/e2e-summary.json new file mode 100644 index 0000000..af4e2dc --- /dev/null +++ b/benchmark/large-codebase/e2e-summary.json @@ -0,0 +1,40 @@ +{ + "schema": 1, + "workload": "representative large PHP e-commerce E2E workload", + "php": "8.3.31 NTS", + "official_pcov": "1.0.12", + "fork": "2.0.0", + "cypress_runners": 1, + "fpm_children": 1, + "tests_per_round": 20, + "navigations_per_round": 120, + "accepted_rounds_per_mode": 3, + "scenario_p95_method": "nearest-rank observed maximum", + "official": { + "cypress_wall_seconds": {"median": 316.245, "p95": 317.229}, + "finalized_wall_seconds": {"median": 316.766, "p95": 317.717}, + "fpm_cpu_seconds": {"median": 208.433, "p95": 210.015}, + "request_export_milliseconds": {"median": 519.1, "p95": 1090.6}, + "merge_seconds_median": 0.489, + "peak_cgroup_bytes_median": 416358400, + "minor_faults_median": 1036936, + "output_bytes_median": 59335207 + }, + "large_codebase": { + "cypress_wall_seconds": {"median": 234.870, "p95": 236.068}, + "finalized_wall_seconds": {"median": 236.125, "p95": 237.366}, + "fpm_cpu_seconds": {"median": 112.699, "p95": 115.070}, + "request_export_milliseconds": {"median": 1.85, "p95": 2.95}, + "merge_seconds_median": 1.255, + "peak_cgroup_bytes_median": 325193728, + "minor_faults_median": 819762, + "output_bytes_median": 3102120, + "validated_hit_records": 435, + "full_fallbacks": 0 + }, + "coverage": { + "files": 208, + "executable_lines": 4341, + "normalized_sha256": "6b1280edc9ecddd14657907a43b0cbed63bebb69dba81b280931c5c43d3c2903" + } +} diff --git a/benchmark/large-codebase/generate.php b/benchmark/large-codebase/generate.php new file mode 100644 index 0000000..994c8f3 --- /dev/null +++ b/benchmark/large-codebase/generate.php @@ -0,0 +1,24 @@ + ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); + if (!is_resource($process)) { + throw new RuntimeException('Cannot start benchmark process'); + } + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + $status = proc_close($process); + return [$status, $stdout, $stderr]; +}; +$extensionCommand = static function (string $extension, string $sourceDirectory): array { + return [ + PHP_BINARY, '-n', '-d', "extension={$extension}", '-d', 'pcov.enabled=1', + '-d', "pcov.directory={$sourceDirectory}", + ]; +}; + +[$status, , $stderr] = $run([PHP_BINARY, $generator, $sources, (string) $fileCount, (string) $statements]); +if ($status !== 0) { + throw new RuntimeException("Fixture generation failed: {$stderr}"); +} + +$deploymentId = hash('sha256', 'pcov-public-large-codebase-benchmark-v1'); +$manifest = $root . '/manifest.pcov'; +$coldStarted = hrtime(true); +$fullPaths = []; +$bootstrapCpuUs = 0; +$bootstrapMaxRssKb = 0; +for ($request = 0; $request < $requests; $request++) { + $full = sprintf('%s/full-%04d.pcov', $root, $request); + $bootstrapCommand = $extensionCommand($options['fork-so'], $sources); + array_push( + $bootstrapCommand, + '-d', 'pcov.large_codebase=1', $worker, 'full', $sources, $full, + '0', (string) ($request * $filesPerRequest), (string) $filesPerRequest, + '', $deploymentId + ); + [$status, $stdout, $stderr] = $run($bootstrapCommand); + if ($status !== 0) { + throw new RuntimeException("Manifest bootstrap failed with status {$status}: {$stderr}"); + } + $bootstrapRow = json_decode(trim($stdout), true, flags: JSON_THROW_ON_ERROR); + $bootstrapCpuUs += $bootstrapRow['cpu_us']; + $bootstrapMaxRssKb = max($bootstrapMaxRssKb, $bootstrapRow['max_rss_kb']); + $fullPaths[] = $full; +} +pcov_manifest_bootstrap($manifest, $deploymentId, $fullPaths); +$coldManifestNs = hrtime(true) - $coldStarted; + +$rawPath = $options['output']; +$failurePath = $rawPath . '.failures.jsonl'; +$results = []; +for ($round = 0; $round < $rounds; $round++) { + $order = $round % 2 === 0 ? ['official', 'large-codebase'] : ['large-codebase', 'official']; + foreach ($order as $mode) { + $directory = "{$root}/{$mode}-{$round}"; + mkdir($directory, 0777, true); + $extension = $mode === 'official' ? $options['official-so'] : $options['fork-so']; + $rows = []; + $paths = []; + for ($request = 0; $request < $requests; $request++) { + $path = sprintf('%s/request-%04d.pcov', $directory, $request); + $command = $extensionCommand($extension, $sources); + if ($mode === 'large-codebase') { + array_push($command, '-d', 'pcov.large_codebase=1'); + } + array_push( + $command, + $worker, $mode, $sources, $path, (string) ($request + 1), + (string) ($request * $filesPerRequest), (string) $filesPerRequest, + $manifest, $deploymentId + ); + [$status, $stdout, $stderr] = $run($command); + if ($status !== 0) { + file_put_contents($failurePath, json_encode([ + 'round' => $round, 'request' => $request, 'mode' => $mode, + 'status' => $status, 'stderr' => $stderr, + ], JSON_THROW_ON_ERROR) . "\n", FILE_APPEND); + throw new RuntimeException("{$mode} round {$round} request {$request} failed: {$stderr}"); + } + $rows[] = json_decode(trim($stdout), true, flags: JSON_THROW_ON_ERROR); + $paths[] = $path; + } + + $mergeStarted = hrtime(true); + if ($mode === 'official') { + $coverage = []; + foreach ($paths as $path) { + $requestCoverage = unserialize(file_get_contents($path), ['allowed_classes' => false]); + foreach ($requestCoverage as $file => $lines) { + foreach ($lines as $line => $value) { + if (!isset($coverage[$file][$line]) || $value === 1) { + $coverage[$file][$line] = $value; + } + } + } + } + $coverage = pcov_coverage_normalize($coverage); + } else { + $merged = pcov_manifest_merge($manifest, $paths); + if (!$merged['coverage_complete'] || $merged['requires_full_discovery']) { + throw new RuntimeException('Manifest merge is incomplete'); + } + $coverage = $merged['coverage']; + } + $mergeNs = hrtime(true) - $mergeStarted; + $result = [ + 'round' => $round, + 'mode' => $mode, + 'php' => $rows[0]['php'], + 'pcov' => $rows[0]['pcov'], + 'files' => count($coverage), + 'executable_lines' => array_sum(array_map('count', $coverage)), + 'requests' => $requests, + 'wall_ns' => array_sum(array_column($rows, 'wall_ns')) + $mergeNs, + 'export_ns' => array_sum(array_column($rows, 'export_ns')), + 'merge_ns' => $mergeNs, + 'cpu_us' => array_sum(array_column($rows, 'cpu_us')), + 'minor_faults' => array_sum(array_column($rows, 'minor_faults')), + 'major_faults' => array_sum(array_column($rows, 'major_faults')), + 'max_rss_kb' => max(array_column($rows, 'max_rss_kb')), + 'output_bytes' => array_sum(array_column($rows, 'output_bytes')), + 'coverage_hash' => pcov_coverage_hash($coverage), + ]; + $results[] = $result; + file_put_contents($rawPath, json_encode($result, JSON_THROW_ON_ERROR) . "\n", FILE_APPEND); + } +} + +$hashes = array_values(array_unique(array_column($results, 'coverage_hash'))); +if (count($hashes) !== 1) { + throw new RuntimeException('Official and large-codebase normalized coverage hashes differ'); +} +$percentile = static function (array $values, float $fraction): int { + sort($values, SORT_NUMERIC); + return $values[(int) ceil(count($values) * $fraction) - 1]; +}; +$summary = [ + 'environment' => [ + 'php' => PHP_VERSION, + 'kernel' => implode(' ', [php_uname('s'), php_uname('r'), php_uname('v'), php_uname('m')]), + 'files' => $fileCount, + 'statements_per_file' => $statements, + 'files_per_request' => $filesPerRequest, + 'requests_per_round' => $requests, + 'rounds' => $rounds, + 'cold_manifest_ns' => $coldManifestNs, + 'cold_manifest_cpu_us' => $bootstrapCpuUs, + 'cold_manifest_max_rss_kb' => $bootstrapMaxRssKb, + 'manifest_bytes' => filesize($manifest), + ], + 'coverage_hash' => $hashes[0], + 'modes' => [], +]; +foreach (['official', 'large-codebase'] as $mode) { + $rows = array_values(array_filter($results, static fn (array $row): bool => $row['mode'] === $mode)); + foreach (['wall_ns', 'cpu_us', 'export_ns', 'merge_ns', 'output_bytes', 'max_rss_kb', 'minor_faults'] as $metric) { + $values = array_column($rows, $metric); + $summary['modes'][$mode][$metric] = [ + 'median' => $percentile($values, 0.5), + 'p95' => $percentile($values, 0.95), + ]; + } + $summary['modes'][$mode]['files'] = $rows[0]['files']; + $summary['modes'][$mode]['executable_lines'] = $rows[0]['executable_lines']; + $summary['modes'][$mode]['pcov'] = $rows[0]['pcov']; +} + +$summaryPath = preg_replace('/\.jsonl$/', '', $rawPath) . '-summary.json'; +file_put_contents($summaryPath, json_encode($summary, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT) . "\n"); +echo json_encode($summary, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), "\n"; diff --git a/benchmark/large-codebase/run.sh b/benchmark/large-codebase/run.sh new file mode 100755 index 0000000..6edc571 --- /dev/null +++ b/benchmark/large-codebase/run.sh @@ -0,0 +1,34 @@ +#!/bin/sh +set -eu + +repository=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) +output=${1:-"$repository/benchmark/results/upstream-vs-large.jsonl"} +workspace=$(mktemp -d "${TMPDIR:-/tmp}/pcov-public-benchmark.XXXXXX") +trap 'rm -rf "$workspace"' EXIT HUP INT TERM +host_uid=$(id -u) +host_gid=$(id -g) + +git clone --quiet --depth 1 --branch v1.0.12 https://github.com/krakjoe/pcov.git "$workspace/official" + +docker run --rm -e HOST_UID="$host_uid" -e HOST_GID="$host_gid" \ + -v "$workspace/official":/src -w /src php:8.5-cli sh -lc ' + phpize && ./configure --enable-pcov && make -j"$(nproc)" + chown -R "$HOST_UID:$HOST_GID" /src +' +docker run --rm -e HOST_UID="$host_uid" -e HOST_GID="$host_gid" \ + -v "$repository":/src -w /src php:8.5-cli sh -lc ' + phpize --clean >/dev/null 2>&1 || true + phpize && CFLAGS="-O2 -Wall -Wextra -Werror" ./configure --enable-pcov && make -j"$(nproc)" + chown -R "$HOST_UID:$HOST_GID" /src +' + +mkdir -p "$(dirname -- "$output")" +docker run --rm \ + -v "$repository":/fork \ + -v "$workspace/official":/official \ + -w /fork \ + php:8.5-cli \ + php benchmark/large-codebase/run.php \ + --official-so=/official/modules/pcov.so \ + --fork-so=/fork/modules/pcov.so \ + --output="${output#$repository/}" diff --git a/benchmark/large-codebase/worker.php b/benchmark/large-codebase/worker.php new file mode 100644 index 0000000..83b2e26 --- /dev/null +++ b/benchmark/large-codebase/worker.php @@ -0,0 +1,93 @@ + (($usage['ru_utime.tv_sec'] + $usage['ru_stime.tv_sec']) * 1_000_000) + + $usage['ru_utime.tv_usec'] + $usage['ru_stime.tv_usec'], + 'minor_faults' => $usage['ru_minflt'], + 'major_faults' => $usage['ru_majflt'], + 'max_rss_kb' => $usage['ru_maxrss'], + ]; +}; + +$before = $usage(); +$wallStarted = hrtime(true); +$files = glob($sourceDirectory . '/*.php'); +sort($files, SORT_STRING); +$selected = []; +$count = count($files); +$take = min((int) $limit, $count); +for ($index = 0; $index < $take; $index++) { + $selected[] = $files[((int) $offset + $index) % $count]; +} +$files = $selected; +foreach ($files as $file) { + require $file; +} + +$workloadChecksum = 0; +pcov\clear(); +pcov\start(); +foreach ($files as $file) { + $function = basename($file, '.php'); + $workloadChecksum ^= $function((int) $seed); +} +pcov\stop(); + +$exportStarted = hrtime(true); +$resultMode = $mode; +if ($mode === 'official') { + pcov_atomic_publish($outputPath, serialize(pcov\collect())); +} elseif ($mode === 'full') { + if ($deploymentId === null) { + throw new RuntimeException('Bootstrap requires a deployment identity'); + } + $result = pcov\export($outputPath, null, $deploymentId); + if (!is_array($result) || $result['mode'] !== 'full') { + throw new RuntimeException('Native full bootstrap export failed'); + } + $resultMode = 'full'; +} else { + if ($manifest === null || $deploymentId === null) { + throw new RuntimeException('Large-codebase request requires manifest and deployment identity'); + } + $result = pcov\export($outputPath, $manifest, $deploymentId); + if (!is_array($result) || $result['mode'] !== 'hit-only') { + throw new RuntimeException('Warm manifest request did not use hit-only export'); + } + $resultMode = $result['mode']; +} +$exportNs = hrtime(true) - $exportStarted; +$after = $usage(); + +echo json_encode([ + 'mode' => $mode, + 'result_mode' => $resultMode, + 'php' => PHP_VERSION, + 'pcov' => constant('pcov\\version'), + 'loaded_files' => count($files), + 'wall_ns' => hrtime(true) - $wallStarted, + 'export_ns' => $exportNs, + 'cpu_us' => $after['cpu_us'] - $before['cpu_us'], + 'minor_faults' => $after['minor_faults'] - $before['minor_faults'], + 'major_faults' => $after['major_faults'] - $before['major_faults'], + 'max_rss_kb' => $after['max_rss_kb'], + 'output_bytes' => filesize($outputPath), + 'workload_checksum' => $workloadChecksum, +], JSON_THROW_ON_ERROR), "\n"; diff --git a/cfg/704/zend_cfg.c b/cfg/704/zend_cfg.c index dc07107..c210bfa 100644 --- a/cfg/704/zend_cfg.c +++ b/cfg/704/zend_cfg.c @@ -14,7 +14,7 @@ +----------------------------------------------------------------------+ | Authors: Dmitry Stogov | +----------------------------------------------------------------------+ - + This source file has been adapted for pcov so that the CFG from O+ is standalone */ diff --git a/composer.json b/composer.json index ed09d4e..fcd42f3 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,25 @@ { - "name": "pecl/pcov", + "name": "vitaliyboyko/pcov", "type": "php-ext", "license": "PHP-3.01", - "description": "PCOV - CodeCoverage compatible driver for PHP", + "description": "PCOV coverage driver optimized for large PHP test suites", + "keywords": ["coverage", "phpunit", "pcov", "testing"], + "homepage": "https://github.com/VitaliyBoyko/pcov", + "support": { + "issues": "https://github.com/VitaliyBoyko/pcov/issues", + "source": "https://github.com/VitaliyBoyko/pcov" + }, "require": { - "php": ">= 7.1.0" + "php": ">=8.3 <8.6" + }, + "conflict": { + "pecl/pcov": "*" }, "php-ext": { "extension-name": "pcov", + "support-zts": false, + "support-nts": true, + "os-families": ["linux"], "configure-options": [ { "name": "enable-pcov", diff --git a/config.m4 b/config.m4 index 634562e..f520336 100644 --- a/config.m4 +++ b/config.m4 @@ -7,29 +7,19 @@ PHP_ARG_ENABLE(pcov, whether to enable php coverage support, if test "$PHP_PCOV" != "no"; then PHP_VERSION=$($PHP_CONFIG --vernum) + AC_CHECK_FUNCS([clock_gettime getrusage]) + if test "$ac_cv_func_clock_gettime" = "yes" -a \ + "$ac_cv_func_getrusage" = "yes"; then + AC_DEFINE([HAVE_PCOV_NATIVE_EXPORT], [1], + [Define to 1 when synchronous native export is supported]) + fi + AC_MSG_CHECKING(PHP version) - if test $PHP_VERSION -gt 80099; then - PHP_NEW_EXTENSION(pcov, pcov.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -std=c99) - else - if test $PHP_VERSION -lt 70100; then - AC_MSG_ERROR([pcov supports PHP 7.1+]) - elif test $PHP_VERSION -lt 70200; then - AC_MSG_RESULT(7.1) - PHP_PCOV_CFG_VERSION=701 - elif test $PHP_VERSION -lt 70300; then - AC_MSG_RESULT(7.2) - PHP_PCOV_CFG_VERSION=702 - elif test $PHP_VERSION -lt 70400; then - AC_MSG_RESULT(7.3) - PHP_PCOV_CFG_VERSION=703 - else - AC_MSG_RESULT(7.4+) - PHP_PCOV_CFG_VERSION=704 - fi - - PHP_NEW_EXTENSION(pcov, pcov.c cfg/$PHP_PCOV_CFG_VERSION/zend_cfg.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) - PHP_ADD_BUILD_DIR($ext_builddir/cfg/$PHP_PCOV_CFG_VERSION, 1) - PHP_ADD_INCLUDE($ext_srcdir/cfg/$PHP_PCOV_CFG_VERSION) + if test $PHP_VERSION -lt 80300 -o $PHP_VERSION -ge 80600; then + AC_MSG_ERROR([this PCOV release supports PHP 8.3, 8.4, and 8.5]) fi + + AC_MSG_RESULT($PHP_VERSION) + PHP_NEW_EXTENSION(pcov, pcov.c pcov_dump.c pcov_fingerprint.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -std=c99) fi diff --git a/config.w32 b/config.w32 index 89d4ac7..80eb0dc 100644 --- a/config.w32 +++ b/config.w32 @@ -9,8 +9,8 @@ if (PHP_PCOV != "no") { } if (PHP_VERSION > 8 || PHP_VERSION == 8 && PHP_MINOR_VERSION >= 1) { - EXTENSION("pcov", "pcov.c", PHP_PCOV_SHARED, - "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); + EXTENSION("pcov", "pcov.c pcov_dump.c pcov_fingerprint.c", PHP_PCOV_SHARED, + "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); } else { var cfg_dir; @@ -20,12 +20,12 @@ if (PHP_PCOV != "no") { cfg_dir = "704"; } - EXTENSION("pcov", "pcov.c", PHP_PCOV_SHARED, - "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 /I" + + EXTENSION("pcov", "pcov.c pcov_dump.c pcov_fingerprint.c", PHP_PCOV_SHARED, + "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 /I" + configure_module_dirname + "/cfg/" + cfg_dir); ADD_SOURCES( configure_module_dirname + "/cfg/" + cfg_dir, - "zend_cfg.c", + "zend_cfg.c", "pcov" ); } diff --git a/docs/manifest-and-records.md b/docs/manifest-and-records.md new file mode 100644 index 0000000..8e0348f --- /dev/null +++ b/docs/manifest-and-records.md @@ -0,0 +1,103 @@ +# Manifest and record architecture + +## Data flow + +PCOV records positive `(filename, line)` hits while PHP executes and retains +selected op-arrays. Recording does not run control-flow-graph discovery. + +`pcov\export()` has two synchronous paths: + +```text +no manifest + -> complete CFG discovery + -> full coverage record + +immutable manifest + -> validate compatibility and every retained source + -> validate every positive hit against executable lines + match -> hit-only record + uncertainty -> complete CFG discovery and full-fallback record +``` + +Native full uses the same forced discovery semantics as `pcov\collect()` and +overlays current positive hits. No request work leaves the PHP process. + +## Identity and source validation + +The environment identity is SHA-256 over canonical JSON containing: + +- PHP version/build mode, integer size, and OS family; +- PCOV and coverage-record versions; +- application revision; +- dependency-lock identity; +- enabled-module/configuration identity; +- generated-code identity; +- relevant source-tree identity. + +Every manifest file has a canonical absolute path, a SHA-256 source +fingerprint, and sorted executable line numbers. PCOV fingerprints selected +regular files after compilation. Validated export can reuse that SHA only when +the canonical path and strong stat identity still match. Otherwise it performs +an open/fstat/read/fstat/path-stat hash. New files, content changes, atomic +replacement, deletion, unreadable input, and detected races fail closed. + +Loaded files without positive hits are validated too. A positive-hit-only +inventory would miss new but unexecuted code. + +## Binary records + +All fixed-width fields use big-endian byte order. The 48-byte header contains: + +| Field | Purpose | +|---|---| +| Magic | Identifies `PCOVREC` coverage records | +| Format version | Selects the record layout | +| Header size | Rejects incompatible framing | +| Record type | Full coverage, manifest, or validated hits | +| Flags | Must contain supported values | +| Payload length | Bounds the exact payload | +| CRC-32 | Detects corruption and truncation | +| `PHP_VERSION_ID` | Enforces runtime compatibility | +| Compatibility version | Fences semantic changes | +| Reserved | Must be zero | + +Payloads contain deployment and manifest SHA-256 identities, a deterministic +fallback reason, fixed-width counts and line numbers, length-prefixed canonical +paths, and source SHA-256 values where required. They contain no pointers, +`size_t`, Zend structs, or process-memory representations. + +Readers reject unknown versions/types/flags, oversized counts and paths, +integer overflow, duplicate or unsorted files and lines, invalid coverage +values, checksum failures, truncation, and trailing bytes before iterating +untrusted lengths. + +## Fallback and publication + +Fallback is synchronous and complete for the current request. It is selected +for unavailable or corrupt manifests, identity mismatch, new or modified +files, source uncertainty, and positive hits absent from the manifest. + +Writers create a unique temporary file with exclusive creation, write the +complete record, close it, and atomically rename it. A failed open, write, +close, or rename removes the temporary file and leaves in-memory hits available +for retry. + +## Immutable merge lifecycle + +```text +manifest N + matching hits + full-fallback records + -> deterministic coverage and candidate delta + -> authoritative inventory/full discovery + -> atomically publish manifest N+1 at a new path +``` + +The merger requires exact deployment and manifest identities. Hit records are +idempotently overlaid on manifest executable lines. A request-local fallback +cannot prove deployment-wide removals or never-loaded files, so it marks the +candidate incomplete until a complete reference suite or authoritative source +inventory rebuilds the manifest. Manifest N is never changed in place and old +records are never interpreted against N+1. + +The safety boundary is deployment orchestration: PCOV validates the supplied +identities and file contents, but cannot prove that caller-provided deployment +digests or an external inventory are authoritative. diff --git a/pcov.c b/pcov.c index 06e60ee..7013fc8 100644 --- a/pcov.c +++ b/pcov.c @@ -39,6 +39,7 @@ #include "zend_vm_opcodes.h" #include "php_pcov.h" +#include "pcov_fingerprint.h" #define PCOV_FILTER_ALL 0 #define PCOV_FILTER_INCLUDE 1 @@ -57,7 +58,7 @@ #define GC_SET_REFCOUNT(ref, rc) (GC_REFCOUNT(ref) = (rc)) #endif -static zend_always_inline bool php_pcov_api_enabled(void) { +PHP_PCOV_API bool php_pcov_api_enabled(void) { const char* env = getenv("PCOV_ENABLED"); if (env) { @@ -125,6 +126,10 @@ PHP_INI_BEGIN() "pcov.enabled", "1", PHP_INI_SYSTEM, OnUpdateBool, ini.enabled, zend_pcov_globals, pcov_globals) + STD_PHP_INI_BOOLEAN( + "pcov.large_codebase", "1", + PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateBool, + ini.large_codebase, zend_pcov_globals, pcov_globals) STD_PHP_INI_ENTRY ( "pcov.directory", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, @@ -332,6 +337,31 @@ zend_op_array* php_pcov_compile_file(zend_file_handle *fh, int type) { /* {{{ */ result->filename, result, sizeof(zend_op_array)); + if (PCG(ini.large_codebase)) { + unsigned char fingerprint[PHP_PCOV_FINGERPRINT_SIZE]; + php_pcov_fingerprint_identity_t identity; + uint64_t started = php_pcov_dump_now(); + int error = 0; + + if (php_pcov_fingerprint_file( + result->filename, fingerprint, &error, + php_pcov_dump_benchmark_enabled() ? + &PCG(compile_fingerprint_metrics) : NULL, + &identity)) { + zval stored; + zend_string *record = zend_string_alloc( + PHP_PCOV_FINGERPRINT_SIZE + sizeof(identity), 0); + memcpy(ZSTR_VAL(record), fingerprint, PHP_PCOV_FINGERPRINT_SIZE); + memcpy(ZSTR_VAL(record) + PHP_PCOV_FINGERPRINT_SIZE, + &identity, sizeof(identity)); + ZSTR_VAL(record)[ZSTR_LEN(record)] = '\0'; + ZVAL_STR(&stored, record); + zend_hash_update(&PCG(fingerprints), result->filename, &stored); + } + PCG(compile_fingerprint_ns) += php_pcov_dump_now() - started; + PCG(compile_fingerprint_files)++; + } + #if PHP_VERSION_ID >= 70400 if (result->refcount) { (*result->refcount)++; @@ -471,6 +501,9 @@ static zend_always_inline void php_pcov_setup_exclude(char *exclude) { /* {{{ */ */ PHP_RINIT_FUNCTION(pcov) { + (void) type; + (void) module_number; + #if defined(COMPILE_DL_PCOV) && defined(ZTS) ZEND_TSRMLS_CACHE_UPDATE(); #endif @@ -487,6 +520,11 @@ PHP_RINIT_FUNCTION(pcov) zend_hash_init(&PCG(wants), INI_INT("pcov.initial.files"), NULL, NULL, 0); zend_hash_init(&PCG(discovered), INI_INT("pcov.initial.files"), NULL, ZVAL_PTR_DTOR, 0); zend_hash_init(&PCG(covered), INI_INT("pcov.initial.files"), NULL, php_pcov_covered_dtor, 0); + zend_hash_init(&PCG(fingerprints), INI_INT("pcov.initial.files"), NULL, ZVAL_PTR_DTOR, 0); + PCG(compile_fingerprint_ns) = 0; + PCG(compile_fingerprint_files) = 0; + memset(&PCG(compile_fingerprint_metrics), 0, + sizeof(PCG(compile_fingerprint_metrics))); php_pcov_setup_directory(INI_STR("pcov.directory")); php_pcov_setup_exclude(INI_STR("pcov.exclude")); @@ -504,6 +542,8 @@ PHP_RINIT_FUNCTION(pcov) PCG(last) = NULL; PCG(next) = NULL; + php_pcov_dump_rinit(); + return SUCCESS; } /* }}} */ @@ -512,7 +552,14 @@ PHP_RINIT_FUNCTION(pcov) */ PHP_RSHUTDOWN_FUNCTION(pcov) { - if (!php_pcov_api_enabled() || CG(unclean_shutdown)) { + (void) type; + (void) module_number; + + if (!php_pcov_api_enabled()) { + return SUCCESS; + } + + if (CG(unclean_shutdown)) { return SUCCESS; } @@ -522,6 +569,7 @@ PHP_RSHUTDOWN_FUNCTION(pcov) zend_hash_destroy(&PCG(discovered)); zend_hash_destroy(&PCG(waiting)); zend_hash_destroy(&PCG(covered)); + zend_hash_destroy(&PCG(fingerprints)); zend_arena_destroy(PCG(mem)); @@ -549,6 +597,7 @@ PHP_MINFO_FUNCTION(pcov) char info[64]; char *directory = INI_STR("pcov.directory"); char *exclude = INI_STR("pcov.exclude"); + (void) zend_module; php_info_print_table_start(); @@ -558,6 +607,9 @@ PHP_MINFO_FUNCTION(pcov) php_info_print_table_row(2, "PCOV version", PHP_PCOV_VERSION); + php_info_print_table_row(2, + "pcov.large_codebase", + INI_BOOL("pcov.large_codebase") ? "enabled" : "disabled"); php_info_print_table_row(2, "pcov.directory", directory && *directory ? directory : (PCG(directory) ? ZSTR_VAL(PCG(directory)) : "auto")); @@ -581,7 +633,10 @@ PHP_MINFO_FUNCTION(pcov) } /* }}} */ -static zend_always_inline void php_pcov_report(php_coverage_t *coverage, zval *filter) { /* {{{ */ +static zend_always_inline void php_pcov_report( + php_coverage_t *coverage, + zval *filter, + php_pcov_dump_stats_t *stats) { /* {{{ */ zval *table; zval *hit; @@ -590,6 +645,9 @@ static zend_always_inline void php_pcov_report(php_coverage_t *coverage, zval *f } do { + if (stats) { + stats->hit_entries++; + } if ((table = zend_hash_find(Z_ARRVAL_P(filter), coverage->file))) { if ((hit = zend_hash_index_find(Z_ARRVAL_P(table), coverage->line))) { Z_LVAL_P(hit) = PHP_PCOV_COVERED; @@ -598,7 +656,11 @@ static zend_always_inline void php_pcov_report(php_coverage_t *coverage, zval *f } while ((coverage = coverage->next)); } /* }}} */ -static void php_pcov_discover_code(zend_arena **arena, zend_op_array *ops, zval *return_value) { /* {{{ */ +static void php_pcov_discover_code( + zend_arena **arena, + zend_op_array *ops, + zval *return_value, + php_pcov_dump_stats_t *stats) { /* {{{ */ zend_cfg cfg; zend_basic_block *block; zend_op *limit = ops->opcodes + ops->last; @@ -609,6 +671,9 @@ static void php_pcov_discover_code(zend_arena **arena, zend_op_array *ops, zval } memset(&cfg, 0, sizeof(zend_cfg)); + if (stats) { + stats->cfg_op_arrays++; + } zend_build_cfg(arena, ops, PHP_PCOV_CFG, &cfg); @@ -656,34 +721,54 @@ static void php_pcov_discover_code(zend_arena **arena, zend_op_array *ops, zval #if PHP_VERSION_ID >= 80100 for (uint32_t def = 0; def < ops->num_dynamic_func_defs; def++) { - php_pcov_discover_code(arena, ops->dynamic_func_defs[def], return_value); + php_pcov_discover_code( + arena, ops->dynamic_func_defs[def], return_value, stats); } #endif } /* }}} */ -static void php_pcov_discover_file(zend_string *file, zval *return_value) { /* {{{ */ +static void php_pcov_discover_file( + zend_string *file, + zval *return_value, + php_pcov_dump_stats_t *stats) { /* {{{ */ zval discovered; zend_op_array *ops; zval *cache = zend_hash_find(&PCG(discovered), file); zend_arena *mem; + uint64_t started = 0; + zend_bool selected = cache || zend_hash_exists(&PCG(files), file); + + if (stats && selected) { + stats->collection_files++; + } if (cache) { zval uncached; + if (stats) { + started = php_pcov_dump_now(); + } ZVAL_DUP(&uncached, cache); zend_hash_update(Z_ARRVAL_P(return_value), file, &uncached); + if (stats) { + stats->array_construction_ns += php_pcov_dump_now() - started; + } return; } if (!(ops = zend_hash_find_ptr(&PCG(files), file))) { return; } + if (stats) { + started = php_pcov_dump_now(); + stats->discovery_operations++; + } array_init(&discovered); mem = zend_arena_create(1024 * 1024); - php_pcov_discover_code(&mem, ops, &discovered); + php_pcov_discover_code(&mem, ops, &discovered, stats); { zend_class_entry *ce; zend_op_array *function; @@ -696,7 +781,7 @@ static void php_pcov_discover_file(zend_string *file, zval *return_value) { /* { if (function->type == ZEND_USER_FUNCTION && function->filename && zend_string_equals(file, function->filename)) { - php_pcov_discover_code(&mem, function, &discovered); + php_pcov_discover_code(&mem, function, &discovered, stats); } } ZEND_HASH_FOREACH_END(); @@ -711,7 +796,8 @@ static void php_pcov_discover_file(zend_string *file, zval *return_value) { /* { if (function->type == ZEND_USER_FUNCTION && function->filename && zend_string_equals(file, function->filename)) { - php_pcov_discover_code(&mem, function, &discovered); + php_pcov_discover_code( + &mem, function, &discovered, stats); } } } @@ -728,15 +814,28 @@ static void php_pcov_discover_file(zend_string *file, zval *return_value) { /* { if (function->type == ZEND_USER_FUNCTION && function->filename && zend_string_equals(file, function->filename)) { - php_pcov_discover_code(&mem, function, &discovered); + php_pcov_discover_code(&mem, function, &discovered, stats); } } ZEND_HASH_FOREACH_END(); } zend_hash_update(&PCG(discovered), file, &discovered); zend_arena_destroy(mem); - - php_pcov_discover_file(file, return_value); + if (stats) { + stats->cfg_discovery_ns += php_pcov_dump_now() - started; + } + + { + zval uncached; + if (stats) { + started = php_pcov_dump_now(); + } + ZVAL_DUP(&uncached, &discovered); + zend_hash_update(Z_ARRVAL_P(return_value), file, &uncached); + if (stats) { + stats->array_construction_ns += php_pcov_dump_now() - started; + } + } } /* }}} */ static zend_always_inline void php_pcov_clean(HashTable *table) { /* {{{ */ @@ -745,31 +844,25 @@ static zend_always_inline void php_pcov_clean(HashTable *table) { /* {{{ */ } } /* }}} */ -/* {{{ array \pcov\collect(int $type = \pcov\all, array $filter = []); */ -PHP_NAMED_FUNCTION(php_pcov_collect) -{ - zend_long type = PCOV_FILTER_ALL; - zval *filter = NULL; - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|la", &type, &filter) != SUCCESS) { - return; - } - - PHP_PCOV_API_ENABLED_GUARD(); - - if (PCOV_FILTER_ALL != type && - PCOV_FILTER_INCLUDE != type && - PCOV_FILTER_EXCLUDE != type) { - zend_throw_error(zend_ce_type_error, - "type must be " - "\\pcov\\inclusive, " - "\\pcov\\exclusive, or \\pcov\\all"); - return; - } +static void php_pcov_collect_data_profiled_impl( + zend_long type, + zval *filter, + zval *return_value, + php_pcov_dump_stats_t *stats, + zend_bool force) { /* {{{ */ + uint64_t started = stats ? php_pcov_dump_now() : 0; + uint64_t accounted; + uint64_t array_started = started; array_init(return_value); + if (stats) { + stats->array_construction_ns += php_pcov_dump_now() - array_started; + } - if (PCG(last) == PCG(next)) { + if (!force && PCG(last) == PCG(next)) { + if (stats) { + stats->range_filter_ns += php_pcov_dump_now() - started; + } return; } @@ -778,12 +871,17 @@ PHP_NAMED_FUNCTION(php_pcov_collect) switch(type) { case PCOV_FILTER_INCLUDE: { zval *filtered; + + if (!filter) { + break; + } + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(filter), filtered) { if (Z_TYPE_P(filtered) != IS_STRING) { continue; } - php_pcov_discover_file(Z_STR_P(filtered), return_value); + php_pcov_discover_file(Z_STR_P(filtered), return_value, stats); } ZEND_HASH_FOREACH_END(); } break; @@ -791,18 +889,21 @@ PHP_NAMED_FUNCTION(php_pcov_collect) zend_string *name; zval *filtered; ZEND_HASH_FOREACH_STR_KEY(&PCG(files), name) { - ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(filter), filtered) { - if (Z_TYPE_P(filtered) != IS_STRING) { - continue; - } + if (filter) { + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(filter), filtered) { + if (Z_TYPE_P(filtered) != IS_STRING) { + continue; + } - if (zend_string_equals(name, Z_STR_P(filtered))) { - goto _php_pcov_collect_exclude; - } - } ZEND_HASH_FOREACH_END(); - php_pcov_discover_file(name, return_value); + if (zend_string_equals(name, Z_STR_P(filtered))) { + goto _php_pcov_collect_data_exclude; + } + } ZEND_HASH_FOREACH_END(); + } + + php_pcov_discover_file(name, return_value, stats); - _php_pcov_collect_exclude: + _php_pcov_collect_data_exclude: continue; } ZEND_HASH_FOREACH_END(); } break; @@ -810,17 +911,79 @@ PHP_NAMED_FUNCTION(php_pcov_collect) case PCOV_FILTER_ALL: { zend_string *name; ZEND_HASH_FOREACH_STR_KEY(&PCG(files), name) { - php_pcov_discover_file(name, return_value); + php_pcov_discover_file(name, return_value, stats); } ZEND_HASH_FOREACH_END(); } break; } - php_pcov_report(PCG(start), return_value); + if (stats) { + uint64_t hit_started = php_pcov_dump_now(); + php_pcov_report(PCG(start), return_value, stats); + stats->hit_traversal_ns += php_pcov_dump_now() - hit_started; + accounted = stats->cfg_discovery_ns + stats->array_construction_ns + + stats->hit_traversal_ns; + started = php_pcov_dump_now() - started; + if (started > accounted) { + stats->range_filter_ns += started - accounted; + } + } else { + php_pcov_report(PCG(start), return_value, NULL); + } +} /* }}} */ + +PHP_PCOV_API void php_pcov_collect_data_profiled( + zend_long type, + zval *filter, + zval *return_value, + php_pcov_dump_stats_t *stats) { /* {{{ */ + php_pcov_collect_data_profiled_impl( + type, filter, return_value, stats, 0); +} /* }}} */ + +PHP_PCOV_API void php_pcov_collect_data_force_profiled( + zend_long type, + zval *filter, + zval *return_value, + php_pcov_dump_stats_t *stats) { /* {{{ */ + php_pcov_collect_data_profiled_impl( + type, filter, return_value, stats, 1); +} /* }}} */ + +PHP_PCOV_API void php_pcov_collect_data( + zend_long type, zval *filter, zval *return_value) { /* {{{ */ + php_pcov_collect_data_profiled(type, filter, return_value, NULL); +} /* }}} */ + +/* {{{ array \pcov\collect(int $type = \pcov\all, array $filter = []); */ +PHP_NAMED_FUNCTION(php_pcov_collect) +{ + zend_long type = PCOV_FILTER_ALL; + zval *filter = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|la", &type, &filter) != SUCCESS) { + return; + } + + PHP_PCOV_API_ENABLED_GUARD(); + + if (PCOV_FILTER_ALL != type && + PCOV_FILTER_INCLUDE != type && + PCOV_FILTER_EXCLUDE != type) { + zend_throw_error(zend_ce_type_error, + "type must be " + "\\pcov\\inclusive, " + "\\pcov\\exclusive, or \\pcov\\all"); + return; + } + + php_pcov_collect_data(type, filter, return_value); } /* }}} */ /* {{{ void \pcov\start(void) */ PHP_NAMED_FUNCTION(php_pcov_start) { + (void) return_value; + if (zend_parse_parameters_none() != SUCCESS) { return; } @@ -833,6 +996,8 @@ PHP_NAMED_FUNCTION(php_pcov_start) /* {{{ void \pcov\stop(void) */ PHP_NAMED_FUNCTION(php_pcov_stop) { + (void) return_value; + if (zend_parse_parameters_none() != SUCCESS) { return; } @@ -846,6 +1011,7 @@ PHP_NAMED_FUNCTION(php_pcov_stop) PHP_NAMED_FUNCTION(php_pcov_clear) { zend_bool files = 0; + (void) return_value; if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &files) != SUCCESS) { return; @@ -856,6 +1022,7 @@ PHP_NAMED_FUNCTION(php_pcov_clear) if (files) { php_pcov_clean(&PCG(files)); php_pcov_clean(&PCG(discovered)); + php_pcov_clean(&PCG(fingerprints)); } zend_arena_destroy(PCG(mem)); @@ -878,7 +1045,7 @@ PHP_NAMED_FUNCTION(php_pcov_waiting) zend_string *waiting; if (zend_parse_parameters_none() != SUCCESS) { - return; + return; } PHP_PCOV_API_ENABLED_GUARD(); @@ -931,6 +1098,16 @@ ZEND_BEGIN_ARG_INFO_EX(php_pcov_clear_arginfo, 0, 0, 0) ZEND_ARG_TYPE_INFO(0, files, _IS_BOOL, 0) ZEND_END_ARG_INFO() /* }}} */ +/* {{{ */ +ZEND_BEGIN_ARG_INFO_EX(php_pcov_export_arginfo, 0, 0, 1) + ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0) + ZEND_ARG_TYPE_INFO(0, manifest, IS_STRING, 1) + ZEND_ARG_TYPE_INFO(0, deployment_id, IS_STRING, 1) + ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, filter, IS_ARRAY, 0) +ZEND_END_ARG_INFO() /* }}} */ + +/* {{{ */ /* {{{ */ ZEND_BEGIN_ARG_INFO_EX(php_pcov_no_arginfo, 0, 0, 0) ZEND_END_ARG_INFO() /* }}} */ @@ -945,6 +1122,8 @@ const zend_function_entry php_pcov_functions[] = { ZEND_NS_FENTRY("pcov", waiting, php_pcov_waiting, php_pcov_no_arginfo, 0) ZEND_NS_FENTRY("pcov", memory, php_pcov_memory, php_pcov_no_arginfo, 0) ZEND_NS_FENTRY("pcov", enabled, php_pcov_enabled, php_pcov_no_arginfo, 0) + ZEND_NS_FENTRY("pcov", export, php_pcov_export, php_pcov_export_arginfo, 0) + ZEND_NS_FENTRY("pcov", export_stats, php_pcov_export_stats, php_pcov_no_arginfo, 0) PHP_FE_END }; /* }}} */ diff --git a/pcov_dump.c b/pcov_dump.c new file mode 100644 index 0000000..84c0f2c --- /dev/null +++ b/pcov_dump.c @@ -0,0 +1,1736 @@ +/* + +----------------------------------------------------------------------+ + | PCOV synchronous coverage export | + +----------------------------------------------------------------------+ + | Native full and validated manifest records for large codebases. | + +----------------------------------------------------------------------+ +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "Zend/zend_smart_str.h" +#include "Zend/zend_exceptions.h" +#include "ext/pcre/php_pcre.h" + +#include "php_pcov.h" +#include "pcov_fingerprint.h" + +#include +#include +#include +#include +#include + +#include + +#ifdef HAVE_PCOV_NATIVE_EXPORT +# include +# include +# include +# include +#endif + +#define PCOV_FILTER_ALL 0 +#define PCOV_FILTER_INCLUDE 1 +#define PCOV_FILTER_EXCLUDE 2 + +#define PCOV_RECORD_HEADER_SIZE 48 +#define PCOV_RECORD_HITS 3 +#define PCOV_RECORD_FORMAT_VERSION 1 +#define PCOV_RECORD_FORMAT_COMPATIBILITY 1 +#define PCOV_RECORD_FULL 1 +#define PCOV_RECORD_MANIFEST 2 +#define PCOV_RECORD_MAX_FILE_SIZE (512U * 1024U * 1024U) +#define PCOV_RECORD_MAX_FILES 1000000U +#define PCOV_RECORD_MAX_LINES 100000000U +#define PCOV_RECORD_MAX_PATH (1024U * 1024U) + +#define PCOV_MANIFEST_REASON_MATCH 0U +#define PCOV_MANIFEST_REASON_OPEN 1U +#define PCOV_MANIFEST_REASON_CORRUPT 2U +#define PCOV_MANIFEST_REASON_ENVIRONMENT 3U +#define PCOV_MANIFEST_REASON_COMPATIBILITY 4U +#define PCOV_MANIFEST_REASON_NEW_FILE 5U +#define PCOV_MANIFEST_REASON_MODIFIED_FILE 6U +#define PCOV_MANIFEST_REASON_FINGERPRINT 7U +#define PCOV_MANIFEST_REASON_UNKNOWN_HIT 8U +#define PCOV_MANIFEST_REASON_EXPLICIT_FULL 9U +#define PCOV_DUMP_PHASE_NONE 0 +#define PCOV_DUMP_PHASE_COLLECT 1 +#define PCOV_DUMP_PHASE_SERIALIZE 2 +#define PCOV_DUMP_PHASE_OPEN 3 +#define PCOV_DUMP_PHASE_WRITE 4 +#define PCOV_DUMP_PHASE_CLOSE 5 +#define PCOV_DUMP_PHASE_RENAME 6 + +static const char *php_pcov_dump_phase_name(int phase) { /* {{{ */ + switch (phase) { + case PCOV_DUMP_PHASE_COLLECT: + return "collection"; + case PCOV_DUMP_PHASE_SERIALIZE: + return "serialization"; + case PCOV_DUMP_PHASE_OPEN: + return "temporary-file open"; + case PCOV_DUMP_PHASE_WRITE: + return "write"; + case PCOV_DUMP_PHASE_CLOSE: + return "close"; + case PCOV_DUMP_PHASE_RENAME: + return "rename"; + default: + return "export"; + } +} /* }}} */ + +#ifdef HAVE_PCOV_NATIVE_EXPORT + +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + +typedef struct _php_pcov_dump_hit_t { + zend_string *file; + uint32_t line; +} php_pcov_dump_hit_t; + +typedef struct _php_pcov_manifest_file_t { + zend_string *file; + unsigned char fingerprint[PHP_PCOV_FINGERPRINT_SIZE]; + uint32_t status; +} php_pcov_manifest_file_t; + +typedef struct _php_pcov_manifest_view_t { + unsigned char *data; + size_t size; + const unsigned char *payload; + size_t payload_size; + unsigned char environment_id[PHP_PCOV_FINGERPRINT_SIZE]; + unsigned char manifest_id[PHP_PCOV_FINGERPRINT_SIZE]; + uint32_t file_count; +} php_pcov_manifest_view_t; + +PHP_PCOV_API uint64_t php_pcov_dump_now(void) { /* {{{ */ + struct timespec now; + + if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) { + return 0; + } + + return ((uint64_t) now.tv_sec * 1000000000ULL) + (uint64_t) now.tv_nsec; +} /* }}} */ + +PHP_PCOV_API zend_bool php_pcov_dump_benchmark_enabled(void) { /* {{{ */ + const char *enabled = getenv("PCOV_DUMP_BENCH_STATS"); + + return enabled && strcmp(enabled, "1") == 0; +} /* }}} */ + +static int64_t php_pcov_dump_timeval_us(struct timeval *time) { /* {{{ */ + return ((int64_t) time->tv_sec * 1000000) + (int64_t) time->tv_usec; +} /* }}} */ + +static void php_pcov_dump_usage_delta( + php_pcov_dump_stats_t *stats, + struct rusage *before, + struct rusage *after) { /* {{{ */ + stats->user_us = + php_pcov_dump_timeval_us(&after->ru_utime) - + php_pcov_dump_timeval_us(&before->ru_utime); + stats->system_us = + php_pcov_dump_timeval_us(&after->ru_stime) - + php_pcov_dump_timeval_us(&before->ru_stime); + stats->minor_faults = after->ru_minflt - before->ru_minflt; + stats->major_faults = after->ru_majflt - before->ru_majflt; + stats->max_rss_kb = after->ru_maxrss; +} /* }}} */ + +static void php_pcov_dump_append_u32(smart_str *buffer, uint32_t value) { /* {{{ */ + char bytes[4]; + + bytes[0] = (char) ((value >> 24) & 0xff); + bytes[1] = (char) ((value >> 16) & 0xff); + bytes[2] = (char) ((value >> 8) & 0xff); + bytes[3] = (char) (value & 0xff); + + smart_str_appendl(buffer, bytes, sizeof(bytes)); +} /* }}} */ + +static void php_pcov_dump_append_u64(smart_str *buffer, uint64_t value) { /* {{{ */ + php_pcov_dump_append_u32(buffer, (uint32_t) (value >> 32)); + php_pcov_dump_append_u32(buffer, (uint32_t) value); +} /* }}} */ + +static uint32_t php_pcov_dump_checksum(const char *data, size_t length) { /* {{{ */ + uint32_t checksum = UINT32_MAX; + size_t index; + + for (index = 0; index < length; index++) { + uint32_t byte = (uint8_t) data[index]; + int bit; + + checksum ^= byte; + for (bit = 0; bit < 8; bit++) { + uint32_t mask = (uint32_t) -(int32_t) (checksum & 1U); + checksum = (checksum >> 1) ^ (0xedb88320U & mask); + } + } + + return ~checksum; +} /* }}} */ + +static uint32_t php_pcov_dump_read_u32(const unsigned char *bytes) { /* {{{ */ + return ((uint32_t) bytes[0] << 24) | + ((uint32_t) bytes[1] << 16) | + ((uint32_t) bytes[2] << 8) | + (uint32_t) bytes[3]; +} /* }}} */ + +static uint64_t php_pcov_dump_read_u64(const unsigned char *bytes) { /* {{{ */ + return ((uint64_t) php_pcov_dump_read_u32(bytes) << 32) | + (uint64_t) php_pcov_dump_read_u32(bytes + 4); +} /* }}} */ + +static zend_bool php_pcov_dump_hex_id( + zend_string *hex, + unsigned char result[PHP_PCOV_FINGERPRINT_SIZE]) { /* {{{ */ + size_t index; + + if (!hex || ZSTR_LEN(hex) != PHP_PCOV_FINGERPRINT_SIZE * 2) { + return 0; + } + for (index = 0; index < PHP_PCOV_FINGERPRINT_SIZE; index++) { + unsigned char high = (unsigned char) ZSTR_VAL(hex)[index * 2]; + unsigned char low = (unsigned char) ZSTR_VAL(hex)[index * 2 + 1]; + + high = high >= '0' && high <= '9' ? high - '0' : + high >= 'a' && high <= 'f' ? high - 'a' + 10 : + high >= 'A' && high <= 'F' ? high - 'A' + 10 : 255; + low = low >= '0' && low <= '9' ? low - '0' : + low >= 'a' && low <= 'f' ? low - 'a' + 10 : + low >= 'A' && low <= 'F' ? low - 'A' + 10 : 255; + if (high > 15 || low > 15) { + return 0; + } + result[index] = (unsigned char) ((high << 4) | low); + } + return 1; +} /* }}} */ + +static zend_string *php_pcov_dump_id_hex( + const unsigned char id[PHP_PCOV_FINGERPRINT_SIZE]) { /* {{{ */ + static const char digits[] = "0123456789abcdef"; + zend_string *result = zend_string_alloc(PHP_PCOV_FINGERPRINT_SIZE * 2, 0); + size_t index; + + for (index = 0; index < PHP_PCOV_FINGERPRINT_SIZE; index++) { + ZSTR_VAL(result)[index * 2] = digits[id[index] >> 4]; + ZSTR_VAL(result)[index * 2 + 1] = digits[id[index] & 15]; + } + ZSTR_VAL(result)[PHP_PCOV_FINGERPRINT_SIZE * 2] = '\0'; + return result; +} /* }}} */ + +static const char *php_pcov_manifest_reason_name(uint32_t reason) { /* {{{ */ + switch (reason) { + case PCOV_MANIFEST_REASON_MATCH: return "match"; + case PCOV_MANIFEST_REASON_OPEN: return "manifest-open"; + case PCOV_MANIFEST_REASON_CORRUPT: return "manifest-corrupt"; + case PCOV_MANIFEST_REASON_ENVIRONMENT: return "environment-identity"; + case PCOV_MANIFEST_REASON_COMPATIBILITY: return "runtime-compatibility"; + case PCOV_MANIFEST_REASON_NEW_FILE: return "new-loaded-file"; + case PCOV_MANIFEST_REASON_MODIFIED_FILE: return "modified-loaded-file"; + case PCOV_MANIFEST_REASON_FINGERPRINT: return "fingerprint-unavailable"; + case PCOV_MANIFEST_REASON_UNKNOWN_HIT: return "hit-not-in-manifest"; + case PCOV_MANIFEST_REASON_EXPLICIT_FULL: return "explicit-full"; + default: return "unknown"; + } +} /* }}} */ + +static int php_pcov_dump_hit_compare(const void *left, const void *right) { /* {{{ */ + const php_pcov_dump_hit_t *first = (const php_pcov_dump_hit_t *) left; + const php_pcov_dump_hit_t *second = (const php_pcov_dump_hit_t *) right; + size_t shared = ZSTR_LEN(first->file) < ZSTR_LEN(second->file) ? + ZSTR_LEN(first->file) : ZSTR_LEN(second->file); + int compared = memcmp(ZSTR_VAL(first->file), ZSTR_VAL(second->file), shared); + + if (compared) { + return compared; + } + if (ZSTR_LEN(first->file) < ZSTR_LEN(second->file)) { + return -1; + } + if (ZSTR_LEN(first->file) > ZSTR_LEN(second->file)) { + return 1; + } + if (first->line < second->line) { + return -1; + } + if (first->line > second->line) { + return 1; + } + + return 0; +} /* }}} */ + +static int php_pcov_manifest_file_compare(const void *left, const void *right) { /* {{{ */ + const php_pcov_manifest_file_t *first = left; + const php_pcov_manifest_file_t *second = right; + size_t shared = ZSTR_LEN(first->file) < ZSTR_LEN(second->file) ? + ZSTR_LEN(first->file) : ZSTR_LEN(second->file); + int compared = memcmp(ZSTR_VAL(first->file), ZSTR_VAL(second->file), shared); + + if (compared) { + return compared; + } + return ZSTR_LEN(first->file) < ZSTR_LEN(second->file) ? -1 : + ZSTR_LEN(first->file) > ZSTR_LEN(second->file) ? 1 : 0; +} /* }}} */ + +static void php_pcov_dump_prepare_hit_filter( + zend_long type, zval *filter, HashTable *selected) { /* {{{ */ + zval *candidate; + uint32_t capacity = filter ? zend_hash_num_elements(Z_ARRVAL_P(filter)) : 0; + + zend_hash_init(selected, capacity, NULL, NULL, 0); + if (type == PCOV_FILTER_ALL || !filter) { + return; + } + + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(filter), candidate) { + if (Z_TYPE_P(candidate) == IS_STRING) { + zval present; + ZVAL_TRUE(&present); + zend_hash_update(selected, Z_STR_P(candidate), &present); + } + } ZEND_HASH_FOREACH_END(); +} /* }}} */ + +static zend_bool php_pcov_dump_file_selected( + zend_string *file, zend_long type, HashTable *selected) { /* {{{ */ + zend_bool matched; + + if (type == PCOV_FILTER_ALL) { + return 1; + } + matched = zend_hash_exists(selected, file); + return type == PCOV_FILTER_INCLUDE ? matched : !matched; +} /* }}} */ + +static zend_bool php_pcov_dump_hit_selected( + php_coverage_t *coverage, zend_long type, HashTable *selected) { /* {{{ */ + zend_bool matched; + + if (type == PCOV_FILTER_ALL) { + return 1; + } + matched = zend_hash_exists(selected, coverage->file); + + return type == PCOV_FILTER_INCLUDE ? matched : !matched; +} /* }}} */ + +static zend_bool php_pcov_manifest_collect_files( + zend_long type, + zval *filter, + php_pcov_manifest_file_t **result, + size_t *result_count, + php_pcov_dump_stats_t *stats, + uint32_t *reason, + int *error) { /* {{{ */ + php_pcov_manifest_file_t *files = NULL; + HashTable selected; + zend_string *file; + size_t count = 0; + size_t position = 0; + uint64_t started = php_pcov_dump_now(); + + php_pcov_dump_prepare_hit_filter(type, filter, &selected); + ZEND_HASH_FOREACH_STR_KEY(&PCG(files), file) { + if (file && php_pcov_dump_file_selected(file, type, &selected)) { + count++; + } + } ZEND_HASH_FOREACH_END(); + + if (count > SIZE_MAX / sizeof(*files)) { + zend_hash_destroy(&selected); + *error = EOVERFLOW; + return 0; + } + if (count) { + files = safe_emalloc(count, sizeof(*files), 0); + ZEND_HASH_FOREACH_STR_KEY(&PCG(files), file) { + if (!file || !php_pcov_dump_file_selected(file, type, &selected)) { + continue; + } + files[position].file = file; + files[position].status = 0; + memset(files[position].fingerprint, 0, + PHP_PCOV_FINGERPRINT_SIZE); + position++; + } ZEND_HASH_FOREACH_END(); + qsort(files, count, sizeof(*files), php_pcov_manifest_file_compare); + } + zend_hash_destroy(&selected); + + for (position = 0; position < count; position++) { + int fingerprint_error = 0; + unsigned char current[PHP_PCOV_FINGERPRINT_SIZE]; + zval *compiled = zend_hash_find(&PCG(fingerprints), files[position].file); + php_pcov_fingerprint_metrics_t *metrics = + php_pcov_dump_benchmark_enabled() ? + &stats->fingerprint_metrics : NULL; + zend_bool reused = 0; + + if (!compiled || Z_TYPE_P(compiled) != IS_STRING || + Z_STRLEN_P(compiled) != PHP_PCOV_FINGERPRINT_SIZE + + sizeof(php_pcov_fingerprint_identity_t)) { + files[position].status = ENODATA; + if (*reason == PCOV_MANIFEST_REASON_MATCH) { + *reason = PCOV_MANIFEST_REASON_FINGERPRINT; + } + continue; + } + memcpy(files[position].fingerprint, Z_STRVAL_P(compiled), + PHP_PCOV_FINGERPRINT_SIZE); + if (PCG(ini.large_codebase)) { + php_pcov_fingerprint_identity_t identity; + + memcpy(&identity, + Z_STRVAL_P(compiled) + PHP_PCOV_FINGERPRINT_SIZE, + sizeof(identity)); + reused = php_pcov_fingerprint_identity_matches( + files[position].file, &identity, + &fingerprint_error, metrics); + if (reused) { + memcpy(current, files[position].fingerprint, + PHP_PCOV_FINGERPRINT_SIZE); + } + } + if (!reused && !php_pcov_fingerprint_file( + files[position].file, + current, + &fingerprint_error, + metrics, NULL)) { + files[position].status = (uint32_t) + (fingerprint_error > 0 ? fingerprint_error : EIO); + if (*reason == PCOV_MANIFEST_REASON_MATCH) { + *reason = PCOV_MANIFEST_REASON_FINGERPRINT; + } + } else if (memcmp(current, files[position].fingerprint, + PHP_PCOV_FINGERPRINT_SIZE) != 0) { + files[position].status = EAGAIN; + if (*reason == PCOV_MANIFEST_REASON_MATCH) { + *reason = PCOV_MANIFEST_REASON_FINGERPRINT; + } + } + } + stats->fingerprint_ns = php_pcov_dump_now() - started; + stats->validation_files = count; + stats->compile_fingerprint_ns = PCG(compile_fingerprint_ns); + stats->compile_fingerprint_files = PCG(compile_fingerprint_files); + stats->compile_fingerprint_metrics = PCG(compile_fingerprint_metrics); + *result = files; + *result_count = count; + return 1; +} /* }}} */ + +static zend_bool php_pcov_manifest_bounds( + const unsigned char *cursor, + const unsigned char *end, + size_t needed) { /* {{{ */ + return cursor <= end && needed <= (size_t) (end - cursor); +} /* }}} */ + +static void php_pcov_manifest_view_destroy(php_pcov_manifest_view_t *view) { /* {{{ */ + if (view->data) { + efree(view->data); + } + memset(view, 0, sizeof(*view)); +} /* }}} */ + +static zend_bool php_pcov_manifest_load( + zend_string *path, + php_pcov_manifest_view_t *view, + uint32_t *reason, + int *error, + php_pcov_dump_stats_t *stats, + zend_bool profile) { /* {{{ */ + struct stat status; + const unsigned char *cursor; + const unsigned char *end; + const unsigned char *previous = NULL; + uint32_t previous_length = 0; + uint32_t index; + int descriptor; + size_t offset = 0; + uint64_t payload_length; + uint64_t started = 0; + + memset(view, 0, sizeof(*view)); + if (!path || !ZSTR_LEN(path) || + strlen(ZSTR_VAL(path)) != ZSTR_LEN(path)) { + *reason = PCOV_MANIFEST_REASON_OPEN; + *error = EINVAL; + return 0; + } + if (profile) { + stats->manifest_open_calls++; + started = php_pcov_dump_now(); + } + descriptor = open(ZSTR_VAL(path), O_RDONLY | O_CLOEXEC); + if (profile) stats->manifest_open_ns += php_pcov_dump_now() - started; + if (descriptor < 0) { + *reason = PCOV_MANIFEST_REASON_OPEN; + *error = errno; + return 0; + } + if (profile) { + stats->manifest_stat_calls++; + started = php_pcov_dump_now(); + } + if (fstat(descriptor, &status) != 0 || !S_ISREG(status.st_mode) || + status.st_size < PCOV_RECORD_HEADER_SIZE || + (uint64_t) status.st_size > PCOV_RECORD_MAX_FILE_SIZE) { + if (profile) stats->manifest_stat_ns += php_pcov_dump_now() - started; + *reason = PCOV_MANIFEST_REASON_CORRUPT; + *error = errno ? errno : EINVAL; + close(descriptor); + return 0; + } + if (profile) stats->manifest_stat_ns += php_pcov_dump_now() - started; + + view->size = (size_t) status.st_size; + if (profile) started = php_pcov_dump_now(); + view->data = safe_emalloc(view->size, 1, 0); + if (profile) { + stats->manifest_allocation_ns += php_pcov_dump_now() - started; + stats->manifest_allocations++; + stats->manifest_allocated_bytes += view->size; + } + while (offset < view->size) { + if (profile) { + stats->manifest_read_calls++; + started = php_pcov_dump_now(); + } + ssize_t received = read( + descriptor, view->data + offset, view->size - offset); + if (profile) stats->manifest_read_ns += php_pcov_dump_now() - started; + if (received < 0) { + if (errno == EINTR) { + continue; + } + *reason = PCOV_MANIFEST_REASON_OPEN; + *error = errno; + close(descriptor); + php_pcov_manifest_view_destroy(view); + return 0; + } + if (received == 0) { + *reason = PCOV_MANIFEST_REASON_CORRUPT; + *error = EIO; + close(descriptor); + php_pcov_manifest_view_destroy(view); + return 0; + } + if (profile) stats->manifest_bytes_read += (uint64_t) received; + offset += (size_t) received; + } + if (profile) started = php_pcov_dump_now(); + if (close(descriptor) != 0) { + if (profile) stats->manifest_close_ns += php_pcov_dump_now() - started; + *reason = PCOV_MANIFEST_REASON_OPEN; + *error = errno; + php_pcov_manifest_view_destroy(view); + return 0; + } + if (profile) stats->manifest_close_ns += php_pcov_dump_now() - started; + + if (profile) started = php_pcov_dump_now(); + if (memcmp(view->data, "PCOVREC\0", 8) != 0 || + php_pcov_dump_read_u32(view->data + 8) != + PCOV_RECORD_FORMAT_VERSION || + php_pcov_dump_read_u32(view->data + 12) != + PCOV_RECORD_HEADER_SIZE || + php_pcov_dump_read_u32(view->data + 16) != + PCOV_RECORD_MANIFEST || + php_pcov_dump_read_u32(view->data + 20) != 0 || + php_pcov_dump_read_u32(view->data + 44) != 0) { + *reason = PCOV_MANIFEST_REASON_CORRUPT; + *error = EINVAL; + php_pcov_manifest_view_destroy(view); + if (profile) stats->manifest_header_ns += php_pcov_dump_now() - started; + return 0; + } + if (php_pcov_dump_read_u32(view->data + 36) != PHP_VERSION_ID || + php_pcov_dump_read_u32(view->data + 40) != + PCOV_RECORD_FORMAT_COMPATIBILITY) { + *reason = PCOV_MANIFEST_REASON_COMPATIBILITY; + *error = EINVAL; + php_pcov_manifest_view_destroy(view); + if (profile) stats->manifest_header_ns += php_pcov_dump_now() - started; + return 0; + } + payload_length = php_pcov_dump_read_u64(view->data + 24); + if (payload_length != view->size - PCOV_RECORD_HEADER_SIZE) { + *reason = PCOV_MANIFEST_REASON_CORRUPT; + *error = EINVAL; + php_pcov_manifest_view_destroy(view); + if (profile) stats->manifest_header_ns += php_pcov_dump_now() - started; + return 0; + } + view->payload = view->data + PCOV_RECORD_HEADER_SIZE; + view->payload_size = (size_t) payload_length; + if (profile) { + stats->manifest_header_ns += php_pcov_dump_now() - started; + stats->manifest_checksum_calls++; + started = php_pcov_dump_now(); + } + if (php_pcov_dump_checksum( + (const char *) view->payload, view->payload_size) != + php_pcov_dump_read_u32(view->data + 32)) { + *reason = PCOV_MANIFEST_REASON_CORRUPT; + *error = EINVAL; + php_pcov_manifest_view_destroy(view); + if (profile) stats->manifest_checksum_ns += php_pcov_dump_now() - started; + return 0; + } + if (profile) { + stats->manifest_checksum_ns += php_pcov_dump_now() - started; + started = php_pcov_dump_now(); + } + + cursor = view->payload; + end = cursor + view->payload_size; + if (!php_pcov_manifest_bounds(cursor, end, 72)) { + goto corrupt; + } + memcpy(view->environment_id, cursor, PHP_PCOV_FINGERPRINT_SIZE); + cursor += PHP_PCOV_FINGERPRINT_SIZE; + memcpy(view->manifest_id, cursor, PHP_PCOV_FINGERPRINT_SIZE); + cursor += PHP_PCOV_FINGERPRINT_SIZE; + if (php_pcov_dump_read_u32(cursor) != PCOV_MANIFEST_REASON_MATCH) { + goto corrupt; + } + cursor += 4; + view->file_count = php_pcov_dump_read_u32(cursor); + cursor += 4; + if (view->file_count > PCOV_RECORD_MAX_FILES) { + goto corrupt; + } + + for (index = 0; index < view->file_count; index++) { + uint32_t length; + uint32_t lines; + uint32_t line_index; + uint32_t previous_line = 0; + + if (!php_pcov_manifest_bounds(cursor, end, 4)) goto corrupt; + length = php_pcov_dump_read_u32(cursor); cursor += 4; + if (!length || length > PCOV_RECORD_MAX_PATH || + !php_pcov_manifest_bounds(cursor, end, (size_t) length + 36)) { + goto corrupt; + } + if (previous) { + size_t shared = previous_length < length ? previous_length : length; + int compared = memcmp(previous, cursor, shared); + if (compared > 0 || (compared == 0 && previous_length >= length)) { + goto corrupt; + } + } + previous = cursor; + previous_length = length; + cursor += length + PHP_PCOV_FINGERPRINT_SIZE; + lines = php_pcov_dump_read_u32(cursor); cursor += 4; + if (lines > PCOV_RECORD_MAX_LINES || + !php_pcov_manifest_bounds(cursor, end, (size_t) lines * 4)) { + goto corrupt; + } + for (line_index = 0; line_index < lines; line_index++) { + uint32_t line = php_pcov_dump_read_u32(cursor); cursor += 4; + if (!line || (line_index && line <= previous_line)) goto corrupt; + previous_line = line; + } + if (profile) { + stats->manifest_records_validated++; + stats->manifest_lines_validated += lines; + } + } + if (cursor != end) goto corrupt; + if (profile) stats->manifest_record_ns += php_pcov_dump_now() - started; + return 1; + +corrupt: + if (profile) stats->manifest_record_ns += php_pcov_dump_now() - started; + *reason = PCOV_MANIFEST_REASON_CORRUPT; + *error = EINVAL; + php_pcov_manifest_view_destroy(view); + return 0; +} /* }}} */ + +static zend_bool php_pcov_manifest_validate( + php_pcov_manifest_view_t *view, + const unsigned char environment_id[PHP_PCOV_FINGERPRINT_SIZE], + php_pcov_manifest_file_t *files, + size_t file_count, + uint32_t *reason) { /* {{{ */ + const unsigned char *cursor = view->payload + 72; + const unsigned char *end = view->payload + view->payload_size; + size_t wanted = 0; + uint32_t index; + + if (memcmp(view->environment_id, environment_id, + PHP_PCOV_FINGERPRINT_SIZE) != 0) { + *reason = PCOV_MANIFEST_REASON_ENVIRONMENT; + return 0; + } + for (wanted = 0; wanted < file_count; wanted++) { + if (files[wanted].status) { + *reason = PCOV_MANIFEST_REASON_FINGERPRINT; + return 0; + } + } + + wanted = 0; + for (index = 0; index < view->file_count && wanted < file_count; index++) { + uint32_t length = php_pcov_dump_read_u32(cursor); cursor += 4; + const unsigned char *name = cursor; + const unsigned char *fingerprint; + uint32_t lines; + size_t shared; + int compared; + + cursor += length; + fingerprint = cursor; + cursor += PHP_PCOV_FINGERPRINT_SIZE; + lines = php_pcov_dump_read_u32(cursor); cursor += 4 + ((size_t) lines * 4); + shared = length < ZSTR_LEN(files[wanted].file) ? + length : ZSTR_LEN(files[wanted].file); + compared = memcmp(name, ZSTR_VAL(files[wanted].file), shared); + if (!compared) { + compared = length < ZSTR_LEN(files[wanted].file) ? -1 : + length > ZSTR_LEN(files[wanted].file) ? 1 : 0; + } + if (compared < 0) { + continue; + } + if (compared > 0) { + *reason = PCOV_MANIFEST_REASON_NEW_FILE; + return 0; + } + if (memcmp(fingerprint, files[wanted].fingerprint, + PHP_PCOV_FINGERPRINT_SIZE) != 0) { + *reason = PCOV_MANIFEST_REASON_MODIFIED_FILE; + return 0; + } + wanted++; + } + if (wanted != file_count || cursor > end) { + *reason = PCOV_MANIFEST_REASON_NEW_FILE; + return 0; + } + return 1; +} /* }}} */ + +static zend_bool php_pcov_manifest_validate_hits( + php_pcov_manifest_view_t *view, + php_pcov_dump_hit_t *hits, + size_t hit_count, + php_pcov_dump_stats_t *stats, + zend_bool profile) { /* {{{ */ + const unsigned char *cursor = view->payload + 72; + size_t hit = 0; + uint32_t file_index; + uint64_t started = 0; + uint64_t file_started = 0; + + if (hit_count) { + if (profile) started = php_pcov_dump_now(); + if (profile) { + stats->validated_sort_calls++; + stats->validated_sort_entries += hit_count; + } + qsort(hits, hit_count, sizeof(*hits), php_pcov_dump_hit_compare); + if (profile) { + size_t index; + stats->manifest_hit_sort_ns += php_pcov_dump_now() - started; + for (index = 1; index < hit_count; index++) { + if (!php_pcov_dump_hit_compare(&hits[index - 1], &hits[index])) { + stats->manifest_hit_duplicates++; + } + } + } + } + if (profile && hit_count) file_started = php_pcov_dump_now(); + for (file_index = 0; file_index < view->file_count && hit < hit_count; file_index++) { + uint32_t length = php_pcov_dump_read_u32(cursor); cursor += 4; + const unsigned char *name = cursor; + uint32_t line_count; + const unsigned char *line_cursor; + size_t shared; + int compared; + + cursor += length + PHP_PCOV_FINGERPRINT_SIZE; + line_count = php_pcov_dump_read_u32(cursor); cursor += 4; + line_cursor = cursor; + cursor += (size_t) line_count * 4; + shared = length < ZSTR_LEN(hits[hit].file) ? + length : ZSTR_LEN(hits[hit].file); + compared = memcmp(name, ZSTR_VAL(hits[hit].file), shared); + if (!compared) { + compared = length < ZSTR_LEN(hits[hit].file) ? -1 : + length > ZSTR_LEN(hits[hit].file) ? 1 : 0; + } + if (profile) { + stats->manifest_hit_files_looked_up++; + } + if (compared < 0) continue; + if (compared > 0) { + if (profile) { + stats->manifest_hit_file_lookup_ns += + php_pcov_dump_now() - file_started; + stats->manifest_hit_unknown_files++; + } + return 0; + } + + if (profile) { + stats->manifest_hit_file_lookup_ns += + php_pcov_dump_now() - file_started; + started = php_pcov_dump_now(); + } + while (hit < hit_count && + ZSTR_LEN(hits[hit].file) == length && + memcmp(ZSTR_VAL(hits[hit].file), name, length) == 0) { + uint32_t line_index; + zend_bool found = 0; + if (profile) { + stats->manifest_hit_entries_validated++; + stats->manifest_hit_search_restarts++; + } + for (line_index = 0; line_index < line_count; line_index++) { + uint32_t line = php_pcov_dump_read_u32(line_cursor + line_index * 4); + if (profile) { + stats->manifest_hit_line_comparisons++; + stats->manifest_hit_line_records_decoded++; + } + if (line == hits[hit].line) { + found = 1; + break; + } + if (line > hits[hit].line) break; + } + if (profile && found) stats->manifest_hit_matches++; + if (!found) { + if (profile) { + stats->manifest_hit_unknown_lines++; + stats->manifest_hit_line_lookup_ns += + php_pcov_dump_now() - started; + } + return 0; + } + hit++; + } + if (profile) { + stats->manifest_hit_line_lookup_ns += php_pcov_dump_now() - started; + file_started = php_pcov_dump_now(); + } + } + if (profile && hit != hit_count) { + stats->manifest_hit_file_lookup_ns += php_pcov_dump_now() - file_started; + stats->manifest_hit_unknown_files++; + } + return hit == hit_count; +} /* }}} */ + +static zend_bool php_pcov_dump_collect_hits( + zend_long type, + zval *filter, + php_pcov_dump_hit_t **result, + size_t *result_count, + php_pcov_dump_stats_t *stats, + zend_bool benchmark, + int *error) { /* {{{ */ + php_coverage_t *coverage = PCG(start); + php_pcov_dump_hit_t *hits = NULL; + HashTable selected; + size_t count = 0; + size_t position = 0; + uint64_t started; + + started = benchmark ? php_pcov_dump_now() : 0; + php_pcov_dump_prepare_hit_filter(type, filter, &selected); + if (benchmark) { + stats->range_filter_ns = php_pcov_dump_now() - started; + started = php_pcov_dump_now(); + } + + if (PCG(last) != PCG(next)) { + while (coverage) { + if (benchmark) { + stats->hit_entries++; + } + if (php_pcov_dump_hit_selected(coverage, type, &selected)) { + if (count == SIZE_MAX) { + *error = EOVERFLOW; + zend_hash_destroy(&selected); + return 0; + } + count++; + } + coverage = coverage->next; + } + } + + if (count > SIZE_MAX / sizeof(php_pcov_dump_hit_t)) { + *error = EOVERFLOW; + zend_hash_destroy(&selected); + return 0; + } + if (count) { + hits = safe_emalloc(count, sizeof(php_pcov_dump_hit_t), 0); + if (benchmark) { + stats->hit_array_allocations++; + stats->hit_array_allocated_bytes += + count * sizeof(php_pcov_dump_hit_t); + } + coverage = PCG(start); + while (coverage) { + if (php_pcov_dump_hit_selected(coverage, type, &selected)) { + hits[position].file = coverage->file; + hits[position].line = coverage->line; + position++; + } + coverage = coverage->next; + } + } + if (benchmark) { + stats->hit_traversal_ns = php_pcov_dump_now() - started; + } + zend_hash_destroy(&selected); + + *result = hits; + *result_count = count; + return 1; +} /* }}} */ + +typedef struct _php_pcov_dump_line_t { + uint32_t line; + int32_t value; +} php_pcov_dump_line_t; + +static int php_pcov_dump_line_compare(const void *left, const void *right) { /* {{{ */ + const php_pcov_dump_line_t *first = left; + const php_pcov_dump_line_t *second = right; + return first->line < second->line ? -1 : first->line > second->line ? 1 : 0; +} /* }}} */ + +static void php_pcov_record_envelope( + smart_str *buffer, + uint32_t record_type, + smart_str *payload, + php_pcov_dump_stats_t *stats, + zend_bool profile) { /* {{{ */ + uint32_t checksum; + uint64_t started = profile ? php_pcov_dump_now() : 0; + + smart_str_0(payload); + if (profile) { + stats->validated_checksum_calls++; + stats->validated_checksum_bytes += ZSTR_LEN(payload->s); + } + checksum = php_pcov_dump_checksum(ZSTR_VAL(payload->s), ZSTR_LEN(payload->s)); + if (profile) { + stats->validated_checksum_ns += php_pcov_dump_now() - started; + started = php_pcov_dump_now(); + } + smart_str_appendl(buffer, "PCOVREC\0", 8); + php_pcov_dump_append_u32(buffer, PCOV_RECORD_FORMAT_VERSION); + php_pcov_dump_append_u32(buffer, PCOV_RECORD_HEADER_SIZE); + php_pcov_dump_append_u32(buffer, record_type); + php_pcov_dump_append_u32(buffer, 0); + php_pcov_dump_append_u64(buffer, ZSTR_LEN(payload->s)); + php_pcov_dump_append_u32(buffer, checksum); + php_pcov_dump_append_u32(buffer, PHP_VERSION_ID); + php_pcov_dump_append_u32(buffer, PCOV_RECORD_FORMAT_COMPATIBILITY); + php_pcov_dump_append_u32(buffer, 0); + if (profile) started = php_pcov_dump_now(); + smart_str_appendl(buffer, ZSTR_VAL(payload->s), ZSTR_LEN(payload->s)); + if (profile) { + stats->validated_payload_copy_ns += php_pcov_dump_now() - started; + stats->validated_payload_copy_bytes += ZSTR_LEN(payload->s); + } + smart_str_0(buffer); +} /* }}} */ + +static zend_bool php_pcov_dump_serialize_validated_hits( + php_pcov_manifest_file_t *files, + size_t file_count, + php_pcov_dump_hit_t *hits, + size_t hit_count, + const unsigned char environment_id[PHP_PCOV_FINGERPRINT_SIZE], + const unsigned char manifest_id[PHP_PCOV_FINGERPRINT_SIZE], + smart_str *buffer, + php_pcov_dump_stats_t *stats, + zend_bool profile, + int *error) { /* {{{ */ + smart_str payload = {0}; + size_t index; + uint32_t hit_files = 0; + uint64_t started = 0; + + if (file_count > UINT32_MAX || hit_count > UINT32_MAX) { + *error = EOVERFLOW; + return 0; + } + if (hit_count) { + if (profile) { + stats->validated_sort_calls++; + stats->validated_sort_entries += hit_count; + started = php_pcov_dump_now(); + } + qsort(hits, hit_count, sizeof(*hits), php_pcov_dump_hit_compare); + if (profile) { + stats->validated_serialize_sort_ns += php_pcov_dump_now() - started; + } + } + if (profile) started = php_pcov_dump_now(); + for (index = 0; index < hit_count;) { + size_t next = index + 1; + while (next < hit_count && + zend_string_equals(hits[index].file, hits[next].file)) next++; + if (!ZSTR_LEN(hits[index].file) || + ZSTR_LEN(hits[index].file) > UINT32_MAX || + next - index > UINT32_MAX || hit_files == UINT32_MAX) { + *error = EOVERFLOW; + return 0; + } + hit_files++; + index = next; + } + if (profile) { + stats->validated_group_ns += php_pcov_dump_now() - started; + stats->validated_group_entries += hit_count; + } + + smart_str_appendl(&payload, (const char *) environment_id, + PHP_PCOV_FINGERPRINT_SIZE); + smart_str_appendl(&payload, (const char *) manifest_id, + PHP_PCOV_FINGERPRINT_SIZE); + php_pcov_dump_append_u32(&payload, PCOV_MANIFEST_REASON_MATCH); + php_pcov_dump_append_u32(&payload, (uint32_t) file_count); + if (profile) started = php_pcov_dump_now(); + for (index = 0; index < file_count; index++) { + if (!ZSTR_LEN(files[index].file) || + ZSTR_LEN(files[index].file) > UINT32_MAX || files[index].status) { + *error = EINVAL; + smart_str_free(&payload); + return 0; + } + php_pcov_dump_append_u32(&payload, (uint32_t) ZSTR_LEN(files[index].file)); + smart_str_appendl(&payload, ZSTR_VAL(files[index].file), + ZSTR_LEN(files[index].file)); + smart_str_appendl(&payload, (const char *) files[index].fingerprint, + PHP_PCOV_FINGERPRINT_SIZE); + } + if (profile) { + stats->validated_file_metadata_ns += php_pcov_dump_now() - started; + stats->validated_file_metadata_entries += file_count; + started = php_pcov_dump_now(); + } + php_pcov_dump_append_u32(&payload, hit_files); + for (index = 0; index < hit_count;) { + size_t next = index + 1; + size_t line; + while (next < hit_count && + zend_string_equals(hits[index].file, hits[next].file)) next++; + php_pcov_dump_append_u32(&payload, (uint32_t) ZSTR_LEN(hits[index].file)); + smart_str_appendl(&payload, ZSTR_VAL(hits[index].file), + ZSTR_LEN(hits[index].file)); + php_pcov_dump_append_u32(&payload, (uint32_t) (next - index)); + for (line = index; line < next; line++) { + php_pcov_dump_append_u32(&payload, hits[line].line); + } + index = next; + } + if (profile) { + stats->validated_emit_ns += php_pcov_dump_now() - started; + stats->validated_emit_entries += hit_count; + stats->validated_payload_bytes = ZSTR_LEN(payload.s); + started = php_pcov_dump_now(); + } + + php_pcov_record_envelope( + buffer, PCOV_RECORD_HITS, &payload, stats, profile); + if (profile) { + uint64_t logical = stats->hit_array_allocated_bytes + + ZSTR_LEN(payload.s) + ZSTR_LEN(buffer->s); + stats->validated_envelope_ns += php_pcov_dump_now() - started; + stats->validated_output_bytes = ZSTR_LEN(buffer->s); + if (logical > stats->temporary_logical_peak_bytes) { + stats->temporary_logical_peak_bytes = logical; + } + } + stats->coverage_files = hit_files; + stats->executable_lines = hit_count; + smart_str_free(&payload); + return 1; +} /* }}} */ + +static zend_bool php_pcov_dump_serialize_validated_full( + php_pcov_manifest_file_t *files, + size_t file_count, + zval *coverage, + const unsigned char environment_id[PHP_PCOV_FINGERPRINT_SIZE], + const unsigned char manifest_id[PHP_PCOV_FINGERPRINT_SIZE], + uint32_t reason, + smart_str *buffer, + php_pcov_dump_stats_t *stats, + int *error) { /* {{{ */ + smart_str payload = {0}; + size_t index; + + if (file_count > UINT32_MAX) { + *error = EOVERFLOW; + return 0; + } + smart_str_appendl(&payload, (const char *) environment_id, + PHP_PCOV_FINGERPRINT_SIZE); + smart_str_appendl(&payload, (const char *) manifest_id, + PHP_PCOV_FINGERPRINT_SIZE); + php_pcov_dump_append_u32(&payload, reason); + php_pcov_dump_append_u32(&payload, (uint32_t) file_count); + + for (index = 0; index < file_count; index++) { + zval *lines = zend_hash_find(Z_ARRVAL_P(coverage), files[index].file); + php_pcov_dump_line_t *records = NULL; + size_t count = lines && Z_TYPE_P(lines) == IS_ARRAY ? + zend_hash_num_elements(Z_ARRVAL_P(lines)) : 0; + size_t position = 0; + + if (!ZSTR_LEN(files[index].file) || + ZSTR_LEN(files[index].file) > UINT32_MAX || count > UINT32_MAX) { + *error = EOVERFLOW; + smart_str_free(&payload); + return 0; + } + if (count) { + zend_ulong line; + zval *value; + records = safe_emalloc(count, sizeof(*records), 0); + ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(lines), line, value) { + if (line > UINT32_MAX || Z_TYPE_P(value) != IS_LONG || + (Z_LVAL_P(value) != -1 && Z_LVAL_P(value) != 1)) { + *error = EINVAL; + efree(records); + smart_str_free(&payload); + return 0; + } + records[position].line = (uint32_t) line; + records[position].value = (int32_t) Z_LVAL_P(value); + position++; + } ZEND_HASH_FOREACH_END(); + qsort(records, count, sizeof(*records), php_pcov_dump_line_compare); + } + + php_pcov_dump_append_u32(&payload, (uint32_t) ZSTR_LEN(files[index].file)); + smart_str_appendl(&payload, ZSTR_VAL(files[index].file), + ZSTR_LEN(files[index].file)); + php_pcov_dump_append_u32(&payload, files[index].status); + smart_str_appendl(&payload, (const char *) files[index].fingerprint, + PHP_PCOV_FINGERPRINT_SIZE); + php_pcov_dump_append_u32(&payload, (uint32_t) count); + for (position = 0; position < count; position++) { + php_pcov_dump_append_u32(&payload, records[position].line); + php_pcov_dump_append_u32(&payload, (uint32_t) records[position].value); + } + if (records) efree(records); + } + + php_pcov_record_envelope( + buffer, PCOV_RECORD_FULL, &payload, stats, 0); + stats->coverage_files = file_count; + smart_str_free(&payload); + return 1; +} /* }}} */ + +static zend_bool php_pcov_dump_write_all(int fd, const char *data, size_t size) { /* {{{ */ + while (size) { + ssize_t written = write(fd, data, size); + + if (written < 0) { + if (errno == EINTR) { + continue; + } + return 0; + } + + if (written == 0) { + errno = EIO; + return 0; + } + + data += written; + size -= (size_t) written; + } + + return 1; +} /* }}} */ + +static zend_bool php_pcov_dump_publish( + zend_string *path, + zend_string *contents, + zend_long sequence, + php_pcov_dump_stats_t *stats) { /* {{{ */ + char *temporary; + size_t capacity; + int descriptor = -1; + int attempt; + int saved = 0; + uint64_t started = php_pcov_dump_now(); + + if (!ZSTR_LEN(path) || strlen(ZSTR_VAL(path)) != ZSTR_LEN(path)) { + stats->error = EINVAL; + stats->phase = PCOV_DUMP_PHASE_OPEN; + return 0; + } + + capacity = ZSTR_LEN(path) + 96; + temporary = emalloc(capacity); + + for (attempt = 0; attempt < 16; attempt++) { + int length = snprintf( + temporary, + capacity, + "%s.pcovtmp.%ld.%ld.%d", + ZSTR_VAL(path), + (long) getpid(), + (long) sequence, + attempt); + + if (length < 0 || (size_t) length >= capacity) { + saved = ENAMETOOLONG; + break; + } + + descriptor = open( + temporary, + O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, + 0600); + + if (descriptor >= 0 || errno != EEXIST) { + break; + } + } + + if (descriptor < 0) { + stats->error = saved ? saved : errno; + stats->phase = PCOV_DUMP_PHASE_OPEN; + efree(temporary); + return 0; + } + + if (!php_pcov_dump_write_all( + descriptor, ZSTR_VAL(contents), ZSTR_LEN(contents))) { + saved = errno; + stats->phase = PCOV_DUMP_PHASE_WRITE; + goto fail; + } + + if (close(descriptor) != 0) { + saved = errno; + descriptor = -1; + stats->phase = PCOV_DUMP_PHASE_CLOSE; + goto fail; + } + descriptor = -1; + + if (rename(temporary, ZSTR_VAL(path)) != 0) { + saved = errno; + stats->phase = PCOV_DUMP_PHASE_RENAME; + goto fail; + } + + stats->bytes = ZSTR_LEN(contents); + stats->write_ns = php_pcov_dump_now() - started; + efree(temporary); + return 1; + +fail: + if (descriptor >= 0) { + close(descriptor); + } + unlink(temporary); + stats->error = saved; + stats->write_ns = php_pcov_dump_now() - started; + efree(temporary); + return 0; +} /* }}} */ + +static zend_bool php_pcov_dump_export( + zend_string *path, + const unsigned char environment_id[PHP_PCOV_FINGERPRINT_SIZE], + zend_long type, + zval *filter, + zend_long sequence, + php_pcov_dump_stats_t *stats) { /* {{{ */ + php_pcov_manifest_file_t *files = NULL; + size_t file_count = 0; + zval coverage; + smart_str serialized = {0}; + uint64_t started; + uint32_t reason = PCOV_MANIFEST_REASON_MATCH; + unsigned char zero_id[PHP_PCOV_FINGERPRINT_SIZE] = {0}; + int error = 0; + zend_bool success; + + started = php_pcov_dump_now(); + php_pcov_collect_data_force_profiled( + type, filter, &coverage, + php_pcov_dump_benchmark_enabled() ? stats : NULL); + stats->collect_ns = php_pcov_dump_now() - started; + if (!php_pcov_manifest_collect_files( + type, filter, &files, &file_count, stats, &reason, &error)) { + stats->error = error; + stats->phase = PCOV_DUMP_PHASE_COLLECT; + zval_ptr_dtor(&coverage); + return 0; + } + + started = php_pcov_dump_now(); + success = php_pcov_dump_serialize_validated_full( + files, file_count, &coverage, environment_id, zero_id, + PCOV_MANIFEST_REASON_EXPLICIT_FULL, + &serialized, stats, &error); + stats->serialize_ns = php_pcov_dump_now() - started; + + if (!success) { + stats->error = error; + stats->phase = PCOV_DUMP_PHASE_SERIALIZE; + if (files) efree(files); + zval_ptr_dtor(&coverage); + smart_str_free(&serialized); + return 0; + } + + success = php_pcov_dump_publish( + path, serialized.s, sequence, stats); + + if (files) efree(files); + zval_ptr_dtor(&coverage); + smart_str_free(&serialized); + return success; +} /* }}} */ + +static zend_bool php_pcov_dump_export_validated( + zend_string *path, + zend_string *manifest_path, + const unsigned char environment_id[PHP_PCOV_FINGERPRINT_SIZE], + zend_long type, + zval *filter, + zend_long sequence, + php_pcov_dump_stats_t *stats, + unsigned char manifest_id[PHP_PCOV_FINGERPRINT_SIZE]) { /* {{{ */ + php_pcov_manifest_file_t *files = NULL; + size_t file_count = 0; + php_pcov_manifest_view_t manifest; + php_pcov_dump_hit_t *hits = NULL; + size_t hit_count = 0; + uint32_t reason = PCOV_MANIFEST_REASON_MATCH; + int error = 0; + zend_bool loaded; + zend_bool matched = 0; + zend_bool success; + zend_bool benchmark = php_pcov_dump_benchmark_enabled(); + uint64_t started = php_pcov_dump_now(); + smart_str serialized = {0}; + + memset(&manifest, 0, sizeof(manifest)); + memset(manifest_id, 0, PHP_PCOV_FINGERPRINT_SIZE); + if (!php_pcov_manifest_collect_files( + type, filter, &files, &file_count, stats, &reason, &error)) { + stats->error = error; + stats->phase = PCOV_DUMP_PHASE_COLLECT; + return 0; + } + + started = php_pcov_dump_now(); + loaded = php_pcov_manifest_load( + manifest_path, &manifest, &reason, &error, stats, benchmark); + stats->manifest_parse_ns = php_pcov_dump_now() - started; + if (loaded) { + memcpy(manifest_id, manifest.manifest_id, PHP_PCOV_FINGERPRINT_SIZE); + if (reason == PCOV_MANIFEST_REASON_MATCH) { + started = benchmark ? php_pcov_dump_now() : 0; + matched = php_pcov_manifest_validate( + &manifest, environment_id, files, file_count, &reason); + if (benchmark) { + stats->manifest_match_ns += php_pcov_dump_now() - started; + } + } + } + if (matched) { + started = php_pcov_dump_now(); + success = php_pcov_dump_collect_hits( + type, filter, &hits, &hit_count, stats, 1, &error); + stats->collect_ns = php_pcov_dump_now() - started; + if (!success) { + stats->error = error; + stats->phase = PCOV_DUMP_PHASE_COLLECT; + goto cleanup; + } + /* Some generated op arrays execute bookkeeping opcodes carrying line 0. + * CFG discovery does not expose those as executable coverage lines, so + * normal pcov\collect() cannot return them. They are trace artifacts, + * not representable coverage records. */ + if (hit_count) { + size_t read; + size_t write = 0; + for (read = 0; read < hit_count; read++) { + if (hits[read].line != 0) { + hits[write++] = hits[read]; + } + } + hit_count = write; + } + started = php_pcov_dump_now(); + if (!php_pcov_manifest_validate_hits( + &manifest, hits, hit_count, stats, benchmark)) { + matched = 0; + reason = PCOV_MANIFEST_REASON_UNKNOWN_HIT; + } + started = php_pcov_dump_now() - started; + stats->validation_ns += started; + if (benchmark) stats->manifest_hit_validation_ns += started; + } + stats->validation_ns += stats->fingerprint_ns + stats->manifest_parse_ns; + stats->manifest_match = matched; + stats->full_fallback = !matched; + stats->mismatch_reason = reason; + + if (matched) { + started = php_pcov_dump_now(); + success = php_pcov_dump_serialize_validated_hits( + files, file_count, hits, hit_count, environment_id, + manifest.manifest_id, &serialized, stats, benchmark, &error); + stats->serialize_ns = php_pcov_dump_now() - started; + } else { + zval coverage; + + started = php_pcov_dump_now(); + php_pcov_collect_data_force_profiled( + type, filter, &coverage, stats); + stats->collect_ns = php_pcov_dump_now() - started; + started = php_pcov_dump_now(); + success = php_pcov_dump_serialize_validated_full( + files, file_count, &coverage, environment_id, manifest_id, + reason, &serialized, stats, &error); + stats->serialize_ns = php_pcov_dump_now() - started; + zval_ptr_dtor(&coverage); + } + +cleanup: + if (hits) efree(hits); + if (loaded) php_pcov_manifest_view_destroy(&manifest); + if (files) efree(files); + if (!success) { + stats->error = error; + stats->phase = PCOV_DUMP_PHASE_SERIALIZE; + smart_str_free(&serialized); + return 0; + } + success = php_pcov_dump_publish(path, serialized.s, sequence, stats); + smart_str_free(&serialized); + return success; +} /* }}} */ + +static zend_bool php_pcov_dump_valid_type(zend_long type) { /* {{{ */ + if (type == PCOV_FILTER_ALL || + type == PCOV_FILTER_INCLUDE || + type == PCOV_FILTER_EXCLUDE) { + return 1; + } + + zend_throw_error(zend_ce_type_error, + "type must be " + "\\pcov\\inclusive, " + "\\pcov\\exclusive, or \\pcov\\all"); + return 0; +} /* }}} */ + +static void php_pcov_dump_warn(php_pcov_dump_stats_t *stats) { /* {{{ */ + if (stats->error) { + php_error_docref(NULL, E_WARNING, + "coverage %s failed: %s", + php_pcov_dump_phase_name(stats->phase), + strerror(stats->error)); + } else { + php_error_docref(NULL, E_WARNING, + "coverage %s failed", + php_pcov_dump_phase_name(stats->phase)); + } +} /* }}} */ + +#endif /* HAVE_PCOV_NATIVE_EXPORT */ + +#ifndef HAVE_PCOV_NATIVE_EXPORT +PHP_PCOV_API uint64_t php_pcov_dump_now(void) { /* {{{ */ + return 0; +} /* }}} */ + +PHP_PCOV_API zend_bool php_pcov_dump_benchmark_enabled(void) { /* {{{ */ + return 0; +} /* }}} */ +#endif + +PHP_PCOV_API void php_pcov_dump_rinit(void) { /* {{{ */ + memset(&PCG(dump_stats), 0, sizeof(PCG(dump_stats))); + PCG(dump_sequence) = 0; +} /* }}} */ + +PHP_NAMED_FUNCTION(php_pcov_export) { /* {{{ */ + zend_string *path; + zend_long type = PCOV_FILTER_ALL; + zval *filter = NULL; + zend_string *manifest_path = NULL; + zend_string *deployment_hex = NULL; +#ifdef HAVE_PCOV_NATIVE_EXPORT + unsigned char deployment_id[PHP_PCOV_FINGERPRINT_SIZE]; + unsigned char manifest_id[PHP_PCOV_FINGERPRINT_SIZE]; +#endif + + if (zend_parse_parameters( + ZEND_NUM_ARGS(), "S|S!S!la", &path, + &manifest_path, &deployment_hex, &type, &filter) != SUCCESS) { + return; + } + + if (!php_pcov_api_enabled() || !PCG(ini.large_codebase)) { + RETURN_FALSE; + } + if (manifest_path != NULL && deployment_hex == NULL) { + zend_throw_error(zend_ce_type_error, + "deployment_id is required when manifest is provided"); + return; + } + +#ifdef HAVE_PCOV_NATIVE_EXPORT + { + php_coverage_t **previous = PCG(last); + struct rusage before; + struct rusage after; + zend_bool success; + zend_string *id_hex; + uint64_t export_started; + + if (!php_pcov_dump_valid_type(type)) { + return; + } + if (deployment_hex && + !php_pcov_dump_hex_id(deployment_hex, deployment_id)) { + zend_throw_error(zend_ce_type_error, + "deployment_id must be exactly 64 hexadecimal characters"); + return; + } + + memset(&PCG(dump_stats), 0, sizeof(PCG(dump_stats))); + PCG(dump_stats).valid = 1; + PCG(dump_sequence)++; + getrusage(RUSAGE_SELF, &before); + export_started = php_pcov_dump_now(); + if (!deployment_hex) { + memset(deployment_id, 0, sizeof(deployment_id)); + } + if (manifest_path) { + success = php_pcov_dump_export_validated( + path, manifest_path, deployment_id, type, filter, + PCG(dump_sequence), &PCG(dump_stats), manifest_id); + } else { + memset(manifest_id, 0, sizeof(manifest_id)); + success = php_pcov_dump_export( + path, deployment_id, type, filter, + PCG(dump_sequence), &PCG(dump_stats)); + } + PCG(dump_stats).export_ns = php_pcov_dump_now() - export_started; + getrusage(RUSAGE_SELF, &after); + php_pcov_dump_usage_delta(&PCG(dump_stats), &before, &after); + + if (!success) { + PCG(last) = previous; + php_pcov_dump_warn(&PCG(dump_stats)); + RETURN_FALSE; + } + + array_init(return_value); + if (manifest_path) { + add_assoc_string(return_value, "mode", PCG(dump_stats).manifest_match ? + "hit-only" : "full-fallback"); + add_assoc_string(return_value, "reason", (char *) + php_pcov_manifest_reason_name(PCG(dump_stats).mismatch_reason)); + } else { + add_assoc_string(return_value, "mode", "full"); + add_assoc_string(return_value, "reason", "explicit-full"); + } + id_hex = php_pcov_dump_id_hex(manifest_id); + add_assoc_str(return_value, "manifest_id", id_hex); + add_assoc_long(return_value, "validation_files", + (zend_long) PCG(dump_stats).validation_files); + add_assoc_long(return_value, "validation_ns", + (zend_long) PCG(dump_stats).validation_ns); + add_assoc_long(return_value, "fingerprint_ns", + (zend_long) PCG(dump_stats).fingerprint_ns); + add_assoc_long(return_value, "manifest_parse_ns", + (zend_long) PCG(dump_stats).manifest_parse_ns); + } +#else + (void) manifest_path; + (void) deployment_hex; + (void) type; + (void) filter; + php_error_docref(NULL, E_WARNING, + "large-codebase coverage export is unavailable on this platform"); + RETURN_FALSE; +#endif +} /* }}} */ + +PHP_NAMED_FUNCTION(php_pcov_export_stats) { /* {{{ */ + if (zend_parse_parameters_none() != SUCCESS) { + return; + } + + array_init(return_value); + if (!PCG(dump_stats).valid) { + return; + } + + add_assoc_long(return_value, "collect_ns", (zend_long) PCG(dump_stats).collect_ns); + add_assoc_long(return_value, "range_filter_ns", (zend_long) PCG(dump_stats).range_filter_ns); + add_assoc_long(return_value, "hit_traversal_ns", (zend_long) PCG(dump_stats).hit_traversal_ns); + add_assoc_long(return_value, "cfg_discovery_ns", (zend_long) PCG(dump_stats).cfg_discovery_ns); + add_assoc_long(return_value, "array_construction_ns", (zend_long) PCG(dump_stats).array_construction_ns); + add_assoc_long(return_value, "native_record_ns", (zend_long) PCG(dump_stats).native_record_ns); + add_assoc_long(return_value, "serialize_ns", (zend_long) PCG(dump_stats).serialize_ns); + add_assoc_long(return_value, "write_ns", (zend_long) PCG(dump_stats).write_ns); + add_assoc_long(return_value, "manifest_parse_ns", (zend_long) PCG(dump_stats).manifest_parse_ns); + add_assoc_long(return_value, "manifest_open_ns", (zend_long) PCG(dump_stats).manifest_open_ns); + add_assoc_long(return_value, "manifest_stat_ns", (zend_long) PCG(dump_stats).manifest_stat_ns); + add_assoc_long(return_value, "manifest_allocation_ns", (zend_long) PCG(dump_stats).manifest_allocation_ns); + add_assoc_long(return_value, "manifest_read_ns", (zend_long) PCG(dump_stats).manifest_read_ns); + add_assoc_long(return_value, "manifest_close_ns", (zend_long) PCG(dump_stats).manifest_close_ns); + add_assoc_long(return_value, "manifest_header_ns", (zend_long) PCG(dump_stats).manifest_header_ns); + add_assoc_long(return_value, "manifest_checksum_ns", (zend_long) PCG(dump_stats).manifest_checksum_ns); + add_assoc_long(return_value, "manifest_record_ns", (zend_long) PCG(dump_stats).manifest_record_ns); + add_assoc_long(return_value, "manifest_match_ns", (zend_long) PCG(dump_stats).manifest_match_ns); + add_assoc_long(return_value, "manifest_hit_sort_ns", (zend_long) PCG(dump_stats).manifest_hit_sort_ns); + add_assoc_long(return_value, "manifest_hit_file_lookup_ns", (zend_long) PCG(dump_stats).manifest_hit_file_lookup_ns); + add_assoc_long(return_value, "manifest_hit_line_lookup_ns", (zend_long) PCG(dump_stats).manifest_hit_line_lookup_ns); + add_assoc_long(return_value, "manifest_hit_validation_ns", (zend_long) PCG(dump_stats).manifest_hit_validation_ns); + add_assoc_long(return_value, "manifest_open_calls", (zend_long) PCG(dump_stats).manifest_open_calls); + add_assoc_long(return_value, "manifest_stat_calls", (zend_long) PCG(dump_stats).manifest_stat_calls); + add_assoc_long(return_value, "manifest_allocations", (zend_long) PCG(dump_stats).manifest_allocations); + add_assoc_long(return_value, "manifest_allocated_bytes", (zend_long) PCG(dump_stats).manifest_allocated_bytes); + add_assoc_long(return_value, "manifest_read_calls", (zend_long) PCG(dump_stats).manifest_read_calls); + add_assoc_long(return_value, "manifest_bytes_read", (zend_long) PCG(dump_stats).manifest_bytes_read); + add_assoc_long(return_value, "manifest_checksum_calls", (zend_long) PCG(dump_stats).manifest_checksum_calls); + add_assoc_long(return_value, "manifest_records_validated", (zend_long) PCG(dump_stats).manifest_records_validated); + add_assoc_long(return_value, "manifest_lines_validated", (zend_long) PCG(dump_stats).manifest_lines_validated); + add_assoc_long(return_value, "manifest_hit_files_looked_up", (zend_long) PCG(dump_stats).manifest_hit_files_looked_up); + add_assoc_long(return_value, "manifest_hit_entries_validated", (zend_long) PCG(dump_stats).manifest_hit_entries_validated); + add_assoc_long(return_value, "manifest_hit_duplicates", (zend_long) PCG(dump_stats).manifest_hit_duplicates); + add_assoc_long(return_value, "manifest_hit_line_comparisons", (zend_long) PCG(dump_stats).manifest_hit_line_comparisons); + add_assoc_long(return_value, "manifest_hit_line_records_decoded", (zend_long) PCG(dump_stats).manifest_hit_line_records_decoded); + add_assoc_long(return_value, "manifest_hit_search_restarts", (zend_long) PCG(dump_stats).manifest_hit_search_restarts); + add_assoc_long(return_value, "manifest_hit_matches", (zend_long) PCG(dump_stats).manifest_hit_matches); + add_assoc_long(return_value, "manifest_hit_unknown_lines", (zend_long) PCG(dump_stats).manifest_hit_unknown_lines); + add_assoc_long(return_value, "manifest_hit_unknown_files", (zend_long) PCG(dump_stats).manifest_hit_unknown_files); + add_assoc_long(return_value, "validated_serialize_sort_ns", (zend_long) PCG(dump_stats).validated_serialize_sort_ns); + add_assoc_long(return_value, "validated_group_ns", (zend_long) PCG(dump_stats).validated_group_ns); + add_assoc_long(return_value, "validated_payload_size_ns", (zend_long) PCG(dump_stats).validated_payload_size_ns); + add_assoc_long(return_value, "validated_file_metadata_ns", (zend_long) PCG(dump_stats).validated_file_metadata_ns); + add_assoc_long(return_value, "validated_emit_ns", (zend_long) PCG(dump_stats).validated_emit_ns); + add_assoc_long(return_value, "validated_envelope_ns", (zend_long) PCG(dump_stats).validated_envelope_ns); + add_assoc_long(return_value, "validated_checksum_ns", (zend_long) PCG(dump_stats).validated_checksum_ns); + add_assoc_long(return_value, "validated_payload_copy_ns", (zend_long) PCG(dump_stats).validated_payload_copy_ns); + add_assoc_long(return_value, "validated_sort_calls", (zend_long) PCG(dump_stats).validated_sort_calls); + add_assoc_long(return_value, "validated_sort_entries", (zend_long) PCG(dump_stats).validated_sort_entries); + add_assoc_long(return_value, "validated_group_entries", (zend_long) PCG(dump_stats).validated_group_entries); + add_assoc_long(return_value, "validated_payload_size_entries", (zend_long) PCG(dump_stats).validated_payload_size_entries); + add_assoc_long(return_value, "validated_file_metadata_entries", (zend_long) PCG(dump_stats).validated_file_metadata_entries); + add_assoc_long(return_value, "validated_emit_entries", (zend_long) PCG(dump_stats).validated_emit_entries); + add_assoc_long(return_value, "validated_checksum_calls", (zend_long) PCG(dump_stats).validated_checksum_calls); + add_assoc_long(return_value, "validated_checksum_bytes", (zend_long) PCG(dump_stats).validated_checksum_bytes); + add_assoc_long(return_value, "validated_payload_copy_bytes", (zend_long) PCG(dump_stats).validated_payload_copy_bytes); + add_assoc_long(return_value, "validated_payload_bytes", (zend_long) PCG(dump_stats).validated_payload_bytes); + add_assoc_long(return_value, "validated_output_bytes", (zend_long) PCG(dump_stats).validated_output_bytes); + add_assoc_long(return_value, "hit_array_allocations", (zend_long) PCG(dump_stats).hit_array_allocations); + add_assoc_long(return_value, "hit_array_allocated_bytes", (zend_long) PCG(dump_stats).hit_array_allocated_bytes); + add_assoc_long(return_value, "temporary_logical_peak_bytes", (zend_long) PCG(dump_stats).temporary_logical_peak_bytes); + add_assoc_long(return_value, "export_ns", (zend_long) PCG(dump_stats).export_ns); + add_assoc_long(return_value, "fingerprint_ns", (zend_long) PCG(dump_stats).fingerprint_ns); + add_assoc_long(return_value, "validation_ns", (zend_long) PCG(dump_stats).validation_ns); + add_assoc_long(return_value, "compile_fingerprint_ns", (zend_long) PCG(dump_stats).compile_fingerprint_ns); + add_assoc_long(return_value, "bytes", (zend_long) PCG(dump_stats).bytes); + add_assoc_long(return_value, "collection_files", (zend_long) PCG(dump_stats).collection_files); + add_assoc_long(return_value, "discovery_operations", (zend_long) PCG(dump_stats).discovery_operations); + add_assoc_long(return_value, "cfg_op_arrays", (zend_long) PCG(dump_stats).cfg_op_arrays); + add_assoc_long(return_value, "hit_entries", (zend_long) PCG(dump_stats).hit_entries); + add_assoc_long(return_value, "coverage_files", (zend_long) PCG(dump_stats).coverage_files); + add_assoc_long(return_value, "executable_lines", (zend_long) PCG(dump_stats).executable_lines); + add_assoc_long(return_value, "validation_files", (zend_long) PCG(dump_stats).validation_files); + add_assoc_long(return_value, "compile_fingerprint_files", (zend_long) PCG(dump_stats).compile_fingerprint_files); + add_assoc_long(return_value, "fingerprint_calls", (zend_long) PCG(dump_stats).fingerprint_metrics.calls); + add_assoc_long(return_value, "fingerprint_open_calls", (zend_long) PCG(dump_stats).fingerprint_metrics.open_calls); + add_assoc_long(return_value, "fingerprint_open_ns", (zend_long) PCG(dump_stats).fingerprint_metrics.open_ns); + add_assoc_long(return_value, "fingerprint_stat_calls", (zend_long) PCG(dump_stats).fingerprint_metrics.stat_calls); + add_assoc_long(return_value, "fingerprint_stat_ns", (zend_long) PCG(dump_stats).fingerprint_metrics.stat_ns); + add_assoc_long(return_value, "fingerprint_read_calls", (zend_long) PCG(dump_stats).fingerprint_metrics.read_calls); + add_assoc_long(return_value, "fingerprint_read_ns", (zend_long) PCG(dump_stats).fingerprint_metrics.read_ns); + add_assoc_long(return_value, "fingerprint_bytes_read", (zend_long) PCG(dump_stats).fingerprint_metrics.bytes_read); + add_assoc_long(return_value, "fingerprint_sha_ns", (zend_long) PCG(dump_stats).fingerprint_metrics.sha_ns); + add_assoc_long(return_value, "fingerprint_close_ns", (zend_long) PCG(dump_stats).fingerprint_metrics.close_ns); + add_assoc_long(return_value, "fingerprint_races", (zend_long) PCG(dump_stats).fingerprint_metrics.races); + add_assoc_long(return_value, "fingerprint_failures", (zend_long) PCG(dump_stats).fingerprint_metrics.failures); + add_assoc_long(return_value, "compile_fingerprint_calls", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.calls); + add_assoc_long(return_value, "compile_fingerprint_open_calls", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.open_calls); + add_assoc_long(return_value, "compile_fingerprint_open_ns", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.open_ns); + add_assoc_long(return_value, "compile_fingerprint_stat_calls", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.stat_calls); + add_assoc_long(return_value, "compile_fingerprint_stat_ns", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.stat_ns); + add_assoc_long(return_value, "compile_fingerprint_read_calls", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.read_calls); + add_assoc_long(return_value, "compile_fingerprint_read_ns", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.read_ns); + add_assoc_long(return_value, "compile_fingerprint_bytes_read", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.bytes_read); + add_assoc_long(return_value, "compile_fingerprint_sha_ns", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.sha_ns); + add_assoc_long(return_value, "compile_fingerprint_close_ns", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.close_ns); + add_assoc_long(return_value, "compile_fingerprint_races", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.races); + add_assoc_long(return_value, "compile_fingerprint_failures", (zend_long) PCG(dump_stats).compile_fingerprint_metrics.failures); + add_assoc_long(return_value, "fingerprint_reuse_checks", (zend_long) PCG(dump_stats).fingerprint_metrics.reuse_checks); + add_assoc_long(return_value, "fingerprint_reuse_hits", (zend_long) PCG(dump_stats).fingerprint_metrics.reuse_hits); + add_assoc_long(return_value, "fingerprint_reuse_misses", (zend_long) PCG(dump_stats).fingerprint_metrics.reuse_misses); + add_assoc_long(return_value, "fingerprint_reuse_ns", (zend_long) PCG(dump_stats).fingerprint_metrics.reuse_ns); + add_assoc_bool(return_value, "manifest_match", PCG(dump_stats).manifest_match); + add_assoc_bool(return_value, "full_fallback", PCG(dump_stats).full_fallback); + add_assoc_long(return_value, "mismatch_reason", PCG(dump_stats).mismatch_reason); +#ifdef HAVE_PCOV_NATIVE_EXPORT + add_assoc_string(return_value, "mismatch_reason_name", (char *) + php_pcov_manifest_reason_name(PCG(dump_stats).mismatch_reason)); +#else + add_assoc_string(return_value, "mismatch_reason_name", "unsupported"); +#endif + add_assoc_long(return_value, "user_us", (zend_long) PCG(dump_stats).user_us); + add_assoc_long(return_value, "system_us", (zend_long) PCG(dump_stats).system_us); + add_assoc_long(return_value, "minor_faults", (zend_long) PCG(dump_stats).minor_faults); + add_assoc_long(return_value, "major_faults", (zend_long) PCG(dump_stats).major_faults); + add_assoc_long(return_value, "max_rss_kb", (zend_long) PCG(dump_stats).max_rss_kb); + add_assoc_long(return_value, "error", PCG(dump_stats).error); + add_assoc_string(return_value, "phase", + (char *) php_pcov_dump_phase_name(PCG(dump_stats).phase)); +} /* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/pcov_fingerprint.c b/pcov_fingerprint.c new file mode 100644 index 0000000..1968f04 --- /dev/null +++ b/pcov_fingerprint.c @@ -0,0 +1,468 @@ +/* + +----------------------------------------------------------------------+ + | Experimental PCOV manifest fingerprints | + +----------------------------------------------------------------------+ + | This is intentionally self-contained. The manifest experiment must | + | not depend on ext/hash being loaded in every instrumented process. | + +----------------------------------------------------------------------+ +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "pcov_fingerprint.h" + +#include +#include +#include +#include +#include +#include + +#ifdef PHP_WIN32 + +zend_bool php_pcov_fingerprint_file( + zend_string *path, + unsigned char fingerprint[PHP_PCOV_FINGERPRINT_SIZE], + int *error, + php_pcov_fingerprint_metrics_t *metrics, + php_pcov_fingerprint_identity_t *identity) { /* {{{ */ + (void) path; + (void) fingerprint; + (void) metrics; + (void) identity; + *error = ENOTSUP; + return 0; +} /* }}} */ + +zend_bool php_pcov_fingerprint_identity_matches( + zend_string *path, + const php_pcov_fingerprint_identity_t *expected, + int *error, + php_pcov_fingerprint_metrics_t *metrics) { /* {{{ */ + (void) path; + (void) expected; + (void) metrics; + *error = ENOTSUP; + return 0; +} /* }}} */ + +#else +# include +# define PCOV_CLOSE close +# define PCOV_READ read + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + +static uint64_t php_pcov_fingerprint_now(void) { +#ifdef CLOCK_MONOTONIC + struct timespec now; + + if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) { + return ((uint64_t) now.tv_sec * 1000000000ULL) + + (uint64_t) now.tv_nsec; + } +#endif + return 0; +} + +typedef struct _php_pcov_sha256_t { + uint32_t state[8]; + uint64_t bytes; + unsigned char block[64]; + size_t used; +} php_pcov_sha256_t; + +static const uint32_t php_pcov_sha256_constants[64] = { + 0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, + 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U, + 0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U, + 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U, 0xc19bf174U, + 0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU, + 0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU, + 0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, + 0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U, + 0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU, 0x53380d13U, + 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U, + 0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, + 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U, + 0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, + 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U, + 0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, + 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U +}; + +static uint32_t php_pcov_sha256_rotate(uint32_t value, unsigned int count) { + return (value >> count) | (value << (32U - count)); +} + +static uint32_t php_pcov_sha256_read(const unsigned char *bytes) { + return ((uint32_t) bytes[0] << 24) | + ((uint32_t) bytes[1] << 16) | + ((uint32_t) bytes[2] << 8) | + (uint32_t) bytes[3]; +} + +static void php_pcov_sha256_transform( + php_pcov_sha256_t *context, const unsigned char block[64]) { + uint32_t words[64]; + uint32_t a, b, c, d, e, f, g, h; + unsigned int index; + + for (index = 0; index < 16; index++) { + words[index] = php_pcov_sha256_read(block + (index * 4)); + } + for (; index < 64; index++) { + uint32_t x = words[index - 15]; + uint32_t y = words[index - 2]; + uint32_t s0 = php_pcov_sha256_rotate(x, 7) ^ + php_pcov_sha256_rotate(x, 18) ^ (x >> 3); + uint32_t s1 = php_pcov_sha256_rotate(y, 17) ^ + php_pcov_sha256_rotate(y, 19) ^ (y >> 10); + words[index] = words[index - 16] + s0 + words[index - 7] + s1; + } + + a = context->state[0]; b = context->state[1]; + c = context->state[2]; d = context->state[3]; + e = context->state[4]; f = context->state[5]; + g = context->state[6]; h = context->state[7]; + + for (index = 0; index < 64; index++) { + uint32_t s1 = php_pcov_sha256_rotate(e, 6) ^ + php_pcov_sha256_rotate(e, 11) ^ php_pcov_sha256_rotate(e, 25); + uint32_t choice = (e & f) ^ ((~e) & g); + uint32_t first = h + s1 + choice + + php_pcov_sha256_constants[index] + words[index]; + uint32_t s0 = php_pcov_sha256_rotate(a, 2) ^ + php_pcov_sha256_rotate(a, 13) ^ php_pcov_sha256_rotate(a, 22); + uint32_t majority = (a & b) ^ (a & c) ^ (b & c); + uint32_t second = s0 + majority; + + h = g; g = f; f = e; e = d + first; + d = c; c = b; b = a; a = first + second; + } + + context->state[0] += a; context->state[1] += b; + context->state[2] += c; context->state[3] += d; + context->state[4] += e; context->state[5] += f; + context->state[6] += g; context->state[7] += h; +} + +static void php_pcov_sha256_init(php_pcov_sha256_t *context) { + static const uint32_t initial[8] = { + 0x6a09e667U, 0xbb67ae85U, 0x3c6ef372U, 0xa54ff53aU, + 0x510e527fU, 0x9b05688cU, 0x1f83d9abU, 0x5be0cd19U + }; + + memcpy(context->state, initial, sizeof(initial)); + context->bytes = 0; + context->used = 0; +} + +static void php_pcov_sha256_update( + php_pcov_sha256_t *context, const unsigned char *data, size_t length) { + context->bytes += length; + while (length) { + size_t available = sizeof(context->block) - context->used; + size_t copy = length < available ? length : available; + + memcpy(context->block + context->used, data, copy); + context->used += copy; + data += copy; + length -= copy; + if (context->used == sizeof(context->block)) { + php_pcov_sha256_transform(context, context->block); + context->used = 0; + } + } +} + +static void php_pcov_sha256_final( + php_pcov_sha256_t *context, + unsigned char result[PHP_PCOV_FINGERPRINT_SIZE]) { + uint64_t bits = context->bytes * 8U; + unsigned int index; + + context->block[context->used++] = 0x80; + if (context->used > 56) { + memset(context->block + context->used, 0, 64 - context->used); + php_pcov_sha256_transform(context, context->block); + context->used = 0; + } + memset(context->block + context->used, 0, 56 - context->used); + for (index = 0; index < 8; index++) { + context->block[63 - index] = (unsigned char) (bits >> (index * 8)); + } + php_pcov_sha256_transform(context, context->block); + + for (index = 0; index < 8; index++) { + result[index * 4] = (unsigned char) (context->state[index] >> 24); + result[index * 4 + 1] = (unsigned char) (context->state[index] >> 16); + result[index * 4 + 2] = (unsigned char) (context->state[index] >> 8); + result[index * 4 + 3] = (unsigned char) context->state[index]; + } + memset(context, 0, sizeof(*context)); +} + +static zend_bool php_pcov_fingerprint_same_file( + const struct stat *before, const struct stat *after) { + if (!(before->st_dev == after->st_dev && + before->st_ino == after->st_ino && + before->st_mode == after->st_mode && + before->st_size == after->st_size && + before->st_mtime == after->st_mtime && + before->st_ctime == after->st_ctime)) { + return 0; + } +#if defined(__APPLE__) + return before->st_mtimespec.tv_nsec == after->st_mtimespec.tv_nsec && + before->st_ctimespec.tv_nsec == after->st_ctimespec.tv_nsec; +#else + return before->st_mtim.tv_nsec == after->st_mtim.tv_nsec && + before->st_ctim.tv_nsec == after->st_ctim.tv_nsec; +#endif +} + +static void php_pcov_fingerprint_identity_from_stat( + const struct stat *status, + php_pcov_fingerprint_identity_t *identity) { + memset(identity, 0, sizeof(*identity)); + identity->device = (uint64_t) status->st_dev; + identity->inode = (uint64_t) status->st_ino; + identity->size = (uint64_t) status->st_size; + identity->mode = (uint32_t) status->st_mode; +#if defined(__APPLE__) + identity->mtime_sec = (int64_t) status->st_mtimespec.tv_sec; + identity->mtime_nsec = (int64_t) status->st_mtimespec.tv_nsec; + identity->ctime_sec = (int64_t) status->st_ctimespec.tv_sec; + identity->ctime_nsec = (int64_t) status->st_ctimespec.tv_nsec; +#else + identity->mtime_sec = (int64_t) status->st_mtim.tv_sec; + identity->mtime_nsec = (int64_t) status->st_mtim.tv_nsec; + identity->ctime_sec = (int64_t) status->st_ctim.tv_sec; + identity->ctime_nsec = (int64_t) status->st_ctim.tv_nsec; +#endif +} + +static zend_bool php_pcov_fingerprint_identity_equal( + const php_pcov_fingerprint_identity_t *first, + const php_pcov_fingerprint_identity_t *second) { + return memcmp(first, second, sizeof(*first)) == 0; +} + +zend_bool php_pcov_fingerprint_identity_matches( + zend_string *path, + const php_pcov_fingerprint_identity_t *expected, + int *error, + php_pcov_fingerprint_metrics_t *metrics) { /* {{{ */ + php_pcov_fingerprint_identity_t current; + struct stat status; + uint64_t started = metrics ? php_pcov_fingerprint_now() : 0; + uint64_t elapsed; + + if (metrics) { + metrics->reuse_checks++; + metrics->stat_calls++; + } + if (!path || !ZSTR_LEN(path) || + strlen(ZSTR_VAL(path)) != ZSTR_LEN(path) || + VCWD_STAT(ZSTR_VAL(path), &status) != 0 || !S_ISREG(status.st_mode)) { + *error = errno ? errno : EINVAL; + if (metrics) { + metrics->reuse_misses++; + metrics->failures++; + metrics->reuse_ns += php_pcov_fingerprint_now() - started; + } + return 0; + } + php_pcov_fingerprint_identity_from_stat(&status, ¤t); + if (metrics) { + elapsed = php_pcov_fingerprint_now() - started; + metrics->reuse_ns += elapsed; + metrics->stat_ns += elapsed; + } + if (!php_pcov_fingerprint_identity_equal(expected, ¤t)) { + *error = EAGAIN; + if (metrics) { + metrics->reuse_misses++; + } + return 0; + } + *error = 0; + if (metrics) { + metrics->reuse_hits++; + } + return 1; +} /* }}} */ + +zend_bool php_pcov_fingerprint_file( + zend_string *path, + unsigned char fingerprint[PHP_PCOV_FINGERPRINT_SIZE], + int *error, + php_pcov_fingerprint_metrics_t *metrics, + php_pcov_fingerprint_identity_t *identity) { /* {{{ */ + unsigned char buffer[32768]; + php_pcov_sha256_t context; + struct stat before; + struct stat after; + struct stat path_after; + int descriptor; + uint64_t started = 0; + + if (metrics) { + metrics->calls++; + } + + if (!path || !ZSTR_LEN(path) || + strlen(ZSTR_VAL(path)) != ZSTR_LEN(path)) { + *error = EINVAL; + if (metrics) { + metrics->failures++; + } + return 0; + } + + if (metrics) { + metrics->open_calls++; + started = php_pcov_fingerprint_now(); + } + descriptor = VCWD_OPEN(ZSTR_VAL(path), O_RDONLY | O_BINARY | O_CLOEXEC); + if (metrics) { + metrics->open_ns += php_pcov_fingerprint_now() - started; + } + if (descriptor < 0) { + *error = errno; + if (metrics) { + metrics->failures++; + } + return 0; + } + if (metrics) { + metrics->stat_calls++; + started = php_pcov_fingerprint_now(); + } + if (fstat(descriptor, &before) != 0 || !S_ISREG(before.st_mode)) { + if (metrics) { + metrics->stat_ns += php_pcov_fingerprint_now() - started; + metrics->failures++; + } + *error = errno ? errno : EINVAL; + PCOV_CLOSE(descriptor); + return 0; + } + if (metrics) { + metrics->stat_ns += php_pcov_fingerprint_now() - started; + } + + php_pcov_sha256_init(&context); + while (1) { + if (metrics) { + metrics->read_calls++; + started = php_pcov_fingerprint_now(); + } + ssize_t received = PCOV_READ(descriptor, buffer, sizeof(buffer)); + if (metrics) { + metrics->read_ns += php_pcov_fingerprint_now() - started; + } + + if (received < 0) { + if (errno == EINTR) { + continue; + } + *error = errno; + if (metrics) { + metrics->failures++; + } + PCOV_CLOSE(descriptor); + return 0; + } + if (received == 0) { + break; + } + if (metrics) { + metrics->bytes_read += (uint64_t) received; + started = php_pcov_fingerprint_now(); + } + php_pcov_sha256_update(&context, buffer, (size_t) received); + if (metrics) { + metrics->sha_ns += php_pcov_fingerprint_now() - started; + } + } + + if (metrics) { + metrics->stat_calls++; + started = php_pcov_fingerprint_now(); + } + if (fstat(descriptor, &after) != 0 || + !php_pcov_fingerprint_same_file(&before, &after)) { + if (metrics) { + metrics->stat_ns += php_pcov_fingerprint_now() - started; + metrics->races++; + metrics->failures++; + } + *error = errno ? errno : EAGAIN; + PCOV_CLOSE(descriptor); + return 0; + } + if (metrics) { + metrics->stat_ns += php_pcov_fingerprint_now() - started; + metrics->stat_calls++; + started = php_pcov_fingerprint_now(); + } + if (VCWD_STAT(ZSTR_VAL(path), &path_after) != 0 || + !php_pcov_fingerprint_same_file(&after, &path_after)) { + if (metrics) { + metrics->stat_ns += php_pcov_fingerprint_now() - started; + metrics->races++; + metrics->failures++; + } + *error = errno ? errno : EAGAIN; + PCOV_CLOSE(descriptor); + return 0; + } + if (metrics) { + metrics->stat_ns += php_pcov_fingerprint_now() - started; + started = php_pcov_fingerprint_now(); + } + if (PCOV_CLOSE(descriptor) != 0) { + if (metrics) { + metrics->close_ns += php_pcov_fingerprint_now() - started; + metrics->failures++; + } + *error = errno; + return 0; + } + if (metrics) { + metrics->close_ns += php_pcov_fingerprint_now() - started; + started = php_pcov_fingerprint_now(); + } + + php_pcov_sha256_final(&context, fingerprint); + if (metrics) { + metrics->sha_ns += php_pcov_fingerprint_now() - started; + } + if (identity) { + php_pcov_fingerprint_identity_from_stat(&after, identity); + } + *error = 0; + return 1; +} /* }}} */ + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/pcov_fingerprint.h b/pcov_fingerprint.h new file mode 100644 index 0000000..7a75d68 --- /dev/null +++ b/pcov_fingerprint.h @@ -0,0 +1,57 @@ +/* + +----------------------------------------------------------------------+ + | Experimental PCOV manifest fingerprints | + +----------------------------------------------------------------------+ +*/ + +#ifndef PCOV_FINGERPRINT_H +#define PCOV_FINGERPRINT_H + +#include "php.h" + +#define PHP_PCOV_FINGERPRINT_SIZE 32 + +typedef struct _php_pcov_fingerprint_metrics_t { + uint64_t calls; + uint64_t open_calls; + uint64_t open_ns; + uint64_t stat_calls; + uint64_t stat_ns; + uint64_t read_calls; + uint64_t read_ns; + uint64_t bytes_read; + uint64_t sha_ns; + uint64_t close_ns; + uint64_t races; + uint64_t failures; + uint64_t reuse_checks; + uint64_t reuse_hits; + uint64_t reuse_misses; + uint64_t reuse_ns; +} php_pcov_fingerprint_metrics_t; + +typedef struct _php_pcov_fingerprint_identity_t { + uint64_t device; + uint64_t inode; + uint64_t size; + uint32_t mode; + int64_t mtime_sec; + int64_t mtime_nsec; + int64_t ctime_sec; + int64_t ctime_nsec; +} php_pcov_fingerprint_identity_t; + +zend_bool php_pcov_fingerprint_file( + zend_string *path, + unsigned char fingerprint[PHP_PCOV_FINGERPRINT_SIZE], + int *error, + php_pcov_fingerprint_metrics_t *metrics, + php_pcov_fingerprint_identity_t *identity); + +zend_bool php_pcov_fingerprint_identity_matches( + zend_string *path, + const php_pcov_fingerprint_identity_t *expected, + int *error, + php_pcov_fingerprint_metrics_t *metrics); + +#endif diff --git a/php_pcov.h b/php_pcov.h index 5fc7587..ce21d9e 100644 --- a/php_pcov.h +++ b/php_pcov.h @@ -21,7 +21,7 @@ extern zend_module_entry pcov_module_entry; #define phpext_pcov_ptr &pcov_module_entry -#define PHP_PCOV_VERSION "1.0.13-dev" +#define PHP_PCOV_VERSION "2.0.0" #ifdef PHP_WIN32 # define PHP_PCOV_API __declspec(dllexport) @@ -35,7 +35,10 @@ extern zend_module_entry pcov_module_entry; #include "TSRM.h" #endif +#include "pcov_fingerprint.h" + typedef struct _php_coverage_t php_coverage_t; +typedef struct _php_pcov_dump_stats_t php_pcov_dump_stats_t; struct _php_coverage_t { zend_string *file; @@ -43,6 +46,97 @@ struct _php_coverage_t { php_coverage_t *next; }; +struct _php_pcov_dump_stats_t { + uint64_t collect_ns; + uint64_t range_filter_ns; + uint64_t hit_traversal_ns; + uint64_t cfg_discovery_ns; + uint64_t array_construction_ns; + uint64_t native_record_ns; + uint64_t serialize_ns; + uint64_t write_ns; + uint64_t manifest_parse_ns; + uint64_t manifest_open_ns; + uint64_t manifest_stat_ns; + uint64_t manifest_allocation_ns; + uint64_t manifest_read_ns; + uint64_t manifest_close_ns; + uint64_t manifest_header_ns; + uint64_t manifest_checksum_ns; + uint64_t manifest_record_ns; + uint64_t manifest_match_ns; + uint64_t manifest_hit_sort_ns; + uint64_t manifest_hit_file_lookup_ns; + uint64_t manifest_hit_line_lookup_ns; + uint64_t manifest_hit_validation_ns; + uint64_t manifest_open_calls; + uint64_t manifest_stat_calls; + uint64_t manifest_allocations; + uint64_t manifest_allocated_bytes; + uint64_t manifest_read_calls; + uint64_t manifest_bytes_read; + uint64_t manifest_checksum_calls; + uint64_t manifest_records_validated; + uint64_t manifest_lines_validated; + uint64_t manifest_hit_files_looked_up; + uint64_t manifest_hit_entries_validated; + uint64_t manifest_hit_duplicates; + uint64_t manifest_hit_line_comparisons; + uint64_t manifest_hit_line_records_decoded; + uint64_t manifest_hit_search_restarts; + uint64_t manifest_hit_matches; + uint64_t manifest_hit_unknown_lines; + uint64_t manifest_hit_unknown_files; + uint64_t validated_serialize_sort_ns; + uint64_t validated_group_ns; + uint64_t validated_payload_size_ns; + uint64_t validated_file_metadata_ns; + uint64_t validated_emit_ns; + uint64_t validated_envelope_ns; + uint64_t validated_checksum_ns; + uint64_t validated_payload_copy_ns; + uint64_t validated_sort_calls; + uint64_t validated_sort_entries; + uint64_t validated_group_entries; + uint64_t validated_payload_size_entries; + uint64_t validated_file_metadata_entries; + uint64_t validated_emit_entries; + uint64_t validated_checksum_calls; + uint64_t validated_checksum_bytes; + uint64_t validated_payload_copy_bytes; + uint64_t validated_payload_bytes; + uint64_t validated_output_bytes; + uint64_t hit_array_allocations; + uint64_t hit_array_allocated_bytes; + uint64_t temporary_logical_peak_bytes; + uint64_t export_ns; + uint64_t fingerprint_ns; + uint64_t validation_ns; + uint64_t compile_fingerprint_ns; + uint64_t bytes; + uint64_t collection_files; + uint64_t discovery_operations; + uint64_t cfg_op_arrays; + uint64_t hit_entries; + uint64_t coverage_files; + uint64_t executable_lines; + uint64_t validation_files; + uint64_t compile_fingerprint_files; + php_pcov_fingerprint_metrics_t fingerprint_metrics; + php_pcov_fingerprint_metrics_t compile_fingerprint_metrics; + int64_t user_us; + int64_t system_us; + int64_t minor_faults; + int64_t major_faults; + int64_t max_rss_kb; + int error; + int phase; + zend_bool valid; + zend_bool manifest_match; + zend_bool full_fallback; + uint32_t mismatch_reason; +}; + ZEND_BEGIN_MODULE_GLOBALS(pcov) zend_bool enabled; zend_arena *mem; @@ -55,10 +149,17 @@ ZEND_BEGIN_MODULE_GLOBALS(pcov) HashTable wants; HashTable discovered; HashTable covered; + HashTable fingerprints; + php_pcov_dump_stats_t dump_stats; + zend_long dump_sequence; + uint64_t compile_fingerprint_ns; + uint64_t compile_fingerprint_files; + php_pcov_fingerprint_metrics_t compile_fingerprint_metrics; zend_string *directory; pcre_cache_entry *exclude; struct { zend_bool enabled; + zend_bool large_codebase; zend_long memory; zend_long files; char *directory; @@ -66,8 +167,23 @@ ZEND_BEGIN_MODULE_GLOBALS(pcov) } ini; ZEND_END_MODULE_GLOBALS(pcov) +ZEND_EXTERN_MODULE_GLOBALS(pcov) + #define PCG(v) ZEND_MODULE_GLOBALS_ACCESSOR(pcov, v) +PHP_PCOV_API void php_pcov_collect_data(zend_long type, zval *filter, zval *return_value); +PHP_PCOV_API void php_pcov_collect_data_profiled( + zend_long type, zval *filter, zval *return_value, php_pcov_dump_stats_t *stats); +PHP_PCOV_API void php_pcov_collect_data_force_profiled( + zend_long type, zval *filter, zval *return_value, php_pcov_dump_stats_t *stats); +PHP_PCOV_API uint64_t php_pcov_dump_now(void); +PHP_PCOV_API zend_bool php_pcov_dump_benchmark_enabled(void); +PHP_PCOV_API bool php_pcov_api_enabled(void); +PHP_PCOV_API void php_pcov_dump_rinit(void); + +PHP_NAMED_FUNCTION(php_pcov_export); +PHP_NAMED_FUNCTION(php_pcov_export_stats); + #if defined(ZTS) && defined(COMPILE_DL_PCOV) ZEND_TSRMLS_CACHE_EXTERN() #endif diff --git a/tests/006.phpt b/tests/006.phpt index 3e615d4..cea46c1 100644 --- a/tests/006.phpt +++ b/tests/006.phpt @@ -4,6 +4,8 @@ phpinfo --INI-- pcov.enabled = 0 +pcov.directory = +pcov.exclude = --FILE-- Disabled PCOV version => %s +pcov.large_codebase => enabled pcov.directory => auto pcov.exclude => none pcov.initial.memory => 65336 bytes diff --git a/tests/016.phpt b/tests/016.phpt new file mode 100644 index 0000000..0f28899 --- /dev/null +++ b/tests/016.phpt @@ -0,0 +1,45 @@ +--TEST-- +native export respects pcov.directory and pcov.exclude +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.exclude=~pcov-excluded-~ +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) diff --git a/tests/022.phpt b/tests/022.phpt new file mode 100644 index 0000000..62ad7b5 --- /dev/null +++ b/tests/022.phpt @@ -0,0 +1,87 @@ +--TEST-- +native synchronous full exports are complete and deterministic +--SKIPIF-- + +--ENV-- +PCOV_DUMP_BENCH_STATS=1 +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +--FILE-- + 0) { + $result++; + } + if ($value > 1) { + $result++; + } + return $result; +} +PHP +); +require $fixture; +require dirname(__DIR__) . '/tools/pcov_manifest_tools.php'; + +pcov\start(); +pcov_full_fixture(1); +pcov\stop(); +$reference = pcov\collect(pcov\inclusive, [$fixture]); +pcov\clear(); +pcov\start(); +pcov_full_fixture(1); +pcov\stop(); +$result = pcov\export($first, null, null, pcov\inclusive, [$fixture]); +$record = pcov_record_load($first); +var_dump(is_array($result)); +var_dump($result['mode'] === 'full'); +var_dump($record['record_type'] === PCOV_RECORD_FULL); +var_dump( + pcov_coverage_normalize($record['records']) === + pcov_coverage_normalize($reference) +); +var_dump(glob($first . '.pcovtmp.*')); + +pcov\clear(); +pcov\start(); +pcov_full_fixture(1); +pcov\stop(); +var_dump(is_array(pcov\export($second, null, null, pcov\inclusive, [$fixture]))); +var_dump(file_get_contents($second) === file_get_contents($first)); + +pcov\clear(); +var_dump(is_array(pcov\export($empty))); +$emptyCoverage = pcov_record_load($empty)['records']; +var_dump( + isset($emptyCoverage[$fixture]) && + count(array_unique($emptyCoverage[$fixture], SORT_REGULAR)) === 1 && + reset($emptyCoverage[$fixture]) === -1 +); + +foreach ([$fixture, $first, $second, $empty] as $path) { + unlink($path); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +array(0) { +} +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/023.phpt b/tests/023.phpt new file mode 100644 index 0000000..10ea264 --- /dev/null +++ b/tests/023.phpt @@ -0,0 +1,58 @@ +--TEST-- +native export reports publication failure and remains retryable +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +--FILE-- + 0); +var_dump(glob($invalid . '.pcovtmp.*')); + +unlink($fixture); +unlink($valid); +?> +--EXPECT-- +bool(false) +bool(true) +bool(true) +bool(true) +bool(true) +array(0) { +} diff --git a/tests/027.phpt b/tests/027.phpt new file mode 100644 index 0000000..883186f --- /dev/null +++ b/tests/027.phpt @@ -0,0 +1,47 @@ +--TEST-- +manifest environment identities and source fingerprints are deterministic +--SKIPIF-- + +--FILE-- + 'revision-1', + 'dependency_lock' => str_repeat('a', 64), + 'modules_config' => str_repeat('b', 64), + 'generated_code' => str_repeat('c', 64), + 'source_tree' => str_repeat('d', 64), + 'extra' => ['z' => 1, 'a' => true], +]); +$second = pcov_manifest_environment_identity([ + 'source_tree' => str_repeat('d', 64), + 'generated_code' => str_repeat('c', 64), + 'modules_config' => str_repeat('b', 64), + 'dependency_lock' => str_repeat('a', 64), + 'application_revision' => 'revision-1', + 'extra' => ['a' => true, 'z' => 1], +]); + +var_dump($first['id'] === $second['id']); +var_dump($first['canonical_json'] === $second['canonical_json']); +var_dump(strlen($first['id']) === 64); +var_dump($first['document']['runtime']['php_version_id'] === PHP_VERSION_ID); +var_dump(pcov_manifest_source_fingerprint(__FILE__) === hash_file('sha256', __FILE__)); + +try { + pcov_manifest_environment_identity([]); + var_dump(false); +} catch (InvalidArgumentException $exception) { + var_dump(str_contains($exception->getMessage(), 'application_revision')); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/028.phpt b/tests/028.phpt new file mode 100644 index 0000000..d216b76 --- /dev/null +++ b/tests/028.phpt @@ -0,0 +1,96 @@ +--TEST-- +validated synchronous dump uses hits for a matching manifest and full discovery after a source change +--SKIPIF-- + +--ENV-- +PCOV_DUMP_BENCH_STATS=1 +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 0) { + return 1; + } + return 0; +} +PHP; +file_put_contents($fixture, $source); +require $fixture; + +pcov\start(); +pcov_validated_fixture(1); +pcov\stop(); +$reference = pcov_coverage_normalize(pcov\collect(pcov\inclusive, [$fixture])); +$identity = pcov_manifest_environment_identity([ + 'application_revision' => 'fixture-v1', + 'dependency_lock' => str_repeat('a', 64), + 'modules_config' => str_repeat('b', 64), + 'generated_code' => str_repeat('c', 64), + 'source_tree' => str_repeat('d', 64), +]); +$manifestStats = pcov_manifest_create_from_coverage($manifest, $identity['id'], $reference); + +pcov\clear(); +pcov\start(); +pcov_validated_fixture(1); +pcov\stop(); +$matched = pcov\export($hit, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$matchedRecord = pcov_record_load($hit); +$matchedStats = pcov\export_stats(); +var_dump($matched['mode']); +var_dump($matched['reason']); +var_dump($matched['manifest_id'] === $manifestStats['manifest_id']); +var_dump($matchedRecord['record_type'] === PCOV_RECORD_HITS); +var_dump($matchedRecord['manifest_id'] === $manifestStats['manifest_id']); +var_dump($matchedRecord['validated_files'][$fixture] === hash_file('sha256', $fixture)); +var_dump($matchedStats['cfg_discovery_ns'] === 0); +var_dump($matchedStats['discovery_operations'] === 0); + +file_put_contents($fixture, $source . "\n// controlled change\n"); +pcov\clear(); +pcov\start(); +pcov_validated_fixture(0); +pcov\stop(); +$changed = pcov\export($fallback, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$fallbackRecord = pcov_record_load($fallback); +var_dump($changed['mode']); +var_dump($changed['reason']); +var_dump($fallbackRecord['record_type'] === PCOV_RECORD_FULL); +var_dump($fallbackRecord['reason'] === 6); +var_dump(isset($fallbackRecord['records'][$fixture])); +var_dump($fallbackRecord['fingerprints'][$fixture] === hash_file('sha256', $fixture)); + +foreach ([$fixture, $manifest, $hit, $fallback] as $path) { + if (is_file($path)) unlink($path); +} +?> +--EXPECT-- +string(8) "hit-only" +string(5) "match" +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +string(13) "full-fallback" +string(23) "fingerprint-unavailable" +bool(true) +bool(false) +bool(true) +bool(false) diff --git a/tests/029.phpt b/tests/029.phpt new file mode 100644 index 0000000..67a9885 --- /dev/null +++ b/tests/029.phpt @@ -0,0 +1,140 @@ +--TEST-- +immutable lifecycle merge builds deterministic candidates and rejects old manifest identities +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 'lifecycle-fixture', + 'dependency_lock' => str_repeat('1', 64), + 'modules_config' => str_repeat('2', 64), + 'generated_code' => str_repeat('3', 64), + 'source_tree' => str_repeat('4', 64), +]); + +pcov\start(); pcov_lifecycle_base(true); pcov\stop(); +$baseCoverage = pcov_coverage_normalize(pcov\collect(pcov\inclusive, [$base])); +$created = pcov_manifest_create_from_coverage($manifestN, $environment['id'], $baseCoverage); + +pcov\clear(); +pcov\start(); pcov_lifecycle_base(false); pcov\stop(); +$hitResult = pcov\export($hit, $manifestN, $environment['id'], pcov\inclusive, [$base]); +var_dump($hitResult['mode'] === 'hit-only'); + +file_put_contents($added, <<<'PHP' +getMessage(), 'Manifest identity mismatch')); +} +var_dump(pcov_manifest_merge($manifestN, [$hit])['coverage_complete']); + +$removed = pcov_manifest_publish_candidate( + $removedCandidate, $merged, pcov_manifest_source_inventory([$added]) +); +$removedRecord = pcov_record_load($removedCandidate); +var_dump($removed['files'] === 1); +var_dump(!isset($removedRecord['records'][$base])); +var_dump(isset($removedRecord['records'][$added])); + +try { + pcov_manifest_publish_candidate( + $removedCandidate, $merged, ['/tmp/not-discovered.php' => str_repeat('0', 64)] + ); + var_dump(false); +} catch (RuntimeException $exception) { + var_dump(str_contains($exception->getMessage(), 'full discovery pass')); +} + +foreach ([$base, $added, $manifestN, $manifestN1, $removedCandidate, $hit, $fallback] as $path) { + if (is_file($path)) unlink($path); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/030.phpt b/tests/030.phpt new file mode 100644 index 0000000..6b0451c --- /dev/null +++ b/tests/030.phpt @@ -0,0 +1,132 @@ +--TEST-- +validated lifecycle detects modified unexecuted code, no-hit files, corrupt manifests, and identity changes +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 'stale-v1', + 'dependency_lock' => str_repeat('a', 64), + 'modules_config' => str_repeat('b', 64), + 'generated_code' => str_repeat('c', 64), + 'source_tree' => str_repeat('d', 64), +]); +$otherEnvironment = pcov_manifest_environment_identity([ + 'application_revision' => 'stale-v2', + 'dependency_lock' => str_repeat('a', 64), + 'modules_config' => str_repeat('b', 64), + 'generated_code' => str_repeat('c', 64), + 'source_tree' => str_repeat('e', 64), +]); + +$oldSource = <<<'PHP' + [$input, $identity, $reason]) { + pcov\clear(); pcov\start(); pcov\stop(); + $output = "/tmp/pcov-stale-invalid-{$id}-{$index}.bin"; $paths[] = $output; + $result = pcov\export($output, $input, $identity, pcov\inclusive, [$silent]); + $record = pcov_record_load($output); + var_dump($result['mode'] === 'full-fallback'); + var_dump($result['reason'] === $reason); + var_dump($record['record_type'] === PCOV_RECORD_FULL); +} + +try { + pcov\export('/tmp/unused', $manifest, 'not-a-sha256'); + var_dump(false); +} catch (TypeError $error) { + var_dump(str_contains($error->getMessage(), '64 hexadecimal')); +} + +foreach (array_merge([$fixture, $silent, $manifest, $corrupt, $incompatible], $paths) as $path) { + if (is_file($path)) unlink($path); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/031.phpt b/tests/031.phpt new file mode 100644 index 0000000..63019db --- /dev/null +++ b/tests/031.phpt @@ -0,0 +1,113 @@ +--TEST-- +concurrent processes read one immutable manifest and produce deterministic hit and delta merges +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 'concurrent', + 'dependency_lock' => str_repeat('1', 64), + 'modules_config' => str_repeat('2', 64), + 'generated_code' => str_repeat('3', 64), + 'source_tree' => str_repeat('4', 64), +]); +pcov\start(); pcov_concurrent_base(1); pcov\stop(); +$coverage = pcov_coverage_normalize(pcov\collect(pcov\inclusive, [$base])); +$manifestStats = pcov_manifest_create_from_coverage($manifest, $identity['id'], $coverage); + +$children = []; +$dumps = []; +for ($worker = 0; $worker < 6; $worker++) { + $output = "/tmp/pcov-concurrent-hit-{$id}-{$worker}.bin"; + $dumps[] = $output; + $pid = pcntl_fork(); + if ($pid === 0) { + pcov\clear(); pcov\start(); pcov_concurrent_base($worker); pcov\stop(); + $result = pcov\export( + $output, $manifest, $identity['id'], pcov\inclusive, [$base] + ); + exit(is_array($result) && $result['mode'] === 'hit-only' ? 0 : 1); + } + $children[] = $pid; +} +$ok = true; +foreach ($children as $pid) { + pcntl_waitpid($pid, $status); + $ok = $ok && pcntl_wifexited($status) && pcntl_wexitstatus($status) === 0; +} +var_dump($ok); +var_dump(file_get_contents($manifest) !== ''); +$merged = pcov_manifest_merge($manifest, $dumps); +$reverse = pcov_manifest_merge($manifest, array_reverse($dumps)); +var_dump($merged['coverage'] === $reverse['coverage']); +var_dump($merged['candidate_manifest_id'] === $manifestStats['manifest_id']); +var_dump($merged['stats']['hit_dumps'] === 6); +var_dump($merged['stats']['hit_entries_before_deduplication'] > + $merged['stats']['hit_entries_after_deduplication']); +var_dump($merged['stats']['hit_entries_after_deduplication'] > 0); + +/* Concurrent fallback deltas add disjoint files without touching manifest N. */ +$deltaDumps = []; +$deltaFiles = []; +$children = []; +for ($worker = 0; $worker < 4; $worker++) { + $file = "/tmp/pcov-concurrent-delta-file-{$id}-{$worker}.php"; + $output = "/tmp/pcov-concurrent-delta-{$id}-{$worker}.bin"; + file_put_contents($file, " +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/032.phpt b/tests/032.phpt new file mode 100644 index 0000000..de2cad6 --- /dev/null +++ b/tests/032.phpt @@ -0,0 +1,75 @@ +--TEST-- +record loader rejects corrupt headers, oversized fields, truncation, and invalid records +--SKIPIF-- + +--FILE-- +getMessage(), $message)); + } +}; +$envelope = static function ( + int $type, + string $payload, + int $version = PCOV_RECORD_FORMAT_VERSION, + int $compat = PCOV_RECORD_FORMAT_COMPATIBILITY +): string { + return "PCOVREC\0" . pack('N*', + $version, 48, $type, 0, 0, strlen($payload), crc32($payload), + PHP_VERSION_ID, $compat, 0 + ) . $payload; +}; +$prefix = hex2bin($environment) . str_repeat("\0", 32) . pack('N', 0); + +$reject(substr($empty, 0, -1), 'payload length'); +$reject(substr_replace($empty, pack('N', 99), 8, 4), 'version'); +$reject(substr_replace($empty, pack('N', 99), 16, 4), 'record type'); +$reject(substr_replace($empty, pack('N', 99), 40, 4), 'compatibility'); +$reject(substr_replace($empty, pack('N', PHP_VERSION_ID - 1), 36, 4), 'PHP version'); +$reject($envelope(PCOV_RECORD_MANIFEST, $prefix . pack('N', PCOV_RECORD_MAX_FILES + 1)), 'file count'); +$reject($envelope(PCOV_RECORD_MANIFEST, $prefix . pack('NN', 1, PCOV_RECORD_MAX_PATH_LENGTH + 1)), 'filename length'); +$reject($envelope(PCOV_RECORD_MANIFEST, hex2bin($environment) . str_repeat("\0", 32) . pack('NN', 7, 0)), 'reason'); + +/* A full record with an unavailable fingerprint parses, but the strict merger + * rejects it instead of creating a candidate from unproved source state. */ +$fixture = '/tmp/pcov-lifecycle-corrupt-source-' . getmypid() . '.php'; +$manifest = '/tmp/pcov-lifecycle-corrupt-manifest-' . getmypid() . '.bin'; +file_put_contents($fixture, " ['fingerprint' => hash_file('sha256', $fixture), 'lines' => [2 => -1]]]; +pcov_atomic_publish($manifest, pcov_manifest_encode($environment, $files)); +$loadedManifest = pcov_record_load($manifest); +$fullPayload = hex2bin($environment) . hex2bin($loadedManifest['manifest_id']) . pack('NN', 7, 1); +$fullPayload .= pack('N', strlen($fixture)) . $fixture . pack('N', 5) . str_repeat("\0", 32) . pack('N', 0); +$full = $envelope(PCOV_RECORD_FULL, $fullPayload); +file_put_contents($path, $full); +try { + pcov_manifest_merge($manifest, [$path]); + var_dump(false); +} catch (RuntimeException $exception) { + var_dump(str_contains($exception->getMessage(), 'unavailable')); +} + +foreach ([$path, $fixture, $manifest] as $file) if (is_file($file)) unlink($file); +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/033.phpt b/tests/033.phpt new file mode 100644 index 0000000..2fb87a9 --- /dev/null +++ b/tests/033.phpt @@ -0,0 +1,58 @@ +--TEST-- +validated dump reports publication failure and leaves coverage retryable +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 'write', 'dependency_lock' => 'a', + 'modules_config' => 'b', 'generated_code' => 'c', 'source_tree' => 'd', +]); +pcov\start(); pcov_validated_write(); pcov\stop(); +$coverage = pcov\collect(pcov\inclusive, [$fixture]); +pcov_manifest_create_from_coverage($manifest, $identity['id'], $coverage); +pcov\clear(); pcov\start(); pcov_validated_write(); pcov\stop(); + +set_error_handler(static function (int $severity, string $message): bool { + var_dump(str_contains($message, 'temporary-file open failed')); + return true; +}); +$failed = pcov\export( + "/tmp/pcov-no-such-directory-{$id}/dump.bin", + $manifest, + $identity['id'], + pcov\inclusive, + [$fixture] +); +restore_error_handler(); +var_dump($failed); +$retried = pcov\export( + $output, $manifest, $identity['id'], pcov\inclusive, [$fixture] +); +var_dump($retried['mode'] === 'hit-only'); +var_dump(pcov_record_load($output)['records'] !== []); +var_dump(glob($output . '.pcovtmp.*')); + +foreach ([$fixture, $manifest, $output] as $path) if (is_file($path)) unlink($path); +?> +--EXPECT-- +bool(true) +bool(false) +bool(true) +bool(true) +array(0) { +} diff --git a/tests/034.phpt b/tests/034.phpt new file mode 100644 index 0000000..d496092 --- /dev/null +++ b/tests/034.phpt @@ -0,0 +1,47 @@ +--TEST-- +validated dump falls back when a positive hit is absent from a fingerprint-matching manifest +--SKIPIF-- + +--ENV-- +PCOV_DUMP_BENCH_STATS=1 +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 'unknown-hit', 'dependency_lock' => 'a', + 'modules_config' => 'b', 'generated_code' => 'c', 'source_tree' => 'd', +]); +$files = [$fixture => [ + 'fingerprint' => hash_file('sha256', $fixture), + 'lines' => [], +]]; +pcov_atomic_publish($manifest, pcov_manifest_encode($identity['id'], $files)); +pcov\start(); pcov_unknown_hit(); pcov\stop(); +$result = pcov\export($output, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$record = pcov_record_load($output); +var_dump($result['mode'] === 'full-fallback'); +var_dump($result['reason'] === 'hit-not-in-manifest'); +var_dump($record['record_type'] === PCOV_RECORD_FULL); +var_dump(in_array(1, $record['records'][$fixture], true)); +var_dump(pcov\export_stats()['discovery_operations'] === 1); +foreach ([$fixture, $manifest, $output] as $path) if (is_file($path)) unlink($path); +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/035.phpt b/tests/035.phpt new file mode 100644 index 0000000..748a11a --- /dev/null +++ b/tests/035.phpt @@ -0,0 +1,62 @@ +--TEST-- +validated export reuses compilation fingerprints without rereading source +--SKIPIF-- + +--ENV-- +PCOV_DUMP_BENCH_STATS=1 +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 'profile-v1', + 'dependency_lock' => str_repeat('a', 64), + 'modules_config' => str_repeat('b', 64), + 'generated_code' => str_repeat('c', 64), + 'source_tree' => str_repeat('d', 64), +]); +pcov_manifest_create_from_coverage($manifest, $identity['id'], $coverage); +pcov\export($dump, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$stats = pcov\export_stats(); + +var_dump($stats['compile_fingerprint_calls']); +var_dump($stats['fingerprint_calls']); +var_dump($stats['compile_fingerprint_bytes_read'] === strlen($source)); +var_dump($stats['fingerprint_bytes_read'] === strlen($source)); +var_dump($stats['compile_fingerprint_stat_calls']); +var_dump($stats['fingerprint_stat_calls']); +var_dump($stats['fingerprint_failures']); + +foreach ([$fixture, $manifest, $dump] as $path) { + if (is_file($path)) unlink($path); +} +rmdir($root); +?> +--EXPECT-- +int(1) +int(0) +bool(true) +bool(false) +int(3) +int(1) +int(0) diff --git a/tests/036.phpt b/tests/036.phpt new file mode 100644 index 0000000..7a003ea --- /dev/null +++ b/tests/036.phpt @@ -0,0 +1,106 @@ +--TEST-- +immutable-source fingerprint reuse is guarded and fails closed on source changes +--SKIPIF-- + +--ENV-- +PCOV_DUMP_BENCH_STATS=1 +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 'reuse-v1', + 'dependency_lock' => str_repeat('a', 64), + 'modules_config' => str_repeat('b', 64), + 'generated_code' => str_repeat('c', 64), + 'source_tree' => str_repeat('d', 64), +]); +pcov_manifest_create_from_coverage($manifest, $identity['id'], $coverage); + +$match = pcov\export($dump, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$stats = pcov\export_stats(); +var_dump($match['mode']); +var_dump($stats['fingerprint_calls']); +var_dump($stats['fingerprint_bytes_read']); +var_dump($stats['fingerprint_reuse_hits']); + +file_put_contents($fixture, $changed); +touch($fixture, $mtime); +clearstatcache(true, $fixture); +pcov\clear(); +pcov\start(); +pcov_fingerprint_reuse_fixture(); +pcov\stop(); +$mismatch = pcov\export($dump, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$stats = pcov\export_stats(); +var_dump($mismatch['mode']); +var_dump($stats['fingerprint_reuse_misses']); +var_dump($stats['fingerprint_calls']); +var_dump($stats['fingerprint_bytes_read'] === strlen($changed)); + +file_put_contents($root . '/replacement.php', $original); +rename($root . '/replacement.php', $fixture); +clearstatcache(true, $fixture); +pcov\clear(); +pcov\start(); +pcov_fingerprint_reuse_fixture(); +pcov\stop(); +$replacement = pcov\export($dump, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$stats = pcov\export_stats(); +var_dump($replacement['mode']); +var_dump($stats['fingerprint_reuse_misses']); +var_dump($stats['fingerprint_calls']); + +unlink($fixture); +pcov\clear(); +pcov\start(); +pcov_fingerprint_reuse_fixture(); +pcov\stop(); +$deleted = pcov\export($dump, $manifest, $identity['id'], pcov\inclusive, [$fixture]); +$stats = pcov\export_stats(); +var_dump($deleted['mode']); +var_dump($stats['fingerprint_reuse_misses']); +var_dump($stats['fingerprint_failures'] > 0); + +foreach ([$manifest, $dump] as $path) { + if (is_file($path)) unlink($path); +} +rmdir($root); +?> +--EXPECT-- +string(8) "hit-only" +int(0) +int(0) +int(1) +string(13) "full-fallback" +int(1) +int(1) +bool(true) +string(8) "hit-only" +int(1) +int(1) +string(13) "full-fallback" +int(1) +bool(true) diff --git a/tests/037.phpt b/tests/037.phpt new file mode 100644 index 0000000..5fcadf3 --- /dev/null +++ b/tests/037.phpt @@ -0,0 +1,99 @@ +--TEST-- +validated hit lookup profiling records sorted linear-search behavior and fail-closed misses +--SKIPIF-- + +--ENV-- +PCOV_DUMP_BENCH_STATS=1 +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 0) { + $result++; + } + return $result; +} +PHP; +file_put_contents($fixture, $source); +require $fixture; + +pcov\start(); +pcov_hit_lookup(1); +pcov\stop(); +$reference = pcov_coverage_normalize(pcov\collect(pcov\inclusive, [$fixture])); +$positive = array_keys(array_filter($reference[$fixture], static fn (int $value): bool => $value === 1)); +$identity = hash('sha256', 'pcov-hit-lookup-profile-v1'); +pcov_manifest_create_from_coverage($manifest, $identity, $reference); + +pcov\clear(); +pcov\start(); +pcov_hit_lookup(1); +pcov_hit_lookup(1); +pcov_hit_lookup(1); +pcov\stop(); +$result = pcov\export($dump, $manifest, $identity, pcov\inclusive, [$fixture]); +$stats = pcov\export_stats(); +$hits = count($positive); +var_dump($result['mode'] === 'hit-only'); +var_dump($stats['manifest_hit_entries_validated'] === $hits); +var_dump($stats['manifest_hit_search_restarts'] === $hits); +var_dump($stats['manifest_hit_matches'] === $hits); +var_dump($stats['manifest_hit_duplicates'] === 0); +var_dump($stats['manifest_hit_line_comparisons'] >= $hits); +var_dump($stats['manifest_hit_line_records_decoded'] === $stats['manifest_hit_line_comparisons']); +var_dump($stats['manifest_hit_unknown_lines'] === 0); +var_dump($stats['manifest_hit_unknown_files'] === 0); + +$staleLines = $reference[$fixture]; +unset($staleLines[$positive[array_key_last($positive)]]); +pcov_atomic_publish($stale, pcov_manifest_encode($identity, [ + $fixture => [ + 'fingerprint' => hash_file('sha256', $fixture), + 'lines' => array_fill_keys(array_keys($staleLines), -1), + ], +])); +pcov\clear(); +pcov\start(); +pcov_hit_lookup(1); +pcov\stop(); +$result = pcov\export($dump, $stale, $identity, pcov\inclusive, [$fixture]); +$stats = pcov\export_stats(); +var_dump($result['mode'] === 'full-fallback'); +var_dump($result['reason'] === 'hit-not-in-manifest'); +var_dump($stats['manifest_hit_unknown_lines'] === 1); +var_dump($stats['manifest_hit_unknown_files'] === 0); + +foreach ([$fixture, $manifest, $stale, $dump] as $path) { + if (is_file($path)) unlink($path); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/038.phpt b/tests/038.phpt new file mode 100644 index 0000000..4c96f1c --- /dev/null +++ b/tests/038.phpt @@ -0,0 +1,105 @@ +--TEST-- +validated hit export profiling records its separate sort, grouping, emission, checksum, and copy passes +--SKIPIF-- + +--ENV-- +PCOV_DUMP_BENCH_STATS=1 +--INI-- +pcov.enabled=1 +pcov.directory=/tmp +pcov.large_codebase=1 +--FILE-- + 0) { + $result++; + } + return $result; +} +PHP); +require $fixture; + +pcov\start(); +pcov_export_passes(1); +pcov\stop(); +$reference = pcov_coverage_normalize(pcov\collect(pcov\inclusive, [$fixture])); +$identity = hash('sha256', 'pcov-export-passes-v1'); +pcov_manifest_create_from_coverage($manifest, $identity, $reference); + +pcov\clear(); +pcov\start(); +pcov_export_passes(1); +pcov\stop(); +$result = pcov\export($dump, $manifest, $identity, pcov\inclusive, [$fixture]); +$record = pcov_record_load($dump); +$stats = pcov\export_stats(); +$hits = count($record['records'][$fixture]); +var_dump($result['mode'] === 'hit-only'); +var_dump($stats['validated_sort_calls'] === 2); +var_dump($stats['validated_sort_entries'] === $hits * 2); +var_dump($stats['manifest_hit_entries_validated'] === $hits); +var_dump($stats['validated_group_entries'] === $hits); +var_dump($stats['validated_payload_size_entries'] === 0); +var_dump($stats['validated_file_metadata_entries'] === 1); +var_dump($stats['validated_emit_entries'] === $hits); +var_dump($stats['validated_checksum_calls'] === 1); +var_dump($stats['validated_checksum_bytes'] === $record['payload_length']); +var_dump($stats['validated_payload_copy_bytes'] === $record['payload_length']); +var_dump($stats['validated_payload_bytes'] === $record['payload_length']); +var_dump($stats['validated_output_bytes'] === filesize($dump)); +var_dump($stats['hit_array_allocations'] === 1); +var_dump($stats['hit_array_allocated_bytes'] > 0); +var_dump($stats['temporary_logical_peak_bytes'] >= filesize($dump)); + +pcov\clear(); +$result = pcov\export($dump, $manifest, $identity, pcov\inclusive, [$fixture]); +$record = pcov_record_load($dump); +$stats = pcov\export_stats(); +var_dump($result['mode'] === 'hit-only'); +var_dump($record['records'] === []); +var_dump($stats['validated_sort_calls'] === 0); +var_dump($stats['validated_group_entries'] === 0); +var_dump($stats['validated_emit_entries'] === 0); +var_dump($stats['validated_checksum_calls'] === 1); +var_dump($stats['hit_array_allocations'] === 0); + +foreach ([$fixture, $manifest, $dump] as $path) { + if (is_file($path)) unlink($path); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/039.phpt b/tests/039.phpt new file mode 100644 index 0000000..713eb61 --- /dev/null +++ b/tests/039.phpt @@ -0,0 +1,66 @@ +--TEST-- +documented full bootstrap, validated export, and deterministic merge workflow +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.large_codebase=1 +pcov.directory=/tmp +--FILE-- + +--EXPECT-- +string(4) "full" +bool(true) +string(8) "hit-only" +bool(true) +bool(true) +bool(true) diff --git a/tests/040.phpt b/tests/040.phpt new file mode 100644 index 0000000..3a3733c --- /dev/null +++ b/tests/040.phpt @@ -0,0 +1,35 @@ +--TEST-- +large-codebase mode is one switch and disabled mode preserves upstream collection +--SKIPIF-- + +--INI-- +pcov.enabled=1 +pcov.large_codebase=0 +pcov.directory=/tmp +--FILE-- + +--EXPECT-- +string(1) "0" +bool(true) +bool(false) +bool(false) +bool(false) +bool(false) diff --git a/tools/pcov_manifest.php b/tools/pcov_manifest.php new file mode 100644 index 0000000..93853ab --- /dev/null +++ b/tools/pcov_manifest.php @@ -0,0 +1,66 @@ += 5) { + $result = pcov_manifest_bootstrap( + $argv[2], $argv[3], array_slice($argv, 4) + ); + } elseif ($command === 'merge' && count($argv) >= 5) { + $result = pcov_manifest_merge($argv[3], array_slice($argv, 4)); + if (!$result['coverage_complete']) { + throw new RuntimeException( + 'Coverage includes a full fallback; rebuild the manifest before claiming completeness' + ); + } + pcov_atomic_publish( + $argv[2], + "= 6) { + $inventoryPaths = json_decode( + file_get_contents($argv[4]), true, flags: JSON_THROW_ON_ERROR + ); + if (!is_array($inventoryPaths) || !array_is_list($inventoryPaths)) { + throw new RuntimeException('Inventory JSON must contain a list of canonical source paths'); + } + $merge = pcov_manifest_merge($argv[3], array_slice($argv, 5)); + $result = pcov_manifest_publish_candidate( + $argv[2], $merge, pcov_manifest_source_inventory($inventoryPaths) + ); + $result['base_manifest_id'] = $merge['base_manifest_id']; + $result['delta_files'] = $merge['delta_files']; + } elseif ($command === 'inspect' && count($argv) === 3) { + $record = pcov_record_load($argv[2]); + $result = [ + 'version' => $record['version'], + 'record_type' => $record['record_type'], + 'environment_id' => $record['environment_id'], + 'manifest_id' => $record['manifest_id'], + 'reason' => $record['reason'], + 'files' => count($record['records']), + 'normalized_hash' => pcov_coverage_hash($record['records']), + ]; + } else { + $usage(); + } + + echo json_encode($result, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), "\n"; +} catch (Throwable $throwable) { + fwrite(STDERR, $throwable->getMessage() . "\n"); + exit(1); +} diff --git a/tools/pcov_manifest_tools.php b/tools/pcov_manifest_tools.php new file mode 100644 index 0000000..2989769 --- /dev/null +++ b/tools/pcov_manifest_tools.php @@ -0,0 +1,568 @@ + $item) { + if (!is_string($key) || $key === '' || str_contains($key, "\0")) { + throw new InvalidArgumentException('Manifest identity object keys must be non-empty strings'); + } + $normalized[$key] = pcov_manifest_identity_normalize($item); + } + ksort($normalized, SORT_STRING); + + return $normalized; +} + +/** + * Build the opaque environment identity referenced by manifests and dumps. + * + * The caller-provided deployment values should identify application revision, + * dependency lockfile, enabled modules/configuration, generated code, and the + * relevant source-tree inventory. They are deliberately explicit: guessing + * any of them from an individual request could claim freshness incorrectly. + * + * @return array{document:array,canonical_json:string,id:string} + */ +function pcov_manifest_environment_identity(array $deployment): array +{ + $required = [ + 'application_revision', + 'dependency_lock', + 'modules_config', + 'generated_code', + 'source_tree', + ]; + foreach ($required as $key) { + if (!array_key_exists($key, $deployment) || + !is_string($deployment[$key]) || $deployment[$key] === '' || + str_contains($deployment[$key], "\0")) { + throw new InvalidArgumentException("Missing or invalid manifest identity component: {$key}"); + } + } + + $document = pcov_manifest_identity_normalize([ + 'schema' => PCOV_MANIFEST_IDENTITY_SCHEMA, + 'runtime' => [ + 'php_version' => PHP_VERSION, + 'php_version_id' => PHP_VERSION_ID, + 'php_zts' => PHP_ZTS, + 'php_debug' => PHP_DEBUG, + 'php_int_size' => PHP_INT_SIZE, + 'php_os_family' => PHP_OS_FAMILY, + 'pcov_version' => defined('pcov\\version') ? constant('pcov\\version') : null, + 'record_format' => PCOV_RECORD_FORMAT_VERSION, + 'record_compatibility' => PCOV_RECORD_FORMAT_COMPATIBILITY, + ], + 'deployment' => $deployment, + ]); + $json = json_encode( + $document, + JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE + ); + + return [ + 'document' => $document, + 'canonical_json' => $json, + 'id' => hash('sha256', $json), + ]; +} + +/** Return a deterministic SHA-256 source fingerprint for a regular file. */ +function pcov_manifest_source_fingerprint(string $path): string +{ + if ($path === '' || str_contains($path, "\0") || !is_file($path)) { + throw new RuntimeException("Cannot fingerprint non-regular PCOV source: {$path}"); + } + $fingerprint = @hash_file('sha256', $path); + if ($fingerprint === false) { + throw new RuntimeException("Cannot fingerprint PCOV source: {$path}"); + } + + return $fingerprint; +} + +/** @return array> */ +function pcov_coverage_normalize(array $coverage): array +{ + $normalized = []; + foreach ($coverage as $file => $lines) { + if (!is_string($file) || $file === '' || str_contains($file, "\0") || $file[0] !== '/') { + throw new RuntimeException('Coverage contains a non-canonical filename'); + } + if (!is_array($lines)) { + throw new RuntimeException("Coverage lines are not an array: {$file}"); + } + + $normalizedLines = []; + foreach ($lines as $line => $value) { + if (!is_int($line) || $line < 0 || $line > 0xffffffff || ($value !== -1 && $value !== 1)) { + throw new RuntimeException("Coverage contains an invalid line record: {$file}"); + } + $normalizedLines[$line] = $value; + } + ksort($normalizedLines, SORT_NUMERIC); + $normalized[$file] = $normalizedLines; + } + ksort($normalized, SORT_STRING); + + return $normalized; +} + +function pcov_coverage_hash(array $coverage): string +{ + $context = hash_init('sha256'); + foreach (pcov_coverage_normalize($coverage) as $file => $lines) { + hash_update($context, pack('N', strlen($file)) . $file . pack('N', count($lines))); + foreach ($lines as $line => $value) { + hash_update($context, pack('NN', $line, $value)); + } + } + + return hash_final($context); +} + +/** @return array> */ +function pcov_full_dumps_merge(array $paths): array +{ + $coverage = []; + foreach ($paths as $path) { + $record = pcov_record_load($path); + if ($record['record_type'] !== PCOV_RECORD_FULL) { + throw new RuntimeException("Record is not complete coverage: {$path}"); + } + foreach ($record['records'] as $file => $lines) { + foreach ($lines as $line => $value) { + if (!isset($coverage[$file][$line]) || $value === 1) { + $coverage[$file][$line] = $value; + } + } + } + } + + return pcov_coverage_normalize($coverage); +} + +function pcov_record_envelope(int $recordType, string $payload): string +{ + if (!in_array($recordType, [ + PCOV_RECORD_FULL, + PCOV_RECORD_MANIFEST, + PCOV_RECORD_HITS, + ], true)) { + throw new InvalidArgumentException("Unsupported PCOV coverage record type {$recordType}"); + } + $length = strlen($payload); + return PCOV_RECORD_MAGIC . pack( + 'N*', + PCOV_RECORD_FORMAT_VERSION, + PCOV_RECORD_HEADER_SIZE, + $recordType, + 0, + intdiv($length, 4294967296), + $length % 4294967296, + crc32($payload), + PHP_VERSION_ID, + PCOV_RECORD_FORMAT_COMPATIBILITY, + 0 + ) . $payload; +} + +/** + * @param array}> $files + */ +function pcov_manifest_id(string $environmentId, array $files): string +{ + if (!preg_match('/^[0-9a-f]{64}$/D', $environmentId)) { + throw new InvalidArgumentException('Environment identity must be lowercase SHA-256 hex'); + } + ksort($files, SORT_STRING); + $context = hash_init('sha256'); + hash_update($context, "PCOV-MANIFEST-V1\0" . hex2bin($environmentId)); + foreach ($files as $file => $entry) { + if (!preg_match('/^[0-9a-f]{64}$/D', $entry['fingerprint'] ?? '')) { + throw new InvalidArgumentException("Invalid source fingerprint: {$file}"); + } + $lines = pcov_coverage_normalize([$file => $entry['lines']])[$file]; + hash_update($context, pack('N', strlen($file)) . $file); + hash_update($context, hex2bin($entry['fingerprint'])); + hash_update($context, pack('N', count($lines))); + foreach ($lines as $line => $_value) { + hash_update($context, pack('N', $line)); + } + } + return hash_final($context); +} + +/** + * @param array}> $files + */ +function pcov_manifest_encode(string $environmentId, array $files): string +{ + ksort($files, SORT_STRING); + $manifestId = pcov_manifest_id($environmentId, $files); + $payload = hex2bin($environmentId) . hex2bin($manifestId) . pack('N', 0) . pack('N', count($files)); + foreach ($files as $file => $entry) { + $lines = pcov_coverage_normalize([$file => $entry['lines']])[$file]; + $payload .= pack('N', strlen($file)) . $file; + $payload .= hex2bin($entry['fingerprint']); + $payload .= pack('N', count($lines)); + foreach ($lines as $line => $_value) { + $payload .= pack('N', $line); + } + } + return pcov_record_envelope(PCOV_RECORD_MANIFEST, $payload); +} + +/** + * Create a manifest from complete coverage and current source files. + * This bootstrap helper is valid only when the full discovery run and source + * fingerprinting belong to the same immutable deployment. + * + * @param array> $coverage + * @return array{manifest_id:string,environment_id:string,files:int,executable_lines:int,size:int} + */ +function pcov_manifest_create_from_coverage( + string $path, + string $environmentId, + array $coverage +): array { + $coverage = pcov_coverage_normalize($coverage); + $files = []; + $lineCount = 0; + foreach ($coverage as $file => $lines) { + $manifestLines = array_fill_keys(array_keys($lines), -1); + $files[$file] = [ + 'fingerprint' => pcov_manifest_source_fingerprint($file), + 'lines' => $manifestLines, + ]; + $lineCount += count($lines); + } + $contents = pcov_manifest_encode($environmentId, $files); + pcov_atomic_publish($path, $contents); + $loaded = pcov_record_load($path); + return [ + 'manifest_id' => $loaded['manifest_id'], + 'environment_id' => $loaded['environment_id'], + 'files' => count($files), + 'executable_lines' => $lineCount, + 'size' => strlen($contents), + ]; +} + +function pcov_atomic_publish(string $path, string $contents): void +{ + if ($path === '' || str_contains($path, "\0")) { + throw new RuntimeException('Invalid PCOV output path'); + } + + $temporary = null; + $handle = false; + for ($attempt = 0; $attempt < 16; $attempt++) { + $suffix = bin2hex(random_bytes(8)); + $candidate = "{$path}.pcovtmp." . getmypid() . ".{$suffix}"; + $handle = @fopen($candidate, 'x+b'); + if ($handle !== false) { + $temporary = $candidate; + break; + } + } + if ($handle === false || $temporary === null) { + throw new RuntimeException("Cannot create temporary PCOV file for: {$path}"); + } + + try { + $offset = 0; + $length = strlen($contents); + while ($offset < $length) { + $written = fwrite($handle, substr($contents, $offset)); + if ($written === false || $written === 0) { + throw new RuntimeException("Cannot write temporary PCOV file for: {$path}"); + } + $offset += $written; + } + if (!fclose($handle)) { + $handle = false; + throw new RuntimeException("Cannot close temporary PCOV file for: {$path}"); + } + $handle = false; + if (!@rename($temporary, $path)) { + throw new RuntimeException("Cannot publish PCOV file: {$path}"); + } + $temporary = null; + } finally { + if (is_resource($handle)) { + fclose($handle); + } + if ($temporary !== null && is_file($temporary)) { + unlink($temporary); + } + } +} + +/** @return array canonical path => SHA-256 */ +function pcov_manifest_source_inventory(array $paths): array +{ + $inventory = []; + foreach ($paths as $path) { + if (!is_string($path) || $path === '' || $path[0] !== '/' || str_contains($path, "\0")) { + throw new InvalidArgumentException('Source inventory paths must be canonical absolute paths'); + } + $inventory[$path] = pcov_manifest_source_fingerprint($path); + } + ksort($inventory, SORT_STRING); + return $inventory; +} + +/** + * Merge immutable manifest N with validated hit dumps and complete fallback + * records. Fallback records are coverage-complete for their request, but a + * request-local delta cannot prove deployment-wide removals; candidate N+1 is + * therefore not publishable until checked against an authoritative inventory. + * + * @return array{ + * coverage:array>, + * coverage_complete:bool, + * candidate_files:array}>, + * candidate_complete:bool, + * requires_full_discovery:bool, + * environment_id:string, + * base_manifest_id:string, + * candidate_manifest_id:string, + * delta_files:list, + * stats:array + * } + */ +function pcov_manifest_merge(string $manifestPath, array $dumpPaths): array +{ + $started = hrtime(true); + $manifest = pcov_record_load($manifestPath); + if ($manifest['version'] !== PCOV_RECORD_FORMAT_VERSION || + $manifest['record_type'] !== PCOV_RECORD_MANIFEST) { + throw new RuntimeException("Record is not a PCOV manifest: {$manifestPath}"); + } + + $coverage = pcov_coverage_normalize($manifest['records']); + $candidate = []; + foreach ($coverage as $file => $lines) { + $fingerprint = $manifest['fingerprints'][$file] ?? null; + if (!is_string($fingerprint) || !preg_match('/^[0-9a-f]{64}$/D', $fingerprint)) { + throw new RuntimeException("Manifest has no valid fingerprint: {$file}"); + } + $candidate[$file] = [ + 'fingerprint' => $fingerprint, + 'lines' => array_fill_keys(array_keys($lines), -1), + ]; + } + + $fallbacks = 0; + $hitDumps = 0; + $hitEntries = 0; + $uniqueHits = []; + $deltaFiles = []; + foreach ($dumpPaths as $dumpPath) { + $dump = pcov_record_load($dumpPath); + if ($dump['version'] !== PCOV_RECORD_FORMAT_VERSION) { + throw new RuntimeException("Incompatible record cannot be merged into this manifest: {$dumpPath}"); + } + if ($dump['environment_id'] !== $manifest['environment_id']) { + throw new RuntimeException("Environment identity mismatch: {$dumpPath}"); + } + if ($dump['manifest_id'] !== $manifest['manifest_id']) { + throw new RuntimeException("Manifest identity mismatch: {$dumpPath}"); + } + + if ($dump['record_type'] === PCOV_RECORD_HITS) { + $hitDumps++; + if ($dump['reason'] !== 0) { + throw new RuntimeException("Hit record has a fallback reason: {$dumpPath}"); + } + foreach ($dump['validated_files'] as $file => $fingerprint) { + if (!isset($candidate[$file])) { + throw new RuntimeException("Validated file is absent from manifest: {$file}"); + } + if (!hash_equals($candidate[$file]['fingerprint'], $fingerprint)) { + throw new RuntimeException("Validated file fingerprint mismatch: {$file}"); + } + } + foreach ($dump['records'] as $file => $lines) { + if (!isset($dump['validated_files'][$file])) { + throw new RuntimeException("Hit file was not fingerprint-validated: {$file}"); + } + foreach ($lines as $line => $_value) { + $hitEntries++; + $uniqueHits[pack('N', strlen($file)) . $file . pack('N', $line)] = true; + if (!isset($coverage[$file]) || !array_key_exists($line, $coverage[$file])) { + throw new RuntimeException("Hit is absent from PCOV manifest: {$file}:{$line}"); + } + $coverage[$file][$line] = 1; + } + } + continue; + } + + if ($dump['record_type'] !== PCOV_RECORD_FULL || $dump['reason'] === 0) { + throw new RuntimeException("Unsupported manifest input record: {$dumpPath}"); + } + $fallbacks++; + foreach ($dump['records'] as $file => $lines) { + $status = $dump['fingerprint_status'][$file] ?? -1; + if ($status !== 0) { + throw new RuntimeException("Fallback fingerprint is unavailable ({$status}): {$file}"); + } + $fingerprint = $dump['fingerprints'][$file] ?? null; + if (!is_string($fingerprint) || !preg_match('/^[0-9a-f]{64}$/D', $fingerprint)) { + throw new RuntimeException("Fallback fingerprint is invalid: {$file}"); + } + foreach ($lines as $line => $value) { + if ($value === 1) { + $hitEntries++; + $uniqueHits[pack('N', strlen($file)) . $file . pack('N', $line)] = true; + } + if (!isset($coverage[$file][$line]) || $value === 1) { + $coverage[$file][$line] = $value; + } + } + $candidate[$file] = [ + 'fingerprint' => $fingerprint, + 'lines' => array_fill_keys(array_keys($lines), -1), + ]; + $deltaFiles[$file] = true; + } + } + + $coverage = pcov_coverage_normalize($coverage); + ksort($candidate, SORT_STRING); + $candidateId = pcov_manifest_id($manifest['environment_id'], $candidate); + + return [ + 'coverage' => $coverage, + 'coverage_complete' => $fallbacks === 0, + 'candidate_files' => $candidate, + 'candidate_complete' => $fallbacks === 0, + 'requires_full_discovery' => $fallbacks !== 0, + 'environment_id' => $manifest['environment_id'], + 'base_manifest_id' => $manifest['manifest_id'], + 'candidate_manifest_id' => $candidateId, + 'delta_files' => array_keys($deltaFiles), + 'stats' => [ + 'merge_ns' => hrtime(true) - $started, + 'hit_dumps' => $hitDumps, + 'hit_entries_before_deduplication' => $hitEntries, + 'hit_entries_after_deduplication' => count($uniqueHits), + 'full_fallbacks' => $fallbacks, + 'delta_files' => count($deltaFiles), + ], + ]; +} + +/** + * Validate a candidate against a complete deployment inventory and publish it + * atomically. Every inventory file must already have executable-line discovery + * data with the same fingerprint; removed files are omitted deterministically. + * + * @param array $merge + * @param array $inventory + * @return array{manifest_id:string,files:int,size:int} + */ +function pcov_manifest_publish_candidate( + string $path, + array $merge, + array $inventory +): array { + ksort($inventory, SORT_STRING); + $files = []; + foreach ($inventory as $file => $fingerprint) { + if (!isset($merge['candidate_files'][$file])) { + throw new RuntimeException("Inventory file needs a full discovery pass: {$file}"); + } + $candidate = $merge['candidate_files'][$file]; + if (!is_string($fingerprint) || + !hash_equals($candidate['fingerprint'], $fingerprint)) { + throw new RuntimeException("Inventory fingerprint needs a full discovery pass: {$file}"); + } + $files[$file] = $candidate; + } + + $contents = pcov_manifest_encode($merge['environment_id'], $files); + pcov_atomic_publish($path, $contents); + $loaded = pcov_record_load($path); + return [ + 'manifest_id' => $loaded['manifest_id'], + 'files' => count($files), + 'size' => strlen($contents), + ]; +} + +/** Build manifest N from complete full-fallback records of one deployment. */ +function pcov_manifest_bootstrap( + string $path, + string $environmentId, + array $fullPaths +): array { + $started = hrtime(true); + $files = []; + $coverage = []; + foreach ($fullPaths as $fullPath) { + $record = pcov_record_load($fullPath); + if ($record['version'] !== PCOV_RECORD_FORMAT_VERSION || + $record['record_type'] !== PCOV_RECORD_FULL || + $record['environment_id'] !== $environmentId) { + throw new RuntimeException("Invalid manifest bootstrap record: {$fullPath}"); + } + foreach ($record['records'] as $file => $lines) { + if (($record['fingerprint_status'][$file] ?? -1) !== 0) { + throw new RuntimeException("Bootstrap fingerprint is unavailable: {$file}"); + } + $fingerprint = $record['fingerprints'][$file]; + if (isset($files[$file]) && !hash_equals($files[$file]['fingerprint'], $fingerprint)) { + throw new RuntimeException("Source changed during manifest bootstrap: {$file}"); + } + foreach ($lines as $line => $value) { + $files[$file]['lines'][$line] = -1; + if (!isset($coverage[$file][$line]) || $value === 1) { + $coverage[$file][$line] = $value; + } + } + $files[$file]['fingerprint'] = $fingerprint; + $files[$file]['lines'] ??= []; + } + } + ksort($files, SORT_STRING); + foreach ($files as &$entry) ksort($entry['lines'], SORT_NUMERIC); + unset($entry); + $contents = pcov_manifest_encode($environmentId, $files); + pcov_atomic_publish($path, $contents); + $manifest = pcov_record_load($path); + return [ + 'manifest_id' => $manifest['manifest_id'], + 'environment_id' => $environmentId, + 'files' => count($files), + 'executable_lines' => array_sum(array_map( + static fn (array $entry): int => count($entry['lines']), $files + )), + 'normalized_hash' => pcov_coverage_hash($coverage), + 'size' => strlen($contents), + 'source_dumps' => count($fullPaths), + 'build_ns' => hrtime(true) - $started, + ]; +} diff --git a/tools/pcov_record_loader.php b/tools/pcov_record_loader.php new file mode 100644 index 0000000..db644fc --- /dev/null +++ b/tools/pcov_record_loader.php @@ -0,0 +1,233 @@ +> + * } + */ +function pcov_record_load(string $path): array +{ + $data = @file_get_contents($path); + + if ($data === false) { + throw new RuntimeException("Cannot open PCOV coverage record: {$path}"); + } + + $size = strlen($data); + $offset = 0; + $read = static function (int $length) use ($data, $size, $path, &$offset): string { + if ($length < 0 || $offset < 0 || $offset > $size || $length > $size - $offset) { + throw new RuntimeException("Truncated PCOV coverage record: {$path}"); + } + + $value = substr($data, $offset, $length); + $offset += $length; + + return $value; + }; + $readU32 = static function () use ($read): int { + return unpack('Nvalue', $read(4))['value']; + }; + $readU64 = static function () use ($read, $path): int { + $parts = unpack('Nhigh/Nlow', $read(8)); + $base = 4294967296; + + if ($parts['high'] > intdiv(PHP_INT_MAX - $parts['low'], $base)) { + throw new RuntimeException("Oversized payload length in PCOV coverage record: {$path}"); + } + + return ($parts['high'] * $base) + $parts['low']; + }; + + if ($read(8) !== PCOV_RECORD_MAGIC) { + throw new RuntimeException("Invalid PCOV coverage record magic: {$path}"); + } + + $version = $readU32(); + $headerSize = $readU32(); + $recordType = $readU32(); + $flags = $readU32(); + $payloadLength = $readU64(); + $checksum = $readU32(); + $phpVersionId = $readU32(); + $compatibility = $readU32(); + $reserved = $readU32(); + + if ($version !== PCOV_RECORD_FORMAT_VERSION) { + throw new RuntimeException("Unsupported PCOV record version {$version}: {$path}"); + } + if ($headerSize !== PCOV_RECORD_HEADER_SIZE) { + throw new RuntimeException("Unsupported PCOV coverage record header size {$headerSize}: {$path}"); + } + if ($recordType !== PCOV_RECORD_HITS && + $recordType !== PCOV_RECORD_MANIFEST && + $recordType !== PCOV_RECORD_FULL) { + throw new RuntimeException("Unsupported PCOV coverage record type {$recordType}: {$path}"); + } + if ($flags !== 0 || $reserved !== 0) { + throw new RuntimeException("Unsupported PCOV coverage record flags: {$path}"); + } + if ($compatibility !== PCOV_RECORD_FORMAT_COMPATIBILITY) { + throw new RuntimeException("Unsupported PCOV coverage record compatibility {$compatibility}: {$path}"); + } + if ($phpVersionId !== PHP_VERSION_ID) { + throw new RuntimeException( + "Incompatible PHP version {$phpVersionId} in PCOV coverage record: {$path}" + ); + } + if ($payloadLength !== $size - PCOV_RECORD_HEADER_SIZE) { + throw new RuntimeException("Invalid payload length in PCOV coverage record: {$path}"); + } + + $payload = substr($data, PCOV_RECORD_HEADER_SIZE); + if (crc32($payload) !== $checksum) { + throw new RuntimeException("Checksum mismatch in PCOV coverage record: {$path}"); + } + + $environmentId = bin2hex($read(32)); + $manifestId = bin2hex($read(32)); + $reason = $readU32(); + if ($recordType === PCOV_RECORD_MANIFEST && $reason !== 0) { + throw new RuntimeException("Manifest record has an invalid reason: {$path}"); + } + $readFile = static function (bool $status) use ($read, $readU32, $path): array { + $fileLength = $readU32(); + if ($fileLength === 0 || $fileLength > PCOV_RECORD_MAX_PATH_LENGTH) { + throw new RuntimeException("Invalid filename length in PCOV coverage record: {$path}"); + } + $file = $read($fileLength); + $fingerprintStatus = $status ? $readU32() : 0; + $fingerprint = bin2hex($read(32)); + return [$file, $fingerprint, $fingerprintStatus]; + }; + $checkFile = static function (?string &$previousFile, string $file) use ($path): void { + if ($previousFile !== null && strcmp($previousFile, $file) >= 0) { + throw new RuntimeException("Unsorted or duplicate filename in PCOV coverage record: {$path}"); + } + $previousFile = $file; + }; + $readLines = static function (bool $values, string $lineFile) use ( + $readU32, $size, $path, $recordType, &$offset + ): array { + $lineCount = $readU32(); + $width = $values ? 8 : 4; + if ($lineCount > PCOV_RECORD_MAX_LINES || + $lineCount > intdiv($size - $offset, $width)) { + throw new RuntimeException("Oversized line count in PCOV coverage record: {$path}"); + } + $lines = []; + $previousLine = null; + for ($lineIndex = 0; $lineIndex < $lineCount; $lineIndex++) { + $line = $readU32(); + if ($line === 0 || ($previousLine !== null && $line <= $previousLine)) { + throw new RuntimeException( + "Unsorted or duplicate line {$line} after " . + ($previousLine ?? 'none') . " for {$lineFile} in PCOV coverage record: {$path}" + ); + } + $previousLine = $line; + if ($values) { + $raw = $readU32(); + $value = $raw === 0xffffffff ? -1 : $raw; + if ($value !== -1 && $value !== 1) { + throw new RuntimeException("Invalid coverage value in PCOV coverage record: {$path}"); + } + $lines[$line] = $value; + } else { + $lines[$line] = $recordType === PCOV_RECORD_HITS ? 1 : -1; + } + } + return $lines; + }; + + $records = []; + $fingerprints = []; + $fingerprintStatus = []; + $validatedFiles = []; + $previousFile = null; + if ($recordType === PCOV_RECORD_HITS) { + $validationCount = $readU32(); + if ($validationCount > PCOV_RECORD_MAX_FILES) { + throw new RuntimeException("Oversized validation file count: {$path}"); + } + for ($index = 0; $index < $validationCount; $index++) { + [$file, $fingerprint] = $readFile(false); + $checkFile($previousFile, $file); + $validatedFiles[$file] = $fingerprint; + } + $fileCount = $readU32(); + if ($fileCount > PCOV_RECORD_MAX_FILES) { + throw new RuntimeException("Oversized hit file count: {$path}"); + } + $previousFile = null; + for ($index = 0; $index < $fileCount; $index++) { + $fileLength = $readU32(); + if ($fileLength === 0 || $fileLength > PCOV_RECORD_MAX_PATH_LENGTH) { + throw new RuntimeException("Invalid filename length in PCOV coverage record: {$path}"); + } + $file = $read($fileLength); + $checkFile($previousFile, $file); + $records[$file] = $readLines(false, $file); + } + } else { + $fileCount = $readU32(); + if ($fileCount > PCOV_RECORD_MAX_FILES) { + throw new RuntimeException("Oversized file count in PCOV coverage record: {$path}"); + } + for ($index = 0; $index < $fileCount; $index++) { + [$file, $fingerprint, $fileStatus] = $readFile( + $recordType === PCOV_RECORD_FULL + ); + $checkFile($previousFile, $file); + $fingerprints[$file] = $fingerprint; + $fingerprintStatus[$file] = $fileStatus; + $records[$file] = $readLines( + $recordType === PCOV_RECORD_FULL, $file + ); + } + } + if ($offset !== $size) { + throw new RuntimeException("Trailing data in PCOV coverage record: {$path}"); + } + + return [ + 'version' => $version, + 'record_type' => $recordType, + 'flags' => $flags, + 'payload_length' => $payloadLength, + 'checksum' => $checksum, + 'php_version_id' => $phpVersionId, + 'compatibility' => $compatibility, + 'environment_id' => $environmentId, + 'manifest_id' => $manifestId, + 'reason' => $reason, + 'records' => $records, + 'fingerprints' => $fingerprints, + 'fingerprint_status' => $fingerprintStatus, + 'validated_files' => $validatedFiles, + ]; +}