`lean_uv_udp_recv` returns a `ByteArray × Option SocketAddress` pair with constructor tag 1 instead of 0

Aperta Adatta ai principianti
#16,820 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à
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
backend

Direzione di ricerca

Inizia dal callback di ricezione in src/runtime/uv/udp.cpp, in particolare dalla chiamata a lean_alloc_ctor alla riga 226; confrontala con la costruzione della coppia in src/runtime/uv/dns.cpp o src/runtime/io.cpp. Riproduci il problema con il programma Lean fornito e verifica che la coppia ricevuta condivida la rappresentazione con una coppia uguale costruita in Lean dopo aver corretto il tag.

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

udp.cpp:226 builds the (ByteArray x Option SocketAddress) result with lean_alloc_ctor(1, 2, 0). Prod.mk has tag 0, and every other pair the runtime builds uses tag 0 (io.cpp:819, uv/dns.cpp:149, uv/system.cpp:242). It is the only lean_alloc_ctor(1, 2, 0) in src/runtime. So recv hands Lean a malformed value, and code that looks at constructor tags (shareCommon hashing/equality, as the run shows) treats it differently from an equal Lean-built pair. Field access by projection still works, so most programs never notice, but the fix is a one-character change.

The program binds a UDP socket to 127.0.0.1:39501, arms recv 1024, sends the 13-byte datagram "hello robert!" from 127.0.0.1:39502, and keeps the promise's Except.ok payload pr : ByteArray × Option SocketAddress exactly as the runtime produced it. It then builds the Lean-equal pair q := Prod.mk pr.1 pr.2 (through an @[noinline] helper, so no structure eta can collapse them) and prints equal={pr == q} -> equal=true. ShareCommon.shareCommon (pr, q) then fails to represent the two structurally equal pairs by one object: the program prints shared-pair-vs-built=false, while in the same program two Lean-built equal pairs print shared-built-vs-built=true (and the same program with the pair built with Prod.mk instead of received prints shared-pair-vs-built=true). The ShareCommon.shareCommon docstring says "Returns a copy of a in which structurally equal subobjects are represented by the same object", and constructor objects are among the shared kinds, so the two Lean-equal Prods must come back as one object and the program must print shared-pair-vs-built=true. It does not, because the value recv resolves its promise with is a mis-tagged pair: the receive callback this extern installs builds it with lean_alloc_ctor(1, 2, 0) (udp.cpp:226), a two-field constructor with runtime tag 1 instead of Prod.mk (tag 0), and lean_sharecommon_hash/lean_sharecommon_eq include the constructor tag, so a mis-tagged pair is never structurally equal to a well-formed one. The object handed to Lean is therefore not a value of ByteArray × Option SocketAddress at all: for that Prod the only constructor has tag 0 (compare the runtime's other pairs, src/runtime/io.cpp:819, src/runtime/uv/net_addr.cpp:118, src/runtime/uv/dns.cpp:149).

Documented behavior it contradicts, from the docstring of ShareCommon.shareCommon (src/Init/ShareCommon.lean):

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

Context
  • Affected function: lean_uv_udp_recv (src/runtime/uv/udp.cpp)
  • Upstream: no existing issue found (searched 'udp recv'; only merged PR #14795, which is unrelated to the tag); the cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce
  1. Save the following as input.lean (imports only Init/Std):
import Std.Internal.UV.UDP
import Std.Net.Addr
import Init.ShareCommon

open Std.Internal.UV.UDP
open Std.Net

def mkAddr (port : UInt16) : SocketAddress :=
  SocketAddress.v4 (SocketAddressV4.mk (.ofParts 127 0 0 1) port)

@[noinline] def mkPair (b : ByteArray) (a : Option SocketAddress) : ByteArray × Option SocketAddress :=
  (b, a)

unsafe def main : IO Unit := do
  let server ← Socket.new
  server.bind (mkAddr 39501)
  let client ← Socket.new
  client.bind (mkAddr 39502)
  client.connect (mkAddr 39501)
  let p ← server.recv 1024
  let _ ← client.send #[String.toUTF8 "hello robert!"] none
  let r ← IO.wait p.result!
  match r with
  | .ok pr =>
      let q := mkPair pr.1 pr.2
      IO.println s!"equal={pr == q}"
      let sh := ShareCommon.shareCommon (pr, q)
      IO.println s!"shared-pair-vs-built={ptrEq sh.1 sh.2}"
      let a := mkPair pr.1 pr.2
      let b := mkPair pr.1 pr.2
      let sh2 := ShareCommon.shareCommon (a, b)
      IO.println s!"shared-built-vs-built={ptrEq sh2.1 sh2.2}"
  | .error e => IO.println s!"recv error: {e}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. Run: ./prog

Expected behavior:

equal=true
shared-pair-vs-built=true
shared-built-vs-built=true

Actual behavior:

equal=true
shared-pair-vs-built=false
shared-built-vs-built=true
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 receive started by recv is answered by the captureless C++ closure it installs (udp.cpp:201-234); for the 13-byte datagram it runs with nread = 13 and takes its nread >= 0 branch. The promise returned to the program is resolved (lean_promise_resolve, udp.cpp:230) with an Except.ok whose single field is a two-field constructor object carrying ctor tag 1, while ByteArray × Option SocketAddress is Prod, whose only constructor Prod.mk has tag 0. No callee hands it the wrong value: libuv delivers the 13-byte datagram, fit_read_buffer returns the 13-byte ByteArray, lean_sockaddr_to_socketaddress returns a well-formed SocketAddress.v4 (tag 0), and lean_alloc_sarray/mk_option_some/mk_except_ok/mk_mt_promise behave correctly; the tag 1 comes from the literal lean_alloc_ctor(1, 2, 0) at udp.cpp:226.

Suggested fix: allocate the pair with tag 0, lean_alloc_ctor(0, 2, 0), as src/runtime/io.cpp:819, src/runtime/uv/dns.cpp:149 and src/runtime/uv/net_addr.cpp:118 do for every other pair the runtime builds. With tag 0 the two Lean-equal pairs are hash-consed into one object and the program prints shared-pair-vs-built=true.

Impact

Every UDP recv returns a pair with the wrong constructor tag. It is visible only to tag-sensitive runtime code such as ShareCommon, where equal pairs are not merged.

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
18h 27m
PR unite (30g)
390

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.