source: src/ASM/PolicyFront.ma @ 2149

Last change on this file since 2149 was 2141, checked in by boender, 8 years ago
  • committed working version
File size: 34.6 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Status.ma".
5include "utilities/extralib.ma".
6include "ASM/Assembly.ma".
7
8(* Internal types *)
9
10(* ppc_pc_map: program length × (pseudo program counter ↦ 〈pc, jump_length〉) *)
11definition ppc_pc_map ≝ ℕ × (BitVectorTrie (ℕ × jump_length) 16).
12
13(* The different properties that we want/need to prove at some point *)
14(* During our iteration, everything not yet seen is None, and vice versa *)
15definition out_of_program_none ≝
16  λprefix:list labelled_instruction.λsigma:ppc_pc_map.
17  ∀i.i < 2^16 → (i > |prefix| ↔ bvt_lookup_opt … (bitvector_of_nat ? i) (\snd sigma) = None ?).
18
19(* If instruction i is a jump, then there will be something in the policy at
20 * position i *)
21definition is_jump' ≝
22  λx:preinstruction Identifier.
23  match x with
24  [ JC _ ⇒ True
25  | JNC _ ⇒ True
26  | JZ _ ⇒ True
27  | JNZ _ ⇒ True
28  | JB _ _ ⇒ True
29  | JNB _ _ ⇒ True
30  | JBC _ _ ⇒ True
31  | CJNE _ _ ⇒ True
32  | DJNZ _ _ ⇒ True
33  | _ ⇒ False
34  ].
35
36definition is_relative_jump ≝
37  λinstr:pseudo_instruction.
38  match instr with
39  [ Instruction i ⇒ is_jump' i
40  | _             ⇒ False
41  ].
42   
43definition is_jump ≝
44  λinstr:pseudo_instruction.
45  match instr with
46  [ Instruction i   ⇒ is_jump' i
47  | Call _ ⇒ True
48  | Jmp _ ⇒ True
49  | _ ⇒ False
50  ].
51
52definition is_call ≝
53  λinstr:pseudo_instruction.
54  match instr with
55  [ Call _ ⇒ True
56  | _ ⇒ False
57  ].
58 
59definition is_jump_to ≝
60  λx:pseudo_instruction.λd:Identifier.
61  match x with
62  [ Instruction i ⇒ match i with
63    [ JC j ⇒ d = j
64    | JNC j ⇒ d = j
65    | JZ j ⇒ d = j
66    | JNZ j ⇒ d = j
67    | JB _ j ⇒ d = j
68    | JNB _ j ⇒ d = j
69    | JBC _ j ⇒ d = j
70    | CJNE _ j ⇒ d = j
71    | DJNZ _ j ⇒ d = j
72    | _ ⇒ False
73    ]
74  | Call c ⇒ d = c
75  | Jmp j ⇒ d = j
76  | _ ⇒ False
77  ].
78 
79definition not_jump_default ≝
80  λprefix:list labelled_instruction.λsigma:ppc_pc_map.
81  ∀i:ℕ.i < |prefix| →
82  ¬is_jump (\snd (nth i ? prefix 〈None ?, Comment []〉)) →
83  \snd (bvt_lookup … (bitvector_of_nat ? i) (\snd sigma) 〈0,short_jump〉) = short_jump.
84 
85(* Between two policies, jumps cannot decrease *)
86definition jmpeqb: jump_length → jump_length → bool ≝
87  λj1.λj2.
88  match j1 with
89  [ short_jump ⇒ match j2 with [ short_jump ⇒ true | _ ⇒ false ]
90  | absolute_jump ⇒ match j2 with [ absolute_jump ⇒ true | _ ⇒ false ]
91  | long_jump ⇒ match j2 with [ long_jump ⇒ true | _ ⇒ false ]
92  ].
93
94lemma jmpeqb_to_eq: ∀j1,j2.jmpeqb j1 j2 → j1 = j2.
95 #j1 #j2 cases j1 cases j2
96 [1,5,9: / by /]
97 #H cases H
98qed.
99
100definition jmple: jump_length → jump_length → Prop ≝
101  λj1.λj2.
102  match j1 with
103  [ short_jump  ⇒
104    match j2 with
105    [ short_jump ⇒ False
106    | _          ⇒ True
107    ]
108  | absolute_jump ⇒
109    match j2 with
110    [ long_jump ⇒ True
111    | _         ⇒ False
112    ]
113  | long_jump   ⇒ False
114  ].
115
116definition jmpleq: jump_length → jump_length → Prop ≝
117  λj1.λj2.jmple j1 j2 ∨ j1 = j2.
118 
119definition jump_increase ≝
120 λprefix:list labelled_instruction.λop:ppc_pc_map.λp:ppc_pc_map.
121 ∀i.i ≤ |prefix| →
122   let 〈opc,oj〉 ≝ bvt_lookup … (bitvector_of_nat ? i) (\snd op) 〈0,short_jump〉 in
123   let 〈pc,j〉 ≝ bvt_lookup … (bitvector_of_nat ? i) (\snd p) 〈0,short_jump〉 in
124     (*opc ≤ pc ∧*) jmpleq oj j.
125     
126(* this is the instruction size as determined by the jump length given *)
127definition expand_relative_jump_internal_unsafe:
128  jump_length → ([[relative]] → preinstruction [[relative]]) → list instruction ≝
129  λjmp_len:jump_length.λi.
130  match jmp_len with
131  [ short_jump ⇒ [ RealInstruction (i (RELATIVE (zero 8))) ]
132  | absolute_jump ⇒ [ ] (* this should not happen *)
133  | long_jump ⇒
134    [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2)));
135      SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *)
136      LJMP (ADDR16 (zero 16))
137    ]
138  ].
139 @I
140qed.
141
142definition expand_relative_jump_unsafe:
143  jump_length → preinstruction Identifier → list instruction ≝
144  λjmp_len:jump_length.λi.
145  match i with
146  [ JC jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JC ?)
147  | JNC jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JNC ?)
148  | JB baddr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JB ? baddr)
149  | JZ jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JZ ?)
150  | JNZ jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JNZ ?)
151  | JBC baddr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JBC ? baddr)
152  | JNB baddr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JNB ? baddr)
153  | CJNE addr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (CJNE ? addr)
154  | DJNZ addr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (DJNZ ? addr)
155  | ADD arg1 arg2 ⇒ [ ADD ? arg1 arg2 ]
156  | ADDC arg1 arg2 ⇒ [ ADDC ? arg1 arg2 ]
157  | SUBB arg1 arg2 ⇒ [ SUBB ? arg1 arg2 ]
158  | INC arg ⇒ [ INC ? arg ]
159  | DEC arg ⇒ [ DEC ? arg ]
160  | MUL arg1 arg2 ⇒ [ MUL ? arg1 arg2 ]
161  | DIV arg1 arg2 ⇒ [ DIV ? arg1 arg2 ]
162  | DA arg ⇒ [ DA ? arg ]
163  | ANL arg ⇒ [ ANL ? arg ]
164  | ORL arg ⇒ [ ORL ? arg ]
165  | XRL arg ⇒ [ XRL ? arg ]
166  | CLR arg ⇒ [ CLR ? arg ]
167  | CPL arg ⇒ [ CPL ? arg ]
168  | RL arg ⇒ [ RL ? arg ]
169  | RR arg ⇒ [ RR ? arg ]
170  | RLC arg ⇒ [ RLC ? arg ]
171  | RRC arg ⇒ [ RRC ? arg ]
172  | SWAP arg ⇒ [ SWAP ? arg ]
173  | MOV arg ⇒ [ MOV ? arg ]
174  | MOVX arg ⇒ [ MOVX ? arg ]
175  | SETB arg ⇒ [ SETB ? arg ]
176  | PUSH arg ⇒ [ PUSH ? arg ]
177  | POP arg ⇒ [ POP ? arg ]
178  | XCH arg1 arg2 ⇒ [ XCH ? arg1 arg2 ]
179  | XCHD arg1 arg2 ⇒ [ XCHD ? arg1 arg2 ]
180  | RET ⇒ [ RET ? ]
181  | RETI ⇒ [ RETI ? ]
182  | NOP ⇒ [ RealInstruction (NOP ?) ]
183  ].
184
185definition instruction_size_jmplen:
186 jump_length → pseudo_instruction → ℕ ≝
187  λjmp_len.
188  λi.
189  let pseudos ≝ match i with
190  [ Cost cost ⇒ [ ]
191  | Comment comment ⇒ [ ]
192  | Call call ⇒
193    match jmp_len with
194    [ short_jump ⇒ [ ] (* this should not happen *)
195    | absolute_jump ⇒ [ ACALL (ADDR11 (zero 11)) ]
196    | long_jump ⇒ [ LCALL (ADDR16 (zero 16)) ]
197    ]
198  | Mov d trgt ⇒
199     [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, DATA16 (zero 16)〉))))]
200  | Instruction instr ⇒ expand_relative_jump_unsafe jmp_len instr
201  | Jmp jmp ⇒
202    match jmp_len with
203    [ short_jump ⇒ [ SJMP (RELATIVE (zero 8)) ]
204    | absolute_jump ⇒ [ AJMP (ADDR11 (zero 11)) ]
205    | long_jump ⇒ [ LJMP (ADDR16 (zero 16)) ]
206    ]
207  ] in
208  let mapped ≝ map ? ? assembly1 pseudos in
209  let flattened ≝ flatten ? mapped in
210  let pc_len ≝ length ? flattened in
211    pc_len.
212 @I.
213qed.
214
215definition sigma_compact_unsafe ≝
216 λprefix:list labelled_instruction.λlabels:label_map.λsigma:ppc_pc_map.
217 ∀n.n < |prefix| →
218  match bvt_lookup_opt … (bitvector_of_nat ? n) (\snd sigma) with
219  [ None ⇒ False
220  | Some x ⇒ let 〈pc,j〉 ≝ x in
221    match bvt_lookup_opt … (bitvector_of_nat ? (S n)) (\snd sigma) with
222    [ None ⇒ False
223    | Some x1 ⇒ let 〈pc1,j1〉 ≝ x1 in
224       pc1 = pc + instruction_size_jmplen j (\snd (nth n ? prefix 〈None ?, Comment []〉))
225    ]
226  ].
227   
228(* new safety condition: sigma corresponds to program and resulting program is compact *)
229definition sigma_compact ≝
230 λprogram:list labelled_instruction.λlabels:label_map.λsigma:ppc_pc_map.
231 ∀n.n < |program| →
232  match bvt_lookup_opt … (bitvector_of_nat ? n) (\snd sigma) with
233  [ None ⇒ False
234  | Some x ⇒ let 〈pc,j〉 ≝ x in
235    match bvt_lookup_opt … (bitvector_of_nat ? (S n)) (\snd sigma) with
236    [ None ⇒ False
237    | Some x1 ⇒ let 〈pc1,j1〉 ≝ x1 in
238       pc1 = pc + instruction_size (λid.bitvector_of_nat ? (lookup_def ?? labels id 0))
239         (λppc.bitvector_of_nat ? (\fst (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
240         (λppc.jmpeqb long_jump (\snd (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
241         (bitvector_of_nat ? n) (\snd (nth n ? program 〈None ?, Comment []〉))
242    ]
243  ].
244
245(* jumps are of the proper size *)
246definition sigma_safe ≝
247 λprefix:list labelled_instruction.λlabels:label_map.λadded:ℕ.
248 λold_sigma:ppc_pc_map.λsigma:ppc_pc_map.
249 ∀i.i < |prefix| →
250 let 〈pc,j〉 ≝ bvt_lookup … (bitvector_of_nat ? i) (\snd sigma) 〈0,short_jump〉 in
251 let pc_plus_jmp_length ≝ bitvector_of_nat ? (\fst (bvt_lookup … (bitvector_of_nat ? (S i)) (\snd sigma) 〈0,short_jump〉)) in
252 let 〈label,instr〉 ≝ nth i ? prefix 〈None ?, Comment [ ]〉 in
253 ∀dest.is_jump_to instr dest →
254   let paddr ≝ lookup_def … labels dest 0 in
255   let addr ≝ bitvector_of_nat ? (if leb paddr (|prefix|) (* jump to address already known *)
256   then \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd sigma) 〈0,short_jump〉)
257   else \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
258   match j with
259   [ short_jump ⇒ \fst (short_jump_cond pc_plus_jmp_length addr) = true ∧
260      ¬is_call instr
261   | absolute_jump ⇒  \fst (absolute_jump_cond pc_plus_jmp_length addr) = true ∧
262       \fst (short_jump_cond pc_plus_jmp_length addr) = false ∧
263       ¬is_relative_jump instr
264   | long_jump   ⇒ \fst (short_jump_cond pc_plus_jmp_length addr) = false ∧
265       \fst (absolute_jump_cond pc_plus_jmp_length addr) = false
266   ].
267 
268(* Definitions and theorems for the jump_length type (itself defined in Assembly) *)
269definition max_length: jump_length → jump_length → jump_length ≝
270  λj1.λj2.
271  match j1 with
272  [ long_jump   ⇒ long_jump
273  | absolute_jump ⇒
274    match j2 with
275    [ absolute_jump ⇒ absolute_jump
276    | _           ⇒ long_jump
277    ]
278  | short_jump  ⇒
279    match j2 with
280    [ short_jump ⇒ short_jump
281    | _          ⇒ long_jump
282    ]
283  ].
284
285lemma dec_jmple: ∀x,y:jump_length.Sum (jmple x y) (¬(jmple x y)).
286 #x #y cases x cases y /3 by inl, inr, nmk, I/
287qed.
288 
289lemma jmpleq_max_length: ∀ol,nl.
290  jmpleq ol (max_length ol nl).
291 #ol #nl cases ol cases nl
292 /2 by or_introl, or_intror, I/
293qed.
294
295lemma dec_eq_jump_length: ∀a,b:jump_length.Sum (a = b) (a ≠ b).
296  #a #b cases a cases b /2/
297  %2 @nmk #H destruct (H)
298qed.
299 
300(* The function that creates the label-to-address map *)
301definition create_label_map: ∀program:list labelled_instruction.
302  (Σlabels:label_map.
303    ∀l.occurs_exactly_once ?? l program →
304    bitvector_of_nat ? (lookup_def ?? labels l 0) =
305     address_of_word_labels_code_mem program l
306  ) ≝
307 λprogram.
308   \fst (create_label_cost_map program).
309 #l #Hl lapply (pi2 ?? (create_label_cost_map0 program)) @pair_elim
310 #labels #costs #EQ normalize nodelta #H whd in match create_label_cost_map;
311 normalize nodelta >EQ @(H l Hl)
312qed.
313
314(* General note on jump length selection: the jump displacement is added/replaced
315 * AFTER the fetch (and attendant PC increase), but we calculate before the
316 * fetch, which means that in the case of a short and medium jump we are 2
317 * bytes off and have to compensate.
318 * For the long jump we don't care, because the PC gets replaced integrally anyway. *)
319definition select_reljump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
320  Identifier → jump_length ≝
321  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
322  let pc ≝ \fst inc_sigma in
323  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
324  let paddr ≝ lookup_def … labels lbl 0 in
325  let addr ≝ bitvector_of_nat ? (if leb paddr ppc (* jump to address already known *)
326  then \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉)
327  else \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
328  let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length addr in
329  if sj_possible
330  then short_jump
331  else long_jump.
332
333definition select_call_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
334  Identifier → jump_length ≝
335  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
336  let pc ≝ \fst inc_sigma in
337  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
338  let paddr ≝ lookup_def ? ? labels lbl 0 in
339  let addr ≝ bitvector_of_nat ?
340    (if leb paddr ppc (* jump to address already known *)
341    then \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd inc_sigma) 〈0,short_jump〉)
342    else \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
343  let 〈aj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length addr in   
344  if aj_possible
345  then absolute_jump
346  else long_jump.
347 
348definition select_jump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
349  Identifier → jump_length ≝
350  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
351  let pc ≝ \fst inc_sigma in
352  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
353  let paddr ≝ lookup_def … labels lbl 0 in
354  let addr ≝ bitvector_of_nat ? (if leb paddr ppc (* jump to address already known *)
355  then \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉)
356  else \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
357  let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length addr in
358  if sj_possible
359  then short_jump
360  else select_call_length labels old_sigma inc_sigma added ppc lbl.
361 
362definition jump_expansion_step_instruction: label_map → ppc_pc_map → ppc_pc_map →
363  ℕ → ℕ → preinstruction Identifier → option jump_length ≝
364  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λi.
365  match i with
366  [ JC j     ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
367  | JNC j    ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
368  | JZ j     ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
369  | JNZ j    ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
370  | JB _ j   ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
371  | JBC _ j  ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
372  | JNB _ j  ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
373  | CJNE _ j ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
374  | DJNZ _ j ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
375  | _        ⇒ None ?
376  ].
377
378lemma dec_is_jump: ∀x.Sum (is_jump x) (¬is_jump x).
379#i cases i
380[#id cases id
381 [1,2,3,6,7,33,34:
382  #x #y %2 whd in match (is_jump ?); /2 by nmk/
383 |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32:
384  #x %2 whd in match (is_jump ?); /2 by nmk/
385 |35,36,37: %2 whd in match (is_jump ?); /2 by nmk/
386 |9,10,14,15: #x %1 / by I/
387 |11,12,13,16,17: #x #y %1 / by I/
388 ]
389|2,3: #x %2 /2 by nmk/
390|4,5: #x %1 / by I/
391|6: #x #y %2 /2 by nmk/
392]
393qed.
394
395(* The first step of the jump expansion: everything to short. *)
396definition jump_expansion_start:
397  ∀program:(Σl:list labelled_instruction.S (|l|) < 2^16 ∧ is_well_labelled_p l).
398  ∀labels:label_map.
399  Σpolicy:option ppc_pc_map.
400    match policy with
401    [ None ⇒ True
402    | Some p ⇒ And (And (And (And (And (And
403       (out_of_program_none (pi1 ?? program) p)
404       (not_jump_default (pi1 ?? program) p))
405       (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd p) 〈0,short_jump〉) = 0))
406       (\fst p = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd p) 〈0,short_jump〉)))
407       (sigma_compact_unsafe program labels p))
408       (∀i.i ≤ |program| → ∃pc.
409         bvt_lookup_opt … (bitvector_of_nat ? i) (\snd p) = Some ? 〈pc,short_jump〉))
410       (\fst p < 2^16)         
411    ] ≝
412  λprogram.λlabels.
413  let final_policy ≝ foldl_strong (option Identifier × pseudo_instruction)
414  (λprefix.Σpolicy:ppc_pc_map.And (And (And (And (And
415    (out_of_program_none prefix policy)
416    (not_jump_default prefix policy))
417    (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd policy) 〈0,short_jump〉) = 0))
418    (\fst policy = \fst (bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd policy) 〈0,short_jump〉)))
419    (sigma_compact_unsafe prefix labels policy))
420    (∀i.i ≤ |prefix| → ∃pc.
421      bvt_lookup_opt … (bitvector_of_nat ? i) (\snd policy) = Some ? 〈pc,short_jump〉))
422  program
423  (λprefix.λx.λtl.λprf.λp.
424   let 〈pc,sigma〉 ≝ pi1 ?? p in
425   let 〈label,instr〉 ≝ x in
426   let isize ≝ instruction_size_jmplen short_jump instr in
427   〈pc + isize, bvt_insert … (bitvector_of_nat 16 (S (|prefix|))) 〈pc+isize,short_jump〉 sigma〉
428  ) 〈0, bvt_insert ?? (bitvector_of_nat 16 0) 〈0,short_jump〉 (Stub ??)〉 in
429  if geb (\fst (pi1 ?? final_policy)) 2^16 then
430    None ?
431  else
432    Some ? (pi1 ?? final_policy).
433[ / by I/
434| lapply p -p generalize in match (foldl_strong ?????); * #p #Hp #hg
435  @conj [ @Hp | @not_le_to_lt @leb_false_to_not_le <geb_to_leb @hg ]
436| @conj [ @conj [ @conj [ @conj [ @conj
437  [ (* out_of_program_none *)
438    #i #Hi2 >append_length <commutative_plus @conj
439    [ (* → *) #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi) -Hi #Hi
440      cases p -p #p cases p -p #pc #p #Hp cases x -x #l #pi
441      [ >lookup_opt_insert_miss
442        [ (* USE[pass]: out_of_program_none → *)
443          @(proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp)))) i Hi2))
444          @le_S_to_le @le_S_to_le @Hi
445        | @bitvector_of_nat_abs
446          [ @Hi2
447          | @(transitive_lt … Hi2) @le_S_to_le @Hi
448          | @sym_neq @lt_to_not_eq @le_S_to_le @Hi
449          ]
450        ]
451      | >lookup_opt_insert_miss
452        [ <Hi
453          (* USE[pass]: out_of_program_none → *)
454          @(proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp)))) (S (S (|prefix|))) ?))
455          [ >Hi @Hi2
456          | @le_S @le_n
457          ]
458        | @bitvector_of_nat_abs
459          [ @Hi2
460          | @(transitive_lt … Hi2) <Hi @le_n
461          | @sym_neq @lt_to_not_eq <Hi @le_n
462          ]
463        ]
464      ]
465    | (* ← *) cases p -p #p cases p -p #pc #p #Hp cases x in prf; -x #l #pi #prf
466      normalize nodelta cases (decidable_eq_nat i (S (|prefix|)))
467      [ #Hi >Hi >lookup_opt_insert_hit #H destruct (H)
468      | #Hi >lookup_opt_insert_miss
469        [ #Hl
470          (* USE[pass]: out_of_program_none ← *)
471          elim (le_to_or_lt_eq … (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp)))) i Hi2) Hl))
472          [ #Hi3 @Hi3
473          | #Hi3 @⊥ @(absurd ? Hi3) @sym_neq @Hi
474          ]
475        | @bitvector_of_nat_abs
476          [ @Hi2
477          | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length
478            <plus_n_Sm @le_S_S @le_plus_n_r
479          | @Hi
480          ]
481        ]
482      ]
483    ]
484  | (* not_jump_default *) cases p -p #p cases p -p #pc #sigma #Hp
485    cases x in prf; #lbl #ins #prf #i >append_length <commutative_plus #Hi
486    normalize in Hi; normalize nodelta cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
487    [ >lookup_insert_miss
488      [ (* USE[pass]: not_jump_default *)
489        lapply ((proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp))))) i Hi)
490        >nth_append_first
491        [ #H #H2 @H @H2
492        | @Hi
493        ]
494      | @bitvector_of_nat_abs
495        [ @(transitive_lt ??? Hi) @le_S_to_le]
496        [1,2: @(transitive_lt … (proj1 ?? (pi2 ?? program))) @le_S_S >prf >append_length
497          <plus_n_Sm @le_S_S @le_plus_n_r
498        | @lt_to_not_eq @le_S @Hi
499        ]
500      ]
501    | >Hi >lookup_insert_miss
502      [ #_ (* USE: everything is short *)
503        elim ((proj2 ?? Hp) (|prefix|) (le_n (|prefix|))) #pc #Hl
504        >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) @refl
505      | @bitvector_of_nat_abs
506        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length @le_plus_n_r
507        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm @le_S_S
508          @le_plus_n_r
509        | @lt_to_not_eq @le_n
510        ]
511      ]
512    ]
513  ]
514  | (* 0 ↦ 0 *)
515    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
516    >lookup_insert_miss
517    [ (* USE[pass]: 0 ↦ 0 *)
518      @(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp))))
519    | @bitvector_of_nat_abs
520      [ / by /
521      | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length @le_S_S <plus_n_Sm
522        @le_S_S @le_plus_n_r
523      | @lt_to_not_eq / by /
524      ]
525    ]
526  ]
527  | (* fst p = pc *)
528    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
529    >append_length >(commutative_plus (|prefix|)) >lookup_insert_hit @refl
530  ]
531  | (* policy_compact_unsafe *) #i >append_length <commutative_plus #Hi normalize in Hi;
532    cases p -p #p cases p -p #fpc #sigma #Hp cases x #lbl #instr normalize nodelta
533    cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
534    [ >lookup_opt_insert_miss
535      [ >lookup_opt_insert_miss
536        [ (* USE[pass]: policy_compact_unsafe *)
537          lapply (proj2 ?? (proj1 ?? Hp) i Hi)
538          lapply (refl ? (bvt_lookup_opt … (bitvector_of_nat ? i) sigma))
539          cases (bvt_lookup_opt … (bitvector_of_nat ? i) sigma) in ⊢ (???% → %);
540          [ #_ normalize nodelta / by /
541          | #x cases x -x #pci #ji #EQi
542            lapply (refl ? (bvt_lookup_opt … (bitvector_of_nat ? (S i)) sigma))
543            cases (bvt_lookup_opt … (bitvector_of_nat ? (S i)) sigma) in ⊢ (???% → %);
544            [ #_ normalize nodelta / by /
545            | #x cases x -x #pcSi #jSi #EQSi normalize nodelta >nth_append_first
546              [ / by /
547              | @Hi
548              ]
549            ]
550          ]
551        ]
552      ]
553      [2: lapply (le_S_to_le … Hi) -Hi #Hi]
554      @bitvector_of_nat_abs
555      [1,4: @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <commutative_plus
556        @le_plus_a @Hi
557      |2,5: @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm
558        @le_S_S @le_plus_n_r
559      |3,6: @lt_to_not_eq @le_S_S @Hi
560      ]
561    | >lookup_opt_insert_miss
562      [ >Hi >lookup_opt_insert_hit normalize nodelta
563        (* USE: everything is short, fst p = pc *)
564        elim ((proj2 ?? Hp) (|prefix|) (le_n ?)) #pc #Hl
565        lapply (proj2 ?? (proj1 ?? (proj1 ?? Hp))) >Hl
566        >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) #EQ normalize nodelta >nth_append_second
567        [ <minus_n_n whd in match (nth ????); >EQ @refl
568        | @le_n
569        ]
570      | @bitvector_of_nat_abs
571        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >Hi >prf @le_S_S >append_length <commutative_plus
572          @le_plus_a @le_n
573        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm
574          @le_S_S @le_plus_n_r
575        | @lt_to_not_eq @le_S_S >Hi @le_n
576        ]
577      ]
578    ]
579  ]
580  | (* everything is short *) #i >append_length <commutative_plus #Hi normalize in Hi;
581    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
582    cases (le_to_or_lt_eq … Hi) -Hi #Hi
583    [ >lookup_opt_insert_miss
584      [ (* USE[pass]: everything is short *)
585        @((proj2 ?? Hp) i (le_S_S_to_le … Hi))
586      | @bitvector_of_nat_abs
587        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length @le_S_S
588          >commutative_plus @le_plus_a @le_S_S_to_le @Hi
589        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length <plus_n_Sm
590          @le_S_S @le_S_S @le_plus_n_r
591        | @lt_to_not_eq @Hi
592        ]
593      ]
594    | >Hi >lookup_opt_insert_hit @(ex_intro ?? (pc+instruction_size_jmplen short_jump instr))
595      @refl
596    ]
597  ]
598| @conj [ @conj [ @conj [ @conj [ @conj
599  [ #i cases i
600    [ #Hi2 @conj
601      [ (* → *) #Hi @⊥ @(absurd ? Hi) @le_to_not_lt / by /
602      | (* ← *) >lookup_opt_insert_hit #Hl destruct (Hl)
603      ]
604    | -i #i #Hi2 @conj
605      [ #Hi >lookup_opt_insert_miss
606        [ / by refl/
607        | @bitvector_of_nat_abs
608          [ @Hi2
609          | / by /
610          | @sym_neq @lt_to_not_eq / by /
611          ]
612        ]
613      | #_ @le_S_S @le_O_n
614      ]
615    ]
616  | #i cases i
617    [ #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
618    | -i #i #Hi #Hj @⊥ @(absurd … Hi) @not_le_Sn_O
619    ]
620  ] ] ]
621  >lookup_insert_hit @refl
622  | #i cases i
623    [ #Hi @⊥ @(absurd … Hi) @le_to_not_lt @le_n
624    | -i #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
625    ]
626  ]
627  | #i cases i
628    [ #Hi >lookup_opt_insert_hit @(ex_intro ?? 0) @refl
629    | -i #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
630    ]
631  ]
632]
633qed.
634
635(* NOTE: we only compare the first elements here because otherwise the
636 * added = 0 → policy_equal property of jump_expansion_step doesn't hold:
637 * if we have not added anything to the pc, we only know the PC hasn't changed,
638 * there might still have been a short/medium jump change *)
639definition sigma_pc_equal ≝
640  λprogram:list labelled_instruction.λp1,p2:ppc_pc_map.
641  (∀n.n ≤ |program| →
642    \fst (bvt_lookup … (bitvector_of_nat 16 n) (\snd p1) 〈0,short_jump〉) =
643    \fst (bvt_lookup … (bitvector_of_nat 16 n) (\snd p2) 〈0,short_jump〉)).
644
645definition sigma_jump_equal ≝
646  λprogram:list labelled_instruction.λp1,p2:ppc_pc_map.
647  (∀n.n < |program| →
648    \snd (bvt_lookup … (bitvector_of_nat 16 n) (\snd p1) 〈0,short_jump〉) =
649    \snd (bvt_lookup … (bitvector_of_nat 16 n) (\snd p2) 〈0,short_jump〉)).
650   
651definition nec_plus_ultra ≝
652  λprogram:list labelled_instruction.λp:ppc_pc_map.
653  ¬(∀i.i < |program| → is_jump (\snd (nth i ? program 〈None ?, Comment []〉)) →
654  \snd (bvt_lookup … (bitvector_of_nat 16 i) (\snd p) 〈0,short_jump〉) = long_jump).
655 
656(*include alias "common/Identifiers.ma".*)
657include alias "ASM/BitVector.ma".
658include alias "basics/lists/list.ma".
659include alias "arithmetics/nat.ma".
660include alias "basics/logic.ma".
661
662lemma jump_length_equal_max: ∀a,b,i.
663  is_jump i → instruction_size_jmplen (max_length a b) i = instruction_size_jmplen a i →
664  (max_length a b) = a.
665 #a #b #i cases i
666 [1: #pi cases pi
667   [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
668     try (#x #y #H #_) try (#x #H #_) try (#H #_) cases H
669   |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y] #_ try (#_ %)
670     try (#abs normalize in abs; destruct (abs) @I)
671     cases a; cases b; try (#_ %) try (#abs normalize in abs; destruct(abs) @I)
672     try (@(subaddressing_mode_elim … x) #w #abs normalize in abs; destruct (abs) @I)
673     cases x * #a1 #a2 @(subaddressing_mode_elim … a2) #w
674     try ( #abs normalize in abs; destruct (abs) @I)
675     @(subaddressing_mode_elim … a1) #w2 #abs normalize in abs; destruct (abs)
676   ]
677  |2,3,6: #x [3: #y] #H cases H
678  |4,5: #id #_ cases a cases b
679    [2,3,4,6,11,12,13,15: normalize #H destruct (H)
680    |1,5,7,8,9,10,14,16,17,18: #H / by refl/
681    ]
682  ]
683qed.
684
685lemma jump_length_le_max: ∀a,b,i.is_jump i →
686  instruction_size_jmplen a i ≤ instruction_size_jmplen (max_length a b) i.
687 #a #b #i cases i
688 [2,3,6: #x [3: #y] #H cases H
689 |4,5: #id #_ cases a cases b / by le_n/
690 |1: #pi cases pi
691   [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
692     try (#x #y #H) try (#x #H) try (#H) cases H
693   |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y]
694     #_ cases a cases b
695     [1,4,5,6,7,8,9: / by le_n/
696     |10,13,14,15,16,17,18: / by le_n/
697     |19,22,23,24,25,26,27: / by le_n/
698     |28,31,32,33,34,35,36: / by le_n/
699     |37,40,41,42,43,44,45: / by le_n/
700     |46,47,48,49,50,51,52,53,54: / by le_n/
701     |55,56,57,58,59,60,61,62,63: / by le_n/
702     |64,65,66,67,68,69,70,71,72: / by le_n/
703     |73,74,75,76,77,78,79,80,81: / by le_n/
704     ]
705     try (@(subaddressing_mode_elim … x) #w normalize @le_S @le_S @le_S @le_S @le_S @le_n)
706     cases x * #ad   
707     try (@(subaddressing_mode_elim … ad) #w #z normalize @le_S @le_S @le_S @le_S @le_S @le_n)
708     #z @(subaddressing_mode_elim … z) #w normalize / by /
709   ]
710 ]
711qed.
712
713lemma label_in_program: ∀program:(Σl.S (|l|) < 2^16 ∧ is_well_labelled_p l).∀labels:(Σlm.
714   ∀l.occurs_exactly_once ?? l program →
715    bitvector_of_nat ? (lookup_def ?? lm l 0) =
716     address_of_word_labels_code_mem program l).∀x.
717 occurs_exactly_once ?? x program →   
718 lookup_def ASMTag ℕ labels x O≤|program|.
719#program cases program -program #program #Hprogram #labels #x cases labels
720-labels #labels #H lapply (H x) -H
721generalize in match (lookup_def … labels x 0);
722whd in match address_of_word_labels_code_mem;
723whd in match index_of; normalize nodelta elim program in Hprogram;
724[ #Hprogram #n cases not_implemented
725| #h #t #Hind #Hprogram #n #Hlm #Hocc lapply (Hlm Hocc) -Hlm #Hbv
726  whd in match (occurs_exactly_once ????) in Hocc;
727  whd in match (index_of_internal ????) in Hbv;
728  lapply (refl ? (instruction_matches_identifier … x h))
729  lapply Hocc; lapply Hbv; -Hocc -Hbv
730  cases (instruction_matches_identifier … x h) in ⊢ (% → % → ???% → %);
731  normalize nodelta #Hbv #Hocc #EQ
732  [ >(bitvector_of_nat_ok 16 n 0)
733    [ @le_O_n
734    | >(eq_eq_bv … Hbv) @I
735    | / by /
736    | cases daemon
737    ]
738  | cases n in Hbv;
739    [ #_ @le_O_n
740    | -n #n #Hbv @le_S_S @(Hind … Hocc)
741      [ @conj
742        [ @(transitive_lt … (proj1 ?? Hprogram)) @le_n
743        | cases daemon (* axiomatize this *)
744        ]
745      | #_ lapply (bitvector_of_nat_ok 16 (S n) (index_of_internal ? (instruction_matches_identifier ?? x) t 1) ???)
746        [ >Hbv >eq_bv_refl @I
747        | @(transitive_lt … (proj1 ?? Hprogram)) cases daemon
748        | cases daemon
749        | #H >(index_of_label_from_internal … Hocc)
750          <plus_O_n >(index_of_label_from_internal … Hocc) in H;
751          #H >(injective_S … H) <plus_O_n @refl
752        ]
753      ]
754    ]
755  ]
756]
757qed.
758
759lemma equal_compact_unsafe_compact: ∀program:(Σl.(S (|l|)) < 2^16 ∧ is_well_labelled_p l).
760  ∀labels.∀old_sigma.∀sigma.
761  sigma_pc_equal program old_sigma sigma →
762  sigma_safe program labels 0 old_sigma sigma →
763  sigma_compact_unsafe program labels sigma →
764  sigma_compact program labels sigma.
765  #program #labels #old_sigma #sigma #Hequal #Hsafe #Hcp_unsafe #i #Hi
766  lapply (Hcp_unsafe i Hi) lapply (Hsafe i Hi)
767  lapply (refl ? (lookup_opt … (bitvector_of_nat ? i) (\snd sigma)))
768  cases (lookup_opt … (bitvector_of_nat ? i) (\snd sigma)) in ⊢ (???% → %);
769  [ / by /
770  | #x cases x -x #x1 #x2 #EQ
771    cases (lookup_opt … (bitvector_of_nat ? (S i)) (\snd sigma))
772    [ / by /
773    | #y cases y -y #y1 #y2 normalize nodelta #H #H2
774      cut (instruction_size_jmplen x2
775       (\snd (nth i ? program 〈None ?, Comment []〉)) =
776       instruction_size … (bitvector_of_nat ? i)
777       (\snd (nth i ? program 〈None ?, Comment []〉)))
778      [5: #H3 <H3 @H2
779      |4: whd in match (instruction_size_jmplen ??);
780          whd in match (instruction_size …);
781          whd in match (assembly_1_pseudoinstruction …);
782          whd in match (expand_pseudo_instruction …);
783          normalize nodelta whd in match (append …) in H;
784          cases (nth i ? program 〈None ?,Comment []〉) in H;
785          #lbl #instr cases instr
786          [2,3,6: #x [3: #y] normalize nodelta #H @refl
787          |4,5: #x >(lookup_opt_lookup_hit … EQ 〈0,short_jump〉) #Hj
788            lapply (Hj x (refl ? x)) -Hj normalize nodelta
789            >add_bitvector_of_nat_plus <(plus_n_Sm i 0) <plus_n_O
790            cases x2 normalize nodelta
791            [1,4: whd in match short_jump_cond; normalize nodelta
792              lapply (refl ? (leb (lookup_def ?? labels x 0) (|[]@program|)))
793              cases (leb (lookup_def ?? labels x 0) (|[]@program|)) in ⊢ (???% → %); #Hlab
794              normalize nodelta
795              >(Hequal (lookup_def ?? labels x 0) ?)
796              [2,4,6,8: @(label_in_program program labels x)
797                cases daemon (* XXX this has to come from somewhere else *)
798              ]
799              <plus_n_O
800              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
801              #result #flags normalize nodelta
802              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
803              cases (get_index' bool 2 0 flags) normalize nodelta
804              [5,6,7,8: #H @⊥ @(absurd ?? (proj2 ?? H)) / by I/
805              |1,2,3,4: cases (eq_bv 9 upper ?)
806                [2,4,6,8: #H lapply (proj1 ?? H) #H3 destruct (H3)
807                |1,3,5,7: #_ normalize nodelta @refl
808                ]
809              ]
810            |2,5: whd in match short_jump_cond; whd in match absolute_jump_cond;
811              lapply (refl ? (leb (lookup_def ?? labels x 0) (|[]@program|)))
812              cases (leb (lookup_def ?? labels x 0) (|[]@program|)) in ⊢ (???% → %); #Hlab
813              normalize nodelta
814              (* USE: added = 0 → policy_pc_equal (from fold) *)
815              >(Hequal (lookup_def ?? labels x 0) ?)
816              [2,4,6,8: @(label_in_program program labels x)
817                cases daemon (* XXX this has to come from somewhere else *)]
818              <plus_n_O
819              normalize nodelta cases (vsplit bool 5 11 ?) #addr1 #addr2
820              cases (vsplit bool 5 11 ?) #pc1 #pc2 normalize nodelta
821              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
822              #result #flags normalize nodelta
823              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
824              cases (get_index' bool 2 0 flags) normalize nodelta
825              #H >(proj2 ?? (proj1 ?? H)) >(proj1 ?? (proj1 ?? H)) normalize nodelta @refl
826            |3,6: whd in match short_jump_cond; whd in match absolute_jump_cond;
827              lapply (refl ? (leb (lookup_def ?? labels x 0) (|[]@program|)))
828              cases (leb (lookup_def ?? labels x 0) (|[]@program|)) in ⊢ (???% → %); #Hlab
829              normalize nodelta
830              (* USE: added = 0 → policy_pc_equal (from fold) *)
831              >(Hequal (lookup_def ?? labels x 0) ?)
832              [2,4,6,8: @(label_in_program program labels x)
833                cases daemon (* XXX this has to come from somewhere else *)]
834              <plus_n_O normalize nodelta
835              cases (vsplit bool 5 11 ?) #addr1 #addr2
836              cases (vsplit bool 5 11 ?) #pc1 #pc2 normalize nodelta
837              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
838              #result #flags normalize nodelta
839              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
840              cases (get_index' bool 2 0 flags) normalize nodelta
841              #H >(proj1 ?? H) >(proj2 ?? H) normalize nodelta @refl
842            ]
843          |1: #pi cases pi
844            [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
845              [1,2,3,6,7,24,25: #x #y
846              |4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23: #x]
847              normalize nodelta #H @refl
848            |9,10,11,12,13,14,15,16,17: [1,2,6,7: #x |3,4,5,8,9: #y #x]
849              normalize nodelta >(lookup_opt_lookup_hit … EQ 〈0,short_jump〉)
850              #Hj lapply (Hj x (refl ? x)) -Hj
851              whd in match expand_relative_jump; normalize nodelta
852              whd in match expand_relative_jump_internal; normalize nodelta
853              whd in match expand_relative_jump_unsafe; normalize nodelta
854              whd in match expand_relative_jump_internal_unsafe;
855              normalize nodelta >(add_bitvector_of_nat_plus ? i 1)
856              <(plus_n_Sm i 0) <plus_n_O
857              cases daemon (* XXX this needs subadressing mode magic, see above *)
858            ]
859          ]
860        ]
861      ]
862    ]
863qed.
Note: See TracBrowser for help on using the repository browser.