Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Coverage][MCDC] Prepare MCDC Builder for pattern matching and llvm 19 #126677

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
coverage. Warn about too much decision depth
  • Loading branch information
zhuyunxing committed Jul 16, 2024
commit 881b0b40151b7771db703a526b4c7f41945808df
2 changes: 2 additions & 0 deletions compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ mir_build_deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =

mir_build_exceeds_mcdc_condition_limit = number of conditions in decision ({$num_conditions}) exceeds limit ({$max_conditions}), so MC/DC analysis will not count this expression

mir_build_exceeds_mcdc_decision_depth = number of decisions evaluated simultaneously exceeds limit ({$max_decision_depth}). MCDC analysis will not count this expression

mir_build_extern_static_requires_unsafe =
use of extern static is unsafe and requires unsafe block
.note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
Expand Down
33 changes: 22 additions & 11 deletions compiler/rustc_mir_build/src/build/coverageinfo/mcdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ use rustc_middle::ty::TyCtxt;
use rustc_span::Span;

use crate::build::Builder;
use crate::errors::MCDCExceedsConditionLimit;
use crate::errors::{MCDCExceedsConditionLimit, MCDCExceedsDecisionDepth};

/// The MCDC bitmap scales exponentially (2^n) based on the number of conditions seen,
/// So llvm sets a maximum value prevents the bitmap footprint from growing too large without the user's knowledge.
/// This limit may be relaxed if the [upstream change](https://github.com/llvm/llvm-project/pull/82448) is merged.
const MAX_CONDITIONS_IN_DECISION: usize = 6;

/// MCDC allocates an i32 variable on stack for each depth. Ignore decisions nested too much to prevent it
/// consuming excessive memory.
const MAX_DECISION_DEPTH: u16 = 0x3FFF;

#[derive(Default)]
struct MCDCDecisionCtx {
/// To construct condition evaluation tree.
Expand Down Expand Up @@ -208,14 +212,14 @@ impl MCDCInfoBuilder {
// is empty, i.e. when all the conditions of the decision were instrumented,
// and the decision is "complete".
if let Some(decision) = decision_result {
match decision.num_conditions {
0 => {
match (decision.num_conditions, decision_depth) {
(0, _) => {
unreachable!("Decision with no condition is not expected");
}
1..=MAX_CONDITIONS_IN_DECISION => {
(1..=MAX_CONDITIONS_IN_DECISION, 0..=MAX_DECISION_DEPTH) => {
self.decision_spans.push(decision);
}
_ => {
(_, _) => {
// Do not generate mcdc mappings and statements for decisions with too many conditions.
// Therefore, first erase the condition info of the (N-1) previous branch spans.
let rebase_idx = self.branch_spans.len() - (decision.num_conditions - 1);
Expand All @@ -225,12 +229,19 @@ impl MCDCInfoBuilder {

// Then, erase this last branch span's info too, for a total of N.
condition_info = None;

tcx.dcx().emit_warn(MCDCExceedsConditionLimit {
span: decision.span,
num_conditions: decision.num_conditions,
max_conditions: MAX_CONDITIONS_IN_DECISION,
});
if decision.num_conditions > MAX_CONDITIONS_IN_DECISION {
tcx.dcx().emit_warn(MCDCExceedsConditionLimit {
span: decision.span,
num_conditions: decision.num_conditions,
max_conditions: MAX_CONDITIONS_IN_DECISION,
});
}
if decision_depth > MAX_DECISION_DEPTH {
tcx.dcx().emit_warn(MCDCExceedsDecisionDepth {
span: decision.span,
max_decision_depth: MAX_DECISION_DEPTH.into(),
});
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,14 @@ pub(crate) struct MCDCExceedsConditionLimit {
pub(crate) max_conditions: usize,
}

#[derive(Diagnostic)]
#[diag(mir_build_exceeds_mcdc_decision_depth)]
pub(crate) struct MCDCExceedsDecisionDepth {
#[primary_span]
pub span: Span,
pub max_decision_depth: usize,
}

#[derive(Diagnostic)]
#[diag(mir_build_pattern_not_covered, code = E0005)]
pub(crate) struct PatternNotCovered<'s, 'tcx> {
Expand Down