From 7a07f4064d75154be90bf96b2af2559be887ea55 Mon Sep 17 00:00:00 2001 From: Eddy Nakamura Date: Thu, 30 Sep 2021 11:31:12 -0300 Subject: [PATCH 1/2] Enabling multithread analysis --- src/BinSkim.Driver/AnalyzeCommand.cs | 4 ++-- .../PERules/BA2024.EnableSpectreMitigations.cs | 13 +++++++------ .../EnableSpectreMitigationsTests.cs | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/BinSkim.Driver/AnalyzeCommand.cs b/src/BinSkim.Driver/AnalyzeCommand.cs index 4dc0162b2..8b869c295 100644 --- a/src/BinSkim.Driver/AnalyzeCommand.cs +++ b/src/BinSkim.Driver/AnalyzeCommand.cs @@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.IL { - public class AnalyzeCommand : AnalyzeCommandBase + public class AnalyzeCommand : MultithreadedAnalyzeCommandBase { private static bool ShouldWarnVerbose = true; @@ -153,7 +153,7 @@ private AnalysisSummary ExtractAnalysisSummary(SarifLog sarifLog, AnalyzeOptions NormalizedPath = string.Join(";", options.TargetFileSpecifiers.Select(p => System.IO.Path.GetDirectoryName(p)).Distinct()), SymbolPath = options.SymbolsPath, FileAnalyzed = artifacts.Count, - // FileNotAnalyzed = + // FileNotAnalyzed = StartTimeUtc = invocation.StartTimeUtc, EndTimeUtc = invocation.EndTimeUtc, TimeConsumed = invocation.EndTimeUtc - invocation.StartTimeUtc, diff --git a/src/BinSkim.Rules/PERules/BA2024.EnableSpectreMitigations.cs b/src/BinSkim.Rules/PERules/BA2024.EnableSpectreMitigations.cs index b90aa8170..dab96fc4c 100644 --- a/src/BinSkim.Rules/PERules/BA2024.EnableSpectreMitigations.cs +++ b/src/BinSkim.Rules/PERules/BA2024.EnableSpectreMitigations.cs @@ -2,6 +2,7 @@ // 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.Composition; @@ -85,7 +86,7 @@ public IEnumerable GetOptions() // Internal so that we can reset this during testing. In practice this should never get reset, but we use several different configs during unit tests. // Please do not access this field outside of this class and unit tests. - internal static Dictionary compilerData = null; + internal static ConcurrentDictionary compilerData = null; public override AnalysisApplicability CanAnalyzePE(PEBinary target, PropertiesDictionary policy, out string reasonForNotAnalyzing) { @@ -367,7 +368,7 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte internal static Version GetClosestCompilerVersionWithSpectreMitigations(BinaryAnalyzerContext context, ExtendedMachine machine, Version omVersion) { - Dictionary compilerMitigationData = LoadCompilerDataFromConfig(context.Policy); + ConcurrentDictionary compilerMitigationData = LoadCompilerDataFromConfig(context.Policy); MachineFamily machineFamily = machine.GetMachineFamily(); if (!compilerMitigationData.ContainsKey(machineFamily)) @@ -409,7 +410,7 @@ internal static Version GetClosestCompilerVersionWithSpectreMitigations(BinaryAn /// internal static CompilerMitigations GetAvailableMitigations(BinaryAnalyzerContext context, ExtendedMachine machine, Version omVersion) { - Dictionary compilerMitigationData = LoadCompilerDataFromConfig(context.Policy); + ConcurrentDictionary compilerMitigationData = LoadCompilerDataFromConfig(context.Policy); MachineFamily machineFamily = machine.GetMachineFamily(); if (!compilerMitigationData.ContainsKey(machineFamily)) @@ -545,16 +546,16 @@ A servicing update to Visual Studio 2015 Update 3 return compilersData; } - internal static Dictionary LoadCompilerDataFromConfig(PropertiesDictionary policy) + internal static ConcurrentDictionary LoadCompilerDataFromConfig(PropertiesDictionary policy) { if (compilerData == null) { - compilerData = new Dictionary(); + compilerData = new ConcurrentDictionary(); PropertiesDictionary configData = policy.GetProperty(MitigatedCompilers); foreach (string key in configData.Keys) { var machine = (MachineFamily)Enum.Parse(typeof(MachineFamily), key); // Neaten this up. - compilerData.Add(machine, CreateSortedVersionDictionary((PropertiesDictionary)configData[key])); + compilerData.TryAdd(machine, CreateSortedVersionDictionary((PropertiesDictionary)configData[key])); } } return compilerData; diff --git a/src/Test.UnitTests.BinSkim.Rules/EnableSpectreMitigationsTests.cs b/src/Test.UnitTests.BinSkim.Rules/EnableSpectreMitigationsTests.cs index c3034ac8b..1c9d06dd1 100644 --- a/src/Test.UnitTests.BinSkim.Rules/EnableSpectreMitigationsTests.cs +++ b/src/Test.UnitTests.BinSkim.Rules/EnableSpectreMitigationsTests.cs @@ -2,6 +2,7 @@ // 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 FluentAssertions; @@ -32,7 +33,7 @@ public void LoadCompilerDataFromConfig_ParsesAndCachesAsExpected() this.AddFakeConfigTestData(context.Policy); - Dictionary result = EnableSpectreMitigations.LoadCompilerDataFromConfig(context.Policy); + ConcurrentDictionary result = EnableSpectreMitigations.LoadCompilerDataFromConfig(context.Policy); this.ValidateResultFromFakeTestData(result[MachineFamily.X86]); result.Should().ContainKeys(new MachineFamily[] { MachineFamily.X86, MachineFamily.Arm }); From 5ab549fa8799677607207e41aeae27e46f8ec45d Mon Sep 17 00:00:00 2001 From: Eddy Nakamura Date: Fri, 8 Oct 2021 10:44:48 -0300 Subject: [PATCH 2/2] updating submodules and moving to multithread --- src/BinSkim.Driver/AnalyzeCommand.cs | 2 +- src/sarif-sdk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BinSkim.Driver/AnalyzeCommand.cs b/src/BinSkim.Driver/AnalyzeCommand.cs index 8b869c295..adb7e6644 100644 --- a/src/BinSkim.Driver/AnalyzeCommand.cs +++ b/src/BinSkim.Driver/AnalyzeCommand.cs @@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.IL { - public class AnalyzeCommand : MultithreadedAnalyzeCommandBase + public class AnalyzeCommand : AnalyzeCommandBase { private static bool ShouldWarnVerbose = true; diff --git a/src/sarif-sdk b/src/sarif-sdk index b262841c9..ca1c2c972 160000 --- a/src/sarif-sdk +++ b/src/sarif-sdk @@ -1 +1 @@ -Subproject commit b262841c97859f3bcfb65a652c7035e8ae6087b3 +Subproject commit ca1c2c972449ee5bf43f821fe86f67a4112e515f