Twin-Finder: Integrated Reasoning Engine for Pointer-Related Code Clone Detection

Hongfa Xue, Yongsheng Mei, Kailash Gogineni, Guru Venkataramani, Tian Lan

TL;DR

This paper introduces Twin-Finder, a novel approach to detecting code clones, specifically those involving pointers, by integrating machine learning with symbolic execution. It achieves higher detection rates and significantly reduces false positives by formally verifying clone candidates and using this feedback to refine the detection process.

What Problem Does This Paper Address?

Identifying code clones—fragments of code that are similar or identical—is a critical task in software engineering, aiding in bug fixing, refactoring, and vulnerability analysis. While existing methods like tree-based comparisons and machine learning have made progress, they often struggle with scalability and, more importantly, generate a substantial number of false positives. This is particularly problematic for pointer-related code, which is prevalent in real-world applications and a common source of security bugs. The challenge lies in accurately detecting these pointer-related clones, especially when they are intertwined with other code, and doing so with high precision to avoid the costly manual verification of false positives.

Methods

Twin-Finder employs a two-pronged strategy: Domain Specific Slicing and Closed-loop Code Clone Detection.

  1. Domain Specific Slicing: To focus on relevant code, Twin-Finder first identifies all pointers in a program. It then performs dependency analysis and lightweight tainting to pinpoint "pointer-related variables" that could influence pointer behavior. Finally, it uses program slicing (both forward and backward) to isolate only the code segments that are directly involved with these pointers, discarding irrelevant code. This significantly narrows down the search space.
  2. Closed-loop Code Clone Detection:
    • Initial Detection: Twin-Finder uses a tree-based code clone detection approach, similar to existing methods, but applied to the sliced code. It generates feature vectors from Abstract Syntax Trees (ASTs) and uses a similarity threshold to cluster potential clone pairs.
  3. Clone Verification: This is where Twin-Finder innovates. It employs symbolic execution (SE) to formally verify if a detected clone pair is a "true clone." This involves checking if the two code fragments share the same "bound safety constraints" related to pointer operations.
  4. Formal Feedback Loop: If the symbolic execution reveals that a clone pair has different bound safety constraints (i.e., it's a false positive), Twin-Finder uses this information to tune its machine learning component. It adjusts the weights of feature vectors associated with the AST nodes that caused the differing constraints. This feedback loop iteratively refines the similarity metric and vector embeddings, effectively "teaching" the system to avoid similar false positives in the future.

Datasets

The paper evaluates Twin-Finder on seven real-world benchmarks: bzip2, hmmer, and sphinx3 from the SPEC2006 suite, man and gzip from Bugbench, thttpd-2.23beat1, and links-2.14. These are standard benchmarks for code analysis tasks. No new datasets were introduced.

Key Insights and Ideas

  • Pointer-Specific Analysis is Crucial: The paper highlights that pointer-related code is a significant source of bugs and requires specialized detection methods beyond general code clone detection.
  • Symbolic Execution as a Precision Tool: Using symbolic execution for formal verification of clone candidates is a powerful way to distinguish true clones from false positives, especially concerning memory safety.
  • Feedback Loop for Continuous Improvement: The core innovation is the closed-loop system where formal verification results directly inform and refine the machine learning-based clone detection. This allows the system to learn from its mistakes and improve its precision over time.
  • Slicing for Efficiency and Relevance: Domain-specific slicing dramatically reduces the amount of code that needs to be analyzed, making the process more scalable and focused on the most critical parts of the code for pointer analysis.

Key Results

  • Increased Detection Rate: Twin-Finder detects up to 9x more code clones on average compared to the tree-based detector Deckard.
  • Significant False Positive Reduction: The system removes an average of 91.69% of false positives.
  • Coverage: Twin-Finder achieves higher coverage of pointer-related lines of code (D.S LoC), ranging from 40.15% to 75.89%, compared to Deckard (9.57% to 32.51%).
  • Flexibility: The approach can effectively detect more clones even when the code similarity threshold is relaxed (e.g., from 1.00 to 0.70), with the feedback mechanism mitigating the expected increase in false positives.

Limitations and Future Work

  • Incomplete False Positive Removal: The authors acknowledge that their current implementation may not remove 100% of false positives. Specific cases, such as multiple branches or indirect memory access where array indices are derived from other pointers, remain challenging.
  • Future Work: The paper suggests further research into handling these complex cases, potentially by extending the symbolic execution capabilities or refining the feedback mechanism to address these specific scenarios. The integrated framework itself offers a strong foundation for future enhancements in robust and precise code analysis.

Summary updated

BibTeX
@inproceedings{xue2020twin,
  author = {Hongfa Xue and Yongsheng Mei and Kailash Gogineni and Guru Venkataramani and Tian Lan},
  title = {{Twin-Finder: Integrated Reasoning Engine for Pointer-Related Code Clone Detection}},
  booktitle = {IWSC},
  year = {2020},
  doi = {10.1109/iwsc50091.2020.9047638},
}
Figure