tla-rs 0.1.0

Rust implementation of the IronFleet verified distributed systems framework
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#![allow(unused_imports)]
use crate::common::collections::comparable::*;
use crate::common::framework::{args_t::clone_vec_u8, environment_s::*};
use crate::implementation::common::marshalling::*;
use builtin::*;
use builtin_macros::*;
use std::collections::HashMap;
use std::net::UdpSocket;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use vstd::slice::*;
use vstd::{modes::*, prelude::*, seq::*, *};
use vstd::std_specs::hash::*;

verus! {
    pub struct AbstractEndPoint {
        pub id: Seq<u8>,
    }

    impl AbstractEndPoint {
        pub open spec fn valid_physical_address(self) -> bool {
            self.id.len() < 0x100000
        }

        pub open spec fn abstractable(self) -> bool {
            self.valid_physical_address()
        }
    }

    define_struct_and_derive_marshalable!{
        #[derive(Eq, Hash, Debug)]
        pub struct EndPoint{
            pub id: Vec::<u8>,
        }
    }

    impl Clone for EndPoint{

        fn clone(&self) -> Self {
            EndPoint{
                id: self.id.clone(),
            }
        }
    }

    impl View for EndPoint {
        type V = AbstractEndPoint;

        open spec fn view(&self) -> AbstractEndPoint {
            AbstractEndPoint{
                id: self.id@,
            }
        }
    }

    impl PartialEq for EndPoint{
        #[verifier(external_body)]
        fn eq(&self, other: &Self) -> bool {
            self.id.len() == other.id.len() && self.id.iter().zip(&other.id).all(|(a, b)| a == b)
        }
    }

    // impl Comparable for EndPoint {

    //     // #[verifier(external_body)]
    //     exec fn equals(&self, other: &Self) -> (result: bool)
    //         // ensures
    //         //     result == (*self == *other)
    //     {
    //         if self.id.len() != other.id.len() {
    //             return false;
    //         }

    //         let mut i: usize = 0;
    //         while i < self.id.len()
    //             invariant
    //                 i <= self.id.len(),
    //                 forall |j: int| 0 <= j < i ==> self.id[j] == other.id[j],
    //                 self.id.len() == other.id.len()
    //         {
    //             if self.id[i] != other.id[i] {
    //                 return false;
    //             }
    //             i += 1;
    //         }
    //         proof {
    //             assert(self.id@ == other.id@);
    //             assert(forall |i: int| 0 <= i < self.id.len() ==> self.id@[i] == other.id@[i]);
    //             assert(forall |i: int| 0 <= i < self.id.len() ==> self.id[i] == other.id[i]);
    //             assert(*self == *other);
    //         }
    //         true
    //     }
    // }

    impl EndPoint {
        // Verus unimpl: Can't call clone through the trait
        pub fn clone_up_to_view(&self) -> (res: EndPoint)
            ensures res@ == self@
        {
            EndPoint{id: clone_vec_u8(&self.id)}
        }

        // pub open spec fn view(self) -> AbstractEndPoint {
        //     AbstractEndPoint{id: self.id@}
        // }

        #[verifier(inline)]
        pub open spec fn abstractable(self) -> bool {
            self@.valid_physical_address()
        }

        pub open spec fn valid_public_key(&self) -> bool {
            self@.valid_physical_address()
        }

        // Translates Common/Native/Io.s.dfy
        pub fn valid_physical_address(&self) -> (out: bool)
        ensures
            out == self@.valid_physical_address(),
        {
            self.id.len() < 0x100000
        }
    }

    #[verifier(broadcast_forall)]
    #[verifier(external_body)]
    pub proof fn axiom_endpoint_view()
        ensures forall |e1:EndPoint, e2:EndPoint| e1@ == e2@ ==> e1 == e2
    {

    }

    #[verifier(broadcast_forall)]
    #[verifier(external_body)]
    pub proof fn axiom_endpoint_key_model()
        ensures #[trigger] obeys_key_model::<EndPoint>()
    {
    }

    pub open spec fn abstractify_end_points(end_points: Vec<EndPoint>) -> Seq<AbstractEndPoint>
    {
        end_points@.map(|i, end_point: EndPoint| end_point@)
    }

    pub type NetPacket = LPacket<AbstractEndPoint, Seq<u8>>;
    pub type NetEvent = LIoOp<AbstractEndPoint, Seq<u8>>;
    pub type History = Seq<NetEvent>;

    pub enum State {
        Receiving,
        Sending,
        Error,
    }

    pub enum NetcReceiveResult {    // Not to be confused with Ironfleet's ReceiveResult type, which contains a parsed message
        Received { sender: EndPoint, message: Vec<u8> },
        TimedOut,
        Error,
    }

    pub struct IronfleetIOError {
        pub message: String,
    }

    pub closed spec fn from_trusted_code() -> bool { true }

    #[verifier(external_body)]
    pub struct NetClientCPointers {
        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
    }

    #[verifier::external_body]
    pub struct DuctTapeProfiler {
        last_event: SystemTime,
        last_report: SystemTime,
        event_counter: HashMap<std::string::String, u64>,
    }

    impl DuctTapeProfiler {
        #[verifier(external)]
        fn new() -> Self {
            println!("Report-ready");
            DuctTapeProfiler {
                last_event: SystemTime::now(),
                last_report: SystemTime::now(),
                event_counter: HashMap::new(),
            }
        }

        #[verifier(external)]
        fn duration_as_ns(duration: &Duration) -> u64
        {
            duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64
        }

        #[verifier(external)]
        fn mark_duration(&mut self, label: &str) {
            let now = SystemTime::now();
            let duration_ns = Self::duration_as_ns(&now.duration_since(self.last_event).expect("arrow of time"));
            self.increment_event(label, duration_ns);
            self.last_event = now;
            self.maybe_report(&now);
        }

        #[verifier(external)]
        fn record_event(&mut self, label: &str) {
            self.increment_event(label, 1);
        }

        #[verifier(external)]
        fn increment_event(&mut self, label: &str, incr: u64) {
            if let Some(entry) = self.event_counter.get_mut(label) {
                *entry += incr;
            } else {
                self.event_counter.insert(label.to_string(), incr);
            }
        }

        #[verifier(external)]
        fn maybe_report(&mut self, now: &SystemTime)
        {
            let report_period = 1 * 1_000_000_000;
            let report_duration_ns = Self::duration_as_ns(&now.duration_since(self.last_report).expect("arrow of time"));
            if report_duration_ns > report_period {
                self.increment_event("report-duration-ns", report_duration_ns);
                self.report();
                self.last_report = now.clone();
                self.event_counter = HashMap::new();
            }
        }

        #[verifier(external)]
        fn report(&self)
        {
            for (key, value) in &self.event_counter {
                if key.ends_with("-ns") {
                    let ms = *value as f64 / 1e6;
                    println!("{key}: {ms} ms");
                } else {
                    println!("{key}: {value} count");
                }
            }
            println!("");
        }
    }

    pub open spec fn MaxPacketSize() -> int { 0xFFFF_FFFF_FFFF_FFFF }

    pub open spec fn ValidPhysicalPacket(p:LPacket<AbstractEndPoint, Seq<u8>>) -> bool
    {
      &&& p.src.valid_physical_address()
      &&& p.dst.valid_physical_address()
      &&& p.msg.len() <= MaxPacketSize()
    }

    pub open spec fn ValidPhysicalIo(io:LIoOp<AbstractEndPoint, Seq<u8>>) -> bool
    {
      &&& (io is Receive ==> ValidPhysicalPacket(io->r))
      &&& (io is Send ==> ValidPhysicalPacket(io->s))
    }

    pub struct NetClient {
        pub state: Ghost<State>,
        pub history: Ghost<History>,
        pub end_point: EndPoint,
        pub c_pointers: NetClientCPointers,
        pub profiler: DuctTapeProfiler,
    }

    impl NetClient {
        //////////////////////////////////////////////////////////////////////////////
        // player-1 accessible interfaces (note requires from_trusted_code())
        //////////////////////////////////////////////////////////////////////////////

        #[verifier(external)]
        pub fn new(
            end_point: EndPoint,
            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
        ) -> (net_client: Self)
            requires from_trusted_code(),
            ensures
                net_client.state() is Receiving,
                net_client.history() == Seq::<NetEvent>::empty(),
                net_client.my_end_point() == end_point,
        {
            NetClient{
                state: Ghost(State::Receiving),
                history: Ghost(seq![]),
                end_point,
                c_pointers: NetClientCPointers{get_time_func: get_time_func, receive_func: receive_func, send_func: send_func},
                profiler: DuctTapeProfiler::new(),
            }
        }

        // Main loop (Player 1 audited code) resets the state after having seen Player 2
        // complete a proof of refinement to an atomic protocol step.
        pub fn reset(&mut self)
            requires
                from_trusted_code()
            ensures
                self.state() is Receiving,
                self.my_end_point() == old(self).my_end_point()
        {
            self.state = Ghost(State::Receiving);
        }

        //////////////////////////////////////////////////////////////////////////////
        // player-2 accessible interfaces
        //////////////////////////////////////////////////////////////////////////////

        // This state field is how Player 2 proves that it calls receive before send.
        pub closed spec fn state(&self) -> State
        {
            self.state@
        }

        /// Translates calls to env.ok.ok().
        pub open spec fn ok(&self) -> bool
        {
            !(self.state() is Error)
        }

        /// translates NetClient.NetClientIsValid
        pub open spec fn valid(&self) -> bool
        {
            &&& self.ok()
            &&& self.my_end_point().abstractable()
        }

        pub closed spec fn history(&self) -> History
        {
            self.history@
        }

        /// Translates MyPublicKey()
        pub closed spec fn my_end_point(&self) -> AbstractEndPoint
        {
            self.end_point@
        }

        pub fn get_my_end_point(&self) -> (ep: EndPoint)
            ensures
                ep@ == self.my_end_point()
        {
            self.end_point.clone_up_to_view()
        }

        #[verifier(external)]
        pub fn get_time_internal(&self) -> (time: u64)
            requires
                from_trusted_code()
        {
            (self.c_pointers.get_time_func)()
        }

        #[verifier(external_body)]
        pub fn get_time(&mut self) -> (time: u64)
            requires
                  old(self).state() is Receiving
            ensures ({
                &&& self.state() is Sending
                &&& self.history() == old(self).history() + seq![LIoOp::ReadClock{t: time as int}]
            })
        {
            let time: u64 = self.get_time_internal();
            self.state = Ghost(State::Sending);
            self.history = Ghost(self.history@ + seq![LIoOp::<AbstractEndPoint, Seq<u8>>::ReadClock{t: time as int}]);
            time
        }

        #[verifier(external)]
        pub unsafe fn receive_internal(&mut self, time_limit_s: i32) -> (result: NetcReceiveResult)
        {
            let mut ok: bool = true;
            let mut timed_out: bool = true;
            let mut remote = std::mem::MaybeUninit::<*mut std::vec::Vec<u8>>::uninit();
            let mut buffer = std::mem::MaybeUninit::<*mut std::vec::Vec<u8>>::uninit();

            // self.profiler.mark_duration("processing-ns");
            (self.c_pointers.receive_func)(time_limit_s, &mut ok, &mut timed_out, remote.as_mut_ptr(), buffer.as_mut_ptr());
            // self.profiler.mark_duration("awaiting-receive-ns");

            if ok {
                if timed_out {
                    self.profiler.record_event("receive-timedout");
                    NetcReceiveResult::TimedOut{}
                }
                else {
                    self.profiler.record_event("receive-ok");
                    let remote_ptr: *mut std::vec::Vec<u8> = remote.assume_init();
                    let buffer_ptr: *mut std::vec::Vec<u8> = buffer.assume_init();
                    let remote_box: Box<std::vec::Vec<u8>> = Box::<std::vec::Vec<u8>>::from_raw(remote_ptr);
                    let buffer_box: Box<std::vec::Vec<u8>> = Box::<std::vec::Vec<u8>>::from_raw(buffer_ptr);
                    let remote_vec: std::vec::Vec<u8> = *remote_box;
                    let buffer_vec: std::vec::Vec<u8> = *buffer_box;
                    let mut remote_verus_vec: Vec<u8> = Vec::new();
                    remote_verus_vec = remote_vec;
                    let mut buffer_verus_vec: Vec<u8> = Vec::new();
                    buffer_verus_vec = buffer_vec;
                    NetcReceiveResult::Received{sender: EndPoint{id: remote_verus_vec}, message: buffer_verus_vec}
                }
            }
            else {
                self.profiler.record_event("receive-error");
                NetcReceiveResult::Error{}
            }
        }

        #[verifier(external_body)]
        pub fn receive(&mut self, time_limit_s: i32) -> (result: NetcReceiveResult)
            requires
              old(self).state() is Receiving
            ensures
              self.my_end_point() == old(self).my_end_point(),
              match result {
                NetcReceiveResult::Received{sender, message} => {
                    &&& self.state() is Receiving
                    &&& sender.abstractable()
                    &&& self.history() == old(self).history() + seq![
                        LIoOp::Receive{
                            r: LPacket{
                                dst: self.my_end_point(),
                                src: sender@,
                                msg: message@}
                        }]
                }
                NetcReceiveResult::TimedOut{} => {
                    &&& self.state() is Sending
                    &&& self.history() == old(self).history() + seq![LIoOp::TimeoutReceive{}]
                }
                NetcReceiveResult::Error{} => {
                    self.state() is Error
                }
            }
        {
            let result: NetcReceiveResult = unsafe { self.receive_internal(time_limit_s) };
            match result {
                NetcReceiveResult::Received{ref sender, ref message} => {
                    self.history = Ghost(self.history@ + seq![LIoOp::Receive{ r: LPacket::<AbstractEndPoint, Seq<u8>> { dst: self.my_end_point(), src: sender@, msg: message@ } } ]);
                }
                NetcReceiveResult::TimedOut{} => {
                    self.history = Ghost(self.history@ + seq![LIoOp::TimeoutReceive{}]);
                }
                NetcReceiveResult::Error{} => {
                    self.state = Ghost(State::Error{});
                }
            }
            result
        }

        #[verifier(external)]
        pub unsafe fn send_internal(&mut self, remote: &EndPoint, message: &Vec<u8>) -> (result: Result<(), IronfleetIOError>)
        {
            let remote_raw: *const u8 = remote.id.as_ptr();
            let message_raw: *const u8 = message.as_ptr();
            let b: bool = (self.c_pointers.send_func)(remote.id.len() as u64, remote_raw, message.len() as u64, message_raw);
            if b {
                Ok(())
            }
            else {
                Err(IronfleetIOError{message: "Failed to send".to_string()})
            }
        }

        #[verifier(external_body)]
        pub fn send_internal_wrapper(&mut self, remote: &EndPoint, message: &Vec<u8>) -> (result: Result<(), IronfleetIOError>)
        ensures
            *self == *old(self),
        {
            unsafe { self.send_internal(remote, message) }
        }

        pub fn send(&mut self, recipient: &EndPoint, message: &Vec<u8>) -> (result: Result<(), IronfleetIOError> )
            requires
                !(old(self).state() is Error)
            ensures
                self.my_end_point() == old(self).my_end_point(),
                self.state() is Error <==> result is Err,
                result is Ok ==> self.state() is Sending,
                result is Ok ==> self.history() == old(self).history() + seq![LIoOp::Send{s: LPacket{dst: recipient@, src: self.my_end_point(), msg: message@}}],
        {
            let result: Result<(), IronfleetIOError> = self.send_internal_wrapper(recipient, message);
            match result {
                Ok(_) => {
                    self.state = Ghost(State::Sending{});
                    self.history = Ghost(self.history@ + seq![LIoOp::Send{s: LPacket{dst: recipient@, src: self.my_end_point(), msg: message@}}]);
                }
                Err(_) => {
                    self.state = Ghost(State::Error{});
                }
            };
            result
        }
    }
} // verus!