`ShareCommon.shareCommon` does not share equal strings/scalar arrays with different capacities (`lean_sharecommon_hash`/`_eq` include `m_capacity`)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 51/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- backend, performance
調査の方向性
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 の本文から書いたものです。
説明
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
ain 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_hashandlean_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_capacityfield 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 printsequal=trueandshared=false.
Steps to Reproduce
- 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}"
- Compile:
lean -c input.c input.lean && leanc -o prog input.c - 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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
leanprover/lean4 のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
leanprover/lean4#16876 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
leanprover/lean4#16842 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
leanprover/lean4#16841 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
leanprover/lean4#16840 ·
メンテナーはふだん 1 日以内に返信
-
doc: stray "public" in Bool and HEq documentation再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
leanprover/lean4#16825 ·
メンテナーはふだん 1 日以内に返信
leanprover/lean4 の issue をすべて見る
似ている issue
-
Thai project names become 'project', 'project-2'… (slug keeps ASCII only)対応中かも @mastel2 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
storytold/craft-anything#2538 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
compiler-explorer/compiler-explorer#9738 ·
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 3 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
openfoodfacts/openfoodfacts-python#573 ·
メンテナーはふだん 2 日以内に返信
-
Rasterio reads over HTTPS do not send the Earthdata token and return 500 "HTTP response code: 404"オープンcredentials rasterio
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
developmentseed/titiler-cmr#359 ·
メンテナーはふだん 1 日以内に返信