forked from FuelLabs/sway
-
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.
Testing and fixing CEI pattern analysis (follow up for FuelLabs#3168) (…
…FuelLabs#3356) ## Test cases - [x] `if` with interaction in the condition and storage in a branch - [x] `match` with interaction in the condition and storage in branches - [x] Pair or struct with interaction in one component and storage in a subsequent component ### `while`-loops tests - [x] `while` loop with effects in the condition or the body - [x] `while` loop with interaction-then-storage - [x] `while` loop with storage-then-interaction (this is a violation too because on the next loop iteration it will be interaction-then-storage) -- this requires a fix implemented in this PR ### Function application tests - [x] Pure function + argument is a code block with CEI violation - [x] Pure function + first argument does interaction + second argument does storage effect - [x] Storage reading/writing function + argument does interaction - [x] Improved error reporting precision for function application - [x] Left-to-right arguments evaluation Closes FuelLabs#3342
1 parent
4fe5a38
commit 88b4cc5
Showing
54 changed files
with
725 additions
and
19 deletions.
There are no files selected for viewing
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
8 changes: 8 additions & 0 deletions
8
..._vm_tests/test_programs/should_pass/language/left_to_right_func_args_evaluation/Forc.lock
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,8 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-88DBE1D69186ED7B' | ||
|
||
[[package]] | ||
name = 'left_to_right_arg_evaluation' | ||
source = 'member' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
..._vm_tests/test_programs/should_pass/language/left_to_right_func_args_evaluation/Forc.toml
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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "left_to_right_arg_evaluation" | ||
entry = "main.sw" | ||
|
||
[dependencies] | ||
core = { path = "../../../../../../../sway-lib-core" } |
22 changes: 22 additions & 0 deletions
22
...est_programs/should_pass/language/left_to_right_func_args_evaluation/json_abi_oracle.json
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,22 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"inputs": [], | ||
"name": "main", | ||
"output": { | ||
"name": "", | ||
"type": 0, | ||
"typeArguments": null | ||
} | ||
} | ||
], | ||
"loggedTypes": [], | ||
"types": [ | ||
{ | ||
"components": null, | ||
"type": "bool", | ||
"typeId": 0, | ||
"typeParameters": null | ||
} | ||
] | ||
} |
75 changes: 75 additions & 0 deletions
75
...m_tests/test_programs/should_pass/language/left_to_right_func_args_evaluation/src/main.sw
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,75 @@ | ||
script; | ||
// This tests function, tuple, struct arguments are evaluated from left to right | ||
|
||
fn func(a: u64, b: u64, c: u64, d: u64) -> u64 { | ||
d | ||
} | ||
|
||
struct MyStruct { | ||
a: u64, | ||
b: u64, | ||
c: u64, | ||
d: u64, | ||
} | ||
|
||
fn main() -> bool { | ||
let mut x: u64 = 0; | ||
|
||
// function arguments evaluation | ||
let func_res = | ||
func( | ||
{ | ||
x = 1; | ||
0 | ||
}, | ||
{ | ||
x = 2; | ||
0 | ||
}, | ||
{ | ||
x = 3; | ||
0 | ||
}, | ||
x | ||
); | ||
|
||
// tuple evaluation | ||
x = 0; | ||
let tuple_res = | ||
( | ||
{ | ||
x = 1; | ||
0 | ||
}, | ||
{ | ||
x = 2; | ||
1 | ||
}, | ||
{ | ||
x = 3; | ||
2 | ||
}, | ||
x | ||
); | ||
|
||
// struct evaluation | ||
x = 0; | ||
let struct_res = | ||
MyStruct { | ||
a: { | ||
x = 1; | ||
0 | ||
}, | ||
b: { | ||
x = 2; | ||
1 | ||
}, | ||
c: { | ||
x = 3; | ||
2 | ||
}, | ||
d: x | ||
}; | ||
|
||
return (func_res == 3) && (tuple_res.3 == 3) && (struct_res.d == 3); | ||
} |
3 changes: 3 additions & 0 deletions
3
..._vm_tests/test_programs/should_pass/language/left_to_right_func_args_evaluation/test.toml
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,3 @@ | ||
category = "run" | ||
expected_result = { action = "return", value = 1 } | ||
validate_abi = true |
13 changes: 13 additions & 0 deletions
13
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-1/Forc.lock
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,13 @@ | ||
[[package]] | ||
name = 'cei_pattern_violation_in_func_app-1' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-CC4CE5C392D95953' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-CC4CE5C392D95953' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-1/Forc.toml
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,8 @@ | ||
[project] | ||
name = "cei_pattern_violation_in_func_app-1" | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
28 changes: 28 additions & 0 deletions
28
...test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-1/src/main.sw
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,28 @@ | ||
contract; | ||
|
||
use std::storage::store; | ||
|
||
abi TestAbi { | ||
#[storage(write)] | ||
fn deposit(amount: u64); | ||
} | ||
|
||
fn pure_function(x: u64) -> u64 { | ||
x | ||
} | ||
|
||
impl TestAbi for Contract { | ||
#[storage(write)] | ||
fn deposit(amount: u64) { | ||
// the function argument is a code block with CEI pattern violation | ||
pure_function( | ||
{ | ||
// interaction | ||
abi(TestAbi, 0x3dba0a4455b598b7655a7fb430883d96c9527ef275b49739e7b0ad12f8280eae).deposit(amount); | ||
// effect -- therefore violation of CEI where effect should go before interaction | ||
store(0x3dba0a4455b598b7655a7fb430883d96c9527ef275b49739e7b0ad12f8280eae, ()); | ||
42 | ||
} | ||
); | ||
} | ||
} |
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-2/Forc.lock
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,13 @@ | ||
[[package]] | ||
name = 'cei_pattern_violation_in_func_app-2' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-3351E955C8744D1C' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-3351E955C8744D1C' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-2/Forc.toml
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,8 @@ | ||
[project] | ||
name = "cei_pattern_violation_in_func_app-2" | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
33 changes: 33 additions & 0 deletions
33
...test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-2/src/main.sw
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,33 @@ | ||
contract; | ||
|
||
use std::storage::store; | ||
|
||
abi TestAbi { | ||
#[storage(write)] | ||
fn deposit(amount: u64); | ||
} | ||
|
||
fn pure_function(x: u64, y: u64) -> u64 { | ||
x | ||
} | ||
|
||
impl TestAbi for Contract { | ||
#[storage(write)] | ||
fn deposit(amount: u64) { | ||
// 1st function argument is a code block with interaction | ||
// 2nd function argument is a code block with effect | ||
pure_function( | ||
{ | ||
// interaction | ||
abi(TestAbi, 0x3dba0a4455b598b7655a7fb430883d96c9527ef275b49739e7b0ad12f8280eae).deposit(amount); | ||
42 | ||
}, | ||
{ | ||
// effect -- therefore violation of CEI where effect should go before interaction | ||
// (assuming left-to-right function argument evaluation) | ||
store(0x3dba0a4455b598b7655a7fb430883d96c9527ef275b49739e7b0ad12f8280eae, ()); | ||
43 | ||
} | ||
); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-2/test.toml
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,3 @@ | ||
category = "compile" | ||
|
||
# check: $()Storage modification after external contract interaction in function or method "deposit". Consider making all storage writes before calling another contract |
13 changes: 13 additions & 0 deletions
13
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-3/Forc.lock
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,13 @@ | ||
[[package]] | ||
name = 'cei_pattern_violation_in_func_app-3' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-EAF483A8C0425562' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-EAF483A8C0425562' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-3/Forc.toml
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,8 @@ | ||
[project] | ||
name = "cei_pattern_violation_in_func_app-3" | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
28 changes: 28 additions & 0 deletions
28
...test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-3/src/main.sw
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,28 @@ | ||
contract; | ||
|
||
use std::storage::store; | ||
|
||
abi TestAbi { | ||
#[storage(write)] | ||
fn deposit(amount: u64); | ||
} | ||
|
||
#[storage(write)] | ||
fn do_something(x: u64) { | ||
// effect | ||
store(0x3dba0a4455b598b7655a7fb430883d96c9527ef275b49739e7b0ad12f8280eae, ()); | ||
} | ||
|
||
impl TestAbi for Contract { | ||
#[storage(write)] | ||
fn deposit(amount: u64) { | ||
// function's argument is a code block with interaction, function does storage write | ||
do_something( | ||
{ | ||
// interaction | ||
abi(TestAbi, 0x3dba0a4455b598b7655a7fb430883d96c9527ef275b49739e7b0ad12f8280eae).deposit(amount); | ||
42 | ||
}, | ||
); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...s/test_programs/should_pass/static_analysis/cei_pattern_violation_in_func_app-3/test.toml
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,3 @@ | ||
category = "compile" | ||
|
||
# check: $()Storage modification after external contract interaction in function or method "deposit". Consider making all storage writes before calling another contract |
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...st_programs/should_pass/static_analysis/cei_pattern_violation_in_if_statement-1/test.toml
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,3 @@ | ||
category = "compile" | ||
|
||
# check: $()Storage modification after external contract interaction in function or method "deposit". Consider making all storage writes before calling another contract |
13 changes: 13 additions & 0 deletions
13
...st_programs/should_pass/static_analysis/cei_pattern_violation_in_if_statement-2/Forc.lock
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,13 @@ | ||
[[package]] | ||
name = 'cei_pattern_violation_in_if_statement-2' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-CFB5A7C521AD2AFD' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-CFB5A7C521AD2AFD' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
...st_programs/should_pass/static_analysis/cei_pattern_violation_in_if_statement-2/Forc.toml
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,8 @@ | ||
[project] | ||
name = "cei_pattern_violation_in_if_statement-2" | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
Oops, something went wrong.