Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Add logging to LibFuzzerDotnetLoader #2141

Merged
merged 2 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions src/agent/LibFuzzerDotnetLoader/LibFuzzerDotnetLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<ItemGroup>
<!-- Requires commit ec28c46 or greater. -->
<ProjectReference Include="../sharpfuzz/src/SharpFuzz/SharpFuzz.csproj" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
</ItemGroup>

</Project>
78 changes: 72 additions & 6 deletions src/agent/LibFuzzerDotnetLoader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
Expand All @@ -21,10 +22,55 @@ class EnvVar
public const string METHOD = "LIBFUZZER_DOTNET_TARGET_METHOD";
}

class Logging
{
public static ILogger CreateLogger<T>()
{
using var loggerFactory = LoggerFactory.Create(builder =>
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("LibFuzzerDotnetLoader.Program", LogLevel.Debug)
.AddSimpleConsole(o =>
{
o.SingleLine = true;
o.TimestampFormat = "HH:mm:ss ";
}
)
);

return loggerFactory.CreateLogger<T>();
}
}

public class Program {
static ILogger logger;

static Program()
{
logger = Logging.CreateLogger<Program>();
}

public static void Main(string[] args) {
try
{
TryMain();
}
catch (Exception e)
{
logger.LogError($"{e.Message}");
throw;
}
}

static void TryMain()
{
logger.LogDebug("Checking environment for target specification");

var target = LibFuzzerDotnetTarget.FromEnvironment();

logger.LogDebug($"Attempting to load assembly from `{target.AssemblyPath}`");

var assem = Assembly.LoadFrom(target.AssemblyPath);

var ty = assem.GetType(target.ClassName)??
Expand All @@ -37,6 +83,11 @@ public static void Main(string[] args) {
return;
}

logger.LogWarning($"Unable to bind method `{target.ClassName}.{target.MethodName}` to signature `void (ReadOnlySpan<byte>)`.");
logger.LogWarning("Attempting to bind to signature `void (byte[])`.");
logger.LogWarning("This will require an extra copy of the test input on each iteration.");
logger.LogWarning("Modify your target method to accept `ReadOnlySpan<byte>` if your project supports it.");

if (TryTestOneArray(method)) {
return;
}
Expand All @@ -52,6 +103,7 @@ static bool TryTestOne<T>(MethodInfo method, Func<T, SharpFuzz.ReadOnlySpanActio

try {
testOneInput = (T) Delegate.CreateDelegate(typeof(T), method);
logger.LogDebug($"Bound method `{method}` to delegate `{typeof(T)}`");
} catch {
// We failed to bind to the target method.
//
Expand All @@ -60,6 +112,8 @@ static bool TryTestOne<T>(MethodInfo method, Func<T, SharpFuzz.ReadOnlySpanActio
}

var action = createAction(testOneInput);

logger.LogInformation($"Running method `{method}`...");
SharpFuzz.Fuzzer.LibFuzzer.Run(action);

return true;
Expand All @@ -82,6 +136,13 @@ class LibFuzzerDotnetTarget
public string ClassName { get; }
public string MethodName { get; }

static ILogger logger;

static LibFuzzerDotnetTarget()
{
logger = Logging.CreateLogger<LibFuzzerDotnetTarget>();
}

public LibFuzzerDotnetTarget(string assemblyPath, string className, string methodName)
{
AssemblyPath = assemblyPath;
Expand All @@ -92,18 +153,23 @@ public LibFuzzerDotnetTarget(string assemblyPath, string className, string metho
public static LibFuzzerDotnetTarget FromEnvironment()
{
try {
logger.LogDebug($"Checking {EnvVar.TARGET} for `:`-delimited target specification.");
return FromEnvironmentVarDelimited();
}
catch
{}
catch (Exception e)
{
logger.LogDebug($"Couldn't find target specification in `{EnvVar.TARGET}`: {e.Message}");
}

try {
logger.LogDebug($"Checking {EnvVar.ASSEMBLY}, {EnvVar.CLASS}, and {EnvVar.METHOD} for target specification.");
return FromEnvironmentVars();
}
catch
{}

throw new Exception("No fuzzing target specified by environment variables");
catch (Exception e)
{
logger.LogDebug($"Couldn't find target specification in individual environment variables : {e.Message}");
throw new Exception("No fuzzing target specified", e);
}
}

static LibFuzzerDotnetTarget FromEnvironmentVars()
Expand Down