Static analysis tools like linters and type checkers are the first line of defense for developers, catching bugs before code ever runs. However, they often stumble on runtime errors—those pesky IndexError or ValueError exceptions that only trigger based on specific inputs. Traditionally, you cannot predict these without actually running the program.
In their ICLR 2023 paper, "Static Prediction of Runtime Errors by Learning to Execute Programs with External Resource Descriptions," David Bieber and colleagues at Google Research and Mila propose a shift in how we approach static analysis. By teaching neural networks to "execute" code in a continuous embedding space and providing them with natural language descriptions of external inputs, they have created a model that can predict and even locate runtime errors without ever hitting the "run" button.
TL;DR
The authors introduce Exception IPA-GNN, a model that predicts runtime errors by simulating program execution over a control-flow graph. By leveraging natural language descriptions of program inputs (like "a single integer N"), the model achieves state-of-the-art accuracy in identifying error types and—surprisingly—localizing the exact line of failure using only weak supervision.
What Problem Does This Paper Address?
The fundamental challenge of static analysis is the external resource problem. A program's behavior isn't just defined by its source code; it depends on inputs from the user, files, or the network. For example, print(my_list[i]) is perfectly valid code, but it will crash if i is larger than the list size.
Standard static tools (like Pylint) often miss these because they don't "know" what the input will be. Dynamic analysis (running the code) finds them but requires a working environment and test cases. This paper asks: Can we bridge this gap by giving a machine learning model a "hint" about the inputs and asking it to simulate the execution?
Methods: Exception IPA-GNN
The authors build upon the Instruction Pointer Attention Graph Neural Network (IPA-GNN). Here is how the "Exception" version works:
- Interpreter-Inspired Architecture: Instead of treating code as a sequence of text (like a standard Transformer), the model operates on a Control-Flow Graph (CFG). Each node is a statement, and edges represent possible execution paths.
- Continuous Execution: The model maintains a "hidden state" (representing program memory) and an "Instruction Pointer" (a probability distribution over which line is currently executing). At each step, it updates the state and moves the pointer.
- Modeling Exceptions: The authors added a "raise" mechanism. At every statement, the model decides the probability that an error occurs. If it does, the Instruction Pointer mass shifts to a special "error node."
- Resource Grounding: To solve the input problem, they provide the model with natural language descriptions of
stdin. They tested three ways to integrate this: - Docstrings: Prepending the description to the code.
- FiLM: Using the description to "modulate" the neural layers.
- Cross-Attention: Allowing the model to "look" at the description while processing each line of code.
Datasets
The researchers curated a massive new benchmark derived from Project CodeNet:
- 2.4 Million Python 3 programs written by competitive programmers.
- 26 Error Classes: Including
IndexError,ValueError,ZeroDivisionError, and "No Error." - Input Descriptions: Each program is paired with the original problem's input description in English or Japanese (e.g., "Input is given from Standard Input in the following format: N...").
- Ground Truth: Every program was run in a sandbox to verify the exact error type and the line number where it occurred.
Key Insights and Ideas
- Inductive Bias Wins: Generic Transformers are great at many things, but they struggle to follow complex logic. By forcing the model to follow the CFG (the "inductive bias"), the IPA-GNN performs significantly better at reasoning about execution.
- "Learning to Execute" Descriptions: The model successfully learns to map phrases like "a single integer" to the behavior of an
int(input())call. It uses the natural language as a proxy for the missing runtime data. - Unsupervised Localization: This is the most striking finding. The model was never told which line caused the error during training—only if an error happened and what kind it was. However, because the model simulates execution, the "Instruction Pointer" naturally accumulates at the failing line. By looking at where the pointer "crashes," the researchers could identify the error location with high accuracy.
Key Results
The Exception IPA-GNN was tested against standard baselines like LSTMs, Transformers, and the static analysis tool Pylint.
- Classification Accuracy: On a balanced test set, the Exception IPA-GNN achieved 71.6% accuracy, outperforming Pylint (60.4%) and standard Transformers (63.6%).
- Localization: Using the Cross-Attention method for resource descriptions, the model correctly identified the failing line in 68.8% of erroneous programs.
- The "Hint" Matters: Adding resource descriptions improved the weighted F1 score of the IPA-GNN from 64.8 to 70.1, proving that "grounding" the model in the input format is crucial for static prediction.
Limitations and Future Work
While promising, the model has clear boundaries:
- Computational Cost: Simulating execution step-by-step is slower than a single Transformer pass. The authors suggest future models might learn to "jump" multiple execution steps at once.
- Language Features: The current dataset excludes programs with user-defined functions (UDFs) to keep the CFG manageable. Expanding to inter-procedural analysis (functions calling functions) is a major next step.
- Real-World Noise: Competitive programming code is self-contained. Real-world software involves complex dependencies and multi-file structures that would challenge the current CFG approach.
By treating code not just as text, but as a dynamic process to be simulated, this research opens the door for a new generation of developer tools that can warn you of a crash before you even save your file.
Summary updated