forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[move] Add additional enums tests (MystenLabs#18567)
## Description Adds a number of additional tests for enums. These tests test a couple things: * All variations of "unpack wrong variant at runtime" (by value, mut ref, and immut ref). * Various incarnations of this (e.g., all switch arms branching to the same branch). * Various reference tests around unpacking enum variants. ## Test plan It's all tests!
- Loading branch information
Showing
20 changed files
with
1,539 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...es/bytecode-verifier-transactional-tests/tests/locals_safety/enum_non_drop_assignment.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
processed 2 tasks | ||
|
||
task 0 'publish'. lines 1-22: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::Enums'. Got VMError: { | ||
major_status: WRITEREF_WITHOUT_DROP_ABILITY, | ||
sub_status: None, | ||
location: 0x1::Enums, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 10)], | ||
} | ||
|
||
task 1 'publish'. lines 24-47: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::Enums'. Got VMError: { | ||
major_status: WRITEREF_WITHOUT_DROP_ABILITY, | ||
sub_status: None, | ||
location: 0x1::Enums, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 10)], | ||
} |
47 changes: 47 additions & 0 deletions
47
...s/bytecode-verifier-transactional-tests/tests/locals_safety/enum_non_drop_assignment.mvir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//# publish | ||
module 0x1.Enums { | ||
enum PolyEnumWithTwoVariants<T> { | ||
One { }, | ||
Two { x: T } | ||
} | ||
|
||
public g<T>(t: T, x2: T): T { | ||
let e: Self.PolyEnumWithTwoVariants<T>; | ||
let x: &mut T; | ||
label b0: | ||
e = PolyEnumWithTwoVariants.Two<T> { x: move(t) }; | ||
variant_switch PolyEnumWithTwoVariants (&e) { | ||
One : b1, | ||
Two : b1, | ||
}; | ||
label b1: | ||
&mut PolyEnumWithTwoVariants.Two<T> { x: x } = &mut e; | ||
*move(x) = move(x2); | ||
abort 0; | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x1.Enums { | ||
struct X { b: bool } | ||
|
||
enum MonoEnumWithTwoVariants { | ||
One { }, | ||
Two { x: Self.X } | ||
} | ||
|
||
public g(t: Self.X, x2: Self.X): Self.X { | ||
let e: Self.MonoEnumWithTwoVariants; | ||
let x: &mut Self.X; | ||
label b0: | ||
e = MonoEnumWithTwoVariants.Two { x: move(t) }; | ||
variant_switch MonoEnumWithTwoVariants (&e) { | ||
One : b1, | ||
Two : b1, | ||
}; | ||
label b1: | ||
&mut MonoEnumWithTwoVariants.Two { x: x } = &mut e; | ||
*move(x) = move(x2); | ||
abort 0; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...rates/bytecode-verifier-transactional-tests/tests/reference_safety/double_enum_unpack.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
processed 1 task |
30 changes: 30 additions & 0 deletions
30
...ates/bytecode-verifier-transactional-tests/tests/reference_safety/double_enum_unpack.mvir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//# publish | ||
module 0x1.o { | ||
enum X has drop { | ||
One { x: u64, y: u64 }, | ||
Two { x: u64, y: u64}, | ||
} | ||
|
||
a(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let y: &mut u64; | ||
label b0: | ||
&mut X.One { x: x, y: y} = copy(e); | ||
&mut X.Two { x: x, y: y} = move(e); | ||
return; | ||
} | ||
|
||
b(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let x2: &mut u64; | ||
let y: &mut u64; | ||
label b0: | ||
&mut X.One { x: x, y: y} = copy(e); | ||
*move(y) = 0; | ||
&mut X.One { x: x2, y: y} = move(e); | ||
// x and x2 are aliases of each other | ||
*move(x) = 0; | ||
*move(x2) = 0; | ||
return; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ates/bytecode-verifier-transactional-tests/tests/reference_safety/enum_variant_factor.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
processed 7 tasks | ||
|
||
task 0 'publish'. lines 1-17: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 5)], | ||
} | ||
|
||
task 1 'publish'. lines 19-36: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 3)], | ||
} | ||
|
||
task 2 'publish'. lines 38-54: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 3)], | ||
} | ||
|
||
task 3 'publish'. lines 56-74: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 5)], | ||
} | ||
|
||
task 5 'publish'. lines 96-118: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::invalid'. Got VMError: { | ||
major_status: WRITEREF_EXISTS_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::invalid, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 21)], | ||
} | ||
|
||
task 6 'publish'. lines 120-142: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::invalid'. Got VMError: { | ||
major_status: READREF_EXISTS_MUTABLE_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::invalid, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 17)], | ||
} |
142 changes: 142 additions & 0 deletions
142
...tes/bytecode-verifier-transactional-tests/tests/reference_safety/enum_variant_factor.mvir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
//# publish | ||
module 0x1.o { | ||
enum X has drop { | ||
Empty { }, | ||
One { x: u64, y: u64 }, | ||
} | ||
|
||
f(e: &mut Self.X) { | ||
let a1: &mut Self.X; | ||
let a2: &mut Self.X; | ||
label b0: | ||
a1 = copy(e); | ||
a2 = copy(e); | ||
&mut X.Empty{} = move(a2); | ||
return; | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x1.o { | ||
enum X has drop { | ||
One { x: u64, y: u64 }, | ||
Two { x: u64, y: u64}, | ||
} | ||
|
||
c(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let y: &mut u64; | ||
let a1: &mut Self.X; | ||
label b0: | ||
a1 = copy(e); | ||
&mut X.One { x: x, y: y} = copy(e); | ||
return; | ||
} | ||
|
||
} | ||
|
||
//# publish | ||
module 0x1.o { | ||
enum X has drop { | ||
One { x: u64, y: u64 }, | ||
Two { x: u64, y: u64}, | ||
} | ||
|
||
c(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let y: &mut u64; | ||
let a1: &mut Self.X; | ||
label b0: | ||
a1 = copy(e); | ||
&mut X.One { x: x, y: y} = move(e); | ||
return; | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x1.o { | ||
enum X has drop { | ||
One { x: u64, y: u64 }, | ||
Two { x: u64, y: u64}, | ||
} | ||
|
||
c(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let y: &mut u64; | ||
let a1: &mut Self.X; | ||
let a2: &mut Self.X; | ||
label b0: | ||
a1 = copy(e); | ||
a2 = copy(e); | ||
&mut X.One { x: x, y: y} = move(a2); | ||
return; | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x1.o { | ||
enum X has drop { | ||
One { x: u64, y: u64 }, | ||
Two { x: u64, y: u64}, | ||
} | ||
|
||
c(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let y: &mut u64; | ||
let a1: &mut Self.X; | ||
let a2: &mut Self.X; | ||
label b0: | ||
a1 = copy(e); | ||
a2 = copy(e); | ||
&mut X.One { x: x, y: y} = move(a1); | ||
return; | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x1.invalid { | ||
enum X has drop { | ||
One { x: u64 }, | ||
Two { x: u64, y: u64}, | ||
} | ||
|
||
h(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let y: &mut u64; | ||
let counter: &mut u64; | ||
label b0: | ||
&mut X.Two { x: x, y: y} = copy(e); | ||
*move(x) = 1; | ||
*copy(y) = 3; | ||
counter = move(y); | ||
&mut X.Two { x: x, y: y} = copy(e); | ||
// Error: Invalid to write to `y` since it's factored to `counter` which is still a live mutable reference | ||
*copy(y) = *copy(counter) - 1; | ||
counter = move(y); | ||
return; | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x1.invalid { | ||
enum X has drop { | ||
One { x: u64 }, | ||
Two { x: u64, y: u64}, | ||
} | ||
|
||
h(e: &mut Self.X) { | ||
let x: &mut u64; | ||
let y: &mut u64; | ||
let counter: &mut u64; | ||
label b0: | ||
&mut X.Two { x: x, y: y} = copy(e); | ||
*move(x) = 1; | ||
*copy(y) = 3; | ||
counter = move(y); | ||
&mut X.Two { x: x, y: y} = copy(e); | ||
// Error: Invalid to read `y` since it's factored to `counter` which is still a live mutable reference | ||
*copy(y) = *copy(y) - 1; | ||
counter = move(y); | ||
return; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../bytecode-verifier-transactional-tests/tests/reference_safety/invalid_enum_ref_unpack.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
processed 7 tasks | ||
|
||
task 0 'publish'. lines 1-20: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: WRITEREF_EXISTS_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 13)], | ||
} | ||
|
||
task 1 'publish'. lines 22-47: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: CALL_BORROWED_MUTABLE_REFERENCE_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 11)], | ||
} | ||
|
||
task 2 'publish'. lines 49-68: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: WRITEREF_EXISTS_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 10)], | ||
} | ||
|
||
task 3 'publish'. lines 70-95: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: CALL_BORROWED_MUTABLE_REFERENCE_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 8)], | ||
} | ||
|
||
task 4 'publish'. lines 97-114: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: WRITEREF_EXISTS_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 10)], | ||
} | ||
|
||
task 5 'publish'. lines 116-134: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: WRITEREF_EXISTS_BORROW_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 10)], | ||
} | ||
|
||
task 6 'publish'. lines 136-160: | ||
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: { | ||
major_status: CALL_BORROWED_MUTABLE_REFERENCE_ERROR, | ||
sub_status: None, | ||
location: 0x1::o, | ||
indices: [(FunctionDefinition, 0)], | ||
offsets: [(FunctionDefinitionIndex(0), 8)], | ||
} |
Oops, something went wrong.