xref: /llvm-project/compiler-rt/test/fuzzer/SanCovDump.cpp (revision 597e407cf23b92db202732e6890561b2f2830a58)
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 
15 static char *CovStart, *CovEnd;
16 
DumpCoverage()17 static void DumpCoverage() {
18   if (const char *DumpPath = getenv("SANCOV_OUT")) {
19     fprintf(stderr, "SanCovDump: %p %p %s\n", CovStart, CovEnd, DumpPath);
20     if (FILE *f = fopen(DumpPath, "w")) {
21       fwrite(CovStart, 1, CovEnd - CovStart, f);
22       fclose(f);
23     }
24   }
25 }
26 
__sanitizer_cov_8bit_counters_init(char * Start,char * End)27 extern "C" void __sanitizer_cov_8bit_counters_init(char *Start, char *End) {
28   CovStart = Start;
29   CovEnd = End;
30   atexit(DumpCoverage);
31 }
32