mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add Fuzz Tests (#26384)
Co-authored-by: anamnavi <annavied@microsoft.com>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using SharpFuzz;
|
||||
using System.Management.Automation.Remoting;
|
||||
|
||||
namespace FuzzTests
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
FuzzTargetMethod(args);
|
||||
}
|
||||
|
||||
public static void FuzzTargetMethod(string[] args)
|
||||
{
|
||||
if (args == null)
|
||||
{
|
||||
Console.WriteLine("args was null");
|
||||
args = Array.Empty<string>();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Fuzzer.LibFuzzer.Run(Target.ExtractToken);
|
||||
}
|
||||
catch (System.ArgumentNullException nex)
|
||||
{
|
||||
Console.WriteLine($"ArgumentNullException in main: {nex.Message}");
|
||||
Console.WriteLine($"Stack Trace: {nex.StackTrace}");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Exception in main: {ex.Message}");
|
||||
Console.WriteLine($"Exception type: {ex.GetType()}");
|
||||
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using SharpFuzz;
|
||||
using System.Management.Automation.Remoting;
|
||||
|
||||
namespace FuzzTests
|
||||
{
|
||||
public static class Target
|
||||
{
|
||||
public static void ExtractToken(ReadOnlySpan<byte> tokenResponse)
|
||||
{
|
||||
RemoteSessionHyperVSocketClient.ExtractToken(tokenResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Description>PowerShell Fuzzing</Description>
|
||||
<AssemblyName>powershell-fuzz-tests</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<DelaySign>true</DelaySign>
|
||||
<AssemblyOriginatorKeyFile>../../../src/signing/visualstudiopublic.snk</AssemblyOriginatorKeyFile>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../../src/System.Management.Automation/System.Management.Automation.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SharpFuzz" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1 @@
|
||||
TOKEN abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=
|
||||
@@ -0,0 +1 @@
|
||||
TOKEN TQ
|
||||
@@ -0,0 +1,75 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
|
||||
param (
|
||||
[string]$libFuzzer = ".\libfuzzer-dotnet-windows.exe",
|
||||
[string]$project = ".\FuzzingApp\powershell-fuzz-tests.csproj",
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$corpus,
|
||||
[string]$command = "sharpfuzz.exe"
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
|
||||
$outputDir = "out"
|
||||
|
||||
if (Test-Path $outputDir) {
|
||||
Remove-Item -Recurse -Force $outputDir
|
||||
}
|
||||
|
||||
Write-Host "dotnet publish $project -c release -o $outputDir"
|
||||
dotnet publish $project -c release -o $outputDir
|
||||
Write-Host "build completed"
|
||||
|
||||
$projectName = (Get-Item $project).BaseName
|
||||
$projectDll = "$projectName.dll"
|
||||
$project = Join-Path $outputDir $projectDll
|
||||
|
||||
$exclusions = @(
|
||||
"dnlib.dll",
|
||||
"SharpFuzz.dll",
|
||||
"SharpFuzz.Common.dll"
|
||||
)
|
||||
|
||||
$exclusions += $projectDll
|
||||
|
||||
Write-Host "instrumenting: $project"
|
||||
& $command $project Target
|
||||
Write-Host "done instrumenting $project"
|
||||
|
||||
$fuzzingTargets = Get-ChildItem $outputDir -Filter *.dll `
|
||||
| Where-Object { $_.Name -notin $exclusions } `
|
||||
| Where-Object { $_.Name -notlike "System.*.dll" }
|
||||
| Where-Object { $_.Name -notlike "Newtonsoft.*.dll" }
|
||||
| Where-Object { $_.Name -notlike "Microsoft.*.dll" }
|
||||
|
||||
foreach ($fuzzingTarget in $fuzzingTargets) {
|
||||
Write-Output "Instrumenting $fuzzingTarget"
|
||||
& $command $fuzzingTarget.FullName
|
||||
|
||||
if ($LastExitCode -ne 0) {
|
||||
Write-Error "An error occurred while instrumenting $fuzzingTarget"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
$smaDllPath = Join-Path $outputDir "System.Management.Automation.dll"
|
||||
|
||||
Write-Host "instrumenting: $smaDllPath"
|
||||
& $command $smaDllPath Remoting
|
||||
Write-Host "done instrumenting: $smaDllPath"
|
||||
|
||||
$fuzzingTargets += $projectDll
|
||||
$fuzzingTargets += $smaDllPath
|
||||
|
||||
if (($fuzzingTargets | Measure-Object).Count -eq 0) {
|
||||
Write-Error "No fuzzing targets found"
|
||||
exit 1
|
||||
}
|
||||
|
||||
$outputPath = Join-Path $outputDir "output.txt"
|
||||
|
||||
Write-Host "launching fuzzer on $project"
|
||||
Write-Host "$libFuzzer --target_path=dotnet --target_arg=$project $corpus"
|
||||
& $libFuzzer --target_path=dotnet --target_arg=$project $corpus -max_len=1024 2>&1 `
|
||||
| Tee-Object -FilePath $outputPath
|
||||
Reference in New Issue
Block a user