[Paper Review] Can We Trust Large Language Models Generated Code? A Framework for In-Context Learning, Security Patterns, and Code Evaluations Across Diverse LLMs
The paper proposes an in-context learning security framework to train and evaluate four diverse LLMs (PDCGs and CCPs) across C++, C#, and Python, using security patterns and SAST/code reviews to assess and improve secure code generation.
Large Language Models (LLMs) such as ChatGPT and GitHub Copilot have revolutionized automated code generation in software engineering. However, as these models are increasingly utilized for software development, concerns have arisen regarding the security and quality of the generated code. These concerns stem from LLMs being primarily trained on publicly available code repositories and internet-based textual data, which may contain insecure code. This presents a significant risk of perpetuating vulnerabilities in the generated code, creating potential attack vectors for exploitation by malicious actors. Our research aims to tackle these issues by introducing a framework for secure behavioral learning of LLMs through In-Content Learning (ICL) patterns during the code generation process, followed by rigorous security evaluations. To achieve this, we have selected four diverse LLMs for experimentation. We have evaluated these coding LLMs across three programming languages and identified security vulnerabilities and code smells. The code is generated through ICL with curated problem sets and undergoes rigorous security testing to evaluate the overall quality and trustworthiness of the generated code. Our research indicates that ICL-driven one-shot and few-shot learning patterns can enhance code security, reducing vulnerabilities in various programming scenarios. Developers and researchers should know that LLMs have a limited understanding of security principles. This may lead to security breaches when the generated code is deployed in production systems. Our research highlights LLMs are a potential source of new vulnerabilities to the software supply chain. It is important to consider this when using LLMs for code generation. This research article offers insights into improving LLM security and encourages proactive use of LLMs for code generation to ensure software system safety.
Motivation & Objective
- Address security risks in LLM-generated code due to training on public repositories and insecure samples.
- Develop and evaluate In-Context Learning (ICL) security patterns to teach LLMs secure coding behaviors.
- Compare prompt-driven code generators (PDCGs) and coding copilots (CCPs) across multiple languages.
- Create and analyze datasets and security evaluation workflows for LLM-generated code.
- Provide insights and datasets to advance secure code generation in AI-assisted development.
Proposed method
- Curate diverse programming problem sets in C++, C#, and Python, including DS&A, API development, and MVC design patterns.
- Develop In-Context Learning (ICL) security patterns with zero-shot, one-shot, and few-shot learning scenarios incorporating chain-of-thought reasoning.
- Generate code with four LLMs (PDCGs: ChatGPT-4, Google Bard; CCPs: GitHub Copilot, Amazon Code Whisperer) across problems.
- Assess generated code using Static Application Security Testing (SAST) and manual security reviews to identify vulnerabilities and hidden code smells.
- Compute security risk metrics at the source code level and analyze persistence of code smells after ICL.
- Release a security-instructions dataset to support future LLM security training.

Experimental results
Research questions
- RQ1RQ1: How well can diverse LLMs generate secure code across various programming challenges in zero-shot scenarios?
- RQ2RQ2: To what extent do LLMs understand and apply best practices and address vulnerabilities after using ICL security patterns in one-shot and few-shot learning?
- RQ3RQ3: How do PDCG LLMs (ChatGPT-4, Google Bard) compare to CCP LLMs (GitHub Copilot, Code Whisperer) in generating secure code under ICL?
- RQ4RQ4: What security code smells persist after employing ICL security patterns and what are the associated risks?
Key findings
- ICL-driven one-shot and few-shot learning patterns can enhance code security and reduce vulnerabilities across programming scenarios.
- Four diverse LLMs are evaluated across three languages, revealing differences between PDCGs and CCPs in secure code generation.
- Security testing combines SAST tools with manual code reviews to identify vulnerabilities and hidden code smells.
- A security risk assessment framework is developed to quantify code-level security threats.
- The study proposes releasing a security-instructions dataset to guide future secure code generation research.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.