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
use super::Host;
use crate::{
    db::{database::ZephyrDatabase, ledger::LedgerStateRead},
    error::{HostError, InternalError},
};
use anyhow::{anyhow, Result};
use soroban_env_host::vm::CustomContextVM;
use wasmi::{core::Pages, Caller, Memory};

pub struct CustomVMCtx<'a, DB: ZephyrDatabase + 'static, L: LedgerStateRead + 'static> {
    caller: Option<&'a Caller<'a, Host<DB, L>>>,
    caller_mut: Option<Caller<'a, Host<DB, L>>>,
}

impl<'a, DB: ZephyrDatabase + 'static, L: LedgerStateRead + 'static> CustomVMCtx<'a, DB, L> {
    pub fn new(ctx: &'a Caller<Host<DB, L>>) -> Self {
        Self {
            caller: Some(ctx),
            caller_mut: None,
        }
    }

    pub fn new_mut(ctx: Caller<'a, Host<DB, L>>) -> Self {
        Self {
            caller: None,
            caller_mut: Some(ctx),
        }
    }
}

impl<'a, DB: ZephyrDatabase + Clone + 'static, L: LedgerStateRead + 'static> CustomContextVM
    for CustomVMCtx<'a, DB, L>
{
    // Note: we prefer not to handle potential VM memory errors here since
    // they would need to be handled by our SVM fork and we're trying to keep
    // as less logic as possible there.
    fn read(&self, mem_pos: usize, buf: &mut [u8]) {
        if let Some(caller) = self.caller {
            let _ = Host::get_memory(caller).read(caller, mem_pos, buf);
        } else {
            let _ = Host::get_memory(self.caller_mut.as_ref().unwrap()).read(
                self.caller_mut.as_ref().unwrap(),
                mem_pos,
                buf,
            );
        }
    }

    fn data(&self) -> &[u8] {
        if let Some(caller) = self.caller {
            Host::get_memory(caller).data(caller)
        } else {
            Host::get_memory(self.caller_mut.as_ref().unwrap())
                .data(self.caller_mut.as_ref().unwrap())
        }
    }

    fn write(&mut self, pos: u32, slice: &[u8]) -> i64 {
        Host::write_to_memory_mut(self.caller_mut.as_mut().unwrap(), pos, slice).unwrap()
    }

    fn data_mut(&mut self) -> &mut [u8] {
        if let Some(caller) = self.caller_mut.as_mut() {
            Host::get_memory(caller).data_mut(caller)
        } else {
            &mut []
        }
    }
}

impl<DB: ZephyrDatabase + Clone + 'static, L: LedgerStateRead + 'static> Host<DB, L> {
    /// Returns wasmi's VM memory handler.
    pub fn get_memory(caller: &Caller<Self>) -> Memory {
        let host = caller.data();

        let memory = {
            let context = host.0.context.borrow();
            let vm = context.vm.as_ref().unwrap().upgrade().unwrap();
            let mem_manager = &vm.memory_manager;

            mem_manager.memory
        };

        memory
    }

    pub(crate) fn write_to_memory(mut caller: Caller<Self>, contents: &[u8]) -> Result<(i64, i64)> {
        let (memory, offset, data) = {
            let host = caller.data();

            let context = host.0.context.borrow();
            let vm = context
                .vm
                .as_ref()
                .ok_or_else(|| HostError::NoContext)?
                .upgrade()
                .ok_or_else(|| HostError::InternalError(InternalError::CannotUpgradeRc))?;

            let manager = &vm.memory_manager;
            let memory = manager.memory;

            let mut offset_mut = manager.offset.borrow_mut();
            let new_offset = offset_mut
                .checked_add(contents.len())
                .ok_or_else(|| HostError::InternalError(InternalError::ArithError))?;

            *offset_mut = new_offset;

            (memory, new_offset, contents)
        };

        // TODO: this should actually only grow the linear memory when needed, so check the current
        // pages and the size of the contents to compute a safe pages size (else error with a growth error).
        // That said, the program cannot grow unbounded since Memory::grow throws an error in that case.
        let _ = memory.grow(&mut caller, Pages::new(1000).unwrap());

        if let Err(error) = memory.write(&mut caller, offset, data) {
            return Err(anyhow!(error));
        };

        Ok((offset as i64, data.len() as i64))
    }

    pub(crate) fn write_to_memory_mut(
        caller: &mut Caller<Self>,
        pos: u32,
        contents: &[u8],
    ) -> Result<i64> {
        let memory = Self::get_memory(caller);

        if let Err(error) = memory.write(caller, pos as usize, contents) {
            return Err(anyhow!(error));
        };

        Ok((pos + contents.len() as u32) as i64)
    }

    pub(crate) fn read_segment_from_memory(
        memory: &Memory,
        caller: &Caller<Self>,
        segment: (i64, i64),
    ) -> Result<Vec<u8>> {
        let mut written_vec = vec![0; segment.1 as usize];
        if let Err(error) = memory.read(caller, segment.0 as usize, &mut written_vec) {
            return Err(anyhow!(error));
        }

        Ok(written_vec)
    }
}