Skip to content

Commit

Permalink
Check the illegal type and some types of values in the data_struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Jun 12, 2024
1 parent e1d222c commit 44ae271
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
processed 1 task

task 0 'publish'. lines 1-11:
status ABORTED with code 10016 in 0000000000000000000000000000000000000000000000000000000000000002::move_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//# publish
module 0x11.TestModule1 {
// error code 10016: INVALID_DATA_STRUCT_WITH_TYPE_PARAMETER
struct S1<phantom T> has drop,copy {v: u32}

metadata {
data_struct {
0x11::TestModule1::S1 -> true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
processed 1 task

task 0 'publish'. lines 1-11:
status ABORTED with code 10016 in 0000000000000000000000000000000000000000000000000000000000000002::move_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//# publish
module 0x11.TestModule1 {
// error code 10016: INVALID_DATA_STRUCT_WITH_TYPE_PARAMETER
struct S1<T> has drop,copy {v: u32}

metadata {
data_struct {
0x11::TestModule1::S1 -> true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
processed 1 task

task 0 'publish'. lines 1-14:
status ABORTED with code 10001 in 0000000000000000000000000000000000000000000000000000000000000002::move_module
status ABORTED with code 10018 in 0000000000000000000000000000000000000000000000000000000000000002::move_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
processed 2 tasks

task 0 'publish'. lines 1-15:
status EXECUTED

task 1 'publish'. lines 17-43:
status ABORTED with code 10018 in 0000000000000000000000000000000000000000000000000000000000000002::move_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//# publish
module 0x11.TestModule0 {
struct S0 has copy,drop { x: u64 }

metadata {
data_struct {
0x11::TestModule0::S0 -> true;
}
}

public new(): Self.S0 {
label b0:
return S0{ x: 123 };
}
}

//# publish
module 0x11.TestModule1 {
import 0x11.TestModule0;
metadata {
data_struct_func {
// error code 10018: INVALID_DATA_STRUCT_FUNC_WITH_EMPTY_PARAM_LIST
0x11::TestModule1::f1 -> [];
}
}

public f1<T1: drop, T2: drop>(arg1: T1, arg2: T2) {
label b0:
_ = move(arg1);
_ = move(arg2);
return;
}

public f2() {
let s0: TestModule0.S0;
let s1: TestModule0.S0;
label b0:
s0 = TestModule0.new();
s1 = TestModule0.new();
Self.f1<TestModule0.S0, TestModule0.S0>(move(s0), move(s1));
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
processed 1 task

task 0 'publish'. lines 1-39:
status ABORTED with code 10009 in 0000000000000000000000000000000000000000000000000000000000000002::move_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//# publish
module 0x11.TestModule1 {
import 0x1.option;
import 0x1.vector;
struct S0 has copy,drop { x: u64 }

metadata {
data_struct {
0x11::TestModule1::S0 -> true;
}
data_struct_func {
0x11::TestModule1::f1 -> [0, 1];
}
}

public new(): Self.S0 {
label b0:
return S0{ x: 123 };
}

public f1<T1: drop, T2: drop>(arg1: T1, arg2: T2) {
label b0:
_ = move(arg1);
_ = move(arg2);
return;
}

public f2<T: copy+drop>(arg: T) {
let s0: Self.S0;
let s1: option.Option<T>;

label b0:
s0 = Self.new();
s1 = option.none<T>();
// error code 10009: INVALID_DATA_STRUCT_TYPE
Self.f1<Self.S0, option.Option<T>>(move(s0), move(s1));
return;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
processed 1 task

task 0 'publish'. lines 1-39:
task 0 'publish'. lines 1-38:
status EXECUTED
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module 0x11.TestModule1 {
label b0:
s0 = Self.new();
s1 = option.some<u32>(123u32);
// error code 10009: INVALID_DATA_STRUCT_TYPE
Self.f1<Self.S0, option.Option<u32>>(move(s0), move(s1));
return;
}
Expand Down
1 change: 1 addition & 0 deletions moveos/moveos-verifier/src/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub enum ErrorCode {
INVALID_DATA_STRUCT_NOT_IN_MODULE_METADATA = 10015,
INVALID_DATA_STRUCT_WITH_TYPE_PARAMETER = 10016,
INVALID_DATA_STRUCT_OPTION_WITHOUT_TYPE_PARAMETER = 10017,
INVALID_DATA_STRUCT_FUNC_WITH_EMPTY_PARAM_LIST = 10018,

INVALID_ENTRY_FUNC_SIGNATURE = 11000,
INVALID_PARAM_TYPE_ENTRY_FUNCTION = 11001,
Expand Down
17 changes: 16 additions & 1 deletion moveos/moveos-verifier/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,16 @@ where
}
}

for (full_func_name, _) in data_structs_func_map.iter() {
for (full_func_name, type_param_list) in data_structs_func_map.iter() {
if type_param_list.is_empty() {
return generate_vm_error(
ErrorCode::INVALID_DATA_STRUCT_FUNC_WITH_EMPTY_PARAM_LIST,
format!("Function {} with empty type param list", full_func_name,),
None,
caller_module,
);
}

check_module_owner(full_func_name, caller_module)?;
let (exists, _) = check_if_function_exist_in_module(caller_module, full_func_name);
if !exists {
Expand Down Expand Up @@ -1262,6 +1271,12 @@ where
return (false, ErrorCode::INVALID_DATA_STRUCT_WITHOUT_COPY_ABILITY);
}

// We don't allow the type parameter for the struct definition.
let struct_type_parameters = struct_handle.type_parameters.clone();
if !struct_type_parameters.is_empty() {
return (false, ErrorCode::INVALID_DATA_STRUCT_WITH_TYPE_PARAMETER);
}

let field_count = struct_def.declared_field_count().unwrap();
for idx in (0..field_count).by_ref() {
let struct_field_def_opt = struct_def.field(idx as usize);
Expand Down

0 comments on commit 44ae271

Please sign in to comment.