From f0a0889d8f751d16560c95cc87d9ffcebaaea797 Mon Sep 17 00:00:00 2001 From: Static-Flow Date: Thu, 30 Nov 2023 19:06:00 -0600 Subject: [PATCH] added fixes --- src/SharpFuzz.CommandLine/Program.cs | 4 ++-- src/SharpFuzz/Fuzzer.cs | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/SharpFuzz.CommandLine/Program.cs b/src/SharpFuzz.CommandLine/Program.cs index 393959e..d3cc5c6 100644 --- a/src/SharpFuzz.CommandLine/Program.cs +++ b/src/SharpFuzz.CommandLine/Program.cs @@ -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; } diff --git a/src/SharpFuzz/Fuzzer.cs b/src/SharpFuzz/Fuzzer.cs index 9d82fd0..36dd541 100644 --- a/src/SharpFuzz/Fuzzer.cs +++ b/src/SharpFuzz/Fuzzer.cs @@ -9,6 +9,7 @@ using dnlib.DotNet; using dnlib.DotNet.Emit; using dnlib.DotNet.MD; +using dnlib.DotNet.Writer; namespace SharpFuzz { @@ -66,7 +67,6 @@ public static IEnumerable Instrument( { throw new InstrumentationException("The specified assembly is already instrumented."); } - var common = typeof(Common.Trace).Assembly; var commonName = common.GetName().Name; @@ -141,7 +141,20 @@ private static SortedSet 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; }