Skip to content

Commit

Permalink
Merge branch 'main' into users/lukaskohl/pdblocation-invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
AllDwarf authored Aug 12, 2024
2 parents 3756410 + ab73bee commit 9e58baf
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

## UNRELEASED
* BUG: Fix `TryGetPortablePdbMetadataReader` unexpectedly causes `UnauthorizedAccessException` error when the PDB file is missing. [1001](https://github.com/microsoft/binskim/pull/1001).
* BUG: Fork telemetry to log always to Console and AppInsights in the same time when Error occur. [1002](https://github.com/microsoft/binskim/pull/1002)

## **v4.3.0**
* DEP: Update `msdia140.dll` from 14.36.32532.0 to 14.40.33810.0. This update fixes the `System.AccessViolationException: Attempted to read or write protected memory` exception that occurs when reading certain PDB files. [996](https://github.com/microsoft/binskim/pull/996)
Expand Down
16 changes: 15 additions & 1 deletion src/BinSkim.Driver/MultithreadedAnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Linq;
using System.Reflection;

using CommandLine;

using Microsoft.CodeAnalysis.BinaryParsers;
using Microsoft.CodeAnalysis.IL.Rules;
using Microsoft.CodeAnalysis.IL.Sdk;
Expand Down Expand Up @@ -48,13 +50,25 @@ private bool IsValidScanTarget(string file)

public override BinaryAnalyzerContext InitializeGlobalContextFromOptions(AnalyzeOptions options, ref BinaryAnalyzerContext context)
{
base.InitializeGlobalContextFromOptions(options, ref context);

if (this.Telemetry?.TelemetryClient != null)
{
// Create an aggregating logger that will combine all loggers into a single logger.
var aggregatingLogger = new AggregatingLogger();
if (context.Logger is AggregatingLogger)
{
aggregatingLogger = context.Logger as AggregatingLogger;
}
else
{
aggregatingLogger.Loggers.Add(context.Logger);
}

var ruleTelemetryLogger = new RuleTelemetryLogger(this.Telemetry.TelemetryClient);
ruleTelemetryLogger.AnalysisStarted();

// Combine rule telemetry with any other loggers that may be present.
aggregatingLogger.Loggers.Add(ruleTelemetryLogger);
context.Logger = aggregatingLogger;
}
Expand All @@ -65,7 +79,7 @@ public override BinaryAnalyzerContext InitializeGlobalContextFromOptions(Analyze
? options.MaxFileSizeInKilobytes.Value
: long.MaxValue;

base.InitializeGlobalContextFromOptions(options, ref context);


// Update context object based on command-line parameters.
context.SymbolPath = options.SymbolsPath ?? context.SymbolPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Text;

using FluentAssertions;

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.CodeAnalysis.IL;
using Microsoft.CodeAnalysis.IL.Sdk;
using Microsoft.CodeAnalysis.Sarif.Writers;

using Xunit;

Expand Down Expand Up @@ -150,5 +155,89 @@ private static string[] Shuffle(string[] data)

return data;
}

[Fact]
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_TelemetryNotSpecifiedHasOneConsoleLogger()
{
var options = new AnalyzeOptions();
options.TargetFileSpecifiers = new List<string> { "test.dll" };
options.DisableTelemetry = true;
options.OutputFilePath = "test/path/";
var context = new BinaryAnalyzerContext();

var command = new MultithreadedAnalyzeCommand();
command.InitializeGlobalContextFromOptions(options, ref context);

Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger);

var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger;
Assert.Contains(aggregatingLogger.Loggers, l => l is ConsoleLogger);
Assert.Equal(1, aggregatingLogger.Loggers.Count);
}

[Fact]
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_TelemetrySpecifiedHasTwoLoggers()
{
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
var telemetryClient = new TelemetryClient(telemetryConfiguration);
var telemetry = new Telemetry(telemetryConfiguration);

var options = new AnalyzeOptions();
options.TargetFileSpecifiers = new List<string> { "test.dll" };
options.OutputFilePath = "test/path/";
var context = new BinaryAnalyzerContext();

var command = new MultithreadedAnalyzeCommand(telemetry);
command.InitializeGlobalContextFromOptions(options, ref context);

Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger);

var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger;
Assert.Contains(aggregatingLogger.Loggers, l => l is ConsoleLogger);
Assert.Contains(aggregatingLogger.Loggers, l => l is RuleTelemetryLogger);
Assert.Equal(2, aggregatingLogger.Loggers.Count);
}

[Fact]
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_QuietOptionSetHasOneRuleTelemetryLogger()
{
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
var telemetryClient = new TelemetryClient(telemetryConfiguration);
var telemetry = new Telemetry(telemetryConfiguration);

var options = new AnalyzeOptions();
options.TargetFileSpecifiers = new List<string> { "test.dll" };
options.OutputFilePath = "test/path/";
options.Quiet = true;
var context = new BinaryAnalyzerContext();

var command = new MultithreadedAnalyzeCommand(telemetry);
command.InitializeGlobalContextFromOptions(options, ref context);

Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger);

var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger;
Assert.Contains(aggregatingLogger.Loggers, l => l is RuleTelemetryLogger);
Assert.Equal(1, aggregatingLogger.Loggers.Count);
}

[Fact]
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_QuietOptionSetAndNoTelemetryHasOneConsoleLogger()
{
var options = new AnalyzeOptions();
options.TargetFileSpecifiers = new List<string> { "test.dll" };
options.DisableTelemetry = true;
options.Quiet = true;
options.OutputFilePath = "test/path/";
var context = new BinaryAnalyzerContext();

var command = new MultithreadedAnalyzeCommand();
command.InitializeGlobalContextFromOptions(options, ref context);

Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger);

var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger;
Assert.Equal(0, aggregatingLogger.Loggers.Count);
}
}
}

0 comments on commit 9e58baf

Please sign in to comment.