forked from VSharp-team/VSharp
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMessages.fs
More file actions
409 lines (341 loc) · 11.1 KB
/
Messages.fs
File metadata and controls
409 lines (341 loc) · 11.1 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
module VSharp.ML.GameServer.Messages
open System.Text
open System.Text.Json
open System.Text.Json.Serialization
open VSharp
type searcher =
| BFS = 0
| DFS = 1
[<Struct>]
type RawInputMessage =
val MessageType: string
val MessageBody: string
[<JsonConstructor>]
new(messageType, messageBody) =
{ MessageBody = messageBody
MessageType = messageType }
type IRawOutgoingMessageBody = interface end
[<Measure>]
type pathConditionVertexId
type pathConditionVertexType =
| UnaryMinus = 0
| BitwiseNot = 1
| BitwiseAnd = 2
| BitwiseOr = 3
| BitwiseXor = 4
| LogicalNot = 5
| LogicalAnd = 6
| LogicalOr = 7
| LogicalXor = 8
| Equal = 9
| NotEqual = 10
| Greater = 11
| Greater_Un = 12
| Less = 13
| Less_Un = 14
| GreaterOrEqual = 15
| GreaterOrEqual_Un = 16
| LessOrEqual = 17
| LessOrEqual_Un = 18
| Add = 19
| AddNoOvf = 20
| AddNoOvf_Un = 21
| Subtract = 22
| SubNoOvf = 23
| SubNoOvf_Un = 24
| Divide = 25
| Divide_Un = 26
| Multiply = 27
| MultiplyNoOvf = 28
| MultiplyNoOvf_Un = 29
| Remainder = 30
| Remainder_Un = 31
| ShiftLeft = 32
| ShiftRight = 33
| ShiftRight_Un = 34
| Nop = 35
| Concrete = 36
| Constant = 37
| Struct = 39
| HeapRef = 40
| Ref = 41
| Ptr = 42
| Slice = 43
| Ite = 44
| StandardFunctionApplication = 45
| Cast = 46
| Combine = 47
| PathConditionRoot = 48
[<Struct>]
type PathConditionVertex =
val Id: uint<pathConditionVertexId>
val Type: pathConditionVertexType
val Children: array<uint<pathConditionVertexId>>
new(id, pathConditionVertexType, children) =
{ Id = id
Type = pathConditionVertexType
Children = children }
[<Measure>]
type test
[<Measure>]
type error
[<Measure>]
type step
[<Measure>]
type percent
[<Measure>]
type basicBlockGlobalId
[<Measure>]
type instruction
[<Struct>]
type GameOverMessageBody =
interface IRawOutgoingMessageBody
val ActualCoverage: uint<percent>
val TestsCount: uint32<test>
val StepsCount: uint32<step>
val ErrorsCount: uint32<error>
new(actualCoverage, testsCount, stepsCount, errorsCount) =
{ ActualCoverage = actualCoverage
TestsCount = testsCount
StepsCount = stepsCount
ErrorsCount = errorsCount }
[<Struct>]
type RawOutgoingMessage =
val MessageType: string
val MessageBody: obj
new(messageType, messageBody) =
{ MessageBody = messageBody
MessageType = messageType }
[<Measure>]
type stateId
[<Struct>]
type GameStep =
val StateId: uint<stateId>
[<JsonConstructor>]
new(stateId) = { StateId = stateId }
[<Struct>]
type StateHistoryElem =
val GraphVertexId: uint<basicBlockGlobalId>
val NumOfVisits: uint
val StepWhenVisitedLastTime: uint<step>
new(graphVertexId, numOfVisits, stepWhenVisitedLastTime) =
{ GraphVertexId = graphVertexId
NumOfVisits = numOfVisits
StepWhenVisitedLastTime = stepWhenVisitedLastTime }
[<Struct>]
type State =
val Id: uint<stateId>
val Position: uint<byte_offset> // to basic block id
val PathCondition: PathConditionVertex
val VisitedAgainVertices: uint
val VisitedNotCoveredVerticesInZone: uint
val VisitedNotCoveredVerticesOutOfZone: uint
val StepWhenMovedLastTime: uint<step>
val InstructionsVisitedInCurrentBlock: uint<instruction>
val History: array<StateHistoryElem>
val Children: array<uint<stateId>>
new
(
id,
position,
pathCondition,
visitedAgainVertices,
visitedNotCoveredVerticesInZone,
visitedNotCoveredVerticesOutOfZone,
stepWhenMovedLastTime,
instructionsVisitedInCurrentBlock,
history,
children
) =
{ Id = id
Position = position
PathCondition = pathCondition
VisitedAgainVertices = visitedAgainVertices
VisitedNotCoveredVerticesInZone = visitedNotCoveredVerticesInZone
VisitedNotCoveredVerticesOutOfZone = visitedNotCoveredVerticesOutOfZone
StepWhenMovedLastTime = stepWhenMovedLastTime
InstructionsVisitedInCurrentBlock = instructionsVisitedInCurrentBlock
History = history
Children = children }
[<Struct>]
type GameMapVertex =
val Id: uint<basicBlockGlobalId>
val InCoverageZone: bool
val BasicBlockSize: uint // instructions
val CoveredByTest: bool
val VisitedByState: bool
val TouchedByState: bool
val ContainsCall: bool
val ContainsThrow: bool
val States: uint<stateId>[]
new
(
id,
inCoverageZone,
basicBlockSize,
containsCall,
containsThrow,
coveredByTest,
visitedByState,
touchedByState,
states
) =
{ Id = id
InCoverageZone = inCoverageZone
BasicBlockSize = basicBlockSize
CoveredByTest = coveredByTest
VisitedByState = visitedByState
TouchedByState = touchedByState
ContainsCall = containsCall
ContainsThrow = containsThrow
States = states }
[<Struct>]
type GameEdgeLabel =
val Token: int
new(token) = { Token = token }
[<Struct>]
type GameMapEdge =
val VertexFrom: uint<basicBlockGlobalId>
val VertexTo: uint<basicBlockGlobalId>
val Label: GameEdgeLabel
new(vFrom, vTo, label) =
{ VertexFrom = vFrom
VertexTo = vTo
Label = label }
[<Struct>]
type GameState =
interface IRawOutgoingMessageBody
val GraphVertices: GameMapVertex[]
val States: State[]
val PathConditionVertices: PathConditionVertex[]
val Map: GameMapEdge[]
new(graphVertices, states, pathConditionVertices, map) =
{ GraphVertices = graphVertices
States = states
PathConditionVertices = pathConditionVertices
Map = map }
member this.ToDot file drawHistoryEdges =
let vertices = ResizeArray<_>()
let edges = ResizeArray<_>()
for v in this.GraphVertices do
let color =
if v.CoveredByTest then "green"
elif v.VisitedByState then "red"
elif v.TouchedByState then "yellow"
else "white"
vertices.Add($"{v.Id} [label={v.Id}, shape=box, style=filled, fillcolor={color}]")
for s in v.States do
edges.Add($"99{s}00 -> {v.Id} [label=L]")
for s in this.States do
vertices.Add($"99{s.Id}00 [label={s.Id}, shape=circle]")
for v in s.Children do
edges.Add($"99{s.Id}00 -> 99{v}00 [label=ch]")
if drawHistoryEdges then
for v in s.History do
edges.Add($"99{s.Id}00 -> {v.GraphVertexId} [label={v.NumOfVisits}]")
for e in this.Map do
edges.Add($"{e.VertexFrom}->{e.VertexTo}[label={e.Label.Token}]")
let dot =
seq {
"digraph g{"
yield! vertices
yield! edges
"}"
}
System.IO.File.WriteAllLines(file, dot)
[<Measure>]
type coverageReward
[<Measure>]
type visitedInstructionsReward
[<Measure>]
type maxPossibleReward
[<Struct>]
type MoveReward =
val ForCoverage: uint<coverageReward>
val ForVisitedInstructions: uint<visitedInstructionsReward>
new(forCoverage, forVisitedInstructions) =
{ ForCoverage = forCoverage
ForVisitedInstructions = forVisitedInstructions }
[<Struct>]
type Reward =
interface IRawOutgoingMessageBody
val ForMove: MoveReward
val MaxPossibleReward: uint<maxPossibleReward>
new(forMove, maxPossibleReward) =
{ ForMove = forMove
MaxPossibleReward = maxPossibleReward }
new(forCoverage, forVisitedInstructions, maxPossibleReward) =
{ ForMove = MoveReward(forCoverage, forVisitedInstructions)
MaxPossibleReward = maxPossibleReward }
type Feedback =
| MoveReward of Reward
| IncorrectPredictedStateId of uint<stateId>
| ServerError of string
[<Struct>]
type GameMap =
val StepsToPlay: uint<step>
val StepsToStart: uint<step>
[<JsonConverter(typeof<JsonStringEnumConverter>)>]
val DefaultSearcher: searcher
val AssemblyFullName: string
val NameOfObjectToCover: string
val MapName: string
new(stepsToPlay, stepsToStart, assembly, defaultSearcher, objectToCover) =
{ StepsToPlay = stepsToPlay
StepsToStart = stepsToStart
AssemblyFullName = assembly
NameOfObjectToCover = objectToCover
DefaultSearcher = defaultSearcher
MapName = $"{objectToCover}_{defaultSearcher}_{stepsToStart}" }
[<JsonConstructor>]
new(stepsToPlay, stepsToStart, assemblyFullName, defaultSearcher, nameOfObjectToCover, mapName) =
{ StepsToPlay = stepsToPlay
StepsToStart = stepsToStart
AssemblyFullName = assemblyFullName
DefaultSearcher = defaultSearcher
NameOfObjectToCover = nameOfObjectToCover
MapName = mapName }
type InputMessage =
| ServerStop
| Start of GameMap
| Step of GameStep
[<Struct>]
type ServerErrorMessageBody =
interface IRawOutgoingMessageBody
val ErrorMessage: string
new(errorMessage) = { ErrorMessage = errorMessage }
[<Struct>]
type IncorrectPredictedStateIdMessageBody =
interface IRawOutgoingMessageBody
val StateId: uint<stateId>
new(stateId) = { StateId = stateId }
type OutgoingMessage =
| GameOver of uint<percent> * uint32<test> * uint32<step> * uint32<error>
| MoveReward of Reward
| IncorrectPredictedStateId of uint<stateId>
| ReadyForNextStep of GameState
| ServerError of string
let (|MsgTypeStart|MsgTypeStep|MsgStop|) (str: string) =
let normalized = str.ToLowerInvariant().Trim()
if normalized = "start" then MsgTypeStart
elif normalized = "step" then MsgTypeStep
elif normalized = "stop" then MsgStop
else failwithf $"Unexpected message type %s{str}"
let deserializeInputMessage (messageData: byte[]) =
let rawInputMessage =
let str = Encoding.UTF8.GetString messageData
str |> JsonSerializer.Deserialize<RawInputMessage>
match rawInputMessage.MessageType with
| MsgStop -> ServerStop
| MsgTypeStart -> Start(JsonSerializer.Deserialize<GameMap> rawInputMessage.MessageBody)
| MsgTypeStep -> Step(JsonSerializer.Deserialize<GameStep>(rawInputMessage.MessageBody))
let serializeOutgoingMessage (message: OutgoingMessage) =
match message with
| GameOver(actualCoverage, testsCount, stepsCount, errorsCount) ->
RawOutgoingMessage("GameOver", box (GameOverMessageBody(actualCoverage, testsCount, stepsCount, errorsCount)))
| MoveReward reward -> RawOutgoingMessage("MoveReward", reward)
| IncorrectPredictedStateId stateId ->
RawOutgoingMessage("IncorrectPredictedStateId", IncorrectPredictedStateIdMessageBody stateId)
| ReadyForNextStep state -> RawOutgoingMessage("ReadyForNextStep", state)
| ServerError errorMessage -> RawOutgoingMessage("ServerError", ServerErrorMessageBody errorMessage)
|> JsonSerializer.Serialize