-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.scala
170 lines (147 loc) · 9.01 KB
/
File.scala
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
/* Copyright 2021 Ateleris GmbH, Brugg */
import contexts._
import definitions.FilesystemDefinitions._
import definitions.PartitionDefinitions._
import BlockCountInvariant._
import stainless.annotation._
import stainless.lang.StaticChecks.{assert => assert2, _}
import stainless.lang.{BooleanDecorations, ghost, old, snapshot, unfold}
import stainless.math.BitVectors._
import stainless.proof._
object File {
@cCode.inline @inlineOnce @opaque
def freeBlocksBitMapOr(cbIdx: Int, k: Int, v: UInt32)(implicit memFSContext: MemFilesystemContext, flashContext: FlashContext): Unit = {
require(0 <= cbIdx && cbIdx < TOTAL_NUMBER_OF_CONTROL_BLOCKS.toInt)
require(0 <= k && k < (FILES_PER_FILECB.toInt + 31) / 32)
require { memFSContext.memFilesystem.ensureLength(); true }
require(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
ghost {
val cb0 = snapshot(memFSContext.memFilesystem.controlBlocks(cbIdx))
cb0.ensureInvariant()
cb0.freeBlocksBitMap(k) |= v
// blockCountInvariantFreeBlocksBitMap(memFSContext.memFilesystem.controlBlocks, flashContext, cbIdx, k, cb0.freeBlocksBitMap(k) | v)
memFSContext.memFilesystem.ensureLength()
memFSContext.memFilesystem.controlBlocks(cbIdx).ensureBitMapLength()
blockCountInvariantUnchanged(memFSContext.memFilesystem.controlBlocks, flashContext, cbIdx, cb0)
}
memFSContext.memFilesystem.controlBlocks(cbIdx).freeBlocksBitMap(k) |= v
}.ensuring(_ =>
{
memFSContext.memFilesystem.ensureLength()
blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext)
}
)
@cCode.`export` @inlineOnce @opaque
private def setBlockAsFree(cbIdx: Int, blockNumber: UInt32)(implicit memFSContext: MemFilesystemContext, flashContext: FlashContext) = {
require(blockNumber < FILES_PER_FILECB)
require(0 <= cbIdx && cbIdx < TOTAL_NUMBER_OF_CONTROL_BLOCKS.toInt)
require { memFSContext.memFilesystem.ensureLength(); true }
require(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
ghost {
memFSContext.memFilesystem.controlBlocks(cbIdx).ensureFileCatalogLength()
}
val k = blockNumber.toInt / BITS_IN_INT32.toInt
val v = 1 << (blockNumber % BITS_IN_INT32)
freeBlocksBitMapOr(cbIdx, k, v)
ghost {
memFSContext.memFilesystem.ensureLength()
memFSContext.memFilesystem.controlBlocks(cbIdx).ensureFileCatalogLength()
}
val oldStatus = memFSContext.memFilesystem.controlBlocks(cbIdx).fileCatalog(blockNumber).blockStatus
ghost {
atLeastOneBlock(memFSContext.memFilesystem.controlBlocks, oldStatus, cbIdx, blockNumber.toInt)
smallCount(memFSContext.memFilesystem.controlBlocks, flashContext)
memFSContext.memFilesystem.ensureLength()
unfold(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
}
updateStatsBlockTransition(oldStatus, E_FILE_BLOCK_STAT_FREE)
@ghost val memFSContext0 = snapshot(memFSContext)
{
assert2 { // we make an `assert` so that all this code disappears for the solver after the `blockStatus` update
memFSContext.memFilesystem.ensureInvariant()
val cb1 = snapshot(memFSContext.memFilesystem.controlBlocks(cbIdx))
cb1.ensureInvariant()
cb1.fileCatalog(blockNumber).blockStatus = E_FILE_BLOCK_STAT_FREE
memFSContext.memFilesystem.controlBlocks(cbIdx).ensureFileCatalogLength()
true
}
memFSContext.memFilesystem.controlBlocks(cbIdx).fileCatalog(blockNumber).blockStatus = E_FILE_BLOCK_STAT_FREE
}
ghost {
memFSContext0.memFilesystem.ensureLength()
unfold(blockCountInvariant(memFSContext0.memFilesystem.controlBlocks, flashContext))
changeStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE, cbIdx, blockNumber.toInt, E_FILE_BLOCK_STAT_FREE)
changeStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR, cbIdx, blockNumber.toInt, E_FILE_BLOCK_STAT_FREE)
changeStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD, cbIdx, blockNumber.toInt, E_FILE_BLOCK_STAT_FREE)
if (oldStatus == E_FILE_BLOCK_STAT_FILE) {
memFSContext.memFilesystem.ensureLength()
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE) - 1)
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR))
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD))
unfold(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
check(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
} else if (oldStatus == E_FILE_BLOCK_STAT_ERROR) {
memFSContext.memFilesystem.ensureLength()
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE))
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR) - 1)
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD))
unfold(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
check(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
} else if (oldStatus == E_FILE_BLOCK_STAT_BAD) {
memFSContext.memFilesystem.ensureLength()
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE))
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR))
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD) - 1)
unfold(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
check(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
} else {
memFSContext.memFilesystem.ensureLength()
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_FILE))
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_ERROR))
assert(countStatus(memFSContext.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD) == countStatus(memFSContext0.memFilesystem.controlBlocks, E_FILE_BLOCK_STAT_BAD))
unfold(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
check(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
}
memFSContext.memFilesystem.ensureLength()
unfold(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
check(blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext))
}
}.ensuring(_ =>
{
memFSContext.memFilesystem.ensureLength()
blockCountInvariant(memFSContext.memFilesystem.controlBlocks, flashContext)
} &&& {
memFSContext.memFilesystem.ensureLength()
old(memFSContext).memFilesystem.ensureLength()
true
}
)
@inline
private def updateStatsBlockTransition(
from: File_blockStatus_e,
to: File_blockStatus_e
)(implicit
memFSContext: MemFilesystemContext,
flashContext: FlashContext
) = {
import flashContext._
require((from == E_FILE_BLOCK_STAT_FILE) ==> usedBlockCount > 0)
require((from == E_FILE_BLOCK_STAT_ERROR) ==> errorBlockCount > 0)
require(from == E_FILE_BLOCK_STAT_BAD ==> badBlockCount > 0)
require(usedBlockCount <= MAX_NUMBER_OF_FILES)
require(errorBlockCount <= MAX_NUMBER_OF_FILES)
require(badBlockCount <= MAX_NUMBER_OF_FILES)
if (from == E_FILE_BLOCK_STAT_FILE)
usedBlockCount = usedBlockCount - 1
else if (from == E_FILE_BLOCK_STAT_ERROR)
errorBlockCount = errorBlockCount - 1
else if (from == E_FILE_BLOCK_STAT_BAD)
badBlockCount = badBlockCount - 1
if (to == E_FILE_BLOCK_STAT_FILE)
usedBlockCount = usedBlockCount + 1
else if (to == E_FILE_BLOCK_STAT_ERROR)
errorBlockCount = errorBlockCount + 1
else if (to == E_FILE_BLOCK_STAT_BAD)
badBlockCount = badBlockCount + 1
}
}