`ShareCommon.shareCommon` does not share equal strings/scalar arrays with different capacities (`lean_sharecommon_hash`/`_eq` include `m_capacity`)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 51/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- backend, performance
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 16h 45m
- PR unite (30g)
- 361
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
leanprover/lean4#16876 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16842 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16841 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
leanprover/lean4#16840 ·
I maintainer di solito rispondono entro 1 giorno
-
doc: stray "public" in Bool and HEq documentationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
leanprover/lean4#16825 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
Thai project names become 'project', 'project-2'… (slug keeps ASCII only)Forse già presa @mastel2 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
storytold/craft-anything#2538 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
compiler-explorer/compiler-explorer#9738 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
openfoodfacts/openfoodfacts-python#573 ·
I maintainer di solito rispondono entro 2 giorni
-
Rasterio reads over HTTPS do not send the Earthdata token and return 500 "HTTP response code: 404"Apertacredentials rasterio
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
developmentseed/titiler-cmr#359 ·
I maintainer di solito rispondono entro 1 giorno