32 lines · cpp
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-exception4 5// Link this tiny library to a binary compiled with6// -fsanitize-coverage=inline-8bit-counters.7// When passed SANCOV_OUT=OUTPUT_PATH, the process will8// dump the 8-bit coverage counters to the file, assuming9// 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("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 27extern "C" void __sanitizer_cov_8bit_counters_init(char *Start, char *End) {28 CovStart = Start;29 CovEnd = End;30 atexit(DumpCoverage);31}32