arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2409.12699cs.SEcs.LG
收藏

PromSec:面向大型语言模型(LLM)安全生成功能源代码的提示词优化

PromSec: Prompt Optimization for Secure Generation of Functional Source Code with Large Language Models (LLMs)

New Jersey Institute of Technology(新泽西理工学院);Qatar Computing Research Institute(卡塔尔计算研究所)

Mahmoud Nazzal, Issa Khalil, Abdallah Khreishah, NhatHai Phan

AI总结:

PromSec提出一种结合生成对抗图神经网络与LLM的交互式提示词优化方法,通过双目标优化在保持代码功能的同时显著消除安全漏洞,并大幅降低推理成本,且提示词可跨模型迁移。

摘要:

使用大型语言模型(LLM)生成高质量源代码的能力缩短了软件开发时间和成本。然而,由于在存在安全漏洞的开源数据上进行训练,它们常常引入安全漏洞。这凸显了确保安全且功能正常的代码生成的需求。本文介绍了PromSec,一种使用LLM进行安全且功能正常的代码生成的提示词优化算法。在PromSec中,我们将1)使用生成对抗图神经网络(称为gGAN)进行代码漏洞清除,以修复和减少生成代码中的安全漏洞,以及2)使用LLM进行代码生成,结合成一个交互式循环,使得gGAN的输出驱动LLM使用增强的提示词生成安全代码,同时保持其功能。在gGAN中引入一种新的对比学习方法,我们将代码清除和生成表述为一个双目标优化问题,使PromSec能够显著减少LLM推理的次数。PromSec为生成安全、功能正常的代码提供了一种经济高效且实用的解决方案。在Python和Java代码数据集上进行的大量实验证实,PromSec有效增强了代码安全性,同时保持了其预期功能。我们的实验表明,虽然最先进的方法未能解决所有代码漏洞,但PromSec有效解决了它们。此外,PromSec在操作时间、LLM查询次数和安全分析成本方面实现了超过一个数量级的减少。此外,使用PromSec为特定LLM优化的提示词可迁移到其他LLM,跨编程语言,并可泛化到训练中未见过的漏洞。这项研究是增强LLM用于安全且功能正常的代码生成的可信度的一步,支持其集成到现实世界的软件开发中。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑