Skip to content

Commit 4007259

Browse files
authored
Fix #638 (#643)
1 parent 73bdfe0 commit 4007259

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

Cli/AttackSurfaceAnalyzerClient.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,17 @@ private static ASA_ERROR RunExportGuidedCommand(ExportGuidedCommandOptions opts)
262262
return ExportCompareResults(results, exportOptions, AsaHelpers.MakeValidFileName($"{first}_vs_{second}"), analysesHash, options.AnalysesFile.Rules);
263263
}
264264

265-
static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> AnalyzeGuided(GuidedModeCommandOptions opts, RuleFile analysisFile)
265+
static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>> AnalyzeGuided(GuidedModeCommandOptions opts, RuleFile analysisFile)
266266
{
267267
if (opts.RunId is null)
268268
{
269269
Log.Warning(Strings.Get("Err_RunIdNull"));
270-
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>();
270+
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>();
271271
}
272272
if (!analysisFile.Rules.Any())
273273
{
274274
Log.Warning(Strings.Get("Err_NoRules"));
275-
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>();
275+
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>();
276276
}
277277

278278
var firstCollectRunId = GuidedRunIdToFirstCollectRunId(opts.RunId);
@@ -318,22 +318,22 @@ private static ASA_ERROR RunExportGuidedCommand(ExportGuidedCommandOptions opts)
318318
return results;
319319
}
320320

321-
public static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> AnalyzeMonitored(CompareCommandOptions opts)
321+
public static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>> AnalyzeMonitored(CompareCommandOptions opts)
322322
{
323323
if (DatabaseManager is null)
324324
{
325325
Log.Error("Err_DatabaseManagerNull", "InsertCompareResults");
326-
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>();
326+
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>();
327327
}
328-
if (opts is null || opts.SecondRunId is null) { return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>(); }
328+
if (opts is null || opts.SecondRunId is null) { return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>(); }
329329
var analyzer = new AsaAnalyzer(new AnalyzerOptions(opts.RunScripts));
330330
return AnalyzeMonitored(opts, analyzer, DatabaseManager.GetMonitorResults(opts.SecondRunId), opts.AnalysesFile ?? throw new ArgumentNullException(nameof(opts.AnalysesFile)));
331331
}
332332

333-
public static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> AnalyzeMonitored(CompareCommandOptions opts, AsaAnalyzer analyzer, IEnumerable<MonitorObject> collectObjects, RuleFile ruleFile)
333+
public static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>> AnalyzeMonitored(CompareCommandOptions opts, AsaAnalyzer analyzer, IEnumerable<MonitorObject> collectObjects, RuleFile ruleFile)
334334
{
335-
if (opts is null) { return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>(); }
336-
var results = new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>();
335+
if (opts is null) { return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>(); }
336+
var results = new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>();
337337
var analysesHash = ruleFile.GetHash();
338338
Parallel.ForEach(collectObjects, monitorResult =>
339339
{
@@ -362,7 +362,7 @@ private static ASA_ERROR RunExportGuidedCommand(ExportGuidedCommandOptions opts)
362362
}
363363

364364
shellResult.Analysis = shellResult.Rules.Count > 0 ? shellResult.Rules.Max(x => ((AsaRule)x).Flag) : ruleFile.GetDefaultLevel(shellResult.ResultType);
365-
results.TryAdd((monitorResult.ResultType, monitorResult.ChangeType), new List<CompareResult>());
365+
results.TryAdd((monitorResult.ResultType, monitorResult.ChangeType), new ConcurrentBag<CompareResult>());
366366
results[(monitorResult.ResultType, monitorResult.ChangeType)].Add(shellResult);
367367
});
368368
return results;
@@ -389,7 +389,7 @@ private static ASA_ERROR RunVerifyRulesCommand(VerifyOptions opts)
389389
return ASA_ERROR.NONE;
390390
}
391391

392-
internal static void InsertCompareResults(ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> results, string? FirstRunId, string SecondRunId, string AnalysesHash)
392+
internal static void InsertCompareResults(ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>> results, string? FirstRunId, string SecondRunId, string AnalysesHash)
393393
{
394394
if (DatabaseManager is null)
395395
{
@@ -399,9 +399,9 @@ internal static void InsertCompareResults(ConcurrentDictionary<(RESULT_TYPE, CHA
399399
DatabaseManager.InsertCompareRun(FirstRunId, SecondRunId, AnalysesHash, RUN_STATUS.RUNNING);
400400
foreach (var key in results.Keys)
401401
{
402-
if (results.TryGetValue(key, out List<CompareResult>? obj))
402+
if (results.TryGetValue(key, out ConcurrentBag<CompareResult>? obj))
403403
{
404-
if (obj is List<CompareResult> Queue)
404+
if (obj is ConcurrentBag<CompareResult> Queue)
405405
{
406406
foreach (var result in Queue)
407407
{
@@ -627,7 +627,7 @@ private static ASA_ERROR RunExportCollectCommand(ExportCollectCommandOptions opt
627627
return ExportCompareResults(results, opts, AsaHelpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId), analysesHash, ruleFile.Rules);
628628
}
629629

630-
internal static ASA_ERROR ExportCompareResults(ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> resultsIn, ExportOptions opts, string baseFileName, string analysesHash, IEnumerable<AsaRule> rules)
630+
internal static ASA_ERROR ExportCompareResults(ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>> resultsIn, ExportOptions opts, string baseFileName, string analysesHash, IEnumerable<AsaRule> rules)
631631
{
632632
var results = resultsIn.Select(x => new KeyValuePair<string, object>($"{x.Key.Item1}_{x.Key.Item2}", x.Value)).ToDictionary(x => x.Key, x => x.Value);
633633
JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings()
@@ -1125,7 +1125,7 @@ public static List<BaseCompare> GetComparators()
11251125
return comparators;
11261126
}
11271127

1128-
public static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> CompareRuns(CompareCommandOptions opts)
1128+
public static ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>> CompareRuns(CompareCommandOptions opts)
11291129
{
11301130
if (opts is null)
11311131
{
@@ -1134,7 +1134,7 @@ public static List<BaseCompare> GetComparators()
11341134
if (DatabaseManager is null)
11351135
{
11361136
Log.Error("Err_DatabaseManagerNull", "CompareRuns");
1137-
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>();
1137+
return new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>();
11381138
}
11391139
comparators = new List<BaseCompare>();
11401140

@@ -1177,8 +1177,12 @@ public static List<BaseCompare> GetComparators()
11771177
{
11781178
foreach ((RESULT_TYPE, CHANGE_TYPE) key in c.Results.Keys)
11791179
{
1180-
if (c.Results[key] is List<CompareResult> queue)
1180+
if (c.Results[key] is IEnumerable<CompareResult> queue)
11811181
{
1182+
if (queue.Any(x => x is null))
1183+
{
1184+
Console.WriteLine("found em");
1185+
}
11821186
IEnumerable<AsaRule> platformRules = opts.AnalysesFile.Rules.Where(rule => rule.Platforms == null || rule.Platforms.Contains(platform));
11831187
if (opts.SingleThreadAnalysis)
11841188
{
@@ -1200,6 +1204,10 @@ void PopulateAnalysisForResult(CompareResult res)
12001204
var selectedRules = platformRules.Where((rule) =>
12011205
(rule.ChangeTypes == null || rule.ChangeTypes.Contains(res.ChangeType))
12021206
&& (rule.ResultType == res.ResultType));
1207+
if (res is null)
1208+
{
1209+
return;
1210+
}
12031211
Log.Verbose("Type: {0}", res.ResultType);
12041212
Log.Verbose("Base: {0}", JsonConvert.SerializeObject(res.Base));
12051213
Log.Verbose("Compare: {0}", JsonConvert.SerializeObject(res.Compare));

Cli/Components/States/Results.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ else
195195
{
196196
if (AttackSurfaceAnalyzerClient.DatabaseManager is null) { return; }
197197
var iterationSize = 1000;
198-
var results = new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>();
198+
var results = new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>();
199199
foreach(var resultType in foundResultTypes)
200200
{
201201
for (int i = 0; i < resultType.Value; i += iterationSize)
@@ -207,7 +207,7 @@ else
207207
{
208208
if (!results.ContainsKey((result.ResultType, result.ChangeType)))
209209
{
210-
results[(result.ResultType, result.ChangeType)] = new List<CompareResult>();
210+
results[(result.ResultType, result.ChangeType)] = new ConcurrentBag<CompareResult>();
211211
}
212212
results[(result.ResultType, result.ChangeType)].Add(result);
213213
}
@@ -217,7 +217,7 @@ else
217217
{
218218
if (!results.ContainsKey((result.ResultType, result.ChangeType)))
219219
{
220-
results[(result.ResultType, result.ChangeType)] = new List<CompareResult>();
220+
results[(result.ResultType, result.ChangeType)] = new ConcurrentBag<CompareResult>();
221221
}
222222
results[(result.ResultType, result.ChangeType)].Add(result);
223223
}

Cli/Properties/launchSettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
},
2525
"applicationUrl": "https://localhost:7280;http://localhost:5280",
2626
"dotnetRunMessages": true
27+
},
28+
"Export-collect last 2": {
29+
"commandName": "Project",
30+
"commandLineArgs": "export-collect"
31+
},
32+
"Export-monitor": {
33+
"commandName": "Project",
34+
"commandLineArgs": "export-monitor"
2735
}
2836
}
2937
}

Lib/Collectors/BaseCompare.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Linq;
1212
using System.Reflection;
1313
using Tpm2Lib;
14+
using System.Diagnostics;
1415

1516
namespace Microsoft.CST.AttackSurfaceAnalyzer.Collectors
1617
{
@@ -21,20 +22,20 @@ public class BaseCompare
2122
{
2223
public BaseCompare()
2324
{
24-
Results = new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>>();
25+
Results = new ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>>();
2526
foreach (RESULT_TYPE? result_type in Enum.GetValues(typeof(RESULT_TYPE)))
2627
{
2728
foreach (CHANGE_TYPE? change_type in Enum.GetValues(typeof(CHANGE_TYPE)))
2829
{
2930
if (result_type is RESULT_TYPE r && change_type is CHANGE_TYPE c)
3031
{
31-
Results[(r, c)] = new List<CompareResult>();
32+
Results[(r, c)] = new ConcurrentBag<CompareResult>();
3233
}
3334
}
3435
}
3536
}
3637

37-
public ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> Results { get; }
38+
public ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), ConcurrentBag<CompareResult>> Results { get; }
3839

3940
public void Compare(IEnumerable<CollectObject> FirstRunObjects, IEnumerable<CollectObject> SecondRunObjects, string? firstRunId, string secondRunId)
4041
{

0 commit comments

Comments
 (0)