revm_bytecode/eof/
types_section.rs

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
use super::{
    decode_helpers::{consume_u16, consume_u8},
    EofDecodeError,
};
use std::vec::Vec;

/// Non returning function has a output 0x80.
const EOF_NON_RETURNING_FUNCTION: u8 = 0x80;

/// Types section that contains stack information for matching code section.
#[derive(Debug, Clone, Default, Hash, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TypesSection {
    /// inputs - 1 byte - `0x00-0x7F`
    /// number of stack elements the code section consumes
    pub inputs: u8,
    /// outputs - 1 byte - `0x00-0x80`
    /// number of stack elements the code section returns or 0x80 for non-returning functions
    pub outputs: u8,
    /// max_stack_height - 2 bytes - `0x0000-0x03FF`
    /// maximum number of elements ever placed onto the stack by the code section
    pub max_stack_size: u16,
}

impl TypesSection {
    /// Returns new `TypesSection` with the given inputs, outputs, and max_stack_size.
    pub fn new(inputs: u8, outputs: u8, max_stack_size: u16) -> Self {
        Self {
            inputs,
            outputs,
            max_stack_size,
        }
    }

    /// True if section is non-returning.
    pub fn is_non_returning(&self) -> bool {
        self.outputs == EOF_NON_RETURNING_FUNCTION
    }

    /// Calculates the difference between the number of input and output stack elements.
    #[inline]
    pub const fn io_diff(&self) -> i32 {
        self.outputs as i32 - self.inputs as i32
    }

    /// Encode the section into the buffer.
    #[inline]
    pub fn encode(&self, buffer: &mut Vec<u8>) {
        buffer.push(self.inputs);
        buffer.push(self.outputs);
        buffer.extend_from_slice(&self.max_stack_size.to_be_bytes());
    }

    /// Decode the section from the input.
    #[inline]
    pub fn decode(input: &[u8]) -> Result<(Self, &[u8]), EofDecodeError> {
        let (input, inputs) = consume_u8(input)?;
        let (input, outputs) = consume_u8(input)?;
        let (input, max_stack_size) = consume_u16(input)?;
        let section = Self {
            inputs,
            outputs,
            max_stack_size,
        };
        section.validate()?;
        Ok((section, input))
    }

    /// Validate the section.
    pub fn validate(&self) -> Result<(), EofDecodeError> {
        if self.inputs > 0x7f || self.outputs > 0x80 || self.max_stack_size > 0x03FF {
            return Err(EofDecodeError::InvalidTypesSection);
        }
        if self.inputs as u16 > self.max_stack_size {
            return Err(EofDecodeError::InvalidTypesSection);
        }
        Ok(())
    }
}