Skip to content

Commit

Permalink
Update Lec 11-12 on context-sensitivity. End of Pointing Analysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
RangerNJU authored and LaplaceDem0n committed Dec 17, 2020
1 parent b6afa81 commit 0d70e73
Show file tree
Hide file tree
Showing 49 changed files with 536 additions and 103 deletions.
File renamed without changes
File renamed without changes
Binary file added .gitbook/assets/harmonyOS (4) (3).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/harmonyOS (4) (4).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/harmonyOS (4).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210134143907.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210134328575.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210135019104.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210135047052.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210145819255.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210151627827.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210165334864.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210183358390.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210183700963.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210184448955.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210184527645.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210184850528.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210191310040.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210192001954.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210193321202.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210193735607.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210195519912.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210200518022.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210200637330.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201210201540194.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .gitbook/assets/image-20201216173124146.png
Binary file added .gitbook/assets/image-20201216175534756.png
Binary file added .gitbook/assets/image-20201216175748133.png
Binary file added .gitbook/assets/image-20201216180114246.png
Binary file added .gitbook/assets/image-20201216180216650.png
Binary file added .gitbook/assets/image-20201216180422745.png
Binary file added .gitbook/assets/image-20201216180523896.png
Binary file added .gitbook/assets/image-20201216202538515.png
Binary file added .gitbook/assets/image-20201216204823876.png
Binary file added .gitbook/assets/image-20201216205125278.png
Binary file added .gitbook/assets/image-20201216210158092.png
Binary file added .gitbook/assets/image-20201216210347469.png
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ Getting started with static program analysis. Read this and start writing your f

> ❓ How to automatically and efficiently guarantee software quality
静态程序分析入门。阅读此书并着手编写你的第一个静态程序分析器吧!本仓库关注一个非常重要的问题:
静态程序分析入门。阅读此书并着手编写你的第一个静态程序分析器吧!本仓库关注一个非常重要的问题:

> ❓ 如何自动化地高效保障软件质量
* [《静态程序分析》Gitbook在线阅读地址](https://ranger-nju.gitbook.io/static-program-analysis-book/)
* 有时gitbook需要科学上网才能访问,没法科学上网的小伙伴可以试试访问[这里](https://spa-book.pblo.gq/)
* [《静态程序分析》GitHub项目地址](https://github.com/RangerNJU/Static-Program-Analysis-Book)
* 离线阅读方式
1. 将本仓库**下载**到本地(安装Git后,在命令行中执行命令`git clone https://github.com/RangerNJU/Static-Program-Analysis-Book.git`
2. 周期性地**更新**,在仓库目录下执行`git pull`
3. 使用[Typora](https://typora.io/)等本地Markdown阅读器**阅读**

### 表达你的声音 👂
### 表达你的声音 👂

* **批评的意见很有价值。** 这是我第一次书写教程,一定有很多做得不好的地方。如果你觉得有值得修改或值得讨论的地方(包括但不仅限于行文风格,内容准确性,图例与解释的易读性等等),可以选择:

1. 加入QQ群(951182704)匿名或实名表达你的意见与看法
2. 提issue
3. 通过邮箱联系我(ranger.nju\#gmail.com\)
* 如果你觉得我写得不错,可以到GitHub仓库中给我一个Star,也可以在自己的社交圈子中宣传,让更多的人了解这个项目。

* 加入QQ群(951182704)匿名或实名表达你的意见与看法
* 提issue
* 通过邮箱联系我(ranger.nju\#gmail.com\)
* 如果你觉得我写得不错,可以到GitHub仓库中给我一个Star,也可以在自己的社交圈子中宣传,让更多的人了解这个项目。

### 更新记录与里程碑事件

1. Oct, 2020. 设立Repo,一个月内获得Star、Fork和PR 🥳
1. Oct, 2020. 设立Repo,一个月内解锁Star、Fork和PR
2. Nov. 将IR与Data Flow Analysis的相关内容暂时移出仓库,更新七至十课——Interprocedural Analysis、Pointer Analysis-Introduction and Foundations。
3. Dec. 更新十一和十二两课——Context Sensitive Pointer Analysis,指针分析大结局。🥳

## 这一《静态程序分析》教程对谁有用?

Expand Down Expand Up @@ -58,7 +58,7 @@ Getting started with static program analysis. Read this and start writing your f

#### 编程语言的分类

当今的计算机世界,面对这样一条恶龙: 👇
当今的计算机世界,面对这样一条恶龙: 👇

> 数十年来语言的核心没有变化,但软件的规模和复杂性增长迅速,如何保证程序的可靠性?
Expand Down Expand Up @@ -126,14 +126,16 @@ Getting started with static program analysis. Read this and start writing your f
1. 学术门槛较高,学习者必须有良好的数学基础才能入门。
2. 验证代价较高,一般来说非常重要的项目会使用这一方式保证程序质量。甚至在操作系统这样重要的软件中,也并不一定会使用。\(截图来自鸿蒙OS直播发布会\)

![](.gitbook/assets/harmonyOS%20%281%29.png)
![](.gitbook/assets/harmonyOS%20%284%29%20%284%29.png)

## 加入项目/How to contribute

> 觉得有所帮助的话可以点个star支持哦。
欢迎希望添加更好的讲解资料或对教程内容进行扩充的小伙伴 `fork, modify, PR` 三连。

**提醒:引用图片时请使用相对路径。**

## 本地化/Localization

We'd love help translating this book! Open a new issue to start working on a new language. Feel free to start :\)
Expand Down
27 changes: 21 additions & 6 deletions SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
# Table of contents

* [简介](README.md)

## 前言 <a id="ch0"></a>

* [写在前面](ch0/README.md)
* [为什么是这本书?](ch0/00-01-why-this-book.md)
* [资料来源与版权信息](ch0/00-02-sources-and-license.md)
* [静态程序分析简介与数据流分析](ch1.md)
* [过程间分析](ch2/README.md)
* [过程间分析简介](ch2/02-01-inter-analysis-spa.md)
* [指针分析简介](ch2/02-02-pointer-analysis-spa.md)
* [指针分析理论一](ch2/02-03-pointer2-analysis-spa.md)
* [指针分析理论二](ch2/02-04-pointer3-analysis-spa.md)

## 数据流分析 <a id="ch1"></a>

* [静态程序分析简介与数据流分析](ch1/ch1.md)

## 过程间分析 <a id="ch2"></a>

* [过程间分析简介](ch2/ch2.md)

## 指针分析 <a id="ch3"></a>

* [指针分析简介](ch3/pointer-analysis/README.md)
* [指针分析简介](ch3/pointer-analysis/03-01-pointer-analysis-spa.md)
* [指针分析理论(上)](ch3/pointer-analysis/03-02-pointer2-analysis-spa.md)
* [指针分析理论(下)](ch3/pointer-analysis/03-03-pointer3-analysis-spa.md)
* [指针分析进阶](ch3/context-sensitivity/README.md)
* [上下文敏感分析(上)](ch3/context-sensitivity/03-04-context-sensitivity.md)
* [上下文敏感分析(下)](ch3/context-sensitivity/03-05-cs2.md)

File renamed without changes.
9 changes: 0 additions & 9 deletions ch2/README.md

This file was deleted.

15 changes: 10 additions & 5 deletions ch2/02-01-inter-analysis-spa.md → ch2/ch2.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# 过程间分析简介

**阅读提示:使用屏幕较大的设备能够看到自带的Sticky Table of Contents,更有利于理清阅读思路。**

对应视频在:

* [第七课-过程间分析](https://www.bilibili.com/video/BV1GQ4y1T7zm)
* [第八课-指针分析](https://www.bilibili.com/video/BV1gg4y1z78p)

## 过程间分析简介

本小节通过四个部分介绍过程间分析。
Expand Down Expand Up @@ -178,15 +185,15 @@ A:分别调用A和C中定义的foo方法。

![](../.gitbook/assets/image-20201029230504891.png)

2. 处理main后向WL中加入A.foo\(\)
1. 处理main后向WL中加入A.foo\(\)

![](../.gitbook/assets/image-20201029230535984.png)

3. 中间省略一些步骤,这里面对C.bar\(\)时,虽然会调用A.foo\(\),但由于A.foo\(\)之前已经处理过(在集合RM中),之后不会再进行处理
1. 中间省略一些步骤,这里面对C.bar\(\)时,虽然会调用A.foo\(\),但由于A.foo\(\)之前已经处理过(在集合RM中),之后不会再进行处理

![](../.gitbook/assets/image-20201029230622120.png)

4. 这里C.m\(\)是不可达的死代码
1. 这里C.m\(\)是不可达的死代码

![](../.gitbook/assets/image-20201029230909895.png)

Expand Down Expand Up @@ -260,8 +267,6 @@ Edge transfer处理引入的call & return edge。为此,我们需要**在之

## Key points

**The X You Need To Understand in This Lecture**

1. How to build call graph via class hierarchy analysis
* 如何利用CHA构建调用关系图\(call graph\)
2. Concept of interprocedural control-flow graph
Expand Down
153 changes: 153 additions & 0 deletions ch3/context-sensitivity/03-04-context-sensitivity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# 上下文敏感分析(上)

> 上下文敏感分析是提高指针分析精度最有效的技术,没有之一。
本课分为以下五个部分:

1. Introduction\(Example\)
2. Introduction\(Theory\)
3. Context Sensitive Pointer Analysis: Rules
4. Context Sensitive Pointer Analysis: Algorithms
5. Context Sensitivity Variants

在上半篇中我们讲解前三个部分,下半篇会继续讲最后的两个部分。

## Introduction\(example\)

首先用一个例子直观地说明上下文不敏感分析的问题所在。

```java
void main() {
Number n1, n2, x, y;
n1 = new One(); // 𝑜1
n2 = new Two(); // 𝑜2
x = id(n1);
y = id(n2);
int i = x.get();
//假设使用我们之前所讲述的上下文不敏感分析
//这里i的常量分析结果是什么?
}
Number id(Number n) {
return n;
}
interface Number {
int get();
}
class One implements Number {
public int get() { return 1; }
}
class Two implements Number {
public int get() { return 2; }
}
```

使用我们之前描述的算法,由于不考虑调用的顺序,会得到这样的PFG。

![](../../.gitbook/assets/image-20201210134143907.png)

也就因此造成了分析结果的低精度,即在动态运行时i一定是1,而分析的结果认为i为NAC\(Not a constant\)

![](../../.gitbook/assets/image-20201210134328575.png)

如果我们使用上下文敏感的分析方式,区分不同时间对的id调用,则会得到这样的PFG:

![](../../.gitbook/assets/image-20201210135019104.png)

对应地也就能得到更为精确的分析结果:

![](../../.gitbook/assets/image-20201210135047052.png)

## Introduction\(Theory\)

### C.I.\(Context Insensitive\)

是什么原因导致了上下文不敏感分析的低精度?

1. 在动态执行时,对同一个函数的不同调用,往往有着不同的调用上下文(calling contexts),如上一小节的例子中两次对id的调用。
2. 不同的调用上下文会被混合并传播,进而形成假的数据流。如上一小节的例子中指针x和y指向两个目标。

### C.S.\(Context Sensitive\)

上下文敏感分析通过区分不同调用上下文的数据流,对调用上下文建模。举个例子,在这一段代码中,对id调用的上下文就是两行call-site,记为\[1\]\[2\]

```java
x = id(n1); //context 1 for id()
y = id(n2); //context 2 for id()
int i = x.get();

Number id(Number n) {
return n;
}
```

进而,我们可以**通过对同一函数的不同调用添加标号进行区分**,而得到更精确的PFG:

![](../../.gitbook/assets/image-20201210145819255.png)

### C.S. heap

**对于Java中被动态分配到heap上的对象,我们也需要对他们做相应的标记以提高分析精度。**例子:~~(或许真的需要录视频了?)~~

* 不做标记时,第八行new出来的对象无法区分,只能同一记作 $$o_8$$
* 做标记后,可以分别记为$$ 3:o_8$$$$ 4:o_8$$

![](../../.gitbook/assets/image-20201210151627827.png)

不过,采取C.I.+ C.S. heap进行分析时,C.S. heap就不能提高精度了。

![](../../.gitbook/assets/image-20201210165334864.png)

## Context Sensitive Pointer Analysis: Rules

### Domains and Notations

接下来我们用介绍适用于上下文敏感分析的规则。

首先我们讨论的Domain中,methods/variables/objects都升级为带有上下文标识的。

![](../../.gitbook/assets/image-20201216173124146.png)

新引入符号说明:引入C表示所有的上下文组成的集合,c表示具体的某个上下文。

值得一提的是,fields不需要带有上下文标记,因为field总是依赖于某一个object。只要object被标记进而通过上下文可被区分了,fields自然也可以被区分。一个不太准确但是可以帮助理解的例子是,如果你能区分一对双胞胎,那么他/她们口袋里的东西也是可以被区分的。

### Rules

首先我们考虑不包含调用(Call)语句的四类语句所对应的规则。

![](../../.gitbook/assets/image-20201216175534756.png)

对比一下上下文不敏感指针分析的规则:

![](../../.gitbook/assets/image-20201216175748133.png)

**唯一的区别在于,对象被加上了上下文标识。**

然后我们来看看调用如何处理。在上下文敏感的指针分析中,规则如下:

![](../../.gitbook/assets/image-20201216180216650.png)

再次对比一下上下文不敏感指针分析的规则:

![](../../.gitbook/assets/image-20201216180114246.png)

**这次我们添加了一个Select函数,它的作用是为object添加上下文标识,例如:**

* 为参数添加上下文标识

![](../../.gitbook/assets/image-20201216180422745.png)

* 为返回值添加上下文标识

![](../../.gitbook/assets/image-20201216180523896.png)

**也就是说,上下文的信息是在处理调用时添加的。**

## Key points

* Concept of context sensitivity \(**C.S.**\)
* Concept of context-sensitive heap \(**C.S. heap**\)
* **Why** C.S. **and** C.S. heap improve **precision**
* 单独使用C.S.或C.S. heap依然会损失精度
* Context-sensitive pointer analysis **rules**

Loading

0 comments on commit 0d70e73

Please sign in to comment.