Skip to content

Operator overloading is skipped when the operand is an erased (mixed/cell) value, so BcMath\Number arithmetic computes on the pointer. #263

Closed

Description

@blacktrs

Summary

Operator overloading is skipped when the operand is an erased (mixed/cell) value, so BcMath\Number arithmetic computes on the pointer.

Repro

<?php
use BcMath\Number;
function plus(mixed $x, mixed $y): mixed { return $x + $y; }
function lt(mixed $x, mixed $y): bool { return $x < $y; }
echo plus(new Number('1.5'), new Number('2')), "\n";
echo plus(new Number('1.5'), 1), "\n";
var_dump(lt(new Number('1'), new Number('2')));
$items = [new Number('1'), new Number('2')];
echo array_reduce($items, fn($c, $n) => $c + $n, new Number('0')), "\n";

Expected (php)

3.5
2.5
bool(true)
3

Actual (manticore a79499e6, Darwin arm64)


[exit code 138 — SIGBUS]

Tracking

Known-bug repro: tests/aot/repro/crash/overloadable_erased_operand.php — bash tests/aot/xfail.sh -k overloadable_erased_operand reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    crash
    on Oct 15, 2026
  2. blacktrs commented on Oct 15, 2026

    @blacktrs
    ContributorAuthor

    Context

    Operator overloading (Manticore\Overloadable, BcMath\Number) is a compile-time rewrite in InlineClosures::tryOperator: an arithmetic / comparison node whose operand is statically typed as an implementing class becomes a __operator / __compare method call. An operand typed mixed / cell / unknown (an untyped parameter, an array_reduce accumulator, an element read) is not rewritten, so + emits integer arithmetic on the object pointer. Result: wrong answer or SIGBUS instead of the Zend answer.

    Impact

    Any generic code that passes a Number (or a user Overloadable) through an untyped slot: callbacks, array_map/array_reduce, mixed params, container elements.

    Proposed approach

    Runtime dispatch on the erased path, decided where the erased arithmetic is emitted (EmitLlvmExpr, the tagged add/sub/mul/div/mod and the cell compare):

    1. In the tagged-arith slow path, when an operand's tag is OBJECT, test the class for Manticore\Overloadable (class table flag, one bit per class) and call its __operator / __compare through the vtable; otherwise keep today's TypeError (Unsupported operand types).
    2. Fast paths (int/float) are untouched, so there is no cost for ordinary numeric code.
    3. Reuse the swapped-operand and ** / unary-minus conventions already in tryOperator.

    Acceptance

    • tests/aot/repro/crash/overloadable_erased_operand.php passes and is promoted to cases/.
    • Same result for a statically typed and an erased operand.
    • Non-Overloadable objects in arithmetic still raise TypeError as in Zend.

    Related: local branch bcmath (operator hooks, Overloadable).

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

    bugcrash

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions