Flaws in escape analysis may result in incorrect optimization #6152
Labels
audit-report
Related to the audit report
bug
Something isn't working
compiler: ir
IRgen and sway-ir including optimization passes
compiler: optimization
IR Optimization Passes
P: high
Should be looked at if there are no critical issues left
Several passes rely on the escape_analysis here. Unfortunately, we've noticed flaws within the implementation which could lead to incorrect optimization. Taking dce as example, due to imprecise symbol tracking, the attached PoC script would be incorrectly optimized, and the store in main will be removed. The immediate cause seems to be load / store / escape symbol resolve ignoring the Incomplete tag of ReferredSymbols (1) / (2) / (3), but it is also reasonable to attribute this to incorrect instruction effect modeling. An easy fix would be making all optimizations more conservative (e.g. give up when escaped symbol is Incomplete), however, this might limit the effectiveness of optimization passes. Notably, instruction modeling is a complex topic which has been a major source of bugs in compilers (e.g. v8 jit), and even one incorrect effect modeling would lead to failure of the entire algorithm. While the ir instruction set is a lot more limited in sway, it would still be really difficult for us to guarantee correctness if algorithm is not sufficiently conservative.
The text was updated successfully, but these errors were encountered: