Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resource loss analysis and definite initialization analysis #1332

Merged
merged 6 commits into from
Jan 5, 2022

Conversation

turbolent
Copy link
Member

Closes #1325

Based heavily on #1329, thank you @SupunS 🙏

Investigating the issue I also discovered that the definite initialization analysis suffered from the same problem.

Description

  • Fix and simplify resource tracking / resource loss detection (82c260a):
    • Consider jumps (break and continue statements) when merging then/else branches' invalidations into the parent
    • Don't record resource invalidations if the code adding the invalidation is unreachable
  • Consider jumps in definite initialization analysis and improve dead code reporting (88b9fbe)
    • In addition to when maybe a return statement occured, initializations should also be ignored when maybe a jump (break or continue statement) occured
    • In addition to when a definite return statement occurs, code is also unreachable if a definite jump (beak or continue statement) occurred before
  • Add tests for resource loss analysis and definite initialization analysis fixes

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@codecov-commenter
Copy link

Codecov Report

Merging #1332 (5d9bef1) into master (834f5f1) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1332      +/-   ##
==========================================
+ Coverage   75.57%   75.59%   +0.02%     
==========================================
  Files         279      279              
  Lines       37813    37837      +24     
==========================================
+ Hits        28578    28604      +26     
+ Misses       7958     7957       -1     
+ Partials     1277     1276       -1     
Flag Coverage Δ
unittests 75.59% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/sema/check_assignment.go 96.10% <100.00%> (+0.02%) ⬆️
runtime/sema/check_block.go 100.00% <100.00%> (ø)
runtime/sema/check_function.go 99.55% <100.00%> (ø)
runtime/sema/check_return_statement.go 100.00% <100.00%> (ø)
runtime/sema/check_switch.go 97.36% <100.00%> (-0.04%) ⬇️
runtime/sema/check_while.go 100.00% <100.00%> (ø)
runtime/sema/checker.go 89.14% <100.00%> (+0.06%) ⬆️
runtime/sema/resources.go 83.95% <100.00%> (ø)
runtime/sema/return_info.go 100.00% <100.00%> (ø)
runtime/sema/resource_invalidations.go 95.12% <0.00%> (+4.87%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 834f5f1...5d9bef1. Read the comment docs.

@github-actions
Copy link

github-actions bot commented Jan 5, 2022

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 834f5f1
The command for i in {1..N}; do go test ./... -run=XXX -bench=. -shuffle=on; done was used.
Bench tests were run a total of 7 times on each branch.

Results

old.txtnew.txt
time/opdelta
RuntimeFungibleTokenTransfer-21.99ms ± 3%1.71ms ±18%~(p=0.181 n=6+7)
RuntimeResourceDictionaryValues-219.4ms ± 2%19.2ms ± 3%~(p=0.318 n=7+7)
ParseDeploy/byte_array-241.4ms ± 3%40.8ms ± 1%~(p=0.051 n=7+6)
ParseDeploy/decode_hex-21.64ms ± 1%1.64ms ± 2%~(p=0.755 n=5+7)
ParseInfix-228.9µs ± 5%28.5µs ± 3%~(p=0.318 n=7+7)
ParseFungibleToken-2552µs ± 2%554µs ± 3%~(p=1.000 n=6+7)
QualifiedIdentifierCreation/Three_levels-2195ns ± 6%194ns ± 4%~(p=0.902 n=7+7)
ContractInterfaceFungibleToken-258.2µs ± 1%58.3µs ± 4%~(p=0.731 n=6+7)
CheckContractInterfaceFungibleTokenConformance-2201µs ± 1%200µs ± 3%~(p=1.000 n=5+6)
InterpretRecursionFib-23.25ms ± 8%3.18ms ± 6%~(p=0.535 n=7+7)
NewInterpreter/new_interpreter-21.40µs ± 2%1.41µs ± 4%~(p=0.557 n=7+7)
NewInterpreter/new_sub-interpreter-22.78µs ± 3%2.73µs ± 2%−1.49%(p=0.041 n=7+7)
QualifiedIdentifierCreation/One_level-23.68ns ± 2%3.61ns ± 1%−1.74%(p=0.002 n=7+7)
ParseArray-227.4ms ± 2%26.7ms ± 3%−2.69%(p=0.038 n=7+7)
 
alloc/opdelta
ContractInterfaceFungibleToken-226.5kB ± 0%26.5kB ± 0%+0.02%(p=0.001 n=7+7)
CheckContractInterfaceFungibleTokenConformance-265.7kB ± 0%65.7kB ± 0%+0.02%(p=0.001 n=7+7)
RuntimeFungibleTokenTransfer-2238kB ± 0%238kB ± 0%~(p=0.689 n=7+7)
RuntimeResourceDictionaryValues-24.04MB ± 0%4.04MB ± 0%~(p=0.456 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
InterpretRecursionFib-21.24MB ± 0%1.24MB ± 0%~(p=0.592 n=7+7)
NewInterpreter/new_interpreter-2720B ± 0%720B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.18kB ± 0%1.18kB ± 0%~(all equal)
 
allocs/opdelta
RuntimeFungibleTokenTransfer-24.54k ± 0%4.54k ± 0%~(p=0.266 n=7+7)
RuntimeResourceDictionaryValues-2102k ± 0%102k ± 0%~(p=0.368 n=7+7)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
ContractInterfaceFungibleToken-2457 ± 0%457 ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-21.07k ± 0%1.07k ± 0%~(all equal)
InterpretRecursionFib-225.0k ± 0%25.0k ± 0%~(all equal)
NewInterpreter/new_interpreter-211.0 ± 0%11.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-236.0 ± 0%36.0 ± 0%~(all equal)
 


assert.IsType(t, &sema.ResourceLossError{}, errs[0])
assert.IsType(t, &sema.UnreachableStatementError{}, errs[1])
assert.IsType(t, &sema.ResourceLossError{}, errs[2])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a potential duplicate error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is now an additional error reported, but "over reporting" isn't a problem: The first resource loss error is reported by the resource loss check by the return statement, and the second is returned by the resource loss check at the end of the function. Both are correct and I don't think it is worth the effort to reduce over reporting


assert.IsType(t, &sema.ResourceLossError{}, errs[0])
assert.IsType(t, &sema.UnreachableStatementError{}, errs[1])
assert.IsType(t, &sema.ResourceLossError{}, errs[2])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here?

Copy link
Member

@SupunS SupunS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! LGTM! 👌

@turbolent turbolent merged commit 2eb9cf7 into master Jan 5, 2022
@turbolent turbolent deleted the bastian/1325-fix-switch branch January 5, 2022 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Resource object may lost when using switch-case to destroy
3 participants