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

Adding SARIF V1 read capability to compiler telemetry #613

Merged
merged 6 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/BinSkim.Driver/MultithreadedAnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override void InitializeConfiguration(AnalyzeOptions options, BinaryAn
// Command-line provided policy is now initialized. Update context
// based on any possible configuration provided in this way.

context.CompilerDataLogger = new CompilerDataLogger(options.OutputFilePath, context, this.FileSystem);
context.CompilerDataLogger = new CompilerDataLogger(options.OutputFilePath, options.SarifOutputVersion, context, this.FileSystem);

// If the user has hard-coded a non-deterministic file path root to elide from telemetry,
// we will honor that. If it has not been specified, and if all file target specifiers
Expand Down
38 changes: 36 additions & 2 deletions src/BinSkim.Sdk/CompilerDataLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.CodeAnalysis.Sarif;
using Microsoft.CodeAnalysis.Sarif.Readers;
using Microsoft.CodeAnalysis.Sarif.VersionOne;
using Microsoft.CodeAnalysis.Sarif.Visitors;

using Newtonsoft.Json;

namespace Microsoft.CodeAnalysis.IL.Sdk
{
Expand Down Expand Up @@ -53,6 +58,7 @@ public class CompilerDataLogger : IDisposable
// analysis) that is extracted from the SARIF log file. We currently therefore
// require that the scan is configured to produce a disk-based report.
private readonly string sarifOutputFilePath;
private readonly Sarif.SarifVersion sarifVersion;
private readonly IFileSystem fileSystem;
private readonly string symbolPath;

Expand All @@ -77,13 +83,14 @@ public class CompilerDataLogger : IDisposable
"CompilerTelemetry", nameof(RootPathToElide), defaultValue: () => string.Empty,
"A non-deterministic file path root that should be elided from paths in telemetry, e.g., 'c:\\Users\\SomeUser\\'.");

public CompilerDataLogger(string sarifOutputFilePath, BinaryAnalyzerContext context, IFileSystem fileSystem = null)
public CompilerDataLogger(string sarifOutputFilePath, Sarif.SarifVersion sarifVersion, BinaryAnalyzerContext context, IFileSystem fileSystem = null)
{
this.syncRoot = new object();
this.sessionId = Guid.NewGuid().ToString();
this.fileSystem = fileSystem ?? new FileSystem();

this.sarifOutputFilePath = sarifOutputFilePath;
this.sarifVersion = sarifVersion;
this.RootPathToElide = context.Policy.GetProperty(RootPathToElideProperty);
this.OwningContextHashCode = context.GetHashCode();
this.symbolPath = context.SymbolPath;
Expand Down Expand Up @@ -354,7 +361,7 @@ private void WriteSummaryData()
{
Debug.Assert(Enabled);

var sarifLog = SarifLog.Load(this.sarifOutputFilePath);
SarifLog sarifLog = LoadVersionAgnosticSarifFile();
AnalysisSummary summary = AnalysisSummaryExtractor.ExtractAnalysisSummary(sarifLog,
RootPathToElide,
this.symbolPath);
Expand All @@ -366,6 +373,33 @@ private void WriteSummaryData()
}
}

private SarifLog LoadVersionAgnosticSarifFile()
{
SarifLog sarifLog;

if (this.sarifVersion == Sarif.SarifVersion.Current)
{
sarifLog = SarifLog.Load(sarifOutputFilePath);
}
else
{
SarifLogVersionOne actualLog;

var serializer = new JsonSerializer() { ContractResolver = SarifContractResolverVersionOne.Instance };

using (JsonTextReader reader = new JsonTextReader(new StreamReader(fileSystem.FileOpenRead(sarifOutputFilePath))))
{
actualLog = serializer.Deserialize<SarifLogVersionOne>(reader);
}

var visitor = new SarifVersionOneToCurrentVisitor();
visitor.VisitSarifLogVersionOne(actualLog);
sarifLog = visitor.SarifLog;
}

return sarifLog;
}
Copy link
Contributor Author

@marmegh marmegh Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bulk of the code change, loading based on SARIF version. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add test for SARIF V2, as well.


public void Dispose()
{
if (!Enabled)
Expand Down
1 change: 1 addition & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* FEATURE: Add new PE `CV_CFL_LANG` language code for `ALIASOBJ` and `Rust`. [530](https://github.com/microsoft/binskim/pull/530)
* BUGFIX: Fix `BA2014.DoNotDisableStackProtectionForFunctions` to eliminate false positive reports that `GsDriverEntry` has disabled the stack protector. [551](https://github.com/microsoft/binskim/pull/551)
* BREAKING: Rename `BA2026.EnableAdditionalSdlSecurityChecks` to `BA2026.EnableMicrosoftCompilerSdlSwitch` to clarify rule purpose. [#586](https://github.com/microsoft/binskim/pull/586)
* BUGFIX: Fix `SummaryEvent` production when using SARIF v1.0.0. [613](https://github.com/microsoft/binskim/pull/613)

## **v1.9.3** [NuGet Package](https://www.nuget.org/packages/Microsoft.CodeAnalysis.BinSkim/1.9.3)

Expand Down
Loading