Papers › Constrained Decoding for Secure Code Generation

Constrained Decoding for Secure Code Generation

30 Apr 2024arXiv:2405.00218archive 2025-07-28

Yanjun Fu, Ethan Baker, Yu Ding, Yizheng Chen

Code Large Language Models (Code LLMs) have been increasingly used by developers to boost productivity, but they often generate vulnerable code. Thus, there is an urgent need to ensure that code generated by Code LLMs is correct and secure. Previous research has primarily focused on generating secure code, overlooking the fact that secure code also needs to be correct. This oversight can lead to a false sense of security. Currently, the community lacks a method to measure actual progress in this area, and we need solutions that address both security and correctness of code generation. This paper introduces a new benchmark, CodeGuard+, along with two new metrics, to measure Code LLMs' ability to generate both secure and correct code. Using our new evaluation methods, we show that the state-of-the-art defense technique, prefix tuning, may not be as strong as previously believed, since it generates secure code but sacrifices functional correctness. We also demonstrate that different decoding methods significantly affect the security of Code LLMs. Furthermore, we explore a new defense direction: constrained decoding for secure code generation. We propose new constrained decoding techniques to generate secure code. Our results reveal that constrained decoding is more effective than prefix tuning to improve the security of Code LLMs, without requiring a specialized training dataset. Moreover, our evaluations over eight state-of-the-art Code LLMs show that constrained decoding has strong performance to improve the security of Code LLMs, and our technique outperforms GPT-4.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2405.00218")

Code

Syntology Ran 8 of 13 code samples harvested from 1 repository linked to this paper; 5 have no recorded run. Of those that ran: 8 ran with no contract checked.

By repository: official repository: 13 samples from 1 repository, 8 ran. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

dynamite321/codeguardplus officialmentioned in papermentioned on GitHubMIT report
codeguardplus/codeguardplus mentioned on GitHubMIT report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

13 samples harvested; 8 ran; 0 honoured the contract we drafted; 5 have no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

8ran
5unverified

Licence: 0 of the 13 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from dynamite321/codeguardplus. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

confidence_interval dynamite321/codeguardplus/correctness_eval.py official repository ran MIT (permissive) · 4bd827a4fdf8d096 · report
duplicate_interleave dynamite321/codeguardplus/inference/hf/modeling_codegen.py official repository ran fingerprinted MIT (permissive) · 03e99c761c545617 · report
fixed_pos_embedding dynamite321/codeguardplus/inference/hf/modeling_codegen.py official repository ran MIT (permissive) · 7ea21b9da7ef5be6 · report
get_old_stats_json dynamite321/codeguardplus/new_stats.py official repository ran MIT (permissive) · 5fe250b209299fb2 · report
get_prompt dynamite321/codeguardplus/correctness_eval.py official repository ran MIT (permissive) · 718990946e149fe1 · report
get_sonar_scans dynamite321/codeguardplus/new_stats.py official repository ran MIT (permissive) · da54b6705bc5cee8 · report
pass_at_k dynamite321/codeguardplus/correctness_eval.py official repository ran fingerprinted MIT (permissive) · aaf864304de22b06 · report
rotate_every_two dynamite321/codeguardplus/inference/hf/modeling_codegen.py official repository ran fingerprinted MIT (permissive) · 621a8a98538cd46d · report
complete_scan dynamite321/codeguardplus/sonar_scan.py official repository unverified MIT (permissive) · 3ed1db97a93b601c · report
config_from_pretrained dynamite321/codeguardplus/inference/model.py official repository unverified MIT (permissive) · e1b5a2d510fa54d0 · report
get_insecure_files dynamite321/codeguardplus/sonar_scan.py official repository unverified MIT (permissive) · 957d15e0dd9a41e0 · report
new_project dynamite321/codeguardplus/sonar_scan.py official repository unverified MIT (permissive) · 8a519fe4005f6a16 · report
try_parse dynamite321/codeguardplus/codeql_eval.py official repository unverified MIT (permissive) · b147b5e666ea522a · report

Tasks

Code Generation

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

Absolute Position EncodingsAdamAttentionBPEDense ConnectionsDropoutGPT-4Label SmoothingLayer NormalizationLinear LayerMulti-Head AttentionPosition-Wise Feed-Forward LayerResidual ConnectionSoftmaxTransformer

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections