Skip to content

feat: boot from Composer's autoloader (closes #21), plus ClassSpecializer::evict() and Core::sizeOfType() - #183

Merged
lisachenko merged 3 commits into
8.4from
claude/userland-php-generics-audit-1t6xy7
Aug 9, 2026
Merged

feat: boot from Composer's autoloader (closes #21), plus ClassSpecializer::evict() and Core::sizeOfType()#183
lisachenko merged 3 commits into
8.4from
claude/userland-php-generics-audit-1t6xy7

Conversation

@lisachenko

@lisachenko lisachenko commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Why

An audit of userland-php-generics (the reference consumer) found it reaching behind z-engine's API line for operations that had no named entry point: probing isset(Core::$executor) to ask "is the engine booted", deleting class-table buckets through the @internal HashTable::delete(), pairing Core::sizeof(Core::type(...)) so a raw FFI\CType crossed the boundary, and re-deriving "can the engine run here" from ini_get('ffi.enable').

Review feedback then pointed at the better fix for most of that: don't make consumers initialize the engine at all.

Closes #21 — automatic initialization via Composer

bootstrap.php, registered through autoload.files, boots the bridge. require vendor/autoload.php is now the whole boot for every consumer.

Issue #21 has been open since 2019 on one blocker, recorded in its own comments: "Preloading breaks all the things — because composer autoloader calls the Core::init() before preload initialization." That is real. An unconditional boot binds the definitions with FFI::cdef(), which lasts for the preload request only, and leaves an engine behind that turns the script's own Core::preload() into a no-op — so the server starts and every request after it fails.

So the bootstrap recognises the preload stage and serves it with Core::preload(), whose FFI::load() publishes the definitions under FFI_SCOPE for the life of the server; everything else gets Core::init(). The stage is identified by the one fact that distinguishes it: during preloading, the script named by opcache.preload is the first file the process included.

Verified end to end on a matching PHP line, not just asserted:

preload via autoloader  → FFI::scope('ZEngine') resolves in the next request   ✅
preload skipping the boot → "Failed loading scope 'ZEngine'"                    ✅ (control)

Core::preload() is now idempotent too, so the explicit call an existing opcache.preload script makes right after requiring the autoloader stays harmless.

Failure is silent, and explained where it matters

A host without ext-ffi, with ffi.enable=0, on an unsupported PHP minor or platform can still legitimately autoload this package — static analysis, FFI-disabled test jobs, composer-time tooling. Throwing from an autoloaded file breaks all of them at require, so the boot is left undone in silence. Nothing is lost: Core::init() is idempotent, so code that needs the engine calls it and gets the same explanatory RuntimeException the bootstrap swallowed. ZENGINE_AUTOBOOT=0 skips the boot entirely.

Note z-engine's own test bootstrap deliberately still calls Core::init() explicitly — every test here drives the engine, so an unsupported host has to say so once rather than through hundreds of confusing failures. AutoBootTest proves the automatic path in child processes, where it can be observed without that line interfering.

Also in this PR

  • ClassSpecializer::evict(string $className): bool — the destroying counterpart of specialize(): removes the class-table bucket so destroy_zend_class() dismantles the entry now, while op_array-refcounted bodies shared with the source stay alive. Reads the class through ReflectionClass::isUserDefined()/isImmutable()/isPreloaded() — no ce_flags from the outside. ReflectionClass::isPreloaded() is added beside isImmutable().
  • Core::sizeOfType(string $type): int — the named form of the sizeof(type(...)) pair; Core::type() is now @internal, so no raw CType crosses the API line.
  • AGENTS.md — the @internal / FFI-CData rule for dependent packages folded into Public APIs never leak CData.

Deliberately no ReflectionClass::evict() mirror: an instance method destroying the entry its own wrapper points at invites use-after-free on $this->pointer.

Review comments

All four addressed in b1c0b92Core::isUsable() dropped (the bootstrap removed the need for it rather than relocating it), evict() moved onto the reflection API, AGENTS.md section folded. Replies are on the threads.

Notes for review

🤖 Generated with Claude Code

https://claude.ai/code/session_019TGQqR7ByjHrVSYKVHPrkJ

…pe(), ClassSpecializer::evict()

Dependant packages were reaching behind the API line for operations that had no
named entry point: probing Core::$executor to ask "is the engine booted",
deleting class-table buckets through the @internal HashTable::delete(), and
pairing Core::sizeof(Core::type(...)) so a raw FFI\CType crossed the boundary.

- ClassSpecializer::evict(): the destroying counterpart of specialize().
  Removes the class-table bucket so destroy_zend_class() dismantles the entry
  now, while op_array-refcounted bodies shared with the source stay alive.
  Refuses internal and shared-memory (immutable/preloaded) entries. The
  explicit-teardown test now exercises it.
- Core::sizeOfType(): the named form of the sizeof(type(...)) pair;
  Core::type() is @internal so no raw CType crosses the API line anymore.
- Core::isUsable(): non-throwing projection of the boot guard, for consumers
  and test bootstraps deciding whether engine paths can run here at all -
  hand-rolled ffi.enable checks get the supported 'preload' mode wrong.
- AGENTS.md: a 'Consuming z-engine from another package' section drawing the
  line those leaks crossed because it was never written down.

No ReflectionClass::evict() mirror on purpose: an instance method destroying
the entry its own wrapper points at invites use-after-free on $this.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019TGQqR7ByjHrVSYKVHPrkJ
Comment thread AGENTS.md Outdated
Comment thread src/Core.php Outdated
Comment thread src/Reflection/ClassSpecializer.php Outdated
claude added 2 commits August 9, 2026 21:14
Closes #21, open since 2019 on one blocker recorded in its own comments:
'the composer autoloader calls Core::init() before preload initialization'.
An unconditional boot binds the definitions with FFI::cdef(), which lasts for
the preload request only, and leaves an engine behind that turns the script's
own Core::preload() into a no-op - the server starts and every request after
it fails.

bootstrap.php (autoload.files) therefore recognises the preload stage and
serves it with Core::preload(), whose FFI::load() publishes the definitions
under FFI_SCOPE for the life of the server; everything else gets Core::init().
The stage is identified by the one fact that distinguishes it - during
preloading the script named by opcache.preload is the first file the process
included. Verified end to end on a matching PHP line: FFI::scope('ZEngine')
resolves in the request that follows, and does not when the preload script
skips the boot.

A host that cannot run the engine (no ext-ffi, ffi.enable=0, wrong PHP minor,
no definitions for the platform) is left uninitialized in silence - throwing
from an autoloaded file would break static analysis, FFI-disabled test jobs
and composer-time tooling at require. Nothing is lost: Core::init() is
idempotent, so code that needs the engine calls it and gets the same
explanation this file swallowed. ZENGINE_AUTOBOOT=0 skips the boot entirely.

Core::preload() is now idempotent too, so the explicit call an existing
opcache.preload script makes after requiring the autoloader stays harmless.

Review feedback on the API additions:
- Core::isUsable() dropped - init() and isInitialized() are enough
- evict() reads the class through ReflectionClass::isUserDefined()/
  isImmutable()/isPreloaded() instead of ce_flags off the raw entry; the new
  isPreloaded() sits beside isImmutable()
- the consumer section folded into 'Public APIs never leak CData', keeping the
  @internal / FFI-CData part

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019TGQqR7ByjHrVSYKVHPrkJ
PHP does not support opcache preloading on Windows at all, so both preload
cases failed there: the engine reported 'Preloading is not supported on
Windows', and the scratch script path went through a short name (RUNNER~1)
whose tilde broke -d ini parsing before that.

Guarded with the same skip the repository already uses for opcache.preload
(issue #119), on the two cases that need it rather than the whole class - the
opt-out and silent-failure cases are plain autoload behaviour and keep running
everywhere. The --fail-on-skipped opcache gate runs on Linux and macOS, where
preloading exists and neither case skips.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019TGQqR7ByjHrVSYKVHPrkJ
@lisachenko lisachenko changed the title feat: name the public consumer API — Core::isUsable(), Core::sizeOfType(), ClassSpecializer::evict() feat: boot from Composer's autoloader (closes #21), plus ClassSpecializer::evict() and Core::sizeOfType() Aug 9, 2026
@lisachenko
lisachenko marked this pull request as ready for review August 9, 2026 21:29
@lisachenko
lisachenko merged commit 753de91 into 8.4 Aug 9, 2026
19 checks passed
@lisachenko
lisachenko deleted the claude/userland-php-generics-audit-1t6xy7 branch August 9, 2026 21:30
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.

Add automatic self-registration via Composer

2 participants