Authors: Khaled Abdelsalam Mohamed Amr Kamel Reverse Engineering State and Strategy Design Patterns using Static Code Analysis Authors: Khaled Abdelsalam Mohamed Amr Kamel
Software Development Continuous changes Bug fixing New features Requirements/Technological
Software Development Development team can also change New team members may need to learn from scratch Hard to know every implementation detail
Software Development Development team can also change New team members may need to learn from scratch Documentation becomes vital Tedious to keep updated
Software Development There have been many techniques to ease legacy code insight and understanding
Legacy/Undocumented code Design patterns have specific intentions Leads to understanding the usage of different parts of the software
Legacy/Undocumented code How can we extract design patterns from software? Decorator Strategy Observer State
The techniques Analysis type Search Methodology How to map the software properties Search Methodology How to search those properties
The techniques Analysis type: Structural analysis: Recovers Class relationships, method signatures, compositions etc. Proxy, Decorator, Adapter etc.
The techniques Analysis type: Structural analysis: Recovers Class relationships, method signatures, compositions etc. Proxy, Decorator, Adapter etc. Behavioral analysis: Recovers the components actions of the software. Iterator, State, Observer etc.
The techniques Analysis type: Structural analysis: Recovers Class relationships, method signatures, compositions etc. Proxy, Decorator, Adapter etc. Behavioral analysis: Recovers the components actions of the software. Iterator, State, Observer etc. Semantic analysis: Supplements both. Uses naming conventions.
The techniques Search Methodology: Database queries: Code is mapped with metadata structures
The techniques Search Methodology: Database queries: Code is mapped with metadata structures Constraint Resolver: Code Is mapped with addition of constraints and roles
The techniques Search Methodology: Database queries: Code is mapped with metadata structures Constraint Resolver: Code Is mapped with addition of constraints and roles XPG formalism and parsing: Source code mapped as a visual language
The techniques Search Methodology: Database queries: Code is mapped with metadata structures Constraint Resolver: Code Is mapped with addition of constraints and roles XPG formalism and parsing: Source code mapped as a visual language Code UML structures used to derive metrics. Found metrics compared against the expected metrics of a given Design Pattern
The techniques Static Analysis Dynamic Analysis Process to analyze code by deriving information from the source code Dynamic Analysis Derive information from running software Static Analysis -> Can be done in incomplete code
The techniques Static Analysis Process to analyze code by deriving information from the source code
The techniques Static Analysis Dynamic Analysis Process to analyze code by deriving information from the source code Dynamic Analysis Derive information from running software
The techniques Static Analysis Dynamic Analysis Process to analyze code by deriving information from the source code Dynamic Analysis Derive information from running software Static Analysis -> Can be done in incomplete code
The approach Use Static code analysis to find design patterns Program dependency Control dependency Data dependency
The approach The code is mapped as a graph Design patterns graphs are matched to code generated graph
The approach The Joern Platform was modified to also save class hierarchy Graph data persisted with Neo4j Gremlin to query the data Joern
The approach - Steps 1. Load source code
The approach - Steps 1. Load source code 2. Code property graph Joern
The approach - Steps Joern 1. Load source code 2. Code property graph 3. Save graph in Neo4j DB Joern
4. Add Design Patterns Attributes The approach - Steps 1. Load source code 2. Code property graph 3. Save graph in Neo4j DB 4. Add Design Patterns Attributes Joern
4. Add Design Patterns Attributes The approach - Steps 1. Load source code 2. Code property graph 3. Save graph in Neo4j DB 4. Add Design Patterns Attributes 5. Select Design Pattern Joern
The approach - Steps Joern 1. Load source code 2. Code property graph 3. Save graph in Neo4j DB 4. Add Design Patterns Attributes 5. Select Design Pattern 6. Load Design Pattern Matching Queries Joern
The approach - Steps Joern 1. Load source code 2. Code property graph 3. Save graph in Neo4j DB 4. Add Design Patterns Attributes 5. Select Design Pattern 6. Load Design Pattern Matching Queries 7. Run Queries Joern
The approach - Steps Joern 1. Load source code 2. Code property graph 3. Save graph in Neo4j DB 4. Add Design Patterns Attributes 5. Select Design Pattern 6. Load Design Pattern Matching Queries 7. Run Queries 8. Display Graph Matching Patterns results Joern
The approach State and Strategy design patterns issue Similar structure Different behavior
State and Strategy design patterns issue Similar structure Different behavior vs
State and Strategy design patterns issue
Metadata graph enrichment Methods to Classes Link between classes and their methods (Joern misses)
Metadata graph enrichment Methods to Classes Link between classes and their methods (Joern misses) Inheritance Adds the relation between Super class and their subclasses
Metadata graph enrichment Methods to Classes Link between classes and their methods (Joern misses) Inheritance Adds the relation between Super class and their subclasses Abstract (Virtual) Methods With body No body
Metadata graph enrichment Class Aggregates Class
Metadata graph enrichment Class Aggregates Class Link between classes aggregating other classes Method Creates Class Link between a method and the class of the object it instantiates
Metadata graph enrichment Class Aggregates Class Link between classes aggregating other classes Method Creates Class Link between a method and the class of the object it instantiates Method Overrides Method Link between methods overriding each other
Metadata graph enrichment Class Aggregates Class Link between classes aggregating other classes Method Creates Class Link between a method and the class of the object it instantiates Method Overrides Method Link between methods overriding each other Method Calls Method Link between a caller method and the method called
Solving the State and Strategy issue Step 1 Find State and Strategy candidates Step 2 Differentiate State and Strategy candidates
Differentiating State and Strategy candidates Loop each pattern candidate Find class aggregations variables (let’s call it X) Use find methods M that use variables X Check if methods M includes subclasses from pattern candidates If c is truth then the candidate is a State design Check if methods from c includes client method from pattern candidates If f is true then the candidate is a strategy
Authors Conclusion Successfully use Code Property Graph to map Design patterns Able to differentiate Strategy and State Uses only static analysis As future work Make catalogue of all relationships and properties of design patterns Support multiple graph pattern variants
Paper critique Abstract and Introduction Deviated from the described a Approach Focus in the Strategy/State No mention of qualitative/quantitative study
My Conclusion Needs to evaluate the approach Practical idea Constrain Test on well know good code (e.g, Linux Kernel on Github) Practical idea Constrain Needs well written code that uses design pattern
References https://refactoring.guru/design-patterns/strategy Kniesel, Gunter and Binun, Alexander, "Standing on the shoulders of giants- a data fusion approach to design pattern detection," in Program Comprehension, 2009. ICPC'09. IEEE 17th International Conference on, 2009. T. A. Ghaleb, "The role of open source software in program analysis for reverse engineering," 2016 2nd International Conference on Open Source Software Computing (OSSCOM), Beirut, 2016, pp. 1-6. doi: 10.1109/OSSCOM.2016.7863684 G. Costagliola and G. Polese, "Extended positional grammars," Proceeding 2000 IEEE International Symposium on Visual Languages, Seattle, WA, 2000, pp. 103-110. doi: 10.1109/VL.2000.874373