Skip to content

Commit dd3ef2a

Browse files
novafacingRowan Hart
andauthored
Update libafl-libfuzzer build scripts with more options (#3358)
Co-authored-by: Rowan Hart <[email protected]>
1 parent 2d6d550 commit dd3ef2a

File tree

3 files changed

+106
-16
lines changed

3 files changed

+106
-16
lines changed

crates/libafl_libfuzzer_runtime/build.ps1

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
#!/usr/bin/env pwsh
2+
param (
3+
[Parameter(Mandatory = $false, HelpMessage = "The build profile to use, e.g. 'release' or 'dev'. Default is 'release'.")]
4+
[string]$Profile = "release",
5+
[Parameter(Mandatory = $false, HelpMessage = "The toolchain to use, e.g. 'stable', 'nightly', etc. Default is 'stable'.")]
6+
[string]$Toolchain = "stable",
7+
[Parameter(Mandatory = $false, HelpMessage = "Additional cargo arguments to pass")]
8+
[string]$CargoArgs = "",
9+
[Parameter(Mandatory = $false, HelpMessage = "Additional rustc arguments to pass")]
10+
[string]$RustcArgs = "",
11+
[Parameter(Mandatory = $false, HelpMessage = "Additional flags to set for the C compiler")]
12+
[string]$CCFlags = ""
13+
)
214

315
$ErrorActionPreference = "Stop"
416

517
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
618

719
Set-Location $SCRIPT_DIR
820

9-
if ($args.Count -eq 0) {
10-
$profile = "release"
11-
} else {
12-
$profile = $args[0]
21+
Write-Host "Building libafl_libfuzzer runtime with profile '$Profile' on toolchain '$Toolchain'" -ForegroundColor Green
22+
Invoke-Command -ScriptBlock {
23+
$env:RUSTFLAGS = $RustcArgs
24+
$env:CFLAGS = $CCFlags
25+
Write-Host "Using Rust flags: $env:RUSTFLAGS" -ForegroundColor Cyan
26+
Write-Host "Using Cargo arguments: $CargoArgs" -ForegroundColor Cyan
27+
Write-Host "Using C compiler flags: $env:CFLAGS" -ForegroundColor Cyan
28+
if ($CargoArgs) {
29+
cargo +$Toolchain build --profile $Profile $CargoArgs
30+
} else {
31+
cargo +$Toolchain build --profile $Profile
32+
}
1333
}
1434

15-
Write-Host "Building libafl_libfuzzer runtime with profile '$profile'" -ForegroundColor Green
16-
Invoke-Expression "cargo build --profile $profile"
1735

1836
$tmpdir = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName())
1937
New-Item -ItemType Directory -Path $tmpdir | Out-Null
@@ -25,13 +43,13 @@ function Cleanup {
2543
}
2644

2745
try {
28-
if ($profile -eq "dev") {
46+
if ($Profile -eq "dev") {
2947
# Set the profile to debug for dev builds, because the path isn't the same
3048
# as the profile name
31-
$profile = "debug"
49+
$Profile = "debug"
3250
}
3351

34-
$targetPath = Join-Path $SCRIPT_DIR "target\$profile\afl_libfuzzer_runtime.lib"
52+
$targetPath = Join-Path $SCRIPT_DIR "target\$Profile\afl_libfuzzer_runtime.lib"
3553
$outputPath = Join-Path $SCRIPT_DIR "libFuzzer.lib"
3654

3755
Copy-Item -Path $targetPath -Destination $outputPath -Force | Out-Null

crates/libafl_libfuzzer_runtime/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ fn main() {
1919

2020
let mut harness_wrap = cc::Build::new();
2121

22+
if let Ok(flags) = env::var("CFLAGS") {
23+
println!("cargo:rerun-if-env-changed=CFLAGS");
24+
harness_wrap.flags(flags.split_whitespace());
25+
}
26+
2227
harness_wrap.cpp(true).file("src/harness_wrap.cpp");
2328

2429
harness_wrap.compile("harness_wrap");

crates/libafl_libfuzzer_runtime/build.sh

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,85 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
66

77
cd "${SCRIPT_DIR}" || exit 1
88

9-
if [ -z ${1+x} ]; then
10-
profile=release
11-
else
12-
profile="$1"
9+
#!/bin/bash
10+
11+
# Default values
12+
profile="release"
13+
toolchain="nightly"
14+
cargoargs=""
15+
rustcargs=""
16+
ccompilerflags=""
17+
18+
# Help message
19+
print_help() {
20+
echo "Usage: $0 [options]"
21+
echo
22+
echo "Options:"
23+
echo " -p, --profile Build profile to use (default: release)"
24+
echo " -t, --toolchain Toolchain to use (default: nightly)"
25+
echo " -c, --cargo-args Additional cargo arguments to pass"
26+
echo " -r, --rustc-args Additional rustc arguments to pass"
27+
echo " -f, --cc-flags Additional flags to set for the C compiler"
28+
echo " -h, --help Show this help message and exit"
29+
echo
30+
echo "Example:"
31+
echo " $0 --profile dev --toolchain nightly --cargo-args \"--verbose\" --rustc-args \"--emit asm\" --cc-flags \"-O2\""
32+
}
33+
34+
# Use getopt for long options
35+
OPTIONS=$(getopt -o p:t:c:r:f:h --long profile:,toolchain:,cargo-args:,rustc-args:,cc-flags:,help -- "$@")
36+
if [ $? -ne 0 ]; then
37+
echo "Invalid options provided"
38+
exit 1
1339
fi
40+
eval set -- "$OPTIONS"
41+
42+
# Parse options
43+
while true; do
44+
case "$1" in
45+
-p|--profile)
46+
profile="$2"
47+
shift 2
48+
;;
49+
-t|--toolchain)
50+
toolchain="$2"
51+
shift 2
52+
;;
53+
-c|--cargo-args)
54+
cargoargs="$2"
55+
shift 2
56+
;;
57+
-r|--rustc-args)
58+
rustcargs="$2"
59+
shift 2
60+
;;
61+
-f|--cc-flags)
62+
ccompilerflags="$2"
63+
shift 2
64+
;;
65+
-h|--help)
66+
print_help
67+
exit 0
68+
;;
69+
--)
70+
shift
71+
break
72+
;;
73+
*)
74+
echo "Unexpected option: $1"
75+
exit 1
76+
;;
77+
esac
78+
done
1479

15-
if ! cargo +nightly --version >& /dev/null; then
80+
if ! cargo +$toolchain --version >& /dev/null; then
1681
echo -e "You must install a recent Rust to build the libafl_libfuzzer runtime!"
1782
exit 1
1883
fi
1984

20-
cargo +nightly build --profile "$profile"
85+
export RUSTFLAGS="${RUSTFLAGS} ${rustcargs}"
86+
export CFLAGS="${CFLAGS} ${ccompilerflags}"
87+
cargo +$toolchain build --profile "$profile" ${cargoargs}
2188

2289
if [[ "$OSTYPE" == "darwin"* ]]; then
2390
# MacOS and iOS
@@ -26,7 +93,7 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
2693
-o libafl_libfuzzer_runtime.dylib
2794
else
2895
# Linux and *BSD
29-
RUSTC_BIN="$(cargo +nightly rustc -Zunstable-options --print target-libdir)/../bin"
96+
RUSTC_BIN="$(cargo +$toolchain rustc -Zunstable-options --print target-libdir)/../bin"
3097
RUST_LLD="${RUSTC_BIN}/rust-lld"
3198
RUST_AR="${RUSTC_BIN}/llvm-ar"
3299

0 commit comments

Comments
 (0)