[Research Paper] Semantics-Based Code Search Using Input/Output Examples

Renhe Jiang, Zhengzhao Chen, Zejun Zhang, Yu Pei, Minxue Pan, Tian ZhangSCAM 2018

Abstract

Traditional keyword-based code search often falls short when developers need specific functional logic rather than just library names. This paper presents Quebio, a semantics-based search tool for Java that allows users to find code using input/output (I/O) examples. By transforming source code into logical path constraints and leveraging SMT solvers, Quebio identifies methods that functionally satisfy requirements. Evaluation on 35 common programming tasks shows that Quebio successfully finds the correct code in 83% of cases, typically ranking it as the top result.

Introduction

Traditional code search engines, such as those on GitHub or Google, rely heavily on textual keywords. While these are effective for broad searches, they often fail when a developer needs a specific logic snippet—for example, "a method that takes an unsorted array and returns a sorted one." Keywords like "sort" may return thousands of results, many of which are irrelevant or non-functional for the specific context.

Previous attempts at semantic search (searching by behavior) were often limited to simple, loop-free programs or specific domains like string manipulation. They frequently struggled with the "path explosion" problem inherent in symbolic execution and could not handle complex Java features like arithmetic or standard container classes (Lists, Sets, Maps). Quebio aims to bridge this gap, making semantic search a viable tool for general-purpose Java development.

Methodology

Quebio operates through a two-phase process: an offline symbolic encoding phase and an online query phase.

The system architecture of Quebio, illustrating the offline encoding and online query pipelines.

Offline Symbolic Encoding

  1. Path Extraction: Using the Soot framework, Quebio builds Control Flow Graphs (CFGs) for methods in a repository. To manage loops, it extracts a finite subset of paths.
  2. SSA Transformation: Extracted paths are converted into Static Single Assignment (SSA) form to track variable versions accurately.
  3. Constraint Templates: To avoid the complexity of analyzing the entire Java Development Kit (JDK), the authors developed manual "logical templates" for common API calls (e.g., ArrayList.add). This allows the system to understand API behavior without executing deep source code.
  4. SMT Encoding: These paths and templates are transformed into logical formulas compatible with the Z3 SMT solver.

Online Query Phase

When a user provides a query in the form of I/O examples, the system performs the following:

  1. Type Matching: It filters for methods whose input and output types match the provided examples.
  2. Path Matching: It binds the user's I/O values to the symbolic variables in the pre-encoded constraints. If the SMT solver finds the formula is "satisfiable" (SAT), the code is considered a match.
  3. Ranking: Methods are ranked based on the number of provided I/O examples they successfully satisfy.

Evaluation and Results

The authors evaluated Quebio using a repository of 1,228 methods crawled from 333 GitHub projects. They used a benchmark of 35 programming tasks from Stack Overflow, such as "Reverse an int value" or "Check if a string is numeric," providing five I/O examples for each.

A table showing input/output examples for a sorting task, used as a search query.

Quebio successfully found the correct code for 29 out of 35 queries (83% success rate). In these successful cases, the correct method was almost always ranked #1. While the average search time was approximately 48 seconds—slower than keyword search—the precision for functional requirements was significantly higher.

Key Insights and Limitations

A major contribution of this work is the use of "templates" to bypass the "deep invocation" problem in symbolic analysis. By using Javadoc-based templates for standard libraries, the authors significantly reduced the computational complexity of the search. Furthermore, the study proves that even a small number of concrete I/O examples can effectively narrow down search results in large repositories.

However, the approach has limitations. A significant portion of methods (93.8%) could not be encoded due to errors in the underlying analysis tools or missing templates for specific method calls. Additionally, the current version struggles with bitwise operations and requires manual effort to create library templates. Future work may involve automatically generating these templates from documentation or using machine learning to infer them.

Conclusion

Quebio demonstrates that semantics-based code search using I/O examples is a powerful alternative to traditional keyword search. By focusing on functional behavior rather than textual labels, it provides developers with a more precise way to find the logic they need in large-scale codebases.

Summary updated

BibTeX
@inproceedings{jiang2018research,
  author = {Renhe Jiang and Zhengzhao Chen and Zejun Zhang and Yu Pei and Minxue Pan and Tian Zhang},
  title = {{[Research Paper] Semantics-Based Code Search Using Input/Output Examples}},
  booktitle = {SCAM},
  year = {2018},
  doi = {10.1109/scam.2018.00018},
}
Figure