-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.zig
179 lines (157 loc) · 7.14 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const std = @import("std");
pub fn build(b: *std.Build) !void {
_ = try addFuzzer(b, "json", &.{});
_ = try addFuzzer(b, "tokenizer", &.{});
_ = try addFuzzer(b, "parse", &.{});
_ = try addFuzzer(b, "deflate", &.{});
_ = try addFuzzer(b, "deflate-roundtrip", &.{});
_ = try addFuzzer(b, "xz", &.{});
_ = try addFuzzer(b, "zstandard", &.{});
_ = try addFuzzer(b, "tar", &.{});
_ = try addFuzzer(b, "tar-fs", &.{});
const deflate_puff = try addFuzzer(b, "deflate-puff", &.{});
for (deflate_puff.libExes()) |lib_exe| {
lib_exe.addIncludePath(b.path("lib/puff"));
lib_exe.addCSourceFile(.{ .file = b.path("lib/puff/puff.c"), .flags = &.{} });
lib_exe.linkLibC();
}
const sin = try addFuzzer(b, "sin", &.{"-lm"});
for (sin.libExes()) |lib_exe| {
lib_exe.linkLibC();
}
const xxhash = try addFuzzer(b, "xxhash", &.{});
for (xxhash.libExes()) |lib_exe| {
lib_exe.addIncludePath(b.path("lib/xxhash"));
lib_exe.addCSourceFile(.{ .file = b.path("lib/xxhash/xxhash.c"), .flags = &.{"-DXXH_NO_XXH3"} });
lib_exe.linkLibC();
}
const zstandard_compare = try addFuzzer(b, "zstandard-compare", &.{});
addZstd(b, &zstandard_compare);
const zstandard_compare_alloc = try addFuzzer(b, "zstandard-compare-alloc", &.{});
addZstd(b, &zstandard_compare_alloc);
const zstandard_compare_stream = try addFuzzer(b, "zstandard-compare-stream", &.{});
addZstd(b, &zstandard_compare_stream);
if (b.option([]const u8, "zig-src", "Zig source root")) |zig_src_dir| {
const markdown = b.createModule(.{
.root_source_file = .{ .cwd_relative = b.pathJoin(&.{ zig_src_dir, "lib/docs/wasm/markdown.zig" }) },
});
const markdown_parse = try addFuzzer(b, "markdown", &.{});
for (markdown_parse.libExes()) |lib_exe| {
lib_exe.root_module.addImport("markdown", markdown);
}
const patch_git_zig = b.addSystemCommand(&.{ "patch", "-u" });
patch_git_zig.addFileArg(.{
.cwd_relative = b.pathJoin(&.{ zig_src_dir, "src/Package/Fetch/git.zig" }),
});
patch_git_zig.addFileArg(b.path("fuzzers/git.patch"));
patch_git_zig.addArg("-o");
const patched_git_zig = patch_git_zig.addOutputFileArg("git.zig");
const git = b.createModule(.{
.root_source_file = patched_git_zig,
});
const git_unpack = try addFuzzer(b, "git", &.{});
for (git_unpack.libExes()) |lib_exe| {
lib_exe.root_module.addImport("git", git);
}
}
// tools
const sin_musl = b.addExecutable(.{
.name = "sin-musl",
.root_source_file = b.path("tools/sin-musl.zig"),
.target = b.resolveTargetQuery(.{ .abi = .musl }),
});
sin_musl.linkLibC();
const install_sin_musl = b.addInstallArtifact(sin_musl, .{});
const zstandard_verify = b.addExecutable(.{
.name = "zstandard-verify",
.root_source_file = b.path("tools/zstandard-verify.zig"),
.target = b.resolveTargetQuery(.{}),
});
const install_zstandard_verify = b.addInstallArtifact(zstandard_verify, .{});
const tools_step = b.step("tools", "Build and install tools");
tools_step.dependOn(&install_sin_musl.step);
tools_step.dependOn(&install_zstandard_verify.step);
}
fn addFuzzer(b: *std.Build, comptime name: []const u8, afl_clang_args: []const []const u8) !FuzzerSteps {
const target = b.resolveTargetQuery(.{});
// The library
const fuzz_lib = b.addStaticLibrary(.{
.name = "fuzz-" ++ name ++ "-lib",
.root_source_file = b.path("fuzzers/" ++ name ++ ".zig"),
.target = target,
.optimize = .Debug,
});
fuzz_lib.want_lto = true;
fuzz_lib.bundle_compiler_rt = true;
// Seems to be necessary for LLVM >= 15
fuzz_lib.root_module.pic = true;
// Setup the output name
const fuzz_executable_name = "fuzz-" ++ name;
// We want `afl-clang-lto -o path/to/output path/to/library`
const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-o" });
const fuzz_exe_path = fuzz_compile.addOutputFileArg(name);
// Add the path to the library file to afl-clang-lto's args
fuzz_compile.addArtifactArg(fuzz_lib);
// Custom args
fuzz_compile.addArgs(afl_clang_args);
// Install the cached output to the install 'bin' path
const fuzz_install = b.addInstallBinFile(fuzz_exe_path, fuzz_executable_name);
fuzz_install.step.dependOn(&fuzz_compile.step);
// Add a top-level step that compiles and installs the fuzz executable
const fuzz_compile_run = b.step("fuzz-" ++ name, "Build executable for fuzz testing '" ++ name ++ "' using afl-clang-lto");
fuzz_compile_run.dependOn(&fuzz_compile.step);
fuzz_compile_run.dependOn(&fuzz_install.step);
// Compile a companion exe for debugging crashes
const fuzz_debug_exe = b.addExecutable(.{
.name = "fuzz-" ++ name ++ "-debug",
.root_source_file = b.path("fuzzers/" ++ name ++ ".zig"),
.target = target,
.optimize = .Debug,
});
// Only install fuzz-debug when the fuzz step is run
const install_fuzz_debug_exe = b.addInstallArtifact(fuzz_debug_exe, .{});
fuzz_compile_run.dependOn(&install_fuzz_debug_exe.step);
return FuzzerSteps{
.lib = fuzz_lib,
.debug_exe = fuzz_debug_exe,
};
}
const FuzzerSteps = struct {
lib: *std.Build.Step.Compile,
debug_exe: *std.Build.Step.Compile,
pub fn libExes(self: *const FuzzerSteps) [2]*std.Build.Step.Compile {
return [_]*std.Build.Step.Compile{ self.lib, self.debug_exe };
}
};
fn addZstd(b: *std.Build, fuzzer_steps: *const FuzzerSteps) void {
for (fuzzer_steps.libExes()) |lib_exe| {
lib_exe.addIncludePath(b.path("lib/zstd/lib"));
lib_exe.addCSourceFiles(
.{
.files = &.{
"lib/zstd/lib/decompress/huf_decompress.c",
"lib/zstd/lib/decompress/zstd_ddict.c",
"lib/zstd/lib/decompress/zstd_decompress.c",
"lib/zstd/lib/decompress/zstd_decompress_block.c",
"lib/zstd/lib/common/entropy_common.c",
"lib/zstd/lib/common/error_private.c",
"lib/zstd/lib/common/fse_decompress.c",
"lib/zstd/lib/common/pool.c",
"lib/zstd/lib/common/xxhash.c",
"lib/zstd/lib/common/zstd_common.c",
"lib/zstd/lib/common/debug.c",
},
.flags = &.{
"-DZSTD_DISABLE_ASM=1",
"-DDEBUGLEVEL=10", // Enable debug logging for easier debugging
// Some inputs trigger UBSAN but I can't reproduce the UB outside of the zig-built .exe.
// TODO: Investigate this more, just shutting off UBSAN is a cop-out.
"-fno-sanitize=undefined",
//"-DNO_PREFETCH=1", // Attempt to avoid unknown instruction (didn't seem to work though)
//"-DZSTD_NO_INTRINSICS=1", // Attempt to avoid unknown instruction (didn't seem to work though)
},
},
);
lib_exe.linkLibC();
}
}