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 Con39C;
impl CertRule for Con39C {
fn rule_id(&self) -> &'static str {
"CON39-C"
}
fn description(&self) -> &'static str {
"Do not join or detach a thread that was previously joined or detached"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"CON39-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
check_for_self_detach(node, source, &mut violations);
violations
}
}
fn check_for_self_detach(node: &Node, file_content: &str, violations: &mut Vec<RuleViolation>) {
let mut cursor = node.walk();
for func_node in node.children(&mut cursor) {
if func_node.kind() == "function_definition" {
if contains_self_detach(&func_node, file_content) {
check_thread_usage(&func_node, file_content, violations);
}
}
}
}
fn contains_self_detach(func_node: &Node, file_content: &str) -> bool {
check_node_for_self_detach(func_node, file_content)
}
fn check_node_for_self_detach(node: &Node, file_content: &str) -> bool {
query::find_first_descendant(*node, |n| {
if n.kind() != "call_expression" {
return false;
}
let Some(func_name_node) = n.child_by_field_name("function") else {
return false;
};
if get_node_text(&func_name_node, file_content) != "thrd_detach" {
return false;
}
let Some(args_node) = n.child_by_field_name("arguments") else {
return false;
};
get_node_text(&args_node, file_content).contains("thrd_current()")
})
.is_some()
}
fn check_thread_usage(func_node: &Node, file_content: &str, violations: &mut Vec<RuleViolation>) {
let func_name = if let Some(declarator) = func_node.child_by_field_name("declarator") {
get_function_name(&declarator, file_content)
} else {
return;
};
if func_name.is_empty() {
return;
}
let root = func_node.parent().unwrap_or(*func_node);
search_for_thread_create(&root, &func_name, file_content, violations);
}
fn search_for_thread_create(
node: &Node,
func_name: &str,
file_content: &str,
violations: &mut Vec<RuleViolation>,
) {
for n in query::find_descendants_of_kind(*node, "call_expression") {
if let Some(fn_node) = n.child_by_field_name("function") {
let fn_name = get_node_text(&fn_node, file_content);
if fn_name == "thrd_create" {
if let Some(args) = n.child_by_field_name("arguments") {
let args_text = get_node_text(&args, file_content);
if args_text.contains(func_name) {
if check_for_join_after_create(&n, file_content) {
let start_pos = n.start_position();
let line = start_pos.row + 1;
let column = start_pos.column + 1;
violations.push(RuleViolation {
rule_id: "CON39-C".to_string(),
severity: Severity::Low,
file_path: String::new(),
line,
column,
message: format!(
"Thread created with function '{}' that detaches itself, but is later joined, causing undefined behavior",
func_name
),
suggestion: None,
requires_manual_review: None,
});
}
}
}
}
}
}
}
fn get_function_name(declarator: &Node, file_content: &str) -> String {
find_identifier_in_node(declarator, file_content).unwrap_or_default()
}
fn find_identifier_in_node(node: &Node, file_content: &str) -> Option<String> {
query::find_first_descendant(*node, |n| n.kind() == "identifier")
.map(|n| get_node_text(&n, file_content).to_string())
}
fn check_for_join_after_create(create_node: &Node, file_content: &str) -> bool {
let mut parent = create_node.parent();
while let Some(p) = parent {
if p.kind() == "function_definition" {
return find_thrd_join_in_node(&p, file_content);
}
parent = p.parent();
}
false
}
fn find_thrd_join_in_node(node: &Node, file_content: &str) -> bool {
query::find_first_descendant(*node, |n| {
if n.kind() != "call_expression" {
return false;
}
let Some(fn_node) = n.child_by_field_name("function") else {
return false;
};
get_node_text(&fn_node, file_content) == "thrd_join"
})
.is_some()
}