用AI自动优化代码生成提示,让大模型写出又安全又可用的程序。
PromSec: Prompt Optimization for Secure Generation of Functional Source Code with Large Language Models (LLMs)
- 构建生成式对抗图神经网络,自动修复生成代码的安全漏洞。
- 通过双目标优化减少超过90%的LLM调用次数和分析成本。
- 优化后的提示可跨语言、跨模型复用,适用于未知漏洞场景。
利用大语言模型(LLMs)生成高质量源代码可显著降低软件开发时间和成本,但因其训练数据包含不安全代码,常引入安全漏洞。本文提出PromSec,一种用于安全且功能性代码生成的提示优化算法。PromSec将生成式对抗图神经网络(gGAN)与LLM生成过程整合为交互循环:gGAN负责修复代码漏洞,其输出用于增强LLM提示,从而生成更安全的代码并保持功能正确性。通过引入对比学习机制,将代码清除与生成建模为双目标优化问题,显著减少对LLM的调用次数。在Python和Java数据集上的实验表明,相较于现有最优方法,PromSec能有效解决所有漏洞,且操作时间、LLM查询次数及安全分析成本均降低一个数量级以上。此外,经PromSec优化的提示具有跨语言、跨模型迁移能力,并可泛化至训练中未见的漏洞类型。本研究推动了大模型在可信代码生成中的应用,支持其在真实开发流程中的集成。
原文摘要 · Abstract (English)
The capability of generating high-quality source code using large language models (LLMs) reduces software development time and costs. However, they often introduce security vulnerabilities due to training on insecure open-source data. This highlights the need for ensuring secure and functional code generation. This paper introduces PromSec, an algorithm for prom optimization for secure and functioning code generation using LLMs. In PromSec, we combine 1) code vulnerability clearing using a generative adversarial graph neural network, dubbed as gGAN, to fix and reduce security vulnerabilities in generated codes and 2) code generation using an LLM into an interactive loop, such that the outcome of the gGAN drives the LLM with enhanced prompts to generate secure codes while preserving their functionality. Introducing a new contrastive learning approach in gGAN, we formulate code-clearing and generation as a dual-objective optimization problem, enabling PromSec to notably reduce the number of LLM inferences. PromSec offers a cost-effective and practical solution for generating secure, functional code. Extensive experiments conducted on Python and Java code datasets confirm that PromSec effectively enhances code security while upholding its intended functionality. Our experiments show that while a state-of-the-art approach fails to address all code vulnerabilities, PromSec effectively resolves them. Moreover, PromSec achieves more than an order-of-magnitude reduction in operation time, number of LLM queries, and security analysis costs. Furthermore, prompts optimized with PromSec for a certain LLM are transferable to other LLMs across programming languages and generalizable to unseen vulnerabilities in training. This study is a step in enhancing the trustworthiness of LLMs for secure and functional code generation, supporting their integration into real-world software development.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。