From 30b2ff9dd23926a28c880ebededb6b6250f3d3e2 Mon Sep 17 00:00:00 2001 From: Arshia Ghafoori Date: Wed, 13 Sep 2023 18:43:00 +0400 Subject: [PATCH] Fix port endianness + make sure alignment of __wasi_addr_port_t is 2 --- libc-bottom-half/cloudlibc/src/common/net.h | 13 +++-- .../headers/public/wasi/api_wasix.h | 49 ++++++++++++------- tools/wasi-headers/src/c_header.rs | 2 +- tools/wasix-headers/WASI | 2 +- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/common/net.h b/libc-bottom-half/cloudlibc/src/common/net.h index 0a845122b..b60651c73 100644 --- a/libc-bottom-half/cloudlibc/src/common/net.h +++ b/libc-bottom-half/cloudlibc/src/common/net.h @@ -47,6 +47,7 @@ static inline int is_wasi_port_ok() { /// Converts a WASI address into a socket address static inline int wasi_to_sockaddr(const struct __wasi_addr_port_t *restrict peer_addr, struct sockaddr *restrict addr, socklen_t *restrict addrlen) { + // This test needs to be here because some older versions of wasmer report the port as big-endian static int tested = 0; static int need_revert = 1; if(!tested && addr) { @@ -65,8 +66,8 @@ static inline int wasi_to_sockaddr(const struct __wasi_addr_port_t *restrict pee } else if (peer_addr->tag == __WASI_ADDRESS_FAMILY_INET6) { struct sockaddr_in6 addr6; addr6.sin6_family = AF_INET6; - addr6.sin6_flowinfo = need_revert?htonl(peer_addr->u.inet6.addr.flow_info):peer_addr->u.inet6.addr.flow_info; - addr6.sin6_scope_id = need_revert?htonl(peer_addr->u.inet6.addr.scope_id):peer_addr->u.inet6.addr.scope_id;; + addr6.sin6_flowinfo = peer_addr->u.inet6.addr.flow_info1 << 16 & peer_addr->u.inet6.addr.flow_info0; + addr6.sin6_scope_id = peer_addr->u.inet6.addr.scope_id1 << 16 & peer_addr->u.inet6.addr.scope_id0;; addr6.sin6_port = need_revert?htons(peer_addr->u.inet6.port):peer_addr->u.inet6.port; memcpy(&addr6.sin6_addr.s6_addr, &peer_addr->u.inet6.addr, sizeof(struct in6_addr)); memcpy(addr, &addr6, MIN(sizeof(struct sockaddr_in6), *addrlen)); @@ -97,9 +98,11 @@ static inline int sockaddr_to_wasi(const struct sockaddr *restrict addr, const s } else if (addr->sa_family == AF_INET6 && addrlen >= sizeof(struct sockaddr_in6)) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr; peer_addr->tag = __WASI_ADDRESS_FAMILY_INET6; - peer_addr->u.inet6.port = ntohs(addr6->sin6_port); - peer_addr->u.inet6.addr.flow_info = ntohl(addr6->sin6_flowinfo); - peer_addr->u.inet6.addr.scope_id = ntohl(addr6->sin6_scope_id); + peer_addr->u.inet6.port = addr6->sin6_port; + peer_addr->u.inet6.addr.flow_info1 = addr6->sin6_flowinfo >> 16; + peer_addr->u.inet6.addr.flow_info0 = addr6->sin6_flowinfo & 0xffff; + peer_addr->u.inet6.addr.scope_id1 = addr6->sin6_scope_id >> 16; + peer_addr->u.inet6.addr.scope_id0 = addr6->sin6_scope_id & 0xffff; memcpy(&peer_addr->u.inet6.addr, &addr6->sin6_addr.s6_addr, sizeof(struct in6_addr)); return 0; } else if (addr->sa_family == AF_UNIX && addrlen >= offsetof(struct sockaddr_un, sun_path) + 1) { diff --git a/libc-bottom-half/headers/public/wasi/api_wasix.h b/libc-bottom-half/headers/public/wasi/api_wasix.h index 2bd0b0d22..d9339c114 100644 --- a/libc-bottom-half/headers/public/wasi/api_wasix.h +++ b/libc-bottom-half/headers/public/wasi/api_wasix.h @@ -2681,14 +2681,27 @@ typedef struct __wasi_addr_ip6_t { uint16_t h3; - uint32_t flow_info; + /** + * flow_info1 contains the most significant two bytes, and comes first in keeping with all wasix syscalls being little endian + */ + uint16_t flow_info1; + + /** + * flow_info0 contains the least significant two bytes + */ + uint16_t flow_info0; + + /** + * Same as flow_info1 and flow_info0 + */ + uint16_t scope_id1; - uint32_t scope_id; + uint16_t scope_id0; } __wasi_addr_ip6_t; _Static_assert(sizeof(__wasi_addr_ip6_t) == 24, "witx calculated size"); -_Static_assert(_Alignof(__wasi_addr_ip6_t) == 4, "witx calculated align"); +_Static_assert(_Alignof(__wasi_addr_ip6_t) == 2, "witx calculated align"); _Static_assert(offsetof(__wasi_addr_ip6_t, n0) == 0, "witx calculated offset"); _Static_assert(offsetof(__wasi_addr_ip6_t, n1) == 2, "witx calculated offset"); _Static_assert(offsetof(__wasi_addr_ip6_t, n2) == 4, "witx calculated offset"); @@ -2697,8 +2710,10 @@ _Static_assert(offsetof(__wasi_addr_ip6_t, h0) == 8, "witx calculated offset"); _Static_assert(offsetof(__wasi_addr_ip6_t, h1) == 10, "witx calculated offset"); _Static_assert(offsetof(__wasi_addr_ip6_t, h2) == 12, "witx calculated offset"); _Static_assert(offsetof(__wasi_addr_ip6_t, h3) == 14, "witx calculated offset"); -_Static_assert(offsetof(__wasi_addr_ip6_t, flow_info) == 16, "witx calculated offset"); -_Static_assert(offsetof(__wasi_addr_ip6_t, scope_id) == 20, "witx calculated offset"); +_Static_assert(offsetof(__wasi_addr_ip6_t, flow_info1) == 16, "witx calculated offset"); +_Static_assert(offsetof(__wasi_addr_ip6_t, flow_info0) == 18, "witx calculated offset"); +_Static_assert(offsetof(__wasi_addr_ip6_t, scope_id1) == 20, "witx calculated offset"); +_Static_assert(offsetof(__wasi_addr_ip6_t, scope_id0) == 22, "witx calculated offset"); /** * Unix socket that is bound to no more than 107 bytes @@ -3055,10 +3070,10 @@ typedef struct __wasi_addr_ip6_port_t { } __wasi_addr_ip6_port_t; -_Static_assert(sizeof(__wasi_addr_ip6_port_t) == 28, "witx calculated size"); -_Static_assert(_Alignof(__wasi_addr_ip6_port_t) == 4, "witx calculated align"); +_Static_assert(sizeof(__wasi_addr_ip6_port_t) == 26, "witx calculated size"); +_Static_assert(_Alignof(__wasi_addr_ip6_port_t) == 2, "witx calculated align"); _Static_assert(offsetof(__wasi_addr_ip6_port_t, port) == 0, "witx calculated offset"); -_Static_assert(offsetof(__wasi_addr_ip6_port_t, addr) == 4, "witx calculated offset"); +_Static_assert(offsetof(__wasi_addr_ip6_port_t, addr) == 2, "witx calculated offset"); /** * An IPv6 address CIDR @@ -3070,8 +3085,8 @@ typedef struct __wasi_addr_ip6_cidr_t { } __wasi_addr_ip6_cidr_t; -_Static_assert(sizeof(__wasi_addr_ip6_cidr_t) == 28, "witx calculated size"); -_Static_assert(_Alignof(__wasi_addr_ip6_cidr_t) == 4, "witx calculated align"); +_Static_assert(sizeof(__wasi_addr_ip6_cidr_t) == 26, "witx calculated size"); +_Static_assert(_Alignof(__wasi_addr_ip6_cidr_t) == 2, "witx calculated align"); _Static_assert(offsetof(__wasi_addr_ip6_cidr_t, addr) == 0, "witx calculated offset"); _Static_assert(offsetof(__wasi_addr_ip6_cidr_t, prefix) == 24, "witx calculated offset"); @@ -3089,8 +3104,8 @@ typedef struct __wasi_addr_t { __wasi_addr_u_t u; } __wasi_addr_t; -_Static_assert(sizeof(__wasi_addr_t) == 112, "witx calculated size"); -_Static_assert(_Alignof(__wasi_addr_t) == 4, "witx calculated align"); +_Static_assert(sizeof(__wasi_addr_t) == 110, "witx calculated size"); +_Static_assert(_Alignof(__wasi_addr_t) == 2, "witx calculated align"); /** * Union that makes a generic IP address and port @@ -3106,8 +3121,8 @@ typedef struct __wasi_addr_port_t { __wasi_addr_port_u_t u; } __wasi_addr_port_t; -_Static_assert(sizeof(__wasi_addr_port_t) == 112, "witx calculated size"); -_Static_assert(_Alignof(__wasi_addr_port_t) == 4, "witx calculated align"); +_Static_assert(sizeof(__wasi_addr_port_t) == 110, "witx calculated size"); +_Static_assert(_Alignof(__wasi_addr_port_t) == 2, "witx calculated align"); /** * Union that makes a generic IP address and prefix. a.k.a. CIDR @@ -3123,8 +3138,8 @@ typedef struct __wasi_addr_cidr_t { __wasi_addr_cidr_u_t u; } __wasi_addr_cidr_t; -_Static_assert(sizeof(__wasi_addr_cidr_t) == 32, "witx calculated size"); -_Static_assert(_Alignof(__wasi_addr_cidr_t) == 4, "witx calculated align"); +_Static_assert(sizeof(__wasi_addr_cidr_t) == 28, "witx calculated size"); +_Static_assert(_Alignof(__wasi_addr_cidr_t) == 2, "witx calculated align"); typedef struct __wasi_route_t { __wasi_addr_cidr_t cidr; @@ -3140,7 +3155,7 @@ typedef struct __wasi_route_t { _Static_assert(sizeof(__wasi_route_t) == 176, "witx calculated size"); _Static_assert(_Alignof(__wasi_route_t) == 8, "witx calculated align"); _Static_assert(offsetof(__wasi_route_t, cidr) == 0, "witx calculated offset"); -_Static_assert(offsetof(__wasi_route_t, via_router) == 32, "witx calculated offset"); +_Static_assert(offsetof(__wasi_route_t, via_router) == 28, "witx calculated offset"); _Static_assert(offsetof(__wasi_route_t, preferred_until) == 144, "witx calculated offset"); _Static_assert(offsetof(__wasi_route_t, expires_at) == 160, "witx calculated offset"); diff --git a/tools/wasi-headers/src/c_header.rs b/tools/wasi-headers/src/c_header.rs index 424557147..15b64ee6e 100644 --- a/tools/wasi-headers/src/c_header.rs +++ b/tools/wasi-headers/src/c_header.rs @@ -871,4 +871,4 @@ fn wasm_type(wasm: &WasmType) -> &'static str { WasmType::F32 => "float", WasmType::F64 => "double", } -} \ No newline at end of file +} diff --git a/tools/wasix-headers/WASI b/tools/wasix-headers/WASI index 3480e1790..1f54b5bf8 160000 --- a/tools/wasix-headers/WASI +++ b/tools/wasix-headers/WASI @@ -1 +1 @@ -Subproject commit 3480e17908ca064b74145c795010f332e1dd1491 +Subproject commit 1f54b5bf8f0b033ce345fb201fe4a134be4d7e5f