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

use crate::{
    execution_mode::ExecutionMode,
    programmable_transactions::execution::{PrimitiveArgumentLayout, bcs_argument_validate},
    sp,
    static_programmable_transactions::{
        env::Env,
        loading::ast::Type,
        typing::ast::{self as T, BytesConstraint, ObjectArg},
    },
};
use indexmap::IndexSet;
use sui_types::{
    base_types::{RESOLVED_ASCII_STR, RESOLVED_STD_OPTION, RESOLVED_UTF8_STR},
    error::{ExecutionError, ExecutionErrorKind, command_argument_error},
    execution_status::CommandArgumentError,
    id::RESOLVED_SUI_ID,
    transfer::RESOLVED_RECEIVING_STRUCT,
};

struct ObjectUsage {
    allow_by_value: bool,
    allow_by_mut_ref: bool,
}

struct Context {
    objects: Vec<ObjectUsage>,
}

impl Context {
    fn new(txn: &T::Transaction) -> Self {
        let objects = txn
            .objects
            .iter()
            .map(|object_input| match &object_input.arg {
                ObjectArg::ImmObject(_) => ObjectUsage {
                    allow_by_value: false,
                    allow_by_mut_ref: false,
                },
                ObjectArg::OwnedObject(_) => ObjectUsage {
                    allow_by_value: true,
                    allow_by_mut_ref: true,
                },
                ObjectArg::SharedObject { mutable, .. } => ObjectUsage {
                    allow_by_value: *mutable,
                    allow_by_mut_ref: *mutable,
                },
            })
            .collect();
        Self { objects }
    }
}

/// Verifies two properties for input objects:
/// 1. That the `Pure` inputs can be serialized to the type inferred and that the type is
///    permissible
///    - Can be relaxed under certain execution modes
/// 2. That any `Object` arguments are used validly. This means mutable references are taken only
///    on mutable objects. And that the gas coin is only taken by value in transfer objects
pub fn verify<Mode: ExecutionMode>(_env: &Env, txn: &T::Transaction) -> Result<(), ExecutionError> {
    let T::Transaction {
        bytes,
        objects: _,
        pure,
        receiving,
        commands,
    } = txn;
    for pure in pure {
        check_pure_input::<Mode>(bytes, pure)?;
    }
    for receiving in receiving {
        check_receving_input(receiving)?;
    }
    let context = &mut Context::new(txn);
    for c in commands {
        command(context, c).map_err(|e| e.with_command_index(c.idx as usize))?;
    }
    Ok(())
}

//**************************************************************************************************
// Pure bytes
//**************************************************************************************************

fn check_pure_input<Mode: ExecutionMode>(
    bytes: &IndexSet<Vec<u8>>,
    pure: &T::PureInput,
) -> Result<(), ExecutionError> {
    let T::PureInput {
        original_input_index,
        byte_index,
        ty,
        constraint,
    } = pure;
    let Some(bcs_bytes) = bytes.get_index(*byte_index) else {
        invariant_violation!(
            "Unbound byte index {} for pure input at index {}",
            byte_index,
            original_input_index.0
        );
    };
    let BytesConstraint { command, argument } = constraint;
    check_pure_bytes::<Mode>(*argument, bcs_bytes, ty)
        .map_err(|e| e.with_command_index(*command as usize))
}

fn check_pure_bytes<Mode: ExecutionMode>(
    command_arg_idx: u16,
    bytes: &[u8],
    constraint: &Type,
) -> Result<(), ExecutionError> {
    assert_invariant!(
        !matches!(constraint, Type::Reference(_, _)),
        "references should not be added as a constraint"
    );
    if Mode::allow_arbitrary_values() {
        return Ok(());
    }
    let Some(layout) = primitive_serialization_layout(constraint)? else {
        let msg = format!(
            "Invalid usage of `Pure` argument for a non-primitive argument type at index {command_arg_idx}.",
        );
        return Err(ExecutionError::new_with_source(
            ExecutionErrorKind::command_argument_error(
                CommandArgumentError::InvalidUsageOfPureArg,
                command_arg_idx,
            ),
            msg,
        ));
    };
    bcs_argument_validate(bytes, command_arg_idx, layout)?;
    Ok(())
}

fn primitive_serialization_layout(
    param_ty: &Type,
) -> Result<Option<PrimitiveArgumentLayout>, ExecutionError> {
    Ok(match param_ty {
        Type::Signer => return Ok(None),
        Type::Reference(_, _) => {
            invariant_violation!("references should not be added as a constraint")
        }
        Type::Bool => Some(PrimitiveArgumentLayout::Bool),
        Type::U8 => Some(PrimitiveArgumentLayout::U8),
        Type::U16 => Some(PrimitiveArgumentLayout::U16),
        Type::U32 => Some(PrimitiveArgumentLayout::U32),
        Type::U64 => Some(PrimitiveArgumentLayout::U64),
        Type::U128 => Some(PrimitiveArgumentLayout::U128),
        Type::U256 => Some(PrimitiveArgumentLayout::U256),
        Type::Address => Some(PrimitiveArgumentLayout::Address),

        Type::Vector(v) => {
            let info_opt = primitive_serialization_layout(&v.element_type)?;
            info_opt.map(|layout| PrimitiveArgumentLayout::Vector(Box::new(layout)))
        }
        Type::Datatype(dt) => {
            let resolved = dt.qualified_ident();
            // is option of a string
            if resolved == RESOLVED_STD_OPTION && dt.type_arguments.len() == 1 {
                let info_opt = primitive_serialization_layout(&dt.type_arguments[0])?;
                info_opt.map(|layout| PrimitiveArgumentLayout::Option(Box::new(layout)))
            } else if dt.type_arguments.is_empty() {
                if resolved == RESOLVED_SUI_ID {
                    Some(PrimitiveArgumentLayout::Address)
                } else if resolved == RESOLVED_ASCII_STR {
                    Some(PrimitiveArgumentLayout::Ascii)
                } else if resolved == RESOLVED_UTF8_STR {
                    Some(PrimitiveArgumentLayout::UTF8)
                } else {
                    None
                }
            } else {
                None
            }
        }
    })
}

fn check_receving_input(receiving: &T::ReceivingInput) -> Result<(), ExecutionError> {
    let T::ReceivingInput {
        original_input_index: _,
        object_ref: _,
        ty,
        constraint,
    } = receiving;
    let BytesConstraint { command, argument } = constraint;
    check_receiving(*argument, ty).map_err(|e| e.with_command_index(*command as usize))
}

fn check_receiving(command_arg_idx: u16, constraint: &Type) -> Result<(), ExecutionError> {
    if is_valid_receiving(constraint) {
        Ok(())
    } else {
        Err(command_argument_error(
            CommandArgumentError::TypeMismatch,
            command_arg_idx as usize,
        ))
    }
}

pub fn is_valid_pure_type(constraint: &Type) -> Result<bool, ExecutionError> {
    Ok(primitive_serialization_layout(constraint)?.is_some())
}

/// Returns true if a type is a `Receiving<t>` where `t` has `key`
pub fn is_valid_receiving(constraint: &Type) -> bool {
    let Type::Datatype(dt) = constraint else {
        return false;
    };
    dt.qualified_ident() == RESOLVED_RECEIVING_STRUCT
        && dt.type_arguments.len() == 1
        && dt.type_arguments[0].abilities().has_key()
}

//**************************************************************************************************
// Object usage
//**************************************************************************************************

fn command(context: &mut Context, sp!(_, c): &T::Command) -> Result<(), ExecutionError> {
    match &c.command {
        T::Command__::MoveCall(mc) => {
            check_obj_usages(context, &mc.arguments)?;
            check_gas_by_values(&mc.arguments)?;
        }
        T::Command__::TransferObjects(objects, recipient) => {
            check_obj_usages(context, objects)?;
            check_obj_usage(context, recipient)?;
            // gas can be used by value in TransferObjects
        }
        T::Command__::SplitCoins(_, coin, amounts) => {
            check_obj_usage(context, coin)?;
            check_obj_usages(context, amounts)?;
            check_gas_by_value(coin)?;
            check_gas_by_values(amounts)?;
        }
        T::Command__::MergeCoins(_, target, coins) => {
            check_obj_usage(context, target)?;
            check_obj_usages(context, coins)?;
            check_gas_by_value(target)?;
            check_gas_by_values(coins)?;
        }
        T::Command__::MakeMoveVec(_, xs) => {
            check_obj_usages(context, xs)?;
            check_gas_by_values(xs)?;
        }
        T::Command__::Publish(_, _, _) => (),
        T::Command__::Upgrade(_, _, _, x, _) => {
            check_obj_usage(context, x)?;
            check_gas_by_value(x)?;
        }
    }
    Ok(())
}

// Checks for valid by-mut-ref and by-value usage of input objects
fn check_obj_usages(
    context: &mut Context,
    arguments: &[T::Argument],
) -> Result<(), ExecutionError> {
    for arg in arguments {
        check_obj_usage(context, arg)?;
    }
    Ok(())
}

fn check_obj_usage(context: &mut Context, arg: &T::Argument) -> Result<(), ExecutionError> {
    match &arg.value.0 {
        T::Argument__::Borrow(true, l) => check_obj_by_mut_ref(context, arg.idx, l),
        T::Argument__::Use(T::Usage::Move(l)) => check_by_value(context, arg.idx, l),
        // We do not care about
        // - immutable object borrowing
        // - copying/read ref (since you cannot copy objects)
        // - freeze (since an input object cannot be a reference without a borrow)
        T::Argument__::Borrow(false, _)
        | T::Argument__::Use(T::Usage::Copy { .. })
        | T::Argument__::Read(_)
        | T::Argument__::Freeze(_) => Ok(()),
    }
}

// Checks for valid by-mut-ref usage of input objects
fn check_obj_by_mut_ref(
    context: &mut Context,
    arg_idx: u16,
    location: &T::Location,
) -> Result<(), ExecutionError> {
    match location {
        T::Location::PureInput(_)
        | T::Location::ReceivingInput(_)
        | T::Location::TxContext
        | T::Location::GasCoin
        | T::Location::Result(_, _) => Ok(()),
        T::Location::ObjectInput(idx) => {
            if !context.objects[*idx as usize].allow_by_mut_ref {
                Err(command_argument_error(
                    CommandArgumentError::InvalidObjectByMutRef,
                    arg_idx as usize,
                ))
            } else {
                Ok(())
            }
        }
    }
}

// Checks for valid by-value usage of input objects
fn check_by_value(
    context: &mut Context,
    arg_idx: u16,
    location: &T::Location,
) -> Result<(), ExecutionError> {
    match location {
        T::Location::GasCoin
        | T::Location::Result(_, _)
        | T::Location::TxContext
        | T::Location::PureInput(_)
        | T::Location::ReceivingInput(_) => Ok(()),
        T::Location::ObjectInput(idx) => {
            if !context.objects[*idx as usize].allow_by_value {
                Err(command_argument_error(
                    CommandArgumentError::InvalidObjectByValue,
                    arg_idx as usize,
                ))
            } else {
                Ok(())
            }
        }
    }
}

// Checks for no by value usage of gas
fn check_gas_by_values(arguments: &[T::Argument]) -> Result<(), ExecutionError> {
    for arg in arguments {
        check_gas_by_value(arg)?;
    }
    Ok(())
}

fn check_gas_by_value(arg: &T::Argument) -> Result<(), ExecutionError> {
    match &arg.value.0 {
        T::Argument__::Use(T::Usage::Move(l)) => check_gas_by_value_loc(arg.idx, l),
        // We do not care about the read/freeze case since they cannot move an object input
        T::Argument__::Borrow(_, _)
        | T::Argument__::Use(T::Usage::Copy { .. })
        | T::Argument__::Read(_)
        | T::Argument__::Freeze(_) => Ok(()),
    }
}

fn check_gas_by_value_loc(idx: u16, location: &T::Location) -> Result<(), ExecutionError> {
    match location {
        T::Location::GasCoin => Err(command_argument_error(
            CommandArgumentError::InvalidGasCoinUsage,
            idx as usize,
        )),
        T::Location::TxContext
        | T::Location::ObjectInput(_)
        | T::Location::PureInput(_)
        | T::Location::ReceivingInput(_)
        | T::Location::Result(_, _) => Ok(()),
    }
}