<div dir="ltr"><div dir="ltr"><br><div>Hi Fangrui,</div><div><br></div><div>Many thanks for providing such detailed feedback! Please find my comments inlined below.</div><div><br></div><div>- Ammar</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jun 28, 2020 at 5:59 AM Fangrui Song <<a href="mailto:maskray@google.com" target="_blank">maskray@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 2020-06-26, Ammar Ben Khadra via llvm-dev wrote:<br>
>## TL;DR<br>
><br>
>We introduce bcov, an open-source binary-level coverage analysis tool [1].<br>
>The details are discussed in our paper [2], which is accepted to<br>
>ESEC/FSE'20. bcov statically instruments x86-64 ELF binaries without<br>
>compiler support. It features several techniques that allow it to achieve<br>
>high performance, transparency, and flexibility.<br>
><br>
>For example, running "make check-llvm-codegen" with an instrumented version<br>
>of llc introduces a mean performance overhead of about 32%. The overhead<br>
>for smaller binaries can be as low as 2%. bcov accurately reports code<br>
>coverage with a mean F-score of 99,86%. All of that without introducing any<br>
>test regressions.<br>
><br>
><br>
>## Design overview<br>
><br>
>Given an ELF module as input, bcov will first analyze it to choose<br>
>appropriate probe locations and estimate the size of the program segments<br>
>required for patching. This analysis depends on the instrumentation policy<br>
>chosen by the user. Then, bcov patches the input module by adding two<br>
>program segments; one segment contains trampoline code and the other keeps<br>
>coverage data.<br>
><br>
>bcov uses trampolines to instrument the binary where it inserts probes in<br>
>targeted locations to track basic block coverage. Each probe consists of a<br>
>detour that diverts control flow to a designated trampoline. The trampoline<br>
>updates coverage data using a single pc-relative mov instruction (e.g., mov<br>
>BYTE PTR [rip+0xadd88],1), executes relocated instructions, and then<br>
>restores control flow to its original state.<br>
><br>
>To dump coverage data, the user can inject our small runtime library,<br>
>libbcov-rt, using the LD_PRELOAD mechanism. This library scans the memory<br>
>mappings of the current process to dump every data segment that starts with<br>
>a specific magic number. This means that coverage data will be dumped<br>
>separately for each ELF module. Dumping data takes place at process<br>
>shutdown or upon receiving a user signal. The latter feature enables online<br>
>coverage tracking.<br>
><br>
><br>
>## Key features<br>
><br>
>bcov integrates several techniques including:<br>
><br>
> - Probe pruning. We instrument only what is necessary for tracking code<br>
>coverage. To this end, we bring Agrawal's probe pruning technique [3] to<br>
>binary-level instrumentation.<br>
><br>
> - Precise CFG analysis. Imprecision in the recovered CFG can have several<br>
>effects, which include causing the instrumented binary to crash. To improve<br>
>CFG precision, we propose a novel jump table analysis and implement a<br>
>variant of an available non-return analysis [4].<br>
><br>
> - Static instrumentation. We implement several techniques like (1)<br>
>optimized probe selection, (2) jump table instrumentation, (3) and<br>
>systematic detour hosting.<br>
><br>
>The result is that our approach can work transparently, and with low<br>
>overhead, on large and well-tested C and C++ programs.<br>
><br>
><br>
>## Relation to the compiler<br>
><br>
>Code coverage analysis at the binary level, as implemented in bcov, can<br>
>offer several benefits: first, it is highly flexible. A user can analyze<br>
>the coverage of a particular module only. Even better, coverage tracking<br>
>can be limited to specific functions, e.g., the ones affected by recent<br>
>changes. Second, it is largely independent of the used compiler and the<br>
>build system. We observed consistent results after experimenting with four<br>
>different versions of gcc and clang and three different build types,<br>
>namely, debug, release, and LTO. Finally, the primitives we use to divert<br>
>control-flow and update coverage are simple. One can imagine (and hope)<br>
>that extending bcov to system code, or other native languages, will not<br>
>require a lot of work.<br>
><br>
>However, the viability of static binary-level coverage analysis ultimately<br>
>depends on the compiler toolchain that produces the binary. Specifically,<br>
>we can think of two areas where compilers might help our tool by adding<br>
>extra support, or at least by not emitting code that is difficult to<br>
>analyze:<br>
><br>
> - Source-level coverage. The ability to know whether a particular basic<br>
>block was covered in the binary is quite interesting. However, developers<br>
>would ultimately want to map this coverage information to source-level<br>
>artifacts. Statement coverage can be supported already with the help of<br>
>dwarf debugging information. But bcov can report the coverage of individual<br>
>branch decisions at the binary level. Therefore, leveraging this<br>
>information to obtain source-level MC/DC seems to be a natural next step.<br>
>Ideally, we want to infer MC/DC using what we have already, namely,<br>
>binary-level coverage and dwarf information. However, adding support to a<br>
>custom mapping format similar to the one available in llvm-cov might be<br>
>necessary. In this regard, we appreciate your suggestions on the best way<br>
>to support MC/DC, if at all feasible. (feedback needed)<br>
><br>
> - Function model. We assume that a function occupies a continuous code<br>
>region. The function's main entry is expected to be at the beginning of<br>
>this region. Also, we consider each landing pad to be a function entry as<br>
>well. Based on this, we assume that the identified entries predominate all<br>
>basic blocks inside the function. In other words, a function can not reach<br>
>an arbitrary basic block inside another function without first visiting one<br>
>of its entries. This assumption seems to hold pretty well in the binaries<br>
>we experimented with, but we are not sure to what extent it generalizes to<br>
>other compilers and ISAs. (feedback needed)<br>
><br>
><br>
>## Potential future work<br>
><br>
>Updating coverage using a single pc-relative mov instruction is transparent<br>
>in the sense that it does not clobber any general-purpose register. It even<br>
>preserves the CPU's eflags. This, in turn, has greatly simplified the<br>
>implementation of our prototype. That is, we did not have to think about<br>
>saving/restoring the CPU state. However, putting mechanisms in place to<br>
>allow eflags to be *safely* clobbered, e.g., liveness analysis, can open<br>
>the door for several design alternatives where a pc-relative mov (e.g., mov<br>
>BYTE PTR [rip+0xadd88],1) can be replaced with:<br>
><br>
>  -  or BYTE PTR [rip+0xadd88],c: where c is one-hot constant. This will<br>
>reduce the size of required coverage data to 1/8 of the size currently<br>
>required by bcov.<br>
><br>
>  - add BYTE PTR [rip+0xadd88],1: 8-bit counter per superblock (or selected<br>
>set of basic blocks). This might be useful for fuzzing.<br>
><br>
>  - add DWORD PTR [rip+0xadd88],1: 32-bit counter per superblock (or<br>
>selected set of basic blocks). This can be useful for profiling.<br>
><br>
><br>
>So there is significant potential here. However, we have not explored these<br>
>ideas any further, and we would highly appreciate any feedback about their<br>
>viability and potential added value in comparison to what is already<br>
>available in llvm-cov and sancov. (feedback needed)<br>
><br>
>To conclude, we introduced bcov [1] and discussed a few areas where the<br>
>LLVM community can help us assess its potential, identify related work, and<br>
>plan its future direction. Many thanks in advance for your feedback.<br>
><br>
><br>
>Kind regards,<br>
><br>
>Ammar<br>
><br>
><br>
> [1]: <a href="https://github.com/abenkhadra/bcov" rel="noreferrer" target="_blank">https://github.com/abenkhadra/bcov</a><br>
> [2]: <a href="https://arxiv.org/pdf/2004.14191.pdf" rel="noreferrer" target="_blank">https://arxiv.org/pdf/2004.14191.pdf</a><br>
> [3]: <a href="https://dl.acm.org/doi/10.1145/174675.175935" rel="noreferrer" target="_blank">https://dl.acm.org/doi/10.1145/174675.175935</a><br>
> [4]: <a href="https://dl.acm.org/doi/10.1145/2931037.2931047" rel="noreferrer" target="_blank">https://dl.acm.org/doi/10.1145/2931037.2931047</a><br>
<br>
Hi Ammar,<br>
<br>
Great work!<br>
<br>
I think the following statement in the paper is not true: "gcov relies on debug<br>
information which is less accurate in optimized builds." GCC's gcov<br>
implementation does not rely on debugging information. This can be verified with<br>
-fdump-debug: `gcc --coverage -fdump-debug -c a.c` dumps an empty a.c.*.debug<br>
file.<br>
<br></blockquote><div><br></div><div>After a second look at <a href="https://gcc.gnu.org/onlinedocs/gcc/Gcov-Intro.html#Gcov-Intro" target="_blank">https://gcc.gnu.org/onlinedocs/gcc/Gcov-Intro.html#Gcov-Intro</a>, it seems that I misunderstood why it is recommended to disable optimizations in gcov. You are right. This should be fixed. Thanks!</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
clang supports gcov instrumentation as well. clang --coverage does leverage<br>
debugging information, but the usage is very minimum - debugging information is<br>
just used to retrieve the start line of a function. I have studied and<br>
contributed a bit to clang's gcov implementation recently and thus I am fairly<br>
confident about this.<br>
<br>
> In comparison, llvm-cov features a custom mapping format embedded in<br>
> LLVM’s intermediate representation (IR).<br>
<br>
I don't know whether a clarification is needed here. llvm-cov is a frontend tool<br>
which supports two formats: (1) coverage mapping (clang -fprofile-instr-generate<br>
-fcoverage-mapping) and (2) gcov. Using -fcoverage-mapping might be clearer in<br>
the context.<br>
<br></blockquote><div><br></div><div>Actually, we are referring to the first usage, which is, as I understand, the most popular. A clarification can indeed help here.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> Also, sancov is tightly coupled with LLVM sanitizers (e.g., ASan) which add<br>
> varying overhead. Extending bcov with additional feedback signals, similar to<br>
> sancov, is an interesting future work<br>
<br>
SanitizerCoverage is a standalone instrumentation pass<br>
(llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp)<br>
which is not coupled with asan. -fsanitize-coverage= can be used standalone, or <br>
together with asan, lsan, msan, ubsan, etc.<br>
<br>
Its overhead can be very small, especially if you use the recent inline-bool-flag<br>
<a href="https://reviews.llvm.org/D77244" rel="noreferrer" target="_blank">https://reviews.llvm.org/D77244</a><br>
(`clang -fsanitize-coverage=inline-bool-flag a.c`) or inline-8bit-counters.<br>
You can choose 3 modes: edge (default), bb, and func.<br>
The flags are stored in a section __sancov_bools.<br>
There is no standalone dumper though...<br>
<br>
<br></blockquote><div><br></div><div>I remember that the term "tightly coupled" was used by a sancov developer in a Github issue. I can not find that thread now. This perhaps refers to the fact that (please correct me)  casual users are not expected to use sancov as an independent pass. Instead, they should rely on the runtime already provided by one of the sanitizers. I shall revisit this issue to improve the wording. However, I think that the main point still holds; given this dependence on the sanitizer runtime and the variety of options offered by sancov, one can not refer to a 'reference' sancov to report its instrumentation overhead. Nevertheless, the functionality offered by the sancov option 'trace-pc-guard' is perhaps closest to that in bcov. If we use this as the basis for measurement, then the performance overhead should be small. Can you please refer me to instructions on how I can use sancov without any sanitizer runtime?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
A few random comments below.<br>
<br>
<br>
If I understand correctly, by leveraging superblock dominator graph (from<br>
"Dominators, Super Blocks, and Program Coverage") and the any-node policy, the<br>
coverage result is binary: not-covered vs covered. I agree that in many cases<br>
this is sufficient. gcov and -fcoverage-mapping, on the other hand, can provide<br>
line execution counts, which are handy in other cases. This fact should probably<br>
mentioned somewhere, "RQ2: Instrumentation overhead" or another place.  Both<br>
-fcoverage-mapping and gcov provide more information, so they have larger<br>
overhead (I agree that gcov isn't a format optimized for file sizes - it uses<br>
uint32_t records everywhere which can be quite expensive representing smaller<br>
integers).<br>
<br>
<br></blockquote><div><br></div><div>Yes, the reported coverage is binary. It indicates whether a basic block is covered or not based on the coverage of its superblock (a set of basic blocks with the same coverage information). Profiling represents an interesting use case, indeed. I noted in the 'Potential future work' section that updating coverage data using:</div><div><br></div><div>add DWORD PTR [rip+0xadd88],1</div><div><br></div><div>instead of the current:</div><div><br></div><div>mov BYTE PTR [rip+0xadd88],1<br></div><div><br></div><div>Enables profiling using a 32-bit counter per superblock. However, a superblock can span multiple lines in the source code which renders this counter difficult to relate to. Instead of grouping basic blocks based on superblocks, we need to think about mapping each source line to its corresponding basic blocks in the binary and then choose the best BB to instrument with such a counter. Expectedly, this will incur more overhead.</div><div><br></div><div>Also, note that "RQ2: Instrumentation overhead" is concerned with evaluating the overhead of bcov only. We did not attempt to directly compare this overhead to that of llvm-cov or gcov since, as you noted already, both tools track different artifacts, i.e., not directly comparable. However, we mentioned that llvm-cov can dump "320GB of coverage (and profiling) data" if online merging was not enabled. So there was an explicit emphasis on the role of profiling here. And that discussion was in the context of showing that the same online merging feature can be leveraged by bcov as well.</div><div><br></div><div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
bcov currently rewrites program headers but not the section header table, so for<br>
example, objdump -d a.any cannot disassemble the synthesized code.<br>
<br>
<br></blockquote><div><br></div><div><br></div><div>The original section header table is preserved. But adding two additional section headers to describe the patch segments is a nice to have feature. I developed a simple tool to disassemble the patch code which was sufficient for my debugging needs. I'm planning to open source this tool later. In the meantime, using gdb as described in the artifacts repository (<a href="https://github.com/abenkhadra/bcov-artifacts/blob/master/sample-binaries/README.md" target="_blank">https://github.com/abenkhadra/bcov-artifacts/blob/master/sample-binaries/README.md</a>) might be the easiest way to inspect the patch code.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
bcov depends on capstone, which appears to be a pretty standard disassembly<br>
tool... It is based on a ~2013 shimmed-down version of LLVM MC and<br>
LLVM*Disassembler. The generated files (e.g. arch/X86/X86DisassemblerDecoder.c)<br>
get ad-hoc sporadic amendment from contributors over the years.. It is a bit<br>
unfortunate for LLVM that MC and LLVM*Disassembler (Machine Code, including<br>
assembly/disassembly libraries) cannot be easily adapted by downstream<br>
users..... (Well, downstream packages can use LLVM exported CMake files and use<br>
find_program(LLVM) and link against some LLVM* libraries but this may pull in a<br>
large set of dependencies)<br></blockquote><div><br></div><div><br></div></div></div>