186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//===-- sanitizer/coverage_interface.h --------------------------*- C++ -*-===//
286277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//
386277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//                     The LLVM Compiler Infrastructure
486277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//
586277eb844c4983c81de62d7c050e92fe7155788Stephen Hines// This file is distributed under the University of Illinois Open Source
686277eb844c4983c81de62d7c050e92fe7155788Stephen Hines// License. See LICENSE.TXT for details.
786277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//
886277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//===----------------------------------------------------------------------===//
986277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//
1086277eb844c4983c81de62d7c050e92fe7155788Stephen Hines// Public interface for sanitizer coverage.
1186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines//===----------------------------------------------------------------------===//
1286277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
1386277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#ifndef SANITIZER_COVERAG_INTERFACE_H
1486277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#define SANITIZER_COVERAG_INTERFACE_H
1586277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
1686277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#include <sanitizer/common_interface_defs.h>
1786277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
1886277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#ifdef __cplusplus
1986277eb844c4983c81de62d7c050e92fe7155788Stephen Hinesextern "C" {
2086277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#endif
2186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
2286277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Initialize coverage.
2386277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  void __sanitizer_cov_init();
2486277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Record and dump coverage info.
2586277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  void __sanitizer_cov_dump();
2686277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Open <name>.sancov.packed in the coverage directory and return the file
2786277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // descriptor. Returns -1 on failure, or if coverage dumping is disabled.
2886277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // This is intended for use by sandboxing code.
2986277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  intptr_t __sanitizer_maybe_open_cov_file(const char *name);
3086277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Get the number of total unique covered entities (blocks, edges, calls).
3186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // This can be useful for coverage-directed in-process fuzzers.
3286277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  uintptr_t __sanitizer_get_total_unique_coverage();
3386277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
3486277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Reset the basic-block (edge) coverage to the initial state.
3586277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Useful for in-process fuzzing to start collecting coverage from scratch.
3686277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Experimental, will likely not work for multi-threaded process.
3786277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  void __sanitizer_reset_coverage();
3886277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Set *data to the array of covered PCs and return the size of that array.
3986277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  // Some of the entries in *data will be zero.
4086277eb844c4983c81de62d7c050e92fe7155788Stephen Hines  uintptr_t __sanitizer_get_coverage_guards(uintptr_t **data);
4186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
427c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // The coverage instrumentation may optionally provide imprecise counters.
437c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // Rather than exposing the counter values to the user we instead map
447c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // the counters to a bitset.
457c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // Every counter is associated with 8 bits in the bitset.
467c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // We define 8 value ranges: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+
477c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // The i-th bit is set to 1 if the counter value is in the i-th range.
487c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // This counter-based coverage implementation is *not* thread-safe.
497c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar
507c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // Returns the number of registered coverage counters.
517c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  uintptr_t __sanitizer_get_number_of_counters();
527c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // Updates the counter 'bitset', clears the counters and returns the number of
537c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // new bits in 'bitset'.
547c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // If 'bitset' is nullptr, only clears the counters.
557c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // Otherwise 'bitset' should be at least
567c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  // __sanitizer_get_number_of_counters bytes long and 8-aligned.
577c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  uintptr_t
587c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar  __sanitizer_update_counter_bitset_and_clear_counters(uint8_t *bitset);
5986277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#ifdef __cplusplus
6086277eb844c4983c81de62d7c050e92fe7155788Stephen Hines}  // extern "C"
6186277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#endif
6286277eb844c4983c81de62d7c050e92fe7155788Stephen Hines
6386277eb844c4983c81de62d7c050e92fe7155788Stephen Hines#endif  // SANITIZER_COVERAG_INTERFACE_H
64