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 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 {
if node.kind() == "call_expression" {
if let Some(func_name_node) = node.child_by_field_name("function") {
let func_name = get_node_text(&func_name_node, file_content);
if func_name == "thrd_detach" {
if let Some(args_node) = node.child_by_field_name("arguments") {
let args_text = get_node_text(&args_node, file_content);
if args_text.contains("thrd_current()") {
return true;
}
}
}
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if check_node_for_self_detach(&child, file_content) {
return true;
}
}
false
}
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>,
) {
if node.kind() == "call_expression" {
if let Some(fn_node) = node.child_by_field_name("function") {
let fn_name = get_node_text(&fn_node, file_content);
if fn_name == "thrd_create" {
if let Some(args) = node.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(node, file_content) {
let start_pos = node.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,
});
}
}
}
}
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
search_for_thread_create(&child, func_name, file_content, violations);
}
}
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> {
if node.kind() == "identifier" {
return Some(get_node_text(node, file_content).to_string());
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(result) = find_identifier_in_node(&child, file_content) {
return Some(result);
}
}
None
}
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 {
if node.kind() == "call_expression" {
if let Some(fn_node) = node.child_by_field_name("function") {
let fn_name = get_node_text(&fn_node, file_content);
if fn_name == "thrd_join" {
return true;
}
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if find_thrd_join_in_node(&child, file_content) {
return true;
}
}
false
}