Skip to content

ComptimeValue.text_refs duplicates the str's holder count (D111/D120): a str read out of a ComptimeValue needs .clone() or it is freed under the copy #3032

Closed

Description

@ehartford

Found by the #2942 str-clone sweep (#3027): removing .clone() from value.text at nine sites in ComptimeEval.w and BuildGraphMaterialize.w double-freed a str in the build-w-workspace-parallel-stress selfhost case (cli-selfhost-build-w-tests, battery red).

Exact chain (debug allocator + the route's trap pair under lldb with WITH_BUILD_JOBS=1): the block is allocated by with_str_clone_ref inside comptime_value_clone ← ComptimeEvaluator.capability_args; released to zero holders and freed by ComptimeValue's drop glue inside eval_buildctx_capability_method; freed again by BuildGraphGeneratedSource's drop in materialize_build_graph_from_comptime.

Why: ComptimeValue manages its text with its own count (text_refs; comptime_value_share bumps it; impl Drop for ComptimeValue frees the text when it reaches zero and blanks the header otherwise), bypassing the str's D111 holder count. A value.text read out of a ComptimeValue is a str copy the str's count knows about but text_refs does not, so the ComptimeValue's drop frees the buffer under the copy. ComptimeValue.w says it: "A boundary transferring text into an ordinary owning str must use comptime_value_clone." The .clone() at those boundaries is a deep copy the convention requires, not ceremony, and the census's str-clone pattern cannot tell (the comments "D32: take intended; clone until the D32 compiler is the seed" mark the sites).

Principled fix: D111/D120 made the side count redundant — a str is an immutable, shared, counted buffer already. Drop text_refs, let comptime_value_share be a str copy and ComptimeValue's drop an ordinary field drop (#1944's quadratic deep clone was the reason for the side count; the str's own count solves it the same way). Then the nine .clone() go, and comptime_value_clone becomes a plain copy. Until then the PR keeps the nine clones with a comment naming this issue.

Activity

  1. added a commit that references this issue on Nov 7, 2026
  2. ehartford commented on Nov 7, 2026

    @ehartford
    ContributorAuthor

    Fixed in #3056 (merged as afbdbb1).

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