| <!DOCTYPE HTML> |
| <html lang="en" class="light sidebar-visible" dir="ltr"> |
| <head> |
| <!-- Book generated using mdBook --> |
| <meta charset="UTF-8"> |
| <title>LLVM source-based code coverage - Rust Compiler Development Guide</title> |
| |
| |
| <!-- Custom HTML head --> |
| |
| <meta name="description" content="A guide to developing the Rust compiler (rustc)"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <meta name="theme-color" content="#ffffff"> |
| |
| <link rel="icon" href="favicon.svg"> |
| <link rel="shortcut icon" href="favicon.png"> |
| <link rel="stylesheet" href="css/variables.css"> |
| <link rel="stylesheet" href="css/general.css"> |
| <link rel="stylesheet" href="css/chrome.css"> |
| <link rel="stylesheet" href="css/print.css" media="print"> |
| |
| <!-- Fonts --> |
| <link rel="stylesheet" href="FontAwesome/css/font-awesome.css"> |
| <link rel="stylesheet" href="fonts/fonts.css"> |
| |
| <!-- Highlight.js Stylesheets --> |
| <link rel="stylesheet" id="highlight-css" href="highlight.css"> |
| <link rel="stylesheet" id="tomorrow-night-css" href="tomorrow-night.css"> |
| <link rel="stylesheet" id="ayu-highlight-css" href="ayu-highlight.css"> |
| |
| <!-- Custom theme stylesheets --> |
| |
| |
| <!-- Provide site root and default themes to javascript --> |
| <script> |
| const path_to_root = ""; |
| const default_light_theme = "light"; |
| const default_dark_theme = "navy"; |
| </script> |
| <!-- Start loading toc.js asap --> |
| <script src="toc.js"></script> |
| </head> |
| <body> |
| <div id="body-container"> |
| <!-- Work around some values being stored in localStorage wrapped in quotes --> |
| <script> |
| try { |
| let theme = localStorage.getItem('mdbook-theme'); |
| let sidebar = localStorage.getItem('mdbook-sidebar'); |
| |
| if (theme.startsWith('"') && theme.endsWith('"')) { |
| localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); |
| } |
| |
| if (sidebar.startsWith('"') && sidebar.endsWith('"')) { |
| localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); |
| } |
| } catch (e) { } |
| </script> |
| |
| <!-- Set the theme before any content is loaded, prevents flash --> |
| <script> |
| const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme; |
| let theme; |
| try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } |
| if (theme === null || theme === undefined) { theme = default_theme; } |
| const html = document.documentElement; |
| html.classList.remove('light') |
| html.classList.add(theme); |
| html.classList.add("js"); |
| </script> |
| |
| <input type="checkbox" id="sidebar-toggle-anchor" class="hidden"> |
| |
| <!-- Hide / unhide sidebar before it is displayed --> |
| <script> |
| let sidebar = null; |
| const sidebar_toggle = document.getElementById("sidebar-toggle-anchor"); |
| if (document.body.clientWidth >= 1080) { |
| try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } |
| sidebar = sidebar || 'visible'; |
| } else { |
| sidebar = 'hidden'; |
| } |
| sidebar_toggle.checked = sidebar === 'visible'; |
| html.classList.remove('sidebar-visible'); |
| html.classList.add("sidebar-" + sidebar); |
| </script> |
| |
| <nav id="sidebar" class="sidebar" aria-label="Table of contents"> |
| <!-- populated by js --> |
| <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox> |
| <noscript> |
| <iframe class="sidebar-iframe-outer" src="toc.html"></iframe> |
| </noscript> |
| <div id="sidebar-resize-handle" class="sidebar-resize-handle"> |
| <div class="sidebar-resize-indicator"></div> |
| </div> |
| </nav> |
| |
| <div id="page-wrapper" class="page-wrapper"> |
| |
| <div class="page"> |
| <div id="menu-bar-hover-placeholder"></div> |
| <div id="menu-bar" class="menu-bar sticky"> |
| <div class="left-buttons"> |
| <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> |
| <i class="fa fa-bars"></i> |
| </label> |
| <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> |
| <i class="fa fa-paint-brush"></i> |
| </button> |
| <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> |
| <li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> |
| </ul> |
| <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar"> |
| <i class="fa fa-search"></i> |
| </button> |
| </div> |
| |
| <h1 class="menu-title">Rust Compiler Development Guide</h1> |
| |
| <div class="right-buttons"> |
| <a href="print.html" title="Print this book" aria-label="Print this book"> |
| <i id="print-button" class="fa fa-print"></i> |
| </a> |
| <a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository"> |
| <i id="git-repository-button" class="fa fa-github"></i> |
| </a> |
| <a href="https://github.com/rust-lang/rustc-dev-guide/edit/master/src/llvm-coverage-instrumentation.md" title="Suggest an edit" aria-label="Suggest an edit"> |
| <i id="git-edit-button" class="fa fa-edit"></i> |
| </a> |
| |
| </div> |
| </div> |
| |
| <div id="search-wrapper" class="hidden"> |
| <form id="searchbar-outer" class="searchbar-outer"> |
| <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> |
| </form> |
| <div id="searchresults-outer" class="searchresults-outer hidden"> |
| <div id="searchresults-header" class="searchresults-header"></div> |
| <ul id="searchresults"> |
| </ul> |
| </div> |
| </div> |
| |
| <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> |
| <script> |
| document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); |
| document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); |
| Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { |
| link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); |
| }); |
| </script> |
| |
| <div id="content" class="content"> |
| <main> |
| <h1 id="llvm-source-based-code-coverage"><a class="header" href="#llvm-source-based-code-coverage">LLVM source-based code coverage</a></h1> |
| <ul> |
| <li><a href="#recommended-bootstraptoml-settings">Recommended <code>bootstrap.toml</code> settings</a></li> |
| <li><a href="#rust-symbol-mangling">Rust symbol mangling</a></li> |
| <li><a href="#the-llvm-profiler-runtime">The LLVM profiler runtime</a></li> |
| <li><a href="#testing-coverage-instrumentation">Testing coverage instrumentation</a></li> |
| </ul> |
| <p><code>rustc</code> supports detailed source-based code and test coverage analysis |
| with a command line option (<code>-C instrument-coverage</code>) that instruments Rust |
| libraries and binaries with additional instructions and data, at compile time.</p> |
| <p>The coverage instrumentation injects calls to the LLVM intrinsic instruction |
| <a href="https://llvm.org/docs/LangRef.html#llvm-instrprof-increment-intrinsic"><code>llvm.instrprof.increment</code></a> at code branches |
| (based on a MIR-based control flow analysis), and LLVM converts these to |
| instructions that increment static counters, when executed. The LLVM coverage |
| instrumentation also requires a <a href="https://llvm.org/docs/CoverageMappingFormat.html">Coverage Map</a> that encodes source metadata, |
| mapping counter IDs--directly and indirectly--to the file locations (with |
| start and end line and column).</p> |
| <p>Rust libraries, with or without coverage instrumentation, can be linked into |
| instrumented binaries. When the program is executed and cleanly terminates, |
| LLVM libraries write the final counter values to a file (<code>default.profraw</code> or |
| a custom file set through environment variable <code>LLVM_PROFILE_FILE</code>).</p> |
| <p>Developers use existing LLVM coverage analysis tools to decode <code>.profraw</code> |
| files, with corresponding Coverage Maps (from matching binaries that produced |
| them), and generate various reports for analysis, for example:</p> |
| <p><img alt="Screenshot of sample `llvm-cov show` result, for function add_quoted_string" |
| src="img/llvm-cov-show-01.png" class="center"/> |
| <br/></p> |
| <p>Detailed instructions and examples are documented in the |
| <a href="https://doc.rust-lang.org/nightly/rustc/instrument-coverage.html">rustc book</a>.</p> |
| <h2 id="recommended-bootstraptoml-settings"><a class="header" href="#recommended-bootstraptoml-settings">Recommended <code>bootstrap.toml</code> settings</a></h2> |
| <p>When working on the coverage instrumentation code, it is usually necessary to |
| <strong>enable the profiler runtime</strong> by setting <code>profiler = true</code> in <code>[build]</code>. |
| This allows the compiler to produce instrumented binaries, and makes it possible |
| to run the full coverage test suite.</p> |
| <p>Enabling debug assertions in the compiler and in LLVM is recommended, but not |
| mandatory.</p> |
| <pre><code class="language-toml"># Similar to the "compiler" profile, but also enables debug assertions in LLVM. |
| # These assertions can detect malformed coverage mappings in some cases. |
| profile = "codegen" |
| |
| [build] |
| # IMPORTANT: This tells the build system to build the LLVM profiler runtime. |
| # Without it, the compiler can't produce coverage-instrumented binaries, |
| # and many of the coverage tests will be skipped. |
| profiler = true |
| |
| [rust] |
| # Enable debug assertions in the compiler. |
| debug-assertions = true |
| </code></pre> |
| <h2 id="rust-symbol-mangling"><a class="header" href="#rust-symbol-mangling">Rust symbol mangling</a></h2> |
| <p><code>-C instrument-coverage</code> automatically enables Rust symbol mangling <code>v0</code> (as |
| if the user specified <code>-C symbol-mangling-version=v0</code> option when invoking |
| <code>rustc</code>) to ensure consistent and reversible name mangling. This has two |
| important benefits:</p> |
| <ol> |
| <li>LLVM coverage tools can analyze coverage over multiple runs, including some |
| changes to source code; so mangled names must be consistent across compilations.</li> |
| <li>LLVM coverage reports can report coverage by function, and even separates |
| out the coverage counts of each unique instantiation of a generic function, |
| if invoked with multiple type substitution variations.</li> |
| </ol> |
| <h2 id="the-llvm-profiler-runtime"><a class="header" href="#the-llvm-profiler-runtime">The LLVM profiler runtime</a></h2> |
| <p>Coverage data is only generated by running the executable Rust program. <code>rustc</code> |
| statically links coverage-instrumented binaries with LLVM runtime code |
| (<a href="https://github.com/llvm/llvm-project/tree/main/compiler-rt/lib/profile">compiler-rt</a>) that implements program hooks |
| (such as an <code>exit</code> hook) to write the counter values to the <code>.profraw</code> file.</p> |
| <p>In the <code>rustc</code> source tree, |
| <code>library/profiler_builtins</code> bundles the LLVM <code>compiler-rt</code> code into a Rust library crate. |
| Note that when building <code>rustc</code>, |
| <code>profiler_builtins</code> is only included when <code>build.profiler = true</code> is set in <code>bootstrap.toml</code>.</p> |
| <p>When compiling with <code>-C instrument-coverage</code>, |
| <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/creader/struct.CrateLoader.html#method.postprocess"><code>CrateLoader::postprocess()</code></a> dynamically loads |
| <code>profiler_builtins</code> by calling <code>inject_profiler_runtime()</code>.</p> |
| <h2 id="testing-coverage-instrumentation"><a class="header" href="#testing-coverage-instrumentation">Testing coverage instrumentation</a></h2> |
| <p><a href="./tests/compiletest.html#coverage-tests">(See also the compiletest documentation for the <code>tests/coverage</code> |
| test suite.)</a></p> |
| <p>Coverage instrumentation in the MIR is validated by a <code>mir-opt</code> test: |
| <a href="https://github.com/rust-lang/rust/blob/master/tests/mir-opt/coverage/instrument_coverage.rs"><code>tests/mir-opt/coverage/instrument_coverage.rs</code></a>.</p> |
| <p>Coverage instrumentation in LLVM IR is validated by the <a href="https://github.com/rust-lang/rust/tree/master/tests/coverage"><code>tests/coverage</code></a> |
| test suite in <code>coverage-map</code> mode. |
| These tests compile a test program to LLVM IR assembly, and then |
| use the <a href="https://github.com/rust-lang/rust/tree/master/src/tools/coverage-dump"><code>src/tools/coverage-dump</code></a> tool to extract and pretty-print the |
| coverage mappings that would be embedded in the final binary.</p> |
| <p>End-to-end testing of coverage instrumentation and coverage reporting is |
| performed by the <a href="https://github.com/rust-lang/rust/tree/master/tests/coverage"><code>tests/coverage</code></a> test suite in <code>coverage-run</code> mode, |
| and by the <a href="https://github.com/rust-lang/rust/tree/master/tests/coverage-run-rustdoc"><code>tests/coverage-run-rustdoc</code></a> test suite. |
| These tests compile and run a test program with coverage |
| instrumentation, then use LLVM tools to convert the coverage data into a |
| human-readable coverage report.</p> |
| <blockquote> |
| <p>Tests in <code>coverage-run</code> mode have an implicit <code>//@ needs-profiler-runtime</code> |
| directive, so they will be skipped if the profiler runtime has not been |
| <a href="#recommended-configtoml-settings">enabled in <code>bootstrap.toml</code></a>.</p> |
| </blockquote> |
| <p>Finally, the <a href="https://github.com/rust-lang/rust/blob/master/tests/mir-opt/coverage/instrument_coverage.rs"><code>tests/codegen/instrument-coverage/testprog.rs</code></a> test compiles a simple Rust program |
| with <code>-C instrument-coverage</code> and compares the compiled program's LLVM IR to |
| expected LLVM IR instructions and structured data for a coverage-enabled |
| program, including various checks for Coverage Map-related metadata and the LLVM |
| intrinsic calls to increment the runtime counters.</p> |
| <p>Expected results for the <code>coverage</code>, <code>coverage-run-rustdoc</code>, |
| and <code>mir-opt</code> tests can be refreshed by running:</p> |
| <pre><code class="language-shell">./x test coverage --bless |
| ./x test coverage-run-rustdoc --bless |
| ./x test tests/mir-opt --bless |
| </code></pre> |
| |
| </main> |
| |
| <nav class="nav-wrapper" aria-label="Page navigation"> |
| <!-- Mobile navigation buttons --> |
| <a rel="prev" href="profile-guided-optimization.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> |
| <i class="fa fa-angle-left"></i> |
| </a> |
| |
| <a rel="next prefetch" href="sanitizers.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> |
| <i class="fa fa-angle-right"></i> |
| </a> |
| |
| <div style="clear: both"></div> |
| </nav> |
| </div> |
| </div> |
| |
| <nav class="nav-wide-wrapper" aria-label="Page navigation"> |
| <a rel="prev" href="profile-guided-optimization.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> |
| <i class="fa fa-angle-left"></i> |
| </a> |
| |
| <a rel="next prefetch" href="sanitizers.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> |
| <i class="fa fa-angle-right"></i> |
| </a> |
| </nav> |
| |
| </div> |
| |
| |
| |
| |
| <script> |
| window.playground_copyable = true; |
| </script> |
| |
| |
| <script src="elasticlunr.min.js"></script> |
| <script src="mark.min.js"></script> |
| <script src="searcher.js"></script> |
| |
| <script src="clipboard.min.js"></script> |
| <script src="highlight.js"></script> |
| <script src="book.js"></script> |
| |
| <!-- Custom JS scripts --> |
| <script src="mermaid.min.js"></script> |
| <script src="mermaid-init.js"></script> |
| |
| |
| </div> |
| </body> |
| </html> |