{"title":"使用大型语言模型进行上下文感知代码分割以实现 C 到 Rust 翻译","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":"{\"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}","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
摘要
虽然大型语言模型(LLM)有望通过生成比基于规则的方法更自然、更安全的代码来实现自动翻译,但之前的研究表明,由于两种语言之间的显著差异和上下文窗口的限制,LLM生成的Rust代码往往无法编译,即使是相对较小的C程序也是如此。我们提出了一种基于 LLM 的翻译方案,可以提高将大规模 C 代码翻译为可编译 Rust 代码的成功率。我们的方法涉及三项关键技术:(1) 预处理 C 代码,使其结构和表达式更好地与 Rust 保持一致;(2) 将代码分割成最佳大小的翻译单元,避免超出 LLM 的上下文窗口限制;(3) 迭代编译和修复错误,同时使用上下文补充提示保持翻译单元之间的一致性。编译成功是实现功能等效的第一步,因为只有可编译代码才能进一步测试。在对 20 个基准 C 程序(包括超过 4 千行代码的程序)进行的实验中,我们成功地将所有程序都翻译成了可编译的 Rust 代码,而没有丢失原始代码的相应部分。
Context-aware Code Segmentation for C-to-Rust Translation using Large Language Models
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.