-
Notifications
You must be signed in to change notification settings - Fork 86
/
MultithreadedAnalyzeCommandBase.cs
1457 lines (1232 loc) · 62.3 KB
/
MultithreadedAnalyzeCommandBase.cs
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Sarif.Driver.Sdk;
using Microsoft.CodeAnalysis.Sarif.Writers;
using Microsoft.Diagnostics.Tracing.Session;
namespace Microsoft.CodeAnalysis.Sarif.Driver
{
public abstract class MultithreadedAnalyzeCommandBase<TContext, TOptions> : PluginDriverCommand<TOptions>
where TContext : AnalyzeContextBase, new()
where TOptions : AnalyzeOptionsBase, new()
{
public const string DefaultPolicyName = "default";
// This is a unit test knob that configures the console output logger to capture its output
// in a string builder. This is important to do because tooling behavior differs in non-trivial
// ways depending on whether output is captured to a log file disk or not. In the latter case,
// the captured output is useful to verify behavior.
internal bool _captureConsoleOutput;
internal ConsoleLogger _consoleLogger;
private uint _fileContextsCount;
private long _filesMatchingGlobalFileDenyRegex;
private long _filesExceedingSizeLimitCount;
private Channel<uint> _resultsWritingChannel;
private Channel<uint> readyToScanChannel;
private ConcurrentDictionary<uint, TContext> _fileContexts;
public static bool RaiseUnhandledExceptionInDriverCode { get; set; }
public virtual Tool Tool { get; set; }
public virtual FileFormat ConfigurationFormat => FileFormat.Json;
protected MultithreadedAnalyzeCommandBase(IFileSystem fileSystem = null)
{
Tool ??= Tool.CreateFromAssemblyData();
FileSystem = fileSystem ?? Sarif.FileSystem.Instance;
}
public string DefaultConfigurationPath
{
get
{
string currentDirectory = Path.GetDirectoryName(GetType().Assembly.Location);
return Path.Combine(currentDirectory, "default.configuration.xml");
}
}
public override int Run(TOptions options)
{
TContext context = default;
return Run(options, ref context);
}
public virtual int Run(TOptions options, ref TContext globalContext)
{
try
{
globalContext ??= new TContext();
options ??= new TOptions();
globalContext = InitializeGlobalContextFromOptions(options, ref globalContext);
// We must make a copy of the global context reference
// to utilize it in a separate thread.
TContext methodLocalContext = globalContext;
if (!string.IsNullOrEmpty(globalContext.EventsFilePath))
{
Guid guid = EventSource.GetGuid(typeof(DriverEventSource));
if (globalContext.EventsFilePath.Equals("console", StringComparison.OrdinalIgnoreCase))
{
globalContext.TraceEventSession = new TraceEventSession($"Sarif-Driver-{Guid.NewGuid()}");
globalContext.TraceEventSession.BufferSizeMB = globalContext.EventsBufferSizeInMegabytes;
TraceEventSession traceEventSession = globalContext.TraceEventSession;
globalContext.TraceEventSession.Source.Dynamic.All += (e =>
{
Console.WriteLine($"{e.TimeStamp:MM/dd/yyyy hh:mm:ss.ffff},{e.ThreadID}," +
$"{e.ProcessorNumber},{e.EventName},{e.TimeStampRelativeMSec},{e.FormattedMessage}");
if (e.EventName.Equals("SessionEnded"))
{
traceEventSession.Dispose();
}
});
traceEventSession.EnableProvider(guid);
}
else
{
string etlFilePath =
Path.GetExtension(globalContext.EventsFilePath).Equals(".csv", StringComparison.OrdinalIgnoreCase)
? $"{Path.GetFileNameWithoutExtension(globalContext.EventsFilePath)}.etl"
: globalContext.EventsFilePath;
globalContext.TraceEventSession = new TraceEventSession($"Sarif-Driver-{Guid.NewGuid()}", etlFilePath);
globalContext.TraceEventSession.BufferSizeMB = globalContext.EventsBufferSizeInMegabytes;
globalContext.TraceEventSession.EnableProvider(guid);
}
}
var analyzeTask = Task.Run(() =>
{
return Run(methodLocalContext);
}, globalContext.CancellationToken);
int msDelay = globalContext.TimeoutInMilliseconds;
int result = FAILURE;
if (Task.WhenAny(analyzeTask, Task.Delay(msDelay)).GetAwaiter().GetResult() == analyzeTask)
{
result = analyzeTask.Result;
}
else
{
Errors.LogAnalysisTimedOut(globalContext);
}
DriverEventSource.Log.SessionEnded(result, globalContext.RuntimeErrors);
return result;
}
catch (Exception ex)
{
globalContext.RuntimeExceptions ??= new List<Exception>();
ProcessException(globalContext, ex);
}
finally
{
globalContext.Dispose();
}
return
globalContext.RichReturnCode == true
? (int)globalContext.RuntimeErrors
: FAILURE;
}
private void ProcessException(TContext globalContext, Exception ex)
{
if (ex is ExitApplicationException<ExitReason> eae)
{
globalContext.RuntimeExceptions.Add(ex);
return;
}
if (ex is AggregateException ae)
{
foreach (Exception innerException in ae.InnerExceptions)
{
ProcessException(globalContext, innerException);
}
return;
}
if (ex.InnerException != null)
{
ProcessException(globalContext, ex.InnerException);
return;
}
if (ex is OperationCanceledException oce)
{
Errors.LogAnalysisCanceled(globalContext, oce);
globalContext.RuntimeExceptions.Add(new ExitApplicationException<ExitReason>(SdkResources.ERR999_AnalysisCanceled, oce)
{
ExitReason = ExitReason.AnalysisCanceled
});
return;
}
Errors.LogUnhandledEngineException(globalContext, ex);
globalContext.RuntimeExceptions.Add(new ExitApplicationException<ExitReason>(DriverResources.MSG_UnexpectedApplicationExit, ex)
{
ExitReason = ExitReason.UnhandledExceptionInEngine
});
}
private int Run(TContext globalContext)
{
bool succeeded;
IDisposable disposableLogger;
globalContext.FileSystem ??= FileSystem;
globalContext = ValidateContext(globalContext);
disposableLogger = globalContext.Logger as IDisposable;
InitializeOutputs(globalContext);
// 1. Instantiate skimmers. We need to do this before initializing
// the output file so that we can preconstruct the tool
// extensions data written to the SARIF file. Due to this ordering,
// we won't emit any failures or notifications in this operation
// to the log file itself: it will only appear in console output.
ISet<Skimmer<TContext>> skimmers = CreateSkimmers(globalContext);
// 2. Initialize skimmers. Initialize occurs a single time only. This
// step needs to occur after initializing configuration in order
// to allow command-line override of rule settings
skimmers = InitializeSkimmers(skimmers, globalContext);
// 3. Log analysis initiation
globalContext.Logger.AnalysisStarted();
// 4. Run all multi-threaded analysis operations.
AnalyzeTargets(globalContext, skimmers);
// 5. For test purposes, raise an unhandled exception if indicated
if (RaiseUnhandledExceptionInDriverCode)
{
throw new InvalidOperationException(GetType().Name);
}
// Analysis is complete. Generate our stopped event and also dispose
// of any disposable logs (which will flush and release locks on
// output files).
globalContext.Logger.AnalysisStopped(globalContext.RuntimeErrors);
disposableLogger?.Dispose();
if (Path.GetExtension(globalContext.EventsFilePath).Equals(".csv", StringComparison.OrdinalIgnoreCase))
{
var dumpEventsCommand = new DumpEventsCommand();
var options = new DumpEventsOptions()
{
EventsFilePath = $"{Path.GetFileNameWithoutExtension(globalContext.EventsFilePath)}.etl",
CsvFilePath = globalContext.EventsFilePath,
};
Console.WriteLine(options);
Console.WriteLine("Dumping session events to CSV (this could take a while)...");
dumpEventsCommand.Run(options);
Console.WriteLine($"Events written to: {globalContext.EventsFilePath}");
}
// Note that we don't clear the logger here. That is because the console
// logger and any other custom loggers can still be useful for these operations.
if ((globalContext.RuntimeErrors & ~RuntimeConditions.Nonfatal) == RuntimeConditions.None)
{
ProcessBaseline(globalContext);
}
// Even if there are fatal errors, if the log file is generated, we can upload it with the ToolExecutionNotifications.
PostLogFile(globalContext);
globalContext.Logger = null;
succeeded = (globalContext.RuntimeErrors & ~RuntimeConditions.Nonfatal) == RuntimeConditions.None;
return
globalContext.RichReturnCode
? (int)globalContext?.RuntimeErrors
: succeeded ? SUCCESS : FAILURE;
}
public virtual TContext InitializeGlobalContextFromOptions(TOptions options, ref TContext context)
{
context ??= new TContext();
context.FileSystem ??= this.FileSystem ?? Sarif.FileSystem.Instance;
context.Quiet = options.Quiet != null ? options.Quiet.Value : context.Quiet;
// We initialize a temporary console logger that's used strictly to emit
// diagnostics output while we load/initialize various configurations settings.
IAnalysisLogger savedLogger = context.Logger;
try
{
context.Logger = new ConsoleLogger(quietConsole: context.Quiet,
levels: BaseLogger.ErrorWarningNote,
kinds: BaseLogger.Fail,
toolName: Tool.Driver.Name);
// Next, we initialize ourselves from disk-based configuration,
// if specified. This allows users to operate against configuration
// XML but to override specific settings within it via options.
context = InitializeConfiguration(options.ConfigurationFilePath, context);
// Now that our context if fully initialized, we can create
// the actual loggers used to complete analysis.
context.Logger = savedLogger;
context.ResultKinds = options.Kind != null ? options.ResultKinds : context.ResultKinds;
context.FailureLevels = options.Level != null ? options.FailureLevels : context.FailureLevels;
context.Logger ??= InitializeLogger(context);
}
finally
{
// Handle the remaining options. This is in a finally block so even if the logger initialization throws
// (possible), we still have a reasonably well-constructed globalContext for controlling any still-relevant
// behaviors (ie: return code behavior.) As a precaution, we handle trivial (clearly non-throwing)
// initialization first.
context.RichReturnCode = options.RichReturnCode ?? context.RichReturnCode;
context.PostUri = options.PostUri ?? context.PostUri;
context.AutomationId = options.AutomationId ?? context.AutomationId;
context.Threads = options.Threads > 0 ? options.Threads : context.Threads;
context.OutputFilePath = options.OutputFilePath ?? context.OutputFilePath;
context.BaselineFilePath = options.BaselineFilePath ?? context.BaselineFilePath;
context.Recurse = options.Recurse != null ? options.Recurse.Value : context.Recurse;
context.GlobalFilePathDenyRegex = options.GlobalFilePathDenyRegex ?? context.GlobalFilePathDenyRegex;
context.AutomationGuid = options.AutomationGuid != default ? options.AutomationGuid : context.AutomationGuid;
context.OutputConfigurationFilePath = options.OutputConfigurationFilePath ?? context.OutputConfigurationFilePath;
context.MaxFileSizeInKilobytes = options.MaxFileSizeInKilobytes != null ? options.MaxFileSizeInKilobytes.Value : context.MaxFileSizeInKilobytes;
context.PluginFilePaths = options.PluginFilePaths?.Any() == true ? options.PluginFilePaths?.ToImmutableHashSet() : context.PluginFilePaths;
context.TimeoutInMilliseconds = options.TimeoutInSeconds != null ? Math.Max(options.TimeoutInSeconds.Value * 1000, 0) : context.TimeoutInMilliseconds;
context.InsertProperties = options.InsertProperties?.Any() == true ? InitializeStringSet(options.InsertProperties) : context.InsertProperties;
context.TargetFileSpecifiers = options.TargetFileSpecifiers?.Any() == true ? InitializeStringSet(options.TargetFileSpecifiers) : context.TargetFileSpecifiers;
context.InvocationPropertiesToLog = options.InvocationPropertiesToLog?.Any() == true ? InitializeStringSet(options.InvocationPropertiesToLog) : context.InvocationPropertiesToLog;
context.Traces = options.Trace.Any() ? InitializeStringSet(options.Trace) : context.Traces;
context.RuleKinds = options.RuleKindOption != null ? options.RuleKinds : context.RuleKinds;
}
// Less-obviously throw-safe. We don't do these in the finally block because we'd prefer not to mask an earlier Exception during logger initialization.
context.DataToInsert = options.DataToInsert?.Any() == true ? options.DataToInsert.ToFlags() : context.DataToInsert;
context.DataToRemove = options.DataToRemove?.Any() == true ? options.DataToRemove.ToFlags() : context.DataToRemove;
context.OutputFileOptions = options.OutputFileOptions?.Any() == true ? options.OutputFileOptions.ToFlags() : context.OutputFileOptions;
context.EventsFilePath = Environment.GetEnvironmentVariable("SPMI_ETW") ?? options.EventsFilePath ?? context.EventsFilePath;
if (context.TargetsProvider == null)
{
context.TargetsProvider =
OrderedFileSpecifier.Create(
context.TargetFileSpecifiers,
context.Recurse,
context.MaxFileSizeInKilobytes,
context.FileSystem);
}
return context;
}
public virtual TContext ValidateContext(TContext globalContext)
{
bool succeeded = true;
bool force = globalContext.OutputFileOptions.HasFlag(FilePersistenceOptions.ForceOverwrite);
succeeded &= ValidateFile(globalContext,
globalContext.OutputFilePath,
shouldExist: force ? (bool?)null : false);
succeeded &= ValidateBaselineFile(globalContext);
bool required = !string.IsNullOrEmpty(globalContext.BaselineFilePath);
succeeded &= ValidateFile(globalContext,
globalContext.BaselineFilePath,
shouldExist: required ? true : (bool?)null);
required = globalContext.PluginFilePaths?.Any() == true;
succeeded &= ValidateFiles(globalContext,
globalContext.PluginFilePaths,
shouldExist: required ? true : (bool?)null);
if (!string.IsNullOrEmpty(globalContext.PostUri))
{
try
{
var httpClient = new HttpClientWrapper();
var content = new StringContent(string.Empty);
HttpResponseMessage httpResponseMessage = httpClient.PostAsync(globalContext.PostUri, content).GetAwaiter().GetResult();
// Internal server error means we found our server but it didn't like our malformed payload
// (in first implementation). In a server update, this condition returns 422 (unprocessable
// payload). We treat either return value as good, i.e. posting a valid SARIF file should work.
if (httpResponseMessage.StatusCode != HttpStatusCode.InternalServerError &&
httpResponseMessage.StatusCode != (HttpStatusCode)422)
{
Errors.LogErrorPostingLogFile(globalContext, globalContext.PostUri);
globalContext.PostUri = null;
succeeded = false;
}
}
catch (Exception e)
{
Errors.LogErrorPostingLogFile(globalContext, globalContext.PostUri);
globalContext.PostUri = null;
succeeded = false;
globalContext.RuntimeExceptions ??= new List<Exception>();
globalContext.RuntimeExceptions.Add(e);
}
finally
{
// TBD add logging if POST URI is null.
}
}
succeeded &= ValidateInvocationPropertiesToLog(globalContext);
if (!succeeded)
{
ThrowExitApplicationException(ExitReason.InvalidCommandLineOption);
}
return globalContext;
}
private static ISet<string> InitializeStringSet(IEnumerable<string> strings)
{
return strings?.Any() == true ?
new StringSet(strings) :
new StringSet();
}
private void MultithreadedAnalyzeTargets(TContext globalContext,
IEnumerable<Skimmer<TContext>> skimmers,
ISet<string> disabledSkimmers)
{
globalContext.CancellationToken.ThrowIfCancellationRequested();
var channelOptions = new BoundedChannelOptions(globalContext.ChannelSize)
{
SingleWriter = true,
SingleReader = false,
};
readyToScanChannel = Channel.CreateBounded<uint>(channelOptions);
channelOptions = new BoundedChannelOptions(globalContext.ChannelSize)
{
SingleWriter = false,
SingleReader = true,
};
_resultsWritingChannel = Channel.CreateBounded<uint>(channelOptions);
var sw = Stopwatch.StartNew();
if (!globalContext.Quiet)
{
Console.WriteLine($"THREADS: {globalContext.Threads}");
}
// 1: First we initiate an asynchronous operation to locate disk files for
// analysis, as specified in analysis configuration (file names, wildcards).
var enumerateTargets = Task.Run(() => EnumerateTargetsAsync(globalContext));
// 2: A dedicated set of threads pull scan targets and analyze them.
// On completing a scan, the thread writes the index of the
// scanned item to a channel that drives logging.
var scanWorkers = new Task[globalContext.Threads];
for (int i = 0; i < globalContext.Threads; i++)
{
scanWorkers[i] = Task.Run(() => ScanTargetsAsync(globalContext, skimmers, disabledSkimmers));
}
// 3: A single-threaded consumer watches for completed scans
// and logs results, if any. This operation is single-threaded
// to ensure determinism in log output. i.e., any scan of the
// same targets using the same production code should produce
// a log file that is byte-for-byte identical to previous log.
var logResults = Task.Run(() => LogResultsAsync(globalContext));
Task.WhenAll(scanWorkers)
.ContinueWith(_ => _resultsWritingChannel.Writer.Complete())
.Wait();
enumerateTargets.Wait();
logResults.Wait();
if (_filesMatchingGlobalFileDenyRegex > 0)
{
string reason = "file path(s) matched the global file deny regex";
Warnings.LogOneOrMoreFilesSkipped(globalContext, _filesMatchingGlobalFileDenyRegex, reason);
}
if (!globalContext.Quiet) { Console.WriteLine(); }
string id;
if (globalContext.Traces.Contains(nameof(DefaultTraces.PeakWorkingSet)))
{
using (var currentProcess = Process.GetCurrentProcess())
{
id = $"TRC101.{nameof(DefaultTraces.PeakWorkingSet)}";
string memoryUsage = $"Peak working set: {currentProcess.PeakWorkingSet64 / 1024 / 1024}MB.";
LogTrace(globalContext, memoryUsage, id);
}
}
if (globalContext.Traces.Contains(nameof(DefaultTraces.ScanTime)))
{
id = $"TRC101.{nameof(DefaultTraces.ScanTime)}";
string timing = $"Done. {_fileContextsCount:n0} files scanned, elapsed time {sw.Elapsed}.";
LogTrace(globalContext, timing, id);
}
else if (!globalContext.Quiet)
{
Console.WriteLine($"Done. {_fileContextsCount:n0} files scanned.");
}
}
private async Task LogResultsAsync(TContext globalContext)
{
uint currentIndex = 0;
ChannelReader<uint> reader = _resultsWritingChannel.Reader;
// Wait until there is work or the channel is closed.
while (await reader.WaitToReadAsync())
{
// Loop while there is work to do.
while (reader.TryRead(out uint item))
{
// This condition can occur if currentIndex moves
// ahead in array processing due to operations
// against it by other threads. For this case,
// since the relevant file has already been
// processed, we just ignore this notification.
if (currentIndex > item) { break; }
TContext context;
context = _fileContexts[currentIndex];
while (context?.AnalysisComplete == true)
{
lock (globalContext)
{
DriverEventSource.Log.LogResultsStart();
globalContext.CurrentTarget = context.CurrentTarget;
LogCachingLogger(globalContext, (CachingLogger)context.Logger, clone: false);
DriverEventSource.Log.LogResultsStop();
globalContext.RuntimeErrors |= context.RuntimeErrors;
if (context.RuntimeExceptions != null)
{
globalContext.RuntimeExceptions ??= new List<Exception>();
foreach (Exception exception in context.RuntimeExceptions)
{
globalContext.RuntimeExceptions.Add(exception);
}
}
globalContext.CurrentTarget = null;
}
_fileContexts.TryRemove(currentIndex, out _);
_fileContexts.TryGetValue(++currentIndex, out context);
}
}
}
}
private static void LogCachingLogger(TContext globalContext, CachingLogger cachingLogger, bool clone = false)
{
// Today, the signal to generate hash data in log files is synonymous with a decision
// to perform target file results-caching (where we only analyze a copy of a file, by
// hash, a single time). We need to separate configuring this mechanism. Some scan
// scenarios, such as binary analysis + crawl of PDB, greatly benefit from this mechanism,
// other scan scenarios, such as lightweight linting of large #'s of source files,
// experience significant memory pressure from it. Disabling caching altogether for now.
//
// https://github.com/microsoft/sarif-sdk/issues/2620
//
// As a result of this gap, `clone` will always be false, because we have a per-file
// (and not per-file-by-hash) logger instance.
IDictionary<ReportingDescriptor, IList<Tuple<Result, int?>>> results = cachingLogger.Results;
globalContext.CancellationToken.ThrowIfCancellationRequested();
IEnumeratedArtifact artifact = globalContext.CurrentTarget;
if (results?.Count > 0)
{
foreach (KeyValuePair<ReportingDescriptor, IList<Tuple<Result, int?>>> kv in results)
{
globalContext.CancellationToken.ThrowIfCancellationRequested();
foreach (Tuple<Result, int?> tuple in kv.Value)
{
Result result = tuple.Item1;
Result currentResult = result;
if (clone)
{
Result clonedResult = result.DeepClone();
UpdateLocationsAndMessageWithCurrentUri(clonedResult.Locations, clonedResult.Message, artifact.Uri);
currentResult = clonedResult;
}
globalContext.Logger.FileRegionsCache = cachingLogger.FileRegionsCache;
globalContext.Logger.Log(kv.Key, currentResult, tuple.Item2);
}
}
}
if (cachingLogger.ToolNotifications != null)
{
foreach (Tuple<Notification, ReportingDescriptor> tuple in cachingLogger.ToolNotifications)
{
globalContext.Logger.LogToolNotification(tuple.Item1, tuple.Item2);
}
}
if (cachingLogger.ConfigurationNotifications != null)
{
foreach (Notification notification in cachingLogger.ConfigurationNotifications)
{
Notification currentNotification = notification;
if (clone)
{
Notification clonedNotification = notification.DeepClone();
UpdateLocationsAndMessageWithCurrentUri(clonedNotification.Locations, notification.Message, artifact.Uri);
currentNotification = clonedNotification;
}
globalContext.Logger.LogConfigurationNotification(currentNotification);
}
}
globalContext.Logger.TargetAnalyzed(globalContext);
}
private async Task<bool> EnumerateTargetsAsync(TContext globalContext)
{
try
{
this._fileContextsCount = 0;
this._fileContexts = new ConcurrentDictionary<uint, TContext>();
DriverEventSource.Log.EnumerateArtifactsStart();
await EnumerateFilesFromArtifactsProvider(globalContext);
DriverEventSource.Log.EnumerateArtifactsStop();
}
finally
{
readyToScanChannel.Writer.Complete();
}
if (_filesExceedingSizeLimitCount > 0)
{
Warnings.LogOneOrMoreFilesSkippedDueToExceedingSizeLimit(globalContext, _filesExceedingSizeLimitCount);
}
if (_fileContextsCount == 0)
{
Errors.LogNoValidAnalysisTargets(globalContext);
ThrowExitApplicationException(ExitReason.NoValidAnalysisTargets);
}
return true;
}
private async Task<bool> EnumerateFilesFromArtifactsProvider(TContext globalContext)
{
foreach (IEnumeratedArtifact artifact in globalContext.TargetsProvider.Artifacts)
{
globalContext.CancellationToken.ThrowIfCancellationRequested();
string filePath = artifact.Uri.GetFilePath();
if (globalContext.CompiledGlobalFileDenyRegex?.Match(filePath).Success == true)
{
_filesMatchingGlobalFileDenyRegex++;
DriverEventSource.Log.ArtifactNotScanned(filePath, DriverEventNames.FilePathDenied, artifact.SizeInBytes.Value, globalContext.GlobalFilePathDenyRegex);
string reason = $"its file path matched the global file deny regex: {globalContext.GlobalFilePathDenyRegex}";
Notes.LogFileSkipped(globalContext, filePath, reason);
continue;
}
if (artifact.SizeInBytes == 0)
{
DriverEventSource.Log.ArtifactNotScanned(filePath, DriverEventNames.EmptyFile, 00, data2: null);
Notes.LogEmptyFileSkipped(globalContext, filePath);
continue;
}
if (!IsTargetWithinFileSizeLimit(artifact.SizeInBytes.Value, globalContext.MaxFileSizeInKilobytes))
{
_filesExceedingSizeLimitCount++;
DriverEventSource.Log.ArtifactNotScanned(filePath, DriverEventNames.FileExceedsSizeLimits, artifact.SizeInBytes.Value, $"{globalContext.MaxFileSizeInKilobytes}");
Notes.LogFileExceedingSizeLimitSkipped(globalContext, artifact.Uri.GetFilePath(), artifact.SizeInBytes.Value / 1000);
continue;
}
TContext fileContext = CreateScanTargetContext(globalContext);
fileContext.Logger =
new CachingLogger(globalContext.FailureLevels,
globalContext.ResultKinds);
Debug.Assert(fileContext.Logger != null);
fileContext.CurrentTarget = artifact;
fileContext.CancellationToken = globalContext.CancellationToken;
lock (globalContext)
{
// We need to generate this event on the global logger, though as
// a result this event means 'target enumerated for analysis'
// rather than literally 'we are analyzing the target'.
//
// This call needs to be protected with a lock as the actual
// logging occurs on a separated thread.
globalContext.Logger.AnalyzingTarget(fileContext);
}
bool added = _fileContexts.TryAdd(_fileContextsCount, fileContext);
Debug.Assert(added);
if (_fileContextsCount == 0)
{
DriverEventSource.Log.FirstArtifactQueued(fileContext.CurrentTarget.Uri.GetFilePath());
}
await readyToScanChannel.Writer.WriteAsync(_fileContextsCount++);
}
// TBD get all skipped artifacts.
return true;
}
private void EnqueueAllDirectories(TContext context, Queue<string> queue, string directory)
{
context.CancellationToken.ThrowIfCancellationRequested();
var sortedDiskItems = new SortedSet<string>();
queue.Enqueue(directory);
foreach (string childDirectory in FileSystem.DirectoryEnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly))
{
sortedDiskItems.Add(childDirectory);
}
foreach (string childDirectory in sortedDiskItems)
{
EnqueueAllDirectories(context, queue, childDirectory);
}
}
private async Task ScanTargetsAsync(TContext globalContext, IEnumerable<Skimmer<TContext>> skimmers, ISet<string> disabledSkimmers)
{
ChannelReader<uint> reader = readyToScanChannel.Reader;
globalContext.CancellationToken.ThrowIfCancellationRequested();
// Wait until there is work or the channel is closed.
while (await reader.WaitToReadAsync())
{
// Loop while there is work to do.
while (reader.TryRead(out uint item))
{
TContext perFileContext = _fileContexts[item]; ;
perFileContext.CancellationToken.ThrowIfCancellationRequested();
string filePath = perFileContext.CurrentTarget.Uri.GetFilePath();
DriverEventSource.Log.ReadArtifactStart(filePath);
// Reading the length property faults in the file contents.
long sizeInBytes = perFileContext.CurrentTarget.SizeInBytes.Value;
DriverEventSource.Log.ReadArtifactStop(filePath, sizeInBytes);
DetermineApplicabilityAndAnalyze(perFileContext, skimmers, disabledSkimmers);
globalContext.RuntimeErrors |= perFileContext.RuntimeErrors;
if (perFileContext != null) { perFileContext.AnalysisComplete = true; }
await _resultsWritingChannel.Writer.WriteAsync(item);
}
}
}
protected virtual void ValidateOptions(TOptions options, TContext context)
{
bool succeeded = true;
succeeded &= ValidateFile(context, options.OutputFilePath, shouldExist: null);
succeeded &= ValidateFile(context, options.ConfigurationFilePath, shouldExist: true);
succeeded &= ValidateFile(context, options.BaselineFilePath, shouldExist: true);
succeeded &= ValidateFiles(context, options.PluginFilePaths, shouldExist: true);
succeeded &= ValidateInvocationPropertiesToLog(context);
succeeded &= options.ValidateOutputOptions(context);
succeeded &= context.MaxFileSizeInKilobytes >= 0;
succeeded &= ValidateOutputFileCanBeCreated(context);
if (!succeeded)
{
ThrowExitApplicationException(ExitReason.InvalidCommandLineOption);
}
}
internal AggregatingLogger InitializeLogger(TContext globalContext)
{
var logger = new AggregatingLogger();
if (!(globalContext.Quiet == true))
{
_consoleLogger =
new ConsoleLogger(quietConsole: false,
Tool.Driver.Name,
globalContext.FailureLevels,
globalContext.ResultKinds)
{
CaptureOutput = _captureConsoleOutput
};
logger.Loggers.Add(_consoleLogger);
}
return logger;
}
protected virtual TContext CreateScanTargetContext(TContext globalContext)
{
var context = new TContext
{
Logger = globalContext.Logger,
RuntimeErrors = globalContext.RuntimeErrors,
FileSystem = globalContext.FileSystem ?? this.FileSystem,
Policy = globalContext.Policy ?? new PropertiesDictionary(),
};
return context;
}
/// <summary>
/// Calculate the file to load the configuration from.
/// </summary>
/// <param name="options">Options</param>
/// <returns>Configuration file path, or null if the built in configuration should be used.</returns>
internal string GetConfigurationFileName(string configurationFilePath, IFileSystem fileSystem)
{
// If the user passes our default file name, return null, indicating that no
// configuration should be loaded. As a result, we will pick up all runtime defaults.
if (configurationFilePath == DefaultPolicyName)
{
return null;
}
if (string.IsNullOrEmpty(configurationFilePath))
{
// If a configuration file is not explicitly specified but we see that
// a default configuration file has been placed in the current working
// directory, we will load it. TBD: ensure this location is alongside
// the client executable?
return fileSystem.FileExists(this.DefaultConfigurationPath)
? this.DefaultConfigurationPath
: null;
}
if (!File.Exists(configurationFilePath))
{
string fileName = Path.GetFileNameWithoutExtension(configurationFilePath);
string spamDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
fileName = Path.Combine(spamDirectory, $"{fileName}.xml");
if (fileSystem.FileExists(fileName))
{
return fileName;
}
}
return configurationFilePath;
}
protected virtual TContext InitializeConfiguration(string configurationFileName, TContext context)
{
context.Policy ??= new PropertiesDictionary();
configurationFileName = GetConfigurationFileName(configurationFileName, context.FileSystem);
context.ConfigurationFilePath = configurationFileName;
if (!ValidateFile(context,
configurationFileName,
shouldExist: !string.IsNullOrEmpty(configurationFileName) ? true : (bool?)null))
{
// TBD exit reason update to invalid config?
ThrowExitApplicationException(ExitReason.InvalidCommandLineOption);
}
if (string.IsNullOrEmpty(configurationFileName)) { return context; }
string extension = Path.GetExtension(configurationFileName);
var configuration = new PropertiesDictionary();
if (extension.Equals(".xml", StringComparison.OrdinalIgnoreCase))
{
configuration.LoadFromXml(configurationFileName);
}
else if (extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
{
configuration.LoadFromJson(configurationFileName);
}
else if (ConfigurationFormat == FileFormat.Xml)
{
configuration.LoadFromXml(configurationFileName);
}
else
{
configuration.LoadFromJson(configurationFileName);
}
configuration.MergePreferFirst(context.Policy);
context.Policy = configuration;
return context;
}
public virtual void InitializeOutputs(TContext globalContext)
{
string filePath = globalContext.OutputFilePath;
if (!string.IsNullOrEmpty(filePath))
{
var aggregatingLogger = globalContext.Logger as AggregatingLogger;
if (aggregatingLogger == null)
{
aggregatingLogger = new AggregatingLogger();
aggregatingLogger.Loggers.Add(globalContext.Logger);
globalContext.Logger = aggregatingLogger;
}
if (globalContext.Traces.Contains(nameof(DefaultTraces.ResultsSummary)))
{
aggregatingLogger.Loggers.Add(new ResultsSummaryLogger());
}
InvokeCatchingRelevantIOExceptions
(
() =>
{
FilePersistenceOptions logFilePersistenceOptions = globalContext.OutputFileOptions;
OptionallyEmittedData dataToInsert = globalContext.DataToInsert;
OptionallyEmittedData dataToRemove = globalContext.DataToRemove;
SarifLogger sarifLogger;
var run = new Run()
{
AutomationDetails = new RunAutomationDetails
{
Id = globalContext.AutomationId,
Guid = globalContext.AutomationGuid
},
VersionControlProvenance = globalContext.VersionControlProvenance,
Tool = Tool,
};
var fileRegionsCache = new FileRegionsCache(fileSystem: globalContext.FileSystem);
sarifLogger = new SarifLogger(globalContext.OutputFilePath,
logFilePersistenceOptions,
dataToInsert,
dataToRemove,
run,
analysisTargets: null,
fileRegionsCache: fileRegionsCache,
invocationTokensToRedact: GenerateSensitiveTokensList(),
invocationPropertiesToLog: globalContext.InvocationPropertiesToLog,
levels: globalContext.FailureLevels,
kinds: globalContext.ResultKinds,
insertProperties: globalContext.InsertProperties);
aggregatingLogger.Loggers.Add(sarifLogger);
},
(ex) =>
{
Errors.LogExceptionCreatingOutputFile(globalContext, filePath, ex);
ThrowExitApplicationException(ExitReason.ExceptionCreatingLogFile, ex);
}
);
}
globalContext.Logger ??= new ConsoleLogger(quietConsole: false, "SPMI");
}
private static IEnumerable<string> GenerateSensitiveTokensList()
{
var result = new List<string>
{
Environment.MachineName,
Environment.UserName,
Environment.UserDomainName
};
string userDnsDomain = Environment.GetEnvironmentVariable("USERDNSDOMAIN");
string logonServer = Environment.GetEnvironmentVariable("LOGONSERVER");
if (!string.IsNullOrEmpty(userDnsDomain)) { result.Add(userDnsDomain); }
if (!string.IsNullOrEmpty(logonServer)) { result.Add(logonServer); }
return result;
}
public void InvokeCatchingRelevantIOExceptions(Action action, Action<Exception> exceptionHandler)
{
try
{
action();
}
catch (UnauthorizedAccessException ex)
{
exceptionHandler(ex);
}
catch (IOException ex)
{
exceptionHandler(ex);