0. Contents
- Program Language and Static Analysis
- Why We Learn Static Analysis
- What is Static Analysis
- Bird‘s Eye View for Static Analysis
1. Program Language and Static Analysis
SA(Static Analysis)是 PL(Program Language) 的一个部分。
PL 具有 3 个部分:
- Theory:程序语言的理论
- Environment:理论的实现环境
- Application:在环境的具体应用
如下图所示:

虚线红框的内容就是指 Static Analysis,它是 Program Language 在应用中的一个子集
2. Why We Learn Static Analysis
为什么 Static Analysis 很重要?
其原因在于我们对程序语言的语用需求增加了,那么就导致了程序代码变得越来庞大且复杂,这就进一步带来了一系列挑战:程序可靠性难达到,安全性难保证,性能提升,程序理解难(IDE对程序代码的识别)
Static Analysis 能干嘛?
所以,我们需要用 Static Analysis 技术来进行:
- 程序的可靠性(Program Reliability)
- 程序的安全性(Program Security)
- 编译器优化(Compiler Optimization)
- 程序理解(Program Understanding)
3. What is Static Analysis
3.1 The Defination of Static Analysis
Static analysis analyzes a program P P P to reason about its behaviors and determines whether it satisfies some properties before running P P P.
静态分析就是在程序运行前,去了解程序的行为并且确定这些行为能不能被实现。
比如:
- 私有信息泄露
- 空指针引用异常(指针是否初始化了)
- CAST 的正确使用(比如 C++ cast功能)
- 是否多个指针指向同一块内存地址
- assert 是否会 fail (用于在判断时加入的 assert 函数会不会在运行时 fail)
- 是否具有死代码
很遗憾,根据 Rice 定理:不可能通过静态分析来准确地判断程序的真实行为。
3.2 Rice’s Theorem
Any non-trivial property of the behavior of programs in a recursively enumerable language is undecidable.
递归可枚举语言中的程序行为的任何非平凡性质都是不可判定的。
什么是递归可枚举语言(recursively enumerable language)?
就是图灵机可识别的语言,基本上能见到的编程语言都是递归可枚举语言。
什么是 non-trivial property?
A property is trivial if either it is not satisfied by any recursively enumerable language, or if it is satisfied by all recursively enumerable languages; otherwise it is non-trivial.
简单来说:non-trivial 的性质就是有趣的性质,只要跟运行时的行为沾边的性质都是 non-trivial 的性质。
总的来说,根据 Rice’s Theorem , 一个 Perfect 的 Static Analysisi 是不存在的!
3.3 Sound 和 Complete
什么是 perfect ? perfect 就是既 Sound 又 Complete !
Sound
Sound 的意思就是 Overapproximate(过近似)
Complete
Complete 的意思就是 Underapproximate(欠近似)
他们的关系如下图所示

4. Bird‘s Eye View for Static Analysis
4.1 用一句话概括静态分析
Static Analysis: ensure (or get close to) soundeness, while making good trade-offs between analysis precision and analysis speed.
确保 soundeness 的前提下,使得精度和速度具有有效的平衡,才是一个有用的 Static Analysis。
4.2 用两个词来总结静态分析
Abstraction + Overapproximate
(抽象)+(过近似)
4.3 一个例子
Determine the sign (+, -, or 0) of all the variables of a given program.
Abstraction
把程序中真实的值,抽象成我们分析的值

Overapproximate: Transfer Function
设计我们的 Transfer Function(转换函数),即定义一套规则,把抽象值进行转换。比如我们设计的转换规则如下:

根据这套规则,我们来分析一个程序,从而可以得到如图中的 3 个结论:

静态程序分析是程序语言应用中的一个子集,用于在程序运行前分析其行为以确保可靠性和安全性,同时进行编译器优化和程序理解。然而,根据Rice定理,完美的静态分析不存在,因为它无法准确预测所有程序行为。静态分析的核心在于在精度和速度之间取得平衡,通过抽象和过近似方法提供近似的程序行为分析。
48

被折叠的 条评论
为什么被折叠?



