diff options
| author | 2021-12-26 16:30:39 +0100 | |
|---|---|---|
| committer | 2021-12-26 18:12:55 +0100 | |
| commit | 3cd587ea0edb03122bdac298eea97ac9f79afd07 (patch) | |
| tree | d77600b33b28cf333b925d5579433c382c57c15d /build.zig | |
| parent | gitignore: ignore coredumps (diff) | |
| download | zig-sqlite-3cd587ea0edb03122bdac298eea97ac9f79afd07.tar.gz zig-sqlite-3cd587ea0edb03122bdac298eea97ac9f79afd07.tar.xz zig-sqlite-3cd587ea0edb03122bdac298eea97ac9f79afd07.zip | |
add some fuzzing capability
Diffstat (limited to '')
| -rw-r--r-- | build.zig | 50 |
1 files changed, 49 insertions, 1 deletions
| @@ -146,7 +146,7 @@ const all_test_targets = switch (builtin.target.cpu.arch) { | |||
| 146 | }, | 146 | }, |
| 147 | }; | 147 | }; |
| 148 | 148 | ||
| 149 | pub fn build(b: *std.build.Builder) void { | 149 | pub fn build(b: *std.build.Builder) !void { |
| 150 | const in_memory = b.option(bool, "in_memory", "Should the tests run with sqlite in memory (default true)") orelse true; | 150 | const in_memory = b.option(bool, "in_memory", "Should the tests run with sqlite in memory (default true)") orelse true; |
| 151 | const dbfile = b.option([]const u8, "dbfile", "Always use this database file instead of a temporary one"); | 151 | const dbfile = b.option([]const u8, "dbfile", "Always use this database file instead of a temporary one"); |
| 152 | const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)"); | 152 | const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)"); |
| @@ -203,4 +203,52 @@ pub fn build(b: *std.build.Builder) void { | |||
| 203 | 203 | ||
| 204 | test_step.dependOn(&tests.step); | 204 | test_step.dependOn(&tests.step); |
| 205 | } | 205 | } |
| 206 | |||
| 207 | // Fuzzing | ||
| 208 | |||
| 209 | const lib = b.addStaticLibrary("sqlite", null); | ||
| 210 | lib.addCSourceFile("c/sqlite3.c", &[_][]const u8{"-std=c99"}); | ||
| 211 | lib.linkLibC(); | ||
| 212 | lib.setBuildMode(.Debug); | ||
| 213 | lib.setTarget(getTarget(target, true)); | ||
| 214 | |||
| 215 | // The library | ||
| 216 | const fuzz_lib = b.addStaticLibrary("fuzz-lib", "fuzz/main.zig"); | ||
| 217 | fuzz_lib.addIncludeDir("c"); | ||
| 218 | fuzz_lib.setBuildMode(.Debug); | ||
| 219 | fuzz_lib.setTarget(getTarget(target, true)); | ||
| 220 | fuzz_lib.linkLibrary(lib); | ||
| 221 | fuzz_lib.want_lto = true; | ||
| 222 | fuzz_lib.bundle_compiler_rt = true; | ||
| 223 | fuzz_lib.addPackagePath("sqlite", "sqlite.zig"); | ||
| 224 | |||
| 225 | // Setup the output name | ||
| 226 | const fuzz_executable_name = "fuzz"; | ||
| 227 | const fuzz_exe_path = try std.fs.path.join(b.allocator, &.{ b.cache_root, fuzz_executable_name }); | ||
| 228 | |||
| 229 | // We want `afl-clang-lto -o path/to/output path/to/library` | ||
| 230 | const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-o", fuzz_exe_path }); | ||
| 231 | fuzz_compile.addArtifactArg(lib); | ||
| 232 | fuzz_compile.addArtifactArg(fuzz_lib); | ||
| 233 | |||
| 234 | // Install the cached output to the install 'bin' path | ||
| 235 | const fuzz_install = b.addInstallBinFile(.{ .path = fuzz_exe_path }, fuzz_executable_name); | ||
| 236 | |||
| 237 | // Add a top-level step that compiles and installs the fuzz executable | ||
| 238 | const fuzz_compile_run = b.step("fuzz", "Build executable for fuzz testing using afl-clang-lto"); | ||
| 239 | // fuzz_compile_run.dependOn(&fuzz_lib.step); | ||
| 240 | fuzz_compile_run.dependOn(&fuzz_compile.step); | ||
| 241 | fuzz_compile_run.dependOn(&fuzz_install.step); | ||
| 242 | |||
| 243 | // Compile a companion exe for debugging crashes | ||
| 244 | const fuzz_debug_exe = b.addExecutable("fuzz-debug", "fuzz/main.zig"); | ||
| 245 | fuzz_debug_exe.addIncludeDir("c"); | ||
| 246 | fuzz_debug_exe.setBuildMode(.Debug); | ||
| 247 | fuzz_debug_exe.setTarget(getTarget(target, true)); | ||
| 248 | fuzz_debug_exe.linkLibrary(lib); | ||
| 249 | fuzz_debug_exe.addPackagePath("sqlite", "sqlite.zig"); | ||
| 250 | |||
| 251 | // Only install fuzz-debug when the fuzz step is run | ||
| 252 | const install_fuzz_debug_exe = b.addInstallArtifact(fuzz_debug_exe); | ||
| 253 | fuzz_compile_run.dependOn(&install_fuzz_debug_exe.step); | ||
| 206 | } | 254 | } |