Skip to content

Migrator: a function-like macro's translation depends on earlier macro lookups (cache side effect) #2265

Closed

Description

@ehartford

Migrator: whether a function-like macro translates depends on which macro lookups ran earlier

Found while landing D109 (#2131). ci_lookup_macro_value writes g_migrate_macro_values (hits) and g_migrate_macro_miss_names (misses) as a side effect. The text translator's literal fallback, ci_lookup_simple_literal_macro_value, reads g_migrate_macro_values and never scans the macro session. So a lookup that some unrelated code path happens to make earlier changes how a later macro translates.

Repro (pcre2 corpus, Darwin, main at 7d729c2). Migrating the pcre2 source directory with the corpus options, main's migrator leaves IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9) untranslated (lldb at the ci_record_untranslated_macro(name) site after ci_translate_c_expr; work_value is the raw body, known_values has 338 bytes and no CHAR_0). A migrator that additionally calls ci_lookup_macro_value for identifiers it sees during body lowering translates the same body, with the same work_value and known_values, to (((x >= 48) and (x <= 57)) as c_int) and also emits ESCAPES_LAST and moves LOOKBEHIND_MAX. The only difference between the two runs is the cache contents.

Expected. A macro's translation is a function of the macro and the translation unit, never of lookup order. Either the literal fallback consults the macro session itself (and IS_DIGIT translates every time) or it reads only what the capture pass recorded (and it never does); which one is right depends on whether CHAR_0's value should be visible there.

Workaround in #2131's change. The offsetof checks read macros through a cache-free ci_peek_macro_value, so they do not perturb other macros. The corpora stay byte-identical to main's migrator apart from offsetof sites.

Activity

  1. ehartford commented on Oct 8, 2026

    @ehartford
    ContributorAuthor

    Root cause found; it was not lookup order in the migrator's design. Three layered bugs:

    1. Move checker (safety). HashMap.remove, increment, decrement and HashSet.remove take an owned key and MIR lowers it as a move, but Sema never marked the binding moved. A later use compiled clean and read the move-blanked string. Repro: let a = mk(); m.remove(a); print(a) prints ``.
    2. Migrator capture. ci_capture_macro_values called g_migrate_macro_last_values.remove(name) and then kept using name, so every macro was stored under the key "": the per-file macro cache held one entry, and only names some other path happened to look up got in. That is the order dependence this issue saw.
    3. Macro probe. Every pcre2 unit's clang probe failed on clang's default error limit (20): the location-less "too many errors emitted" read as a header error and discarded the whole batch, so no pcre2 macro got its typed, folded value.

    Fix in progress on branch macro-cache: Sema marks those keys moved (four compile-error tests), the capture drops the needless remove (insert replaces), and parses pass -ferror-limit=0. The corpora are being regenerated and tested.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions