Skip to content

Conversation

@dkouba-atym
Copy link
Contributor

While working with WAMR on Zephyr, I discovered that the sizes of a couple socket enums were not consistent between the host and inside of WASM. This resulted in an inability to create a UDP socket, as the layout of the enum was compressed (enums became 1 byte instead of 4) and the offset of hints_enabled was incorrect, and therefore all sockets created were TCP.

In WASM: sizes: enum=4, hints_t=12, off(type)=0 off(family)=4 off(en)=8
In Zephyr host: sizes: enum=1, hints_t=3, off(type)=0 off(family)=1 off(en)=2

As a result I've enforced the struct sizes using the same method it was done for a different enum and added the appropriate assertions to validate.

@lum1n0us
Copy link
Collaborator

I believe we should synchronize this modification with core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h.

@lum1n0us lum1n0us added the bug-fix Determine if this PR addresses a bug. It will be used by scripts to classify PRs. label Oct 28, 2025
typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t;
/* Force 32-bit wire width for cross-boundary fields */
typedef int32_t __wasi_sock_type_t;
enum { SOCKET_ANY = -1, SOCKET_DGRAM = 0, SOCKET_STREAM = 1 };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree it isn't appropriate to use enums to define an ABI.

it's better to retire all enums in this file.
ie.

#define SOCKET_DGRAM 0

it's even better to stop using too generic names.

typedef int32_t __wamr_socket_ext_sock_type_t;
#define WAMR_SOCKET_EXT_SOCK_TYPE_DGRAM 0

note that SOCKET_DGRAM is NOT wasi SOCK_DGRAM.

Copy link
Contributor

@srberard srberard Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yamt,
Are you suggesting that we replace all of the enums with #defines? For example:

enum { IPv4 = 0, IPv6 };

with this:

#define IPv4   0
#define IPv6   1

I'm happy to update the PR and make this change. The current PR continues to use enums but fixes the typedef to use fixed sizes (following the pattern used for __wasi_addr_type_t).

I also agree with changing things like SOCKET_DGRAM to something like WAMR_SOCKET_EXT_SOCK_TYPE_DGRAM as it is very confusing, particularly when having to debug. The only reservation I have is that will likely break existing code that uses the SOCKET_* values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix Determine if this PR addresses a bug. It will be used by scripts to classify PRs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants