-
Notifications
You must be signed in to change notification settings - Fork 741
Fix mismatch of enum sizes between WASM and host #4676
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
base: main
Are you sure you want to change the base?
Fix mismatch of enum sizes between WASM and host #4676
Conversation
Signed-off-by: Dan Kouba <[email protected]>
|
I believe we should synchronize this modification with core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h. |
| 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 }; |
There was a problem hiding this comment.
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 0it'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 0note that SOCKET_DGRAM is NOT wasi SOCK_DGRAM.
There was a problem hiding this comment.
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 1I'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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe what @yamt means is to use #define instead of enum. Otherwise, there is still an implicit casting from enum to int32_t, which could be avoided.
I am okay with using more readable names (perhaps due to poor memory).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly.
sorry for late response.
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_enabledwas incorrect, and therefore all sockets created were TCP.In WASM:
sizes: enum=4, hints_t=12, off(type)=0 off(family)=4 off(en)=8In Zephyr host:
sizes: enum=1, hints_t=3, off(type)=0 off(family)=1 off(en)=2As 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.