This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
GoodBad
C# example project, integration test (#2148)
- Loading branch information
Showing
4 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env bash | ||
set -ex -o pipefail | ||
|
||
# Required environment variables: | ||
# - GOODBAD_DOTNET | ||
# - LIBFUZZER_DOTNET | ||
# - LIBFUZZER_DOTNET_LOADER | ||
# - SHARPFUZZ | ||
|
||
export GOODBAD_DLL='GoodBad/GoodBad.dll' | ||
|
||
TMP=$(mktemp -d) | ||
cd $TMP | ||
|
||
cp -r ${GOODBAD_DOTNET} GoodBad | ||
|
||
# Instrument DLL under test. | ||
${SHARPFUZZ} GoodBad/GoodBad.dll | ||
|
||
# Create seed and crash inputs. | ||
printf 'good' > good.txt | ||
printf 'bad!' > bad.txt | ||
|
||
# Test individual env vars. | ||
export LIBFUZZER_DOTNET_TARGET_ASSEMBLY="${GOODBAD_DLL}" | ||
export LIBFUZZER_DOTNET_TARGET_CLASS='GoodBad.Fuzzer' | ||
export LIBFUZZER_DOTNET_TARGET_METHOD='TestInput' | ||
|
||
${LIBFUZZER_DOTNET} --target_path=${LIBFUZZER_DOTNET_LOADER} good.txt | ||
|
||
# Expect nonzero exit. | ||
! ${LIBFUZZER_DOTNET} --target_path=${LIBFUZZER_DOTNET_LOADER} bad.txt | ||
|
||
# Test delimited env var. | ||
export LIBFUZZER_DOTNET_TARGET="${LIBFUZZER_DOTNET_TARGET_ASSEMBLY}:${LIBFUZZER_DOTNET_TARGET_CLASS}:${LIBFUZZER_DOTNET_TARGET_METHOD}" | ||
unset LIBFUZZER_DOTNET_TARGET_ASSEMBLY | ||
unset LIBFUZZER_DOTNET_TARGET_CLASS | ||
unset LIBFUZZER_DOTNET_TARGET_METHOD | ||
|
||
${LIBFUZZER_DOTNET} --target_path=${LIBFUZZER_DOTNET_LOADER} good.txt | ||
|
||
# Expect nonzero exit. | ||
! ${LIBFUZZER_DOTNET} --target_path=${LIBFUZZER_DOTNET_LOADER} bad.txt | ||
|
||
rm -rf $TMP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace GoodBad; | ||
|
||
public class BinaryParser | ||
{ | ||
int count = 0; | ||
|
||
public void ProcessInput(ReadOnlySpan<byte> data) { | ||
if (data.Length < 4) { | ||
return; | ||
} | ||
|
||
if (data[0] == 'b') { count++; } | ||
if (data[1] == 'a') { count++; } | ||
if (data[2] == 'd') { count++; } | ||
if (data[3] == '!') { count++; } | ||
|
||
// Simulate an out-of-bounds access while parsing. | ||
if (count >= 4) { | ||
var _ = data[0xdead]; | ||
} | ||
} | ||
} | ||
|
||
public class Fuzzer { | ||
/// Preferred test method. | ||
public static void TestInput(ReadOnlySpan<byte> data) { | ||
var parser = new BinaryParser(); | ||
parser.ProcessInput(data); | ||
} | ||
|
||
/// Backwards-compatible test method for legacy code that can't use `Span` types. | ||
/// | ||
/// Incurs an extra copy of `data` per fuzzing iteration. | ||
public static void TestInputCompat(byte[] data) { | ||
var parser = new BinaryParser(); | ||
parser.ProcessInput(data); | ||
} | ||
|
||
/// Invalid static method that has a fuzzing-incompatible signature. | ||
public static void BadSignature(ReadOnlySpan<int> data) { | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<LangVersion>10.0</LangVersion> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |