Skip to content

Commit

Permalink
Fixing empty required compilers message
Browse files Browse the repository at this point in the history
  • Loading branch information
eddynaka committed Feb 15, 2022
1 parent d85b633 commit 1359468
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 24 additions & 15 deletions src/BinSkim.Rules/PERules/BA2006.BuildWithSecureTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;

using Microsoft.CodeAnalysis.BinaryParsers;
Expand Down Expand Up @@ -127,12 +128,11 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte

Version minCompilerVersion;

var goodCompilers = new HashSet<string>();
var inPolicyCompilers = new HashSet<string>();

var badModules = new List<ObjectModuleDetails>();
StringToVersionMap allowedLibraries = context.Policy.GetProperty(AllowedLibraries);

var languageToBadModules = new Dictionary<Language, List<ObjectModuleDetails>>();
var languageToOutOfPolicyModules = new SortedDictionary<Language, List<ObjectModuleDetails>>();

foreach (DisposableEnumerableView<Symbol> omView in pdb.CreateObjectModuleIterator())
{
Expand Down Expand Up @@ -281,18 +281,27 @@ and a SymTagCompilandDetails tag (high detail).

if (foundIssue)
{
badModules.Add(omDetails);
if (!languageToOutOfPolicyModules.TryGetValue(omDetails.Language, out List<ObjectModuleDetails> outOfPolicyModules))
{
outOfPolicyModules = new List<ObjectModuleDetails>();
languageToOutOfPolicyModules.Add(omDetails.Language, outOfPolicyModules);
}

outOfPolicyModules.Add(omDetails);
}
else
{
goodCompilers.Add(BuildCompilerIdentifier(omDetails));
inPolicyCompilers.Add(BuildCompilerIdentifier(omDetails));
}
}

if (badModules.Count != 0)
if (languageToOutOfPolicyModules.Count != 0)
{
string badModulesText = badModules.CreateOutputCoalescedByCompiler();
string minimumRequiredCompilers = BuildMinimumCompilersList(context, languageToBadModules);
string outOfPolicyModulesText = BuildOutOfPolicyModulesList(languageToOutOfPolicyModules);
string minimumRequiredCompilers = BuildMinimumCompilersList(context, languageToOutOfPolicyModules);

Debug.Assert(!string.IsNullOrWhiteSpace(outOfPolicyModulesText) ||
!string.IsNullOrWhiteSpace(minimumRequiredCompilers));

// '{0}' was compiled with one or more modules which were not built using
// minimum required tool versions ({1}). More recent toolchains
Expand All @@ -307,11 +316,11 @@ and a SymTagCompilandDetails tag (high detail).
nameof(RuleResources.BA2006_Error),
context.TargetUri.GetFileName(),
minimumRequiredCompilers,
badModulesText));
outOfPolicyModulesText));
return;
}

string[] sorted = goodCompilers.ToArray();
string[] sorted = inPolicyCompilers.ToArray();
Array.Sort(sorted);

// All linked modules of '{0}' satisfy configured policy (observed compilers: {1}).
Expand All @@ -327,25 +336,25 @@ private string BuildCompilerIdentifier(ObjectModuleDetails omDetails)
return omDetails.CompilerName + ":" + omDetails.Language + ":" + omDetails.CompilerBackEndVersion;
}

private string BuildMinimumCompilersList(BinaryAnalyzerContext context, Dictionary<Language, List<ObjectModuleDetails>> languageToBadModules)
private string BuildMinimumCompilersList(BinaryAnalyzerContext context, SortedDictionary<Language, List<ObjectModuleDetails>> languageToOutOfPolicyModules)
{
var languages = new List<string>();

foreach (Language language in languageToBadModules.Keys)
foreach (Language language in languageToOutOfPolicyModules.Keys)
{
Version version = context.Policy.GetProperty(MinimumToolVersions)[language.ToString()];
languages.Add($"{language} ({version})");
}
return string.Join(", ", languages);
}

private string BuildBadModulesList(Dictionary<Language, List<ObjectModuleDetails>> languageToBadModules)
private string BuildOutOfPolicyModulesList(SortedDictionary<Language, List<ObjectModuleDetails>> languageToOutOfPolicyModules)
{
var coalescedModules = new List<string>();

foreach (Language language in languageToBadModules.Keys)
foreach (Language language in languageToOutOfPolicyModules.Keys)
{
string modulesText = languageToBadModules[language].CreateOutputCoalescedByCompiler();
string modulesText = languageToOutOfPolicyModules[language].CreateOutputCoalescedByCompiler();
coalescedModules.Add(modulesText);
}
return string.Join(string.Empty, coalescedModules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@
"id": "Error",
"arguments": [
"Native_x86_VS2012_SDL_Enabled.exe",
"",
"C (17.0.65501.17013), Cxx (17.0.65501.17013)",
"Microsoft (R) Optimizing Compiler : c : 17.0.50929.0 : MSVCRTD.lib (_newmode.obj,atonexit.obj,chandler4gs.obj,cinitexe.obj,crtexe.obj,dllargv.obj,fp8.obj,gs_cookie.obj,gs_report.obj,gs_support.obj,loadcfg.obj,merr.obj,natstart.obj,pesect.obj,secchk.obj,wildcard.obj,xncommod.obj,xthdloc.obj,xtxtmode.obj)\r\nMicrosoft (R) Optimizing Compiler : cxx : 17.0.50929.0 : MSVCRTD.lib (ehvecdtr.obj,error.obj,init.obj,initsect.obj,pdblkup.obj,stack.obj,ti_inst.obj,unhandld.obj,userapi.obj)\r\nMicrosoft (R) Optimizing Compiler : cxx : 17.0.61232.400 : [directly linked] (Native_x86_VS2012.obj)\r\n"
]
},
Expand Down

0 comments on commit 1359468

Please sign in to comment.