Repository navigation
Expand file tree
/
Copy pathRelationalSem.thy
More file actions
executable file
·283 lines (246 loc) · 12.4 KB
/
Copy pathRelationalSem.thy
File metadata and controls
executable file
·283 lines (246 loc) · 12.4 KB
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
section {* What can Happen during a Contract Invocation *}
text {* This section defines a set of sequence of account states that can appeear during an
invocation of a countract. The invocation can be a nested reentrancy, but we focus on one
invocation. This means we do not look into details of further reentrancy, but just assume
that the inner reentrancy keeps the invariant of the contract. Of course we need to prove
that the invariant holds for the code, but when we do that we can assume that the inner
nested calls keep the invariants (we can say we are doing mathematical induction on the depth
of reentrancy). *}
theory RelationalSem
imports Main "./Evm"
begin
subsection {* Some Possible Changes on Our Account State *}
text {* The account state might change even when the account's code is not executing. *}
text {* During blocks, the account might gain balances because somebody mines
Ether for the account. The balance might increase also while other contracts execute
because they might destroy themselves and send their balance to our account.
The following relation captures this possibility.
*}
text {* When a transaction finishes, if our contract is marked as killed, it is destroyed. *}
inductive account_state_natural_change :: "account_state \<Rightarrow> account_state \<Rightarrow> bool"
where
natural:
"old_bal \<le> new_bal \<Longrightarrow>
account_state_natural_change
\<lparr> account_address = addr
, account_storage = str
, account_code = code
, account_balance = old_bal
, account_ongoing_calls = going
, account_killed = killed
\<rparr>
\<lparr> account_address = addr
, account_storage = str
, account_code = code
, account_balance = new_bal
, account_ongoing_calls = going
, account_killed = killed
\<rparr>"
| cleaned: -- {* This happens only at the end of a transaction, but we don't know
the transaction boundaries.
So this can happen at any moment when there are no ongoing calls. *}
"account_state_natural_change
\<lparr> account_address = addr
, account_storage = str
, account_code = code
, account_balance = old_bal
, account_ongoing_calls = []
, account_killed = True
\<rparr>
(empty_account addr)"
declare account_state_natural_change.simps [simp]
text "When the execution comes back from an external call, the account state might have changed
arbitrariliy. Our strategy is to assume that an invariant is kept here; and later prove that
the invariant actually holds."
inductive account_state_return_change :: "(account_state \<Rightarrow> bool) \<Rightarrow> account_state \<Rightarrow> account_state \<Rightarrow> bool"
where
account_return:
"invariant
\<lparr> account_address = addr
, account_storage = new_str
, account_code = code
, account_balance = new_bal
, account_ongoing_calls = ongoing
, account_killed = killed
\<rparr>
\<Longrightarrow>
account_state_return_change invariant
\<lparr> account_address = addr
, account_storage = old_str
, account_code = code
, account_balance = old_bal
, account_ongoing_calls = ongoing
, account_killed = killed
\<rparr>
\<lparr> account_address = addr
, account_storage = new_str
, account_code = code
, account_balance = new_bal
, account_ongoing_calls = ongoing
, account_killed = killed
\<rparr>
"
declare account_state_return_change.simps [simp]
text "Next we specify which program results might see a return."
fun returnable_result :: "program_result \<Rightarrow> bool"
where
"returnable_result ProgramStepRunOut = False"
| "returnable_result (ProgramToWorld (ContractCall _) _ _ _) = True"
| "returnable_result (ProgramToWorld (ContractCreate _) _ _ _) = True"
| "returnable_result (ProgramToWorld ContractSuicide _ _ _) = False"
| "returnable_result (ProgramToWorld ContractFail _ _ _) = False"
(* because we are not modeling nested calls here, the effect of the nested calls are modeled in
* account_state_return_change *)
| "returnable_result (ProgramToWorld (ContractReturn _) _ _ _) = False"
| "returnable_result (ProgramInit _) = False"
| "returnable_result ProgramInvalid = False"
| "returnable_result ProgramAnnotationFailure = False"
subsection {* A Round of the Game *}
text {* Now we are ready to specify the worlds turn. *}
inductive world_turn :: "(account_state \<Rightarrow> bool) (* The invariant of our contract*)
\<Rightarrow> (account_state * program_result)
(* the account state before the world's move
and the last thing our account did *)
\<Rightarrow> (account_state * variable_env)
(* the account state after the world's move
and the variable environment from which our contract must start.
*)
\<Rightarrow> bool (* a boolean indicating if that is a possible world's move. *)"
where
world_call: -- {* the world might call our contract. *}
"
build_venv_called old_state callargs next_venv \<Longrightarrow>
world_turn I (old_state, ProgramInit callargs) (old_state, next_venv)"
| world_return: -- {* the world might return to our contract. *}
"account_state_return_change I account_state_going_out account_state_back \<Longrightarrow>
build_venv_returned account_state_back result new_v \<Longrightarrow>
returnable_result program_r \<Longrightarrow>
world_turn I (account_state_going_out, program_r)
(account_state_pop_ongoing_call account_state_back, new_v)"
| world_fail: -- {* the world might fail from an account into our contract. *}
"account_state_return_change I account_state_going_out account_state_back \<Longrightarrow>
build_venv_failed account_state_back = Some new_v \<Longrightarrow>
returnable_result result \<Longrightarrow>
world_turn I (account_state_going_out, result)
(account_state_pop_ongoing_call account_state_back, new_v)"
text {* As a reply, our contract might make a move, or report an annotation failure.*}
inductive contract_turn :: "(account_state * variable_env) \<Rightarrow> (account_state * program_result) \<Rightarrow> bool"
where
contract_to_world:
"build_cenv old_account = cenv \<Longrightarrow>
program_sem old_venv cenv
(program_length (cenv_program cenv)) steps
= ProgramToWorld act st bal opt_v \<Longrightarrow> (* if the program behaves like this *)
account_state_going_out (* and if the account state is updated like this*)
= update_account_state old_account act st bal opt_v \<Longrightarrow>
contract_turn (old_account, old_venv) (* the contract can change the account state like this. *)
(account_state_going_out, ProgramToWorld act st bal opt_v)"
| contract_annotation_failure:
"build_cenv old_account = cenv \<Longrightarrow>
program_sem old_venv cenv
(program_length (cenv_program cenv)) steps = ProgramAnnotationFailure \<Longrightarrow>
contract_turn (old_account, old_venv) (old_account, ProgramAnnotationFailure)"
text {* When we combine the world's turn and the contract's turn, we get one round.
The round is a binary relation over a single set.
*}
inductive one_round :: "(account_state \<Rightarrow> bool) \<Rightarrow> (account_state * program_result) \<Rightarrow> (account_state * program_result) \<Rightarrow> bool"
where
round:
"world_turn I a b \<Longrightarrow> contract_turn b c \<Longrightarrow> one_round I a c"
subsection {* Repetitions of rounds *}
text {* So, we can repeat rounds and see where they bring us. *}
text {* The definition is taken from the book Concrete Semantics, and then modified *}
inductive star :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
where
refl: "star r x x" |
step: "r x y \<Longrightarrow> star r y z \<Longrightarrow> star r x z"
text {* The repetition of rounds is either zero-times or once and the repetition. *}
lemma star_case :
"star r a c \<Longrightarrow>
(a = c \<or> (\<exists> b. r a b \<and> star r b c))"
apply(induction rule: star.induct; auto)
done
text {* The next lemma is purely for convenience.
Actually the rounds can go nowhere after this invocation fails.
*}
lemma no_entry_fail [dest!]:
"star (one_round I)
(a, ProgramToWorld ContractFail st bal v_opt)
(b, c) \<Longrightarrow> b = a \<and> c = ProgramToWorld ContractFail st bal v_opt"
apply(drule star_case; simp)
apply(simp add: one_round.simps add: world_turn.simps)
done
text {* Similarly, the rounds can go nowhere after this invocation returns. *}
lemma no_entry_return [dest!]:
"star (one_round I)
(a, ProgramToWorld (ContractReturn data) st bal v_opt)
(b, c) \<Longrightarrow> b = a \<and> c = ProgramToWorld (ContractReturn data) st bal v_opt"
apply(drule star_case; simp)
apply(simp add: one_round.simps add: world_turn.simps)
done
text {* Also similarly, the rounds can go nowhere after this invocation
causes our contract to destroy itself.
*}
lemma no_entry_suicide [dest!]:
"star (one_round I)
(a, ProgramToWorld ContractSuicide st bal v_opt)
(b, c) \<Longrightarrow> b = a \<and> c = ProgramToWorld ContractSuicide st bal v_opt"
apply(drule star_case; simp)
apply(simp add: one_round.simps add: world_turn.simps)
done
text {* And then the rounds can go nowhere after an annotation failure. *}
lemma no_entry_annotation_failure [dest!]:
"star (one_round I)
(a, ProgramAnnotationFailure)
(b, c) \<Longrightarrow> b = a \<and> c = ProgramAnnotationFailure"
apply(drule star_case; simp)
apply(simp add: one_round.simps add: world_turn.simps)
done
subsection {* How to State an Invariant *}
text {* For any invariant @{term I} over account states, now @{term "star (one_round I)"}
relation shows all possibilities during one invocation, assuming that the
invariant is kept during external calls\footnote{This assumption about deeper reentrancy should
be considered as an induction hypothesis. There has to be a lemma actually perform
such induction.}.
The following template traverses the states and proves that the invariant is actually kept
after every possible round. Also the template states that no annotation failures happen.
*}
definition no_assertion_failure_post :: "(account_state \<Rightarrow> bool) \<Rightarrow> (account_state \<times> program_result) \<Rightarrow> bool"
where
"no_assertion_failure_post I fin =
(I (fst fin) \<and>
snd fin \<noteq> ProgramAnnotationFailure)
"
definition no_assertion_failure :: "(account_state \<Rightarrow> bool) \<Rightarrow> bool"
where
"no_assertion_failure (I :: account_state \<Rightarrow> bool) ==
(\<forall> addr str code bal ongoing killed callenv.
I \<lparr> account_address = addr, account_storage = str, account_code = code,
account_balance = bal, account_ongoing_calls = ongoing, account_killed = killed \<rparr> \<longrightarrow>
(\<forall> fin. star (one_round I) (
\<lparr> account_address = addr, account_storage = str, account_code = code,
account_balance = bal, account_ongoing_calls = ongoing, account_killed = killed \<rparr>
, ProgramInit callenv) fin \<longrightarrow>
no_assertion_failure_post I fin
))"
subsection {* How to State a Pre-Post Condition Pair *}
text {* After proving a theorem of the above form, I might be interested in
a more specific case (if the caller is not this particular account, nothing should change).
For that purpose, here is another template statement. This contains everything above plus
an assumption about the initial call, and a conclusion about the state after the invocation.
*}
definition pre_post_conditions ::
"(account_state \<Rightarrow> bool) \<Rightarrow> (account_state \<Rightarrow> call_env \<Rightarrow> bool) \<Rightarrow>
(account_state \<Rightarrow> call_env \<Rightarrow> (account_state \<times> program_result) \<Rightarrow> bool) \<Rightarrow> bool"
where
"pre_post_conditions (I :: account_state \<Rightarrow> bool) (precondition :: account_state \<Rightarrow> call_env\<Rightarrow> bool)
(postcondition :: (account_state \<Rightarrow> call_env \<Rightarrow> (account_state \<times> program_result) \<Rightarrow> bool)) ==
(\<forall> initial_account initial_call. I initial_account \<longrightarrow>
precondition initial_account initial_call \<longrightarrow>
(\<forall> fin. star (one_round I) (initial_account, ProgramInit initial_call) fin \<longrightarrow>
snd fin \<noteq> ProgramAnnotationFailure \<and>
(\<forall> fin_observed. account_state_natural_change (fst fin) fin_observed \<longrightarrow>
I fin_observed \<and>
postcondition initial_account initial_call (fin_observed, snd fin))))
"
end