fuel_vm/
predicate.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
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
//! Predicate representations with required data to be executed during VM runtime

use fuel_tx::field;

use crate::interpreter::MemoryRange;

/// Runtime representation of a predicate
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RuntimePredicate {
    range: MemoryRange,
    idx: usize,
}

impl RuntimePredicate {
    /// Empty predicate for testing
    #[cfg(test)]
    pub const fn empty() -> Self {
        Self {
            range: MemoryRange::new(0, 0),
            idx: 0,
        }
    }

    /// Memory slice with the program representation of the predicate
    pub const fn program(&self) -> &MemoryRange {
        &self.range
    }

    /// Index of the transaction input that maps to this predicate
    pub const fn idx(&self) -> usize {
        self.idx
    }

    /// Create a new runtime predicate from a transaction, given the input index
    ///
    /// Return `None` if the tx input doesn't map to an input with a predicate
    pub fn from_tx<T>(tx: &T, tx_offset: usize, idx: usize) -> Option<Self>
    where
        T: field::Inputs,
    {
        let (ofs, len) = tx.inputs_predicate_offset_at(idx)?;
        let addr = ofs.saturating_add(tx_offset);
        Some(Self {
            range: MemoryRange::new(addr, len),
            idx,
        })
    }
}

#[allow(clippy::cast_possible_truncation)]
#[cfg(test)]
mod tests {
    use alloc::{
        vec,
        vec::Vec,
    };
    use core::iter;
    use fuel_asm::op;
    use fuel_tx::{
        field::ScriptGasLimit,
        TransactionBuilder,
    };
    use fuel_types::bytes;
    use rand::{
        rngs::StdRng,
        Rng,
        SeedableRng,
    };

    use crate::{
        checked_transaction::{
            CheckPredicateParams,
            EstimatePredicates,
        },
        constraints::reg_key::{
            HP,
            IS,
            ONE,
            SSP,
            ZERO,
        },
        error::PredicateVerificationFailed,
        interpreter::InterpreterParams,
        prelude::{
            predicates::check_predicates,
            *,
        },
        storage::{
            predicate::empty_predicate_storage,
            BlobData,
        },
    };

    #[test]
    fn from_tx_works() {
        let rng = &mut StdRng::seed_from_u64(2322u64);

        let height = 1.into();

        #[rustfmt::skip]
        let predicate: Vec<u8> = vec![
            op::addi(0x10, 0x00, 0x01),
            op::addi(0x10, 0x10, 0x01),
            op::ret(0x01),
        ].into_iter().collect();

        let predicate_data = b"If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.".to_vec();

        let owner = (*Contract::root_from_code(&predicate)).into();
        let a = Input::coin_predicate(
            rng.gen(),
            owner,
            rng.gen(),
            rng.gen(),
            rng.gen(),
            0,
            predicate.clone(),
            predicate_data.clone(),
        );

        let b = Input::message_coin_predicate(
            rng.gen(),
            rng.gen(),
            rng.gen(),
            rng.gen(),
            0,
            predicate.clone(),
            predicate_data.clone(),
        );

        let c = Input::message_data_predicate(
            rng.gen(),
            rng.gen(),
            rng.gen(),
            rng.gen(),
            0,
            vec![0xff; 10],
            predicate.clone(),
            predicate_data,
        );

        let inputs = vec![a, b, c];

        for i in inputs {
            let tx = TransactionBuilder::script(vec![], vec![])
                .add_input(i)
                .add_fee_input()
                .finalize_checked_basic(height);

            // assert invalid idx wont panic
            let idx = 1;
            let tx_offset = TxParameters::DEFAULT.tx_offset();
            let runtime = RuntimePredicate::from_tx(tx.as_ref(), tx_offset, idx);

            assert!(runtime.is_none());

            // fetch the input predicate
            let idx = 0;
            let runtime = RuntimePredicate::from_tx(tx.as_ref(), tx_offset, idx)
                .expect("failed to generate predicate from valid tx");

            assert_eq!(idx, runtime.idx());

            let mut interpreter = Interpreter::<_, _, _>::with_storage(
                MemoryInstance::new(),
                empty_predicate_storage(),
                InterpreterParams::default(),
            );

            assert!(interpreter
                .init_predicate(
                    Context::PredicateVerification {
                        program: RuntimePredicate::empty(),
                    },
                    tx.transaction().clone(),
                    *tx.transaction().script_gas_limit(),
                )
                .is_ok());

            let pad = bytes::padded_len(&predicate).unwrap() - predicate.len();

            // assert we are testing an edge case
            assert_ne!(0, pad);

            let padded_predicate: Vec<u8> = predicate
                .iter()
                .copied()
                .chain(iter::repeat(0u8).take(pad))
                .collect();

            let program = runtime.program();
            let program = &interpreter.memory()[program.usizes()];

            // assert the program in the vm memory is the same of the input
            assert_eq!(program, &padded_predicate);
        }
    }

    fn assert_inputs_are_validated_for_predicates(
        inputs: Vec<(
            Vec<Instruction>,
            bool,
            Result<(), PredicateVerificationFailed>,
        )>,
        blob: Vec<Instruction>,
    ) {
        let rng = &mut StdRng::seed_from_u64(2322u64);

        let height = 1.into();
        let predicate_data =
            b"If you think it's simple, then you have misunderstood the problem."
                .to_vec();

        let mut storage = MemoryStorage::new(Default::default(), Default::default());

        let blob_id = BlobId::zeroed();
        let blob: Vec<u8> = blob.into_iter().collect();
        storage
            .storage_as_mut::<BlobData>()
            .insert(&blob_id, &blob)
            .unwrap();

        macro_rules! predicate_input {
            ($predicate:expr) => {{
                let predicate: Vec<u8> = $predicate.into_iter().collect();
                let owner = Input::predicate_owner(&predicate);
                [
                    Input::coin_predicate(
                        rng.gen(),
                        owner,
                        rng.gen(),
                        rng.gen(),
                        rng.gen(),
                        0,
                        predicate.clone(),
                        predicate_data.clone(),
                    ),
                    Input::message_coin_predicate(
                        rng.gen(),
                        owner,
                        rng.gen(),
                        rng.gen(),
                        0,
                        predicate.clone(),
                        predicate_data.clone(),
                    ),
                    Input::message_data_predicate(
                        rng.gen(),
                        owner,
                        rng.gen(),
                        rng.gen(),
                        0,
                        vec![rng.gen(); rng.gen_range(1..100)],
                        predicate.clone(),
                        predicate_data.clone(),
                    ),
                ]
            }};
        }

        for (i, (input_predicate, correct_gas, expected)) in
            inputs.into_iter().enumerate()
        {
            let input_group = predicate_input!(input_predicate);
            for mut input in input_group {
                if !correct_gas {
                    input.set_predicate_gas_used(1234);
                }

                let mut script = TransactionBuilder::script(
                    [op::ret(0x01)].into_iter().collect(),
                    vec![],
                )
                .add_input(input)
                .add_fee_input()
                .finalize();

                if correct_gas {
                    script
                        .estimate_predicates(
                            &CheckPredicateParams::default(),
                            MemoryInstance::new(),
                            &storage,
                        )
                        .unwrap();
                }

                let tx = script
                    .into_checked_basic(height, &Default::default())
                    .unwrap();

                let result = check_predicates(
                    &tx,
                    &CheckPredicateParams::default(),
                    MemoryInstance::new(),
                    &storage,
                );

                assert_eq!(result.map(|_| ()), expected, "failed at input {}", i);
            }
        }
    }

    /// Verifies the runtime predicate validation rules outlined in the spec are actually
    /// validated https://github.com/FuelLabs/fuel-specs/blob/master/src/fuel-vm/index.md#predicate-verification
    #[test]
    fn inputs_are_validated_for_good_predicate_inputs() {
        const CORRECT_GAS: bool = true;
        let good_blob = vec![op::noop(), op::ret(0x01)];

        let inputs = vec![
            (
                // A valid predicate
                vec![
                    op::addi(0x10, 0x00, 0x01),
                    op::addi(0x10, 0x10, 0x01),
                    op::ret(0x01),
                ],
                CORRECT_GAS,
                Ok(()),
            ),
            (
                // Use `LDC` with mode `1` to load the blob into the predicate.
                vec![
                    // Allocate 32 byte on the heap.
                    op::movi(0x10, 32),
                    op::aloc(0x10),
                    // This will be our zeroed blob id
                    op::move_(0x10, HP),
                    // Store the size of the blob
                    op::bsiz(0x11, 0x10),
                    // Store start of the blob code
                    op::move_(0x12, SSP),
                    // Subtract the start of the code from the end of the code
                    op::sub(0x12, 0x12, IS),
                    // Divide the code by the instruction size to get the number of
                    // instructions
                    op::divi(0x12, 0x12, Instruction::SIZE as u16),
                    // Load the blob by `0x10` ID with the `0x11` size
                    op::ldc(0x10, ZERO, 0x11, 1),
                    // Jump to a new code location
                    op::jmp(0x12),
                ],
                CORRECT_GAS,
                Ok(()),
            ),
            (
                // Use `LDC` with mode `2` to load the part of the predicate from the
                // transaction.
                vec![
                    // Skip the return opcodes. One of two opcodes is a good opcode that
                    // returns `0x1`. This opcode is our source for the `LDC`
                    // opcode. We will copy return good opcode to the end
                    // of the `ssp` via `LDC`. And jump there to
                    // return `true` from the predicate.
                    op::jmpf(ZERO, 2),
                    // Bad return opcode that we want to skip.
                    op::ret(0x0),
                    // Good return opcode that we want to use for the `LDC`.
                    op::ret(0x1),
                    // Take the start of the code and move it for 2 opcodes to get the
                    // desired opcode to copy.
                    op::move_(0x10, IS),
                    // We don't need to copy `jmpf` and bad `ret` opcodes via `LDC`.
                    op::addi(0x10, 0x10, 2 * Instruction::SIZE as u16),
                    // Store end of the code
                    op::move_(0x12, SSP),
                    // Subtract the start of the code from the end of the code
                    op::sub(0x12, 0x12, IS),
                    // Divide the code by the instruction size to get the number of
                    // instructions
                    op::divi(0x12, 0x12, Instruction::SIZE as u16),
                    // We want to load only on good `ret` opcode.
                    op::movi(0x11, Instruction::SIZE as u32),
                    // Load the code from the memory address `0x10` with the `0x11` size
                    op::ldc(0x10, ZERO, 0x11, 2),
                    // Jump to a new code location
                    op::jmp(0x12),
                ],
                CORRECT_GAS,
                Ok(()),
            ),
        ];

        assert_inputs_are_validated_for_predicates(inputs, good_blob)
    }

    #[test]
    fn inputs_are_validated_for_bad_predicate_inputs() {
        const CORRECT_GAS: bool = true;
        const INCORRECT_GAS: bool = false;

        let bad_blob = vec![op::noop(), op::ret(0x00)];

        let inputs = vec![
            (
                // A valid predicate, but gas amount mismatches
                vec![
                    op::addi(0x10, 0x00, 0x01),
                    op::addi(0x10, 0x10, 0x01),
                    op::ret(0x01),
                ],
                INCORRECT_GAS,
                Err(PredicateVerificationFailed::GasMismatch),
            ),
            (
                // Returning an invalid value
                vec![op::ret(0x0)],
                CORRECT_GAS,
                Err(PredicateVerificationFailed::Panic(
                    PanicReason::PredicateReturnedNonOne,
                )),
            ),
            (
                // Using a contract instruction
                vec![op::time(0x20, 0x1), op::ret(0x1)],
                CORRECT_GAS,
                Err(PredicateVerificationFailed::PanicInstruction(
                    PanicInstruction::error(
                        PanicReason::ContractInstructionNotAllowed,
                        op::time(0x20, 0x1).into(),
                    ),
                )),
            ),
            (
                // Using a contract instruction
                vec![op::ldc(ONE, ONE, ONE, 0)],
                CORRECT_GAS,
                Err(PredicateVerificationFailed::PanicInstruction(
                    PanicInstruction::error(
                        PanicReason::ContractInstructionNotAllowed,
                        op::ldc(ONE, ONE, ONE, 0).into(),
                    ),
                )),
            ),
            (
                // Use `LDC` with mode `1` to load the blob into the predicate.
                vec![
                    // Allocate 32 byte on the heap.
                    op::movi(0x10, 32),
                    op::aloc(0x10),
                    // This will be our zeroed blob id
                    op::move_(0x10, HP),
                    // Store the size of the blob
                    op::bsiz(0x11, 0x10),
                    // Store start of the blob code
                    op::move_(0x12, SSP),
                    // Subtract the start of the code from the end of the code
                    op::sub(0x12, 0x12, IS),
                    // Divide the code by the instruction size to get the number of
                    // instructions
                    op::divi(0x12, 0x12, Instruction::SIZE as u16),
                    // Load the blob by `0x10` ID with the `0x11` size
                    op::ldc(0x10, ZERO, 0x11, 1),
                    // Jump to a new code location
                    op::jmp(0x12),
                ],
                CORRECT_GAS,
                Err(PredicateVerificationFailed::Panic(
                    PanicReason::PredicateReturnedNonOne,
                )),
            ),
            (
                // Use `LDC` with mode `2` to load the part of the predicate from the
                // transaction.
                vec![
                    // Skip the return opcodes. One of two opcodes is a bad opcode that
                    // returns `0x0`. This opcode is our source for the `LDC`
                    // opcode. We will copy return bad opcode to the end
                    // of the `ssp` via `LDC`. And jump there to
                    // return `false` from the predicate adn fail.
                    op::jmpf(ZERO, 2),
                    // Good return opcode that we want to skip.
                    op::ret(0x1),
                    // Bad return opcode that we want to use for the `LDC`.
                    op::ret(0x0),
                    // Take the start of the code and move it for 2 opcodes to get the
                    // desired opcode to copy.
                    op::move_(0x10, IS),
                    // We don't need to copy `jmpf` and bad `ret` opcodes via `LDC`.
                    op::addi(0x10, 0x10, 2 * Instruction::SIZE as u16),
                    // Store end of the code
                    op::move_(0x12, SSP),
                    // Subtract the start of the code from the end of the code
                    op::sub(0x12, 0x12, IS),
                    // Divide the code by the instruction size to get the number of
                    // instructions
                    op::divi(0x12, 0x12, Instruction::SIZE as u16),
                    // We want to load only on bad `ret` opcode.
                    op::movi(0x11, Instruction::SIZE as u32),
                    // Load the code from the memory address `0x10` with the `0x11` size
                    op::ldc(0x10, ZERO, 0x11, 2),
                    // Jump to a new code location
                    op::jmp(0x12),
                ],
                CORRECT_GAS,
                Err(PredicateVerificationFailed::Panic(
                    PanicReason::PredicateReturnedNonOne,
                )),
            ),
        ];

        assert_inputs_are_validated_for_predicates(inputs, bad_blob)
    }
}