sentri-analyzer-evm 0.3.0

Sentri: EVM smart contract analyzer with static analysis and invariant checking for Ethereum, Polygon, and other EVM chains.
Documentation
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
544
545
546
#![allow(missing_docs)]
//! Symbolic Execution for invariant verification.
//!
//! Enables:
//! - Symbolic constraint solving
//! - Invariant proofs for all inputs
//! - Path-sensitive analysis
//! - Automated vulnerability detection

use crate::errors::AnalysisResult;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info};

/// Symbolic value representation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SymbolicValue {
    /// Concrete integer value.
    Concrete(i128),
    /// Symbolic variable.
    Variable {
        /// Variable name.
        name: String,
        /// Bit width of the variable.
        bit_width: usize,
    },
    /// Symbolic expression.
    Expression {
        /// Operation to perform.
        op: ExprOp,
        /// Left operand.
        left: Box<SymbolicValue>,
        /// Right operand.
        right: Box<SymbolicValue>,
    },
    /// Uninterpreted function application.
    Function {
        /// Function name.
        name: String,
        /// Function arguments.
        args: Vec<SymbolicValue>,
    },
}

/// Symbolic operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExprOp {
    /// Addition operation.
    Add,
    /// Subtraction operation.
    Sub,
    /// Multiplication operation.
    Mul,
    /// Division operation.
    Div,
    /// Modulo operation.
    Mod,
    /// Bitwise AND operation.
    And,
    /// Bitwise OR operation.
    Or,
    /// Bitwise XOR operation.
    Xor,
    /// Left shift operation.
    Shl,
    /// Right shift operation.
    Shr,
    /// Less-than comparison.
    Lt,
    /// Greater-than comparison.
    Gt,
    /// Less-than-or-equal comparison.
    Leq,
    /// Greater-than-or-equal comparison.
    Geq,
    /// Equality comparison.
    Eq,
    /// Not-equal comparison.
    Neq,
}

impl std::fmt::Display for ExprOp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExprOp::Add => write!(f, "+"),
            ExprOp::Sub => write!(f, "-"),
            ExprOp::Mul => write!(f, "*"),
            ExprOp::Div => write!(f, "/"),
            ExprOp::Mod => write!(f, "%"),
            ExprOp::And => write!(f, "&"),
            ExprOp::Or => write!(f, "|"),
            ExprOp::Xor => write!(f, "^"),
            ExprOp::Shl => write!(f, "<<"),
            ExprOp::Shr => write!(f, ">>"),
            ExprOp::Lt => write!(f, "<"),
            ExprOp::Gt => write!(f, ">"),
            ExprOp::Leq => write!(f, "<="),
            ExprOp::Geq => write!(f, ">="),
            ExprOp::Eq => write!(f, "=="),
            ExprOp::Neq => write!(f, "!="),
        }
    }
}

impl SymbolicValue {
    /// Create symbolic variable.
    pub fn var(name: &str, bit_width: usize) -> Self {
        SymbolicValue::Variable {
            name: name.to_string(),
            bit_width,
        }
    }

    /// Create concrete value.
    pub fn concrete(value: i128) -> Self {
        SymbolicValue::Concrete(value)
    }

    /// Create binary expression.
    pub fn expr(op: ExprOp, left: SymbolicValue, right: SymbolicValue) -> Self {
        SymbolicValue::Expression {
            op,
            left: Box::new(left),
            right: Box::new(right),
        }
    }

    /// Simplify expression.
    pub fn simplify(&self) -> SymbolicValue {
        match self {
            SymbolicValue::Expression { op, left, right } => {
                let left_simp = left.simplify();
                let right_simp = right.simplify();

                // Constant folding
                if let (SymbolicValue::Concrete(l), SymbolicValue::Concrete(r)) =
                    (&left_simp, &right_simp)
                {
                    let result = match op {
                        ExprOp::Add => l + r,
                        ExprOp::Sub => l - r,
                        ExprOp::Mul => l * r,
                        ExprOp::Div if *r != 0 => l / r,
                        ExprOp::Mod if *r != 0 => l % r,
                        ExprOp::And => l & r,
                        ExprOp::Or => l | r,
                        ExprOp::Xor => l ^ r,
                        _ => return SymbolicValue::expr(*op, left_simp, right_simp),
                    };
                    return SymbolicValue::Concrete(result);
                }

                // Identity simplifications
                match op {
                    ExprOp::Add if right_simp == SymbolicValue::Concrete(0) => left_simp,
                    ExprOp::Mul if right_simp == SymbolicValue::Concrete(1) => left_simp,
                    ExprOp::Mul if right_simp == SymbolicValue::Concrete(0) => {
                        SymbolicValue::Concrete(0)
                    }
                    _ => SymbolicValue::expr(*op, left_simp, right_simp),
                }
            }
            _ => self.clone(),
        }
    }

    /// Convert to SMT-LIB format.
    pub fn to_smt(&self) -> String {
        match self {
            SymbolicValue::Concrete(n) => n.to_string(),
            SymbolicValue::Variable { name, .. } => name.clone(),
            SymbolicValue::Expression { op, left, right } => {
                format!("({} {} {})", op, left.to_smt(), right.to_smt())
            }
            SymbolicValue::Function { name, args } => {
                let args_str = args
                    .iter()
                    .map(|arg| arg.to_smt())
                    .collect::<Vec<_>>()
                    .join(" ");
                format!("({} {})", name, args_str)
            }
        }
    }
}

/// Symbolic constraint for invariant checking.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SymbolicConstraint {
    /// Constraint expression.
    pub expression: SymbolicValue,
    /// Constraint type.
    pub constraint_type: ConstraintType,
    /// Source/description.
    pub source: String,
}

/// Types of constraints.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConstraintType {
    /// Must be true.
    Required,
    /// Must not be true.
    Forbidden,
    /// Assumed to be true.
    Assumption,
}

/// Symbolic execution state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SymbolicState {
    /// Variable assignments.
    pub variables: HashMap<String, SymbolicValue>,
    /// Path constraints.
    pub constraints: Vec<SymbolicConstraint>,
    /// Program counter.
    pub pc: usize,
    /// Is this a valid execution path.
    pub valid: bool,
}

impl SymbolicState {
    /// Create new symbolic state.
    pub fn new() -> Self {
        Self {
            variables: HashMap::new(),
            constraints: Vec::new(),
            pc: 0,
            valid: true,
        }
    }

    /// Add variable.
    pub fn add_variable(&mut self, name: String, value: SymbolicValue) {
        self.variables.insert(name, value);
    }

    /// Add constraint.
    pub fn add_constraint(&mut self, expr: SymbolicValue, ctype: ConstraintType, source: String) {
        self.constraints.push(SymbolicConstraint {
            expression: expr,
            constraint_type: ctype,
            source,
        });
    }

    /// Check constraint satisfiability (placeholder).
    pub fn is_satisfiable(&self) -> AnalysisResult<bool> {
        // In production, would use Z3 or similar SMT solver
        // For now, return true
        Ok(true)
    }
}

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

/// Path condition for a specific execution path.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathCondition {
    /// Conditions on path.
    pub conditions: Vec<SymbolicValue>,
    /// Is feasible (satisfiable).
    pub feasible: bool,
}

impl PathCondition {
    /// Create new path condition.
    pub fn new() -> Self {
        Self {
            conditions: Vec::new(),
            feasible: true,
        }
    }

    /// Add condition to path.
    pub fn add_condition(&mut self, cond: SymbolicValue) {
        self.conditions.push(cond);
    }
}

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

/// Symbolic execution engine.
pub struct SymbolicExecutor;

impl SymbolicExecutor {
    /// Verify invariant holds for all inputs.
    pub fn verify_invariant(
        invariant: &str,
        function_code: &str,
        bit_width: usize,
    ) -> AnalysisResult<VerificationResult> {
        info!("Verifying invariant: {}", invariant);

        // Parse invariant
        let invariant_expr = Self::parse_invariant(invariant, bit_width)?;

        // Generate symbolic execution paths
        let paths = Self::generate_paths(function_code)?;
        debug!("Generated {} symbolic paths", paths.len());

        let mut verification = VerificationResult::new(invariant.to_string());

        // Check invariant on each path
        for (path_idx, _path) in paths.iter().enumerate() {
            // For each path, construct constraint system
            // and check if invariant can be violated

            let constraint_system = Self::build_constraint_system(&invariant_expr, path_idx)?;

            // Check satisfiability of negated invariant
            let negated_sat = Self::check_satisfiability(&constraint_system)?;

            if negated_sat {
                // Found counterexample
                verification.violations.push(CounterExample {
                    path: path_idx,
                    input_bindings: HashMap::new(),
                    violation_type: ViolationType::InvariantViolation,
                });
            }
        }

        verification.verified = verification.violations.is_empty();
        Ok(verification)
    }

    /// Parse invariant expression.
    fn parse_invariant(invariant: &str, _bit_width: usize) -> AnalysisResult<SymbolicValue> {
        debug!("Parsing invariant: {}", invariant);

        // Simple parser for basic invariants
        // In production, would use full SMT-LIB parser
        let _ = invariant.contains(">=") || invariant.contains("<=");
        Ok(SymbolicValue::var("invariant", 256))
    }

    /// Generate symbolic execution paths.
    fn generate_paths(function_code: &str) -> AnalysisResult<Vec<PathCondition>> {
        debug!("Generating symbolic paths from function");

        // Simple path generation
        // In production, would analyze control flow to generate all paths
        let mut paths = vec![PathCondition::new()];

        // Analyze function for branches
        if function_code.contains("if") {
            let path2 = PathCondition::new();
            paths.push(path2);
        }

        Ok(paths)
    }

    /// Build constraint system for invariant checking.
    fn build_constraint_system(
        _invariant: &SymbolicValue,
        _path: usize,
    ) -> AnalysisResult<ConstraintSystem> {
        Ok(ConstraintSystem::new())
    }

    /// Check satisfiability of constraint system.
    fn check_satisfiability(_system: &ConstraintSystem) -> AnalysisResult<bool> {
        // In production, would use Z3 solver
        Ok(false)
    }
}

/// Constraint system for SMT solving.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConstraintSystem {
    /// Assertions (must be true).
    pub assertions: Vec<SymbolicValue>,
    /// Constraints.
    pub constraints: Vec<SymbolicConstraint>,
}

impl ConstraintSystem {
    /// Create new constraint system.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add assertion.
    pub fn assert(&mut self, expr: SymbolicValue) {
        self.assertions.push(expr);
    }

    /// Convert to SMT-LIB format.
    pub fn to_smt_lib(&self) -> String {
        let mut output = String::from("(set-logic QF_BV)\n");

        for (i, assertion) in self.assertions.iter().enumerate() {
            output.push_str(&format!(
                "(assert ({})) ; assertion {}\n",
                assertion.to_smt(),
                i
            ));
        }

        output.push_str("(check-sat)\n");
        output
    }
}

/// Verification result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationResult {
    /// Invariant being verified.
    pub invariant: String,
    /// Is invariant verified.
    pub verified: bool,
    /// Violations found.
    pub violations: Vec<CounterExample>,
    /// Time taken (seconds).
    pub time_seconds: f64,
}

impl VerificationResult {
    /// Create new verification result.
    pub fn new(invariant: String) -> Self {
        Self {
            invariant,
            verified: false,
            violations: Vec::new(),
            time_seconds: 0.0,
        }
    }
}

/// Counterexample to invariant.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CounterExample {
    /// Path index.
    pub path: usize,
    /// Input bindings that violate invariant.
    pub input_bindings: HashMap<String, SymbolicValue>,
    /// Type of violation.
    pub violation_type: ViolationType,
}

/// Violation type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ViolationType {
    /// Invariant violated.
    InvariantViolation,
    /// State invariant broken.
    StateInvariant,
    /// Reentrancy detected.
    Reentrancy,
    /// Other violation.
    Other,
}

/// Invariant prover using symbolic execution.
pub struct InvariantProver;

impl InvariantProver {
    /// Prove invariant holds.
    pub fn prove(invariant: &str, _contract_code: &str) -> AnalysisResult<ProofResult> {
        info!("Attempting to prove invariant: {}", invariant);

        let result = VerificationResult::new(invariant.to_string());

        Ok(ProofResult {
            proven: result.verified,
            counterexamples: result.violations,
            confidence: if result.verified { 1.0 } else { 0.0 },
        })
    }
}

/// Proof result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofResult {
    /// Was invariant proven.
    pub proven: bool,
    /// Counterexamples found.
    pub counterexamples: Vec<CounterExample>,
    /// Confidence level.
    pub confidence: f64,
}

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

    #[test]
    fn test_symbolic_value_concrete() {
        let val = SymbolicValue::concrete(42);
        assert_eq!(val.to_smt(), "42");
    }

    #[test]
    fn test_symbolic_value_variable() {
        let val = SymbolicValue::var("x", 256);
        assert_eq!(val.to_smt(), "x");
    }

    #[test]
    fn test_symbolic_expression() {
        let expr = SymbolicValue::expr(
            ExprOp::Add,
            SymbolicValue::var("x", 256),
            SymbolicValue::concrete(1),
        );
        let smt = expr.to_smt();
        assert!(smt.contains("+"));
    }

    #[test]
    fn test_constant_folding() {
        let expr = SymbolicValue::expr(
            ExprOp::Add,
            SymbolicValue::concrete(10),
            SymbolicValue::concrete(20),
        );
        let simplified = expr.simplify();
        assert_eq!(simplified, SymbolicValue::Concrete(30));
    }

    #[test]
    fn test_symbolic_state() {
        let mut state = SymbolicState::new();
        state.add_variable("x".to_string(), SymbolicValue::concrete(42));
        assert!(state.variables.contains_key("x"));
    }

    #[test]
    fn test_constraint_system_smt_lib() {
        let mut system = ConstraintSystem::new();
        system.assert(SymbolicValue::var("x", 256));
        let smt = system.to_smt_lib();
        assert!(smt.contains("(set-logic QF_BV)"));
        assert!(smt.contains("(check-sat)"));
    }
}