Skip to content

compiler double-frees a HashMap in Zcu.compile_source_frontend_mode on a c_import error path (openssl 4.0.2 evp.h); regression since v0.15.2.1 #1158

Closed

Description

@ehartford

Repro

with init + with get c.openssl (fetches OpenSSL 4.0.2 today) + build/release_uat_fixtures/openssl_main.w as src/main.w, then with check src/main.w with the current main compiler (0a0ff13, darwin-aarch64):

invalid free addr=4451869408
debug-alloc: invalid-free forensics: slab range 27 start=4451827712 header-offset=41680 header-word=4451880832
panic: invalid free: pointer is not an allocated payload start

With WITH_DEBUG_ALLOC=1:

debug-alloc: DOUBLE FREE addr=4789543648 size=64 origin=unknown first_drop=drop#struct __drop_struct_229 second_drop=<untagged>

lldb at dbg_report_double_free:

frame #6: with_hashmap_free at rt_core.w:3312
frame #7: Zcu.compile_source_frontend_mode at main.w (inlined)
frame #9: Zcu.compile_file_frontend_with_config
frame #10: Compilation.compile_file
frame #11: run_cli

What the path is

On these headers the generated <c_import openssl/evp.h> module raises a Sema error (let OSSL_DEPRECATEDIN_4_0: c_int = OSSL_DEPRECATED(4.0) → "undefined variable": an object macro whose value calls an attribute macro that has no With form; the release PR #1155 stops emitting such macros). The double free happens on that error path of compile_source_frontend_mode: a HashMap local is dropped twice.

Attribution

  • The 09-06 release compiler v0.15.2.1 takes the same error path on the same headers and exits cleanly with the diagnostic (plus the fixture's now-gated write import) — no double free. So this is a regression on main between d9091ce (v0.15.2.1) and 0a0ff13.
  • It surfaced now because with get c.openssl moved from 3.x to 4.0.2, whose new OSSL_DEPRECATEDIN_4_0 macros first reach that error path.

Route

Per CLAUDE.md: --dump-drop-state / --trace-ownership on compile_source_frontend_mode for the map local, WITH_DEBUG_ALLOC_TRAP_FREE=<addr> under WITH_ALLOC_NO_REUSE with tools/debug_drop_sites.lldb to get the two drop sites, then the MIR that scheduled the second drop. Candidates: the error-return edges added to the frontend by the D39/#1151 work (bundle interface collection on demand, one path boundary) between 09-06 and 09-16.

Blocks the openssl release UAT only until #1155's generator fix lands (the error path is then not taken for this fixture); the double free itself remains for any c_import module that errors.

Activity

  1. ehartford commented on Sep 17, 2026

    @ehartford
    ContributorAuthor

    The address-trap investigation located the earlier free. The root is broader
    than frontend error-edge cleanup: HashMap extraction shallow-copies non-Copy
    elements into an owning Vec.

    On the current source (69bf4e70), LLDB with ASLR disabled and
    WITH_DEBUG_ALLOC_TRAP_FREE=4712080096 shows:

    1. Key-string allocation in Zcu.c_import_record_omissions_frontend.
    2. Free at sema_clone_str_str_hashmap +404 (0x1004ece58): the generated
      destructor for the helper's let ks = src.keys() Vec drops its strings.
    3. Reuse of that allocation in Sema.select_trait_impl via hm_grow.
    4. Free through the original map's stale key at Frontend.w:1780.
    5. Double free when the actual trait-selection map is destroyed.

    MAP_KEYS → with_hashmap_keys_out raw-copies string headers. MAP_VALUES
    and MAP_ITEMS have the same contract defect, and ordinary for map iteration
    also lowers through MAP_ITEMS. The correction must cover the collection
    operations, not avoid keys() in this Sema helper. No memory fix is applied yet.

    Small string-map probes run successfully normally but fail after snapshot
    destruction with WITH_DEBUG_ALLOC_SCRIBBLE=1. The reducer predicate requires
    both the normal success and the scribbled failure, avoiding a false reduction
    to an unrelated empty-map unwrap panic.

    The audit blind spot is filed as #1159; the address-trap helper defects found
    during the hunt are #1160. Full evidence and the release failure matrix are
    being recorded in docs/with-get-release-investigation.md.

    Separately, #1155 did not eliminate the OpenSSL macro failure. LLDB shows
    ci_translate_macros marking OSSL_DEPRECATED emitted before recording
    the same macro untranslated; the dependent object macro then passes the
    emitted-callee guard. This also reproduces on Darwin with a cold cache.

  2. ehartford commented on Sep 19, 2026

    @ehartford
    ContributorAuthor

    Fixed by #1190 (D44 step 1, c1760ffd on main).

    Root cause: HashMap.keys() / values() / items() byte-copied their elements (with_hashmap_{keys,values,items}_out), so for a non-Copy element the returned Vec and the map owned the same buffers. sema_clone_str_str_hashmap called .keys() on a HashMap[str, str]; dropping that Vec freed the map's key strings, and the map freed them again. Allocator verdict: DOUBLE FREE size=64 first_drop=drop#struct __drop_struct_229; lldb at dbg_report_double_free: second free with_hashmap_free from Zcu.compile_source_frontend_mode. It vanished under WITH_ALLOC_NO_REUSE=1 and under instrumentation because it needed the freed block to be reused.

    Evidence after the fix: with a release compiler built from main + #1190, with build of the fetched OpenSSL project no longer dies with invalid free (it reports the underlying c_import error cleanly); on the campaign branch, which also carries the c_import fixes, with run prints openssl UAT passed and the whole darwin :release-uat group is green. test/behavior/behav_d44_map_ownership.w holds the regression under --debug-alloc with WITH_DEBUG_ALLOC_SCRIBBLE=1.

    The audit blind spot that let it through stays open as #1159.

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