Skip to content

Commit 6c2b646

Browse files
committed
Update SharpFuzz to use the new OnBranch tracing
1 parent ed14295 commit 6c2b646

File tree

5 files changed

+60
-17
lines changed

5 files changed

+60
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ dotnet add package Sigil --version 4.7.0
238238
the following command:
239239

240240
```shell
241-
dotnet add package SharpFuzz --version 1.4.3
241+
dotnet add package SharpFuzz --version 1.5.0
242242
```
243243

244244
**6.** Now it's time to write some code. The **Main**

build/Common.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1313
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
14-
<Copyright>Copyright © 2018, Nemanja Mijailovic</Copyright>
15-
<PackageTags>fuzzing testing fuzz-testing fuzzer</PackageTags>
14+
<Copyright>Copyright © 2018-2019, Nemanja Mijailovic</Copyright>
15+
<PackageTags>fuzzing testing fuzz-testing fuzzer afl afl-fuzz libfuzzer</PackageTags>
1616
</PropertyGroup>
1717
</Project>

src/SharpFuzz/Fuzzer.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ public static partial class Fuzzer
2828
/// A function that accepts the full name of the class and returns
2929
/// true if the class should be instrumented, false otherwise.
3030
/// </param>
31+
/// <param name="enableOnBranchCallback">
32+
/// True if <see cref="SharpFuzz.Common.Trace.OnBranch"/> callback
33+
/// should be called each time a branch is hit, false otherwise.
34+
/// </param>
3135
/// <returns>An ordered collection of instrumented types.</returns>
32-
public static IEnumerable<string> Instrument(string source, Func<string, bool> matcher)
36+
public static IEnumerable<string> Instrument(
37+
string source,
38+
Func<string, bool> matcher,
39+
bool enableOnBranchCallback)
3340
{
3441
ThrowIfNull(source, nameof(source));
3542
ThrowIfNull(matcher, nameof(matcher));
@@ -62,14 +69,14 @@ public static IEnumerable<string> Instrument(string source, Func<string, bool> m
6269
{
6370
var traceType = GenerateTraceType(src);
6471
src.Types.Add(traceType);
65-
types = Instrument(src, dst, matcher, traceType);
72+
types = Instrument(src, dst, matcher, enableOnBranchCallback, traceType);
6673
}
6774
else
6875
{
6976
using (var commonMod = ModuleDefMD.Load(common.Location))
7077
{
7178
var traceType = commonMod.Types.Single(t => t.FullName == typeof(Common.Trace).FullName);
72-
types = Instrument(src, dst, matcher, traceType);
79+
types = Instrument(src, dst, matcher, enableOnBranchCallback, traceType);
7380
}
7481
}
7582
}
@@ -85,13 +92,20 @@ public static IEnumerable<string> Instrument(string source, Func<string, bool> m
8592
return types;
8693
}
8794

88-
private static SortedSet<string> Instrument(ModuleDefMD src, Stream dst, Func<string, bool> matcher, TypeDef traceType)
95+
private static SortedSet<string> Instrument(
96+
ModuleDefMD src,
97+
Stream dst,
98+
Func<string, bool> matcher,
99+
bool enableOnBranchCallback,
100+
TypeDef traceType)
89101
{
90102
var sharedMemDef = traceType.Fields.Single(f => f.Name == nameof(Common.Trace.SharedMem));
91103
var prevLocationDef = traceType.Fields.Single(f => f.Name == nameof(Common.Trace.PrevLocation));
104+
var onBranchDef = traceType.Fields.Single(f => f.Name == nameof(Common.Trace.OnBranch));
92105

93106
var sharedMemRef = src.Import(sharedMemDef);
94107
var prevLocationRef = src.Import(prevLocationDef);
108+
var onBranchRef = src.Import(onBranchDef);
95109

96110
var types = new SortedSet<string>();
97111

@@ -105,7 +119,7 @@ private static SortedSet<string> Instrument(ModuleDefMD src, Stream dst, Func<st
105119
{
106120
if (method.HasBody)
107121
{
108-
Method.Instrument(sharedMemRef, prevLocationRef, method);
122+
Method.Instrument(sharedMemRef, prevLocationRef, onBranchRef, enableOnBranchCallback, method);
109123

110124
if (!instrumented)
111125
{

src/SharpFuzz/Method.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using dnlib.DotNet;
@@ -12,15 +13,30 @@ internal sealed class Method
1213
{
1314
private readonly MemberRef sharedMem;
1415
private readonly MemberRef prevLocation;
16+
private readonly MemberRef onBranch;
17+
private readonly bool enableOnBranchCallback;
1518

19+
private readonly IMethod invoke;
1620
private readonly CilBody body;
1721
private readonly List<Instruction> instructions;
1822
private readonly Dictionary<Instruction, Instruction> instrumented;
1923

20-
private Method(MemberRef sharedMem, MemberRef prevLocation, MethodDef method)
24+
private Method(
25+
MemberRef sharedMem,
26+
MemberRef prevLocation,
27+
MemberRef onBranch,
28+
bool enableOnBranchCallback,
29+
MethodDef method)
2130
{
2231
this.sharedMem = sharedMem;
2332
this.prevLocation = prevLocation;
33+
this.onBranch = onBranch;
34+
this.enableOnBranchCallback = enableOnBranchCallback;
35+
36+
if (enableOnBranchCallback)
37+
{
38+
invoke = method.Module.Import(typeof(Action<int, string>).GetMethod(nameof(Action.Invoke)));
39+
}
2440

2541
body = method.Body;
2642
instructions = body.Instructions.ToList();
@@ -30,16 +46,21 @@ private Method(MemberRef sharedMem, MemberRef prevLocation, MethodDef method)
3046
body.Instructions.Clear();
3147

3248
FindInstrumentationTargets();
33-
Instrument();
49+
Instrument(method.FullName);
3450
UpdateBranchTargets();
3551
UpdateExceptionHandlers();
3652

3753
body.OptimizeBranches();
3854
}
3955

40-
public static void Instrument(MemberRef sharedMem, MemberRef prevLocation, MethodDef method)
56+
public static void Instrument(
57+
MemberRef sharedMem,
58+
MemberRef prevLocation,
59+
MemberRef onBranch,
60+
bool enableOnBranchCallback,
61+
MethodDef method)
4162
{
42-
new Method(sharedMem, prevLocation, method);
63+
new Method(sharedMem, prevLocation, onBranch, enableOnBranchCallback, method);
4364
}
4465

4566
// Find all the locations that we want to instrument. These are:
@@ -83,13 +104,13 @@ private void FindInstrumentationTargets()
83104
// Regenerate the IL for the method. If some instruction was
84105
// previously marked as an instrumentation target, generate
85106
// the instrumentation code and put it before the instruction.
86-
private void Instrument()
107+
private void Instrument(string methodName)
87108
{
88109
foreach (var ins in instructions)
89110
{
90111
if (instrumented.ContainsKey(ins))
91112
{
92-
using (var it = GenerateInstrumentationInstructions().GetEnumerator())
113+
using (var it = GenerateInstrumentationInstructions(methodName).GetEnumerator())
93114
{
94115
it.MoveNext();
95116
instrumented[ins] = it.Current;
@@ -111,7 +132,7 @@ private void Instrument()
111132
// var id = IdGenerator.Next();
112133
// SharpFuzz.Common.Trace.SharedMem[id ^ SharpFuzz.Common.Trace.PrevLocation]++;
113134
// SharpFuzz.Common.Trace.PrevLocation = id >> 1;
114-
private IEnumerable<Instruction> GenerateInstrumentationInstructions()
135+
private IEnumerable<Instruction> GenerateInstrumentationInstructions(string methodName)
115136
{
116137
int id = IdGenerator.Next();
117138

@@ -128,6 +149,14 @@ private IEnumerable<Instruction> GenerateInstrumentationInstructions()
128149
yield return Instruction.Create(OpCodes.Stind_I1);
129150
yield return Instruction.Create(OpCodes.Ldc_I4, id >> 1);
130151
yield return Instruction.Create(OpCodes.Stsfld, prevLocation);
152+
153+
if (enableOnBranchCallback)
154+
{
155+
yield return Instruction.Create(OpCodes.Ldsfld, onBranch);
156+
yield return Instruction.Create(OpCodes.Ldc_I4, id);
157+
yield return Instruction.Create(OpCodes.Ldstr, methodName);
158+
yield return Instruction.Create(OpCodes.Callvirt, invoke);
159+
}
131160
}
132161

133162
// Change all branch destinations to point to the first instruction

src/SharpFuzz/SharpFuzz.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<PackageId>SharpFuzz</PackageId>
1111
<Title>SharpFuzz</Title>
12-
<PackageVersion>1.4.3</PackageVersion>
13-
<AssemblyVersion>1.4.3.0</AssemblyVersion>
12+
<PackageVersion>1.5.0</PackageVersion>
13+
<AssemblyVersion>1.5.0.0</AssemblyVersion>
1414
<Description>AFL-based fuzz testing for .NET</Description>
1515
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1616
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

0 commit comments

Comments
 (0)