Skip to content

Commit d9091cc

Browse files
authored
Fix mismatch of enum sizes between WASM and host (#4676)
- refactor(wasi_types): deprecate enums in platform_wasi_types.h. - Use macros instead. Signed-off-by: Krisztian Szilvasi <34309983+kr-t@users.noreply.github.com> Signed-off-by: Dan Kouba <dan@atym.io>
1 parent 163a91b commit d9091cc

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

core/shared/platform/include/platform_wasi_types.h

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -533,19 +533,14 @@ assert_wasi_layout(offsetof(__wasi_subscription_t, userdata) == 0, "witx calcula
533533
assert_wasi_layout(offsetof(__wasi_subscription_t, u) == 8, "witx calculated offset");
534534

535535
/* keep syncing with wasi_socket_ext.h */
536-
typedef enum {
537-
/* Used only for sock_addr_resolve hints */
538-
SOCKET_ANY = -1,
539-
SOCKET_DGRAM = 0,
540-
SOCKET_STREAM,
541-
} __wasi_sock_type_t;
542536

543537
typedef uint16_t __wasi_ip_port_t;
544538

545539
/* Ensure that __wasi_addr_type_t has a size of 4 byte (I32).
546540
However, it will not have the type safety of enum. */
547541
typedef uint32_t __wasi_addr_type_t;
548-
enum { IPv4 = 0, IPv6 };
542+
#define IPv4 (0)
543+
#define IPv6 (1)
549544

550545
/* n0.n1.n2.n3 */
551546
typedef struct __wasi_addr_ip4_t {
@@ -592,25 +587,45 @@ typedef struct __wasi_addr_t {
592587
} addr;
593588
} __wasi_addr_t;
594589

595-
typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t;
590+
/* Force 32-bit wire width for cross-boundary fields */
591+
typedef int32_t __wasi_sock_type_t;
592+
#define SOCKET_ANY (-1)
593+
#define SOCKET_DGRAM (0)
594+
#define SOCKET_STREAM (1)
595+
596+
typedef int32_t __wasi_address_family_t;
597+
#define INET4 (0)
598+
#define INET6 (1)
599+
#define INET_UNSPEC (2)
596600

597601
typedef struct __wasi_addr_info_t {
598-
__wasi_addr_t addr;
602+
__wasi_addr_t addr;
599603
__wasi_sock_type_t type;
600604
} __wasi_addr_info_t;
601605

602606
typedef struct __wasi_addr_info_hints_t {
603-
__wasi_sock_type_t type;
604-
__wasi_address_family_t family;
605-
// this is to workaround lack of optional parameters
606-
uint8_t hints_enabled;
607+
__wasi_sock_type_t type; // 4 bytes
608+
__wasi_address_family_t family; // 4 bytes
609+
uint8_t hints_enabled; // 1 byte
610+
uint8_t _pad[3]; // enforce layout
607611
} __wasi_addr_info_hints_t;
608612

613+
assert_wasi_layout(sizeof(__wasi_sock_type_t) == 4, "sock_type must be 4 bytes");
614+
assert_wasi_layout(sizeof(__wasi_address_family_t) == 4, "addr_family must be 4 bytes");
615+
616+
assert_wasi_layout(sizeof(__wasi_addr_info_hints_t) == 12, "hints_t must be 12 bytes");
617+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, type) == 0, "hints.type@0");
618+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, family) == 4, "hints.family@4");
619+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, hints_enabled) == 8, "hints.enabled@8");
620+
621+
assert_wasi_layout(offsetof(__wasi_addr_info_t, type) == sizeof(__wasi_addr_t),
622+
"addr_info.type follows addr");
623+
609624
#undef assert_wasi_layout
610625

611626
/* clang-format on */
612627
#ifdef __cplusplus
613628
}
614629
#endif
615630

616-
#endif /* end of _PLATFORM_WASI_TYPES_H */
631+
#endif /* end of _PLATFORM_WASI_TYPES_H */

0 commit comments

Comments
 (0)