python / python/cpython

Tier 2 optimizer may use canonical builtins for functions with a copied __builtins__ dictionary

Offen
#157,468 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core topic-JIT type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

A function can use a builtins dictionary other than the interpreter’s canonical builtins dictionary. With the JIT enabled, _LOAD_GLOBAL_BUILTINS may nevertheless be constant-folded using interp->builtins.

A dictionary created by vars(builtins).copy() can share its keys table and keys version with the canonical dictionary while storing independent values. Replacing an existing value such as len does not necessarily change that keys version.

The optimizer validates and watches interp->builtins, then obtains the constant from that dictionary. It does not first verify that the optimized function’s func_builtins is the same dictionary. Consequently, an optimized executor can continue using the
canonical len after the function’s own builtins dictionary has been changed.

I reproduced this on main at commit a60343ed17785ebbcd43de9080cadd8e2541db6f. The non-JIT interpreter produces the expected result.

The direct reproducer is a regression introduced by GH-138379 and first appears in Python 3.15.0a1.
A related case involving distinct functions with the same function/code version but different builtins dictionaries dates back to GH-116460 and Python 3.13.0a5.

Minimal reproducer

import builtins
from _testinternalcapi import TIER2_THRESHOLD

namespace = {"__builtins__": vars(builtins).copy()}

exec(
    """
def size(value):
    return len(value)

def run(value, n):
    for _ in range(n):
        result = size(value)
    return result
""",
    namespace,
)

print(namespace["run"]([0], TIER2_THRESHOLD))

namespace["__builtins__"]["len"] = lambda value: 42

print(namespace["run"]([0], 8))

Run it with a JIT-enabled build:

$ PYTHON_JIT=1 ./python repro.py
1
1

Expected output:

1
42

With the JIT disabled, the expected result is produced:

$ PYTHON_JIT=0 ./python repro.py
1
42

Proposed fix

Only constant-fold _LOAD_GLOBAL_BUILTINS when the current function uses the interpreter’s canonical builtins dictionary:

ctx->frame->func != NULL &&
ctx->frame->func->func_builtins == interp->builtins

For a custom builtins dictionary, retain the ordinary _LOAD_GLOBAL_BUILTINS operation so that it reads and guards the
function’s actual mapping.

A runtime identity guard should also accompany constants folded from the canonical builtins dictionary:

DEOPT_IF(BUILTINS() != tstate->interp->builtins);

The runtime guard is needed because function or code version checks alone do not identify the function’s builtins mapping.
A distinct function created from the same code object can use a different __builtins__ dictionary while satisfying the existing version guard.

Regression tests should cover:

  1. Mutating an existing value in a copied builtins dictionary after an executor has been created.
  2. Calling a different function with the same code/version but a different builtins dictionary through an existing optimized
    executor.
CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-157766

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie, indem Sie den minimalen Reproducer mit PYTHON_JIT=1 ausführen und ihn mit dem Ergebnis ohne JIT vergleichen. Verfolgen Sie die Behandlung von _LOAD_GLOBAL_BUILTINS durch den Tier-2-Optimizer und die vorhandenen Laufzeitprüfungen; fügen Sie anschließend Regressionstests sowohl für kopierte Builtins-Dictionaries als auch für unterschiedliche Funktionen hinzu, die sich Code/Version teilen. Erledigt ist die Aufgabe, wenn beide Fälle nach der Optimierung das erwartete Ergebnis mit benutzerdefinierten Builtins erzeugen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, testing-qa
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.