Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: lib: sockets: Heap mem pool for SOCKETPAIR is too small #87047

Open
IVandeVeire opened this issue Mar 13, 2025 · 1 comment
Open

net: lib: sockets: Heap mem pool for SOCKETPAIR is too small #87047

IVandeVeire opened this issue Mar 13, 2025 · 1 comment
Assignees
Labels
area: Networking bug The issue is a bug, or the PR is fixing a bug

Comments

@IVandeVeire
Copy link
Contributor

Describe the bug

The default value of HEAP_MEM_POOL_ADD_SIZE_SOCKETPAIR (255) seems to be insufficient to create a socket_pair.

To create a socket pair using z_impl_zsock_socketpair from socketpair.c, spair_new() is executed twice. spair_new() does a malloc() of sizeof(struct spair) (148) bytes. This results in a total of 296 bytes of heap usage.

Since the heap is only 256 bytes, this will fail.

Is this derised behaviour? Or should the default size be increased?
Also, sizeof(struct spair) depends on CONFIG_NET_SOCKETPAIR_BUFFER_SIZE. Should this somehow be taken into account?

To Reproduce
There is no direct way to reproduce using the zephyr tree. I stumbled apon the issue while developing a coap_server for native_sim. The issue was introduced in 671f0f6.

Impact

Coap server thread stops running, so my device is not reachable over coap.

Environment (please complete the following information):

  • OS: Linux
  • Commit SHA or Version used: main since 671f0f6
@IVandeVeire IVandeVeire added the bug The issue is a bug, or the PR is fixing a bug label Mar 13, 2025
@jukkar
Copy link
Member

jukkar commented Mar 14, 2025

The default value of HEAP_MEM_POOL_ADD_SIZE_SOCKETPAIR (255) seems to be insufficient to create a socket_pair.

To create a socket pair using z_impl_zsock_socketpair from socketpair.c, spair_new() is executed twice. spair_new() does a malloc() of sizeof(struct spair) (148) bytes. This results in a total of 296 bytes of heap usage.

If there is only one entity like socketpair wanting heap, then this is indeed problem. If there are other entities adding their requirements to the heap size, then the issue is probably not seen so often.

Since the heap is only 256 bytes, this will fail.

Is this derised behaviour? Or should the default size be increased? Also, sizeof(struct spair) depends on CONFIG_NET_SOCKETPAIR_BUFFER_SIZE. Should this somehow be taken into account?

Yes, we should allocate the enough space for the two spair struct.

Would you be able to send a fix for this, so increase the default value from 255 to what ever is needed?
Also it might make sense to add build asserts in the code so that if there is not enough space allocated, then the build will fail so we can detect this issue earlier. With the build assert, we do not need to specifically check the value of CONFIG_NET_SOCKETPAIR_BUFFER_SIZE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Networking bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

No branches or pull requests

4 participants