[3] | 1 | (**************************************************************************) |
---|
| 2 | (* ___ *) |
---|
| 3 | (* ||M|| *) |
---|
| 4 | (* ||A|| A project by Andrea Asperti *) |
---|
| 5 | (* ||T|| *) |
---|
| 6 | (* ||I|| Developers: *) |
---|
| 7 | (* ||T|| The HELM team. *) |
---|
| 8 | (* ||A|| http://helm.cs.unibo.it *) |
---|
| 9 | (* \ / *) |
---|
| 10 | (* \ / This file is distributed under the terms of the *) |
---|
| 11 | (* v GNU General Public License Version 2 *) |
---|
| 12 | (* *) |
---|
| 13 | (**************************************************************************) |
---|
| 14 | |
---|
[1993] | 15 | include "common/FrontEndMem.ma". |
---|
[3] | 16 | |
---|
| 17 | |
---|
| 18 | (* The memory model stores the entire value regardless of whether it fits in |
---|
| 19 | the amount of memory used. Fortunately, it checks the size of the memory |
---|
| 20 | access, preventing you reading back more information than the memory can |
---|
| 21 | store. *) |
---|
[1988] | 22 | definition store0 := empty. |
---|
[2176] | 23 | definition store1block : mem × Σb:block.? ≝ alloc store0 0 4 XData. |
---|
[717] | 24 | definition store1 : mem ≝ fst ?? store1block. |
---|
[1875] | 25 | definition block :block := pi1 … (snd ?? store1block). |
---|
[12] | 26 | |
---|
[3] | 27 | (* write a value *) |
---|
[717] | 28 | definition store2 : mem. |
---|
[2176] | 29 | letin r ≝ (store (ASTint I16 Unsigned) store1 (mk_pointer block zero_offset) (Vint I16 (repr ? 1))) |
---|
[12] | 30 | |
---|
[1634] | 31 | lapply (refl ? r); elim r in ⊢ (???% → ?); [ whd in ⊢ (??%? → ?); #H destruct |
---|
[717] | 32 | | #rr #_ @rr ] qed. |
---|
[12] | 33 | |
---|
[3] | 34 | (* overwrite the first byte of the value *) |
---|
[717] | 35 | definition store3 : mem. |
---|
[2176] | 36 | letin r ≝ (store (ASTint I8 Unsigned) store2 (mk_pointer block zero_offset) (Vint I8 (repr ? 1))) |
---|
[12] | 37 | |
---|
[1634] | 38 | lapply (refl ? r) elim r in ⊢ (???% → ?); [ whd in ⊢ (??%? → ?); #H destruct |
---|
[717] | 39 | | #rr #_ @ rr ] qed. |
---|
[3] | 40 | |
---|
[1875] | 41 | (* This is what happened under the original CompCert 1.6 ported memory model: |
---|
| 42 | The size checking rejects the read and gives us Some Vundef. |
---|
[2176] | 43 | example vl_undef: load (ASTint I16 Signed) store3 (mk_pointer block zero_offset) = Some ? Vundef. @refl qed. |
---|
[1875] | 44 | *) |
---|
| 45 | (* Now we do byte-wise values and get to see the altered integer. *) |
---|
[2176] | 46 | example vl_changed: load (ASTint I16 Signed) store3 (mk_pointer block zero_offset) = Some ? (Vint I16 (repr ? 257)). @refl qed. |
---|
[3] | 47 | |
---|
[961] | 48 | (* The read is successful and returns the value. *) |
---|
[2176] | 49 | example vl_ok': load (ASTint I8 Unsigned) store3 (mk_pointer block zero_offset) = Some ? (Vint I8 (repr ? 1)). // qed. |
---|
[3] | 50 | |
---|
| 51 | (* NB: Double frees are allowed by the memory model (although not necessarily |
---|
| 52 | by the language). *) |
---|
[1988] | 53 | definition store4 := free store3 block. |
---|
| 54 | definition store5 : mem. letin r ≝ (free store3 block); whd in r; @r qed. |
---|
[3] | 55 | |
---|
| 56 | (* No ordering is imposed on deallocation (even though alloc and free are only used for |
---|
| 57 | stack memory in the semantics. *) |
---|
[1988] | 58 | definition storeA := empty. |
---|
[2176] | 59 | definition storeBblkB := alloc storeA 0 4 XData. |
---|
| 60 | definition storeCblkC := alloc (fst ?? storeBblkB) 0 8 XData. |
---|
[1988] | 61 | definition storeD := free (fst ?? storeCblkC) (snd ?? storeBblkB). |
---|
| 62 | definition storeE : mem. letin r ≝ (free storeD (snd ?? storeCblkC)). |
---|
[1634] | 63 | whd in r; @r qed. |
---|
[3] | 64 | |
---|
[1875] | 65 | (* Access to the "object representation" (as the C standard puts it) was not specified, |
---|
| 66 | but can now split up integer values into bytes. The front-end interface |
---|
| 67 | doesn't currently expose parts of pointers. *) |
---|
[1988] | 68 | definition storeI := empty. |
---|
[2176] | 69 | definition storeIIblk := alloc storeI 0 4 XData. |
---|
[717] | 70 | definition storeIII : mem. |
---|
[2176] | 71 | letin r ≝ (store (ASTint I32 Unsigned) (fst ?? storeIIblk) (mk_pointer (snd ?? storeIIblk) zero_offset) (Vint I32 (repr ? 1))); |
---|
[1634] | 72 | lapply (refl ? r) elim r in ⊢ (???% → ?); [ whd in ⊢ (??%? → ?); #H destruct |
---|
[717] | 73 | | #rr #_ @rr ] qed. |
---|
[2176] | 74 | definition byte := load (ASTint I8 Unsigned) storeIII (mk_pointer (snd ?? storeIIblk) zero_offset). |
---|
[1875] | 75 | example byteundef: byte = Some ? (Vint I8 (repr ? 0)). // qed. (* :) *) |
---|
[2176] | 76 | |
---|