Skip to content

Commit

Permalink
[move][adapter] Improve type argument errors (MystenLabs#6286)
Browse files Browse the repository at this point in the history
* [move][adapter] Improve type argument errors

- A missing type in a module currently throws an invariant violation from the Move VM, this catches that error and produces a better error
  • Loading branch information
tnowacki authored Nov 22, 2022
1 parent 0e65506 commit e92dc67
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ task 4 'run'. lines 39-47:
written: object(107)

task 5 'run'. lines 49-49:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Type mismatch.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Type mismatch.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: TypeMismatch }), source: Some("Unexpected non-ASCII value (outside of ASCII character range) in argument 0") } }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
processed 7 tasks

init:
A: object(100)

task 1 'publish'. lines 8-15:
created: object(104)
written: object(103)

task 2 'run'. lines 17-17:
Error: Transaction Effects Status: Entry Type Argument Error. Error for type argument at index 0: A package (or module) in the type argument was not found
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryTypeArgumentError(EntryTypeArgumentError { argument_idx: 0, kind: ModuleNotFound }), source: None } }

task 3 'run'. lines 19-19:
Error: Transaction Effects Status: Entry Type Argument Error. Error for type argument at index 0: A type was not found in the module specified
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryTypeArgumentError(EntryTypeArgumentError { argument_idx: 0, kind: TypeNotFound }), source: None } }

task 4 'run'. lines 21-21:
Error: Transaction Effects Status: Entry Type Argument Error. Error for type argument at index 0: Mismatch between the number of actual versus expected type arguments.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryTypeArgumentError(EntryTypeArgumentError { argument_idx: 0, kind: ArityMismatch }), source: None } }

task 5 'run'. lines 23-23:
Error: Transaction Effects Status: Entry Type Argument Error. Error for type argument at index 0: Mismatch between the number of actual versus expected type arguments.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryTypeArgumentError(EntryTypeArgumentError { argument_idx: 0, kind: ArityMismatch }), source: None } }

task 6 'run'. lines 25-25:
Error: Transaction Effects Status: Entry Type Argument Error. Error for type argument at index 0: A type provided did not match the specified constraints.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryTypeArgumentError(EntryTypeArgumentError { argument_idx: 0, kind: ConstraintNotSatisfied }), source: None } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// tests invalid type args

//# init --addresses test=0x0 --accounts A

//# publish
module test::m {

struct S<phantom T: copy> {}

entry fun foo<T>() {}

}

//# run test::m::foo --type-args test::x::x

//# run test::m::foo --type-args test::m::SUI

//# run test::m::foo --type-args test::m::S

//# run test::m::foo --type-args test::m::S<u64,u8>

//# run test::m::foo --type-args test::m::S<signer>
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ created: object(116)
written: object(115)

task 9 'run'. lines 140-143:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Type mismatch.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Type mismatch.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: TypeMismatch }), source: Some("Expected argument of type Test::M::Obj, but found type Test::M::AnotherObj") } }

task 10 'run'. lines 145-145:
Expand All @@ -47,15 +47,15 @@ created: object(121)
written: object(120)

task 12 'run'. lines 149-152:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Type mismatch.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Type mismatch.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: TypeMismatch }), source: Some("Expected argument of type Test::M::Obj, but found type Test::M::AnotherObj") } }

task 13 'run'. lines 154-154:
created: object(124)
written: object(123)

task 14 'run'. lines 156-159:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: InvalidObjectByValue }), source: Some("Immutable and shared objects cannot be passed by-value, violation found in argument 0") } }

task 15 'run'. lines 161-161:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ created: object(115)
written: object(114)

task 8 'run'. lines 137-140:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Type mismatch.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Type mismatch.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: TypeMismatch }), source: Some("Expected argument of type Test::M::ObjAny<T0>, but found type Test::M::AnotherObjAny<Test::M::Any>") } }

task 9 'run'. lines 142-142:
Expand All @@ -44,15 +44,15 @@ created: object(120)
written: object(119)

task 11 'run'. lines 146-149:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Type mismatch.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Type mismatch.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: TypeMismatch }), source: Some("Expected argument of type Test::M::ObjAny<T0>, but found type Test::M::AnotherObjAny<Test::M::Any>") } }

task 12 'run'. lines 151-151:
created: object(123)
written: object(122)

task 13 'run'. lines 153-156:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: InvalidObjectByValue }), source: Some("Immutable and shared objects cannot be passed by-value, violation found in argument 0") } }

task 14 'run'. lines 158-158:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Version: 1
Contents: t2::o2::Obj2 {id: sui::object::UID {id: sui::object::ID {bytes: fake(107)}}}

task 5 'run'. lines 45-45:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: InvalidObjectByValue }), source: Some("Immutable and shared objects cannot be passed by-value, violation found in argument 0") } }

task 6 'run'. lines 47-47:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: InvalidObjectByValue }), source: Some("Immutable and shared objects cannot be passed by-value, violation found in argument 0") } }
Empty file.
4 changes: 2 additions & 2 deletions crates/sui-adapter-transactional-tests/tests/sui/freeze.exp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ task 3 'run'. lines 74-74:
written: object(106), object(107)

task 4 'run'. lines 76-76:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Immutable and shared objects cannot be passed by-value.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: InvalidObjectByValue }), source: Some("Immutable and shared objects cannot be passed by-value, violation found in argument 0") } }

task 5 'run'. lines 78-78:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 0: Immutable objects cannot be passed by mutable reference, &mut.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 0: Immutable objects cannot be passed by mutable reference, &mut.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 0, kind: InvalidObjectByMuteRef }), source: Some("Argument 0 is expected to be mutable, immutable object found") } }
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ created: object(103)
written: object(102)

task 2 'run'. lines 14-16:
Error: Transaction Effects Status: Entry Argument Type Error. Error for argument at index 1: Mismatch between the number of actual versus expected argument.
Error: Transaction Effects Status: Entry Argument Error. Error for argument at index 1: Mismatch between the number of actual versus expected arguments.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: EntryArgumentError(EntryArgumentError { argument_idx: 1, kind: ArityMismatch }), source: Some("Expected 2 arguments calling function 'create', but found 1") } }

task 3 'run'. lines 17-17:
Expand Down
26 changes: 24 additions & 2 deletions crates/sui-adapter/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ use linked_hash_map::LinkedHashMap;
use move_binary_format::{
access::ModuleAccess,
binary_views::BinaryIndexedView,
errors::VMResult,
file_format::{AbilitySet, CompiledModule, LocalIndex, SignatureToken, StructHandleIndex},
errors::{VMError, VMResult},
file_format::{
AbilitySet, CompiledModule, LocalIndex, SignatureToken, StructHandleIndex,
TypeParameterIndex,
},
};
use move_bytecode_verifier::VerifierConfig;
use move_core_types::{
Expand Down Expand Up @@ -205,6 +208,12 @@ fn execute_internal<
.map(|(id, (owner, _))| (*id, (by_value_objects.contains(id), *owner)))
.collect();
let mut session = new_session(vm, state_view, input_objects);
// check type arguments separately for error conversion
for (idx, ty) in type_args.iter().enumerate() {
session
.load_type(ty)
.map_err(|e| convert_type_argument_error(idx, e))?;
}
// script visibility checked manually for entry points
let (
SerializedReturnValues {
Expand Down Expand Up @@ -1372,3 +1381,16 @@ fn missing_unwrapped_msg(id: &ObjectID) -> String {
id
)
}

fn convert_type_argument_error(idx: usize, error: VMError) -> ExecutionError {
use move_core_types::vm_status::StatusCode;
use sui_types::messages::EntryTypeArgumentErrorKind;
let kind = match error.major_status() {
StatusCode::LINKER_ERROR => EntryTypeArgumentErrorKind::ModuleNotFound,
StatusCode::TYPE_RESOLUTION_FAILURE => EntryTypeArgumentErrorKind::TypeNotFound,
StatusCode::NUMBER_OF_TYPE_ARGUMENTS_MISMATCH => EntryTypeArgumentErrorKind::ArityMismatch,
StatusCode::CONSTRAINT_NOT_SATISFIED => EntryTypeArgumentErrorKind::ConstraintNotSatisfied,
_ => return error.into(),
};
ExecutionErrorKind::entry_type_argument_error(idx as TypeParameterIndex, kind).into()
}
5 changes: 3 additions & 2 deletions crates/sui-core/src/generate_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use sui_types::{
AuthoritySignature, KeypairTraits, Signature,
},
messages::{
CallArg, EntryArgumentErrorKind, ExecutionFailureStatus, ExecutionStatus, ObjectArg,
ObjectInfoRequestKind, SingleTransactionKind, TransactionKind,
CallArg, EntryArgumentErrorKind, EntryTypeArgumentErrorKind, ExecutionFailureStatus,
ExecutionStatus, ObjectArg, ObjectInfoRequestKind, SingleTransactionKind, TransactionKind,
},
object::{Data, Owner},
storage::DeleteKind,
Expand Down Expand Up @@ -77,6 +77,7 @@ fn get_registry() -> Result<Registry> {
tracer.trace_type::<ExecutionFailureStatus>(&samples)?;
tracer.trace_type::<AbortLocation>(&samples)?;
tracer.trace_type::<EntryArgumentErrorKind>(&samples)?;
tracer.trace_type::<EntryTypeArgumentErrorKind>(&samples)?;
tracer.trace_type::<CallArg>(&samples)?;
tracer.trace_type::<ObjectArg>(&samples)?;
tracer.trace_type::<Data>(&samples)?;
Expand Down
45 changes: 32 additions & 13 deletions crates/sui-core/tests/staged/sui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ EntryArgumentErrorKind:
UnsupportedPureArg: UNIT
5:
ArityMismatch: UNIT
EntryTypeArgumentError:
STRUCT:
- argument_idx: U16
- kind:
TYPENAME: EntryTypeArgumentErrorKind
EntryTypeArgumentErrorKind:
ENUM:
0:
ModuleNotFound: UNIT
1:
TypeNotFound: UNIT
2:
ArityMismatch: UNIT
3:
ConstraintNotSatisfied: UNIT
Envelope:
STRUCT:
- data:
Expand Down Expand Up @@ -167,53 +182,57 @@ ExecutionFailureStatus:
NEWTYPE:
TYPENAME: EntryArgumentError
18:
EntryTypeArgumentError:
NEWTYPE:
TYPENAME: EntryTypeArgumentError
19:
CircularObjectOwnership:
NEWTYPE:
TYPENAME: CircularObjectOwnership
19:
20:
InvalidChildObjectArgument:
NEWTYPE:
TYPENAME: InvalidChildObjectArgument
20:
21:
InvalidSharedByValue:
NEWTYPE:
TYPENAME: InvalidSharedByValue
21:
22:
TooManyChildObjects:
STRUCT:
- object:
TYPENAME: ObjectID
22:
23:
InvalidParentDeletion:
STRUCT:
- parent:
TYPENAME: ObjectID
- kind:
OPTION:
TYPENAME: DeleteKind
23:
24:
InvalidParentFreezing:
STRUCT:
- parent:
TYPENAME: ObjectID
24:
PublishErrorEmptyPackage: UNIT
25:
PublishErrorNonZeroAddress: UNIT
PublishErrorEmptyPackage: UNIT
26:
PublishErrorDuplicateModule: UNIT
PublishErrorNonZeroAddress: UNIT
27:
SuiMoveVerificationError: UNIT
PublishErrorDuplicateModule: UNIT
28:
MovePrimitiveRuntimeError: UNIT
SuiMoveVerificationError: UNIT
29:
MovePrimitiveRuntimeError: UNIT
30:
MoveAbort:
TUPLE:
- TYPENAME: ModuleId
- U64
30:
VMVerificationOrDeserializationError: UNIT
31:
VMVerificationOrDeserializationError: UNIT
32:
VMInvariantViolation: UNIT
ExecutionStatus:
ENUM:
Expand Down
Loading

0 comments on commit e92dc67

Please sign in to comment.