Skip to content

Commit ce279ef

Browse files
dkouba-atymkr-t
authored andcommitted
Fix mismatch of enum sizes between WASM and host
Signed-off-by: Dan Kouba <dan@atym.io>
1 parent f76e664 commit ce279ef

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

core/shared/platform/include/platform_wasi_types.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,6 @@ assert_wasi_layout(offsetof(__wasi_subscription_t, userdata) == 0, "witx calcula
530530
assert_wasi_layout(offsetof(__wasi_subscription_t, u) == 8, "witx calculated offset");
531531

532532
/* keep syncing with wasi_socket_ext.h */
533-
typedef enum {
534-
/* Used only for sock_addr_resolve hints */
535-
SOCKET_ANY = -1,
536-
SOCKET_DGRAM = 0,
537-
SOCKET_STREAM,
538-
} __wasi_sock_type_t;
539533

540534
typedef uint16_t __wasi_ip_port_t;
541535

@@ -589,20 +583,36 @@ typedef struct __wasi_addr_t {
589583
} addr;
590584
} __wasi_addr_t;
591585

592-
typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t;
586+
/* Force 32-bit wire width for cross-boundary fields */
587+
typedef int32_t __wasi_sock_type_t;
588+
enum { SOCKET_ANY = -1, SOCKET_DGRAM = 0, SOCKET_STREAM = 1 };
589+
590+
typedef int32_t __wasi_address_family_t;
591+
enum { INET4 = 0, INET6 = 1, INET_UNSPEC = 2 };
593592

594593
typedef struct __wasi_addr_info_t {
595-
__wasi_addr_t addr;
594+
__wasi_addr_t addr;
596595
__wasi_sock_type_t type;
597596
} __wasi_addr_info_t;
598597

599598
typedef struct __wasi_addr_info_hints_t {
600-
__wasi_sock_type_t type;
601-
__wasi_address_family_t family;
602-
// this is to workaround lack of optional parameters
603-
uint8_t hints_enabled;
599+
__wasi_sock_type_t type; // 4 bytes
600+
__wasi_address_family_t family; // 4 bytes
601+
uint8_t hints_enabled; // 1 byte
602+
uint8_t _pad[3]; // enforce layout
604603
} __wasi_addr_info_hints_t;
605604

605+
assert_wasi_layout(sizeof(__wasi_sock_type_t) == 4, "sock_type must be 4 bytes");
606+
assert_wasi_layout(sizeof(__wasi_address_family_t) == 4, "addr_family must be 4 bytes");
607+
608+
assert_wasi_layout(sizeof(__wasi_addr_info_hints_t) == 12, "hints_t must be 12 bytes");
609+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, type) == 0, "hints.type@0");
610+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, family) == 4, "hints.family@4");
611+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, hints_enabled) == 8, "hints.enabled@8");
612+
613+
assert_wasi_layout(offsetof(__wasi_addr_info_t, type) == sizeof(__wasi_addr_t),
614+
"addr_info.type follows addr");
615+
606616
#undef assert_wasi_layout
607617

608618
/* clang-format on */

0 commit comments

Comments
 (0)