foundry_evm_coverage/analysis.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
use super::{CoverageItem, CoverageItemKind, SourceLocation};
use alloy_primitives::map::HashMap;
use foundry_common::TestFunctionExt;
use foundry_compilers::artifacts::ast::{self, Ast, Node, NodeType};
use rayon::prelude::*;
use std::sync::Arc;
/// A visitor that walks the AST of a single contract and finds coverage items.
#[derive(Clone, Debug)]
pub struct ContractVisitor<'a> {
/// The source ID of the contract.
source_id: usize,
/// The source code that contains the AST being walked.
source: &'a str,
/// The name of the contract being walked.
contract_name: &'a Arc<str>,
/// The current branch ID
branch_id: usize,
/// Stores the last line we put in the items collection to ensure we don't push duplicate lines
last_line: usize,
/// Coverage items
pub items: Vec<CoverageItem>,
}
impl<'a> ContractVisitor<'a> {
pub fn new(source_id: usize, source: &'a str, contract_name: &'a Arc<str>) -> Self {
Self { source_id, source, contract_name, branch_id: 0, last_line: 0, items: Vec::new() }
}
pub fn visit_contract(&mut self, node: &Node) -> eyre::Result<()> {
// Find all functions and walk their AST
for node in &node.nodes {
match node.node_type {
NodeType::FunctionDefinition => {
self.visit_function_definition(node)?;
}
NodeType::ModifierDefinition => {
self.visit_modifier_or_yul_fn_definition(node)?;
}
_ => {}
}
}
Ok(())
}
fn visit_function_definition(&mut self, node: &Node) -> eyre::Result<()> {
let name: String =
node.attribute("name").ok_or_else(|| eyre::eyre!("Function has no name"))?;
let kind: String =
node.attribute("kind").ok_or_else(|| eyre::eyre!("Function has no kind"))?;
match &node.body {
Some(body) => {
// Do not add coverage item for constructors without statements.
if kind == "constructor" && !has_statements(body) {
return Ok(())
}
self.push_item_kind(CoverageItemKind::Function { name }, &node.src);
self.visit_block(body)
}
_ => Ok(()),
}
}
fn visit_modifier_or_yul_fn_definition(&mut self, node: &Node) -> eyre::Result<()> {
let name: String =
node.attribute("name").ok_or_else(|| eyre::eyre!("Modifier has no name"))?;
match &node.body {
Some(body) => {
self.push_item_kind(CoverageItemKind::Function { name }, &node.src);
self.visit_block(body)
}
_ => Ok(()),
}
}
fn visit_block(&mut self, node: &Node) -> eyre::Result<()> {
let statements: Vec<Node> = node.attribute("statements").unwrap_or_default();
for statement in &statements {
self.visit_statement(statement)?;
}
Ok(())
}
fn visit_statement(&mut self, node: &Node) -> eyre::Result<()> {
match node.node_type {
// Blocks
NodeType::Block | NodeType::UncheckedBlock | NodeType::YulBlock => {
self.visit_block(node)
}
// Inline assembly block
NodeType::InlineAssembly => self.visit_block(
&node
.attribute("AST")
.ok_or_else(|| eyre::eyre!("inline assembly block with no AST attribute"))?,
),
// Simple statements
NodeType::Break |
NodeType::Continue |
NodeType::EmitStatement |
NodeType::RevertStatement |
NodeType::YulAssignment |
NodeType::YulBreak |
NodeType::YulContinue |
NodeType::YulLeave |
NodeType::YulVariableDeclaration => {
self.push_item_kind(CoverageItemKind::Statement, &node.src);
Ok(())
}
// Skip placeholder statements as they are never referenced in source maps.
NodeType::PlaceholderStatement => Ok(()),
// Return with eventual subcall
NodeType::Return => {
self.push_item_kind(CoverageItemKind::Statement, &node.src);
if let Some(expr) = node.attribute("expression") {
self.visit_expression(&expr)?;
}
Ok(())
}
// Variable declaration
NodeType::VariableDeclarationStatement => {
self.push_item_kind(CoverageItemKind::Statement, &node.src);
if let Some(expr) = node.attribute("initialValue") {
self.visit_expression(&expr)?;
}
Ok(())
}
// While loops
NodeType::DoWhileStatement | NodeType::WhileStatement => {
self.visit_expression(
&node
.attribute("condition")
.ok_or_else(|| eyre::eyre!("while statement had no condition"))?,
)?;
let body = node
.body
.as_deref()
.ok_or_else(|| eyre::eyre!("while statement had no body node"))?;
self.visit_block_or_statement(body)
}
// For loops
NodeType::ForStatement => {
if let Some(stmt) = node.attribute("initializationExpression") {
self.visit_statement(&stmt)?;
}
if let Some(expr) = node.attribute("condition") {
self.visit_expression(&expr)?;
}
if let Some(stmt) = node.attribute("loopExpression") {
self.visit_statement(&stmt)?;
}
let body = node
.body
.as_deref()
.ok_or_else(|| eyre::eyre!("for statement had no body node"))?;
self.visit_block_or_statement(body)
}
// Expression statement
NodeType::ExpressionStatement | NodeType::YulExpressionStatement => self
.visit_expression(
&node
.attribute("expression")
.ok_or_else(|| eyre::eyre!("expression statement had no expression"))?,
),
// If statement
NodeType::IfStatement => {
self.visit_expression(
&node
.attribute("condition")
.ok_or_else(|| eyre::eyre!("if statement had no condition"))?,
)?;
let true_body: Node = node
.attribute("trueBody")
.ok_or_else(|| eyre::eyre!("if statement had no true body"))?;
// We need to store the current branch ID here since visiting the body of either of
// the if blocks may increase `self.branch_id` in the case of nested if statements.
let branch_id = self.branch_id;
// We increase the branch ID here such that nested branches do not use the same
// branch ID as we do.
self.branch_id += 1;
match node.attribute::<Node>("falseBody") {
// Both if/else statements.
Some(false_body) => {
// Add branch coverage items only if one of true/branch bodies contains
// statements.
if has_statements(&true_body) || has_statements(&false_body) {
// The branch instruction is mapped to the first opcode within the true
// body source range.
self.push_item_kind(
CoverageItemKind::Branch {
branch_id,
path_id: 0,
is_first_opcode: true,
},
&true_body.src,
);
// Add the coverage item for branch 1 (false body).
// The relevant source range for the false branch is the `else`
// statement itself and the false body of the else statement.
self.push_item_kind(
CoverageItemKind::Branch {
branch_id,
path_id: 1,
is_first_opcode: false,
},
&ast::LowFidelitySourceLocation {
start: node.src.start,
length: false_body.src.length.map(|length| {
false_body.src.start - true_body.src.start + length
}),
index: node.src.index,
},
);
// Process the true body.
self.visit_block_or_statement(&true_body)?;
// Process the false body.
self.visit_block_or_statement(&false_body)?;
}
}
None => {
// Add single branch coverage only if it contains statements.
if has_statements(&true_body) {
// Add the coverage item for branch 0 (true body).
self.push_item_kind(
CoverageItemKind::Branch {
branch_id,
path_id: 0,
is_first_opcode: true,
},
&true_body.src,
);
// Process the true body.
self.visit_block_or_statement(&true_body)?;
}
}
}
Ok(())
}
NodeType::YulIf => {
self.visit_expression(
&node
.attribute("condition")
.ok_or_else(|| eyre::eyre!("yul if statement had no condition"))?,
)?;
let body = node
.body
.as_deref()
.ok_or_else(|| eyre::eyre!("yul if statement had no body"))?;
// We need to store the current branch ID here since visiting the body of either of
// the if blocks may increase `self.branch_id` in the case of nested if statements.
let branch_id = self.branch_id;
// We increase the branch ID here such that nested branches do not use the same
// branch ID as we do
self.branch_id += 1;
self.push_item_kind(
CoverageItemKind::Branch { branch_id, path_id: 0, is_first_opcode: false },
&node.src,
);
self.visit_block(body)?;
Ok(())
}
// Try-catch statement. Coverage is reported for expression, for each clause and their
// bodies (if any).
NodeType::TryStatement => {
self.visit_expression(
&node
.attribute("externalCall")
.ok_or_else(|| eyre::eyre!("try statement had no call"))?,
)?;
// Add coverage for each Try-catch clause.
for clause in node
.attribute::<Vec<Node>>("clauses")
.ok_or_else(|| eyre::eyre!("try statement had no clause"))?
{
// Add coverage for clause statement.
self.push_item_kind(CoverageItemKind::Statement, &clause.src);
self.visit_statement(&clause)?;
// Add coverage for clause body only if it is not empty.
if let Some(block) = clause.attribute::<Node>("block") {
if has_statements(&block) {
self.push_item_kind(CoverageItemKind::Statement, &block.src);
self.visit_block(&block)?;
}
}
}
Ok(())
}
NodeType::YulSwitch => {
// Add coverage for each case statement amd their bodies.
for case in node
.attribute::<Vec<Node>>("cases")
.ok_or_else(|| eyre::eyre!("yul switch had no case"))?
{
self.push_item_kind(CoverageItemKind::Statement, &case.src);
self.visit_statement(&case)?;
if let Some(body) = case.body {
self.push_item_kind(CoverageItemKind::Statement, &body.src);
self.visit_block(&body)?
}
}
Ok(())
}
NodeType::YulForLoop => {
if let Some(condition) = node.attribute("condition") {
self.visit_expression(&condition)?;
}
if let Some(pre) = node.attribute::<Node>("pre") {
self.visit_block(&pre)?
}
if let Some(post) = node.attribute::<Node>("post") {
self.visit_block(&post)?
}
if let Some(body) = &node.body {
self.push_item_kind(CoverageItemKind::Statement, &body.src);
self.visit_block(body)?
}
Ok(())
}
NodeType::YulFunctionDefinition => self.visit_modifier_or_yul_fn_definition(node),
_ => {
warn!("unexpected node type, expected a statement: {:?}", node.node_type);
Ok(())
}
}
}
fn visit_expression(&mut self, node: &Node) -> eyre::Result<()> {
match node.node_type {
NodeType::Assignment |
NodeType::UnaryOperation |
NodeType::Conditional |
NodeType::YulFunctionCall => {
self.push_item_kind(CoverageItemKind::Statement, &node.src);
Ok(())
}
NodeType::FunctionCall => {
// Do not count other kinds of calls towards coverage (like `typeConversion`
// and `structConstructorCall`).
let kind: Option<String> = node.attribute("kind");
if let Some("functionCall") = kind.as_deref() {
self.push_item_kind(CoverageItemKind::Statement, &node.src);
let expr: Option<Node> = node.attribute("expression");
if let Some(NodeType::Identifier) = expr.as_ref().map(|expr| &expr.node_type) {
// Might be a require call, add branch coverage.
let name: Option<String> = expr.and_then(|expr| expr.attribute("name"));
if let Some("require" | "assert") = name.as_deref() {
let branch_id = self.branch_id;
self.branch_id += 1;
self.push_item_kind(
CoverageItemKind::Branch {
branch_id,
path_id: 0,
is_first_opcode: false,
},
&node.src,
);
self.push_item_kind(
CoverageItemKind::Branch {
branch_id,
path_id: 1,
is_first_opcode: false,
},
&node.src,
);
}
}
}
Ok(())
}
NodeType::BinaryOperation => {
self.push_item_kind(CoverageItemKind::Statement, &node.src);
// visit left and right expressions
// There could possibly a function call in the left or right expression
// e.g: callFunc(a) + callFunc(b)
if let Some(expr) = node.attribute("leftExpression") {
self.visit_expression(&expr)?;
}
if let Some(expr) = node.attribute("rightExpression") {
self.visit_expression(&expr)?;
}
Ok(())
}
// Does not count towards coverage
NodeType::FunctionCallOptions |
NodeType::Identifier |
NodeType::IndexAccess |
NodeType::IndexRangeAccess |
NodeType::Literal |
NodeType::YulLiteralValue |
NodeType::YulIdentifier => Ok(()),
_ => {
warn!("unexpected node type, expected an expression: {:?}", node.node_type);
Ok(())
}
}
}
fn visit_block_or_statement(&mut self, node: &Node) -> eyre::Result<()> {
match node.node_type {
NodeType::Block => self.visit_block(node),
NodeType::Break |
NodeType::Continue |
NodeType::DoWhileStatement |
NodeType::EmitStatement |
NodeType::ExpressionStatement |
NodeType::ForStatement |
NodeType::IfStatement |
NodeType::InlineAssembly |
NodeType::Return |
NodeType::RevertStatement |
NodeType::TryStatement |
NodeType::VariableDeclarationStatement |
NodeType::YulVariableDeclaration |
NodeType::WhileStatement => self.visit_statement(node),
// Skip placeholder statements as they are never referenced in source maps.
NodeType::PlaceholderStatement => Ok(()),
_ => {
warn!("unexpected node type, expected block or statement: {:?}", node.node_type);
Ok(())
}
}
}
/// Creates a coverage item for a given kind and source location. Pushes item to the internal
/// collection (plus additional coverage line if item is a statement).
fn push_item_kind(&mut self, kind: CoverageItemKind, src: &ast::LowFidelitySourceLocation) {
let item = CoverageItem { kind, loc: self.source_location_for(src), hits: 0 };
// Push a line item if we haven't already
if matches!(item.kind, CoverageItemKind::Statement | CoverageItemKind::Branch { .. }) &&
self.last_line < item.loc.line
{
self.items.push(CoverageItem {
kind: CoverageItemKind::Line,
loc: item.loc.clone(),
hits: 0,
});
self.last_line = item.loc.line;
}
self.items.push(item);
}
fn source_location_for(&self, loc: &ast::LowFidelitySourceLocation) -> SourceLocation {
let loc_start =
self.source.char_indices().map(|(i, _)| i).nth(loc.start).unwrap_or_default();
SourceLocation {
source_id: self.source_id,
contract_name: self.contract_name.clone(),
start: loc.start as u32,
length: loc.length.map(|x| x as u32),
line: self.source[..loc_start].lines().count(),
}
}
}
/// Helper function to check if a given node is or contains any statement.
fn has_statements(node: &Node) -> bool {
match node.node_type {
NodeType::DoWhileStatement |
NodeType::EmitStatement |
NodeType::ExpressionStatement |
NodeType::ForStatement |
NodeType::IfStatement |
NodeType::RevertStatement |
NodeType::TryStatement |
NodeType::VariableDeclarationStatement |
NodeType::WhileStatement => true,
_ => {
let statements: Vec<Node> = node.attribute("statements").unwrap_or_default();
!statements.is_empty()
}
}
}
/// [`SourceAnalyzer`] result type.
#[derive(Debug)]
pub struct SourceAnalysis {
/// A collection of coverage items.
pub items: Vec<CoverageItem>,
}
/// Analyzes a set of sources to find coverage items.
#[derive(Debug)]
pub struct SourceAnalyzer<'a> {
sources: &'a SourceFiles<'a>,
}
impl<'a> SourceAnalyzer<'a> {
/// Creates a new source analyzer.
pub fn new(data: &'a SourceFiles<'a>) -> Self {
Self { sources: data }
}
/// Analyzes contracts in the sources held by the source analyzer.
///
/// Coverage items are found by:
/// - Walking the AST of each contract (except interfaces)
/// - Recording the items of each contract
///
/// Each coverage item contains relevant information to find opcodes corresponding to them: the
/// source ID the item is in, the source code range of the item, and the contract name the item
/// is in.
///
/// Note: Source IDs are only unique per compilation job; that is, a code base compiled with
/// two different solc versions will produce overlapping source IDs if the compiler version is
/// not taken into account.
pub fn analyze(&self) -> eyre::Result<SourceAnalysis> {
let items = self
.sources
.sources
.par_iter()
.flat_map_iter(|(&source_id, SourceFile { source, ast })| {
ast.nodes.iter().map(move |node| {
if !matches!(node.node_type, NodeType::ContractDefinition) {
return Ok(vec![]);
}
// Skip interfaces which have no function implementations.
let contract_kind: String = node
.attribute("contractKind")
.ok_or_else(|| eyre::eyre!("Contract has no kind"))?;
if contract_kind == "interface" {
return Ok(vec![]);
}
let name = node
.attribute("name")
.ok_or_else(|| eyre::eyre!("Contract has no name"))?;
let mut visitor = ContractVisitor::new(source_id, source, &name);
visitor.visit_contract(node)?;
let mut items = visitor.items;
let is_test = items.iter().any(|item| {
if let CoverageItemKind::Function { name } = &item.kind {
name.is_any_test()
} else {
false
}
});
if is_test {
items.clear();
}
Ok(items)
})
})
.collect::<eyre::Result<Vec<Vec<_>>>>()?;
Ok(SourceAnalysis { items: items.concat() })
}
}
/// A list of versioned sources and their ASTs.
#[derive(Debug, Default)]
pub struct SourceFiles<'a> {
/// The versioned sources.
pub sources: HashMap<usize, SourceFile<'a>>,
}
/// The source code and AST of a file.
#[derive(Debug)]
pub struct SourceFile<'a> {
/// The source code.
pub source: String,
/// The AST of the source code.
pub ast: &'a Ast,
}