sui_adapter_latest/static_programmable_transactions/typing/verify/
move_functions.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::execution_mode::ExecutionMode;
use crate::programmable_transactions::execution::check_private_generics;
use crate::static_programmable_transactions::typing::ast::InputArg;
use crate::static_programmable_transactions::{env::Env, loading::ast::Type, typing::ast as T};
use move_binary_format::{CompiledModule, file_format::Visibility};
use sui_types::{
    error::{ExecutionError, ExecutionErrorKind, command_argument_error},
    execution_status::CommandArgumentError,
};

struct Context {
    gas_coin: IsDirty,
    inputs: Vec<IsDirty>,
    results: Vec<Vec<IsDirty>>,
}

/// Is dirty for entry verifier rules
#[derive(Copy, Clone)]
enum IsDirty {
    /// BCS input is not yet fixed
    NotFixed,
    Fixed {
        is_dirty: bool,
    },
}

impl Context {
    fn new(inputs: &T::Inputs) -> Self {
        let inputs = inputs
            .iter()
            .map(|(arg, _)| match arg {
                InputArg::Pure(_) => IsDirty::NotFixed,
                InputArg::Receiving(_) | InputArg::Object(_) => IsDirty::Fixed { is_dirty: false },
            })
            .collect();
        Self {
            gas_coin: IsDirty::Fixed { is_dirty: false },
            inputs,
            results: vec![],
        }
    }

    // check if dirty, and mark it as fixed if mutably borrowing a pure input
    fn is_dirty(&mut self, arg: &T::Argument) -> bool {
        match &arg.value.0 {
            T::Argument__::Borrow(/* mut */ true, T::Location::Input(i)) => {
                match &mut self.inputs[*i as usize] {
                    input @ IsDirty::NotFixed => {
                        *input = IsDirty::Fixed { is_dirty: false };
                        false
                    }
                    IsDirty::Fixed { is_dirty } => *is_dirty,
                }
            }
            _ => self.is_loc_dirty(arg.value.0.location()),
        }
    }

    fn is_loc_dirty(&self, location: T::Location) -> bool {
        match location {
            T::Location::GasCoin => self.gas_coin.is_dirty(),
            T::Location::Input(i) => self.inputs[i as usize].is_dirty(),
            T::Location::Result(i, j) => self.results[i as usize][j as usize].is_dirty(),
        }
    }

    /// Marks mutable usages as dirty. We don't care about `Move` since the value will be moved
    /// and that location is no longer accessible.
    fn mark_dirty(&mut self, arg: &T::Argument) {
        match &arg.value.0 {
            T::Argument__::Borrow(/* mut */ true, loc) => self.mark_loc_dirty(*loc),
            T::Argument__::Borrow(/* mut */ false, _)
            | T::Argument__::Use(_)
            | T::Argument__::Read(_) => (),
        }
    }

    fn mark_loc_dirty(&mut self, location: T::Location) {
        match location {
            T::Location::GasCoin => self.gas_coin = IsDirty::Fixed { is_dirty: true },
            T::Location::Input(i) => match &mut self.inputs[i as usize] {
                // if it needs to be dirtied, it will first be marked as fixed
                IsDirty::NotFixed => (),
                IsDirty::Fixed { is_dirty } => *is_dirty = true,
            },
            T::Location::Result(i, j) => {
                self.results[i as usize][j as usize] = IsDirty::Fixed { is_dirty: true }
            }
        }
    }
}

impl IsDirty {
    fn is_dirty(self) -> bool {
        match self {
            IsDirty::NotFixed => false,
            IsDirty::Fixed { is_dirty } => is_dirty,
        }
    }
}

/// Checks the following
/// - entry function taint rules
/// - valid visibility for move function calls
///   - Can be disabled under certain execution modes
/// - private generics rules for move function calls
/// - no references returned from move calls
///    - Can be disabled under certain execution modes
pub fn verify<Mode: ExecutionMode>(env: &Env, txn: &T::Transaction) -> Result<(), ExecutionError> {
    let T::Transaction { inputs, commands } = txn;
    let mut context = Context::new(inputs);
    for (c, result) in commands {
        let result_dirties = command::<Mode>(env, &mut context, c, result)
            .map_err(|e| e.with_command_index(c.idx as usize))?;
        assert_invariant!(
            result_dirties.len() == result.len(),
            "result length mismatch"
        );
        context.results.push(result_dirties);
    }
    Ok(())
}

fn command<Mode: ExecutionMode>(
    env: &Env,
    context: &mut Context,
    command: &T::Command,
    result: &T::ResultType,
) -> Result<Vec<IsDirty>, ExecutionError> {
    Ok(match &command.value {
        T::Command_::MoveCall(call) => move_call::<Mode>(env, context, call, result)?,
        T::Command_::TransferObjects(objs, recipient) => {
            arguments(env, context, objs);
            argument(env, context, recipient);
            vec![]
        }
        T::Command_::SplitCoins(_, coin, amounts) => {
            let amounts_are_dirty = arguments(env, context, amounts);
            let coin_is_dirty = argument(env, context, coin);
            debug_assert!(!amounts_are_dirty);
            let is_dirty = amounts_are_dirty || coin_is_dirty;
            vec![IsDirty::Fixed { is_dirty }; result.len()]
        }
        T::Command_::MergeCoins(_, target, coins) => {
            let is_dirty = arguments(env, context, coins);
            argument(env, context, target);
            if is_dirty {
                context.mark_dirty(target);
            }
            vec![]
        }
        T::Command_::MakeMoveVec(_, args) => {
            let is_dirty = arguments(env, context, args);
            debug_assert_eq!(result.len(), 1);
            vec![IsDirty::Fixed { is_dirty }]
        }
        T::Command_::Publish(_, _, _) => {
            debug_assert_eq!(Mode::packages_are_predefined(), result.is_empty());
            debug_assert_eq!(!Mode::packages_are_predefined(), result.len() == 1);
            result
                .iter()
                .map(|_| IsDirty::Fixed { is_dirty: false })
                .collect::<Vec<_>>()
        }
        T::Command_::Upgrade(_, _, _, ticket, _) => {
            debug_assert_eq!(result.len(), 1);
            let result = vec![IsDirty::Fixed { is_dirty: false }];
            argument(env, context, ticket);
            result
        }
    })
}

fn arguments(env: &Env, context: &mut Context, args: &[T::Argument]) -> bool {
    args.iter().any(|arg| argument(env, context, arg))
}

fn argument(_env: &Env, context: &mut Context, arg: &T::Argument) -> bool {
    context.is_dirty(arg)
}

fn move_call<Mode: ExecutionMode>(
    env: &Env,
    context: &mut Context,
    call: &T::MoveCall,
    result: &T::ResultType,
) -> Result<Vec<IsDirty>, ExecutionError> {
    let T::MoveCall {
        function,
        arguments: args,
    } = call;
    check_signature::<Mode>(function)?;
    check_private_generics(&function.runtime_id, function.name.as_ident_str())?;
    let (vis, is_entry) = check_visibility::<Mode>(env, function)?;
    let arg_dirties = args
        .iter()
        .map(|arg| argument(env, context, arg))
        .collect::<Vec<_>>();
    if is_entry && matches!(vis, Visibility::Private) {
        for (idx, &arg_is_dirty) in arg_dirties.iter().enumerate() {
            if arg_is_dirty && !Mode::allow_arbitrary_values() {
                return Err(command_argument_error(
                    CommandArgumentError::InvalidArgumentToPrivateEntryFunction,
                    idx,
                ));
            }
        }
    } else if !is_entry {
        // mark args dirty if not entry
        for arg in args {
            context.mark_dirty(arg);
        }
    }
    let is_dirty = is_entry || arg_dirties.iter().any(|&d| d);
    Ok(vec![IsDirty::Fixed { is_dirty }; result.len()])
}

fn check_signature<Mode: ExecutionMode>(
    function: &T::LoadedFunction,
) -> Result<(), ExecutionError> {
    fn check_return_type<Mode: ExecutionMode>(
        idx: usize,
        return_type: &T::Type,
    ) -> Result<(), ExecutionError> {
        match return_type {
            Type::Reference(_, _) => {
                if !Mode::allow_arbitrary_values() {
                    return Err(ExecutionError::from_kind(
                        ExecutionErrorKind::InvalidPublicFunctionReturnType { idx: idx as u16 },
                    ));
                }
                todo!("RUNTIME"); // can we support this?
            }
            t => t,
        };
        Ok(())
    }
    for (idx, ty) in function.signature.return_.iter().enumerate() {
        check_return_type::<Mode>(idx, ty)?;
    }
    Ok(())
}

fn check_visibility<Mode: ExecutionMode>(
    env: &Env,
    function: &T::LoadedFunction,
) -> Result<(Visibility, /* is_entry */ bool), ExecutionError> {
    let module = env.module_definition(&function.runtime_id, &function.linkage)?;
    let module: &CompiledModule = module.as_ref();
    let Some((_index, fdef)) = module.find_function_def_by_name(function.name.as_str()) else {
        invariant_violation!(
            "Could not resolve function '{}' in module {}. \
            This should have been checked when linking",
            &function.name,
            module.self_id(),
        );
    };
    let visibility = fdef.visibility;
    let is_entry = fdef.is_entry;
    match (visibility, is_entry) {
        // can call entry
        (Visibility::Private | Visibility::Friend, true) => (),
        // can call public entry
        (Visibility::Public, true) => (),
        // can call public
        (Visibility::Public, false) => (),
        // cannot call private or friend if not entry
        (Visibility::Private | Visibility::Friend, false) => {
            if !Mode::allow_arbitrary_function_calls() {
                return Err(ExecutionError::new_with_source(
                    ExecutionErrorKind::NonEntryFunctionInvoked,
                    "Can only call `entry` or `public` functions",
                ));
            }
        }
    };
    Ok((visibility, is_entry))
}