DocTer: documentation-guided fuzzing for testing deep learning API functions

Danning Xie, Yitong Li, Mijung Kim, Hung Viet Pham, Lin Tan, Xiangyu Zhang, Michael W. Godfrey

TL;DR

Deep learning libraries are complex and bug-prone, but their documentation is often informal and hard to parse for testing. This paper introduces DocTer, a novel technique that automatically extracts input constraints from this documentation. DocTer then uses these constraints to guide fuzzing, significantly improving bug detection in deep learning APIs compared to traditional methods.

What Problem Does This Paper Address?

Deep learning (DL) libraries like TensorFlow and PyTorch are foundational to modern AI development. However, they are also complex and prone to bugs, which can impact model accuracy and performance. A key challenge in testing these libraries is understanding the specific input constraints for their API functions. These constraints, such as data types, shapes, and valid ranges, are often described informally in free-form natural language documentation. Existing techniques for extracting constraints from text are not well-suited for the unique nature of DL API documentation. This gap makes it difficult to generate effective test inputs that can uncover bugs in the core functionality of these libraries, as opposed to just their input validation layers.

Methods

DocTer tackles this problem in three main phases:

  1. Rule Construction: This phase aims to automatically derive rules for extracting constraints. It starts with a small, manually annotated subset of API documentation. DocTer normalizes the text (e.g., replacing "int32" with "D_TYPE") and then parses it into dependency trees. It then uses frequent subtree mining to identify common syntactic patterns in these trees. By analyzing the relationship between these patterns and the manually annotated constraints (called Abstract Constraints or ACs), DocTer constructs rules that map syntactic patterns to specific constraint types (e.g., data type, structure, shape, valid value). This is framed as an optimization problem to find the smallest, most accurate set of rules.
  2. Constraint Extraction: Once the rules are built, DocTer applies them to the full corpus of API documentation. It parses the documentation, matches the syntactic patterns identified by the rules, and projects these matches to extract concrete DL-specific input constraints for each API parameter.
  3. Testing Phase (Fuzzing): The extracted constraints are then used to guide a fuzzing process. DocTer's input generator creates two types of inputs:
  4. Conforming Inputs (CIs): These inputs adhere to the extracted constraints, aiming to test the core functionality of the API.
  5. Violating Inputs (VIs): These inputs intentionally violate one or more constraints, designed to test the API's input validation and error handling. A test case evaluator then executes these inputs against the DL libraries, reporting any crashes or severe failures as potential bugs.

Datasets

The primary "dataset" used in this paper is the API documentation from three popular DL libraries:

  • TensorFlow (v2.1.0)
  • PyTorch (v1.5.0)
  • MXNet (v1.6.0)

The authors collected a significant amount of API documentation (e.g., 854,900 words for TensorFlow v2.1.0). For rule construction, a subset (30%) of parameters was manually annotated with Abstract Constraints (ACs). An additional 5% of parameters were used for evaluating the quality of the extracted constraints. The paper also mentions testing the generality of the rules on scikit-learn, a classic machine learning library.

Key Insights and Ideas

  • Natural Language as a Constraint Source: The core idea is that even informal natural language documentation contains structured information about API constraints, which can be programmatically extracted.
  • Syntactic Patterns for Semantic Constraints: DocTer leverages the fact that developers tend to use recurring syntactic patterns (like "a Tensor of type X" or "must be one of Y") to express constraints, and these patterns can be reliably mapped to specific constraint types.
  • Abstract Constraints (ACs) as an Intermediate Representation: Using ACs allows for a more general rule construction process that isn't tied to specific data types or values, making the rules more reusable.
  • Guided Fuzzing is Crucial: Simply generating random inputs is insufficient for complex DL libraries. Guiding fuzzing with extracted constraints dramatically increases the chances of reaching deeper code paths and uncovering subtle bugs.
  • Documentation Bugs are Real Bugs: DocTer's ability to detect inconsistencies within documentation highlights that documentation errors can directly lead to developer bugs and security vulnerabilities.

Key Results

  • Constraint Extraction Precision: DocTer achieved an overall precision of 85.4% in extracting DL-specific input constraints across the three libraries.
  • Bug Detection: DocTer detected 94 bugs in 174 API functions, significantly outperforming a baseline fuzzer (which detected 59 bugs).
  • New Bugs: 63 of the detected bugs were previously unknown.
  • Security Vulnerability: One previously unknown bug was documented in the CVE database.
  • Developer Action: 54 of the new bugs were fixed or confirmed by developers.
  • Documentation Bugs: DocTer identified 43 documentation bugs, 39 of which were fixed or confirmed by developers.
  • Valid Input Generation: DocTer generated passing inputs (inputs that executed without crashing) 55.3% more often than the baseline, indicating more effective generation of valid inputs.
  • Rule Generality: The rules learned from one library could be applied to another with good precision, and the approach showed promise for new libraries and versions with minimal manual effort.

Limitations and Future Work

  • Complex Constraints: DocTer struggles with highly complex constraints involving nested structures or indirect dependencies between parameters. However, the paper notes these are uncommon.
  • Manual Annotation Cost: While automated, the initial rule construction requires a one-time manual annotation cost (around 36 hours).
  • Incomplete Documentation: The effectiveness of DocTer is still limited by the completeness and accuracy of the original API documentation.
  • Future Work: The authors suggest extending DocTer to other domains (e.g., classic machine learning libraries like scikit-learn) and exploring more sophisticated handling of complex constraints. They also note that while they test Python APIs that invoke C++ code, directly testing C++ code could be a future direction.

Summary updated

BibTeX
@inproceedings{xie2022docter,
  author = {Danning Xie and Yitong Li and Mijung Kim and Hung Viet Pham and Lin Tan and Xiangyu Zhang and Michael W. Godfrey},
  title = {{DocTer: documentation-guided fuzzing for testing deep learning API functions}},
  booktitle = {ISSTA},
  year = {2022},
  doi = {10.1145/3533767.3534220},
}
Figure