Skip to content

Commit f44b571

Browse files
committed
[style] fixed style
1 parent e845184 commit f44b571

File tree

15 files changed

+129
-148
lines changed

15 files changed

+129
-148
lines changed

VSharp.CoverageTool/CoverageTool.fs

Lines changed: 72 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
namespace VSharp.CoverageTool
2+
3+
open System.Collections.Generic
24
open System.Diagnostics
35
open System.IO
46
open System.Reflection
57
open System.Runtime.InteropServices
68
open Microsoft.FSharp.NativeInterop
9+
10+
open VSharp
711
open VSharp.Utils.EnvironmentUtils
812
open VSharp.CSharpUtils
913

10-
open VSharp
1114

1215
#nowarn "9"
1316

@@ -21,9 +24,6 @@ module private ExternalCalls =
2124
[<DllImport("libvsharpCoverage", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)>]
2225
extern void SetCurrentThreadId(int id)
2326

24-
let inline castPtr ptr =
25-
ptr |> NativePtr.toVoidPtr |> NativePtr.ofVoidPtr
26-
2727
module private Configuration =
2828

2929
let (|Windows|MacOs|Linux|) _ =
@@ -66,39 +66,48 @@ module private Configuration =
6666
methodToken: string
6767
}
6868

69-
let private withCoverageToolConfiguration mainOnly =
70-
withConfiguration {
71-
coreclrProfiler = "{2800fea6-9667-4b42-a2b6-45dc98e77e9e}"
72-
coreclrProfilerPath = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}libvsharpCoverage{libExtension}"
73-
coreclrEnableProfiling = enabled
74-
instrumentMainOnly = if mainOnly then enabled else ""
75-
}
69+
let private withCoverageToolConfiguration mainOnly processInfo =
70+
let currentDirectory = Directory.GetCurrentDirectory()
71+
let configuration =
72+
{
73+
coreclrProfiler = "{2800fea6-9667-4b42-a2b6-45dc98e77e9e}"
74+
coreclrProfilerPath = $"{currentDirectory}{Path.DirectorySeparatorChar}libvsharpCoverage{libExtension}"
75+
coreclrEnableProfiling = enabled
76+
instrumentMainOnly = if mainOnly then enabled else ""
77+
}
78+
withConfiguration configuration processInfo
7679

7780
let withMainOnlyCoverageToolConfiguration =
7881
withCoverageToolConfiguration true
7982

8083
let withAllMethodsCoverageToolConfiguration =
8184
withCoverageToolConfiguration false
8285

83-
let withPassiveModeConfiguration (method: MethodBase) resultName =
84-
withConfiguration {
85-
passiveModeEnable = enabled
86-
resultName = resultName
87-
assemblyName = method.Module.Assembly.FullName
88-
moduleName = method.Module.FullyQualifiedName
89-
methodToken = method.MetadataToken.ToString()
90-
}
86+
let withPassiveModeConfiguration (method : MethodBase) resultName processInfo =
87+
let configuration =
88+
{
89+
passiveModeEnable = enabled
90+
resultName = resultName
91+
assemblyName = method.Module.Assembly.FullName
92+
moduleName = method.Module.FullyQualifiedName
93+
methodToken = method.MetadataToken.ToString()
94+
}
95+
withConfiguration configuration processInfo
9196

9297
let isCoverageToolAttached () = isConfigured<BaseCoverageToolConfiguration> ()
9398

9499
type InteractionCoverageTool() =
95100
let mutable entryMainWasSet = false
96101

102+
let castPtr ptr =
103+
NativePtr.toVoidPtr ptr |> NativePtr.ofVoidPtr
104+
97105
do
98106
if Configuration.isCoverageToolAttached () |> not then internalfail "Coverage tool wasn't attached"
99107

100108
member this.GetRawHistory () =
101-
if not entryMainWasSet then Prelude.internalfail "Try call GetRawHistory, while entryMain wasn't set"
109+
if not entryMainWasSet then
110+
Prelude.internalfail "Try call GetRawHistory, while entryMain wasn't set"
102111
let sizePtr = NativePtr.stackalloc<uint> 1
103112
let dataPtrPtr = NativePtr.stackalloc<nativeint> 1
104113

@@ -111,97 +120,83 @@ type InteractionCoverageTool() =
111120
Marshal.Copy(dataPtr, data, 0, size)
112121
data
113122

114-
member this.SetEntryMain (assembly: Assembly) (moduleName: string) (methodToken: int) =
123+
member this.SetEntryMain (assembly : Assembly) (moduleName : string) (methodToken : int) =
115124
entryMainWasSet <- true
116125
let assemblyNamePtr = fixed assembly.FullName.ToCharArray()
117126
let moduleNamePtr = fixed moduleName.ToCharArray()
118127
let assemblyNameLength = assembly.FullName.Length
119128
let moduleNameLength = moduleName.Length
120129

121130
ExternalCalls.SetEntryMain(
122-
ExternalCalls.castPtr assemblyNamePtr,
131+
castPtr assemblyNamePtr,
123132
assemblyNameLength,
124-
ExternalCalls.castPtr moduleNamePtr,
133+
castPtr moduleNamePtr,
125134
moduleNameLength,
126135
methodToken
127136
)
128137

129138
member this.SetCurrentThreadId id =
130139
ExternalCalls.SetCurrentThreadId(id)
131140

132-
static member WithCoverageTool (procInfo: ProcessStartInfo) =
141+
static member WithCoverageTool (procInfo : ProcessStartInfo) =
133142
Configuration.withMainOnlyCoverageToolConfiguration procInfo
134143

135144
type PassiveCoverageTool(workingDirectory: DirectoryInfo, method: MethodBase) =
136145

137146
let resultName = "coverage.cov"
138147

139148
let getHistory () =
140-
workingDirectory.EnumerateFiles(resultName)
141-
|> Seq.tryHead
142-
|> Option.map (fun x -> File.ReadAllBytes(x.FullName))
143-
|> Option.map CoverageDeserializer.getRawReports
144-
|> Option.map CoverageDeserializer.reportsFromRawReports
145-
146-
147-
let printCoverage (allBlocks: seq<BasicBlock>) (visited: seq<BasicBlock>) =
149+
let coverageFile = workingDirectory.EnumerateFiles(resultName) |> Seq.tryHead
150+
match coverageFile with
151+
| Some coverageFile ->
152+
File.ReadAllBytes(coverageFile.FullName)
153+
|> CoverageDeserializer.getRawReports
154+
|> CoverageDeserializer.reportsFromRawReports
155+
|> Some
156+
| None -> None
157+
158+
let printCoverage (allBlocks: ResizeArray<BasicBlock>) (visited: HashSet<BasicBlock>) =
148159
Logger.writeLine $"Coverage for method {method.Name}:"
149160

150-
let hasNonCovered = allBlocks |> Seq.exists (fun b -> Seq.contains b visited |> not)
151-
152-
if hasNonCovered then
153-
allBlocks
154-
|> Seq.iter (fun block ->
155-
if Seq.contains block visited |> not then
156-
Logger.writeLine $"Block [0x{block.StartOffset:X} .. 0x{block.FinalOffset:X}] not covered"
157-
)
158-
else
161+
let mutable allCovered = true
162+
for block in allBlocks do
163+
if visited.Contains block |> not then
164+
Logger.writeLine $"Block [0x{block.StartOffset:X} .. 0x{block.FinalOffset:X}] not covered"
165+
allCovered <- false
166+
if allCovered then
159167
Logger.writeLine "All blocks are covered"
160168

161-
let computeCoverage (cfg: CfgInfo) (visited: seq<CoverageReport>) =
162-
// filtering coverage records that are only relevant to this method
163-
let visitedInMethod =
164-
visited
165-
|> Seq.map (fun x -> x.coverageLocations)
166-
|> Seq.concat
167-
|> Seq.filter (fun x ->
168-
x.methodToken = method.MetadataToken
169-
&& x.moduleName = method.Module.FullyQualifiedName
170-
)
171-
172-
let visitedBlocks = System.Collections.Generic.HashSet<BasicBlock>()
169+
let computeCoverage (cfg: CfgInfo) (visited: CoverageReport[]) =
170+
let visitedBlocks = HashSet<BasicBlock>()
173171

174-
for location in visitedInMethod do
175-
let offset = LanguagePrimitives.Int32WithMeasure location.offset
176-
let block = cfg.ResolveBasicBlock(offset)
177-
if block.FinalOffset = offset then
178-
visitedBlocks.Add block |> ignore
172+
let token = method.MetadataToken
173+
let moduleName = method.Module.FullyQualifiedName
174+
for coverageReport in visited do
175+
for loc in coverageReport.coverageLocations do
176+
// Filtering coverage records that are only relevant to this method
177+
if loc.methodToken = token && loc.moduleName = moduleName then
178+
let offset = Offset.from loc.offset
179+
let block = cfg.ResolveBasicBlock offset
180+
if block.FinalOffset = offset then
181+
visitedBlocks.Add block |> ignore
179182

180183
printCoverage cfg.SortedBasicBlocks visitedBlocks
181-
182184
let coveredSize = visitedBlocks |> Seq.sumBy (fun x -> x.BlockSize)
183-
184185
(double coveredSize) / (double cfg.MethodSize) * 100. |> int
185186

186187
member this.RunWithCoverage (args: string) =
187-
let procInfo =
188-
ProcessStartInfo()
189-
|> (fun x ->
190-
x.Arguments <- args
191-
x.FileName <- DotnetExecutablePath.ExecutablePath
192-
x.WorkingDirectory <- workingDirectory.FullName
193-
x
194-
)
195-
|> Configuration.withMainOnlyCoverageToolConfiguration
196-
|> Configuration.withPassiveModeConfiguration method resultName
197-
198-
let applicationMethod = Application.getMethod(method)
188+
let procInfo = ProcessStartInfo()
189+
procInfo.Arguments <- args
190+
procInfo.FileName <- DotnetExecutablePath.ExecutablePath
191+
procInfo.WorkingDirectory <- workingDirectory.FullName
192+
Configuration.withMainOnlyCoverageToolConfiguration procInfo
193+
Configuration.withPassiveModeConfiguration method resultName procInfo
199194

200-
if applicationMethod.HasBody |> not then
201-
Logger.warning $"CoverageRunner was given a method without body; 100%% coverage assumed"
195+
let method = Application.getMethod method
196+
if not method.HasBody then
197+
Logger.warning "CoverageRunner was given a method without body; 100%% coverage assumed"
202198
100
203199
else
204-
205200
let proc = procInfo.StartWithLogging(
206201
(fun x -> Logger.info $"{x}"),
207202
(fun x -> Logger.error $"{x}")
@@ -213,7 +208,7 @@ type PassiveCoverageTool(workingDirectory: DirectoryInfo, method: MethodBase) =
213208
-1
214209
else
215210
match getHistory () with
216-
| Some history -> computeCoverage applicationMethod.CFG history
211+
| Some history -> computeCoverage method.CFG history
217212
| None ->
218-
Logger.error $"Failed to retrieve coverage locations"
213+
Logger.error "Failed to retrieve coverage locations"
219214
-1

VSharp.Fuzzer/Interaction.fs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ open System.Threading.Tasks
99
open VSharp
1010
open VSharp.CSharpUtils
1111
open VSharp.Fuzzer.Communication
12-
open Logger
1312
open VSharp.Fuzzer.Communication.Contracts
1413
open VSharp.Fuzzer.Communication.Services
1514
open VSharp.Fuzzer.Startup
@@ -76,7 +75,7 @@ type private TestRestorer (fuzzerOptions, assemblyPath, outputDirectory) =
7675
| Some test ->
7776
let testPath = $"{outputDirectory}{Path.DirectorySeparatorChar}fuzzer_error_{errorTestIdGenerator.NextId()}.vst"
7877
test.Serialize(testPath)
79-
| None _ -> error "Failed to create test while restoring"
78+
| None -> Logger.error "Failed to create test while restoring"
8079

8180
| None -> internalfail "Unexpected generic solving fail"
8281

@@ -98,9 +97,8 @@ type private FuzzingProcess(outputPath, targetAssemblyPath, fuzzerOptions, fuzze
9897
let mutable state = NotStarted
9998
let mutable lastRequestTime = Unchecked.defaultof<DateTime>
10099

101-
102100
let unhandledExceptionPath = $"{outputPath}{Path.DirectorySeparatorChar}exception.info"
103-
let logFuzzingProcess msg = traceCommunication $"[FuzzingProcess] {msg}"
101+
let logFuzzingProcess msg = Logger.traceCommunication $"[FuzzingProcess] {msg}"
104102

105103
let fuzzerStarted () = fuzzerProcess <> null
106104
let fuzzerAlive () = fuzzerStarted () && (not fuzzerProcess.HasExited)

VSharp.Fuzzer/Startup.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ let internal waitDebuggerAttached () =
4545
if value = "1" then
4646
while not Diagnostics.Debugger.IsAttached do ()
4747

48-
4948
let internal startFuzzer options developerOptions =
5049
let info = Diagnostics.ProcessStartInfo()
5150
info.WorkingDirectory <- IO.Directory.GetCurrentDirectory()
@@ -65,7 +64,7 @@ let internal startFuzzer options developerOptions =
6564
if developerOptions.redirectStdout then
6665
info.RedirectStandardOutput <- true
6766

68-
let info = InteractionCoverageTool.WithCoverageTool info
67+
InteractionCoverageTool.WithCoverageTool info
6968
let proc = System.Diagnostics.Process.Start info
7069

7170
let stderrTag = "Fuzzer STDERR"
@@ -80,10 +79,11 @@ let internal startFuzzer options developerOptions =
8079
proc.OutputDataReceived.Add (fun x -> Logger.infoWithTag stdoutTag $"{x.Data}")
8180
proc.BeginOutputReadLine ()
8281

83-
if
82+
let waitDebugger =
8483
developerOptions.waitDebuggerAttachedFuzzer
8584
|| developerOptions.waitDebuggerAttachedCoverageTool
86-
|| developerOptions.waitDebuggerAttachedOnAssertCoverageTool then
85+
|| developerOptions.waitDebuggerAttachedOnAssertCoverageTool
86+
if waitDebugger then
8787
Logger.warning $"One of \"Wait debugger\" options is enabled, you may attach to process by pid {proc.Id}"
8888

8989
if proc.HasExited then

VSharp.Fuzzer/Utils.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open System.Runtime.InteropServices
66
let inline isNull value = obj.ReferenceEquals(value, null)
77
let inline failIfNull value message = if isNull value then VSharp.Prelude.internalfail message
88

9-
type IdGenerator(initValue: int) =
9+
type IdGenerator(initValue : int) =
1010
let mutable currentValue = initValue
1111
member this.NextId() =
1212
currentValue <- currentValue + 1

VSharp.IL/CFG.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ and CfgInfo internal (method : MethodWithBody) =
210210
addEdge srcBasicBlock newBasicBlock
211211
dfs' newBasicBlock dst k
212212

213-
let processCall (callee: MethodWithBody) callFrom returnTo k =
213+
let processCall (callee : MethodWithBody) callFrom returnTo k =
214214
calls.Add(currentBasicBlock, CallInfo(callee :?> Method, callFrom, returnTo))
215215
currentBasicBlock.FinalOffset <- callFrom
216216
let newBasicBlock = makeNewBasicBlock returnTo
@@ -497,31 +497,31 @@ type ApplicationGraph() =
497497
let getShortestDistancesToGoals (states : array<codeLocation>) =
498498
__notImplemented__()
499499

500-
member this.RegisterMethod (method: Method) =
500+
member this.RegisterMethod (method : Method) =
501501
assert method.HasBody
502502

503503
member this.AddCallEdge (sourceLocation : codeLocation) (targetLocation : codeLocation) =
504504
addCallEdge sourceLocation targetLocation
505505

506-
member this.SpawnState (state:IGraphTrackableState) =
507-
[|state|] |> addStates None
506+
member this.SpawnState (state : IGraphTrackableState) =
507+
[| state |] |> addStates None
508508

509-
member this.SpawnStates (states:seq<IGraphTrackableState>) =
509+
member this.SpawnStates (states : seq<IGraphTrackableState>) =
510510
Array.ofSeq states |> addStates None
511511

512-
member this.AddForkedStates (parentState:IGraphTrackableState) (forkedStates:seq<IGraphTrackableState>) =
512+
member this.AddForkedStates (parentState : IGraphTrackableState) (forkedStates : seq<IGraphTrackableState>) =
513513
addStates (Some parentState) (Array.ofSeq forkedStates)
514514

515515
member this.MoveState (fromLocation : codeLocation) (toLocation : IGraphTrackableState) =
516516
moveState fromLocation toLocation
517517

518-
member x.AddGoal (location:codeLocation) =
518+
member x.AddGoal (location : codeLocation) =
519519
()
520520

521-
member x.AddGoals (locations:array<codeLocation>) =
521+
member x.AddGoals (locations : array<codeLocation>) =
522522
()
523523

524-
member x.RemoveGoal (location:codeLocation) =
524+
member x.RemoveGoal (location : codeLocation) =
525525
()
526526

527527
type IVisualizer =

VSharp.SILI.Core/Terms.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type stackKey =
5050
| TemporaryLocalVariableKey (typ, index) -> $"temporary##%s{Reflection.getFullTypeName typ}%d{index}"
5151
fullname.GetDeterministicHashCode()
5252
interface IComparable with
53-
override x.CompareTo(other: obj) =
53+
override x.CompareTo(other : obj) =
5454
match other with
5555
| :? stackKey as other ->
5656
match x, other with

VSharp.SILI.Core/TypeSolver.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ module TypeSolver =
271271

272272
let private typeParameterCandidates makeGenericCandidates =
273273
let getMock _ = EmptyTypeMock() :> ITypeMock
274-
let validate (t: Type) =
274+
let validate (t : Type) =
275275
if t.IsGenericTypeDefinition then
276276
makeGenericCandidates t |> Option.map GenericCandidate
277277
else Candidate t |> Some
278278
let assemblies = getAssemblies()
279279
enumerateTypes List.empty getMock validate assemblies
280280

281281
let private typeParameterGroundCandidates getMock subst (parameter : Type, constraints : typeConstraints) =
282-
let validate (typ: Type) =
282+
let validate (typ : Type) =
283283
if not typ.IsGenericTypeDefinition && GroundUtils.satisfiesTypeParameterConstraints parameter subst typ then
284284
Candidate typ |> Some
285285
else None
@@ -303,7 +303,7 @@ module TypeSolver =
303303
makeGenericCandidate
304304
childDepth
305305

306-
let private makeGenericCandidate (typedef: Type) depth =
306+
let private makeGenericCandidate (typedef : Type) depth =
307307
let childDepth _ _ _ = Int32.MaxValue
308308
genericCandidate.TryCreate typedef depth (makeParameterSubstitutions childDepth)
309309

0 commit comments

Comments
 (0)