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
use *;
use *;
use *;
use *;
use *;
use *;
use *;
use crateMarshalable;
use crateEndPoint;
use crateNetcReceiveResult;
use crateRSL*;
verus!
// #![allow(unused_imports)]
// #![allow(unused_attributes)]
// use builtin::*;
// use builtin_macros::*;
// use vstd::prelude::*;
// pub mod common;
// pub mod implementation;
// pub mod protocol;
// pub mod services;
// // mod temp_verify;
// pub mod main;
// // pub mod test;
// pub mod verus_extra;
// use crate::common::native::io_s::EndPoint;
// use crate::common::native::io_s::NetcReceiveResult;
// use crate::services::lock::main_i::*;
// verus! {
// // The function `unflatten_args` takes arguments passed to us by the C# main
// // executable and unflattens then into a vector of arguments. C# flattens
// // the arguments by contatenating them all together, and passing us an array
// // of their lengths.
// #[verifier(external)]
// #[verus::line_count::ignore]
// pub unsafe fn unflatten_args (
// num_args: i32,
// arg_lengths: *const i32,
// _total_arg_length: i32,
// flattened_args: *const u8
// ) -> Vec<Vec<u8>>
// {
// let mut offset: isize = 0;
// let mut args: Vec<Vec<u8>> = Vec::new();
// for i in 0..num_args as isize {
// let arg_length = *arg_lengths.offset(i as isize);
// let arg_array: &[u8] = std::slice::from_raw_parts(flattened_args.offset(offset), arg_length as usize);
// let arg_vec: std::vec::Vec<u8> = arg_array.to_vec();
// let mut arg: Vec<u8> = Vec::new();
// arg = arg_vec;
// args.push(arg);
// offset += arg_length as isize;
// }
// args
// }
// #[verifier(external)]
// #[verus::line_count::ignore]
// pub unsafe fn lock_main_placeholder_to_test_netclient(
// nc: &mut crate::common::native::io_s::NetClient,
// args: &Vec<Vec<u8>>
// )
// {
// for i in 0..args.len()
// {
// println!("Command-line argument #{}: {:#?}", i+1, args[i]);
// }
// let my_end_point: EndPoint = nc.get_my_end_point();
// println!("My end point: {:#?}", my_end_point.id);
// println!("Current time is {}", nc.get_time());
// let mut message: Vec<u8> = Vec::new();
// message = "Hello, world!".as_bytes().to_vec();
// let _ = nc.send(&my_end_point, &message);
// match nc.receive(0) {
// NetcReceiveResult::Received{sender, message} => {
// println!("Received message {:#?}", message);
// },
// NetcReceiveResult::TimedOut{} => {
// println!("Timed out");
// }
// NetcReceiveResult::Error{} => {
// println!("Error");
// }
// }
// std::thread::sleep(std::time::Duration::from_millis(1000));
// match nc.receive(0) {
// NetcReceiveResult::Received{sender, message} => {
// println!("Received message {:#?}", message);
// },
// NetcReceiveResult::TimedOut{} => {
// println!("Timed out");
// }
// NetcReceiveResult::Error{} => {
// println!("Error");
// }
// }
// }
// // This routine is exported to the C# main executable containing the I/O
// // framework. This lets the I/O framework allocate Rust buffers that it can fill
// // and return to us.
// //
// // For instance, suppose the I/O framework is about to receive a packet, and has
// // learned that packet's length. It will call `allocate_buffer`, and we'll
// // return to it two things: `buffer_ptr`, a pointer to a region of memory with
// // length `length`, and `box_vec_ptr`, a pointer that it will return to us when
// // we ask to receive a message.
// #[verifier(external)]
// #[no_mangle]
// #[verus::line_count::ignore]
// pub unsafe extern "C" fn allocate_buffer(
// length: u64,
// box_vec_ptr: *mut *mut std::vec::Vec<u8>,
// buffer_ptr: *mut *mut u8
// )
// {
// // Allocate a std::vec::Vec<u8> with the given length.
// let mut v: std::vec::Vec<u8> = std::vec::Vec::<u8>::with_capacity(length as usize);
// v.set_len(length as usize);
// // Box the vector.
// let mut b: Box<std::vec::Vec<u8>> = Box::<std::vec::Vec<u8>>::new(v);
// // Return the raw pointer to the vector's buffer as `*buffer_ptr`.
// *buffer_ptr = (*b).as_mut_ptr();
// // Return the raw pointer to the Box as `*box_vec_ptr`.
// *box_vec_ptr = Box::<std::vec::Vec<u8>>::into_raw(b);
// }
// // This routine is exported to the C# main executable containing the I/O
// // framework. This lets the I/O framework deallocate a Rust buffers that
// // it allocated with `allocate_buffer` that it was planning to return to
// // us but has now decided it doesn't want to return to us. For instance,
// // if the I/O framework allocated it to store an incoming packet, but
// // detected that the connection closed, it needs to free the buffer.
// #[verifier(external)]
// #[verus::line_count::ignore]
// #[no_mangle]
// pub unsafe extern "C" fn free_buffer(
// box_vec_ptr: *mut std::vec::Vec<u8>
// )
// {
// // Convert back from a raw pointer to a Box so that when the Box
// // goes out of scope at the end of this function, it will be
// // freed.
// let _box_vec: Box<std::vec::Vec<u8>> = Box::<std::vec::Vec<u8>>::from_raw(box_vec_ptr);
// }
// #[verifier(external)]
// #[verus::line_count::ignore]
// #[no_mangle]
// pub unsafe extern "C" fn lock_main_wrapper(
// num_args: i32,
// arg_lengths: *const i32,
// total_arg_length: i32,
// flattened_args: *const u8,
// get_my_end_point_func: extern "C" fn(*mut *mut std::vec::Vec<u8>),
// get_time_func: extern "C" fn() -> u64,
// receive_func: extern "C" fn(i32, *mut bool, *mut bool, *mut *mut std::vec::Vec<u8>, *mut *mut std::vec::Vec<u8>),
// send_func: extern "C" fn(u64, *const u8, u64, *const u8) -> bool
// ) -> i32 {
// let args: Vec<Vec<u8>> = unflatten_args(num_args, arg_lengths, total_arg_length, flattened_args);
// let mut my_end_point_vec_ptr = std::mem::MaybeUninit::<*mut std::vec::Vec<u8>>::uninit();
// get_my_end_point_func(my_end_point_vec_ptr.as_mut_ptr());
// let my_end_point_ptr: *mut std::vec::Vec<u8> = my_end_point_vec_ptr.assume_init();
// let my_end_point_box: Box<std::vec::Vec<u8>> = Box::<std::vec::Vec<u8>>::from_raw(my_end_point_ptr);
// let my_end_point_vec: std::vec::Vec<u8> = *my_end_point_box;
// let mut my_end_point: Vec<u8> = Vec::new();
// my_end_point = my_end_point_vec;
// let mut nc = crate::common::native::io_s::NetClient::new(EndPoint{id: my_end_point}, get_time_func, receive_func, send_func);
// match lock_main(nc, args) {
// Ok(_) => 0,
// Err(_) => 1,
// }
// }
// }