Introduction
Code clones—identical or highly similar code segments—are a double-edged sword in software engineering. While they facilitate rapid development, they also propagate bugs: a vulnerability found in one snippet likely exists in all its clones. This is particularly critical for pointer-related code, which is notorious for memory safety bugs. Traditional syntax-based detection tools often struggle because relevant pointer logic is frequently intertwined with irrelevant code, leading to high false-positive rates or missed detections.
The Twin-Finder Framework
Twin-Finder is a hybrid "closed-loop" framework designed to bridge the gap between machine learning (ML) and formal logic. It identifies pointer-related clones through a specialized three-stage pipeline.
1. Domain-Specific Slicing
To isolate relevant logic, Twin-Finder identifies a target pointer and performs forward and backward program slicing. This process strips away "noise"—lines of code that do not affect the pointer's base, offset, or bounds—leaving only the logic relevant to memory safety. This significantly increases the signal-to-noise ratio for subsequent analysis.
2. ML-Based Detection
The isolated code is converted into Abstract Syntax Trees (ASTs). These trees are then transformed into numerical feature vectors and clustered using Locality Sensitive Hashing (LSH) to find potential clones based on structural similarity.
3. Formal Verification & Feedback Loop
The "Twin" in Twin-Finder refers to the integration of a formal reasoning engine that acts as a "teacher" for the ML model. The system samples clusters and uses symbolic execution (via KLEE and Z3) to verify semantic equivalence.
- True Clones: If the symbolic executor finds that two snippets share the same path constraints (e.g., identical buffer length checks), they are confirmed as true clones.
- False Positives: If constraints differ, the system calculates the structural difference between their ASTs and applies a numerical weight ($\delta$) to those features in the vector space. This feedback loop "pushes" dissimilar samples apart, ensuring the ML model does not misclassify them in future iterations.
Experimental Evaluation
The authors evaluated Twin-Finder on seven real-world benchmarks, including SPEC2006 components (bzip2, hmmer, sphinx3) and network applications (thttpd, links). These codebases range from 3k to 178k lines of code with varying pointer complexity.
Key Results
- Higher Recall: Twin-Finder detected an average of 393.68% more clones than DECKARD. In the
gzipbenchmark, it identified 9x more clones. - Precision Boost: The formal feedback loop eliminated an average of 91.69% of false positives. In
bzip2, the system reached near-perfect precision (99.32% false positives eliminated) within just 10 iterations. - Domain Specificity: The tool successfully identified domain-specific lines of code representing 40% to 75% of total pointer-related code, whereas DECKARD only covered 9% to 32%.
Conclusion
Twin-Finder demonstrates that combining the efficiency of ML with the rigor of formal methods can significantly improve code clone detection. By focusing on semantic behavior rather than just syntax, it provides a robust solution for identifying security-critical clones. Future research will focus on scaling symbolic execution for multi-million line codebases and expanding this "closed-loop" reasoning to other vulnerability types like integer overflows or concurrency issues.
Summary updated