Skip to content

Commit 65e08f7

Browse files
committed
Add wasm32-wasip2 definitions necessary for std::net support
1 parent cdf6896 commit 65e08f7

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed

libc-test/build.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,7 @@ fn test_dragonflybsd(target: &str) {
14551455

14561456
fn test_wasi(target: &str) {
14571457
assert!(target.contains("wasi"));
1458+
let p2 = target.contains("wasip2");
14581459

14591460
let mut cfg = ctest_cfg();
14601461
cfg.define("_GNU_SOURCE", None);
@@ -1468,6 +1469,9 @@ fn test_wasi(target: &str) {
14681469
"limits.h",
14691470
"locale.h",
14701471
"malloc.h",
1472+
[p2]: "netdb.h",
1473+
[p2]: "netinet/in.h",
1474+
[p2]: "netinet/tcp.h",
14711475
"poll.h",
14721476
"sched.h",
14731477
"stdbool.h",
@@ -1499,6 +1503,12 @@ fn test_wasi(target: &str) {
14991503
// to omit them.
15001504
cfg.cfg("libc_ctest", None);
15011505

1506+
// `ctest2` has a hard-coded list of default cfgs which doesn't include
1507+
// wasip2, which is why it has to be set here manually.
1508+
if p2 {
1509+
cfg.cfg("target_env", Some("p2"));
1510+
}
1511+
15021512
cfg.type_name(move |ty, is_struct, is_union| match ty {
15031513
"FILE" | "fd_set" | "DIR" => ty.to_string(),
15041514
t if is_union => format!("union {}", t),
@@ -1521,6 +1531,14 @@ fn test_wasi(target: &str) {
15211531
// used here to generate a pointer to them in bindings so skip these tests.
15221532
cfg.skip_static(|c| c.starts_with("_CLOCK_"));
15231533

1534+
cfg.skip_const(|c| match c {
1535+
// These constants aren't yet defined in wasi-libc.
1536+
// Exposing them is being tracked by https://github.com/WebAssembly/wasi-libc/issues/531.
1537+
"SO_BROADCAST" | "SO_LINGER" => true,
1538+
1539+
_ => false,
1540+
});
1541+
15241542
cfg.skip_fn(|f| match f {
15251543
// This function doesn't actually exist in libc's header files
15261544
"__errno_location" => true,

libc-test/semver/wasi-p2.txt

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
sa_family_t
2+
in_port_t
3+
in_addr_t
4+
socklen_t
5+
sockaddr
6+
in_addr
7+
sockaddr_in
8+
in6_addr
9+
sockaddr_in6
10+
sockaddr_storage
11+
addrinfo
12+
ip_mreq
13+
ipv6_mreq
14+
SHUT_RD
15+
SHUT_WR
16+
SHUT_RDWR
17+
MSG_NOSIGNAL
18+
MSG_PEEK
19+
SO_REUSEADDR
20+
SO_ERROR
21+
SO_BROADCAST
22+
SO_LINGER
23+
SO_RCVTIMEO
24+
SO_SNDTIMEO
25+
SOCK_DGRAM
26+
SOCK_STREAM
27+
SOL_SOCKET
28+
AF_INET
29+
AF_INET6
30+
IPPROTO_IP
31+
IPPROTO_TCP
32+
IPPROTO_IPV6
33+
IP_TTL
34+
IP_MULTICAST_TTL
35+
IP_MULTICAST_LOOP
36+
IP_ADD_MEMBERSHIP
37+
IP_DROP_MEMBERSHIP
38+
IPV6_MULTICAST_LOOP
39+
IPV6_JOIN_GROUP
40+
IPV6_LEAVE_GROUP
41+
IPV6_V6ONLY
42+
IPV6_ADD_MEMBERSHIP
43+
IPV6_DROP_MEMBERSHIP
44+
TCP_NODELAY
45+
EAI_SYSTEM
46+
socket
47+
connect
48+
bind
49+
listen
50+
accept
51+
getsockname
52+
getpeername
53+
sendto
54+
recvfrom
55+
getsockopt
56+
setsockopt
57+
getaddrinfo
58+
freeaddrinfo
59+
gai_strerror

src/wasi.rs renamed to src/wasi/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -882,3 +882,10 @@ extern "C" {
882882

883883
pub fn __errno_location() -> *mut ::c_int;
884884
}
885+
886+
cfg_if! {
887+
if #[cfg(target_env = "p2")] {
888+
mod p2;
889+
pub use self::p2::*;
890+
}
891+
}

src/wasi/p2.rs

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
pub type sa_family_t = ::c_ushort;
2+
pub type in_port_t = ::c_ushort;
3+
pub type in_addr_t = ::c_uint;
4+
5+
pub type socklen_t = ::c_uint;
6+
7+
s! {
8+
#[repr(align(16))]
9+
pub struct sockaddr {
10+
pub sa_family: sa_family_t,
11+
pub sa_data: [::c_char; 0],
12+
}
13+
14+
pub struct in_addr {
15+
pub s_addr: in_addr_t,
16+
}
17+
18+
#[repr(align(16))]
19+
pub struct sockaddr_in {
20+
pub sin_family: sa_family_t,
21+
pub sin_port: in_port_t,
22+
pub sin_addr: in_addr,
23+
}
24+
25+
#[repr(align(4))]
26+
pub struct in6_addr {
27+
pub s6_addr: [::c_uchar; 16],
28+
}
29+
30+
#[repr(align(16))]
31+
pub struct sockaddr_in6 {
32+
pub sin6_family: sa_family_t,
33+
pub sin6_port: in_port_t,
34+
pub sin6_flowinfo: ::c_uint,
35+
pub sin6_addr: in6_addr,
36+
pub sin6_scope_id: ::c_uint,
37+
}
38+
39+
#[repr(align(16))]
40+
pub struct sockaddr_storage {
41+
pub ss_family: sa_family_t,
42+
pub __ss_data: [::c_char; 32],
43+
}
44+
45+
pub struct addrinfo {
46+
pub ai_flags: ::c_int,
47+
pub ai_family: ::c_int,
48+
pub ai_socktype: ::c_int,
49+
pub ai_protocol: ::c_int,
50+
pub ai_addrlen: socklen_t,
51+
pub ai_addr: *mut sockaddr,
52+
pub ai_canonname: *mut ::c_char,
53+
pub ai_next: *mut addrinfo,
54+
}
55+
56+
pub struct ip_mreq {
57+
pub imr_multiaddr: in_addr,
58+
pub imr_interface: in_addr,
59+
}
60+
61+
pub struct ipv6_mreq {
62+
pub ipv6mr_multiaddr: in6_addr,
63+
pub ipv6mr_interface: ::c_uint,
64+
}
65+
}
66+
67+
pub const SHUT_RD: ::c_int = 1 << 0;
68+
pub const SHUT_WR: ::c_int = 1 << 1;
69+
pub const SHUT_RDWR: ::c_int = SHUT_RD | SHUT_WR;
70+
71+
pub const MSG_NOSIGNAL: ::c_int = 0x4000;
72+
pub const MSG_PEEK: ::c_int = 0x0002;
73+
74+
pub const SO_REUSEADDR: ::c_int = 2;
75+
pub const SO_ERROR: ::c_int = 4;
76+
pub const SO_BROADCAST: ::c_int = 6;
77+
pub const SO_LINGER: ::c_int = 13;
78+
pub const SO_RCVTIMEO: ::c_int = 66;
79+
pub const SO_SNDTIMEO: ::c_int = 67;
80+
81+
pub const SOCK_DGRAM: ::c_int = 5;
82+
pub const SOCK_STREAM: ::c_int = 6;
83+
84+
pub const SOL_SOCKET: ::c_int = 0x7fffffff;
85+
86+
pub const AF_INET: ::c_int = 1;
87+
pub const AF_INET6: ::c_int = 2;
88+
89+
pub const IPPROTO_IP: ::c_int = 0;
90+
pub const IPPROTO_TCP: ::c_int = 6;
91+
pub const IPPROTO_IPV6: ::c_int = 41;
92+
93+
pub const IP_TTL: ::c_int = 2;
94+
pub const IP_MULTICAST_TTL: ::c_int = 33;
95+
pub const IP_MULTICAST_LOOP: ::c_int = 34;
96+
pub const IP_ADD_MEMBERSHIP: ::c_int = 35;
97+
pub const IP_DROP_MEMBERSHIP: ::c_int = 36;
98+
99+
pub const IPV6_MULTICAST_LOOP: ::c_int = 19;
100+
pub const IPV6_JOIN_GROUP: ::c_int = 20;
101+
pub const IPV6_LEAVE_GROUP: ::c_int = 21;
102+
pub const IPV6_V6ONLY: ::c_int = 26;
103+
104+
pub const IPV6_ADD_MEMBERSHIP: ::c_int = IPV6_JOIN_GROUP;
105+
pub const IPV6_DROP_MEMBERSHIP: ::c_int = IPV6_LEAVE_GROUP;
106+
107+
pub const TCP_NODELAY: ::c_int = 1;
108+
109+
pub const EAI_SYSTEM: ::c_int = -11;
110+
111+
#[cfg_attr(
112+
feature = "rustc-dep-of-std",
113+
link(
114+
name = "c",
115+
kind = "static",
116+
modifiers = "-bundle",
117+
cfg(target_feature = "crt-static")
118+
)
119+
)]
120+
#[cfg_attr(
121+
feature = "rustc-dep-of-std",
122+
link(name = "c", cfg(not(target_feature = "crt-static")))
123+
)]
124+
extern "C" {
125+
pub fn socket(domain: ::c_int, type_: ::c_int, protocol: ::c_int) -> ::c_int;
126+
pub fn connect(fd: ::c_int, name: *const sockaddr, addrlen: socklen_t) -> ::c_int;
127+
pub fn bind(socket: ::c_int, addr: *const sockaddr, addrlen: socklen_t) -> ::c_int;
128+
pub fn listen(socket: ::c_int, backlog: ::c_int) -> ::c_int;
129+
pub fn accept(socket: ::c_int, addr: *mut sockaddr, addrlen: *mut socklen_t) -> ::c_int;
130+
131+
pub fn getsockname(socket: ::c_int, addr: *mut sockaddr, addrlen: *mut socklen_t) -> ::c_int;
132+
pub fn getpeername(socket: ::c_int, addr: *mut sockaddr, addrlen: *mut socklen_t) -> ::c_int;
133+
134+
pub fn sendto(
135+
socket: ::c_int,
136+
buffer: *const ::c_void,
137+
length: ::size_t,
138+
flags: ::c_int,
139+
addr: *const sockaddr,
140+
addrlen: socklen_t,
141+
) -> ::ssize_t;
142+
pub fn recvfrom(
143+
socket: ::c_int,
144+
buffer: *mut ::c_void,
145+
length: ::size_t,
146+
flags: ::c_int,
147+
addr: *mut sockaddr,
148+
addrlen: *mut socklen_t,
149+
) -> ::ssize_t;
150+
151+
pub fn getsockopt(
152+
sockfd: ::c_int,
153+
level: ::c_int,
154+
optname: ::c_int,
155+
optval: *mut ::c_void,
156+
optlen: *mut socklen_t,
157+
) -> ::c_int;
158+
pub fn setsockopt(
159+
sockfd: ::c_int,
160+
level: ::c_int,
161+
optname: ::c_int,
162+
optval: *const ::c_void,
163+
optlen: socklen_t,
164+
) -> ::c_int;
165+
166+
pub fn getaddrinfo(
167+
host: *const ::c_char,
168+
serv: *const ::c_char,
169+
hint: *const addrinfo,
170+
res: *mut *mut addrinfo,
171+
) -> ::c_int;
172+
pub fn freeaddrinfo(p: *mut addrinfo);
173+
pub fn gai_strerror(ecode: ::c_int) -> *const ::c_char;
174+
}

0 commit comments

Comments
 (0)