Debian Codegraph @

crt/aws-c-io/docs/epoll_event_loop_proof.md

raw symbols SCIP
1 # VCC Proof Signoff
2
3 Verification tool: VCC (code-level proof)
4
5 Proofs: `tests/vcc/`
6
7 Implementation: Linux event loop (`source/linux/epoll_event_loop.c`)
8
9 Specification / Properties (`preamble.h`):
10 - *Memory safety*: the implementation only accesses valid memory.
11 - *Thread safety*: threads only update objects that they own.
12 - *Functional correctness*: sequential task and event callback execution in
13 the presence of multiple client threads. The proofs verify that:
14 - The scheduler for task execution is thread-local to the event loop
15 thread (so is sequential since no other threads read or write directly to
16 the scheduler). Tasks move from client threads to the event loop via
17 properly-synchronized ownership transfers, using locking.
18 - Subscribe/notify for event execution is properly-synchronized via epoll.
19
20 ## Assumptions
21
22 Generally, we assume well-behaved clients; the correctness of underlying
23 primitives (memory allocation, AWS C Common library, syscalls); and, minor
24 assumptions due to limitations in VCC. More precisely, the proofs assume:
25
26 - Well-behaved client: all client threads use the event loop API in a manner
27 that adheres to the specification. A badly-behaved client can invalidate
28 the proven properties. For example, a client that reads, writes, or frees a
29 task struct object that is scheduled on the event loop is racy and no
30 longer thread safe. The specification given in `preamble.h` forbids this
31 behavior (the ownership of the task struct changes as a result of the
32 schedule function) but we cannot, in general, enforce this behavior since
33 we do not verify client code.
34
35 - Thread safety of the allocator functions `aws_mem_{calloc,release}`. This is
36 important in the case where a client uses a custom allocator.
37
38 - Memory safety and function contracts for the following AWS C Common functions:
39
40 aws_atomic_compare_exchange_ptr
41 aws_atomic_{init, load, store}_{int, ptr}
42 aws_linked_list_{init, pop_front, swap_contents}
43 aws_mutex_{lock, unlock}
44 aws_raise_error
45 aws_task_init
46 aws_task_scheduler_schedule_{now, future}
47 aws_task_scheduler_{init, run_all, clean_up, cancel_tasks, has_tasks}
48 aws_thread_{clean_up, current_thread_id, decrement_unjoined_count, increment_unjoined_count, init, join, launch, thread_id_equal}
49
50 and similarly for the AWS C-IO functions:
51
52 aws_event_loop_{init_base, clean_up_base}
53 aws_open_nonblocking_posix_pipe
54
55 and similarly for the system calls:
56
57 close
58 epoll_{ctl, wait, create}
59 eventfd
60 read, write
61
62 The contracts are given in the `preamble.h` and proof files. The contracts
63 are assumed, not proven. The memory safety of the AWS C Common linked list
64 functions have been proven in CBMC.
65
66 - Thread safety of the epoll syscalls `epoll_{ctl, wait}`. We additionally
67 assume that the `ctl` (subscribe) and `wait` syscalls induce "happens
68 before" so that the litmus test (See Appendix) is data-race free and
69 therefore properly-synchronizes event subscribe/notify.
70
71 - Minor assumptions due to limitations of the VCC tool.
72
73 - In `s_is_on_callers_thread` we assume the loaded value from the atomic
74 var `running_thread_id` is thread-local and either `NULL` or the address
75 of the owner of the event loop. We cannot make this an object invariant
76 because the access is atomic. We manually validate that this assumption
77 is reasonable.
78
79 - In `s_run` we do not model the ownership transfer of the event loop from
80 the client thread to the freshly-launched event loop thread. We manually
81 validate that this assumption is reasonable.
82
83 - The Sequentially Consistent Data Race Free (SC-DRF) guarantee required by
84 the C11 standard: if a program is race-free and contains no non-SC atomic
85 operations, then it has only SC semantics [Note 12, N1570]. We rely on
86 SC-DRF to justify the use of VCC's SC memory model. We manually
87 validate that the event loop implementation contains no non-SC atomic
88 operations. Validation is required for pre-C11 compilers.
89
90 ## Simplifications
91
92 - Omit modeling of hash-table `local_data` in event loop.
93 - The log functions `AWS_LOGF_{...}` are no-ops (hash-defined out).
94 - Allocator functions are hash-defined to malloc/free.
95 - In `s_destroy`, we (re-)take the `epoll_loop` pointer after stop and wait
96 have been called. This has no semantic change to the program but is
97 necessary for the proof.
98 - Workarounds for VCC frontend (no semantic diff, but changes to syntax)
99
100 // Function pointer declarations
101 // For example, the following
102 typedef int(aws_io_clock_fn)(uint64_t *timestamp);
103 // is replaced with
104 typedef int(* aws_io_clock_fn_ptr)(uint64_t *timestamp);
105
106 // Array and struct literal initializers
107 // For example, the following
108 int pipe_fds[2] = {0};
109 // is replaced with
110 int pipe_fds[2]; pipe_fds[0] = 0; pipe_fds[1] = 0;
111
112 ## Trusted computing base
113
114 - Soundness of verification tools: VCC, Boogie, Z3
115 - C Compiler, because the verification is at the C code-level and the
116 properties proved may not be preserved by compilation.
117
118 ## References
119
120 [N1570] ISO/IEC. Programming languages – C. International standard 9899:201x,
121 2011
122
123 ## Appendix
124
125 Assumption on "happens before" induced by `epoll_{ctl/wait}`. Informally,
126 we need "message-passing" to hold so that the shared data passed from T1-to-T2
127 is guaranteed not-to-race.
128
129 // Initially *data == 0 (non-atomic location)
130 // T1
131 *data = 1;
132 epoll_ctl(...); // register event
133
134 // T2
135 if (1 == epoll_wait(...)) { // receive event
136 r0 = *data; // guaranteed that r0==1
137 }