sqc 0.4.13

Software Code Quality - CERT C compliance checker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! CON07-C: Ensure that compound operations on shared variables are atomic
//!
//! Compound operations are operations that consist of more than one discrete
//! operation. Expressions that include postfix or prefix increment (++), postfix or
//! prefix decrement (--), or compound assignment operators always result in compound
//! operations. Compound assignment expressions use operators such as *=, /=, %=,
//! +=, -=, <<=, >>=, ^=, and |=. Compound operations on shared variables must be
//! performed atomically to prevent data races.
//!
//! ## Examples:
//!
//! **Non-compliant:**
//! ```c
//! static int a;
//! static int b;
//!
//! int get_sum(void) {
//!   return a + b;  // Non-atomic compound operation on shared variables
//! }
//!
//! void set_values(int new_a, int new_b) {
//!   a = new_a;  // Non-atomic compound operation
//!   b = new_b;
//! }
//! ```
//!
//! **Compliant (Mutex):**
//! ```c
//! #include <threads.h>
//!
//! static int a;
//! static int b;
//! mtx_t flag_mutex;
//!
//! int get_sum(void) {
//!   if (thrd_success != mtx_lock(&flag_mutex)) {
//!     /* Handle error */
//!   }
//!   int sum = a + b;
//!   if (thrd_success != mtx_unlock(&flag_mutex)) {
//!     /* Handle error */
//!   }
//!   return sum;
//! }
//! ```
//!
//! **Compliant (Atomic struct):**
//! ```c
//! #include <stdatomic.h>
//!
//! static _Atomic struct ab_s {
//!   int a, b;
//! } ab;
//!
//! int get_sum(void) {
//!   struct ab_s new_ab = atomic_load(&ab);
//!   return new_ab.a + new_ab.b;
//! }
//! ```
//!
//! ## Detection Strategy:
//! - Find static variables that are accessed by multiple operations
//! - Detect compound operations: +=, -=, *=, /=, %=, <<=, >>=, ^=, |=, ++, --
//! - Check for functions that read multiple static variables without synchronization
//! - Look for functions that perform operations on static variables without mutex locks
//! - Check for absence of atomic operations or mutex locks

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 Con07C;

impl CertRule for Con07C {
    fn rule_id(&self) -> &'static str {
        "CON07-C"
    }

    fn description(&self) -> &'static str {
        "Ensure that compound operations on shared variables are atomic"
    }

    fn severity(&self) -> Severity {
        Severity::Medium
    }

    fn category(&self) -> RuleCategory {
        RuleCategory::Recommendation
    }

    fn cert_id(&self) -> &'static str {
        "CON07-C"
    }

    fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
        let mut violations = Vec::new();

        // First pass: collect static variables
        let static_vars = self.collect_static_variables(node, source);

        // Second pass: check for non-atomic compound operations on these variables
        self.check_node(node, source, &static_vars, &mut violations);

        violations
    }
}

impl Con07C {
    /// Collect all static variable names from the translation unit
    fn collect_static_variables(&self, node: &Node, source: &str) -> Vec<String> {
        let mut static_vars = Vec::new();
        self.find_static_variables(node, source, &mut static_vars);
        static_vars
    }

    fn find_static_variables(&self, node: &Node, source: &str, static_vars: &mut Vec<String>) {
        if node.kind() == "declaration" {
            let mut is_static = false;
            let mut is_file_scope = node
                .parent()
                .is_some_and(|p| p.kind() == "translation_unit");
            let mut is_mutex_or_thread_type = false;
            let mut var_names = Vec::new();

            for i in 0..node.child_count() {
                if let Some(child) = node.child(i) {
                    match child.kind() {
                        "storage_class_specifier" => {
                            if get_node_text(&child, source) == "static" {
                                is_static = true;
                            }
                            if get_node_text(&child, source) == "extern" {
                                // extern declarations are not owned by this file
                                is_file_scope = false;
                            }
                        }
                        "type_identifier" => {
                            let type_name = get_node_text(&child, source);
                            if type_name.contains("mutex")
                                || type_name.contains("pthread")
                                || type_name.contains("thrd")
                                || type_name.contains("cnd")
                            {
                                is_mutex_or_thread_type = true;
                            }
                        }
                        "init_declarator" => {
                            // Case: static int a = 0;
                            if let Some(declarator) = child.child_by_field_name("declarator") {
                                if let Some(name) = self.get_identifier_name(&declarator, source) {
                                    var_names.push(name);
                                }
                            }
                        }
                        "identifier" => {
                            // Case: static int a;  (no initializer)
                            var_names.push(get_node_text(&child, source).to_string());
                        }
                        _ => {}
                    }
                }
            }

            // Collect static variables AND file-scope globals (non-mutex types)
            if is_static || (is_file_scope && !is_mutex_or_thread_type) {
                static_vars.extend(var_names);
            }
        }

        // Recursively check children
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.find_static_variables(&child, source, static_vars);
            }
        }
    }

    fn get_identifier_name(&self, node: &Node, source: &str) -> Option<String> {
        if node.kind() == "identifier" {
            return Some(get_node_text(node, source).to_string());
        }

        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if let Some(name) = self.get_identifier_name(&child, source) {
                    return Some(name);
                }
            }
        }

        None
    }

    fn check_node(
        &self,
        node: &Node,
        source: &str,
        static_vars: &[String],
        violations: &mut Vec<RuleViolation>,
    ) {
        // Look for function definitions that might access static variables
        if node.kind() == "function_definition" {
            self.check_function_for_non_atomic_operations(node, source, static_vars, violations);
        }

        // Recursively check child nodes
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.check_node(&child, source, static_vars, violations);
            }
        }
    }

    fn check_function_for_non_atomic_operations(
        &self,
        function_node: &Node,
        source: &str,
        static_vars: &[String],
        violations: &mut Vec<RuleViolation>,
    ) {
        let func_name = self
            .get_function_name(function_node, source)
            .unwrap_or_else(|| "<unknown>".to_string());

        // Skip initialization functions (they run before threading typically)
        if func_name.to_lowercase().contains("init") {
            return;
        }

        // Skip functions that use mutex locks (compliant)
        if self.uses_mutex_lock(function_node, source) {
            return;
        }

        // Skip functions that use atomic operations (compliant)
        if self.uses_atomic_operations(function_node, source) {
            return;
        }

        // Get function body
        let body = match function_node.child_by_field_name("body") {
            Some(b) => b,
            None => return,
        };

        // Check for compound operations on static variables
        let static_var_accesses = self.find_static_var_accesses(&body, source, static_vars);

        // If function performs a compound write on multiple static variables, flag it.
        // Pure reads of multiple statics are not a compound-operation violation.
        if static_var_accesses.len() > 1 {
            let has_compound_write = static_var_accesses
                .iter()
                .any(|v| self.has_compound_operation_on_var(&body, source, v));
            if has_compound_write {
                violations.push(RuleViolation {
                    rule_id: self.rule_id().to_string(),
                    severity: Severity::Medium,
                    message: format!(
                        "Function '{}' performs compound operation on shared static variables ({}) without synchronization",
                        func_name,
                        static_var_accesses.join(", ")
                    ),
                    file_path: String::new(),
                    line: function_node.start_position().row + 1,
                    column: function_node.start_position().column + 1,
                    suggestion: Some(
                        "Use mutex locks (mtx_lock/mtx_unlock) or atomic operations to ensure atomicity".to_string()
                    ),
                    ..Default::default()
                });
            }
        } else if static_var_accesses.len() == 1 {
            // Check for compound assignment operations on a single static variable
            if self.has_compound_operation_on_var(&body, source, &static_var_accesses[0]) {
                violations.push(RuleViolation {
                    rule_id: self.rule_id().to_string(),
                    severity: Severity::Medium,
                    message: format!(
                        "Function '{}' performs compound operation on shared static variable '{}' without synchronization",
                        func_name,
                        static_var_accesses[0]
                    ),
                    file_path: String::new(),
                    line: function_node.start_position().row + 1,
                    column: function_node.start_position().column + 1,
                    suggestion: Some(
                        "Use mutex locks (mtx_lock/mtx_unlock) or atomic operations to ensure atomicity".to_string()
                    ),
                    ..Default::default()
                });
            }
        }
    }

    fn get_function_name(&self, function_node: &Node, source: &str) -> Option<String> {
        for i in 0..function_node.child_count() {
            if let Some(child) = function_node.child(i) {
                if child.kind() == "function_declarator" {
                    if let Some(name) = self.get_identifier_name(&child, source) {
                        return Some(name);
                    }
                }
            }
        }
        None
    }

    fn uses_mutex_lock(&self, node: &Node, source: &str) -> bool {
        if node.kind() == "call_expression" {
            if let Some(func) = node.child_by_field_name("function") {
                let func_name = get_node_text(&func, source);
                if matches!(
                    func_name,
                    "mtx_lock"
                        | "mtx_unlock"
                        | "pthread_mutex_lock"
                        | "pthread_mutex_unlock"
                        | "stdThreadLockAcquire"
                        | "stdThreadLockRelease"
                ) {
                    return true;
                }
            }
        }

        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if self.uses_mutex_lock(&child, source) {
                    return true;
                }
            }
        }

        false
    }

    fn uses_atomic_operations(&self, node: &Node, source: &str) -> bool {
        if node.kind() == "call_expression" {
            if let Some(func) = node.child_by_field_name("function") {
                let func_name = get_node_text(&func, source);
                if func_name.starts_with("atomic_") {
                    return true;
                }
            }
        }

        // Check for _Atomic type qualifier
        if node.kind() == "type_qualifier" {
            let text = get_node_text(node, source);
            if text == "_Atomic" {
                return true;
            }
        }

        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if self.uses_atomic_operations(&child, source) {
                    return true;
                }
            }
        }

        false
    }

    fn find_static_var_accesses(
        &self,
        node: &Node,
        source: &str,
        static_vars: &[String],
    ) -> Vec<String> {
        let mut accesses = Vec::new();
        self.collect_static_var_accesses(node, source, static_vars, &mut accesses);
        accesses.sort();
        accesses.dedup();
        accesses
    }

    fn collect_static_var_accesses(
        &self,
        node: &Node,
        source: &str,
        static_vars: &[String],
        accesses: &mut Vec<String>,
    ) {
        if node.kind() == "identifier" {
            let name = get_node_text(node, source);
            if static_vars.contains(&name.to_string()) {
                accesses.push(name.to_string());
            }
        }

        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.collect_static_var_accesses(&child, source, static_vars, accesses);
            }
        }
    }

    fn has_compound_operation_on_var(&self, node: &Node, source: &str, var_name: &str) -> bool {
        if node.kind() == "assignment_expression" {
            let left = node.child_by_field_name("left");
            let right = node.child_by_field_name("right");
            if let Some(left_node) = left {
                let left_text = get_node_text(&left_node, source);
                if left_text == var_name {
                    // Compound assignment: x += 1, x -= 1, etc.
                    if let Some(operator) = node.child_by_field_name("operator") {
                        let op_text = get_node_text(&operator, source);
                        if matches!(
                            op_text,
                            "+=" | "-=" | "*=" | "/=" | "%=" | "<<=" | ">>=" | "&=" | "^=" | "|="
                        ) {
                            return true;
                        }
                    }
                    // Read-modify-write: x = x OP expr (var appears in RHS)
                    if let Some(right_node) = right {
                        let right_text = get_node_text(&right_node, source);
                        if right_text.contains(var_name) {
                            return true;
                        }
                    }
                }
            }
        }

        // Increment/decrement: x++, ++x, x--, --x
        if matches!(node.kind(), "update_expression") {
            if let Some(argument) = node.child_by_field_name("argument") {
                if get_node_text(&argument, source) == var_name {
                    return true;
                }
            }
        }

        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if self.has_compound_operation_on_var(&child, source, var_name) {
                    return true;
                }
            }
        }

        false
    }
}