1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Lunatic VM host functions.

pub mod error {
    #[link(wasm_import_module = "lunatic::error")]
    extern "C" {
        pub fn string_size(error_id: u64) -> u32;
        pub fn to_string(error_id: u64, error_str: *mut u8);
        pub fn drop(error_id: u64);
    }
}

pub mod message {
    #[link(wasm_import_module = "lunatic::message")]
    extern "C" {
        pub fn create_data(tag: i64, capacity: u64);
        pub fn write_data(data: *const u8, data_len: usize) -> usize;
        pub fn read_data(data: *mut u8, data_len: usize) -> usize;
        #[allow(dead_code)]
        pub fn seek_data(position: u64);
        pub fn get_tag() -> i64;
        pub fn get_process_id() -> u64;
        #[allow(dead_code)]
        pub fn data_size() -> u64;
        pub fn push_module(module_id: u64) -> u64;
        pub fn take_module(index: u64) -> u64;
        pub fn push_tcp_stream(tcp_stream_id: u64) -> u64;
        pub fn take_tcp_stream(index: u64) -> u64;
        pub fn push_tls_stream(tls_stream_id: u64) -> u64;
        pub fn take_tls_stream(index: u64) -> u64;
        pub fn send(process_id: u64) -> u32;
        pub fn send_receive_skip_search(process_id: u64, wait_on_tag: i64, timeout: u64) -> u32;
        pub fn receive(tag: *const i64, tag_len: usize, timeout: u64) -> u32;
    }
}

pub mod timer {
    #[link(wasm_import_module = "lunatic::timer")]
    extern "C" {
        pub fn send_after(process_id: u64, duration: u64) -> u64;
        pub fn cancel_timer(timer_id: u64) -> u32;
    }
}

pub mod networking {
    #[link(wasm_import_module = "lunatic::networking")]
    extern "C" {
        pub fn resolve(name_str: *const u8, name_str_len: usize, timeout: u64, id: *mut u64)
            -> u32;
        pub fn drop_dns_iterator(dns_iter_id: u64);
        pub fn resolve_next(
            dns_iter_id: u64,
            addr_type: *mut u32,
            addr: *mut u8,
            port: *mut u16,
            flow_info: *mut u32,
            scope_id: *mut u32,
        ) -> u32;
        pub fn tcp_bind(
            addr_type: u32,
            addr: *const u8,
            port: u32,
            flow_info: u32,
            scope_id: u32,
            id: *mut u64,
        ) -> u32;
        pub fn udp_bind(
            addr_type: u32,
            addr: *const u8,
            port: u32,
            flow_info: u32,
            scope_id: u32,
            id: *mut u64,
        ) -> u32;
        pub fn drop_tcp_listener(tcp_listener_id: u64);
        pub fn drop_tls_listener(tcp_listener_id: u64);
        pub fn drop_udp_socket(udp_socket_id: u64);
        pub fn tcp_local_addr(tcp_listener_id: u64, addr_dns_iter: *mut u64) -> u32;
        pub fn tls_local_addr(tcp_listener_id: u64, addr_dns_iter: *mut u64) -> u32;
        pub fn udp_local_addr(udp_socket_id: u64, addr_dns_iter: *mut u64) -> u32;
        pub fn tcp_peer_addr(tcp_stream_id: u64, addr_dns_iter: *mut u64) -> u32;
        pub fn udp_peer_addr(udp_stream_id: u64, addr_dns_iter: *mut u64) -> u32;
        pub fn tcp_accept(listener_id: u64, id: *mut u64, peer_dns_iter: *mut u64) -> u32;
        pub fn tcp_connect(
            addr_type: u32,
            addr: *const u8,
            port: u32,
            flow_info: u32,
            scope_id: u32,
            timeout: u64,
            id: *mut u64,
        ) -> u32;
        pub fn udp_connect(
            udp_socket_id: u64,
            addr_type: u32,
            addr: *const u8,
            port: u32,
            flow_info: u32,
            scope_id: u32,
            timeout: u64,
            id: *mut u64,
        ) -> u32;
        pub fn drop_tcp_stream(tcp_stream_id: u64);
        pub fn clone_tcp_stream(tcp_stream_id: u64) -> u64;
        pub fn tcp_write_vectored(
            tcp_stream_id: u64,
            ciovec_array: *const u32,
            ciovec_array_len: usize,
            opaque: *mut u64,
        ) -> u32;
        pub fn tcp_read(
            tcp_stream_id: u64,
            buffer: *mut u8,
            buffer_len: usize,

            opaque: *mut u64,
        ) -> u32;
        pub fn tcp_peek(
            tcp_stream_id: u64,
            buffer: *mut u8,
            buffer_len: usize,

            opaque: *mut u64,
        ) -> u32;
        pub fn udp_send(
            udp_socket_id: u64,
            buffer: *const u8,
            buffer_len: usize,

            opaque: *mut u64,
        ) -> u32;
        pub fn udp_send_to(
            udp_socket_id: u64,
            buffer: *const u8,
            buffer_len: usize,
            addr_type: u32,
            addr: *const u8,
            port: u32,
            flow_info: u32,
            scope_id: u32,
            opaque: *mut u64,
        ) -> u32;
        pub fn udp_receive(
            udp_socket_id: u64,
            buffer: *mut u8,
            buffer_len: usize,
            opaque: *mut u64,
        ) -> u32;
        pub fn udp_receive_from(
            udp_socket_id: u64,
            buffer: *mut u8,
            buffer_len: usize,
            opaque: *mut u64,
            dns_iter_ptr: *mut u64,
        ) -> u32;
        pub fn set_udp_socket_ttl(udp_socket_id: u64, ttl: u32);
        pub fn set_udp_socket_broadcast(udp_socket_id: u64, broadcast: u32);
        pub fn get_udp_socket_ttl(udp_socket_id: u64) -> u32;
        pub fn get_udp_socket_broadcast(udp_socket_id: u64) -> i32;
        pub fn clone_udp_socket(udp_socket_id: u64) -> u64;
        pub fn tcp_flush(tcp_stream_id: u64, error_id: *mut u64) -> u32;
        pub fn tls_flush(tcp_stream_id: u64, error_id: *mut u64) -> u32;
        pub fn set_read_timeout(tcp_stream_id: u64, duration: u64);
        pub fn get_read_timeout(tcp_stream_id: u64) -> u64;
        pub fn set_write_timeout(tcp_stream_id: u64, duration: u64);
        pub fn get_write_timeout(tcp_stream_id: u64) -> u64;
        pub fn set_peek_timeout(tcp_stream_id: u64, duration: u64);
        pub fn get_peek_timeout(tcp_stream_id: u64) -> u64;

        // tls
        pub fn tls_bind(
            addr_type: u32,
            addr: *const u8,
            port: u32,
            flow_info: u32,
            scope_id: u32,
            id: *mut u64,
            certs_array_ptr: *const u32,
            certs_array_len: usize,
            keys_array_ptr: *const u32,
            keys_array_len: usize,
        ) -> u32;
        pub fn tls_accept(listener_id: u64, id: *mut u64, peer_dns_iter: *mut u64) -> u32;
        pub fn tls_connect(
            addr_str: *const u8,
            addr_str_len: u32,
            port: u32,
            timeout: u64,
            id: *mut u64,
            certs_ptr: *const u8,
            certs_len: u32,
        ) -> u32;
        pub fn drop_tls_stream(tcp_stream_id: u64);
        pub fn clone_tls_stream(tcp_stream_id: u64) -> u64;
        pub fn tls_write_vectored(
            tcp_stream_id: u64,
            ciovec_array: *const u32,
            ciovec_array_len: usize,
            opaque: *mut u64,
        ) -> u32;
        pub fn tls_read(
            tcp_stream_id: u64,
            buffer: *mut u8,
            buffer_len: usize,
            opaque: *mut u64,
        ) -> u32;
        pub fn set_tls_read_timeout(tls_stream_id: u64, duration: u64);
        pub fn get_tls_read_timeout(tls_stream_id: u64) -> u64;
        pub fn set_tls_write_timeout(tls_stream_id: u64, duration: u64);
        pub fn get_tls_write_timeout(tls_stream_id: u64) -> u64;
    }
}

pub mod trap {
    #[link(wasm_import_module = "lunatic::trap")]
    extern "C" {
        pub fn catch(function: usize, argument: usize) -> usize;
    }
}

pub mod process {
    #[link(wasm_import_module = "lunatic::process")]
    extern "C" {
        pub fn compile_module(data: *const u8, data_len: usize, id: *mut u64) -> i32;
        pub fn drop_module(config_id: u64);
        pub fn create_config() -> i64;
        pub fn drop_config(config_id: u64);
        pub fn config_set_max_memory(config_id: u64, max_memory: u64);
        pub fn config_get_max_memory(config_id: u64) -> u64;
        pub fn config_set_max_fuel(config_id: u64, max_fuel: u64);
        pub fn config_get_max_fuel(config_id: u64) -> u64;
        pub fn config_can_compile_modules(config_id: u64) -> u32;
        pub fn config_set_can_compile_modules(config_id: u64, can: u32);
        pub fn config_can_create_configs(config_id: u64) -> u32;
        pub fn config_set_can_create_configs(config_id: u64, can: u32);
        pub fn config_can_spawn_processes(config_id: u64) -> u32;
        pub fn config_set_can_spawn_processes(config_id: u64, can: u32);
        pub fn spawn(
            link: i64,
            config_id: i64,
            module_id: i64,
            function: *const u8,
            function_len: usize,
            params: *const u8,
            params_len: usize,
            id: *mut u64,
        ) -> u32;
        pub fn get_or_spawn(
            name: *const u8,
            name_len: usize,
            link: i64,
            config_id: i64,
            module_id: i64,
            function: *const u8,
            function_len: usize,
            params: *const u8,
            params_len: usize,
            node_id: *mut u64,
            id: *mut u64,
        ) -> u32;
        pub fn sleep_ms(millis: u64);
        pub fn die_when_link_dies(trap: u32);
        pub fn process_id() -> u64;
        pub fn link(tag: i64, process_id: u64);
        pub fn unlink(process_id: u64);
        pub fn monitor(process_id: u64);
        pub fn stop_monitoring(process_id: u64);
        pub fn kill(process_id: u64);
        pub fn exists(process_id: u64) -> i32;
    }
}

pub mod registry {
    #[link(wasm_import_module = "lunatic::registry")]
    extern "C" {
        pub fn put(name: *const u8, name_len: usize, node_id: u64, process_id: u64);
        pub fn get(
            name: *const u8,
            name_len: usize,
            node_id: *mut u64,
            process_id: *mut u64,
        ) -> u32;
        pub fn remove(name: *const u8, name_len: usize);
    }
}

pub mod wasi {
    #[link(wasm_import_module = "lunatic::wasi")]
    extern "C" {
        pub fn config_add_environment_variable(
            config_id: u64,
            key: *const u8,
            key_len: usize,
            value: *const u8,
            value_len: usize,
        );
        pub fn config_add_command_line_argument(config_id: u64, key: *const u8, key_len: usize);
        pub fn config_preopen_dir(config_id: u64, key: *const u8, key_len: usize);
    }
}

#[allow(clashing_extern_declarations)]
pub mod distributed {
    #[link(wasm_import_module = "lunatic::distributed")]
    extern "C" {
        pub fn get_nodes(nodes_ptr: *mut u64, nodes_len: u32) -> u32;
        pub fn exec_lookup_nodes(
            query_ptr: *const u8,
            query_len: u32,
            query_id_ptr: *mut u64,
            node_len_ptr: *mut u32,
            error_ptr: *mut u64,
        ) -> u32;
        pub fn copy_lookup_nodes_results(
            query_id: u64,
            nodes_ptr: *mut u64,
            nodes_len: u32,
            error_ptr: *mut u64,
        ) -> i32;
        pub fn nodes_count() -> u32;
        pub fn node_id() -> u64;
        pub fn module_id() -> u64;
        pub fn send(node_id: u64, process_id: u64) -> u32;
        pub fn send_receive_skip_search(
            node_id: u64,
            process_id: u64,
            wait_on_tag: i64,
            timeout: u64,
        ) -> u32;
        pub fn spawn(
            node_id: u64,
            config_id: i64,
            module_id: u64,
            function: *const u8,
            function_len: usize,
            params: *const u8,
            params_len: usize,
            id: *mut u64,
        ) -> u32;
    }
}

pub mod version {
    #[link(wasm_import_module = "lunatic::version")]
    extern "C" {
        pub fn major() -> u32;
        pub fn minor() -> u32;
        pub fn patch() -> u32;
    }
}

pub mod metrics {
    #[link(wasm_import_module = "lunatic::metrics")]
    extern "C" {
        pub fn counter(name: *const u8, name_len: usize, value: u64);
        pub fn increment_counter(name: *const u8, name_len: usize);
        pub fn gauge(name: *const u8, name_len: usize, value: f64);
        pub fn increment_gauge(name: *const u8, name_len: usize, value: f64);
        pub fn decrement_gauge(name: *const u8, name_len: usize, value: f64);
        pub fn histogram(name: *const u8, name_len: usize, value: f64);
    }
}