use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use tree_sitter::Node;
pub struct Con09C;
impl CertRule for Con09C {
fn rule_id(&self) -> &'static str {
"CON09-C"
}
fn description(&self) -> &'static str {
"Avoid the ABA problem when using lock-free algorithms"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"CON09-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Con09C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "function_definition" {
self.check_function(node, source, violations);
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn check_function(&self, func_node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
let body = if let Some(body_node) = func_node.child_by_field_name("body") {
body_node
} else {
return;
};
let has_mutex_lock = self.has_mutex_lock(&body, source);
self.find_and_check_cas_operations(&body, source, has_mutex_lock, violations);
}
fn has_mutex_lock(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(func_node) = node.child_by_field_name("function") {
let func_name = get_node_text(&func_node, source);
if self.is_lock_function(&func_name) {
return true;
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.has_mutex_lock(&child, source) {
return true;
}
}
}
false
}
fn find_and_check_cas_operations(
&self,
node: &Node,
source: &str,
has_mutex_lock: bool,
violations: &mut Vec<RuleViolation>,
) {
if node.kind() == "call_expression" {
if let Some(func_node) = node.child_by_field_name("function") {
let func_name = get_node_text(&func_node, source);
if self.is_cas_function(&func_name) {
if !has_mutex_lock {
let start_point = node.start_position();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: format!(
"Atomic compare-and-swap operation '{}' used without mutex protection, which may lead to ABA problem",
func_name
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"Protect compare-and-swap operations with a mutex, or use hazard pointers to prevent the ABA problem".to_string()
),
..Default::default()
});
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.find_and_check_cas_operations(&child, source, has_mutex_lock, violations);
}
}
}
fn is_lock_function(&self, func_name: &str) -> bool {
matches!(
func_name.trim(),
"mtx_lock"
| "pthread_mutex_lock"
| "mtx_trylock"
| "pthread_mutex_trylock"
| "mtx_timedlock"
| "pthread_mutex_timedlock"
)
}
fn is_cas_function(&self, func_name: &str) -> bool {
let trimmed = func_name.trim();
trimmed.starts_with("atomic_compare_exchange")
|| trimmed == "CAS"
|| trimmed == "__sync_bool_compare_and_swap"
|| trimmed == "__sync_val_compare_and_swap"
}
}