Skip to content

fix: each compiled file names the native fingerprint its library was built for - #169

Merged
heyJordanParker merged 6 commits into
masterfrom
fix/version-check
Oct 18, 2026
Merged

heyJordanParker merged 6 commits into
masterfrom
fix/version-check

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Closes a path #160 opened: inside Composer's own process, PHP# code ran on an engine the installed standard library was not built for.

The problem

mago compile writes a <Name>.php beside each .sharp file since #160. Inside Composer's own process, autoload.php skips requireNative, so composer install runs on any engine. Composer's own loader includes the .php file, which loads the .sharp file directly. So a post-autoload-dump callback ran PHP# code on a mismatched engine.

The first version of this PR kept requireNative's refusal for the request. Its review found three paths that still ran PHP# code:

  • composer --no-plugins dump-autoload, where nothing calls requireNative.
  • A file opcache already caches, in the CLI file cache or in php-fpm's shared memory.
  • composer update to a mismatched library in one run. Composer skips the new autoload.php, so the run only said "out of date".

The fix

One rule closes all three, whatever loads the file.

  • Each compiled file names its library. mago compile writes the library's native fingerprint, from its native.php, and the version installed.json lists into each .sharpc header, which grows from 104 to 128 bytes. A project without the standard library writes an all-zero fingerprint.
  • The engine refuses a compiled file built for other native bodies. sharp_check compares the fingerprint with SHARP_NATIVE. Every loader reaches sharp_check, and opcache reaches it when it revalidates a cached file, through sharp_compiled_revision.
  • The per-request refusal is gone, and so is the check of each native body. requireNative keeps its three arguments, so an engine and a library of any two versions refuse each other with the engine's message. Outside Composer, autoload.php still calls it first.
  • The Composer plugin checks at each post-autoload-dump, before its early return, and prints the refusal as an error line. composer update runs the new version's plugin, so the refusal names the new version at once. Composer still exits 0.

The refusal names both versions and the two fixes:

This project's PHP# library is 0.3.2, and the PHP# engine is 0.3.1. Install PHP# 0.3.2, or require heyjordanparker/php-sharp-composer 0.3.1.

When both are the same version, it says they were built from different sources: rebuild the engine, or reinstall the library. When Composer reports no version, it names only the engine's.

Opcache

  • Shared memory revalidates a file after opcache.revalidate_freq. The file cache validates on load when opcache.validate_timestamps is on. Both call sharp_compiled_revision, which runs sharp_check.
  • Preloaded scripts and opcache.validate_timestamps=0 never revalidate any file, by opcache's design. They only serve op arrays this engine's sharp_check accepted. A mismatch needs a new .sharpc on disk, which a revalidating cache sees, and the next restart preloads through sharp_check again.

Tests

Each new row failed first, with the check switched off.

  • Zend/tests/sharp/library_autoload_composer_container.phpt runs a post-autoload-dump callback on a matching and a mismatched library. It runs without opcache, under opcache preloading, and with --no-plugins. On the mismatched library, App\Greeting through app/Greeting.php, Sharp\Container, the plugin's loader, require and require_once each refuse.
  • Zend/tests/sharp/native_fingerprint_opcache.phpt caches a file in the CLI file cache and in a server's shared memory. After a compile against other native bodies, both refuse it.
  • Zend/tests/sharp/library_autoload_composer_plugin.phpt drives the plugin's dumpClassMap and checks the error line.
  • sharp/mago/tests/compile.rs checks that mago compile writes the fingerprint and the version. It also checks that a library with no native.php writes none, and that a native.php with no fingerprint fails the compile.
  • require_native.phpt and the library_autoload_* tests expect the new messages. refusal_damaged.phpt refuses a version text past the texts.

A real Composer ran on the review's fixtures:

  • On mm, the mismatched project:
    • composer --no-plugins dump-autoload refuses at the .php file, at require and at require_once.
    • composer install, dump-autoload and dump-autoload --optimize exit 0 and print the refusal.
    • A separate process and preloading refuse.
  • On up, composer update from 0.3.1 to 0.3.2 prints the refusal and exits 0.
  • On fc, a file cached at 0.3.1 is refused after the update and the compile. This holds for the CLI file cache and for php-fpm's shared memory in the same worker.
  • On ok and fx2, the matching projects, nothing changed. ok prints hello in Composer's process and no greeting in a separate one. fx2 prints no greeting in both.

Problem: since #160, mago compile writes a <Name>.php beside each .sharp
file. Inside Composer's own process, autoload.php skipped requireNative
so composer install runs on any engine, and only the plugin's loader
checked the engine. Composer's own loader includes the .php file, which
loads the .sharp file directly, so a post-autoload-dump callback ran
PHP# code on an engine the installed library does not match, until it
reached a missing native body.

Solution: the engine keeps the refusal requireNative throws, and
refuses every .sharp file the request loads after it with the same
message, whatever loads it. autoload.php calls requireNative in
Composer's process too and ignores its refusal there, so composer
install still runs and only PHP# code fails.

ext/sharp/
└── sharp.c                     native_refusal: requireNative keeps its
                                refusal, sharp_compile_file throws it
sharp/composer/
└── autoload.php                requireNative runs in Composer's process,
                                its refusal ignored there
Zend/tests/sharp/
└── library_autoload_composer_container.phpt   a mismatched library is
                                refused through the .php file, the
                                container and a require, also preloaded
docs/
├── decisions/081-discovery.md  the .php file meets the same refusal
├── architecture/modules.md     the test's new rows
└── architecture/slices/sharp-discovery.md   found-not-fixed 6 is fixed
…built for

#169's review found three paths that still ran PHP# code on an engine the
installed library does not match: composer --no-plugins, a file opcache already
caches, and composer update to such a library in one run. One rule closes all
three, whatever loads the file.

- mago compile writes the library's native fingerprint, from its native.php,
  and the version installed.json lists into each .sharpc header, now 128 bytes.
- sharp_check refuses a compiled file whose fingerprint is not SHARP_NATIVE.
  Every loader reaches it, and opcache's revalidation through
  sharp_compiled_revision.
- The per-request refusal and the per-body check are gone. requireNative keeps
  its three arguments, so engines and libraries of any version refuse each other.
- The Composer plugin calls requireNative at each post-autoload-dump, before its
  early return, and prints the refusal as an error line. Composer goes on.
- The refusal names both versions and the two fixes.
@heyJordanParker heyJordanParker changed the title fix: a mismatched engine refuses PHP# code in Composer's process fix: each compiled file names the native fingerprint its library was built for Oct 17, 2026
@heyJordanParker
heyJordanParker merged commit 415ccf7 into master Oct 18, 2026
24 checks passed
@heyJordanParker
heyJordanParker deleted the fix/version-check branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant