`ByteArray.copySlice` aborts with "INTERNAL PANIC: out of memory" for a bignum offset or length instead of clamping

Aperta
#16,801 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
backend

Direzione di ricerca

Read src/runtime/object.cpp around lean_byte_array_copy_slice and the reference definition in src/Init/Data/ByteArray/Basic.lean; reproduce with the provided input.lean program. Check the bignum conversions for srcOff, len, and destOff. Done means all three variants return the clamped result without panicking, with a regression test covering them.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Note on existing PRs: open PR #14286 ("fix: borrow Nat arguments to ByteArray.copySlice", not merged as of 2026-11-27) would fix this issue: it reads the offsets and length with a saturating conversion, so out-of-range values clamp instead of panicking, and adds the regression test tests/compile/byteArrayCopySliceBignum.lean. There is no issue tracking the bug itself, so this issue documents it, with a reproduction on nightly-2026-11-26, as the issue PR #14286 can close. (Open PR #14271 touches the same early return but only fixes a reference leak there; it does not fix this bug.)

lean_byte_array_copy_slice (src/runtime/object.cpp:2782-2787) converts srcOff, len and destOff with lean_nat_to_size_t before clamping them. lean_nat_to_size_t panics with "out of memory" for any Nat >= 2^64 (object.cpp:2702), so the src_off > ssz early return that implements the reference definition (dest unchanged) is never reached. The reference definition in ByteArray/Basic.lean clamps any Nat, so the panic contradicts the documented semantics. Offsets or lengths of 2^64 or more are very rare in practice.

The program below builds src = the 3-byte array [1, 2, 3] and dst = the 2-byte array [9, 9] at run time, prints them, then evaluates src.copySlice 18446744073709551616 dst 0 1 and prints the returned array. With srcOff = 2^64 > src.size = 3, destOff = 0, len = 1, the reference definition gives [] ++ [] ++ dst.extract (0 + min 1 (3 - 2^64) = 0) 2 = dst = [9, 9], so the program must print result [9, 9] and exit 0. Instead the process is terminated inside the call: stdout stops after the first line, stderr is INTERNAL PANIC: out of memory, and the exit status is 1. The call is well typed (srcOff : Nat may be any Nat), and the function even contains the correct handling for it (the src_off > ssz early return that returns dest unchanged), but it converts the raw Nat to size_t first, and lean_nat_to_size_t panics for this value, so that early return is unreachable for every srcOff >= 2^64. The same happens for a len or a destOff >= 2^64 (lines 2786 and 2787): src.copySlice 0 dst 0 18446744073709551616 and src.copySlice 0 dst 18446744073709551616 2 die the same way, although the definition clamps len and destOff to the arrays' sizes.

Documented behavior it contradicts, the docstring and reference implementation of ByteArray.copySlice (src/Init/Data/ByteArray/Basic.lean:154-161):

Copies the slice at [srcOff, srcOff + len) in {name}src to [destOff, destOff + len) in
{name}dest, growing {name}dest if necessary. If {name}exact is {name}false, the capacity
will be doubled when grown.

def copySlice (src : @& ByteArray) (srcOff : Nat) (dest : ByteArray) (destOff len : Nat) (exact : Bool := true) : ByteArray :=
  ⟨dest.data.extract 0 destOff ++ src.data.extract srcOff (srcOff + len) ++ dest.data.extract (destOff + min len (src.data.size - srcOff)) dest.data.size⟩
Context
  • Affected function: lean_byte_array_copy_slice (src/runtime/object.cpp)
  • Upstream: no existing issue found. Open PR #14286 fixes this (see the note above); open PR #14271 fixes only the related reference leak on the same early return. The cited lines are unchanged in nightly-2026-11-26.
Steps to Reproduce
  1. Save the following as input.lean (imports only Init/Std):
-- ByteArray.copySlice with a source offset that no machine size can hold.
-- The reference definition gives the result by the Lean formula
--   dest.data.extract 0 destOff ++ src.data.extract srcOff (srcOff + len)
--     ++ dest.data.extract (destOff + min len (src.data.size - srcOff)) dest.data.size
-- with srcOff = 2^64 > src.size = 3, destOff = 0, len = 1 that is dest unchanged, [9, 9].
def main (args : List String) : IO Unit := do
  let k := args.length
  let src := (ByteArray.mk #[1, 2, 3]).extract 0 (3 + k)
  let dst := (ByteArray.mk #[9, 9]).extract 0 (2 + k)
  let srcOff := 18446744073709551616  -- 2^64
  IO.println s!"src {src.toList} dst {dst.toList} srcOff {srcOff}"
  let r := src.copySlice srcOff dst 0 1
  IO.println s!"result {r.toList}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. Run: ./prog

Variants with the bignum in the other positions: replace the call with src.copySlice 0 dst 0 18446744073709551616 (len) or src.copySlice 0 dst 18446744073709551616 2 (destOff).

Expected behavior:

src [1, 2, 3] dst [9, 9] srcOff 18446744073709551616
result [9, 9]

exit status 0 (by the same definition the variants should print result [1, 2, 3] (len clamped to 3) and result [9, 9, 1, 2] (destOff clamped to 2) respectively).

Actual behavior:

stdout:

src [1, 2, 3] dst [9, 9] srcOff 18446744073709551616

stderr:

INTERNAL PANIC: out of memory

exit status 1. Both variants (bignum len, bignum destOff) give the same panic and exit status 1.

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. src is an ordinary 3-byte ByteArray and dest an ordinary 2-byte ByteArray, destOff = 0 and len = 1 are small scalars, and srcOff is the Nat 18446744073709551616 (a heap bignum). Neither the docstring nor the source bounds these Nats, so compiled Lean code may pass them. The call never returns: it converts the raw offset with lean_nat_to_size_t, which cannot represent 2^64 and panics (lean_internal_panic_out_of_memory -> "INTERNAL PANIC: out of memory", std::exit(1)), killing the program. It should have returned dest unchanged, [9, 9]. The offsets only ever need comparing against src.size / dest.size (the very next statement does if (src_off > ssz) return dest;), so the conversion must come after clamping or be done on the Nat. The same defect is in the o_len and o_dest_off conversions (lines 2786-2787). lean_nat_to_size_t itself behaves as intended for a bignum that does not fit a size_t (it does not return a truncated value but fails with lean_internal_panic_out_of_memory).

Suggested fix: in lean_byte_array_copy_slice, compare/clamp on the Nat first (e.g. check the bignum against the sizes, or use a saturating conversion as PR #14286 does) and only then take a size_t. The early-return path also never releases the owned o_len/o_dest_off bignums (PR #14271).

Impact

Only code that passes a bignum offset or length to ByteArray.copySlice (for example a "copy everything" sentinel) is affected: it aborts instead of getting a clamped result.

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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.