Context-aware Code Segmentation for C-to-Rust Translation using Large Language Models

Momoko Shiraishi, Takahiro Shinagawa
{"title":"Context-aware Code Segmentation for C-to-Rust Translation using Large Language Models","authors":"Momoko Shiraishi, Takahiro Shinagawa","doi":"arxiv-2409.10506","DOIUrl":null,"url":null,"abstract":"There is strong motivation to translate C code into Rust code due to the\ncontinuing threat of memory safety vulnerabilities in existing C programs and\nthe significant attention paid to Rust as an alternative to the C language.\nWhile large language models (LLMs) show promise for automating this translation\nby generating more natural and safer code than rule-based methods, previous\nstudies have shown that LLM-generated Rust code often fails to compile, even\nfor relatively small C programs, due to significant differences between the two\nlanguages and context window limitations. We propose an LLM-based translation\nscheme that improves the success rate of translating large-scale C code into\ncompilable Rust code. Our approach involves three key techniques: (1)\npre-processing the C code to better align its structure and expressions with\nRust, (2) segmenting the code into optimally sized translation units to avoid\nexceeding the LLM's context window limits, and (3) iteratively compiling and\nrepairing errors while maintaining consistency between translation units using\ncontext-supplementing prompts. Compilation success is an essential first step\nin achieving functional equivalence, as only compilable code can be further\ntested. In experiments with 20 benchmark C programs, including those exceeding\n4 kilo lines of code, we successfully translated all programs into compilable\nRust code without losing corresponding parts of the original code.","PeriodicalId":501278,"journal":{"name":"arXiv - CS - Software Engineering","volume":null,"pages":null},"PeriodicalIF":0.0000,"publicationDate":"2024-09-16","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"0","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"arXiv - CS - Software Engineering","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/arxiv-2409.10506","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
引用次数: 0

Abstract

There is strong motivation to translate C code into Rust code due to the continuing threat of memory safety vulnerabilities in existing C programs and the significant attention paid to Rust as an alternative to the C language. While large language models (LLMs) show promise for automating this translation by generating more natural and safer code than rule-based methods, previous studies have shown that LLM-generated Rust code often fails to compile, even for relatively small C programs, due to significant differences between the two languages and context window limitations. We propose an LLM-based translation scheme that improves the success rate of translating large-scale C code into compilable Rust code. Our approach involves three key techniques: (1) pre-processing the C code to better align its structure and expressions with Rust, (2) segmenting the code into optimally sized translation units to avoid exceeding the LLM's context window limits, and (3) iteratively compiling and repairing errors while maintaining consistency between translation units using context-supplementing prompts. Compilation success is an essential first step in achieving functional equivalence, as only compilable code can be further tested. In experiments with 20 benchmark C programs, including those exceeding 4 kilo lines of code, we successfully translated all programs into compilable Rust code without losing corresponding parts of the original code.
使用大型语言模型进行上下文感知代码分割以实现 C 到 Rust 翻译
虽然大型语言模型(LLM)有望通过生成比基于规则的方法更自然、更安全的代码来实现自动翻译,但之前的研究表明,由于两种语言之间的显著差异和上下文窗口的限制,LLM生成的Rust代码往往无法编译,即使是相对较小的C程序也是如此。我们提出了一种基于 LLM 的翻译方案,可以提高将大规模 C 代码翻译为可编译 Rust 代码的成功率。我们的方法涉及三项关键技术:(1) 预处理 C 代码,使其结构和表达式更好地与 Rust 保持一致;(2) 将代码分割成最佳大小的翻译单元,避免超出 LLM 的上下文窗口限制;(3) 迭代编译和修复错误,同时使用上下文补充提示保持翻译单元之间的一致性。编译成功是实现功能等效的第一步,因为只有可编译代码才能进一步测试。在对 20 个基准 C 程序(包括超过 4 千行代码的程序)进行的实验中,我们成功地将所有程序都翻译成了可编译的 Rust 代码,而没有丢失原始代码的相应部分。
本文章由计算机程序翻译,如有差异,请以英文原文为准。
求助全文
约1分钟内获得全文 求助全文
来源期刊
自引率
0.00%
发文量
0
×
引用
GB/T 7714-2015
复制
MLA
复制
APA
复制
导出至
BibTeX EndNote RefMan NoteFirst NoteExpress
×
提示
您的信息不完整,为了账户安全,请先补充。
现在去补充
×
提示
您因"违规操作"
具体请查看互助需知
我知道了
×
提示
确定
请完成安全验证×
copy
已复制链接
快去分享给好友吧!
我知道了
右上角分享
点击右上角分享
0
联系我们:info@booksci.cn Book学术提供免费学术资源搜索服务,方便国内外学者检索中英文文献。致力于提供最便捷和优质的服务体验。 Copyright © 2023 布克学术 All rights reserved.
京ICP备2023020795号-1
ghs 京公网安备 11010802042870号
Book学术文献互助
Book学术文献互助群
群 号:481959085
Book学术官方微信