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 Pos51C;
impl Pos51C {
pub fn new() -> Self {
Self
}
fn is_mutex_lock_call(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source).trim();
return func_name == "pthread_mutex_lock";
}
}
false
}
fn get_mutex_argument(&self, node: &Node, source: &str) -> Option<String> {
if let Some(arguments) = node.child_by_field_name("arguments") {
for i in 0..arguments.child_count() {
if let Some(arg) = arguments.child(i) {
if arg.kind() != "," && arg.kind() != "(" && arg.kind() != ")" {
return Some(get_node_text(&arg, source).trim().to_string());
}
}
}
}
None
}
fn collect_mutex_locks<'a>(&self, node: &Node<'a>, source: &str) -> Vec<(Node<'a>, String)> {
let mut locks = Vec::new();
if self.is_mutex_lock_call(node, source) {
if let Some(mutex_arg) = self.get_mutex_argument(node, source) {
locks.push((*node, mutex_arg));
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
locks.extend(self.collect_mutex_locks(&child, source));
}
}
locks
}
fn contains_conditional_ordering(&self, node: &Node, source: &str) -> bool {
if node.kind() == "if_statement" {
if let Some(condition) = node.child_by_field_name("condition") {
let cond_text = get_node_text(&condition, source);
if (cond_text.contains("->id") || cond_text.contains(".id"))
&& (cond_text.contains('<') || cond_text.contains('>'))
{
return true;
}
if cond_text.contains('<') || cond_text.contains('>') {
if self.is_pointer_comparison(&condition, source) {
return true;
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_conditional_ordering(&child, source) {
return true;
}
}
}
false
}
fn is_pointer_comparison(&self, node: &Node, source: &str) -> bool {
if node.kind() == "binary_expression" {
let _text = get_node_text(node, source);
if let Some(op) = node.child_by_field_name("operator") {
let op_text = get_node_text(&op, source);
if op_text == "<" || op_text == ">" {
if let Some(left) = node.child_by_field_name("left") {
if let Some(right) = node.child_by_field_name("right") {
let left_text = get_node_text(&left, source);
let right_text = get_node_text(&right, source);
if !left_text.contains('(')
&& !right_text.contains('(')
&& !left_text.chars().all(|c| c.is_ascii_digit())
&& !right_text.chars().all(|c| c.is_ascii_digit())
{
return true;
}
}
}
}
}
}
false
}
fn check_function_for_deadlock(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
if node.kind() != "function_definition" {
return;
}
if let Some(body) = node.child_by_field_name("body") {
let locks = self.collect_mutex_locks(&body, source);
if locks.len() >= 2 {
let has_conditional = self.contains_conditional_ordering(&body, source);
if !has_conditional {
let uses_variables = locks.iter().any(|(_, mutex_arg)| {
mutex_arg.contains("->") || mutex_arg.contains('.')
});
if uses_variables {
if let Some((first_lock, _)) = locks.first() {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: format!(
"Potential deadlock: multiple pthread_mutex_lock() calls ({} locks) without predefined ordering. Locks may be acquired in inconsistent order across threads.",
locks.len()
),
file_path: String::new(),
line: first_lock.start_position().row + 1,
column: first_lock.start_position().column + 1,
suggestion: Some(
"Acquire locks in a predefined order (e.g., based on unique resource IDs). Use conditional statements to ensure consistent lock ordering: if (res1->id < res2->id) { lock(res1); lock(res2); } else { lock(res2); lock(res1); }".to_string()
),
..Default::default()
});
}
}
}
}
}
}
}
impl CertRule for Pos51C {
fn rule_id(&self) -> &'static str {
"POS51-C"
}
fn description(&self) -> &'static str {
"Avoid deadlock with POSIX threads by locking in predefined order"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"POS51-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Pos51C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
self.check_function_for_deadlock(node, source, violations);
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
}