1+ param (
2+ [string ]$configuration = $null
3+ )
4+
5+ Set-StrictMode - Version Latest
6+
7+ $libFuzzer = " libfuzzer-dotnet-windows.exe"
8+ $outputDir = " bin"
9+
10+ function Get-LibFuzzer {
11+ param (
12+ [string ]$Path
13+ )
14+
15+ $libFuzzerUrl = " https://github.com/Metalnem/libfuzzer-dotnet/releases/download/v2025.05.02.0904/libfuzzer-dotnet-windows.exe"
16+ $expectedHash = " 17af5b3f6ff4d2c57b44b9a35c13051b570eb66f0557d00015df3832709050bf"
17+
18+ Write-Output " Downloading libFuzzer from $libFuzzerUrl ..."
19+
20+ try {
21+ $tempFile = " $Path .tmp"
22+ Invoke-WebRequest - Uri $libFuzzerUrl - OutFile $tempFile - UseBasicParsing
23+
24+ $downloadedHash = (Get-FileHash - Path $tempFile - Algorithm SHA256).Hash
25+
26+ if ($downloadedHash -eq $ExpectedHash ) {
27+ Move-Item - Path $tempFile - Destination $Path - Force
28+ Write-Output " libFuzzer downloaded successfully to $Path "
29+ }
30+ else {
31+ Write-Error " Hash validation failed."
32+ Remove-Item - Path $tempFile - Force - ErrorAction SilentlyContinue
33+ exit 1
34+ }
35+ }
36+ catch {
37+ Write-Error " Failed to download libFuzzer: $ ( $_.Exception.Message ) "
38+ Remove-Item - Path $tempFile - Force - ErrorAction SilentlyContinue
39+ exit 1
40+ }
41+ }
42+
43+ # Check if libFuzzer exists, download if not
44+ if (-not (Test-Path $libFuzzer )) {
45+ Get-LibFuzzer - Path $libFuzzer
46+ }
47+
48+ $toolListOutput = dotnet tool list -- global sharpFuzz.CommandLine 2> $null
49+ if (-not ($toolListOutput -match " sharpfuzz" )) {
50+ Write-Output " Installing sharpfuzz CLI"
51+ dotnet tool install -- global sharpFuzz.CommandLine
52+ }
53+
54+ if (Test-Path $outputDir ) {
55+ Remove-Item - Recurse - Force $outputDir
56+ }
57+
58+ if ($configuration -eq $null ) {
59+ $configuration = " Debug"
60+ }
61+
62+ dotnet publish - c $configuration - o $outputDir
63+
64+ $project = Join-Path $outputDir " Markdig.Fuzzing.dll"
65+
66+ $fuzzingTarget = Join-Path $outputDir " Markdig.dll"
67+
68+ Write-Output " Instrumenting $fuzzingTarget "
69+ & sharpfuzz $fuzzingTarget
70+
71+ if ($LastExitCode -ne 0 ) {
72+ Write-Error " An error occurred while instrumenting $fuzzingTarget "
73+ exit 1
74+ }
75+
76+ New-Item - ItemType Directory - Force - Path corpus | Out-Null
77+
78+ $libFuzzerArgs = @ (" --target_path=dotnet" , " --target_arg=$project " , " -timeout=10" , " corpus" )
79+
80+ # Add any additional arguments passed to the script
81+ if ($args ) {
82+ $libFuzzerArgs += $args
83+ }
84+
85+ Write-Output " Starting libFuzzer with arguments: $libFuzzerArgs "
86+ & ./ $libFuzzer @libFuzzerArgs
0 commit comments