`ShareCommon.shareCommon` does not share equal strings/scalar arrays with different capacities (`lean_sharecommon_hash`/`_eq` include `m_capacity`)

オープン
#16,808 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
51/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発

調査の方向性

Read src/runtime/sharecommon.cpp, especially lean_sharecommon_hash and lean_sharecommon_eq, alongside the ShareCommon.shareCommon documentation in src/Init/ShareCommon.lean. Reproduce the issue with the supplied input.lean example and inspect how strings and scalar arrays are hashed and compared. Done means equal strings with different capacities are shared, and the example prints shared=true.

索引モデルが issue の本文から書いたものです。

説明

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    If in doubt, please reach out on [email protected] for responsible disclosure.
    (Not a soundness bug.)
Description

lean_sharecommon_hash (sharecommon.cpp:33-45) and lean_sharecommon_eq (sharecommon.cpp:13-31) hash and compare every byte after the lean_object header. For strings and scalar arrays that range includes m_capacity, so two equal strings with different capacities (a literal and an appended string) get different hashes and compare unequal. visit_terminal (sharecommon.cpp:390-398) then keeps them as distinct objects. The ShareCommon.shareCommon docstring (Init/ShareCommon.lean:111-117) says strings and scalar arrays are shared, and PR #5840 meant equality to depend only on the value, not the allocation.

The program builds the string "hello" by appending (args.getD 0 "h" ++ "ello") and prints equal=true (it compares equal to the literal "hello"), then calls ShareCommon.shareCommon ("hello", s) and prints shared=false: the copy keeps the two structurally equal strings as two distinct objects, so the documented result (structurally equal subobjects represented by the same object) is not delivered. lean_sharecommon_hash is called on the literal "hello" (m_size 6, m_length 5, m_capacity 6) and on the character-equal appended string (m_size 6, m_length 5, m_capacity 12) and returns different hashes, because the hashed range includes m_capacity. Because the hashes differ, m_set.find in sharecommon_quick_fn::visit_terminal (sharecommon.cpp:391) misses, the appended string is inserted as a new key and returned as itself, and ptrEq of the two strings in the result is false. lean_sharecommon_eq is never even called here, so the different hash is what breaks the sharing. Note the same defect exists one step later: lean_sharecommon_eq's memcmp covers the same range [8, 38), which also contains the m_capacity field, so it would report the pair unequal too.

Documented behavior it contradicts, from the ShareCommon.shareCommon docstring:

Returns a copy of a in which structurally equal subobjects are represented by the same object.

Only arrays, scalar arrays, strings, big numbers, and constructor objects are shared; closures,
thunks, tasks, references, and external objects are kept as they are. Sharing is local to one call:
results of separate calls do not share subobjects with each other.

Context
  • Affected functions: lean_sharecommon_hash and lean_sharecommon_eq (src/runtime/sharecommon.cpp)
  • Related upstream report: #5831, closed by PR #5840, concerned hashing unused, potentially uninitialized bytes beyond the string's actual content. That fix changed the processed range from allocated capacity to actual data size, but the range still includes the initialized m_capacity field in the string/scalar-array object structure. This report concerns that remaining capacity-field issue: equal values with different capacities still fail to share after the earlier fix. The cited code remains present in nightly-2026-11-26, where the string example below still prints equal=true and shared=false.
Steps to Reproduce
  1. Save the following as input.lean (imports only Init):
/-
Two strings with the same characters: a literal "hello" (size 6, capacity 6) and the result of
appending "h" ++ "ello" (size 6, capacity 12). `ShareCommon.shareCommon` must represent these
structurally equal subobjects by the same object.
-/
unsafe def main (args : List String) : IO Unit := do
  let s := args.getD 0 "h" ++ "ello"
  IO.println s!"equal={s == "hello"}"
  let q := ShareCommon.shareCommon ("hello", s)
  IO.println s!"shared={ptrEq q.1 q.2}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. Run: ./prog

Expected behavior:

equal=true
shared=true

Actual behavior:

equal=true
shared=false
Versions

Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)

Additional Information

Root cause analysis. The argument is an ordinary String: sharecommon_quick_fn::set_hash/m_set.find/m_set.insert receive whatever objects sharecommon_quick_fn::visit sends to visit_terminal, here the value of args.getD 0 "h" ++ "ello" (a lean_string_object with m_size 6, m_length 5, m_capacity 12, characters "hello"). Two objects with the same tag, the same m_other and the same characters must get the same hash, otherwise the hash-consing set cannot match them. No callee hands it a wrong value: lean_object_data_byte_size returns 38 for both strings, which is the correct salient size sizeof(lean_string_object) + m_size = 32 + 6 for a 5-character string, and hash_str hashes exactly the bytes and length it is given. The differing hash therefore comes from this function's own choice of range (sharecommon.cpp:41-44, hash_str over the (sz - sizeof(lean_object)) bytes starting at the object + 8), which for a String begins with the m_size/m_capacity/m_length header fields, so the two strings differ at offset 16 (m_capacity 6 vs 12) although their characters are identical.

Suggested fix: a fix confined to lean_sharecommon_hash will not restore the sharing: lean_sharecommon_eq's memcmp covers the same [8, 38) range and would still report the two strings unequal, so hash and eq must both stop covering the m_capacity word at offset 16 for strings/arrays/scalar arrays.

Impact

Any use of ShareCommon on data with runtime-built strings or byte arrays, whose capacity almost always differs from their size, silently misses sharing, so less memory is saved than documented.

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

主要言語
Lean
スター
10.3k
フォーク
1.1k
平均マージ
16時間 45分
マージ済み PR(30日)
361

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

leanprover/lean4 のほかの issue

leanprover/lean4 の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。