-
-
Notifications
You must be signed in to change notification settings - Fork 504
Add a basic fuzzing project #903
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
Merged
+183
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 @@ | ||
| corpus | ||
| libfuzzer-dotnet-windows.exe | ||
| crash-* | ||
| timeout-* |
This file contains hidden or 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,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="SharpFuzz" Version="2.2.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Markdig\Markdig.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,71 @@ | ||
| using Markdig; | ||
| using Markdig.Renderers.Roundtrip; | ||
| using Markdig.Syntax; | ||
| using SharpFuzz; | ||
| using System.Diagnostics; | ||
| using System.Text; | ||
|
|
||
| ReadOnlySpanAction fuzzTarget = ParseRenderFuzzer.FuzzTarget; | ||
|
|
||
| if (args.Length > 0) | ||
| { | ||
| // Run the target on existing inputs | ||
| string[] files = Directory.Exists(args[0]) | ||
| ? Directory.GetFiles(args[0]) | ||
| : [args[0]]; | ||
|
|
||
| Debugger.Launch(); | ||
|
|
||
| foreach (string inputFile in files) | ||
| { | ||
| fuzzTarget(File.ReadAllBytes(inputFile)); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Fuzzer.LibFuzzer.Run(fuzzTarget); | ||
| } | ||
|
|
||
| sealed class ParseRenderFuzzer | ||
| { | ||
| private static readonly MarkdownPipeline s_advancedPipeline = new MarkdownPipelineBuilder() | ||
| .UseAdvancedExtensions() | ||
| .Build(); | ||
|
|
||
| private static readonly ResettableRoundtripRenderer _roundtripRenderer = new(); | ||
|
|
||
| public static void FuzzTarget(ReadOnlySpan<byte> bytes) | ||
| { | ||
| string text = Encoding.UTF8.GetString(bytes); | ||
|
|
||
| try | ||
| { | ||
| MarkdownDocument document = Markdown.Parse(text); | ||
| _ = document.ToHtml(); | ||
|
|
||
| document = Markdown.Parse(text, s_advancedPipeline); | ||
| _ = document.ToHtml(s_advancedPipeline); | ||
|
|
||
| document = Markdown.Parse(text, trackTrivia: true); | ||
| _ = document.ToHtml(); | ||
| _roundtripRenderer.Reset(); | ||
| _roundtripRenderer.Render(document); | ||
|
|
||
| _ = Markdown.Normalize(text); | ||
| _ = Markdown.ToPlainText(text); | ||
| } | ||
| catch (Exception ex) when (IsIgnorableException(ex)) { } | ||
| } | ||
|
|
||
| private static bool IsIgnorableException(Exception exception) | ||
| { | ||
| return exception.Message.Contains("Markdown elements in the input are too deeply nested", StringComparison.Ordinal); | ||
| } | ||
|
|
||
| private sealed class ResettableRoundtripRenderer : RoundtripRenderer | ||
| { | ||
| public ResettableRoundtripRenderer() : base(new StringWriter(new StringBuilder(1024 * 1024))) { } | ||
|
|
||
| public new void Reset() => base.Reset(); | ||
| } | ||
| } |
This file contains hidden or 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,86 @@ | ||
| param ( | ||
| [string]$configuration = $null | ||
| ) | ||
|
|
||
| Set-StrictMode -Version Latest | ||
|
|
||
| $libFuzzer = "libfuzzer-dotnet-windows.exe" | ||
| $outputDir = "bin" | ||
|
|
||
| function Get-LibFuzzer { | ||
| param ( | ||
| [string]$Path | ||
| ) | ||
|
|
||
| $libFuzzerUrl = "https://github.com/Metalnem/libfuzzer-dotnet/releases/download/v2025.05.02.0904/libfuzzer-dotnet-windows.exe" | ||
| $expectedHash = "17af5b3f6ff4d2c57b44b9a35c13051b570eb66f0557d00015df3832709050bf" | ||
|
|
||
| Write-Output "Downloading libFuzzer from $libFuzzerUrl..." | ||
|
|
||
| try { | ||
| $tempFile = "$Path.tmp" | ||
| Invoke-WebRequest -Uri $libFuzzerUrl -OutFile $tempFile -UseBasicParsing | ||
|
|
||
| $downloadedHash = (Get-FileHash -Path $tempFile -Algorithm SHA256).Hash | ||
|
|
||
| if ($downloadedHash -eq $ExpectedHash) { | ||
| Move-Item -Path $tempFile -Destination $Path -Force | ||
| Write-Output "libFuzzer downloaded successfully to $Path" | ||
| } | ||
| else { | ||
| Write-Error "Hash validation failed." | ||
| Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue | ||
| exit 1 | ||
| } | ||
| } | ||
| catch { | ||
| Write-Error "Failed to download libFuzzer: $($_.Exception.Message)" | ||
| Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue | ||
| exit 1 | ||
| } | ||
| } | ||
|
|
||
| # Check if libFuzzer exists, download if not | ||
| if (-not (Test-Path $libFuzzer)) { | ||
| Get-LibFuzzer -Path $libFuzzer | ||
| } | ||
|
|
||
| $toolListOutput = dotnet tool list --global sharpFuzz.CommandLine 2>$null | ||
| if (-not ($toolListOutput -match "sharpfuzz")) { | ||
| Write-Output "Installing sharpfuzz CLI" | ||
| dotnet tool install --global sharpFuzz.CommandLine | ||
| } | ||
|
|
||
| if (Test-Path $outputDir) { | ||
| Remove-Item -Recurse -Force $outputDir | ||
| } | ||
|
|
||
| if ($configuration -eq $null) { | ||
| $configuration = "Debug" | ||
| } | ||
|
|
||
| dotnet publish -c $configuration -o $outputDir | ||
|
|
||
| $project = Join-Path $outputDir "Markdig.Fuzzing.dll" | ||
|
|
||
| $fuzzingTarget = Join-Path $outputDir "Markdig.dll" | ||
|
|
||
| Write-Output "Instrumenting $fuzzingTarget" | ||
| & sharpfuzz $fuzzingTarget | ||
|
|
||
| if ($LastExitCode -ne 0) { | ||
| Write-Error "An error occurred while instrumenting $fuzzingTarget" | ||
| exit 1 | ||
| } | ||
|
|
||
| New-Item -ItemType Directory -Force -Path corpus | Out-Null | ||
|
|
||
| $libFuzzerArgs = @("--target_path=dotnet", "--target_arg=$project", "-timeout=10", "corpus") | ||
|
|
||
| # Add any additional arguments passed to the script | ||
| if ($args) { | ||
| $libFuzzerArgs += $args | ||
| } | ||
|
|
||
| Write-Output "Starting libFuzzer with arguments: $libFuzzerArgs" | ||
| & ./$libFuzzer @libFuzzerArgs | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.