source: src/ASM/PolicyFront.ma @ 2225

Last change on this file since 2225 was 2225, checked in by sacerdot, 8 years ago

Minor and major improvements everywhere, shortened proofs.

File size: 33.0 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     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       bool_to_Prop (¬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       bool_to_Prop (¬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    And (bitvector_of_nat ? (lookup_def ?? labels l 0) =
305     address_of_word_labels_code_mem program l)
306    (lookup_def ?? labels l 0 < |program|)
307  ) ≝
308 λprogram.
309   \fst (create_label_cost_map program).
310 #l #Hl lapply (pi2 ?? (create_label_cost_map0 program)) @pair_elim
311 #labels #costs #EQ normalize nodelta #H whd in match create_label_cost_map;
312 normalize nodelta >EQ @(H l Hl)
313qed.
314
315(* General note on jump length selection: the jump displacement is added/replaced
316 * AFTER the fetch (and attendant PC increase), but we calculate before the
317 * fetch, which means that in the case of a short and medium jump we are 2
318 * bytes off and have to compensate.
319 * For the long jump we don't care, because the PC gets replaced integrally anyway. *)
320definition select_reljump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
321  Identifier → jump_length ≝
322  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
323  let pc ≝ \fst inc_sigma in
324  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
325  let paddr ≝ lookup_def … labels lbl 0 in
326  let addr ≝ bitvector_of_nat ? (if leb paddr ppc (* jump to address already known *)
327  then \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉)
328  else \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
329  let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length addr in
330  if sj_possible
331  then short_jump
332  else long_jump.
333
334definition select_call_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
335  Identifier → jump_length ≝
336  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
337  let pc ≝ \fst inc_sigma in
338  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
339  let paddr ≝ lookup_def ? ? labels lbl 0 in
340  let addr ≝ bitvector_of_nat ?
341    (if leb paddr ppc (* jump to address already known *)
342    then \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd inc_sigma) 〈0,short_jump〉)
343    else \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
344  let 〈aj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length addr in   
345  if aj_possible
346  then absolute_jump
347  else long_jump.
348 
349definition select_jump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
350  Identifier → jump_length ≝
351  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
352  let pc ≝ \fst inc_sigma in
353  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
354  let paddr ≝ lookup_def … labels lbl 0 in
355  let addr ≝ bitvector_of_nat ? (if leb paddr ppc (* jump to address already known *)
356  then \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉)
357  else \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
358  let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length addr in
359  if sj_possible
360  then short_jump
361  else select_call_length labels old_sigma inc_sigma added ppc lbl.
362
363definition destination_of: preinstruction Identifier → option Identifier ≝
364  λi.
365  match i with
366  [ JC j     ⇒ Some ? j
367  | JNC j    ⇒ Some ? j
368  | JZ j     ⇒ Some ? j
369  | JNZ j    ⇒ Some ? j
370  | JB _ j   ⇒ Some ? j
371  | JBC _ j  ⇒ Some ? j
372  | JNB _ j  ⇒ Some ? j
373  | CJNE _ j ⇒ Some ? j
374  | DJNZ _ j ⇒ Some ? j
375  | _        ⇒ None ?
376  ].
377
378definition jump_expansion_step_instruction: label_map → ppc_pc_map → ppc_pc_map →
379  ℕ → ℕ → preinstruction Identifier → option jump_length ≝
380  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λi.
381  match destination_of i with
382  [ Some j     ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
383  | None       ⇒ None ?
384  ].
385
386(* The first step of the jump expansion: everything to short. *)
387definition jump_expansion_start:
388  ∀program:(Σl:list labelled_instruction.S (|l|) < 2^16 ∧ is_well_labelled_p l).
389  ∀labels:label_map.
390  Σpolicy:option ppc_pc_map.
391    match policy with
392    [ None ⇒ True
393    | Some p ⇒ And (And (And (And (And
394       (not_jump_default (pi1 ?? program) p)
395       (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd p) 〈0,short_jump〉) = 0))
396       (\fst p = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd p) 〈0,short_jump〉)))
397       (sigma_compact_unsafe program labels p))
398       (∀i.i ≤ |program| → ∃pc.
399         bvt_lookup_opt … (bitvector_of_nat ? i) (\snd p) = Some ? 〈pc,short_jump〉))
400       (\fst p ≤ 2^16)         
401    ] ≝
402  λprogram.λlabels.
403  let final_policy ≝ foldl_strong (option Identifier × pseudo_instruction)
404  (λprefix.Σpolicy:ppc_pc_map.And (And (And (And
405    (not_jump_default prefix policy)
406    (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd policy) 〈0,short_jump〉) = 0))
407    (\fst policy = \fst (bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd policy) 〈0,short_jump〉)))
408    (sigma_compact_unsafe prefix labels policy))
409    (∀i.i ≤ |prefix| → ∃pc.
410      bvt_lookup_opt … (bitvector_of_nat ? i) (\snd policy) = Some ? 〈pc,short_jump〉))
411  program
412  (λprefix.λx.λtl.λprf.λp.
413   let 〈pc,sigma〉 ≝ pi1 ?? p in
414   let 〈label,instr〉 ≝ x in
415   let isize ≝ instruction_size_jmplen short_jump instr in
416   〈pc + isize, bvt_insert … (bitvector_of_nat 16 (S (|prefix|))) 〈pc+isize,short_jump〉 sigma〉
417  ) 〈0, bvt_insert ?? (bitvector_of_nat 16 0) 〈0,short_jump〉 (Stub ??)〉 in
418  if gtb (\fst (pi1 ?? final_policy)) 2^16 then
419    None ?
420  else
421    Some ? (pi1 ?? final_policy).
422[ / by I/
423| lapply p -p cases final_policy -final_policy #p #Hp #hg
424  @conj [ @Hp | @not_lt_to_le @ltb_false_to_not_lt @hg ]
425| @conj [ @conj [ @conj [ @conj
426  [ (* not_jump_default *) cases p -p #p cases p -p #pc #sigma #Hp
427    cases x in prf; #lbl #ins #prf #i >append_length <commutative_plus #Hi
428    normalize in Hi; normalize nodelta cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
429    [ >lookup_insert_miss
430      [ (* USE[pass]: not_jump_default *)
431        lapply (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp))) i Hi)
432        >nth_append_first
433        [ #H #H2 @H @H2
434        | @Hi
435        ]
436      | @bitvector_of_nat_abs
437        [ @(transitive_lt ??? Hi) @le_S_to_le]
438        [1,2: @(transitive_lt … (proj1 ?? (pi2 ?? program))) @le_S_S >prf >append_length
439          <plus_n_Sm @le_S_S @le_plus_n_r
440        | @lt_to_not_eq @le_S @Hi
441        ]
442      ]
443    | >Hi >lookup_insert_miss
444      [ #_ (* USE: everything is short *)
445        elim ((proj2 ?? Hp) (|prefix|) (le_n (|prefix|))) #pc #Hl
446        >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) @refl
447      | @bitvector_of_nat_abs
448        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length @le_plus_n_r
449        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm @le_S_S
450          @le_plus_n_r
451        | @lt_to_not_eq @le_n
452        ]
453      ]
454    ]
455  | (* 0 ↦ 0 *)
456    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
457    >lookup_insert_miss
458    [ (* USE[pass]: 0 ↦ 0 *)
459      @(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp))))
460    | @bitvector_of_nat_abs
461      [ / by /
462      | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length @le_S_S <plus_n_Sm
463        @le_S_S @le_plus_n_r
464      | @lt_to_not_eq / by /
465      ]
466    ]
467  ]
468  | (* fst p = pc *)
469    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
470    >append_length >(commutative_plus (|prefix|)) >lookup_insert_hit @refl
471  ]
472  | (* policy_compact_unsafe *) #i >append_length <commutative_plus #Hi normalize in Hi;
473    cases p -p #p cases p -p #fpc #sigma #Hp cases x #lbl #instr normalize nodelta
474    cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
475    [ >lookup_opt_insert_miss
476      [ >lookup_opt_insert_miss
477        [ (* USE[pass]: policy_compact_unsafe *)
478          lapply (proj2 ?? (proj1 ?? Hp) i Hi)
479          lapply (refl ? (bvt_lookup_opt … (bitvector_of_nat ? i) sigma))
480          cases (bvt_lookup_opt … (bitvector_of_nat ? i) sigma) in ⊢ (???% → %);
481          [ #_ normalize nodelta / by /
482          | #x cases x -x #pci #ji #EQi
483            lapply (refl ? (bvt_lookup_opt … (bitvector_of_nat ? (S i)) sigma))
484            cases (bvt_lookup_opt … (bitvector_of_nat ? (S i)) sigma) in ⊢ (???% → %);
485            [ #_ normalize nodelta / by /
486            | #x cases x -x #pcSi #jSi #EQSi normalize nodelta >nth_append_first
487              [ / by /
488              | @Hi
489              ]
490            ]
491          ]
492        ]
493      ]
494      [2: lapply (le_S_to_le … Hi) -Hi #Hi]
495      @bitvector_of_nat_abs
496      [1,4: @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <commutative_plus
497        @le_plus_a @Hi
498      |2,5: @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm
499        @le_S_S @le_plus_n_r
500      |3,6: @lt_to_not_eq @le_S_S @Hi
501      ]
502    | >lookup_opt_insert_miss
503      [ >Hi >lookup_opt_insert_hit normalize nodelta
504        (* USE: everything is short, fst p = pc *)
505        elim ((proj2 ?? Hp) (|prefix|) (le_n ?)) #pc #Hl
506        lapply (proj2 ?? (proj1 ?? (proj1 ?? Hp))) >Hl
507        >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) #EQ normalize nodelta >nth_append_second
508        [ <minus_n_n whd in match (nth ????); >EQ @refl
509        | @le_n
510        ]
511      | @bitvector_of_nat_abs
512        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >Hi >prf @le_S_S >append_length <commutative_plus
513          @le_plus_a @le_n
514        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm
515          @le_S_S @le_plus_n_r
516        | @lt_to_not_eq @le_S_S >Hi @le_n
517        ]
518      ]
519    ]
520  ]
521  | (* everything is short *) #i >append_length <commutative_plus #Hi normalize in Hi;
522    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
523    cases (le_to_or_lt_eq … Hi) -Hi #Hi
524    [ >lookup_opt_insert_miss
525      [ (* USE[pass]: everything is short *)
526        @((proj2 ?? Hp) i (le_S_S_to_le … Hi))
527      | @bitvector_of_nat_abs
528        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length @le_S_S
529          >commutative_plus @le_plus_a @le_S_S_to_le @Hi
530        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length <plus_n_Sm
531          @le_S_S @le_S_S @le_plus_n_r
532        | @lt_to_not_eq @Hi
533        ]
534      ]
535    | >Hi >lookup_opt_insert_hit @(ex_intro ?? (pc+instruction_size_jmplen short_jump instr))
536      @refl
537    ]
538  ]
539| @conj [ @conj [ @conj [ @conj
540  [ #i cases i
541    [ #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
542    | -i #i #Hi #Hj @⊥ @(absurd … Hi) @not_le_Sn_O
543    ]
544  ] ]
545  >lookup_insert_hit @refl
546  | #i cases i
547    [ #Hi @⊥ @(absurd … Hi) @le_to_not_lt @le_n
548    | -i #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
549    ]
550  ]
551  | #i cases i
552    [ #Hi >lookup_opt_insert_hit @(ex_intro ?? 0) @refl
553    | -i #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
554    ]
555  ]
556]
557qed.
558
559(* NOTE: we only compare the first elements here because otherwise the
560 * added = 0 → policy_equal property of jump_expansion_step doesn't hold:
561 * if we have not added anything to the pc, we only know the PC hasn't changed,
562 * there might still have been a short/medium jump change *)
563definition sigma_pc_equal ≝
564  λprogram:list labelled_instruction.λp1,p2:ppc_pc_map.
565  (∀n.n ≤ |program| →
566    \fst (bvt_lookup … (bitvector_of_nat 16 n) (\snd p1) 〈0,short_jump〉) =
567    \fst (bvt_lookup … (bitvector_of_nat 16 n) (\snd p2) 〈0,short_jump〉)).
568
569definition sigma_jump_equal ≝
570  λprogram:list labelled_instruction.λp1,p2:ppc_pc_map.
571  (∀n.n < |program| →
572    \snd (bvt_lookup … (bitvector_of_nat 16 n) (\snd p1) 〈0,short_jump〉) =
573    \snd (bvt_lookup … (bitvector_of_nat 16 n) (\snd p2) 〈0,short_jump〉)).
574   
575definition nec_plus_ultra ≝
576  λprogram:list labelled_instruction.λp:ppc_pc_map.
577  ¬(∀i.i < |program| → is_jump (\snd (nth i ? program 〈None ?, Comment []〉)) →
578  \snd (bvt_lookup … (bitvector_of_nat 16 i) (\snd p) 〈0,short_jump〉) = long_jump).
579 
580(*include alias "common/Identifiers.ma".*)
581include alias "ASM/BitVector.ma".
582include alias "basics/lists/list.ma".
583include alias "arithmetics/nat.ma".
584include alias "basics/logic.ma".
585
586lemma jump_length_equal_max: ∀a,b,i.
587  is_jump i → instruction_size_jmplen (max_length a b) i = instruction_size_jmplen a i →
588  (max_length a b) = a.
589 #a #b #i cases i
590 [1: #pi cases pi
591   try (#x #y #H #EQ) try (#x #H #EQ) try (#H #EQ) cases H
592   cases a in EQ; cases b #EQ try %
593   try (normalize in EQ; destruct(EQ) @False)
594   try (lapply EQ @(subaddressing_mode_elim … x) #w #EQ normalize in EQ; destruct(EQ) @False)
595   lapply EQ -EQ cases x * #a1 #a2 @(subaddressing_mode_elim … a2) #w
596   try (#EQ normalize in EQ; destruct(EQ) @False)
597   @(subaddressing_mode_elim … a1) #w
598   #EQ normalize in EQ; destruct(EQ)
599  |2,3,6: #x [3: #y] #H cases H
600  |4,5: #id #_ cases a cases b #H try % normalize in H; destruct(H)
601 ]
602qed.
603
604lemma jump_length_le_max: ∀a,b,i.is_jump i →
605  instruction_size_jmplen a i ≤ instruction_size_jmplen (max_length a b) i.
606 #a #b #i cases i
607 [2,3,6: #x [3: #y] #H cases H
608 |4,5: #id #_ cases a cases b / by le_n/
609 |1: #pi cases pi
610    try (#x #y #H) try (#x #H) try (#H) cases H
611    -H cases a cases b @leb_true_to_le try %
612    try (@(subaddressing_mode_elim … x) #w % @False)
613    cases x * #a1 #a2 @(subaddressing_mode_elim … a2) #w try %
614    @(subaddressing_mode_elim … a1) #w %
615 ]
616qed.
617
618lemma equal_compact_unsafe_compact: ∀program:(Σl.(S (|l|)) < 2^16 ∧ is_well_labelled_p l).
619  ∀old_sigma.∀sigma.
620  sigma_pc_equal program old_sigma sigma →
621  sigma_safe program (create_label_map program) 0 old_sigma sigma →
622  sigma_compact_unsafe program (create_label_map program) sigma →
623  sigma_compact program (create_label_map program) sigma.
624  #program cases program -program #program #Hprogram #old_sigma #sigma #Hequal
625  #Hsafe #Hcp_unsafe #i #Hi
626  lapply (Hcp_unsafe i Hi) lapply (Hsafe i Hi)
627  lapply (refl ? (lookup_opt … (bitvector_of_nat ? i) (\snd sigma)))
628  cases (lookup_opt … (bitvector_of_nat ? i) (\snd sigma)) in ⊢ (???% → %);
629  [ / by /
630  | #x cases x -x #x1 #x2 #EQ
631    cases (lookup_opt … (bitvector_of_nat ? (S i)) (\snd sigma))
632    [ / by /
633    | #y cases y -y #y1 #y2 normalize nodelta #H #H2
634      cut (instruction_size_jmplen x2
635       (\snd (nth i ? program 〈None ?, Comment []〉)) =
636       instruction_size … (bitvector_of_nat ? i)
637       (\snd (nth i ? program 〈None ?, Comment []〉)))
638      [5: #H3 <H3 @H2
639      |4: whd in match (instruction_size_jmplen ??);
640          whd in match (instruction_size …);
641          whd in match (assembly_1_pseudoinstruction …);
642          whd in match (expand_pseudo_instruction …);
643          normalize nodelta whd in match (append …) in H;
644          lapply (refl ? (nth i ? program 〈None ?, Comment []〉)) lapply H
645          cases (nth i ? program 〈None ?,Comment []〉) in ⊢ (% → ???% → %);
646          #lbl #instr cases instr
647          [2,3,6: #x [3: #y] normalize nodelta #H #_ %
648          |4,5: #x >(lookup_opt_lookup_hit … EQ 〈0,short_jump〉) #Hj #Heq
649            lapply (Hj x (refl ? x)) -Hj normalize nodelta
650            >add_bitvector_of_nat_plus <(plus_n_Sm i 0) <plus_n_O
651            cases x2 normalize nodelta
652            [1,4: whd in match short_jump_cond; normalize nodelta
653              cut (lookup_def ?? (create_label_map program) x 0 ≤ (|program|))
654              [1,3: cases (create_label_map program) #clm #Hclm
655                @le_S_to_le @(proj2 ?? (Hclm x ?))
656                [1: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Jmp x) ??)
657                |2: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Call x) ??)]
658                [1,4: >nat_of_bitvector_bitvector_of_nat_inverse
659                  [2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S] @Hi
660                |2,5: whd in match fetch_pseudo_instruction; normalize nodelta
661                  >nth_safe_nth
662                  [1,3: >nat_of_bitvector_bitvector_of_nat_inverse
663                    [1,3: >Heq / by refl/
664                    |2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S @Hi
665                    ]
666                  ]
667                |3,6: / by /
668                ]
669              |2,4: #H >(le_to_leb_true … H) normalize nodelta <plus_n_O
670              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
671              #result #flags normalize nodelta
672              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
673              cases (get_index' bool 2 0 flags) normalize nodelta
674              [3,4: #H cases (proj2 ?? H)
675              |1,2: cases (eq_bv 9 upper ?)
676                [2,4: #H lapply (proj1 ?? H) #H3 destruct (H3)
677                |1,3: #_ normalize nodelta @refl
678                ]
679              ]
680              ]
681            |2,5: whd in match short_jump_cond; whd in match absolute_jump_cond;
682              cut (lookup_def ?? (create_label_map program) x 0 ≤ (|program|))
683              [1,3: cases (create_label_map program) #clm #Hclm
684                @le_S_to_le @(proj2 ?? (Hclm x ?))
685                [1: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Jmp x) ??)
686                |2: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Call x) ??)]
687                [1,4: >nat_of_bitvector_bitvector_of_nat_inverse
688                  [2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S] @Hi
689                |2,5: whd in match fetch_pseudo_instruction; normalize nodelta
690                  >nth_safe_nth
691                  [1,3: >nat_of_bitvector_bitvector_of_nat_inverse
692                    [1,3: >Heq / by refl/
693                    |2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S @Hi
694                    ]
695                  ]
696                |3,6: / by /
697                ]
698              |2,4: #H >(le_to_leb_true … H) normalize nodelta <plus_n_O
699              normalize nodelta cases (vsplit bool 5 11 ?) #addr1 #addr2
700              cases (vsplit bool 5 11 ?) #pc1 #pc2 normalize nodelta
701              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
702              #result #flags normalize nodelta
703              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
704              cases (get_index' bool 2 0 flags) normalize nodelta
705              #H >(proj2 ?? (proj1 ?? H)) >(proj1 ?? (proj1 ?? H)) normalize nodelta @refl
706              ]
707            |3,6: whd in match short_jump_cond; whd in match absolute_jump_cond;
708              cut (lookup_def ?? (create_label_map program) x 0 ≤ (|program|))
709              [1,3: cases (create_label_map program) #clm #Hclm
710                @le_S_to_le @(proj2 ?? (Hclm x ?))
711                [1: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Jmp x) ??)
712                |2: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Call x) ??)]
713                [1,4: >nat_of_bitvector_bitvector_of_nat_inverse
714                  [2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S] @Hi
715                |2,5: whd in match fetch_pseudo_instruction; normalize nodelta
716                  >nth_safe_nth
717                  [1,3: >nat_of_bitvector_bitvector_of_nat_inverse
718                    [1,3: >Heq / by refl/
719                    |2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S @Hi
720                    ]
721                  ]
722                |3,6: / by /
723                ]
724              |2,4: #H >(le_to_leb_true … H) normalize nodelta <plus_n_O
725              cases (vsplit bool 5 11 ?) #addr1 #addr2
726              cases (vsplit bool 5 11 ?) #pc1 #pc2 normalize nodelta
727              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
728              #result #flags normalize nodelta
729              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
730              cases (get_index' bool 2 0 flags) normalize nodelta
731              #H >(proj1 ?? H) >(proj2 ?? H) normalize nodelta @refl
732              ]
733            ]
734          |1: cut (∀A,B,ab.fst A B ab = (let 〈a,b〉 ≝ ab in a))
735              [#A #B * / by refl/] #fst_foo
736              cut (∀x.
737                    instruction_has_label x (\snd  (nth i labelled_instruction program 〈None (identifier ASMTag),Comment []〉)) →
738               lookup_def ?? (create_label_map program) x 0 ≤ (|program|))
739              [#x #Heq cases (create_label_map program) #clm #Hclm
740               @le_S_to_le @(proj2 ?? (Hclm x ?))
741               @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (\snd (nth i ? program 〈None ?, Comment []〉)) ??)
742               [ >nat_of_bitvector_bitvector_of_nat_inverse
743                 [2: @(transitive_lt … (proj1 ?? Hprogram)) @le_S] @Hi
744               | whd in match fetch_pseudo_instruction; normalize nodelta
745                 >nth_safe_nth
746                 [ >nat_of_bitvector_bitvector_of_nat_inverse
747                   [ @pair_elim // ]
748                   @(transitive_lt … (proj1 ?? Hprogram)) @le_S @Hi ]
749                 | assumption ]] #lookup_in_program
750              -H #pi cases pi
751              try (#y #x #H #Heq) try (#x #H #Heq) try (#H #Heq) try %
752              normalize nodelta >(lookup_opt_lookup_hit … EQ 〈0,short_jump〉) in H;
753              #Hj lapply (Hj x (refl ? x)) -Hj
754              whd in match expand_relative_jump; normalize nodelta
755              whd in match expand_relative_jump_internal; normalize nodelta
756              whd in match expand_relative_jump_unsafe; normalize nodelta
757              whd in match expand_relative_jump_internal_unsafe;
758              normalize nodelta >(add_bitvector_of_nat_plus ? i 1)
759              <(plus_n_Sm i 0) <plus_n_O <plus_n_O cases x2 normalize nodelta
760              [1,4,7,10,13,16,19,22,25:
761                >fst_foo @pair_elim #sj_possible #disp #H #H2
762                @(pair_replace ?????????? (eq_to_jmeq … H))
763                [1,3,5,7,9,11,13,15,17: >(le_to_leb_true … (lookup_in_program …))
764                  try % >Heq % ]
765                >(proj1 ?? H2) try (@refl) normalize nodelta
766                [1,2,3,5: @(subaddressing_mode_elim … y) #w %
767                | cases y * #sth #sth2 @(subaddressing_mode_elim … sth)
768                  @(subaddressing_mode_elim … sth2) #x [3,4: #x2] %
769                ]
770              |2,5,8,11,14,17,20,23,26: ** #_ #_ #abs cases abs
771              ]
772              * #H #_ >fst_foo in H; @pair_elim #sj_possible #disp #H
773              @(pair_replace ?????????? (eq_to_jmeq … H))
774                [1,3,5,7,9,11,13,15,17: >(le_to_leb_true … (lookup_in_program …))
775                  try % >Heq % ]
776                #H2 >H2 try (@refl) normalize nodelta
777                [1,2,3,5: @(subaddressing_mode_elim … y) #w %
778                | cases y * #sth #sth2 @(subaddressing_mode_elim … sth2) #w
779                  [1,2: %] whd in match (map ????); whd in match (flatten ??);
780                  whd in match (map ????) in ⊢ (???%); whd in match (flatten ??) in ⊢ (???%);
781                  >length_append >length_append %]]]]]
782qed.
783
784lemma instruction_size_irrelevant: ∀i.
785  ¬is_jump i → ∀j1,j2.instruction_size_jmplen j1 i = instruction_size_jmplen j2 i.
786 #i cases i
787 [2,3,6: #x [3: #y] #Hj #j1 #j2 %
788 |4,5: #x #Hi cases Hi
789 |1: #pi cases pi try (#x #y #Hj #j1 #j2) try (#y #Hj #j1 #j2) try (#Hj #j1 #j2)
790     try % cases Hj ]
791qed.
Note: See TracBrowser for help on using the repository browser.