arXiv:2504.21043cs.CRcs.AI2025-04被引 7

CodeBC通过三阶段微调,让大模型生成更安全的区块链智能合约。

CodeBC: A More Secure Large Language Model for Smart Contract Code Generation in Blockchain

  • 用漏洞标签替代人工标注对,降低数据依赖
  • 生成代码编译通过率更高,漏洞率显著下降
  • 适合区块链开发者快速生成高安全性合约

大型语言模型在自然语言转代码方面表现优异,但在智能合约等高安全要求任务中常忽略安全漏洞。现有方法依赖人工标注的漏洞数据,但仅适用于主流语言如Python和C++。对于Solidity这类低资源语言,大规模标注数据稀缺。为此,本文提出CodeBC,基于CodeLlama的三阶段微调模型,通过漏洞与安全标签教会模型区分安全与不安全代码,无需成对漏洞标注。推理时利用安全标签生成健壮代码。实验表明,CodeBC在BLEU、CodeBLEU和编译通过率上优于基线模型,漏洞率大幅降低,验证了该策略的有效性与成本效益,为生成安全智能合约提供了可行方案。

原文摘要 · Abstract (English)

Large language models (LLMs) excel at generating code from natural language instructions, yet they often lack an understanding of security vulnerabilities. This limitation makes it difficult for LLMs to avoid security risks in generated code, particularly in high-security programming tasks such as smart contract development for blockchain. Researchers have attempted to enhance the vulnerability awareness of these models by training them to differentiate between vulnerable and fixed code snippets. However, this approach relies heavily on manually labeled vulnerability data, which is only available for popular languages like Python and C++. For low-resource languages like Solidity, used in smart contracts, large-scale annotated datasets are scarce and difficult to obtain. To address this challenge, we introduce CodeBC, a code generation model specifically designed for generating secure smart contracts in blockchain. CodeBC employs a three-stage fine-tuning approach based on CodeLlama, distinguishing itself from previous methods by not relying on pairwise vulnerability location annotations. Instead, it leverages vulnerability and security tags to teach the model the differences between vulnerable and secure code. During the inference phase, the model leverages security tags to generate secure and robust code. Experimental results demonstrate that CodeBC outperforms baseline models in terms of BLEU, CodeBLEU, and compilation pass rates, while significantly reducing vulnerability rates. These findings validate the effectiveness and cost-efficiency of our three-stage fine-tuning strategy, making CodeBC a promising solution for generating secure smart contract code.

智能合约代码生成安全模型区块链

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。