1// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2// See https://llvm.org/LICENSE.txt for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5// Link this tiny library to a binary compiled with
6// -fsanitize-coverage=inline-8bit-counters.
7// When passed SANCOV_OUT=OUTPUT_PATH, the process will
8// dump the 8-bit coverage counters to the file, assuming
9// the regular exit() is called.
10//
11// See OutOfProcessFuzzTarget.cpp for usage.
12#include <stdio.h>
13#include <stdlib.h>
14
15static char *CovStart, *CovEnd;
16
17static void DumpCoverage() {
18 if (const char *DumpPath = getenv(name: "SANCOV_OUT")) {
19 fprintf(stderr, format: "SanCovDump: %p %p %s\n", CovStart, CovEnd, DumpPath);
20 if (FILE *f = fopen(filename: DumpPath, modes: "w")) {
21 fwrite(ptr: CovStart, size: 1, n: CovEnd - CovStart, s: f);
22 fclose(stream: f);
23 }
24 }
25}
26
27extern "C" void __sanitizer_cov_8bit_counters_init(char *Start, char *End) {
28 CovStart = Start;
29 CovEnd = End;
30 atexit(func: DumpCoverage);
31}
32

source code of compiler-rt/test/fuzzer/SanCovDump.cpp