-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implement SanitizerCoverage support (Refs. #6513) Please refer to https://clang.llvm.org/docs/SanitizerCoverage.html TLDR: `ModuleSanitizerCoveragePass` instruments the IR by inserting calls to callbacks at certain constructs. What the callbacks should do is up to the implementation. They are effectively required for fuzzing to be effective, and are provided by e.g. libfuzzer. One huge caveat is `SanitizerCoverageOptions` which controls which which callbacks should actually be inserted. I just don't know what to do about it. Right now i have hardcoded the set that would have been enabled by `-fsanitize=fuzzer-no-link`, because the alternative, due to halide unflexibility, would be to introduce ~16 suboptions to control each one. * Simplify test * sancov test: avoid potential signedness warnings. * Rename all instances of sancov to sanitizecoverage * Adjust spelling of "SanitizerCoverage" in some places * Actually adjust the feature name in build system for the test * Hopefully fix Makefile build Co-authored-by: Steven Johnson <srj@google.com>
- Loading branch information
1 parent
7eb9949
commit f11d820
Showing
11 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#include "HalideBuffer.h" | ||
#include "HalideRuntime.h" | ||
#include "sanitizercoverage.h" | ||
|
||
#include <iostream> | ||
#include <limits> | ||
#include <type_traits> | ||
#include <vector> | ||
|
||
using namespace std; | ||
using namespace Halide::Runtime; | ||
|
||
bool enable_callbacks = false; | ||
|
||
#if defined(__linux__) | ||
// Used by -fsanitize-coverage=stack-depth to track stack depth | ||
__attribute__((tls_model("initial-exec"))) thread_local uintptr_t __sancov_lowest_stack; | ||
#endif | ||
|
||
extern "C" void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_8bit_counters_init. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg, | ||
const uintptr_t *pcs_end) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_pcs_init. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_cmp1. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_cmp4. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_cmp8. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_const_cmp1. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_const_cmp2. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_const_cmp4. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_const_cmp8. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_switch. Success!\n"); | ||
} | ||
|
||
extern "C" void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { | ||
if (!enable_callbacks) return; | ||
printf("Hit __sanitizer_cov_trace_pc_indir. Success!\n"); | ||
} | ||
|
||
template<typename T> | ||
void clear_out(T &image) { | ||
image.fill(-42); | ||
} | ||
|
||
void verify_out(const Buffer<int8_t> &image) { | ||
image.for_each_element([&](int x, int y, int c) { | ||
int expected = 42 + c; | ||
int actual = image(x, y, c); | ||
if (actual != expected) { | ||
fprintf(stderr, "Failure @ %d %d %d: expected %d, got %d\n", x, y, c, expected, actual); | ||
exit(-1); | ||
} | ||
}); | ||
} | ||
|
||
//----------------------------------------------------------------------------- | ||
|
||
auto sanitizercoverage_wrapper(struct halide_buffer_t *out) { | ||
enable_callbacks = true; | ||
auto status = sanitizercoverage(out); | ||
enable_callbacks = false; | ||
return status; | ||
} | ||
|
||
int main() { | ||
fprintf(stderr, "Entering main().\n"); | ||
auto out = Buffer<int8_t>(4, 4, 3); | ||
fprintf(stderr, "Clearing output buffer.\n"); | ||
clear_out(out); | ||
fprintf(stderr, "Performing the transformation.\n"); | ||
if (sanitizercoverage_wrapper(out) != 0) { | ||
fprintf(stderr, "Failure!\n"); | ||
exit(-1); | ||
} | ||
fprintf(stderr, "Verifying the transformation.\n"); | ||
verify_out(out); | ||
// We rely on the callbacks being called and printing Success. | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "Halide.h" | ||
|
||
namespace { | ||
|
||
class SanitizerCoverage : public Halide::Generator<SanitizerCoverage> { | ||
public: | ||
Output<Buffer<int8_t>> output{"output", 3}; | ||
|
||
void generate() { | ||
// Currently the test just exercises Target::SanitizerCoverage | ||
output(x, y, c) = cast<int8_t>(42 + c); | ||
} | ||
|
||
void schedule() { | ||
output.dim(0).set_stride(Expr()).set_extent(4).dim(1).set_extent(4).dim(2).set_extent(3); | ||
} | ||
|
||
private: | ||
// Currently the test just exercises Target::SanitizerCoverage | ||
Var x, y, c; | ||
}; | ||
|
||
} // namespace | ||
|
||
HALIDE_REGISTER_GENERATOR(SanitizerCoverage, sanitizercoverage) |