revm_interpreter/instructions/
data.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
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
use crate::{
    gas::{cost_per_word, BASE, DATA_LOAD_GAS, VERYLOW},
    instructions::utility::read_u16,
    interpreter::Interpreter,
    Host,
};
use primitives::U256;

pub fn data_load<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
    require_eof!(interpreter);
    gas!(interpreter, DATA_LOAD_GAS);
    pop_top!(interpreter, offset);

    let offset_usize = as_usize_saturated!(offset);

    let slice = interpreter
        .contract
        .bytecode
        .eof()
        .expect("eof")
        .data_slice(offset_usize, 32);

    let mut word = [0u8; 32];
    word[..slice.len()].copy_from_slice(slice);

    *offset = U256::from_be_bytes(word);
}

pub fn data_loadn<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
    require_eof!(interpreter);
    gas!(interpreter, VERYLOW);
    let offset = unsafe { read_u16(interpreter.instruction_pointer) } as usize;

    let slice = interpreter
        .contract
        .bytecode
        .eof()
        .expect("eof")
        .data_slice(offset, 32);

    let mut word = [0u8; 32];
    word[..slice.len()].copy_from_slice(slice);

    push_b256!(interpreter, word.into());

    // add +2 to the instruction pointer to skip the offset
    interpreter.instruction_pointer = unsafe { interpreter.instruction_pointer.offset(2) };
}

pub fn data_size<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
    require_eof!(interpreter);
    gas!(interpreter, BASE);
    let data_size = interpreter.eof().expect("eof").header.data_size;

    push!(interpreter, U256::from(data_size));
}

pub fn data_copy<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
    require_eof!(interpreter);
    gas!(interpreter, VERYLOW);
    pop!(interpreter, mem_offset, offset, size);

    // sizes more than u64::MAX will spend all the gas in memory resize.
    let size = as_usize_or_fail!(interpreter, size);
    // size of zero should not change the memory
    if size == 0 {
        return;
    }
    // fail if mem offset is big as it will spend all the gas
    let mem_offset = as_usize_or_fail!(interpreter, mem_offset);
    resize_memory!(interpreter, mem_offset, size);

    gas_or_fail!(interpreter, cost_per_word(size as u64, VERYLOW));

    let offset = as_usize_saturated!(offset);
    let data = interpreter.contract.bytecode.eof().expect("eof").data();

    // set data from the eof to the shared memory. Padded it with zeros.
    interpreter
        .shared_memory
        .set_data(mem_offset, offset, size, data);
}

#[cfg(test)]
mod test {
    use bytecode::{Bytecode, Eof};
    use primitives::{b256, bytes, Bytes};
    use specification::hardfork::PragueSpec;
    use std::sync::Arc;
    use wiring::DefaultEthereumWiring;

    use super::*;
    use crate::{table::make_instruction_table, DummyHost, Gas, Interpreter};
    use bytecode::opcode::{DATACOPY, DATALOAD, DATALOADN, DATASIZE};

    fn dummy_eof(code_bytes: Bytes) -> Bytecode {
        let bytes = bytes!("ef000101000402000100010400000000800000fe");
        let mut eof = Eof::decode(bytes).unwrap();

        eof.body.data_section =
            bytes!("000000000000000000000000000000000000000000000000000000000000000102030405");
        eof.header.data_size = eof.body.data_section.len() as u16;

        eof.header.code_sizes[0] = code_bytes.len() as u16;
        eof.body.code_section[0] = code_bytes;
        Bytecode::Eof(Arc::new(eof))
    }

    #[test]
    fn dataload_dataloadn() {
        let table = make_instruction_table::<DummyHost<DefaultEthereumWiring>, PragueSpec>();
        let mut host = DummyHost::default();
        let eof = dummy_eof(Bytes::from([
            DATALOAD, DATALOADN, 0x00, 0x00, DATALOAD, DATALOADN, 0x00, 35, DATALOAD, DATALOADN,
            0x00, 36, DATASIZE,
        ]));

        let mut interp = Interpreter::new_bytecode(eof);
        interp.gas = Gas::new(10000);

        // DATALOAD
        interp.stack.push(U256::from(0)).unwrap();
        interp.step(&table, &mut host);
        assert_eq!(interp.stack.data(), &vec![U256::from(0x01)]);
        interp.stack.pop().unwrap();

        // DATALOADN
        interp.step(&table, &mut host);
        assert_eq!(interp.stack.data(), &vec![U256::from(0x01)]);
        interp.stack.pop().unwrap();

        // DATALOAD (padding)
        interp.stack.push(U256::from(35)).unwrap();
        interp.step(&table, &mut host);
        assert_eq!(
            interp.stack.data(),
            &vec![b256!("0500000000000000000000000000000000000000000000000000000000000000").into()]
        );
        interp.stack.pop().unwrap();

        // DATALOADN (padding)
        interp.step(&table, &mut host);
        assert_eq!(
            interp.stack.data(),
            &vec![b256!("0500000000000000000000000000000000000000000000000000000000000000").into()]
        );
        interp.stack.pop().unwrap();

        // DATALOAD (out of bounds)
        interp.stack.push(U256::from(36)).unwrap();
        interp.step(&table, &mut host);
        assert_eq!(interp.stack.data(), &vec![U256::ZERO]);
        interp.stack.pop().unwrap();

        // DATALOADN (out of bounds)
        interp.step(&table, &mut host);
        assert_eq!(interp.stack.data(), &vec![U256::ZERO]);
        interp.stack.pop().unwrap();

        // DATA SIZE
        interp.step(&table, &mut host);
        assert_eq!(interp.stack.data(), &vec![U256::from(36)]);
    }

    #[test]
    fn data_copy() {
        let table = make_instruction_table::<DummyHost<DefaultEthereumWiring>, PragueSpec>();
        let mut host = DummyHost::default();
        let eof = dummy_eof(Bytes::from([DATACOPY, DATACOPY, DATACOPY, DATACOPY]));

        let mut interp = Interpreter::new_bytecode(eof);
        interp.gas = Gas::new(10000);

        // Data copy
        // size, offset mem_offset,
        interp.stack.push(U256::from(32)).unwrap();
        interp.stack.push(U256::from(0)).unwrap();
        interp.stack.push(U256::from(0)).unwrap();
        interp.step(&table, &mut host);
        assert_eq!(
            interp.shared_memory.context_memory(),
            &bytes!("0000000000000000000000000000000000000000000000000000000000000001")
        );

        // Data copy (Padding)
        // size, offset mem_offset,
        interp.stack.push(U256::from(2)).unwrap();
        interp.stack.push(U256::from(35)).unwrap();
        interp.stack.push(U256::from(1)).unwrap();
        interp.step(&table, &mut host);
        assert_eq!(
            interp.shared_memory.context_memory(),
            &bytes!("0005000000000000000000000000000000000000000000000000000000000001")
        );

        // Data copy (Out of bounds)
        // size, offset mem_offset,
        interp.stack.push(U256::from(2)).unwrap();
        interp.stack.push(U256::from(37)).unwrap();
        interp.stack.push(U256::from(1)).unwrap();
        interp.step(&table, &mut host);
        assert_eq!(
            interp.shared_memory.context_memory(),
            &bytes!("0000000000000000000000000000000000000000000000000000000000000001")
        );

        // Data copy (Size == 0)
        // mem_offset, offset, size
        interp.stack.push(U256::from(0)).unwrap();
        interp.stack.push(U256::from(37)).unwrap();
        interp.stack.push(U256::from(1)).unwrap();
        interp.step(&table, &mut host);
        assert_eq!(
            interp.shared_memory.context_memory(),
            &bytes!("0000000000000000000000000000000000000000000000000000000000000001")
        );
    }
}