use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use lang_parsing_substrate::query;
use tree_sitter::Node;
pub struct Con43C;
impl CertRule for Con43C {
fn rule_id(&self) -> &'static str {
"CON43-C"
}
fn description(&self) -> &'static str {
"Do not allow data races in multithreaded code"
}
fn severity(&self) -> Severity {
Severity::High
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"CON43-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
check_static_volatile(node, source, &mut violations);
check_double_fetch(node, source, &mut violations);
violations
}
}
fn check_static_volatile(node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
for n in query::find_descendants_of_kind(*node, "declaration") {
let text = get_node_text(&n, source);
if text.contains("static") && text.contains("volatile") {
if let Some(var_name) = extract_var_name(&n, source) {
violations.push(RuleViolation {
rule_id: "CON43-C".to_string(),
file_path: "".to_string(),
message: format!(
"Static volatile variable '{}' may be accessed by multiple threads without synchronization",
var_name
),
line: n.start_position().row + 1,
column: n.start_position().column,
severity: Severity::High,
suggestion: Some("Use atomic operations, mutex locks, or other synchronization primitives to protect shared data".to_string()),
requires_manual_review: Some(true),
});
}
}
}
}
fn check_double_fetch(node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
for n in query::find_descendants_of_kind(*node, "switch_statement") {
if let Some(condition) = get_switch_condition(&n) {
if contains_pointer_deref(&condition, source) {
if !has_synchronization_nearby(&n, source) {
violations.push(RuleViolation {
rule_id: "CON43-C".to_string(),
file_path: "".to_string(),
message: "Dereferencing pointer in switch statement may create double-fetch vulnerability".to_string(),
line: n.start_position().row + 1,
column: n.start_position().column,
severity: Severity::High,
suggestion: Some("Store dereferenced value in a local variable or use atomic operations".to_string()),
requires_manual_review: Some(true),
});
}
}
}
}
}
fn has_synchronization_nearby(node: &Node, source: &str) -> bool {
let mut current = *node;
while let Some(parent) = current.parent() {
if parent.kind() == "function_definition" {
let text = get_node_text(&parent, source);
if text.contains("mtx_lock")
|| text.contains("atomic_")
|| text.contains("pthread_mutex")
{
return true;
}
break;
}
current = parent;
}
false
}
fn contains_pointer_deref(node: &Node, _source: &str) -> bool {
query::find_first_descendant(*node, |n| n.kind() == "pointer_expression").is_some()
}
fn extract_var_name(node: &Node, source: &str) -> Option<String> {
find_identifier(node, source)
}
fn find_identifier(node: &Node, source: &str) -> Option<String> {
query::find_first_descendant(*node, |n| n.kind() == "identifier")
.map(|n| get_node_text(&n, source).to_string())
}
fn get_switch_condition<'a>(node: &'a Node) -> Option<Node<'a>> {
let mut cursor = node.walk();
let result = node
.children(&mut cursor)
.find(|child| child.kind() == "parenthesized_expression");
result
}