lwIP sockets: a UDP send crashes the VM when pbuf_alloc fails instead of returning an error

未关闭 适合新手
#2,477 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, erlang

调研方向

阅读src/libAtomVM/otp_socket.c中OTP_SOCKET_LWIP分支的SocketStateUDP情况,位置在do_socket_send。检查附近的套接字错误如何返回,以及pbuf_alloc失败如何处理。为NULL pbuf添加失败路径,返回适当的套接字错误,然后验证UDP发送数据过大或内存不足时会返回错误,而不是导致VM故障。

由索引模型根据 Issue 内容生成。

描述

Affected: release-0.7 at ca1583068, lwIP socket backend (RP2 port).
Not changed by #2345.

What happens

do_socket_send builds the datagram with:

struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
uint8_t *bytes = (uint8_t *) p->payload;
memcpy(bytes, buf, len);

There is no NULL check. When lwIP cannot allocate the pbuf (payload larger than
the lwIP heap, or memory exhausted by a burst of datagrams), p is NULL and
the VM dereferences it: hard fault on the Pico W, instead of {error, Reason}.

How to reproduce (Pico W)

{ok, S} = socket:open(inet, dgram, udp),
Dest = #{family => inet, addr => {192, 168, 0, 1}, port => 5000},
socket:sendto(S, binary:copy(<<0>>, 60000), Dest).   % larger than MEM_SIZE

The board faults. A tight loop of 1400-byte datagrams hits the same path once
the pbuf memory is exhausted.

Where

src/libAtomVM/otp_socket.c, do_socket_send, #elif OTP_SOCKET_LWIP branch,
SocketStateUDP case.

Suggested fix

Return SocketOtherError with ERR_MEM (or SocketWouldBlock, so that
socket:sendto/3 waits and retries) when pbuf_alloc returns NULL.

主要语言
Erlang
星标
2.3k
派生
171
平均合并
4 天 11 小时
30 天内合并 PR
20

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

atomvm/AtomVM 的其他 Issue

查看 atomvm/AtomVM 的全部 Issue

相似的 Issue

更多 Embedded & IoT Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。