From f65b284930a658a274c2bfa8ab849022896be6e0 Mon Sep 17 00:00:00 2001 From: Jora Troosh Date: Sat, 20 Jul 2024 22:16:20 +0300 Subject: [PATCH] init --- .gitattributes | 2 + .github/workflows/ci.yaml | 37 ++++++++ .gitignore | 3 + LICENSE | 21 +++++ README.md | 27 ++++++ build.zig | 192 ++++++++++++++++++++++++++++++++++++++ build.zig.zon | 17 ++++ 7 files changed, 299 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/workflows/ci.yaml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.zig create mode 100644 build.zig.zon diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..515471b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.zig text eol=lf +*.zon text eol=lf diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..2586e7d --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,37 @@ +name: Continuous Integration + +on: + push: + branches: [main] + + pull_request: + branches: [main] + + workflow_dispatch: + +jobs: + exes: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Zig + uses: mlugg/setup-zig@v1 + + - name: Run `exes` + run: zig build exes + + fmt: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Zig + uses: mlugg/setup-zig@v1 + + - name: Run `fmt` + run: zig build fmt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0dc86ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +kcov-output/ +.zig-cache/ +zig-out/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3e5e1e4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Jora Troosh + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a83c195 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# AFLplusplus + +[![CI][ci-shd]][ci-url] +[![LC][lc-shd]][lc-url] + +## Zig build of [AFLplusplus executable suite](https://github.com/AFLplusplus/AFLplusplus). + +### :rocket: Usage + +```sh +git clone https://github.com/allyourcodebase/AFLplusplus.git +cd AFLplusplus/ +zig build exes -Doptimize=ReleaseFast +./zig-out/bin/afl-fuzz +./zig-out/bin/afl-showmap +./zig-out/bin/afl-tmin +./zig-out/bin/afl-analyze +./zig-out/bin/afl-gotcpu +./zig-out/bin/afl-as +``` + + + +[ci-shd]: https://img.shields.io/github/actions/workflow/status/allyourcodebase/AFLplusplus/ci.yaml?branch=main&style=for-the-badge&logo=github&label=CI&labelColor=black +[ci-url]: https://github.com/allyourcodebase/AFLplusplus/blob/main/.github/workflows/ci.yaml +[lc-shd]: https://img.shields.io/github/license/allyourcodebase/AFLplusplus.svg?style=for-the-badge&labelColor=black +[lc-url]: https://github.com/allyourcodebase/AFLplusplus/blob/main/LICENSE diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..b8211c4 --- /dev/null +++ b/build.zig @@ -0,0 +1,192 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const version = std.SemanticVersion{ .major = 4, .minor = 21, .patch = 0 }; + + // Dependencies + const AFLplusplus_dep = b.dependency("AFLplusplus", .{}); + const AFLplusplus_src_path = AFLplusplus_dep.path("src/"); + const AFLplusplus_inc_path = AFLplusplus_dep.path("include/"); + + // Executable suite + const exes_step = b.step("exes", "Install executables"); + + var flags = std.BoundedArray([]const u8, 15){}; + flags.appendSliceAssumeCapacity(&FLAGS); + if (target.result.cpu.arch.isX86()) { + flags.appendSliceAssumeCapacity(&.{ "-mavx2", "-D_HAVE_AVX2" }); + } + if (target.query.isNative()) { + flags.appendAssumeCapacity("-march=native"); + } + + const fuzz_exe = b.addExecutable(.{ + .name = "afl-fuzz", + .pic = true, + .target = target, + .version = version, + .optimize = optimize, + }); + fuzz_exe.addCSourceFiles(.{ + .root = AFLplusplus_src_path, + .files = &(FUZZ_SOURCES ++ SHARED_SOURCES), + .flags = flags.constSlice(), + }); + fuzz_exe.addIncludePath(AFLplusplus_inc_path); + fuzz_exe.linkSystemLibrary("z"); + fuzz_exe.linkLibC(); + + const fuzz_exe_install = b.addInstallArtifact(fuzz_exe, .{}); + exes_step.dependOn(&fuzz_exe_install.step); + + const showmap_exe = b.addExecutable(.{ + .name = "afl-showmap", + .pic = true, + .target = target, + .version = version, + .optimize = optimize, + }); + showmap_exe.addCSourceFiles(.{ + .root = AFLplusplus_src_path, + .files = &(.{ "afl-showmap.c", "afl-fuzz-mutators.c", "afl-fuzz-python.c" } ++ SHARED_SOURCES), + .flags = flags.constSlice(), + }); + showmap_exe.addIncludePath(AFLplusplus_inc_path); + showmap_exe.linkSystemLibrary("z"); + showmap_exe.linkLibC(); + + const showmap_exe_install = b.addInstallArtifact(showmap_exe, .{}); + exes_step.dependOn(&showmap_exe_install.step); + + const tmin_exe = b.addExecutable(.{ + .name = "afl-tmin", + .pic = true, + .target = target, + .version = version, + .optimize = optimize, + }); + tmin_exe.addCSourceFiles(.{ + .root = AFLplusplus_src_path, + .files = &(.{"afl-tmin.c"} ++ SHARED_SOURCES), + .flags = flags.constSlice(), + }); + tmin_exe.addIncludePath(AFLplusplus_inc_path); + tmin_exe.linkSystemLibrary("z"); + tmin_exe.linkLibC(); + + const tmin_exe_install = b.addInstallArtifact(tmin_exe, .{}); + exes_step.dependOn(&tmin_exe_install.step); + + const analyze_exe = b.addExecutable(.{ + .name = "afl-analyze", + .pic = true, + .target = target, + .version = version, + .optimize = optimize, + }); + analyze_exe.addCSourceFiles(.{ + .root = AFLplusplus_src_path, + .files = &(.{"afl-analyze.c"} ++ SHARED_SOURCES), + .flags = flags.constSlice(), + }); + analyze_exe.addIncludePath(AFLplusplus_inc_path); + analyze_exe.linkSystemLibrary("z"); + analyze_exe.linkLibC(); + + const analyze_exe_install = b.addInstallArtifact(analyze_exe, .{}); + exes_step.dependOn(&analyze_exe_install.step); + + const gotcpu_exe = b.addExecutable(.{ + .name = "afl-gotcpu", + .pic = true, + .target = target, + .version = version, + .optimize = optimize, + }); + gotcpu_exe.addCSourceFiles(.{ + .root = AFLplusplus_src_path, + .files = &.{ "afl-gotcpu.c", "afl-common.c" }, + .flags = flags.constSlice(), + }); + gotcpu_exe.addIncludePath(AFLplusplus_inc_path); + gotcpu_exe.linkSystemLibrary("z"); + gotcpu_exe.linkLibC(); + + const gotcpu_exe_install = b.addInstallArtifact(gotcpu_exe, .{}); + exes_step.dependOn(&gotcpu_exe_install.step); + + const as_exe = b.addExecutable(.{ + .name = "afl-as", + .pic = true, + .target = target, + .version = version, + .optimize = optimize, + }); + as_exe.addCSourceFiles(.{ + .root = AFLplusplus_src_path, + .files = &.{"afl-as.c"}, + .flags = flags.constSlice(), + }); + as_exe.addIncludePath(AFLplusplus_inc_path); + as_exe.linkSystemLibrary("z"); + as_exe.linkLibC(); + + const as_exe_install = b.addInstallArtifact(as_exe, .{}); + exes_step.dependOn(&as_exe_install.step); + + b.default_step.dependOn(exes_step); + + // Formatting checks + const fmt_step = b.step("fmt", "Run formatting checks"); + + const fmt = b.addFmt(.{ + .paths = &.{ + "build.zig", + }, + .check = true, + }); + fmt_step.dependOn(&fmt.step); + b.default_step.dependOn(fmt_step); +} + +const FUZZ_SOURCES = .{ + "afl-fuzz-bitmap.c", + "afl-fuzz-cmplog.c", + "afl-fuzz-extras.c", + "afl-fuzz-init.c", + "afl-fuzz-mutators.c", + "afl-fuzz-one.c", + "afl-fuzz-python.c", + "afl-fuzz-queue.c", + "afl-fuzz-redqueen.c", + "afl-fuzz-run.c", + "afl-fuzz-skipdet.c", + "afl-fuzz-state.c", + "afl-fuzz-stats.c", + "afl-fuzz-statsd.c", + "afl-fuzz.c", +}; + +const SHARED_SOURCES = .{ + "afl-performance.c", + "afl-common.c", + "afl-forkserver.c", + "afl-sharedmem.c", +}; + +const FLAGS = .{ + "-O2", + "-g", + "-Wno-pointer-sign", + "-Wno-variadic-macros", + "-Wall", + "-Wextra", + "-Wno-pointer-arith", + "-D_AFL_SPECIAL_PERFORMANCE", + "-DHAVE_ZLIB", + "-DAFL_PATH=\"\"", + "-DBIN_PATH=\"\"", + "-DDOC_PATH=\"\"", +}; diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..4abedf4 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,17 @@ +.{ + .name = "AFLplusplus", + .version = "4.21.0", + .minimum_zig_version = "0.13.0", + .dependencies = .{ + .AFLplusplus = .{ + .url = "https://github.com/AFLplusplus/AFLplusplus/archive/19ca7b3.tar.gz", + .hash = "12202609f778b890810bfa3a2574c8319dc211e170419bc2bc4f51241f549ba5033a", + }, + }, + .paths = .{ + "build.zig", + "build.zig.zon", + "LICENSE", + "README.md", + }, +}