-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathCecilEx.fs
392 lines (322 loc) · 12.3 KB
/
CecilEx.fs
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
// Based in part upon https://github.com/lucaslorentz/minicover/blob/fe7467b0c17f1d1dee6ce9c45f6fd3bf6933b429/src/MiniCover/Instrumentation/ILProcessorExtensions.cs
// Based in part upon C# code by Sergiy Sakharov (sakharov@gmail.com)
// http://code.google.com/p/dot-net-coverage/source/browse/trunk/Coverage/Instrument/CounterAssemblyBuilder.cs
// http://code.google.com/p/dot-net-coverage/source/browse/trunk/Coverage/Instrument/InstrumentorVisitor.cs
namespace AltCover
open System
open System.Collections.Generic
open System.Diagnostics.CodeAnalysis
open System.IO
open System.Reflection
open Mono.Cecil
open Mono.Cecil.Cil
module AssemblyConstants =
let internal nugetCache =
Path.Combine(
Path.Combine(
Environment.GetFolderPath Environment.SpecialFolder.UserProfile,
".nuget"
),
"packages"
)
let internal dotnetDir =
let list =
Environment
.GetEnvironmentVariable("PATH")
.Split([| Path.PathSeparator |])
|> Seq.map _.Trim([| '"' |])
let files = [ "dotnet"; "dotnet.exe" ]
list
|> Seq.tryFind (fun p ->
files
|> List.exists (fun f -> File.Exists(Path.Combine(p, f))))
let internal packageEnv =
let e =
Environment.GetEnvironmentVariable "NUGET_PACKAGES"
|> Option.ofObj
|> (Option.defaultValue String.Empty)
e.Split([| Path.PathSeparator |])
|> Seq.map _.Trim([| '"' |])
|> Seq.filter (String.IsNullOrWhiteSpace >> not)
|> Seq.toList
let internal resolutionTable =
Dictionary<string, AssemblyDefinition>()
let internal findAssemblyName f =
try
(AssemblyName.GetAssemblyName f).ToString()
with
| :? ArgumentException
| :? FileNotFoundException
| :? System.Security.SecurityException
| :? BadImageFormatException
| :? FileLoadException -> String.Empty
[<SuppressMessage("Gendarme.Rules.Smells",
"RelaxedAvoidCodeDuplicatedInSameClassRule",
Justification = "minimum size overloads")>]
[<Sealed>]
type internal AssemblyResolver() as self =
inherit DefaultAssemblyResolver()
do
self.add_ResolveFailure
<| AssemblyResolveEventHandler(AssemblyResolver.ResolveFromNugetCache)
[<SuppressMessage("Gendarme.Rules.Exceptions",
"UseObjectDisposedExceptionRule",
Justification =
"Dispose() => cache clear in DefaultAssemblyResolver => harmless")>]
override self.Resolve(name: AssemblyNameReference) =
// Option.orElseWith ifNoneThunk option
let key = name.ToString()
if AssemblyConstants.resolutionTable.ContainsKey key then
AssemblyConstants.resolutionTable.[key]
else
base.Resolve name
static member private AssemblyRegister (name: string) (path: string) =
let def = AssemblyResolver.ReadAssembly path // recursive
AssemblyConstants.resolutionTable.[name] <- def
def
[<SuppressMessage("Gendarme.Rules.Correctness",
"EnsureLocalDisposalRule",
Justification = "Owned by registration table")>]
static member Register (name: string) (path: string) =
AssemblyResolver.AssemblyRegister name path
|> ignore
static member ReadAssembly(path: String) =
let reader = ReaderParameters()
// fsharplint:disable-next-line RedundantNewKeyword
reader.AssemblyResolver <- new AssemblyResolver()
AssemblyDefinition.ReadAssembly(path, reader)
static member ReadAssembly(file: Stream) =
let reader = ReaderParameters()
// fsharplint:disable-next-line RedundantNewKeyword
reader.AssemblyResolver <- new AssemblyResolver()
AssemblyDefinition.ReadAssembly(file, reader)
[<SuppressMessage("Gendarme.Rules.Performance",
"AvoidUnusedParametersRule",
Justification = "meets an interface")>]
static member internal ResolveFromNugetCache _ (y: AssemblyNameReference) =
let name = y.ToString()
if AssemblyConstants.resolutionTable.ContainsKey name then
AssemblyConstants.resolutionTable.[name]
else
// Placate Gendarme here
let share =
"|usr|share"
.Replace('|', Path.DirectorySeparatorChar)
let shareLocal =
"|usr|local|share"
.Replace('|', Path.DirectorySeparatorChar)
let dotnetShared =
"dotnet|shared"
.Replace('|', Path.DirectorySeparatorChar)
let wingac =
"Microsoft.NET|assembly"
.Replace('|', Path.DirectorySeparatorChar)
let monogac =
"lib|mono|gac"
.Replace('|', Path.DirectorySeparatorChar)
let sources =
[ AssemblyConstants.packageEnv
[ Some AssemblyConstants.nugetCache
Some <| Path.Combine("usr", monogac)
Environment.GetEnvironmentVariable "WinDir"
|> Option.ofObj
|> Option.map (fun p -> Path.Combine(p, wingac))
Some <| Path.Combine("usr", monogac)
Environment.GetEnvironmentVariable "MONO_GAC_PREFIX"
|> Option.ofObj
|> Option.map (fun p -> Path.Combine(p, monogac))
Environment.GetEnvironmentVariable "ProgramFiles"
|> Option.ofObj
|> Option.map (fun p -> Path.Combine(p, dotnetShared))
Some <| Path.Combine(share, dotnetShared)
Some <| Path.Combine(shareLocal, dotnetShared)
AssemblyConstants.dotnetDir
|> Option.map (fun p -> Path.Combine(p, "shared")) ]
|> List.choose id ]
|> List.concat
|> List.filter Directory.Exists
|> List.distinct
let candidate source =
source
|> List.filter (String.IsNullOrWhiteSpace >> not)
|> List.filter Directory.Exists
|> Seq.distinct
|> Seq.collect (fun dir ->
Directory.GetFiles(dir, y.Name + ".*", SearchOption.AllDirectories))
|> Seq.sortDescending
|> Seq.filter (fun f ->
let x = Path.GetExtension f
x.Equals(".exe", StringComparison.OrdinalIgnoreCase)
|| x.Equals(".dll", StringComparison.OrdinalIgnoreCase))
|> Seq.filter (fun f ->
y
.ToString()
.Equals(AssemblyConstants.findAssemblyName f, StringComparison.Ordinal))
|> Seq.tryHead
match candidate sources with
| None -> null
| Some x ->
String.Format(
System.Globalization.CultureInfo.CurrentCulture,
Output.resources.GetString "resolved",
y.ToString(),
x
)
|> (Output.warnOn true)
AssemblyResolver.AssemblyRegister name x
[<AutoOpen>]
module internal CecilExtension =
// Adjust the IL for exception handling
// param name="handler">The exception handler</param>
// param name="oldBoundary">The uninstrumented location</param>
// param name="newBoundary">Where it has moved to</param>
let substituteExceptionBoundary
(oldValue: Instruction)
(newValue: Instruction)
(handler: ExceptionHandler)
=
if handler.FilterStart = oldValue then
handler.FilterStart <- newValue
if handler.HandlerEnd = oldValue then
handler.HandlerEnd <- newValue
if handler.HandlerStart = oldValue then
handler.HandlerStart <- newValue
if handler.TryEnd = oldValue then
handler.TryEnd <- newValue
if handler.TryStart = oldValue then
handler.TryStart <- newValue
let substituteInstructionOperand
(oldValue: Instruction)
(newValue: Instruction)
(instruction: Instruction)
=
// Performance reasons - only 3 types of operators have operands of Instruction types
// instruction.Operand getter - is rather slow to execute it for every operator
match instruction.OpCode.OperandType with
| OperandType.InlineBrTarget
| OperandType.ShortInlineBrTarget ->
if instruction.Operand = (oldValue :> Object) then
instruction.Operand <- newValue
// At this point instruction.Operand will be either Operand != oldOperand
// or instruction.Operand will be of type Instruction[]
// (in other words - it will be a switch operator's operand)
| OperandType.InlineSwitch ->
let operands =
instruction.Operand :?> Instruction array
operands
|> Array.iteri (fun i x ->
if x = oldValue then
Array.set operands i newValue)
| _ -> ()
let replaceInstructionReferences
(oldInstruction: Instruction)
(newInstruction: Instruction)
(ilProcessor: ILProcessor)
=
ilProcessor.Body.ExceptionHandlers
|> Seq.iter (substituteExceptionBoundary oldInstruction newInstruction)
// Update instructions with a target instruction
ilProcessor.Body.Instructions
|> Seq.iter (substituteInstructionOperand oldInstruction newInstruction)
let bulkInsertBefore
(ilProcessor: ILProcessor)
(target: Instruction)
(newInstructions: Instruction seq)
(updateReferences: bool)
=
let newTarget =
newInstructions
|> Seq.rev
|> Seq.fold
(fun next i ->
ilProcessor.InsertBefore(next, i)
i)
target
if updateReferences then
replaceInstructionReferences target newTarget ilProcessor
newTarget
let replaceReturnsByLeave (ilProcessor: ILProcessor) =
let methodDefinition =
ilProcessor.Body.Method
let voidType =
methodDefinition.Module.TypeSystem.Void
// capture current state
let instructions =
ilProcessor.Body.Instructions |> Seq.toArray
if methodDefinition.ReturnType = voidType then
let newReturnInstruction =
ilProcessor.Create(OpCodes.Ret)
ilProcessor.Append(newReturnInstruction)
instructions
|> Seq.filter (fun i -> i.OpCode = OpCodes.Ret)
|> Seq.iter (fun i ->
i.OpCode <- OpCodes.Leave
i.Operand <- newReturnInstruction)
(newReturnInstruction, methodDefinition.ReturnType, [])
else
// this is the new part that AltCover didn't have before
// i.e. handling non-void methods
let returnVariable =
VariableDefinition(methodDefinition.ReturnType)
ilProcessor.Body.Variables.Add(returnVariable)
let loadResultInstruction =
ilProcessor.Create(OpCodes.Ldloc, returnVariable)
ilProcessor.Append(loadResultInstruction)
let newReturnInstruction =
ilProcessor.Create(OpCodes.Ret)
ilProcessor.Append(newReturnInstruction)
(loadResultInstruction,
methodDefinition.ReturnType,
instructions
|> Seq.filter (fun i -> i.OpCode = OpCodes.Ret)
|> Seq.map (fun i ->
i.OpCode <- OpCodes.Leave
i.Operand <- loadResultInstruction
bulkInsertBefore
ilProcessor
i
[| ilProcessor.Create(OpCodes.Stloc, returnVariable) |]
true)
|> Seq.toList)
let private findFirstInstruction (body: MethodBody) = body.Instructions |> Seq.head
let encapsulateWithTryFinally (ilProcessor: ILProcessor) =
let body = ilProcessor.Body
let firstInstruction =
findFirstInstruction body
let (newReturn, methodType, stlocs) =
replaceReturnsByLeave ilProcessor
let endFinally =
Instruction.Create(OpCodes.Endfinally)
ilProcessor.InsertBefore(newReturn, endFinally)
if
(findFirstInstruction body)
.Equals(firstInstruction)
then
let tryStart =
Instruction.Create(OpCodes.Nop)
ilProcessor.InsertBefore(firstInstruction, tryStart)
let finallyStart =
Instruction.Create(OpCodes.Nop)
ilProcessor.InsertBefore(endFinally, finallyStart)
let handler =
ExceptionHandler(ExceptionHandlerType.Finally)
handler.TryStart <- firstInstruction
handler.TryEnd <- finallyStart
handler.HandlerStart <- finallyStart
handler.HandlerEnd <- newReturn
body.ExceptionHandlers.Add(handler)
(endFinally, methodType, stlocs)
let removeTailInstructions (ilProcessor: ILProcessor) =
ilProcessor.Body.Instructions
|> Seq.toArray // reify
|> Seq.filter (fun i -> i.OpCode = OpCodes.Tail)
|> Seq.iter (fun i ->
i.OpCode <- OpCodes.Nop
i.Operand <- nullObject)
[<assembly: SuppressMessage("Gendarme.Rules.Exceptions",
"InstantiateArgumentExceptionCorrectlyRule",
Scope = "member", // MethodDefinition
Target =
"AltCover.AssemblyResolver/candidate@167::Invoke(Microsoft.FSharp.Collections.FSharpList`1<System.String>)",
Justification = "code inlined")>]
()