pmat 2.93.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! WebAssembly Analysis Support for PMAT
//!
//! This module provides WASM-specific analysis capabilities using wasmparser
//! for bytecode analysis and complexity metrics extraction from WASM modules.

use wasmparser::{Parser, Payload};
#[cfg(feature = "wasm-ast")]
use crate::services::context::AstItem;
use std::path::{Path, PathBuf};

/// WASM module analyzer that extracts WASM-specific information
pub struct WasmModuleAnalyzer {
    items: Vec<AstItem>,
    _file_path: PathBuf,
    module_name: String,
    function_count: usize,
    _import_count: usize,
    _export_count: usize,
}

impl WasmModuleAnalyzer {
    /// Creates a new WASM module analyzer
    #[must_use] 
    pub fn new(file_path: &Path) -> Self {
        Self {
            items: Vec::new(),
            _file_path: file_path.to_path_buf(),
            module_name: file_path.file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("unknown")
                .to_string(),
            function_count: 0,
            _import_count: 0,
            _export_count: 0,
        }
    }

    /// Analyzes WASM binary and extracts AST items (complexity ≤10)
    pub fn analyze_wasm_binary(mut self, wasm_bytes: &[u8]) -> Result<Vec<AstItem>, String> {
        if wasm_bytes.len() < 8 {
            return Err("Invalid WASM binary: too short".to_string());
        }

        if &wasm_bytes[0..4] != b"\0asm" {
            return Err("Invalid WASM magic number".to_string());
        }

        let parser = Parser::new(0);
        for (i, payload) in parser.parse_all(wasm_bytes).enumerate() {
            match payload {
                Ok(Payload::FunctionSection(_)) => {
                    self.function_count = i + 1;
                    self.items.push(AstItem::Function {
                        name: self.get_qualified_name(&format!("function_{i}")),
                        visibility: "export".to_string(),
                        is_async: false,
                        line: 1,
                    });
                }
                _ => continue,
            }
        }

        Ok(self.items)
    }

    /// Analyzes WASM text format (.wat) (complexity ≤10)
    pub fn analyze_wat_text(mut self, wat_source: &str) -> Result<Vec<AstItem>, String> {
        let mut function_count = 0;

        for line in wat_source.lines() {
            let trimmed = line.trim();

            if trimmed.contains("(func ") {
                let func_name = self.extract_wat_function_name(trimmed);
                let qualified_name = self.get_qualified_name(&func_name);

                self.items.push(AstItem::Function {
                    name: qualified_name,
                    visibility: "export".to_string(),
                    is_async: false,
                    line: function_count + 1,
                });
                function_count += 1;
            }
        }

        self.function_count = function_count;
        Ok(self.items)
    }

    /// Extracts function information from WASM (complexity ≤10)
    fn _extract_wasm_functions(&mut self, _parser: &Parser) -> Result<(), String> {
        // TO BE IMPLEMENTED
        todo!("Extract function information from WASM module")
    }

    /// Extracts import/export information (complexity ≤10)
    fn _extract_imports_exports(&mut self, _parser: &Parser) -> Result<(), String> {
        // TO BE IMPLEMENTED
        todo!("Extract import/export information from WASM")
    }

    /// Calculates WASM-specific complexity metrics (complexity ≤10)
    fn _calculate_wasm_complexity(&self, _function_body: &[u8]) -> Result<(u32, u32), String> {
        // TO BE IMPLEMENTED
        todo!("Calculate cyclomatic and cognitive complexity for WASM function")
    }

    /// Extracts function name from WAT line (complexity ≤10)
    fn extract_wat_function_name(&self, line: &str) -> String {
        if let Some(start) = line.find('$') {
            if let Some(end) = line[start..].find(' ') {
                line[start+1..start+end].to_string()
            } else {
                format!("function_{}", self.function_count)
            }
        } else {
            format!("function_{}", self.function_count)
        }
    }

    /// Gets qualified name for WASM symbol (complexity ≤10)
    fn get_qualified_name(&self, symbol_name: &str) -> String {
        if self.module_name.is_empty() {
            symbol_name.to_string()
        } else {
            format!("{}::{}", self.module_name, symbol_name)
        }
    }
}

/// WASM stack depth analyzer for complexity calculation (complexity ≤10)
pub struct WasmStackAnalyzer {
    max_stack_depth: u32,
    current_depth: u32,
    branch_count: u32,
}

impl Default for WasmStackAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl WasmStackAnalyzer {
    /// Creates a new WASM stack analyzer
    #[must_use] 
    pub fn new() -> Self {
        Self {
            max_stack_depth: 0,
            current_depth: 0,
            branch_count: 0,
        }
    }

    /// Analyzes stack depth complexity (complexity ≤10)
    pub fn analyze_stack_complexity(&mut self, function_body: &[u8]) -> Result<u32, String> {
        self.current_depth = 0;
        self.max_stack_depth = 0;

        for &byte in function_body {
            match byte {
                0x41 => self.current_depth += 1, // i32.const
                0x42 => self.current_depth += 1, // i64.const
                0x6a => {                         // i32.add
                    if self.current_depth >= 2 {
                        self.current_depth -= 1;
                    }
                }
                0x1a => {                         // drop
                    if self.current_depth > 0 {
                        self.current_depth -= 1;
                    }
                }
                _ => {}
            }
            self.max_stack_depth = self.max_stack_depth.max(self.current_depth);
        }

        Ok(self.max_stack_depth)
    }

    /// Analyzes control flow complexity (complexity ≤10)
    pub fn analyze_control_flow_complexity(&mut self, function_body: &[u8]) -> Result<u32, String> {
        self.branch_count = 1; // Base complexity

        for &byte in function_body {
            match byte {
                0x04 => self.branch_count += 1, // if block
                0x05 => self.branch_count += 1, // else
                0x03 => self.branch_count += 1, // loop
                0x02 => self.branch_count += 1, // block
                0x0c => self.branch_count += 1, // br (branch)
                0x0d => self.branch_count += 1, // br_if (conditional branch)
                _ => {}
            }
        }

        Ok(self.branch_count)
    }
}

/// WASM validation and quality checks (complexity ≤10)
pub struct WasmValidator {
    validation_errors: Vec<String>,
    security_warnings: Vec<String>,
}

impl Default for WasmValidator {
    fn default() -> Self {
        Self::new()
    }
}

impl WasmValidator {
    /// Creates a new WASM validator
    #[must_use] 
    pub fn new() -> Self {
        Self {
            validation_errors: Vec::new(),
            security_warnings: Vec::new(),
        }
    }

    /// Validates WASM module for correctness (complexity ≤10)
    pub fn validate_wasm_module(&mut self, wasm_bytes: &[u8]) -> Result<bool, String> {
        if wasm_bytes.len() < 8 {
            self.validation_errors.push("Module too short".to_string());
            return Ok(false);
        }

        if &wasm_bytes[0..4] != b"\0asm" {
            self.validation_errors.push("Invalid magic number".to_string());
            return Ok(false);
        }

        let parser = Parser::new(0);
        for payload in parser.parse_all(wasm_bytes) {
            if payload.is_err() {
                self.validation_errors.push("Parse error in module".to_string());
                return Ok(false);
            }
        }

        Ok(true)
    }

    /// Performs security analysis on WASM module (complexity ≤10)
    pub fn analyze_security(&mut self, wasm_bytes: &[u8]) -> Result<Vec<String>, String> {
        let mut warnings = Vec::new();

        if wasm_bytes.len() > 1024 * 1024 {
            warnings.push("Large WASM module may consume excessive memory".to_string());
        }

        let parser = Parser::new(0);
        for payload in parser.parse_all(wasm_bytes) {
            if let Ok(Payload::ImportSection(_)) = payload {
                warnings.push("Module imports external functions".to_string());
            }
        }

        self.security_warnings = warnings.clone();
        Ok(warnings)
    }

    /// Gets validation errors
    #[must_use] 
    pub fn get_validation_errors(&self) -> &[String] {
        &self.validation_errors
    }

    /// Gets security warnings
    #[must_use] 
    pub fn get_security_warnings(&self) -> &[String] {
        &self.security_warnings
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    // Simple WASM binary that adds two numbers (hand-crafted minimal example)
    const SIMPLE_WASM_BINARY: &[u8] = &[
        0x00, 0x61, 0x73, 0x6d, // WASM magic number
        0x01, 0x00, 0x00, 0x00, // Version
        0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, // Type section
        0x03, 0x02, 0x01, 0x00, // Function section
        0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b // Code section
    ];

    const SIMPLE_WAT_TEXT: &str = r#"
(module
  (func $add (param $x i32) (param $y i32) (result i32)
    local.get $x
    local.get $y
    i32.add
  )
  (export "add" (func $add))
)
"#;

    const COMPLEX_WAT_WITH_CONTROL_FLOW: &str = r#"
(module
  (func $fibonacci (param $n i32) (result i32)
    (if (i32.lt_s (local.get $n) (i32.const 2))
      (then (local.get $n))
      (else
        (i32.add
          (call $fibonacci (i32.sub (local.get $n) (i32.const 1)))
          (call $fibonacci (i32.sub (local.get $n) (i32.const 2)))
        )
      )
    )
  )
  (export "fibonacci" (func $fibonacci))
)
"#;

    #[test]
    fn test_simple_wasm_binary_analysis() {
        let analyzer = WasmModuleAnalyzer::new(Path::new("simple.wasm"));
        let items = analyzer.analyze_wasm_binary(SIMPLE_WASM_BINARY)
            .expect("Should parse simple WASM binary");

        assert!(!items.is_empty(), "Should extract at least one AST item from WASM");

        let function_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Function { .. }))
            .collect();

        assert_eq!(function_items.len(), 1, "Should extract exactly one function");

        if let AstItem::Function { name, visibility, is_async, .. } = &function_items[0] {
            assert!(name.contains("simple"), "Should include module name in function name");
            assert_eq!(visibility, "export", "WASM exported functions have export visibility");
            assert!(!is_async, "WASM functions are not async");
        }
    }

    #[test]
    fn test_wat_text_analysis() {
        let analyzer = WasmModuleAnalyzer::new(Path::new("add.wasm"));
        let items = analyzer.analyze_wat_text(SIMPLE_WAT_TEXT)
            .expect("Should parse WAT text format");

        assert!(!items.is_empty(), "Should extract AST items from WAT text");

        let function_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Function { .. }))
            .collect();

        assert_eq!(function_items.len(), 1, "Should extract the add function");

        if let AstItem::Function { name, .. } = &function_items[0] {
            assert!(name.contains("add"), "Should extract function name from WAT");
        }
    }

    #[test]
    fn test_wasm_complexity_analysis() {
        let mut analyzer = WasmStackAnalyzer::new();
        let stack_complexity = analyzer.analyze_stack_complexity(&[0x20, 0x00, 0x20, 0x01, 0x6a])
            .expect("Should analyze WASM stack complexity");

        assert!(stack_complexity >= 1, "Should have at least complexity of 1");
        assert!(stack_complexity <= 10, "Should maintain complexity ≤10 for simple WASM");

        let control_flow_complexity = analyzer.analyze_control_flow_complexity(&[0x04, 0x40, 0x0b])
            .expect("Should analyze control flow complexity");

        assert!(control_flow_complexity >= 1, "Should have control flow complexity");
    }

    #[test]
    fn test_complex_wat_control_flow() {
        let analyzer = WasmModuleAnalyzer::new(Path::new("fibonacci.wasm"));
        let items = analyzer.analyze_wat_text(COMPLEX_WAT_WITH_CONTROL_FLOW)
            .expect("Should parse complex WAT with control flow");

        let function_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Function { .. }))
            .collect();

        assert_eq!(function_items.len(), 1, "Should extract fibonacci function");

        // Should detect higher complexity due to recursive calls and conditionals
        if let AstItem::Function { name, line, .. } = &function_items[0] {
            assert!(name.contains("fibonacci"), "Should extract fibonacci function name");
            assert!(*line >= 1, "Should have valid line number");
        }
    }

    #[test]
    fn test_wasm_module_validation() {
        let mut validator = WasmValidator::new();
        let is_valid = validator.validate_wasm_module(SIMPLE_WASM_BINARY)
            .expect("Should validate WASM module");

        assert!(is_valid, "Simple WASM binary should be valid");
        assert!(validator.get_validation_errors().is_empty(), "Should have no validation errors");
    }

    #[test]
    fn test_wasm_security_analysis() {
        let mut validator = WasmValidator::new();
        let security_warnings = validator.analyze_security(SIMPLE_WASM_BINARY)
            .expect("Should perform security analysis");

        // Simple add function should have minimal security concerns
        assert!(security_warnings.len() <= 2, "Simple WASM should have few security warnings");
    }

    #[test]
    fn test_invalid_wasm_binary() {
        let analyzer = WasmModuleAnalyzer::new(Path::new("invalid.wasm"));
        let invalid_bytes = &[0xFF, 0xFF, 0xFF, 0xFF]; // Invalid WASM magic
        let result = analyzer.analyze_wasm_binary(invalid_bytes);

        assert!(result.is_err(), "Should return error for invalid WASM binary");
    }

    #[test]
    fn test_empty_wasm_module() {
        let analyzer = WasmModuleAnalyzer::new(Path::new("empty.wasm"));
        let minimal_wasm = &[
            0x00, 0x61, 0x73, 0x6d, // WASM magic
            0x01, 0x00, 0x00, 0x00, // Version
        ];
        let items = analyzer.analyze_wasm_binary(minimal_wasm)
            .expect("Should handle minimal WASM module");

        assert!(items.is_empty(), "Empty WASM module should produce no function items");
    }

    #[test]
    fn test_wasm_import_export_extraction() {
        let analyzer = WasmModuleAnalyzer::new(Path::new("with_imports.wasm"));
        let items = analyzer.analyze_wat_text(r#"
(module
  (import "env" "log" (func $log (param i32)))
  (func $main (result i32)
    i32.const 42
    call $log
    i32.const 0
  )
  (export "main" (func $main))
)
"#).expect("Should parse WASM with imports/exports");

        let function_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Function { .. }))
            .collect();

        // Should extract both imported and local functions
        assert!(function_items.len() >= 1, "Should extract at least the main function");
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;
    use std::path::Path;

    proptest! {
        #[test]
        fn test_wasm_analyzer_handles_various_module_names(
            module_name in "[a-zA-Z_][a-zA-Z0-9_]*"
        ) {
            let file_path = format!("{}.wasm", module_name);
            let analyzer = WasmModuleAnalyzer::new(Path::new(&file_path));

            // Basic property: analyzer should be created successfully
            prop_assert_eq!(analyzer.module_name, module_name);
            prop_assert_eq!(analyzer.function_count, 0);
            prop_assert_eq!(analyzer._import_count, 0);
            prop_assert_eq!(analyzer._export_count, 0);
        }

        #[test]
        fn test_wasm_stack_analyzer_bounds(
            stack_operations in 1usize..20
        ) {
            let mut analyzer = WasmStackAnalyzer::new();

            // Test with varying numbers of stack operations
            let mut operations = Vec::new();
            for _ in 0..stack_operations {
                operations.extend_from_slice(&[0x41, 0x01]); // i32.const 1
            }
            operations.push(0x1a); // drop

            if let Ok(complexity) = analyzer.analyze_stack_complexity(&operations) {
                // Complexity should be proportional to operations but bounded
                prop_assert!(complexity >= 1);
                prop_assert!(complexity <= stack_operations as u32 + 5);
            }
        }

        #[test]
        fn test_wasm_validator_consistency(
            module_size in 8usize..100
        ) {
            let mut validator = WasmValidator::new();

            // Create a minimal valid WASM module of varying sizes
            let mut wasm_bytes = vec![0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
            wasm_bytes.resize(module_size, 0x00);

            // Validation should be consistent - same result on repeated calls
            let result1 = validator.validate_wasm_module(&wasm_bytes);
            let result2 = validator.validate_wasm_module(&wasm_bytes);

            prop_assert_eq!(result1.is_ok(), result2.is_ok());
        }

        #[test]
        fn test_wasm_complexity_scales_reasonably(
            control_flow_depth in 1u32..8
        ) {
            let mut analyzer = WasmStackAnalyzer::new();

            // Create nested control flow (if blocks)
            let mut instructions = Vec::new();
            for _ in 0..control_flow_depth {
                instructions.extend_from_slice(&[0x04, 0x40]); // if block
            }
            for _ in 0..control_flow_depth {
                instructions.push(0x0b); // end
            }

            if let Ok(complexity) = analyzer.analyze_control_flow_complexity(&instructions) {
                // Complexity should scale with nesting depth
                prop_assert!(complexity >= control_flow_depth);
                prop_assert!(complexity <= control_flow_depth * 2 + 3);
            }
        }
    }
}