[论文解读] LMs: Understanding Code Syntax and Semantics for Code Analysis
该研究评估大语言模型在多种语言下对代码语法和语义的理解在静态与动态分析中的表现,发现对语法理解较强但对动态语义存在局限,同时存在明显的数据漂移和幻觉风险。
Large language models~(LLMs) demonstrate significant potential to revolutionize software engineering (SE) by exhibiting outstanding performance in SE tasks such as code and document generation. However, the high reliability and risk control requirements in software engineering raise concerns about the lack of interpretability of LLMs. To address this concern, we conducted a study to evaluate the capabilities of LLMs and their limitations for code analysis in SE. We break down the abilities needed for artificial intelligence~(AI) models to address SE tasks related to code analysis into three categories: 1) syntax understanding, 2) static behavior understanding, and 3) dynamic behavior understanding. Our investigation focused on the ability of LLMs to comprehend code syntax and semantic structures, which include abstract syntax trees (AST), control flow graphs (CFG), and call graphs (CG). We employed four state-of-the-art foundational models, GPT4, GPT3.5, StarCoder and CodeLlama-13b-instruct. We assessed the performance of LLMs on cross-language tasks involving C, Java, Python, and Solidity. Our findings revealed that while LLMs have a talent for understanding code syntax, they struggle with comprehending code semantics, particularly dynamic semantics. We conclude that LLMs possess capabilities similar to an Abstract Syntax Tree (AST) parser, demonstrating initial competencies in static code analysis. Furthermore, our study highlights that LLMs are susceptible to hallucinations when interpreting code semantic structures and fabricating nonexistent facts. These results indicate the need to explore methods to verify the correctness of LLM output to ensure its dependability in SE. More importantly, our study provides an initial answer to why the codes generated by LLM are usually syntax-correct but vulnerable.
研究动机与目标
- 评估LLMs是否能够理解代码语法(AST 与表达式角色)。
- 评估LLMs理解静态行为(CFG、CG、数据依赖、污点、指针分析)的能力。
- 探究LLMs对动态行为(等价变异体、易出错的测试)的理解。
- 分析提示与数据源以确定代码分析任务的可靠性和局限性。
提出的方法
- 评估四种最先进的LLMs:GPT-4、GPT-3.5、StarCoder、CodeLlama-13b-instruct,覆盖9项任务。
- 设计测试语法理解的任务(AST 生成、表达式匹配)。
- 测试静态分析能力(CFG、CG、数据流、污点分析、指针分析)。
- 通过等价变异体检测和易出错的测试推理评估对动态行为的理解。
- 使用经过筛选的数据集(2,560 个数据点、151,633 LoC、217 个程序)以及多种语言(Python、Java、C、Solidity)。
- 采用基于角色的提示与指令提示,结合基于GPT的工具进行提示优化;将输出与 AST 解析器、专家评审以及真实工具(Tree-Sitter、Slither、Frama-C)进行对比评估。
实验结果
研究问题
- RQ1RQ1: Can LLMs understand code syntax well?
- RQ2RQ2: Can LLMs understand code static behaviors?
- RQ3RQ3: Can LLMs understand code dynamic behaviors?
主要发现
- GPT-4 和 GPT-3.5 在代码语法理解方面表现强劲,并且可以作为 AST 解析器使用。
- LLMs 对静态分析具有部分能力,能够进行 CFG 与 CG 生成以及类似数据流的初级分析。
- LLMs 在等价变异体检测和易出错的测试推理等动态语义任务上存在困难。
- 在不同项目上表现差异(数据漂移),数据依赖与污点分析的 F1 值在 0 到 0.8 之间。
- 开源模型(CodeLlama、StarCoder)在这些任务上的表现不如 OpenAI 模型。
- LLMs 能生成语法正确的代码,但可能对语义产生幻觉;在软件工程情境中需要对输出进行验证。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。