> For the complete documentation index, see [llms.txt](https://p4d0rn.gitbook.io/java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://p4d0rn.gitbook.io/java/code-inspector/theory/static-analysis/dfa.md).

# DFA

## Overview of DFA

DFA：Data Flow Analysis

How application-specific data flows through the nodes and edges of CFG（source code->IR->CFG）

> most static analyzer tends to may analysis
>
> * may analysis
>   * outputs information that may be true (over-approximation)
> * must analysis
>   * outputs information that must be true (under-approximation)

different data-flow analysis applications have

different data abstraction and

different flow safe-approximation strategies and

different transfer functions and control-flow handlings

> e.g., determine the sgin of a variable
>
> data abstraction：+、-、0、unknown、undefined
>
> transfer function：+ op + = + , + op - = -
>
> control-flow handlings：union the signs at merges

## Preliminaries of DFA

### Input and Output States

* Each execution of an IR statement transforms an input state to a new output state
* The input/output state is associated with the program point before/after the statement
* ![image-20240312212229562](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-3f21867c49a244a64086ea7a11135251e305e532%2Fimage-20240312212229562.png?alt=media)

> In each data-flow analysis application, we associate with every program point a data-flow value that represents an abstraction of the set of all possible program states that can be observed for that point.
>
> <img src="https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-09be77e848ca0ce3ed0f27a1ea838a634e792e2f%2Fimage-20240404102713528.png?alt=media" alt="image-20240404102713528" data-size="original">

DFA is to find a solution to a set of safe-approximation-directed constraints on the IN\[s]’s and OUT\[s]’s for all statements

* constraints based on semantics of statements（transfer function）
* constraints based on the flows of control

### Transfer Function

![image-20240312213047748](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-22629e83e5d6acb598503a5afe2b66adcb9eff09%2Fimage-20240312213047748.png?alt=media)

### Control Flow

![image-20240312213409543](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-666b0bd98bcd67bb548a8e5887f5a33c030b0efa%2Fimage-20240312213409543.png?alt=media)

## Reaching Definitions analysis

> A definition d at program point p reaches a point q if there is path from p to q such that d is not “killed” along that path

* A definition of a variable v is a statement that assigns a value to v
* how to be “killed”: new definition of v

![image-20240404104015211](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-f26f67c2a104d50cbb133ba1effad101ab496e58%2Fimage-20240404104015211.png?alt=media)

Reaching definitions can be used to detect possible undefined variables.

> e.g., introduce a dummy definition for each variable v at the entry of CFG, and if the dummy definition of v reaches a point p where v is used, then v may be used before definition (as undefined reaches v)

So reaching definitions analysis is may analysis.

(不放过动态运行时所有可能的路径)

### Abstraction

The definitions of all the variables in a program can be represented by bit vectors.

![image-20240404105015142](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-a23fdfab152729dac2c96bbbd7664112c7ad7e3e%2Fimage-20240404105015142.png?alt=media)

### Safe-Approximation

#### Transfer Function

![image-20240404110234375](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-60eab65ab84021901e51df349f18aa57396ffb90%2Fimage-20240404110234375.png?alt=media)

![image-20240404105623734](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-b6025cdf24ef1c1d19a0ddc71c75db9dd3646dba%2Fimage-20240404105623734.png?alt=media)

> genB：definitions generated in this BB.
>
> killB: new definition D in this BB kills other definitions
>
> The definitions in genB can reach the program point at OUT\[B] and those in killB can't reach the program point at OUT\[B]
>
> obviously, same variables in definition bit vectors can not exist at the same time.

#### Control Flow

![image-20240404110250025](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-472502913931a0878d5db8a7185b25befdf2d400%2Fimage-20240404110250025.png?alt=media)

reaching definition is a may analysis.So here use union(∪) as the meet operator.

#### Algorithm

![image-20240404110529658](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-0c1be94b3a382ed2b6c7f16464a160fad446eb5b%2Fimage-20240404110529658.png?alt=media)

For Example:

![image-20240404111804621](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-25a3a469c49900f3b8cd44054c753968e53a05cd%2Fimage-20240404111804621.png?alt=media)

CFG above can be transfered to pseudo-code below.

```
D1
D2
do {
	D3
	D4
	if(condition){
		D5
		D6
	} else {
		D7
		break
	}
} while(condition)
D8
```

* Iteration 0 —— init

![image-20240404111847156](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-8938703ee9b23df36f23720d4b108fa0f4dc858b%2Fimage-20240404111847156.png?alt=media)

* Iteration 1

IN\[B1] = 0000 0000 = OUT\[Entry]

OUT\[B1] = 1100 0000 generate D1、D2

IN\[B2] = 1100 0000 = OUT\[B1] ∪ OUT\[B4]

OUT\[B2] = 1011 0000 generate D3、D4，kill D2

IN\[B3] = 1011 0000 = OUT\[B2]

OUT\[B3] = 0011 0010 generate D7，kill D1

IN\[B4] = 1011 0000 = OUT\[B2]

OUT\[B4] = 0011 1100 generate D5、D6，kill D1

IN\[B5] = 0011 1110 = OUT\[B4] ∪ OUT\[B3]

OUT\[B5] = 0011 1011 generate D8, kill D6

![image-20240404114642223](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-daf017e451a380c5e82cf6001425130dd655f4ea%2Fimage-20240404114642223.png?alt=media)

* Iteration 2

IN\[B1] = 0000 0000 = OUT\[Entry]

OUT\[B1] = 1100 0000 generate D1、D2

IN\[B2] = 1111 1100 = OUT\[B1] ∪ OUT\[B4]

OUT\[B2] = 1011 1100 generate D3、D4，kill D2

IN\[B3] = 1011 1100 = OUT\[B2]

OUT\[B3] = 0011 0110 generate D7，kill D1、D5

IN\[B4] = 1011 1100 = OUT\[B2]

OUT\[B4] = 0011 1100 generate D5、D6，kill D1、D7、D8

IN\[B5] = 0011 1110 = OUT\[B4] ∪ OUT\[B3]

OUT\[B5] = 0011 1011 generate D8, kill D6

![image-20240404115359792](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-04508fd1e459fcd50a6b01866aeb800c4f1a4ed7%2Fimage-20240404115359792.png?alt=media)

* Iteration 3

IN\[B1] = 0000 0000 = OUT\[Entry]

OUT\[B1] = 1100 0000 generate D1、D2

IN\[B2] = 1111 1100 = OUT\[B1] ∪ OUT\[B4]

OUT\[B2] = 1011 1100 generate D3、D4，kill D2

IN\[B3] = 1011 1100 = OUT\[B2]

OUT\[B3] = 0011 0110 generate D7，kill D1、D5

IN\[B4] = 1011 1100 = OUT\[B2]

OUT\[B4] = 0011 1100 generate D5、D6，kill D1、D7、D8

IN\[B5] = 0011 1110 = OUT\[B4] ∪ OUT\[B3]

OUT\[B5] = 0011 1011 generate D8, kill D6

![image-20240404115705891](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-d79ab59a435e006f282409d518f6f56bc5f35862%2Fimage-20240404115705891.png?alt=media)

The final result is the green part which means definitions can reach this point in the program.

> Why this iterative algorithm can finally stop?
>
> <img src="https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-5d87f6ec2ae6d96fdeeca026193db54b8432c2eb%2Fimage-20240404121117629.png?alt=media" alt="image-20240404121117629" data-size="original">
>
> INs will not change if OUTs do not change
>
> OUTs will not change if INs do not change
>
> Reach a fixed point. Also related with monotonicity

## Live Variables Analysis

Live variables analysis tells whether the value of variable v at program point p could be used along some path in CFG starting at p.If so, v is live at p; otherwise, v is dead at p.

![image-20240404122526868](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-df2c90a0812cc64635e39a6101418cb6cf397d05%2Fimage-20240404122526868.png?alt=media)

> information of live variables can be used for register allocations.
>
> e.g., all registers are full and we need to use one, then we should favor using a register with a dead value.

### Abstraction

All variables in a program can be represented by bit vectors.

![image-20240404122932842](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-95485c406542b4570c02dbbcf7264df836abc5e1%2Fimage-20240404122932842.png?alt=media)

### Safe-Approximation

redefinition will break the path. forwards analysis requires record of previous state, so we use **backwards analysis**

One live path will be ok, so we use **may analysis**

#### Control Flow

![image-20240404125456185](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-e3a8e6751d3684202fc683cc0c74bd5ecb51b56d%2Fimage-20240404125456185.png?alt=media)

#### Transfer Function

![image-20240404125631901](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-3bec9f419e547d40e1c5030049374de182339c4e%2Fimage-20240404125631901.png?alt=media)

![image-20240404125640830](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-25cbd5363dcdd4241973464afdcf793ffe03eeb1%2Fimage-20240404125640830.png?alt=media)

> useB：variables used before redefined in B
>
> defB：variables redefined in B
>
> OUT\[B]：variables live coming out of B

note: we focus on the live state of variables at some program point.

#### Algorithm

![image-20240404130057765](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-959a33b3c13a3e0d19d986e3291b0dc2a0aa3a9f%2Fimage-20240404130057765.png?alt=media)

* Iteration 0 —— init

![image-20240404131040928](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-23fb550e051ed3268b4cbc057062d08aedf31130%2Fimage-20240404131040928.png?alt=media)

* Iteration 1

IN\[Exit] = 000 0000

OUT\[B5] = 000 0000 = IN\[Exit]

IN\[B5] = 000 1000 use p, redefine z

OUT\[B3] = 000 1000 = IN\[B5]

IN\[B3] = 100 1000 use x

OUT\[B4] = 000 1000 = IN\[B5] ∪ IN\[B2]

IN\[B4] = 010 1000 use y, redefine x、q

OUT\[B2] = 110 1000 = IN\[B3] ∪ IN\[B4]

IN\[B2] = 100 1001 use k, redefine m、y

OUT\[B1] = 100 1001 = IN\[B2]

IN\[B1] = 001 1101 use p、q、z, redefine x、y

![image-20240404132406602](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-7be80388ce030ea2c46b576aafed3c6db1f56904%2Fimage-20240404132406602.png?alt=media)

* Iteration 2

IN\[Exit] = 000 0000

OUT\[B5] = 000 0000 = IN\[Exit]

IN\[B5] = 000 1000 use p, redefine z

OUT\[B3] = 000 1000 = IN\[B5]

IN\[B3] = 100 1000 use x

OUT\[B4] = 100 1001 = IN\[B5] ∪ IN\[B2]

IN\[B4] = 010 1001 use y, redefine x、q

OUT\[B2] = 110 1001 = IN\[B3] ∪ IN\[B4]

IN\[B2] = 100 1001 use k, redefine m、y

OUT\[B1] = 100 1001 = IN\[B2]

IN\[B1] = 001 1101 use p、q、z, redefine x、y

![image-20240404132806730](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-559bf80d1d39fb3c9520396648b9d31b0157da85%2Fimage-20240404132806730.png?alt=media)

* Iteration 3

IN\[Exit] = 000 0000

OUT\[B5] = 000 0000 = IN\[Exit]

IN\[B5] = 000 1000 use p, redefine z

OUT\[B3] = 000 1000 = IN\[B5]

IN\[B3] = 100 1000 use x

OUT\[B4] = 100 1001 = IN\[B5] ∪ IN\[B2]

IN\[B4] = 010 1001 use y, redefine x、q

OUT\[B2] = 110 1001 = IN\[B3] ∪ IN\[B4]

IN\[B2] = 100 1001 use k, redefine m、y

OUT\[B1] = 100 1001 = IN\[B2]

IN\[B1] = 001 1101 use p、q、z, redefine x、y

![image-20240404133110495](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-37a079a1c3ad3e94bd0b94ae5a755cb6ad3b59e5%2Fimage-20240404133110495.png?alt=media)

The final result is the green part which means variable is live along the path(can be used in future) from this program point

## Available Expressions Analysis

An expression `x op y` is available at program point p if

1. all paths from the entry to p must pass through the evaluation of `x op y`
2. after the last evaluation of `x op y`, there is no redefinition of x or y

> available expressions can be used for detecting global common subexpressions.

### Abstraction

All the expressions in a program can be represented by bit vectors.

![image-20240404135114804](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-6e35c15d34a32161b9abd2ed2009e405de1ce78d%2Fimage-20240404135114804.png?alt=media)

### Safe-Approximation

#### Transfer Function

![image-20240404140102302](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-63aae8ba65365f37275c6d77ed33a08b32d9be1d%2Fimage-20240404140102302.png?alt=media)

#### Control Flow

All paths from entry to point p must pass through the evaluation of `x op y`，so we use must analysis

for safety of the analysis, it may report an expression as unavailable even if it is truly available.（用于编译器优化，不能优化错误的内容, under-approximation）

![image-20240404140531596](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-d3339f7c5ff97195a0f0abc4f246e701a3acab4e%2Fimage-20240404140531596.png?alt=media)

#### Algorithm

![image-20240404141422819](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-e46da49beac0b510a21928d37c334e621221e3ed%2Fimage-20240404141422819.png?alt=media)

* Iteration 0 —— init

![image-20240404142143318](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-5a8f9355e6e1b43d1cdf2a3c0a6ff422976480c3%2Fimage-20240404142143318.png?alt=media)

* Iteration 1

OUT\[Entry] = 00000

IN\[B1] = 00000

OUT\[B1] = 10000 generate E1

IN\[B2] = 10000 OUT\[B1] ∩ OUT\[B4]

OUT\[B2] = 01010 generate E2、E4，kill E1

IN\[B3] = 01010 = OUT\[B2]

OUT\[B3] = 00011 generate E5，kill E2

IN\[B4] = 01010 = OUT\[B2]

OUT\[B4] = 01110 generate E3、E4

IN\[B5] = 00010 = OUT\[B3] ∩ OUT\[B4]

OUT\[B5] = 01010 generate E4、E2，kill E3

![image-20240404142943287](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-29758be67bd7ff411111e71d2a953f635b7789cf%2Fimage-20240404142943287.png?alt=media)

* Iteration 2

OUT\[Entry] = 00000

IN\[B1] = 00000

OUT\[B1] = 10000 generate E1

IN\[B2] = 00000 OUT\[B1] ∩ OUT\[B4]

OUT\[B2] = 01010 generate E2、E4，kill E1

IN\[B3] = 01010 = OUT\[B2]

OUT\[B3] = 00011 generate E5，kill E2

IN\[B4] = 01010 = OUT\[B2]

OUT\[B4] = 01110 generate E3、E4

IN\[B5] = 00010 = OUT\[B3] ∩ OUT\[B4]

OUT\[B5] = 01010 generate E4、E2，kill E3

![image-20240404143148713](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-b069ccbb1fa9dcf41f4028566d594dc786e98a97%2Fimage-20240404143148713.png?alt=media)

## Summary

![image-20240404143825127](https://1239337109-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HLPOkuOfb7iyCzDJ8vA%2Fuploads%2Fgit-blob-4317bc150f2f0852cda604e370cc10f52848a19d%2Fimage-20240404143825127.png?alt=media)
