-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_cmp_impl.v
225 lines (195 loc) · 7.55 KB
/
memory_cmp_impl.v
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
Require Import Arith.
Require Import Nat.
Require Import Bool.
Require Import bbv.Word.
Require Import Coq.NArith.NArith.
Require Import List.
Import ListNotations.
Require Import FORVES2.constants.
Import Constants.
Require Import FORVES2.program.
Import Program.
Require Import FORVES2.execution_state.
Import ExecutionState.
Require Import FORVES2.stack_operation_instructions.
Import StackOpInstrs.
Require Import FORVES2.misc.
Import Misc.
Require Import FORVES2.symbolic_state.
Import SymbolicState.
Require Import FORVES2.symbolic_state_eval.
Import SymbolicStateEval.
Require Import FORVES2.valid_symbolic_state.
Import ValidSymbolicState.
Require Import FORVES2.valid_symbolic_state.
Import ValidSymbolicState.
Require Import FORVES2.symbolic_state_cmp.
Import SymbolicStateCmp.
Require Import FORVES2.eval_common.
Import EvalCommon.
Require Import FORVES2.constraints.
Import Constraints.
Require Import FORVES2.context.
Import Context.
Module MemoryCmpImpl.
(* just handles the case of empty memory updates *)
Definition trivial_memory_cmp (sstack_val_cmp: sstack_val_cmp_t) (ctx: ctx_t) (smem1 smem2 :smemory) (maxidx1: nat) (sb1: sbindings) (maxidx2: nat) (sb2: sbindings) (ops: stack_op_instr_map) : bool :=
match smem1,smem2 with
| [], [] => true
| _, _ => false
end.
(* identical memory updates *)
Fixpoint basic_memory_cmp (sstack_val_cmp: sstack_val_cmp_t) (ctx: ctx_t) (smem1 smem2 :smemory) (maxidx1: nat) (sb1: sbindings) (maxidx2: nat) (sb2: sbindings) (ops: stack_op_instr_map) : bool :=
match smem1,smem2 with
| [], [] => true
| (U_MSTORE _ soffset1 svalue1)::sstrg1', (U_MSTORE _ soffset2 svalue2)::sstrg2' =>
if sstack_val_cmp ctx soffset1 soffset2 maxidx1 sb1 maxidx2 sb2 ops then
if sstack_val_cmp ctx svalue1 svalue2 maxidx1 sb1 maxidx2 sb2 ops then
basic_memory_cmp sstack_val_cmp ctx sstrg1' sstrg2' maxidx1 sb1 maxidx2 sb2 ops
else
false
else
false
| (U_MSTORE8 _ soffset1 svalue1)::sstrg1', (U_MSTORE8 _ soffset2 svalue2)::sstrg2' =>
if sstack_val_cmp ctx soffset1 soffset2 maxidx1 sb1 maxidx2 sb2 ops then
if sstack_val_cmp ctx svalue1 svalue2 maxidx1 sb1 maxidx2 sb2 ops then
basic_memory_cmp sstack_val_cmp ctx sstrg1' sstrg2' maxidx1 sb1 maxidx2 sb2 ops
else
false
else
false
| _, _ => false
end.
(* match follow_in_smap offset1 maxid sb, follow_in_smap offset2 maxid sb with
| Some (FollowSmapVal (SymBasicVal (Val v1)) _ _), Some (FollowSmapVal (SymBasicVal (Val v2)) _ _)=>
((wordToN v2)+31 <? (wordToN v1))%N
| _, _ => false
end *)
Definition swap_memory_update (ctx: ctx_t) (u1 u2 : memory_update sstack_val) (maxid: nat) (sb: sbindings) : bool :=
match u1, u2 with
| U_MSTORE _ offset1 _, U_MSTORE _ offset2 _ =>
match follow_in_smap offset1 maxid sb with
| Some (FollowSmapVal smv1 _ _) =>
match smv1 with
| SymBasicVal sv1 =>
match follow_in_smap offset2 maxid sb with
| Some (FollowSmapVal smv2 _ _) =>
match smv2 with
| SymBasicVal sv2 =>
match sv1, sv2 with
| Val v1, Val v2 => ((wordToN v2)+31 <? (wordToN v1))%N
| _, _ => chk_lt_lshift_wrt_ctx ctx sv2 sv1 31
end
| _ => false
end
| _ => false
end
| _ => false
end
| _ => false
end
| U_MSTORE8 _ offset1 _, U_MSTORE8 _ offset2 _ =>
match follow_in_smap offset1 maxid sb with
| Some (FollowSmapVal smv1 _ _) =>
match smv1 with
| SymBasicVal sv1 =>
match follow_in_smap offset2 maxid sb with
| Some (FollowSmapVal smv2 _ _) =>
match smv2 with
| SymBasicVal sv2 =>
match sv1, sv2 with
| Val v1, Val v2 => ((wordToN v2) <? (wordToN v1))%N
| _, _ => chk_lt_wrt_ctx ctx sv2 sv1
end
| _ => false
end
| _ => false
end
| _ => false
end
| _ => false
end
| U_MSTORE _ offset1 _, U_MSTORE8 _ offset2 _ =>
match follow_in_smap offset1 maxid sb with
| Some (FollowSmapVal smv1 _ _) =>
match smv1 with
| SymBasicVal sv1 =>
match follow_in_smap offset2 maxid sb with
| Some (FollowSmapVal smv2 _ _) =>
match smv2 with
| SymBasicVal sv2 =>
match sv1, sv2 with
| Val v1, Val v2 => ((wordToN v2) <? (wordToN v1))%N
| _, _ => chk_lt_wrt_ctx ctx sv2 sv1
end
| _ => false
end
| _ => false
end
| _ => false
end
| _ => false
end
| U_MSTORE8 _ offset1 _, U_MSTORE _ offset2 _ =>
match follow_in_smap offset1 maxid sb with
| Some (FollowSmapVal smv1 _ _) =>
match smv1 with
| SymBasicVal sv1 =>
match follow_in_smap offset2 maxid sb with
| Some (FollowSmapVal smv2 _ _) =>
match smv2 with
| SymBasicVal sv2 =>
match sv1, sv2 with
| Val v1, Val v2 => ((wordToN v2)+31 <? (wordToN v1))%N
| _, _ => chk_lt_lshift_wrt_ctx ctx sv2 sv1 31
end
| _ => false
end
| _ => false
end
| _ => false
end
| _ => false
end
end.
Fixpoint reorder_updates' (d : nat) (ctx: ctx_t) (smem :smemory) (maxidx: nat) (sb: sbindings) : bool * smemory :=
match d with
| O => (false,smem)
| S d' =>
match smem with
| u1::u2::smem' =>
if swap_memory_update ctx u1 u2 maxidx sb then
match reorder_updates' d' ctx (u1::smem') maxidx sb with
(_,smem'') => (true,u2::smem'')
end
else
match reorder_updates' d' ctx (u2::smem') maxidx sb with
(r,smem'') => (r,u1::smem'')
end
| _ => (false,smem)
end
end.
(* n is basically the length of smem, we pass it as a parameter to
avoid computing *)
Fixpoint reorder_memory_updates (d n: nat) (ctx: ctx_t) (smem :smemory) (maxidx: nat) (sb: sbindings) : smemory :=
match d with
| O => smem
| S d' =>
match reorder_updates' n ctx smem maxidx sb with
| (changed,smem') =>
if changed then
reorder_memory_updates d' n ctx smem' maxidx sb
else
smem'
end
end.
Definition po_memory_cmp (sstack_val_cmp : sstack_val_cmp_t) (ctx: ctx_t) (smem1 smem2 :smemory) (maxidx1: nat) (sb1: sbindings) (maxidx2: nat) (sb2: sbindings) (ops: stack_op_instr_map) : bool :=
let n1 := length smem1 in
let n2 := length smem2 in
if (n1 =? n2) then
let smem1' := reorder_memory_updates n1 n1 ctx smem1 maxidx1 sb1 in
let smem2' := reorder_memory_updates n2 n2 ctx smem2 maxidx2 sb2 in
basic_memory_cmp sstack_val_cmp ctx smem1' smem2' maxidx1 sb1 maxidx2 sb2 ops
else
false.
End MemoryCmpImpl.