Skip to content

Commit

Permalink
Change compiler report rule to report all modules in file (#476)
Browse files Browse the repository at this point in the history
* Fix the report to show all lines since the dialect and languages apply to each line

* Add history

Co-authored-by: Eddy Nakamura <eddynaka@gmail.com>
  • Loading branch information
shaopeng-gh and eddynaka authored Sep 9, 2021
1 parent 1ee5f41 commit becf484
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ private void PrintCompilerData(BinaryAnalyzerContext context, List<DwarfCompileC
var record = new CompilerData
{
BinaryType = "ELF",
ModuleName = info.FileName,
Dialect = info.GetDialect(),
ModuleLibrary = string.Empty,
CommandLine = info.CommandLine,
CompilerName = compiler.Compiler.ToString(),
CompilerBackEndVersion = compiler.Version.ToString(),
Expand All @@ -100,7 +102,7 @@ private void PrintCompilerData(BinaryAnalyzerContext context, List<DwarfCompileC

processedRecords.Add(record);

context.CompilerDataLogger.Write(record, info.FileName);
context.CompilerDataLogger.Write(record);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/BinSkim.Rules/PERules/BA4001.ReportPECompilerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte
var record = new CompilerData
{
BinaryType = "PE",
ModuleName = omDetails?.Name,
ModuleLibrary = omDetails?.Library,
Dialect = omDetails.GetDialect(out _),
CompilerName = omDetails.CompilerName,
CommandLine = omDetails.RawCommandLine,
Expand All @@ -104,7 +106,7 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte

foreach (KeyValuePair<CompilerData, ObjectModuleDetails> kv in records)
{
context.CompilerDataLogger.Write(kv.Key, kv.Value);
context.CompilerDataLogger.Write(kv.Key);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/BinSkim.Sdk/CompilerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ public struct CompilerData
public string Dialect { get; set; }
public string Language { get; set; }
public string BinaryType { get; set; }
public string ModuleName { get; set; }
public string CommandLine { get; set; }
public string FileVersion { get; set; }
public string CompilerName { get; set; }
public string ModuleLibrary { get; set; }
public string DebuggingFileName { get; set; }
public string DebuggingFileGuid { get; set; }
public string CompilerBackEndVersion { get; set; }
public string CompilerFrontEndVersion { get; set; }

public override string ToString()
{
return $"{CompilerName},{CompilerBackEndVersion},{CompilerFrontEndVersion},{FileVersion},{BinaryType},{Language},{DebuggingFileName},{DebuggingFileGuid},{CommandLine},{Dialect}";
return $"{CompilerName},{CompilerBackEndVersion},{CompilerFrontEndVersion},{FileVersion},{BinaryType},{Language},{DebuggingFileName},{DebuggingFileGuid},{CommandLine},{Dialect},{ModuleName},{(ModuleLibrary == ModuleName ? string.Empty : ModuleLibrary)}";
}
}
}
55 changes: 4 additions & 51 deletions src/BinSkim.Sdk/CompilerDataLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.CodeAnalysis.BinaryParsers.ProgramDatabase;
using Microsoft.CodeAnalysis.Sarif;

namespace Microsoft.CodeAnalysis.IL.Sdk
Expand Down Expand Up @@ -119,10 +118,8 @@ public void PrintHeader()
}
}

public void Write(CompilerData compilerData, ObjectModuleDetails omDetails)
public void Write(CompilerData compilerData)
{
string name = omDetails?.Name;
string library = omDetails?.Library;
if (TelemetryEnabled)
{
string commandLineId = string.Empty;
Expand All @@ -135,55 +132,11 @@ public void Write(CompilerData compilerData, ObjectModuleDetails omDetails)
{ "fileVersion", compilerData.FileVersion ?? string.Empty },
{ "binaryType", compilerData.BinaryType },
{ "language", compilerData.Language },
{ "debuggingFileName", compilerData.DebuggingFileName },
{ "debuggingGuid", compilerData.DebuggingFileGuid },
{ "dialect", compilerData.Dialect },
{ "moduleName", name ?? string.Empty },
{ "moduleLibrary", (name == library ? string.Empty : library) },
{ "sessionId", s_sessionId },
{ "hash", this.sha256 },
{ "error", string.Empty }
};

if (!string.IsNullOrWhiteSpace(compilerData.CommandLine))
{
commandLineId = Guid.NewGuid().ToString();
properties.Add("commandLineId", commandLineId);
}

s_telemetryClient.TrackEvent(CompilerEventName, properties: properties);

if (!string.IsNullOrWhiteSpace(commandLineId))
{
SendChunkedCommandLine(commandLineId, compilerData.CommandLine);
}
}
else
{
string log = $@"{this.relativeFilePath},{compilerData},,""{name}"",""{(name == library ? string.Empty : library)}"",{this.sha256},";
Console.WriteLine(log);
}
}

public void Write(CompilerData compilerData, string file)
{
if (TelemetryEnabled)
{
string commandLineId = string.Empty;
var properties = new Dictionary<string, string>
{
{ "target", this.relativeFilePath },
{ "compilerName", compilerData.CompilerName },
{ "compilerBackEndVersion", compilerData.CompilerBackEndVersion },
{ "compilerFrontEndVersion", compilerData.CompilerFrontEndVersion },
{ "fileVersion", string.Empty },
{ "binaryType", compilerData.BinaryType },
{ "language", compilerData.Language },
{ "debuggingFileName", compilerData.DebuggingFileName ?? string.Empty },
{ "debuggingGuid", compilerData.DebuggingFileGuid ?? string.Empty },
{ "dialect", compilerData.Dialect },
{ "moduleName", file ?? string.Empty },
{ "moduleLibrary", string.Empty },
{ "moduleName", compilerData.ModuleName ?? string.Empty },
{ "moduleLibrary", (compilerData.ModuleName == compilerData.ModuleLibrary ? string.Empty : compilerData.ModuleLibrary ?? string.Empty) },
{ "sessionId", s_sessionId },
{ "hash", this.sha256 },
{ "error", string.Empty }
Expand All @@ -204,7 +157,7 @@ public void Write(CompilerData compilerData, string file)
}
else
{
string log = $"{this.relativeFilePath},{compilerData},,{file},,{this.sha256},";
string log = $"{this.relativeFilePath},{compilerData},{this.sha256},";
Console.WriteLine(log);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* BUGFIX: Change compiler report rule to report all modules in file. [#476](https://github.com/microsoft/binskim/pull/476)
* FEATURE: Add dialects to the reporting rules. [#475](https://github.com/microsoft/binskim/pull/475)
* BUGFIX: Fix exception `System.AccessViolationException` caused by trying to read data out of boundary. [#470](https://github.com/microsoft/binskim/pull/470)
* BUGFIX: Fix exception handling when PDB cannot be loaded by `IDiaDataSource`. [#461](https://github.com/microsoft/binskim/pull/461)
Expand Down

0 comments on commit becf484

Please sign in to comment.