use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WebAssemblyVariant {
AssemblyScript,
Wat,
Wasm,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct WasmMetrics {
pub memory_sections: u32,
pub table_sections: u32,
pub import_count: u32,
pub export_count: u32,
pub function_count: u32,
pub global_count: u32,
pub linear_memory_pages: u32,
pub indirect_calls: u32,
pub memory_operations: MemoryOpStats,
pub instruction_histogram: HashMap<WasmOpcode, u32>,
pub custom_sections: u32,
pub element_segments: u32,
pub data_segments: u32,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct MemoryOpStats {
pub loads: u32,
pub stores: u32,
pub grows: u32,
pub atomic_ops: u32,
pub simd_ops: u32,
pub bulk_ops: u32,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct WasmComplexity {
pub cyclomatic: u32,
pub memory_pressure: f32,
pub indirect_call_overhead: f32,
pub estimated_gas: f64,
pub cognitive: u32,
pub hot_path_score: f32,
pub max_loop_depth: u32,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct MemoryAnalysis {
pub peak_usage_bytes: u64,
pub allocation_patterns: Vec<AllocationPattern>,
pub leak_risk_score: f32,
pub optimization_hints: Vec<MemoryOptimizationHint>,
pub max_stack_depth: u32,
pub alignment_issues: Vec<AlignmentIssue>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllocationPattern {
pub pattern_type: String,
pub location: SourceLocation,
pub severity: Severity,
pub description: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryOptimizationHint {
pub hint_type: OptimizationType,
pub expected_improvement: f32,
pub difficulty: Difficulty,
pub suggestion: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlignmentIssue {
pub offset: u32,
pub required_alignment: u32,
pub actual_alignment: u32,
pub performance_impact: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceLocation {
pub file: String,
pub line: u32,
pub column: u32,
pub offset: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Severity {
Low,
Medium,
High,
Critical,
}
impl std::fmt::Display for Severity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Severity::Low => write!(f, "Low"),
Severity::Medium => write!(f, "Medium"),
Severity::High => write!(f, "High"),
Severity::Critical => write!(f, "Critical"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Difficulty {
Easy,
Medium,
Hard,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationType {
ReduceAllocations,
ImproveAlignment,
UseStackMemory,
PoolAllocations,
CompactDataStructures,
EliminateLeaks,
ReduceFragmentation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum WasmOpcode {
Unreachable = 0x00,
Nop = 0x01,
Block = 0x02,
Loop = 0x03,
If = 0x04,
Else = 0x05,
End = 0x0B,
Br = 0x0C,
BrIf = 0x0D,
BrTable = 0x0E,
Return = 0x0F,
Call = 0x10,
CallIndirect = 0x11,
I32Load = 0x28,
I64Load = 0x29,
F32Load = 0x2A,
F64Load = 0x2B,
I32Store = 0x36,
I64Store = 0x37,
F32Store = 0x38,
F64Store = 0x39,
MemorySize = 0x3F,
MemoryGrow = 0x40,
I32Const = 0x41,
I64Const = 0x42,
F32Const = 0x43,
F64Const = 0x44,
LocalGet = 0x20,
LocalSet = 0x21,
LocalTee = 0x22,
GlobalGet = 0x23,
GlobalSet = 0x24,
Other(u8),
}
impl From<u8> for WasmOpcode {
fn from(byte: u8) -> Self {
match byte {
0x00 => Self::Unreachable,
0x01 => Self::Nop,
0x02 => Self::Block,
0x03 => Self::Loop,
0x04 => Self::If,
0x05 => Self::Else,
0x0B => Self::End,
0x0C => Self::Br,
0x0D => Self::BrIf,
0x0E => Self::BrTable,
0x0F => Self::Return,
0x10 => Self::Call,
0x11 => Self::CallIndirect,
0x28 => Self::I32Load,
0x29 => Self::I64Load,
0x2A => Self::F32Load,
0x2B => Self::F64Load,
0x36 => Self::I32Store,
0x37 => Self::I64Store,
0x38 => Self::F32Store,
0x39 => Self::F64Store,
0x3F => Self::MemorySize,
0x40 => Self::MemoryGrow,
0x41 => Self::I32Const,
0x42 => Self::I64Const,
0x43 => Self::F32Const,
0x44 => Self::F64Const,
0x20 => Self::LocalGet,
0x21 => Self::LocalSet,
0x22 => Self::LocalTee,
0x23 => Self::GlobalGet,
0x24 => Self::GlobalSet,
other => Self::Other(other),
}
}
}
#[cfg(test)]
mod property_tests {
use proptest::prelude::*;
proptest! {
#[test]
fn basic_property_stability(_input in ".*") {
prop_assert!(true);
}
#[test]
fn module_consistency_check(_x in 0u32..1000) {
prop_assert!(_x < 1001);
}
}
}