fuel-vm 0.26.0

FuelVM interpreter.
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use super::memory::OwnershipRegisters;
use super::{ExecutableTransaction, Interpreter, MemoryRange};
use crate::call::CallFrame;
use crate::consts::*;
use crate::error::{Bug, BugId, BugVariant, RuntimeError};
use crate::storage::InterpreterStorage;

use fuel_asm::{PanicReason, RegId};
use fuel_tx::{Output, Receipt};
use fuel_types::bytes::{self, Deserializable};
use fuel_types::{Address, AssetId, Bytes32, Bytes8, ContractId, RegisterId, Word};

use crate::arith::{add_usize, checked_add_usize, checked_add_word, checked_sub_word};
use crate::interpreter::PanicContext;
use core::slice;
use std::ops::Range;

#[cfg(test)]
mod test;

impl<S, Tx> Interpreter<S, Tx>
where
    S: InterpreterStorage,
    Tx: ExecutableTransaction,
{
    pub(crate) fn coinbase(&self) -> Result<Address, RuntimeError> {
        self.storage.coinbase().map_err(RuntimeError::from_io)
    }

    /// Loads contract ID pointed by `a`, and then for that contract,
    /// copies `c` bytes from it starting from offset `b` into the stack.
    /// ```txt
    /// contract_id = mem[$rA, 32]
    /// contract_code = contracts[contract_id]
    /// mem[$ssp, $rC] = contract_code[$rB, $rC]
    /// ```
    pub(crate) fn load_contract_code(&mut self, a: Word, b: Word, c: Word) -> Result<(), RuntimeError> {
        let ssp = self.registers[RegId::SSP];
        let sp = self.registers[RegId::SP];
        let fp = self.registers[RegId::FP] as usize;

        if ssp != sp {
            return Err(PanicReason::ExpectedUnallocatedStack.into());
        }

        let contract_id = a as usize;
        let contract_id_end = checked_add_usize(ContractId::LEN, contract_id)?;
        let contract_offset = b as usize;
        let length = bytes::padded_len_usize(c as usize);

        let memory_offset = ssp as usize;
        let memory_offset_end = checked_add_usize(memory_offset, length)?;

        // Validate arguments
        if memory_offset_end > self.registers[RegId::HP] as usize
            || contract_id_end as Word > VM_MAX_RAM
            || length > MEM_MAX_ACCESS_SIZE as usize
            || length > self.params.contract_max_size as usize
        {
            return Err(PanicReason::MemoryOverflow.into());
        }

        // compiler will optimize to memset
        self.memory[memory_offset..memory_offset_end]
            .iter_mut()
            .for_each(|m| *m = 0);

        // fetch the contract id
        let contract_id = &self.memory[contract_id..contract_id_end];

        // Safety: Memory bounds are checked and consistent
        let contract_id = unsafe { ContractId::as_ref_unchecked(contract_id) };

        // the contract must be declared in the transaction inputs
        if !self.transaction().input_contracts().any(|id| id == contract_id) {
            self.panic_context = PanicContext::ContractId(*contract_id);
            return Err(PanicReason::ContractNotInInputs.into());
        };

        // fetch the storage contract
        let contract = self.contract(contract_id)?;
        let contract = contract.as_ref().as_ref();

        if contract_offset > contract.len() {
            return Err(PanicReason::MemoryOverflow.into());
        }

        let contract = &contract[contract_offset..];
        let len = contract.len().min(length);

        let code = &contract[..len];

        // Safety: all bounds are checked
        let memory = &self.memory[memory_offset] as *const u8;
        let memory = unsafe { slice::from_raw_parts_mut(memory as *mut u8, len) };

        // perform the code copy
        memory.copy_from_slice(code);

        self.registers[RegId::SP]
            //TODO this is looser than the compare against [RegId::HP,RegId::SSP+length]
            .checked_add(length as Word)
            .map(|sp| {
                self.registers[RegId::SP] = sp;
                self.registers[RegId::SSP] = sp;
            })
            .ok_or_else(|| Bug::new(BugId::ID007, BugVariant::StackPointerOverflow))?;

        // update frame pointer, if we have a stack frame (e.g. fp > 0)
        if fp > 0 {
            let fpx = add_usize(fp, CallFrame::code_size_offset());

            self.memory[fp..fpx].copy_from_slice(&length.to_be_bytes());
        }

        self.inc_pc()
    }

    pub(crate) fn burn(&mut self, a: Word) -> Result<(), RuntimeError> {
        let (c, cx) = self.internal_contract_bounds()?;

        // Safety: Memory bounds logically verified by the interpreter
        let contract = unsafe { ContractId::as_ref_unchecked(&self.memory[c..cx]) };
        let asset_id = unsafe { AssetId::as_ref_unchecked(&self.memory[c..cx]) };

        let balance = self.balance(contract, asset_id)?;
        let balance = balance.checked_sub(a).ok_or(PanicReason::NotEnoughBalance)?;

        self.storage
            .merkle_contract_asset_id_balance_insert(contract, asset_id, balance)
            .map_err(RuntimeError::from_io)?;

        self.inc_pc()
    }

    pub(crate) fn mint(&mut self, a: Word) -> Result<(), RuntimeError> {
        let (c, cx) = self.internal_contract_bounds()?;

        // Safety: Memory bounds logically verified by the interpreter
        let contract = unsafe { ContractId::as_ref_unchecked(&self.memory[c..cx]) };
        let asset_id = unsafe { AssetId::as_ref_unchecked(&self.memory[c..cx]) };

        let balance = self.balance(contract, asset_id)?;
        let balance = checked_add_word(balance, a)?;

        self.storage
            .merkle_contract_asset_id_balance_insert(contract, asset_id, balance)
            .map_err(RuntimeError::from_io)?;

        self.inc_pc()
    }

    pub(crate) fn code_copy(&mut self, a: Word, b: Word, c: Word, d: Word) -> Result<(), RuntimeError> {
        let bx = checked_add_word(b, ContractId::LEN as Word)?;
        let cd = checked_add_word(c, d)?;

        if d > MEM_MAX_ACCESS_SIZE || a > checked_sub_word(VM_MAX_RAM, d)? || bx > VM_MAX_RAM || cd > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }

        let (a, b, c, d) = (a as usize, b as usize, c as usize, d as usize);
        let (bx, cd) = (bx as usize, cd as usize);

        // Safety: Memory bounds are checked by the interpreter
        let contract = unsafe { ContractId::as_ref_unchecked(&self.memory[b..bx]) };

        if !self.transaction().input_contracts().any(|input| input == contract) {
            self.panic_context = PanicContext::ContractId(*contract);
            return Err(PanicReason::ContractNotInInputs.into());
        }

        let contract = self.contract(contract)?.into_owned();

        if contract.as_ref().len() < d {
            self.try_zeroize(a, d)?;
        } else {
            self.try_mem_write(a, &contract.as_ref()[c..cd])?;
        }

        self.inc_pc()
    }

    pub(crate) fn block_hash(&mut self, a: Word, b: Word) -> Result<(), RuntimeError> {
        let hash = self.storage.block_hash(b as u32).map_err(|e| e.into())?;

        self.try_mem_write(a as usize, hash.as_ref())?;

        self.inc_pc()
    }

    pub(crate) fn set_block_height(&mut self, ra: RegisterId) -> Result<(), RuntimeError> {
        Self::is_register_writable(ra)?;

        self.context
            .block_height()
            .map(|h| h as Word)
            .map(|h| self.registers[ra] = h)
            .ok_or(PanicReason::TransactionValidity)?;

        self.inc_pc()
    }

    pub(crate) fn block_proposer(&mut self, a: Word) -> Result<(), RuntimeError> {
        self.coinbase()
            .and_then(|data| self.try_mem_write(a as usize, data.as_ref()))?;

        self.inc_pc()
    }

    pub(crate) fn code_root(&mut self, a: Word, b: Word) -> Result<(), RuntimeError> {
        let ax = checked_add_word(a, Bytes32::LEN as Word)?;
        let bx = checked_add_word(b, ContractId::LEN as Word)?;

        if ax > VM_MAX_RAM || bx > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }

        let (a, b) = (a as usize, b as usize);
        let bx = bx as usize;

        // Safety: Memory bounds are checked by the interpreter
        let contract_id = unsafe { ContractId::as_ref_unchecked(&self.memory[b..bx]) };

        let (_, root) = self
            .storage
            .storage_contract_root(contract_id)
            .transpose()
            .ok_or(PanicReason::ContractNotFound)?
            .map_err(RuntimeError::from_io)?
            .into_owned();

        self.try_mem_write(a, root.as_ref())?;

        self.inc_pc()
    }

    pub(crate) fn code_size(&mut self, ra: RegisterId, b: Word) -> Result<(), RuntimeError> {
        Self::is_register_writable(ra)?;

        let bx = checked_add_word(b, ContractId::LEN as Word)?;

        if bx > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }

        let (b, bx) = (b as usize, bx as usize);

        // Safety: Memory bounds are checked by the interpreter
        let contract_id = unsafe { ContractId::as_ref_unchecked(&self.memory[b..bx]) };

        // FIXME: This is checking the cost after the db call has happened.
        // when https://github.com/FuelLabs/fuel-vm/pull/272 lands this check
        // should happen on the pinned slice before reading it.
        let len = self.contract(contract_id)?.as_ref().as_ref().len() as Word;
        self.dependent_gas_charge(self.gas_costs.csiz, len)?;
        self.registers[ra] = len;

        self.inc_pc()
    }

    pub(crate) fn state_clear_qword(&mut self, a: Word, rb: RegisterId, c: Word) -> Result<(), RuntimeError> {
        Self::is_register_writable(rb)?;

        let contract_id = *self.internal_contract()?;
        let input = StateClearQWord::new(a, c)?;
        let Self {
            ref mut storage,
            ref memory,
            ref mut registers,
            ..
        } = self;

        state_clear_qword(&contract_id, storage, memory, &mut registers[rb], input)?;

        self.inc_pc()
    }

    pub(crate) fn state_read_word(&mut self, ra: RegisterId, rb: RegisterId, c: Word) -> Result<(), RuntimeError> {
        Self::is_register_writable(ra)?;
        Self::is_register_writable(rb)?;

        let cx = checked_add_word(c, Bytes32::LEN as Word)?;

        if cx > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }

        let (c, cx) = (c as usize, cx as usize);

        let contract = self.internal_contract()?;

        // Safety: Memory bounds are checked by the interpreter
        let key = unsafe { Bytes32::as_ref_unchecked(&self.memory[c..cx]) };

        let result = self
            .storage
            .merkle_contract_state(contract, key)
            .map_err(RuntimeError::from_io)?
            .map(|state| unsafe { Bytes8::from_slice_unchecked(state.as_ref().as_ref()).into() })
            .map(Word::from_be_bytes);

        self.registers[ra] = result.unwrap_or(0);
        self.registers[rb] = result.is_some() as Word;

        self.inc_pc()
    }

    pub(crate) fn state_read_qword(&mut self, a: Word, rb: RegisterId, c: Word, d: Word) -> Result<(), RuntimeError> {
        Self::is_register_writable(rb)?;
        let contract_id = *self.internal_contract()?;
        let input = StateReadQWord::new(a, c, d, self.ownership_registers())?;
        let Self {
            ref storage,
            ref mut memory,
            ref mut registers,
            ..
        } = self;

        state_read_qword(&contract_id, storage, memory, &mut registers[rb], input)?;
        self.inc_pc()
    }

    pub(crate) fn state_write_word(&mut self, a: Word, rb: RegisterId, c: Word) -> Result<(), RuntimeError> {
        Self::is_register_writable(rb)?;

        let ax = checked_add_word(a, Bytes32::LEN as Word)?;

        if ax > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }

        let (a, ax) = (a as usize, ax as usize);
        let (d, dx) = self.internal_contract_bounds()?;

        // Safety: Memory bounds logically verified by the interpreter
        let contract = unsafe { ContractId::as_ref_unchecked(&self.memory[d..dx]) };
        let key = unsafe { Bytes32::as_ref_unchecked(&self.memory[a..ax]) };

        let mut value = Bytes32::default();

        value[..WORD_SIZE].copy_from_slice(&c.to_be_bytes());

        let result = self
            .storage
            .merkle_contract_state_insert(contract, key, &value)
            .map_err(RuntimeError::from_io)?;

        self.registers[rb] = result.is_some() as Word;

        self.inc_pc()
    }

    pub(crate) fn state_write_qword(&mut self, a: Word, rb: RegisterId, c: Word, d: Word) -> Result<(), RuntimeError> {
        Self::is_register_writable(rb)?;
        let contract_id = *self.internal_contract()?;
        let input = StateWriteQWord::new(a, c, d)?;
        let Self {
            ref mut storage,
            ref mut memory,
            ref mut registers,
            ..
        } = self;

        state_write_qword(&contract_id, storage, memory, &mut registers[rb], input)?;
        self.inc_pc()
    }

    pub(crate) fn timestamp(&mut self, ra: RegisterId, b: Word) -> Result<(), RuntimeError> {
        Self::is_register_writable(ra)?;

        self.block_height()
            .and_then(|c| (b <= c as Word).then_some(()).ok_or(PanicReason::TransactionValidity))?;

        let b = u32::try_from(b).map_err(|_| PanicReason::ArithmeticOverflow)?;

        self.registers[ra] = self.storage.timestamp(b).map_err(|e| e.into())?;

        self.inc_pc()
    }

    pub(crate) fn message_output(&mut self, a: Word, b: Word, c: Word, d: Word) -> Result<(), RuntimeError> {
        let ax = checked_add_word(a, Address::LEN as Word)?;
        let bx = checked_add_word(ax, b)?;

        //TODO check on b vs MEM_MAX_ACCESS_SIZE is looser than msg length check
        if b > self.params.max_message_data_length || b > MEM_MAX_ACCESS_SIZE || bx > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }

        let (a, ax, bx) = (a as usize, ax as usize, bx as usize);

        let idx = c;
        let amount = d;

        // Safety: checked len
        let recipient = unsafe { Address::from_slice_unchecked(&self.memory[a..ax]) };
        if recipient == Address::zeroed() {
            return Err(PanicReason::ZeroedMessageOutputRecipient.into());
        }

        let offset = self
            .transaction()
            .outputs_offset_at(c as usize)
            .and_then(|ofs| ofs.checked_add(self.tx_offset()))
            .ok_or(PanicReason::OutputNotFound)?;

        // halt with I/O error because tx should be serialized correctly into vm memory
        let output = Output::from_bytes(&self.memory[offset..])?;

        // amount isn't checked because we are allowed to send zero balances with a message
        if !matches!(output, Output::Message { recipient, .. } if recipient == Address::zeroed()) {
            return Err(PanicReason::NonZeroMessageOutputRecipient.into());
        }

        // validations passed, perform the mutations

        self.base_asset_balance_sub(amount)?;

        let fp = self.registers[RegId::FP] as usize;
        let txid = self.tx_id();
        let data = ax;
        let data = &self.memory[data..bx];
        let data = data.to_vec();

        let fpx = checked_add_usize(fp, Address::LEN)?;

        // Safety: $fp is guaranteed to contain enough bytes
        let sender = unsafe { Address::from_slice_unchecked(&self.memory[fp..fpx]) };

        let message = Output::message(recipient, amount);
        let receipt = Receipt::message_out_from_tx_output(txid, idx, sender, recipient, amount, data);

        self.set_message_output(idx as usize, message)?;
        self.append_receipt(receipt);

        self.inc_pc()
    }
}

struct StateReadQWord {
    /// The destination memory address is
    /// stored in this range of memory.
    destination_address_memory_range: Range<usize>,
    /// The starting storage key location is stored
    /// in this range of memory.
    origin_key_memory_range: Range<usize>,
    /// Number of slots to read.
    num_slots: Word,
}

impl StateReadQWord {
    fn new(
        destination_memory_address: Word,
        origin_key_memory_address: Word,
        num_slots: Word,
        ownership_registers: OwnershipRegisters,
    ) -> Result<Self, RuntimeError> {
        let mem_range = MemoryRange::new(
            destination_memory_address,
            (Bytes32::LEN as Word).saturating_mul(num_slots),
        );
        if !ownership_registers.has_ownership_range(&mem_range) {
            return Err(PanicReason::MemoryOwnership.into());
        }
        if ownership_registers.context.is_external() {
            return Err(PanicReason::ExpectedInternalContext.into());
        }
        let dest_end = checked_add_word(
            destination_memory_address,
            Bytes32::LEN.saturating_mul(num_slots as usize) as Word,
        )?;
        let origin_end = checked_add_word(origin_key_memory_address, Bytes32::LEN as Word)?;
        if dest_end > VM_MAX_RAM || origin_end > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }
        Ok(Self {
            destination_address_memory_range: (destination_memory_address as usize)..(dest_end as usize),
            origin_key_memory_range: (origin_key_memory_address as usize)..(origin_end as usize),
            num_slots,
        })
    }
}

fn state_read_qword(
    contract_id: &ContractId,
    storage: &impl InterpreterStorage,
    memory: &mut [u8],
    result_register: &mut Word,
    input: StateReadQWord,
) -> Result<(), RuntimeError> {
    // Safety: Memory bounds are checked by the interpreter
    let origin_key = unsafe { Bytes32::as_ref_unchecked(&memory[input.origin_key_memory_range]) };

    let mut all_set = true;
    let result: Vec<u8> = storage
        .merkle_contract_state_range(contract_id, origin_key, input.num_slots)
        .map_err(RuntimeError::from_io)?
        .into_iter()
        .flat_map(|bytes| match bytes {
            Some(bytes) => **bytes,
            None => {
                all_set = false;
                *Bytes32::zeroed()
            }
        })
        .collect();

    *result_register = all_set as Word;

    memory[input.destination_address_memory_range].copy_from_slice(&result);

    Ok(())
}

struct StateWriteQWord {
    /// The starting storage key location is stored
    /// in this range of memory.
    starting_storage_key_memory_range: Range<usize>,
    /// The source data memory address is
    /// stored in this range of memory.
    source_address_memory_range: Range<usize>,
}

impl StateWriteQWord {
    fn new(
        starting_storage_key_memory_address: Word,
        source_memory_address: Word,
        num_slots: Word,
    ) -> Result<Self, RuntimeError> {
        let source_end = checked_add_word(
            source_memory_address,
            Bytes32::LEN.saturating_mul(num_slots as usize) as Word,
        )?;
        let starting_key_end = checked_add_word(starting_storage_key_memory_address, Bytes32::LEN as Word)?;
        if starting_key_end > VM_MAX_RAM || source_end > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }
        Ok(Self {
            source_address_memory_range: (source_memory_address as usize)..(source_end as usize),
            starting_storage_key_memory_range: (starting_storage_key_memory_address as usize)
                ..(starting_key_end as usize),
        })
    }
}

fn state_write_qword(
    contract_id: &ContractId,
    storage: &mut impl InterpreterStorage,
    memory: &[u8],
    result_register: &mut Word,
    input: StateWriteQWord,
) -> Result<(), RuntimeError> {
    // Safety: Memory bounds are checked by the interpreter
    let destination_key = unsafe { Bytes32::as_ref_unchecked(&memory[input.starting_storage_key_memory_range]) };

    let values: Vec<_> = memory[input.source_address_memory_range]
        .chunks_exact(Bytes32::LEN)
        .flat_map(|chunk| Some(Bytes32::from(<[u8; 32]>::try_from(chunk).ok()?)))
        .collect();

    let any_none = storage
        .merkle_contract_state_insert_range(contract_id, destination_key, &values)
        .map_err(RuntimeError::from_io)?
        .is_some();
    *result_register = any_none as Word;

    Ok(())
}

struct StateClearQWord {
    /// The starting storage key location is stored
    /// in this range of memory.
    start_storage_key_memory_range: Range<usize>,
    /// Number of slots to read.
    num_slots: Word,
}

impl StateClearQWord {
    fn new(start_storage_key_memory_address: Word, num_slots: Word) -> Result<Self, RuntimeError> {
        let start_key_end = checked_add_word(start_storage_key_memory_address, Bytes32::LEN as Word)?;
        if start_key_end > VM_MAX_RAM {
            return Err(PanicReason::MemoryOverflow.into());
        }
        Ok(Self {
            start_storage_key_memory_range: (start_storage_key_memory_address as usize)..(start_key_end as usize),
            num_slots,
        })
    }
}

fn state_clear_qword(
    contract_id: &ContractId,
    storage: &mut impl InterpreterStorage,
    memory: &[u8],
    result_register: &mut Word,
    input: StateClearQWord,
) -> Result<(), RuntimeError> {
    // Safety: Memory bounds are checked by the interpreter
    let start_key = unsafe { Bytes32::as_ref_unchecked(&memory[input.start_storage_key_memory_range]) };

    let all_previously_set = storage
        .merkle_contract_state_remove_range(contract_id, start_key, input.num_slots)
        .map_err(RuntimeError::from_io)?
        .is_some();

    *result_register = all_previously_set as Word;

    Ok(())
}