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
use lazy_static::*;
use std::collections::*;
use std::sync::*;
use std::thread::*;

////////////////////////////////////////////////////////////////////////////////
// DATA STRUCTURES
////////////////////////////////////////////////////////////////////////////////

lazy_static! {
    static ref CONTEXT: Mutex<HashMap<ThreadId, StateContext>> = Mutex::new(HashMap::new());
}

#[derive(Debug, Clone, PartialEq)]
pub enum ExitCondition {
    Goto,
    Error,
    Break,
    Hold,
}

#[derive(Debug, Copy, Clone)]
pub enum ExecutionState {
    Normal,
    Loop,
}

#[derive(Debug)]
pub struct StateContext {
    state: Vec<ExecutionState>,
    rip: usize,
}

////////////////////////////////////////////////////////////////////////////////
// TRAIT FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

impl Default for StateContext {
    fn default() -> Self {
        Self {
            state: Vec::default(),
            rip: 0,
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

impl StateContext {
    pub fn clear_rip() {
        let thread_id = current().id();
        let mut hashmap = CONTEXT.lock().unwrap();

        if let Some(state_context) = hashmap.get_mut(&thread_id) {
            state_context.rip = 0;
        }
    }

    pub fn inc_rip() {
        let thread_id = current().id();
        let mut hashmap = CONTEXT.lock().unwrap();

        hashmap
            .entry(thread_id)
            .or_insert_with(|| StateContext::default());

        if let Some(state_context) = hashmap.get_mut(&thread_id) {
            state_context.rip += 1;
        }
    }

    pub fn get_rip() -> usize {
        let thread_id = current().id();
        let mut hashmap = CONTEXT.lock().unwrap();

        hashmap
            .entry(thread_id)
            .or_insert_with(|| StateContext::default());

        if let Some(state_context) = hashmap.get(&thread_id) {
            state_context.rip
        } else {
            unreachable!();
        }
    }
}

impl StateContext {
    pub fn clear_state() {
        let thread_id = current().id();
        let mut hashmap = CONTEXT.lock().unwrap();

        hashmap
            .entry(thread_id)
            .or_insert_with(|| StateContext::default());

        if let Some(state_context) = hashmap.get_mut(&thread_id) {
            state_context.state.clear();
        }
    }

    pub fn set_state(state: ExecutionState) {
        let thread_id = current().id();
        let mut hashmap = CONTEXT.lock().unwrap();

        hashmap
            .entry(thread_id)
            .or_insert_with(|| StateContext::default());

        if let Some(state_context) = hashmap.get_mut(&thread_id) {
            match state {
                ExecutionState::Loop => {
                    state_context.state.push(state);
                }
                ExecutionState::Normal => {
                    state_context.state.pop();
                }
            }
        }
    }

    pub fn get_state() -> ExecutionState {
        let thread_id = current().id();
        let mut hashmap = CONTEXT.lock().unwrap();

        hashmap
            .entry(thread_id)
            .or_insert_with(|| StateContext::default());

        if let Some(state_context) = hashmap.get(&thread_id) {
            if state_context.state.is_empty() {
                return ExecutionState::Normal;
            }
        }

        ExecutionState::Loop
    }
}