pmat 3.11.0

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
#![cfg_attr(coverage_nightly, coverage(off))]
//! Deep WASM Service
//!
//! Main service that coordinates all deep WASM inspection components.

use crate::services::deep_wasm::{
    BytecodeAnalyzer, CorrelationEngine, DeepWasmAnalysisRequest, DeepWasmReport, DeepWasmResult,
    Disassembler, DwarfParser, PipelineOverview, SourceLanguage, SourceMapHandler, SourceMetrics,
    WasmInspector, WasmModuleAnalysis, WasmQualityGates,
};
use crate::services::rust_wasm_analyzer;
use std::fs;
use std::path::Path;
use wasmparser;

/// Deep WASM analysis service
pub struct DeepWasmService {
    wasm_inspector: WasmInspector,
    dwarf_parser: DwarfParser,
    source_map_handler: SourceMapHandler,
    correlation_engine: CorrelationEngine,
    quality_gates: WasmQualityGates,
    bytecode_analyzer: BytecodeAnalyzer,
    disassembler: Disassembler,
    enable_deep_analysis: bool,
}

impl DeepWasmService {
    pub fn new() -> Self {
        Self {
            wasm_inspector: WasmInspector::new(),
            dwarf_parser: DwarfParser::new(),
            source_map_handler: SourceMapHandler::new(),
            correlation_engine: CorrelationEngine::new(),
            quality_gates: WasmQualityGates::new(),
            bytecode_analyzer: BytecodeAnalyzer::new(),
            disassembler: Disassembler::new(),
            enable_deep_analysis: true,
        }
    }

    pub fn with_quality_gates(mut self, gates: WasmQualityGates) -> Self {
        self.quality_gates = gates;
        self
    }

    pub fn with_deep_analysis(mut self, enabled: bool) -> Self {
        self.enable_deep_analysis = enabled;
        self.bytecode_analyzer = BytecodeAnalyzer::with_deep_analysis(enabled);
        self.disassembler = Disassembler::with_pattern_detection(enabled);
        self
    }

    pub async fn analyze(
        &self,
        request: DeepWasmAnalysisRequest,
    ) -> DeepWasmResult<DeepWasmReport> {
        // Analyze WASM binary if provided
        let wasm_analysis = if let Some(ref wasm_path) = request.wasm_path {
            self.wasm_inspector.inspect_file(wasm_path)?
        } else {
            WasmModuleAnalysis {
                module_size_bytes: 0,
                function_count: 0,
                exported_functions: 0,
                max_complexity: 0,
                has_dwarf: false,
                has_source_map: false,
            }
        };

        // Enhanced bytecode analysis (Issue #65)
        let (bytecode_analysis, disassembled_functions, suspicious_patterns) =
            if self.enable_deep_analysis && request.wasm_path.is_some() {
                let wasm_bytes = fs::read(request.wasm_path.as_ref().expect("checked is_some"))
                    .map_err(crate::services::deep_wasm::DeepWasmError::Io)?;

                // Perform detailed bytecode analysis
                let bytecode_result = self.bytecode_analyzer.analyze(&wasm_bytes)?;

                // Prepare parser for disassembly
                let mut disassembled = Vec::new();
                let mut all_patterns = Vec::new();

                // Reparse the module to access function bodies
                let parser = wasmparser::Parser::new(0);
                let mut code_section_index = 0;

                for payload in parser.parse_all(&wasm_bytes) {
                    if let Ok(wasmparser::Payload::CodeSectionEntry(function_body)) = payload {
                        // Get function analysis to determine if we should disassemble
                        let func_idx = code_section_index;
                        let func_analysis = bytecode_result
                            .functions
                            .iter()
                            .find(|f| f.function_index as usize == func_idx);

                        code_section_index += 1;

                        // Only disassemble if exported or high complexity
                        if let Some(func_analysis) = func_analysis {
                            if func_analysis.is_exported
                                || func_analysis.complexity.cyclomatic_complexity > 10
                            {
                                // Disassemble the function
                                let result = self.disassembler.disassemble_function(
                                    func_analysis.function_index,
                                    func_analysis.name.clone(),
                                    &function_body,
                                )?;

                                // Detect patterns in this function's instructions
                                let function_patterns =
                                    self.disassembler.detect_patterns(&result.instructions);
                                all_patterns.extend(function_patterns);

                                disassembled.push(result);
                            }
                        }
                    }
                }

                (
                    Some(bytecode_result),
                    Some(disassembled),
                    Some(all_patterns),
                )
            } else {
                (None, None, None)
            };

        // Analyze source code for WASM constructs
        let source_metrics =
            self.analyze_source_code(&request.source_path, request.language.clone())?;

        // Parse DWARF debug information if provided
        let dwarf_entries = if let Some(ref dwarf_path) = request.dwarf_path {
            let dwarf_data =
                fs::read(dwarf_path).map_err(crate::services::deep_wasm::DeepWasmError::Io)?;
            self.dwarf_parser
                .parse_dwarf_sections(&dwarf_data, None, None)?
        } else {
            vec![]
        };

        // Parse source map if provided
        let source_map_entries = if let Some(ref source_map_path) = request.source_map_path {
            self.source_map_handler.parse_source_map(source_map_path)?
        } else {
            vec![]
        };

        // Create source-to-WASM correlations
        let correlations = if !dwarf_entries.is_empty() || !source_map_entries.is_empty() {
            self.correlation_engine
                .correlate(&dwarf_entries, &source_map_entries)?
        } else {
            vec![]
        };

        let quality_results = self.quality_gates.evaluate(&wasm_analysis)?;

        Ok(DeepWasmReport {
            project_name: request
                .source_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown")
                .to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            pmat_version: env!("CARGO_PKG_VERSION").to_string(),
            pipeline_overview: PipelineOverview {
                source_language: request.language,
                source_version: String::new(),
                target: "wasm32-unknown-unknown".to_string(),
                optimization_level: String::new(),
                debug_symbols: Some(if dwarf_entries.is_empty() {
                    "none".to_string()
                } else {
                    format!("{} DWARF entries", dwarf_entries.len())
                }),
            },
            source_metrics,
            wasm_module_analysis: wasm_analysis,
            correlations,
            type_flows: vec![],
            hotspots: vec![],
            quality_gate_results: quality_results,
            bytecode_analysis,
            disassembled_functions,
            suspicious_patterns,
        })
    }

    /// Analyze source code for WASM-specific constructs
    fn analyze_source_code(
        &self,
        source_path: &Path,
        language: SourceLanguage,
    ) -> DeepWasmResult<SourceMetrics> {
        use crate::services::deep_wasm::DeepWasmError;

        // Read source file
        let source_code = fs::read_to_string(source_path).map_err(DeepWasmError::Io)?;

        // Count lines of code
        let lines_of_code = source_code.lines().count();

        match language {
            SourceLanguage::Rust => {
                // Parse as Rust code
                let syntax_tree = syn::parse_file(&source_code).map_err(|e| {
                    DeepWasmError::Analysis(format!("Failed to parse Rust source: {}", e))
                })?;

                // Analyze WASM constructs
                let wasm_analysis = rust_wasm_analyzer::analyze_wasm_constructs(&syntax_tree);

                // Calculate total function count (including non-WASM functions)
                let mut total_functions = 0;
                let mut max_complexity = 0;

                for item in &syntax_tree.items {
                    match item {
                        syn::Item::Fn(_) => total_functions += 1,
                        syn::Item::Impl(impl_block) => {
                            for impl_item in &impl_block.items {
                                if let syn::ImplItem::Fn(_) = impl_item {
                                    total_functions += 1;
                                }
                            }
                        }
                        _ => {}
                    }
                }

                // Estimate max complexity from WASM boundary functions
                // (For Phase 1, this is a simplified estimation)
                for boundary_fn in &wasm_analysis.boundary_functions {
                    let complexity_estimate = if boundary_fn.has_unsafe { 5 } else { 3 };
                    max_complexity = max_complexity.max(complexity_estimate);
                }

                Ok(SourceMetrics {
                    lines_of_code,
                    function_count: total_functions,
                    max_complexity,
                    wasm_boundary_functions: wasm_analysis.boundary_functions.len(),
                })
            }
            SourceLanguage::Ruchy => {
                // For Ruchy, use simplified analysis (Phase 1)
                // Count functions using simple pattern matching
                let mut total_functions = 0;
                let mut max_complexity = 0;

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

                    // Detect function declarations: "fun name(...)" or "async fun name(...)"
                    if (trimmed.starts_with("fun ") || trimmed.starts_with("async fun "))
                        && trimmed.contains('(')
                    {
                        total_functions += 1;

                        // Simple complexity heuristic
                        // Functions with "unsafe", "async", or complex patterns get higher complexity
                        if trimmed.contains("unsafe") || trimmed.contains("async") {
                            max_complexity = max_complexity.max(5);
                        } else {
                            max_complexity = max_complexity.max(3);
                        }
                    }
                }

                Ok(SourceMetrics {
                    lines_of_code,
                    function_count: total_functions,
                    max_complexity,
                    wasm_boundary_functions: 0, // Ruchy Phase 1: no WASM-specific analysis yet
                })
            }
        }
    }
}

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

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::services::deep_wasm::{AnalysisFocus, SourceLanguage};
    use std::path::PathBuf;

    #[test]
    fn test_service_creation() {
        let _service = DeepWasmService::new();
    }

    #[test]
    fn test_service_with_custom_gates() {
        let gates = WasmQualityGates::new();
        let _service = DeepWasmService::new().with_quality_gates(gates);
    }

    #[tokio::test]
    #[ignore] // Five Whys: Process-global CWD modification causes race conditions under parallel execution
              // Root cause: std::env::set_current_dir() is process-wide, not thread-local
              // Fix attempted: RAII CwdGuard failed because current_dir() fails if CWD deleted
              // Decision: Mark as #[ignore] - unsuitable for parallel test execution
              // Run manually: cargo test test_analyze_minimal_request -- --ignored --test-threads=1
    async fn test_analyze_minimal_request() {
        let service = DeepWasmService::new();
        let request = DeepWasmAnalysisRequest {
            source_path: PathBuf::from("tests/fixtures/test.rs"),
            wasm_path: None,
            dwarf_path: None,
            source_map_path: None,
            language: SourceLanguage::Rust,
            analysis_focus: AnalysisFocus::Full,
        };

        let result = service.analyze(request).await;
        assert!(result.is_ok());
        let report = result.unwrap();
        assert!(!report.project_name.is_empty());
    }

    #[tokio::test]
    #[ignore] // Five Whys: Process-global CWD modification causes race conditions under parallel execution
              // Root cause: std::env::set_current_dir() is process-wide, not thread-local
              // Fix attempted: RAII CwdGuard failed because current_dir() fails if CWD deleted
              // Decision: Mark as #[ignore] - unsuitable for parallel test execution
              // Run manually: cargo test test_analyze_ruchy_file -- --ignored --test-threads=1
    async fn test_analyze_ruchy_file() {
        let service = DeepWasmService::new();
        let request = DeepWasmAnalysisRequest {
            source_path: PathBuf::from("tests/fixtures/deep_wasm_ruchy_test.ruchy"),
            wasm_path: None,
            dwarf_path: None,
            source_map_path: None,
            language: SourceLanguage::Ruchy,
            analysis_focus: AnalysisFocus::Source,
        };

        let result = service.analyze(request).await;
        assert!(result.is_ok());
        let report = result.unwrap();

        // Verify report has correct language
        assert_eq!(
            report.pipeline_overview.source_language,
            SourceLanguage::Ruchy
        );

        // Verify source metrics were calculated
        assert!(report.source_metrics.lines_of_code > 0);
        assert_eq!(report.source_metrics.function_count, 3); // fibonacci, factorial, fetch_data
        assert!(report.source_metrics.max_complexity > 0);

        // Phase 1: No WASM boundary analysis for Ruchy yet
        assert_eq!(report.source_metrics.wasm_boundary_functions, 0);
    }

    #[tokio::test]
    #[serial_test::serial]
    #[ignore] // Integration test requiring wasm test file
    async fn test_disassemble_wasm_module() {
        // This test verifies the disassembly functionality works correctly
        let service = DeepWasmService::new().with_deep_analysis(true);
        let request = DeepWasmAnalysisRequest {
            source_path: PathBuf::from("tests/fixtures/test.rs"),
            wasm_path: Some(PathBuf::from("tests/fixtures/test.wasm")),
            dwarf_path: None,
            source_map_path: None,
            language: SourceLanguage::Rust,
            analysis_focus: AnalysisFocus::Full,
        };

        let result = service.analyze(request).await;
        assert!(result.is_ok());

        let report = result.unwrap();

        // Verify disassembly data is present
        assert!(report.bytecode_analysis.is_some());

        // If we have exported functions, we should have disassembled them
        if let Some(bytecode) = &report.bytecode_analysis {
            let exported_count = bytecode.functions.iter().filter(|f| f.is_exported).count();

            if exported_count > 0 {
                assert!(report.disassembled_functions.is_some());
                if let Some(disassembled) = &report.disassembled_functions {
                    // We should have at least one disassembled function
                    assert!(!disassembled.is_empty());

                    // First disassembled function should have instructions
                    assert!(!disassembled[0].instructions.is_empty());

                    // Check for basic blocks
                    assert!(!disassembled[0].basic_blocks.is_empty());
                }
            }

            // If we found patterns, check they're structured correctly
            if let Some(patterns) = &report.suspicious_patterns {
                for pattern in patterns {
                    assert!(!pattern.name.is_empty());
                    assert!(!pattern.description.is_empty());

                    // If suspicious, should have a reason
                    if pattern.suspicious {
                        assert!(pattern.suspicion_reason.is_some());
                    }
                }
            }
        }
    }
}