ARXIV PREPRINT
1
MARGIN: Margin-Aware Regularized Geometry for Imbalanced Vulnerability Detection
arXiv:2605.10240v1 [cs.SE] 11 May 2026
Yuteng Zhang, Huifang Ma, Member, IEEE, Jiahui Wei, Qingqing Li, and Yafei Yang
Abstract—Software vulnerability detection is critical for ensuring software security and reliability. Despite recent advances in deep learning, real-world vulnerability datasets suffer from two severe challenges: frequency imbalance and difficulty imbalance. We reinterpret these challenges from an embedding geometry perspective, observing that such imbalances induce geometric distortions in hyperspherical representation space. To address this issue, we propose MARGIN (Margin-Aware Regularized Geometry for Imbalanced Vulnerability DetectioN), a metricbased framework that learns discriminative vulnerability representations through adaptive-margin metric learning and hyperspherical prototype modeling. MARGIN dynamically adjusts geometric regularization according to the distribution structure estimated by the von Mises–Fisher concentration, aligning the probability mass of embedding distributions with their corresponding Voronoi cells, thereby reducing geometric distortion and yielding more stable decision boundaries. Extensive experiments on public vulnerability datasets show that MARGIN consistently outperforms strong baselines, achieving notable improvements in classification and detection, especially on challenging, imbalanced datasets. Further analysis demonstrates that MARGIN produces more structured embedding geometries, improving robustness, interpretability, and generalization. Index Terms—Software Vulnerability Detection, Software Security, Imbalance Learning, Metric Learning.
I. I NTRODUCTION S modern software systems keep growing in size and complexity, security vulnerabilities hidden in source code have become a serious threat to software reliability and system safety. These vulnerabilities can cause unexpected program failures or be exploited by attackers to break into systems. Therefore, automatically detecting and classifying vulnerabilities from source code has become an important problem in software engineering. Recent advances in deep learning have significantly improved software vulnerability detection and classification. However, despite these advances, these models still face substantial challenges when applied to real-world vulnerability datasets. Deep learning-based vulnerability analysis aims to learn discriminative class representations by clustering samples of the same category while separating different categories. However, real-world vulnerability data are often highly imbalanced in both class frequency and sample difficulty, which can bias model optimization, hinder reliable convergence, and degrade generalization performance.
A
Yuteng Zhang, Huifang Ma, Jiahui Wei, Yafei Yang, and Qingqing Li are with the College of Artificial Intelligence and Computer Science, Northwest Normal University, Lanzhou 730070, China. Corresponding author: Huifang Ma (email: [email protected]). Manuscript created May, 2026;
Frequency Imbalance. A key challenge in deep learning for vulnerability detection is the severe frequency imbalance in real-world software data. This issue appears not only as the imbalance between vulnerable and non-vulnerable code, but more importantly as the uneven distribution across different Common Weakness Enumeration (CWE) categories. As shown in Fig. 1, which presents the frequencies of the top 25 CWE types from recent CVE records (2023–2025), the data follows a clear long-tailed distribution with large differences in sample counts. A small number of common categories, such as CWE-79 (Cross-Site Scripting) and CWE89 (SQL Injection), contain tens of thousands of samples and make up a large portion of the dataset. In contrast, many other vulnerabilities, such as CWE-400 (Uncontrolled Resource Consumption), CWE-98 (Use of an Improperly Restricted Directory), and CWE-863 (Incorrect Authorization),appear very rarely and only account for a small part of the data. This large gap in frequency leads to biased metric learning. Standard loss functions like Cross-Entropy and contrastive Loss treat all samples equally [1], so classes with more samples contribute much more to the gradients and dominate the parameter updates. Meanwhile, minority classes provide much weaker signals and have limited influence on the learned representations. In the learned embedding space, majorityclass samples typically cluster tightly, while minority-class samples remain scattered and fail to reliably converge around their prototypes. As illustrated in Fig. 3 (a), even when class difficulty is similar, the embeddings of minority class a shows a more dispersed and blur distribution than class b. Because of this, the model tends to learn biased patterns from high-frequency classes, shifting the decision boundary toward dominant categories while overlooking the subtle characteristics of rare vulnerabilities [2], [3]. Such bias severely limits the reliability of vulnerability detection systems in realworld security scenarios, where accurately identifying rare yet critical vulnerabilities is particularly important. Difficulty imbalance. Most existing methods for handling imbalanced vulnerability detection primarily focus on frequency imbalance. Common solutions include resampling, cost-sensitive learning, focal loss, and class-balanced reweighting [1], [4]. While these approaches mitigate the influence of majority classes, they usually assume that all classes are equally difficult to learn. Difficulty imbalance can significantly hinder model performance even when class frequencies are balanced, leading to slower convergence, lower precision for difficult classes, and biased evaluation metrics in real-world vulnerability datasets. In practice, vulnerability datasets also exhibit clear differ-
ARXIV PREPRINT
2