Skip to content

Commit

Permalink
Capability for driver to read from a binary log
Browse files Browse the repository at this point in the history
This adds a new option to the driver, `--binlog`. When this is specified the driver will read all C# invocations from the binary log and process them.
  • Loading branch information
jaredpar authored and tamasvajk committed Jun 4, 2024
1 parent 60970ff commit 0c434f2
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions csharp/extractor/Semmle.Extraction.CSharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ codeql_csharp_library(
"//csharp/extractor/Semmle.Util",
"@paket.main//microsoft.build",
"@paket.main//microsoft.codeanalysis.csharp",
"@paket.main//basic.compilerlog.util"
],
)
51 changes: 49 additions & 2 deletions csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Microsoft.CodeAnalysis.Text;
using Semmle.Util;
using Semmle.Util.Logging;
using Basic.CompilerLog;
using Basic.CompilerLog.Util;

namespace Semmle.Extraction.CSharp
{
Expand Down Expand Up @@ -92,11 +94,56 @@ public static ILogger MakeLogger(Verbosity verbosity, bool includeConsole)
/// <param name="args">Command line arguments as passed to csc.exe</param>
/// <returns><see cref="ExitCode"/></returns>
public static ExitCode Run(string[] args)
{
var options = Options.CreateWithEnvironment(args);
if (options.BinaryLogPath is string binlogPath)
{
using var fileStream = new FileStream(binlogPath, FileMode.Open, FileAccess.Read, FileShare.Read);

// Filter out compiler calls that aren't interesting for examination
var predicate = bool (CompilerCall compilerCall) =>
{
if (!compilerCall.IsCSharp)
{
return false;
}

return compilerCall.Kind switch
{
CompilerCallKind.XamlPreCompile => false,
CompilerCallKind.Satellite => false,
CompilerCallKind.WpfTemporaryCompile => false,
_ => true
};
};

var compilerCalls = BinaryLogUtil.ReadAllCompilerCalls(fileStream, predicate);
var exitCode = ExitCode.Ok;
foreach (var compilerCall in compilerCalls)
{
Console.WriteLine($"Processing {compilerCall.GetDiagnosticName()}");
var compilerCallOptions = Options.CreateWithEnvironment([]);
compilerCallOptions.CompilerName = compilerCall.CompilerFilePath;
compilerCallOptions.CompilerArguments.AddRange(compilerCall.GetArguments());
var ec = Run(compilerCallOptions);
if (ec != ExitCode.Ok)
{
exitCode = ec;
}
}

return exitCode;
}
else
{
return Run(options);
}
}

public static ExitCode Run(Options options)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

var options = Options.CreateWithEnvironment(args);
var workingDirectory = Directory.GetCurrentDirectory();
var compilerArgs = options.CompilerArguments.ToArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public sealed class Options : CommonOptions
/// <summary>
/// All other arguments passed to the compilation.
/// </summary>
public IList<string> CompilerArguments { get; } = new List<string>();
public List<string> CompilerArguments { get; } = new List<string>();

/// <summary>
/// Holds if assembly information should be prefixed to TRAP labels.
/// </summary>
public bool AssemblySensitiveTrap { get; private set; } = false;

public string? BinaryLogPath { get; set; }

public static Options CreateWithEnvironment(string[] arguments)
{
var options = new Options();
Expand Down Expand Up @@ -65,6 +67,9 @@ public override bool HandleOption(string key, string value)
case "load-sources-from-project":
ProjectsToLoad.Add(value);
return true;
case "binlog":
BinaryLogPath = value;
return true;
default:
return base.HandleOption(key, value);
}
Expand Down
2 changes: 1 addition & 1 deletion csharp/extractor/Semmle.Extraction.CSharp/paket.references
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Microsoft.Build
Microsoft.CodeAnalysis.CSharp

Basic.CompilerLog.Util
3 changes: 2 additions & 1 deletion csharp/paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source https://api.nuget.org/v3/index.json
# behave like nuget in choosing transitive dependency versions
strategy: min

nuget Basic.CompilerLog.Util
nuget Mono.Posix.NETStandard
nuget Newtonsoft.Json
nuget xunit
Expand All @@ -17,4 +18,4 @@ nuget Microsoft.Win32.Primitives
nuget System.Net.Primitives
nuget System.Security.Principal
nuget System.Threading.ThreadPool
nuget System.IO.FileSystem
nuget System.IO.FileSystem
21 changes: 21 additions & 0 deletions csharp/paket.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0c434f2

Please sign in to comment.