Fault Localization from the Semantic Code Search Perspective

Yihao Qin, Shangwen Wang, Yan Lei, Zhuo Zhang, Bo Lin, Xin Peng, Jun Ma, Liqian Chen, Xiaoguang Mao

Abstract

Locating bugs in large-scale software projects remains a significant challenge in software engineering. Traditional Fault Localization (FL) techniques often treat the problem as a navigation task or a statistical analysis of execution traces. This paper presents CosFL, a novel framework that re-conceptualizes FL as a Semantic Code Search problem. By utilizing Large Language Models (LLMs) to bridge the gap between raw error logs and functional code descriptions, CosFL achieves a Top-1 accuracy of 38.8% on major benchmarks, outperforming existing state-of-the-art tools by up to 57%.

Introduction

Finding a bug in a massive codebase is frequently likened to finding a needle in a haystack. Historically, researchers have approached this through spectrum-based analysis (comparing passing and failing runs) or navigation-based agents that "walk" through code structures. However, these methods often struggle with "semantic-poor" information—error logs that indicate a failure occurred but fail to describe the intended logic.

CosFL proposes a paradigm shift: treating the search for a bug like a semantic search query. By transforming cryptic stack traces into descriptive natural language, the system can search the codebase for functionality that matches the "buggy-looking" description.

Methodology

The CosFL framework operates through a structured three-phase pipeline designed to maximize the semantic understanding of the codebase and the fault.

Comparison of traditional learning-based and navigation-based paradigms with the proposed CosFL approach.

1. Software Knowledge Base Construction

To enable effective search, CosFL builds a comprehensive index of the project. It uses program instrumentation to generate a dynamic method call graph during test execution. Methods are then grouped into modules using the Leiden community detection algorithm. Finally, an LLM generates semantic summaries for every module, method, and code "chunk," creating a searchable index that captures both high-level architecture and low-level logic.

2. Agentic Query Generation

A specialized LLM agent, acting as a "Diagnostics Specialist," analyzes the available fault information (test cases, stack traces, console output). If the information is ambiguous, the agent can query the knowledge base for additional context before generating multi-level search queries (Module, Method, and Chunk levels).

3. Fault Retrieval and Voting

CosFL uses text embeddings to match these queries against the indexed codebase. To ensure precision, it employs a multi-granularity voting mechanism (based on the Borda count) to aggregate results across different levels of code abstraction, producing a final ranked list of suspicious methods.

The Semantic Bridge

The core innovation of CosFL is the "Semantic Bridge." Traditional tools often get lost because they lack a conceptual understanding of what the code should be doing. By using LLMs to describe the likely faulty functionality in natural language, CosFL can bypass the limitations of raw keyword matching or rigid navigation paths.

The transformation of raw fault information into a semantic description of faulty functionality to pinpoint the exact code location.

As shown in the example above, a failed test case and its output are translated into a specific functional description: "The method... is not correctly handling the parameters of nested functions." This descriptive query allows the search engine to identify the exact method (removeUnreferencedFunctionArgs) and the necessary fix.

Experimental Evaluation

The researchers evaluated CosFL using a benchmark of 835 real-world bugs across 23 Java projects, including the industry-standard Defects4J and the newer GrowingBugs dataset to ensure results were not skewed by data leakage in the LLM's training set.

Key Results

  • Accuracy: CosFL successfully localized 324 bugs (38.8%) in the Top-1 position.
  • Comparative Performance: It outperformed AgentFL by 26.6% and AutoFL by 57.3% in Top-1 accuracy.
  • Efficiency: By utilizing a search-based approach, CosFL avoids the path-dependency errors common in navigation-based agents, where a single wrong turn can lead the agent away from the actual bug.

Discussion and Limitations

While CosFL represents a significant advancement, it currently has some limitations. It is primarily Java-centric due to its reliance on specific instrumentation tools. Additionally, because it utilizes dynamic call graphs, it is limited to code paths executed during testing. Future work may involve integrating static analysis to cover unexecuted branches and exploring finer-grained retrieval at the variable level.

Conclusion

CosFL demonstrates that re-framing fault localization as a semantic code search task significantly improves the precision of automated debugging. By leveraging the descriptive power of LLMs to create a semantic bridge between error states and code logic, developers can more effectively find the "needle" in even the most complex codebases.

Summary updated

BibTeX
@article{qin2026fault,
  author = {Yihao Qin and Shangwen Wang and Yan Lei and Zhuo Zhang and Bo Lin and Xin Peng and Jun Ma and Liqian Chen and Xiaoguang Mao},
  title = {{Fault Localization from the Semantic Code Search Perspective}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2026},
  doi = {10.1145/3757915},
}
Figure