Software Quality Journal Software
StackLoc: A CodeBERT-BiLSTM framework for stack overflow vulnerability detection and localization
Abstract
Stack overflow vulnerabilities pose severe security threats, potentially leading to program crashes, data breaches, and significant financial losses. Attackers exploit these vulnerabilities to execute attack techniques such as Return-Oriented Programming (ROP), Jump-Oriented Programming (JOP), and Structured Exception Handler (SEH) exploitation. This paper introduces StackLoc, a CodeBERT-BiLSTM framework for detecting and localizing stack overflow vulnerabilities at the source code level. First, StackLoc generates differential (diff) files from vulnerable samples and their corresponding patched versions, enabling automated annotation and localization of vulnerable code fragments. Next, StackLoc employs a code-slicing mechanism that integrates focus windows and context windows to extract vulnerability-related semantic information, thereby providing more accurate contextual representations for feature learning. By integrating CodeBERT with a bidirectional long short-term memory (BiLSTM) network, StackLoc encodes code fragments into semantic vector representations, modeling bidirectional semantic dependencies and enhancing its ability to capture contextual structures and vulnerability-related patterns. As a result, StackLoc improves the performance of stack overflow vulnerability detection while maintaining high localization accuracy. To evaluate StackLoc, extensive experiments are conducted on the CWE-121 stack overflow vulnerability dataset, including parameter analysis, ablation studies, and comparisons with representative source code-based detection methods. The experimental results demonstrate that StackLoc achieves the best recall and F1-score among the compared baselines while maintaining competitive accuracy and precision. Furthermore, the window-based prediction and visualization framework of StackLoc supports vulnerability region highlighting and improves the interpretability of detection results.