Skip to content

Language: instanceof with dynamic class name — TypeError for non-string operand (zend_execute.c parity) #4339

Description

@PurHur

Category

language

Problem

Compile-time instanceof ClassName and union RHS (A|B) work (#138, #3461). For dynamic RHS $obj instanceof $className, Zend requires operand #2 to be a string (or valid class name string after coercion in legacy modes); passing an int/array/object raises TypeError.

This compiler's VM TYPE_INSTANCEOF always calls $frame->scope[$op->arg3]->toString() on the RHS without a type guard, which can silently stringify or mis-compare instead of throwing.

php-src reference

Repro (failure today)

<?php
declare(strict_types=1);

class C {}
$o = new C();

try {
    var_export($o instanceof 123);
} catch (TypeError $e) {
    echo 'TypeError', "\n";
}

try {
    var_export($o instanceof []);
} catch (TypeError $e) {
    echo 'TypeError', "\n";
}

$name = 'C';
var_export($o instanceof $name);
echo "\n";
HARNESS_DOCKER_RUN_OPTS='--memory=8g --cpus=2' ./script/docker-exec.sh -- bash -lc '
  source script/php-env.sh
  php -r "...snippet above..."
  php bin/vm.php -r "...snippet above..."
'
Call Zend PHP 8.x This compiler VM (today)
$o instanceof 123 TypeError may coerce/compare without TypeError
$o instanceof [] TypeError may stringify / false without TypeError
$o instanceof $name ($name='C') true likely OK — keep working

Scope (this repo)

Module Path
Compiler lib/Compiler.php (instanceof lowering when RHS is non-literal)
VM lib/VM.php (TYPE_INSTANCEOF, valueInstanceOfClassName)
JIT lib/JIT.php (TYPE_INSTANCEOF branch)
Tests test/compliance/cases/language/instanceof_dynamic_typeerror.phpt

Done when

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITarea:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions