Anthropic发布Claude形式化验证研究:RISC-V级百个库自动证明
LLMs are making formal software verification feasible at a much larger scale
形式化验证是AI工程落地的关键深水区,这篇报告展示了Claude在底层代码证明上的硬核能力,给安全开发提供了新范式,值得深入阅读。
Chapter 01: Introduction
第一章:简介
Keane Lucas, Aengus Lynch, Luna Tong, Luke Champine, Zygi Straznickas, Dino Distefano, Harshikaa Agrawal
Evyatar Ben Asher, Ava Howell, Sami Jawhar, Chris Rohlf, Nickolai Zeldovich
Nicholas Carlini, Rajashree Agrawal, Jason Gross
Most software is riddled with bugs, which AI can now easily find and exploit. The standard defenses against cyberattacks are no longer sufficient: we need ways to prove that code is free of bugs. Formal software verification could provide this guarantee, but it has historically required years of expert effort, making it impractical for most real-world projects. In a collaboration with Theorem, however, we show this is no longer the case: LLMs can now produce formal proofs at large scale, enabling new software development workflows that include proven security guarantees. Using Claude, we proved security properties at the assembly code level (RISC-V) in over 100 real-world open-source software libraries. To our knowledge, this represents the first large-scale effort to use AI to apply formal verification to production software at the assembly code level. While writing the proofs, Claude also discovered bugs that had eluded detection for years, despite trillions of test runs. As model capabilities continue to improve, we expect formal verification to become an essential part of secure software development.
大多数软件都充斥着漏洞,而人工智能现在可以轻松发现并利用这些漏洞。针对网络攻击的标准防御措施已不再足够:我们需要证明代码没有漏洞的方法。形式化软件验证可以提供这种保证,但历史上这需要专家花费数年时间,使其对大多数实际项目来说不切实际。然而,在与 Theorem 的合作中,我们表明情况已不再如此:大型语言模型(LLMs)现在可以大规模生成形式化证明,从而启用包含经过验证的安全保证的新软件开发工作流。使用 Claude,我们在超过 100 个真实世界的开源软件库中证明了汇编代码级别(RISC-V)的安全属性。据我们所知,这代表了首次大规模利用人工智能在汇编代码级别将形式化验证应用于生产软件的尝试。在编写证明的过程中,Claude 还发现了多年来一直未被检测到的漏洞,尽管测试运行次数已达万亿次。随着模型能力的不断提升,我们预计形式化验证将成为安全软件开发的重要组成部分。
Introduction
简介
Even mature, heavily tested software contains bugs, and LLMs are increasingly adept at finding them. The Linux kernel—one of the most well-tested pieces of software today, deployed on billions of devices worldwide—recently had over 1,000 vulnerabilities disclosed. With frontier models becoming more capable of chaining seemingly benign bugs into critical exploits, we need techniques that can detect and fix these bugs before they can be exploited in the wild.
即使是成熟且经过大量测试的软件也包含漏洞,而大型语言模型越来越擅长发现它们。Linux 内核——当今测试最充分的软件之一,部署在全球数十亿台设备上——最近披露了超过 1,000 个漏洞。随着前沿模型越来越能够将看似无害的漏洞链式组合成关键性利用手段,我们需要能够在漏洞被野外利用之前检测和修复这些漏洞的技术。
Ideally, such techniques could find and patch all bugs, and then provide some proof that there are no more to be found. Formal verification is one way to do this. In formal verification, developers translate their code into a mathematical model, then try to prove that it always follows some defined set of security rules. If they fail to prove this, that means there could be a bug in the code that would violate a rule, and the nature of the failure implies what the bug might be. Repeating this process (after eliminating the cause of failure each time) will ultimately yield proof that the code is devoid of any bug that would violate those rules.
理想情况下,此类技术能够发现并修补所有漏洞,然后提供证明表明没有其他漏洞可寻。形式化验证是实现这一目标的一种方式。在形式化验证中,开发人员将他们的代码转化为数学模型,然后试图证明它始终遵循某些定义的安全规则集。如果无法证明这一点,则意味着代码中可能存在违反规则的漏洞,而失败的性质暗示了漏洞可能是什么。重复此过程(每次消除失败原因后),最终将产生证明,表明代码中不存在任何违反这些规则的漏洞。
However, formal verification isn’t easy. Proofs can be many times larger than the original code, and writing them requires significant effort and a niche, highly specialized skill set. For instance, it took two person-years to implement the seL4 operating system kernel, but 20 person-years to formally verify it. Accordingly, formal verification has been practical only in truly mission-critical applications, such as Airbus’s avionics code.
然而,形式化验证并不容易。证明文件的规模可能是原始代码的数倍,且编写它们需要巨大的努力以及一套小众、高度专业化的技能。例如,实现 seL4 操作系统内核花费了两个人年,而对其进行形式化验证则花费了 20 个人年。因此,形式化验证仅在真正关键的任务型应用中具有实用性,例如空客(Airbus)的航空电子代码。
But LLMs are making it far easier. Frontier models can now produce correctness proofs for real-world software fully autonomously. To demonstrate this, we asked Claude to prove useful security properties across 123 open-source libraries, including showing that some of their executable artifacts can never crash under certain conditions. Specifically, Claude proved that these programs will always exit normally without triggering memory-safety errors or failing assertions, for every input less than 64 MB and every modeled Linux operating system (OS) response and memory layout, under certain assumptions (namely that the program runs as a single-threaded static binary with a fixed memory budget on a RISC-V1 CPU using Linux OS). This is still far away from fixing all possible bugs, but it does get us much closer than before and provides a roadmap toward that end.
但大语言模型(LLMs)使其变得容易得多。前沿模型现在能够完全自主地为真实世界的软件生成正确性证明。为了展示这一点,我们要求 Claude 对 123 个开源库证明有用的安全属性,包括证明其中一些可执行工件在某些条件下永远不会崩溃。具体而言,Claude 证明了这些程序在特定假设下(即程序在基于 RISC-V1 CPU 和 Linux 操作系统的固定内存预算上以单线程静态二进制文件运行),对于小于 64 MB 的每个输入以及每个模拟的 Linux 操作系统(OS)响应和内存布局,始终正常退出而不会触发内存安全错误或断言失败。这距离修复所有可能的错误仍有很大差距,但它确实让我们比之前更接近这一目标,并提供了一条通往该目标的路线图。
We’re publishing a subset of these proofs so that anyone can verify and reuse them, along with the RISC-V and OS semantics. Theorem, our collaborator on this project, is also offering to run the same pipeline for other open-source projects, and is publishing a Claude skill to help developers get started with verifying their own code.
我们正在发布这些证明的一个子集,以便任何人都可以验证和重用它们,同时发布的还有 RISC-V 和 OS 语义。本项目的合作伙伴 Theorem 也愿意为其他开源项目运行相同的流程,并发布了一项 Claude 技能,以帮助开发人员开始验证他们自己的代码。
Formal verification is a broad field with many methodologies. Below, we also describe our initial explorations using other formal verification methods to accelerate other security or software development tasks, such as assessing the security of the zlib compression algorithm, proving behavioral equivalence of a Python interpreter written in Rust, and rooting out long-standing bugs in critical software.
形式化验证是一个拥有众多方法论的广泛领域。在下面,我们还描述了使用其他形式化验证方法进行初步探索的情况,以加速其他安全或软件开发任务,例如评估 zlib 压缩算法的安全性、证明用 Rust 编写的 Python 解释器的行为等价性,以及根除关键软件中长期存在的错误。
The road ahead for formal verification is promising. The cost of writing proofs is falling as models become more intelligent. Moreover, as more code is formalized and methods of achieving security guarantees become more standardized, the ability to reuse or modify proofs across codebases will amortize the cost. We encourage all developers to explore formal verification with LLMs—especially those who previously tried it with less capable models and found it didn’t work.
形式化验证的前途一片光明。随着模型变得越来越智能,编写证明的成本正在下降。此外,随着越来越多的代码被形式化,以及实现安全保证的方法变得更加标准化,跨代码库重用或修改证明的能力将分摊成本。我们鼓励所有开发人员探索使用大语言模型(LLM)进行形式化验证——尤其是那些之前尝试过能力较弱的模型但发现行不通的开发人员。
In the rest of this post, we cover our formal verification experiments in more detail.
在本文的其余部分,我们将更详细地介绍我们的形式化验证实验。
Turning tests into proofs
将测试转化为证明
To prove that a piece of software is correct, we must first state what “correct” even means. Correctness is defined by the specification (spec). In some programs, there is a clear way to formally specify the program’s intended behavior. For example, a PNG decoder takes a sequence of bytes and (following RFC 2083) decodes these bytes into the corresponding image. But for most programs, things aren’t so straightforward. What would it mean for a text editor or a web browser to be “correct”? The difficulty of obtaining a comprehensive and unambiguous spec is a significant obstacle to verifying software at a larger scale.
要证明一段软件是正确的,我们必须首先阐明“正确”究竟意味着什么。正确性由规范(spec)定义。在某些程序中,可以明确地以形式化方式指定程序的预期行为。例如,PNG 解码器接收一系列字节,并(遵循 RFC 2083 标准)将这些字节解码为对应的图像。但对于大多数程序来说,情况并非如此简单。文本编辑器或 Web 浏览器所谓的“正确”是什么意思?获取全面且无歧义的规范是大规模验证软件的一个重大障碍。
To address this for the code in the open-source libraries that we sought to prove, we take the approach of automatically converting their preexisting compilable test suites into a spec by encoding the rule “this test suite always passes,” using a general model of what it means to execute any RISC-V-compiled binary on Linux. Historically, proving such specs was intractable, given the inherent complexity of writing a proof that can encompass and make an argument over all possible behaviors and interactions of tens of thousands of assembly instructions over any input (and the fact that proofs tend to be extremely large as a result). This is where today’s LLMs come in: churning through millions of lines of code in pursuit of a well-defined goal is a task that LLMs are extremely well suited for.
为了应对我们在试图证明其正确性的开源库代码中的这一问题,我们采取的方法是将它们现有的可编译测试套件自动转换为规范,通过编码“该测试套件始终通过”这一规则,并使用一个关于在 Linux 上执行任何 RISC-V 编译二进制文件含义的通用模型。从历史上看,证明此类规范是难以处理的,因为编写能够涵盖并对数万条汇编指令在任何输入下的所有可能行为和交互进行论证的证明,其内在复杂性极高(而且因此证明往往极其庞大)。这正是当今大语言模型发挥作用的地方:在处理数百万行代码以追求一个明确定义的目标方面,大语言模型具有极高的适配性。
For this project, we started with fuzz tests. In these tests, developers write an entry point2 into their code that takes inputs, such that any input that causes a crash is a bug the authors care about. Then the fuzzer, a testing engine, generates enormous numbers of semirandom and mutated inputs at the entry point to try to find a bug (visualized in Figure 1). The longer a fuzzer runs, the more bugs it can find. But this is inherently a brute-force process, with no guarantee of exercising every possible code path, regardless of how many inputs you try.
对于这个项目,我们从模糊测试(fuzz tests)开始。在这些测试中,开发者在代码中编写一个入口点2,该入口点接收输入,任何导致崩溃的输入都是作者所关注的 bug。然后,模糊测试引擎(fuzzer)会在该入口点生成海量半随机和经过变异的输入,试图发现 bug(如图 1 所示)。模糊测试运行时间越长,发现的 bug 就越多。但这本质上是一个暴力过程,无论尝试多少输入,都无法保证覆盖所有可能的代码路径。
Fuzzing works by running huge numbers of generated inputs through one entry point to find the inputs that cause a crash. There are infinite inputs, so the search can never be complete.
模糊测试的工作原理是将大量生成的输入通过同一个入口点运行,以找出会导致崩溃的输入。由于输入空间是无限的,因此搜索永远无法穷尽。
To find all the bugs that would’ve been surfaced by a fuzz test, we turned the fuzz test into a spec defined over the compiled RISC-V instructions. The spec says that the fuzz test won’t crash for any input less than 64 MB.3 If Claude can prove this spec, the fuzz test on RISC-V becomes superfluous: fuzzing that entry point will never find any bugs.
为了找出模糊测试本应暴露的所有 bug,我们将模糊测试转化为针对编译后的 RISC-V 指令定义的形式化规范(spec)。该规范指出:对于小于 64 MB 的任何输入,模糊测试都不会崩溃3。如果 Claude 能够证明这一规范,那么在 RISC-V 上的模糊测试就变得多余了:对该入口点进行模糊测试将永远不会发现任何 bug。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力