Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change compiler report rule to report all modules in file #476

Merged
merged 3 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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