# Large Language Models for Code: Security Hardening and Adversarial Testing

**Authors:** Jingxuan He, Martin Vechev  
**arXiv:** [2302.05319](https://arxiv.org/abs/2302.05319)  
**Date:** 2023-02-10  
**Summary page:** https://www.summarizepaper.com/en/paper/2302.05319v3/

## Key points

- Large language models (LMs) lack security awareness and often produce unsafe code
- Authors focus on studying LM security in two aspects: security hardening and adversarial testing
- Proposed a new security task called controlled code generation
- Introduced a learning-based approach called SVEN to solve the task
- SVEN uses property-specific continuous vectors to guide program generation towards desired properties without modifying LM weights
- Training procedure optimizes continuous vectors using specialized loss terms on different regions of code with high-quality dataset
- Evaluation shows SVEN is highly effective in achieving strong security control
- SVEN increases generation of secure code from 59.1% to 92.3% during security hardening and decreases it to 36.8% during adversarial testing for a CodeGen LM with 2.7B parameters
- SVEN maintains functional correctness similar to original LMs
- This work addresses security concerns of large LMs used for generating code by introducing controlled code generation task and proposing SVEN as an effective approach

## Summary

Large language models (LMs) are increasingly being trained on massive codebases and used to generate code. However, these LMs lack awareness of security and often produce unsafe code. In this work, the authors focus on studying the security of LMs in two important aspects: security hardening and adversarial testing. To address these issues, the authors propose a new security task called controlled code generation. This task takes a binary property as input to guide the LM in generating either secure or unsafe code while still maintaining its ability to generate functionally correct code. They introduce a novel learning-based approach called SVEN to solve this task. SVEN leverages property-specific continuous vectors to guide program generation towards the desired property without modifying the weights of the LM. The training procedure optimizes these continuous vectors by enforcing specialized loss terms on different regions of code using a carefully curated high-quality dataset. The evaluation of SVEN shows that it is highly effective in achieving strong security control. For example, when applied to a state-of-the-art CodeGen LM with 2.7B parameters, SVEN increases the generation of secure code from 59.1% to 92.3% during security hardening and decreases it to 36.8% during adversarial testing. Importantly, SVEN maintains functional correctness similar to the original LMs. In summary, this work addresses the security concerns associated with large LMs used for generating code by introducing a new task called controlled code generation and proposing an effective learning-based approach called SVEN. The results demonstrate that SVEN significantly improves the security control of LMs while preserving their functionality.
