about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2023-11-12 18:07:28 +1100
committerZalathar <Zalathar@users.noreply.github.com>2023-11-12 18:33:11 +1100
commited8298b825b8c2c5b57471cdd13dec9f94e26c03 (patch)
treef1687edeaf1dc32b7914e93c6e3aa7a0e3914ee8
parenta04d56b36d8f634abd7bdd64dd859a30655f1818 (diff)
downloadrust-ed8298b825b8c2c5b57471cdd13dec9f94e26c03.tar.gz
rust-ed8298b825b8c2c5b57471cdd13dec9f94e26c03.zip
coverage: Avoid creating malformed macro name spans
This method is trying to detect macro invocations, so that it can split a span
into two parts just after the `!` of the invocation.

Under some circumstances (probably involving nested macros), it gets confused
and produces a span that is larger than the original span, and possibly extends
outside its enclosing function and even into an adjacent file.

In extreme cases, that can result in malformed coverage mappings that cause
`llvm-cov` to fail. For now, we at least want to detect these egregious cases
and avoid them, so that coverage reports can still be produced.
-rw-r--r--compiler/rustc_mir_transform/src/coverage/spans.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs
index 704eea413e1..b318134ae67 100644
--- a/compiler/rustc_mir_transform/src/coverage/spans.rs
+++ b/compiler/rustc_mir_transform/src/coverage/spans.rs
@@ -381,6 +381,12 @@ impl<'a> CoverageSpansGenerator<'a> {
 
         let merged_prefix_len = self.curr_original_span.lo() - curr.span.lo();
         let after_macro_bang = merged_prefix_len + BytePos(visible_macro.as_str().len() as u32 + 1);
+        if self.curr().span.lo() + after_macro_bang > self.curr().span.hi() {
+            // Something is wrong with the macro name span;
+            // return now to avoid emitting malformed mappings.
+            // FIXME(#117788): Track down why this happens.
+            return;
+        }
         let mut macro_name_cov = curr.clone();
         self.curr_mut().span = curr.span.with_lo(curr.span.lo() + after_macro_bang);
         macro_name_cov.span =