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

Open Beginner friendly
#16,820 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
backend

Research direction

Start with the receive callback in src/runtime/uv/udp.cpp, especially the lean_alloc_ctor call at line 226; compare it with the pair construction in src/runtime/uv/dns.cpp or src/runtime/io.cpp. Reproduce with the supplied Lean program and check that the received pair shares with an equal Lean-built pair after the tag is corrected.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.