bcore-mutation 1.1.0

Mutation testing tool for Bitcoin Core
Documentation
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
use crate::ast_analysis::{filter_mutatable_lines, AridNodeDetector};
use crate::db::{compute_patch_hash, generate_diff, Database, MutantData};
use crate::error::{MutationError, Result};
use crate::git_changes::{get_changed_files, get_commit_hash, get_lines_touched};
use crate::operators::{self, OperatorSet};
use crate::project::Project;
use regex::Regex;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug)]
pub struct FileToMutate {
    pub file_path: String,
    pub lines_touched: Vec<usize>,
    pub is_unit_test: bool,
}

/// Chunk size for DB batch inserts.
const DB_BATCH_SIZE: usize = 100;

/// Serialize execution config options into a JSON string for the runs table.
/// Returns `None` when there is nothing worth recording.
fn build_config_json(range_lines: Option<(usize, usize)>) -> Option<String> {
    range_lines.map(|(start, end)| format!("{{\"range\":[{},{}]}}", start, end))
}

pub async fn run_mutation(
    project: Project,
    pr_number: Option<u32>,
    file: Option<PathBuf>,
    one_mutant: bool,
    only_security_mutations: bool,
    range_lines: Option<(usize, usize)>,
    coverage: Option<HashMap<String, Vec<usize>>>,
    test_only: bool,
    skip_lines: HashMap<String, Vec<usize>>,
    enable_ast_filtering: bool,
    custom_expert_rule: Option<String>,
    sqlite_path: Option<PathBuf>,
) -> Result<()> {
    // Set up database if requested.
    let mut db_and_run: Option<(Database, i64)> = None;
    if let Some(ref path) = sqlite_path {
        let db = Database::open(path)?;
        db.ensure_schema()?;
        db.seed_projects()?;
        let project_id = db.get_project_id(project.db_name())?;
        let commit_hash = get_commit_hash()
            .await
            .unwrap_or_else(|_| "unknown".to_string());
        let tool_version = env!("CARGO_PKG_VERSION");
        let config_json = build_config_json(range_lines);
        let run_id = db.create_run(
            project_id,
            &commit_hash,
            tool_version,
            pr_number,
            config_json.as_deref(),
        )?;
        println!("SQLite: created run id={} in {}", run_id, path.display());
        db_and_run = Some((db, run_id));
    }

    let operator_set = operators::for_project(project);

    let mut all_mutants: Vec<MutantData> = Vec::new();

    if let Some(file_path) = file {
        let file_str = file_path.to_string_lossy().to_string();
        let is_unit_test = file_str.contains("test") && !file_str.contains(".py");

        let mutants = mutate_file(
            &file_str,
            None,
            None,
            one_mutant,
            only_security_mutations,
            range_lines,
            &coverage,
            is_unit_test,
            &skip_lines,
            enable_ast_filtering,
            custom_expert_rule,
            operator_set.as_ref(),
        )
        .await?;
        all_mutants.extend(mutants);
    } else {
        let files_changed = get_changed_files(pr_number, project).await?;
        let mut files_to_mutate = Vec::new();

        for file_changed in files_changed {
            // Skip non-source files (docs, tooling, benchmarks, ...).
            // The exact set is project-specific; see `Project::should_skip_file`.
            if project.should_skip_file(&file_changed) {
                continue;
            }

            let lines_touched = get_lines_touched(&file_changed, project).await?;
            let is_unit_test = file_changed.contains("test")
                && !file_changed.contains(".py")
                && !file_changed.contains("util");

            if test_only && !(is_unit_test || file_changed.contains(".py")) {
                continue;
            }

            files_to_mutate.push(FileToMutate {
                file_path: file_changed,
                lines_touched,
                is_unit_test,
            });
        }

        for file_info in files_to_mutate {
            let mutants = mutate_file(
                &file_info.file_path,
                Some(file_info.lines_touched),
                pr_number,
                one_mutant,
                only_security_mutations,
                range_lines,
                &coverage,
                file_info.is_unit_test,
                &skip_lines,
                enable_ast_filtering,
                custom_expert_rule.clone(),
                operator_set.as_ref(),
            )
            .await?;
            all_mutants.extend(mutants);
        }
    }

    // Persist mutants to the database in chunks.
    if let Some((ref mut db, run_id)) = db_and_run {
        let total = all_mutants.len();
        let mut inserted = 0usize;
        for chunk in all_mutants.chunks(DB_BATCH_SIZE) {
            db.insert_mutant_batch(run_id, chunk)?;
            inserted += chunk.len();
        }
        println!(
            "SQLite: inserted {}/{} mutants for run_id={}",
            inserted, total, run_id
        );
    }

    Ok(())
}

pub async fn mutate_file(
    file_to_mutate: &str,
    touched_lines: Option<Vec<usize>>,
    pr_number: Option<u32>,
    one_mutant: bool,
    only_security_mutations: bool,
    range_lines: Option<(usize, usize)>,
    coverage: &Option<HashMap<String, Vec<usize>>>,
    is_unit_test: bool,
    skip_lines: &HashMap<String, Vec<usize>>,
    enable_ast_filtering: bool,
    custom_expert_rule: Option<String>,
    operator_set: &dyn OperatorSet,
) -> Result<Vec<MutantData>> {
    println!("\n\nGenerating mutants for {}...", file_to_mutate);

    let source_code = fs::read_to_string(file_to_mutate)?;
    let lines: Vec<&str> = source_code.lines().collect();
    println!("File has {} lines", lines.len());

    // Initialize AST-based arid node detection for C++ files
    let mut arid_detector = if enable_ast_filtering
        && (file_to_mutate.ends_with(".cpp") || file_to_mutate.ends_with(".h"))
    {
        let mut detector = AridNodeDetector::new()?;

        // Add custom expert rule if provided
        if let Some(rule) = custom_expert_rule {
            detector.add_expert_rule(&rule, "Custom user rule")?;
        }

        Some(detector)
    } else {
        if !enable_ast_filtering {
            println!("AST filtering disabled - generating all possible mutants");
        }
        None
    };

    // Filter out arid lines using AST analysis (for C++ files)
    let ast_filtered_lines = if let Some(ref mut detector) = arid_detector {
        let string_lines: Vec<String> = lines.iter().map(|s| s.to_string()).collect();
        let mutatable_line_numbers = filter_mutatable_lines(&string_lines, detector);
        println!(
            "AST analysis filtered to {} mutatable lines (from {})",
            mutatable_line_numbers.len(),
            lines.len()
        );

        // Show some examples of filtered out lines
        let filtered_out_count = lines.len() - mutatable_line_numbers.len();
        if filtered_out_count > 0 {
            println!(
                "Filtered out {} arid lines (logging, reserve calls, etc.)",
                filtered_out_count
            );
        }

        Some(mutatable_line_numbers)
    } else {
        None
    };

    // Select operators based on file type and options
    let operators = if only_security_mutations {
        println!("Using security operators");
        operator_set.security_operators()?
    } else if file_to_mutate.contains(".py") || is_unit_test {
        println!("Using test operators (Python or unit test file)");
        operator_set.test_operators()?
    } else {
        println!("Using regex operators");
        operator_set.regex_operators()?
    };

    println!("Loaded {} operators", operators.len());

    let skip_lines_for_file = skip_lines.get(file_to_mutate);
    let mut touched_lines = touched_lines.unwrap_or_else(|| (1..=lines.len()).collect());

    // Apply AST filtering if available
    if let Some(ast_lines) = ast_filtered_lines {
        // Intersect touched_lines with AST-filtered lines
        touched_lines.retain(|line_num| ast_lines.contains(line_num));
        println!(
            "After AST filtering: {} lines to process",
            touched_lines.len()
        );
    }

    // Get coverage data for this file
    let lines_with_test_coverage = if let Some(cov) = coverage {
        cov.iter()
            .find(|(path, _)| file_to_mutate.contains(path.as_str()))
            .map(|(_, lines)| lines.clone())
            .unwrap_or_default()
    } else {
        Vec::new()
    };

    if !lines_with_test_coverage.is_empty() {
        println!(
            "Using coverage data with {} covered lines",
            lines_with_test_coverage.len()
        );
    }

    let mut mutant_count = 0;
    let mut collected: Vec<MutantData> = Vec::new();

    if one_mutant {
        println!("One mutant mode enabled");
    }

    for line_num in touched_lines {
        let line_idx = line_num.saturating_sub(1);

        // Check coverage if provided
        if !lines_with_test_coverage.is_empty() && !lines_with_test_coverage.contains(&line_num) {
            continue;
        }

        // Check range if provided
        if let Some((start, end)) = range_lines {
            if line_idx < start || line_idx > end {
                continue;
            }
        }

        // Check skip lines (skip_lines uses 1-indexed line numbers)
        if let Some(skip) = skip_lines_for_file {
            if skip.contains(&line_num) {
                continue;
            }
        }

        if line_idx >= lines.len() {
            continue;
        }

        let line_before_mutation = lines[line_idx];

        // Check if line should be skipped (traditional approach)
        if should_skip_line(
            line_before_mutation,
            file_to_mutate,
            is_unit_test,
            operator_set,
        )? {
            continue;
        }

        let mut line_had_match = false;

        for operator in &operators {
            // Special handling for test operators
            if file_to_mutate.contains(".py") || is_unit_test {
                if !operator_set.should_mutate_test_line(line_before_mutation) {
                    continue;
                }
            }

            if operator.pattern.is_match(line_before_mutation) {
                line_had_match = true;
                let line_mutated = operator
                    .pattern
                    .replace(line_before_mutation, &operator.replacement);

                // Create mutated file content
                let mut mutated_lines = lines.clone();
                mutated_lines[line_idx] = &line_mutated;
                let mut mutated_content = mutated_lines.join("\n");
                if source_code.ends_with('\n') {
                    mutated_content.push('\n');
                }

                mutant_count = write_mutation(
                    file_to_mutate,
                    &mutated_content,
                    mutant_count,
                    pr_number,
                    range_lines,
                )?;

                // Collect mutant metadata for DB persistence.
                let diff = match generate_diff(file_to_mutate, &mutated_content).await {
                    Ok(d) => d,
                    Err(e) => {
                        eprintln!(
                            "  Warning: could not generate diff for mutant at line {}: {}",
                            line_num, e
                        );
                        continue;
                    }
                };
                let patch_hash = compute_patch_hash(&diff);
                let operator_label =
                    format!("{} ==> {}", operator.pattern.as_str(), operator.replacement);
                collected.push(MutantData {
                    diff,
                    patch_hash,
                    file_path: file_to_mutate.to_string(),
                    operator: operator_label,
                });

                if one_mutant {
                    break; // Break only from operator loop, continue to next line
                }
            }
        }

        // Debug output for lines that didn't match any patterns
        if !line_had_match && !line_before_mutation.trim().is_empty() {
            println!(
                "Line {} '{}' didn't match any patterns",
                line_num,
                line_before_mutation.trim()
            );
        }

        // Note: Removed the early break that was stopping line processing
        // Now each line gets processed independently
    }

    // Print AST analysis statistics
    if let Some(detector) = arid_detector {
        let stats = detector.get_stats();
        println!("AST Analysis Stats: {:?}", stats);
    }

    println!("Generated {} mutants...", mutant_count);
    Ok(collected)
}

fn should_skip_line(
    line: &str,
    file_path: &str,
    is_unit_test: bool,
    operator_set: &dyn OperatorSet,
) -> Result<bool> {
    let trimmed = line.trim_start();

    // Check basic patterns to skip
    for pattern in operator_set.do_not_mutate_patterns() {
        if trimmed.starts_with(pattern) {
            return Ok(true);
        }
    }

    // Check skip if contain patterns
    for pattern in operator_set.skip_if_contain_patterns() {
        if line.contains(pattern) {
            return Ok(true);
        }
    }

    // Language-specific checks
    if file_path.contains(".py") || is_unit_test {
        let patterns = if is_unit_test {
            operator_set.do_not_mutate_unit_patterns()
        } else {
            operator_set.do_not_mutate_py_patterns()
        };

        for pattern in patterns {
            if line.contains(pattern) {
                return Ok(true);
            }
        }

        // Check for assignment patterns
        let assignment_regex = if is_unit_test {
            Regex::new(
                r"\b(?:[a-zA-Z_][a-zA-Z0-9_:<>*&\s]+)\s+[a-zA-Z_][a-zA-Z0-9_]*(?:\[[^\]]*\])?(?:\.(?:[a-zA-Z_][a-zA-Z0-9_]*)|\->(?:[a-zA-Z_][a-zA-Z0-9_]*))*(?:\s*=\s*[^;]+|\s*\{[^;]+\})\s*",
            )?
        } else {
            Regex::new(r"^\s*([a-zA-Z_]\w*)\s*=\s*(.+)$")?
        };

        if assignment_regex.is_match(line) {
            return Ok(true);
        }
    }

    Ok(false)
}

fn get_folder_path(file_to_mutate: &str) -> String {
    let path = Path::new(file_to_mutate);

    // Get the parent directory
    if let Some(parent) = path.parent() {
        let parent_str = parent.to_str().unwrap_or("");

        // Remove "src/" prefix if it exists
        let without_src = parent_str
            .strip_prefix("src/")
            .or_else(|| parent_str.strip_prefix("src"))
            .unwrap_or(parent_str);

        // If we're left with something after removing src, return it
        // Otherwise return empty string
        if without_src.is_empty() || without_src == "src" {
            String::new()
        } else {
            without_src.to_string()
        }
    } else {
        String::new()
    }
}

fn write_mutation(
    file_to_mutate: &str,
    mutated_content: &str,
    mutant_index: usize,
    pr_number: Option<u32>,
    range_lines: Option<(usize, usize)>,
) -> Result<usize> {
    let file_extension = if file_to_mutate.ends_with(".h") {
        ".h"
    } else if file_to_mutate.ends_with(".py") {
        ".py"
    } else {
        ".cpp"
    };

    let folders = get_folder_path(file_to_mutate);

    let base_file_name = Path::new(file_to_mutate)
        .file_stem()
        .and_then(|s| s.to_str())
        .ok_or_else(|| MutationError::InvalidInput("Invalid file path".to_string()))?;

    // Combine folders with base filename
    let file_name = if folders.is_empty() {
        base_file_name.to_string()
    } else {
        format!("{}/{}", folders, base_file_name)
    };

    let ext = file_extension.trim_start_matches('.');
    let folder = if let Some(pr) = pr_number {
        format!("muts-pr-{}-{}-{}", pr, file_name.replace('/', "-"), ext)
    } else if let Some(range) = range_lines {
        format!(
            "muts-pr-{}-{}-{}",
            file_name.replace('/', "-"),
            range.0,
            range.1
        )
    } else {
        format!("muts-{}-{}", file_name.replace('/', "-"), ext)
    };

    create_mutation_folder(&folder, file_to_mutate)?;

    let mutator_file = format!(
        "{}/{}.mutant.{}{}",
        folder, base_file_name, mutant_index, file_extension
    );
    fs::write(mutator_file, mutated_content)?;

    Ok(mutant_index + 1)
}

fn create_mutation_folder(folder_name: &str, file_to_mutate: &str) -> Result<()> {
    let folder_path = Path::new(folder_name);

    if !folder_path.exists() {
        fs::create_dir_all(folder_path)?;

        let original_file_path = folder_path.join("original_file.txt");
        fs::write(original_file_path, file_to_mutate)?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_should_skip_line() {
        let ops = operators::for_project(Project::BitcoinCore);
        let ops = ops.as_ref();

        // Test basic skip patterns
        assert!(should_skip_line("// This is a comment", "test.cpp", false, ops).unwrap());
        assert!(should_skip_line("assert(condition);", "test.cpp", false, ops).unwrap());
        assert!(should_skip_line("LogPrintf(\"test\");", "test.cpp", false, ops).unwrap());
        assert!(should_skip_line("LogDebug(\"test\");", "test.cpp", false, ops).unwrap());

        // Test normal lines that shouldn't be skipped
        assert!(!should_skip_line("int x = 5;", "test.cpp", false, ops).unwrap());
        assert!(!should_skip_line("return value;", "test.cpp", false, ops).unwrap());
    }

    #[test]
    fn test_create_mutation_folder() {
        let temp_dir = tempdir().unwrap();
        let folder_path = temp_dir.path().join("test_muts");
        let folder_name = folder_path.to_str().unwrap();

        create_mutation_folder(folder_name, "test/file.cpp").unwrap();

        assert!(folder_path.exists());
        assert!(folder_path.join("original_file.txt").exists());

        let content = fs::read_to_string(folder_path.join("original_file.txt")).unwrap();
        assert_eq!(content, "test/file.cpp");
    }

    #[test]
    fn test_write_mutation() {
        let temp_dir = tempdir().unwrap();
        std::env::set_current_dir(&temp_dir).unwrap();

        let result = write_mutation("test.cpp", "mutated content", 0, None, None).unwrap();
        assert_eq!(result, 1);

        let folder_path = Path::new("muts-test-cpp");
        assert!(folder_path.exists());
        assert!(folder_path.join("test.mutant.0.cpp").exists());

        let content = fs::read_to_string(folder_path.join("test.mutant.0.cpp")).unwrap();
        assert_eq!(content, "mutated content");
    }
}