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

Fixes issue with obfuscated libraries failing instrumentation #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/SharpFuzz.CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public static int Main(string[] args)
Console.Error.WriteLine(ex.Message);
return 1;
}
catch
catch (Exception ex)
{
Console.Error.WriteLine("Failed to instrument the specified file, most likely because it's not a valid .NET assembly.");
Console.Error.WriteLine("Failed to instrument the specified file, most likely because it's not a valid .NET assembly: "+ex);
return 1;
}

Expand Down
17 changes: 15 additions & 2 deletions src/SharpFuzz/Fuzzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using dnlib.DotNet.MD;
using dnlib.DotNet.Writer;

namespace SharpFuzz
{
Expand Down Expand Up @@ -66,7 +67,6 @@ public static IEnumerable<string> Instrument(
{
throw new InstrumentationException("The specified assembly is already instrumented.");
}

var common = typeof(Common.Trace).Assembly;
var commonName = common.GetName().Name;

Expand Down Expand Up @@ -141,7 +141,20 @@ private static SortedSet<string> Instrument(
}
}

src.Write(dst);
try
{
src.Write(dst);
}
catch (ModuleWriterException)
{
/*
* Likely if we got here it's because of obfuscation so we attempt to bypass it by keeping the old stack.
* If that doesn't solve it the error is likely to happen again which will bubble up to the user
*/
var writerOptions = new ModuleWriterOptions(src);
writerOptions.MetadataOptions.Flags |= MetadataFlags.KeepOldMaxStack;
src.Write(dst,writerOptions);
}
return types;
}

Expand Down