[Theory] Static Program Analysis 讀書(shū)筆記(一):approximations, type analysis & lattice theory

0. Preface

Static program analysis is the art of reasoning about the behavior of computer programs without actually running them. This is useful not only in optimizing compilers for producing e?cient code but also for automatic error detection and other tools that can help programmers.

As known from Turing and Rice, all nontrivial properties of the behavior of programs written in common programming languages are mathematically undecidable.

This means that automated reasoning of software generally must involve approximation.

One of the key challenges when developing such analyses is how to ensure high precision and e?ciency to be practically useful.

We take a constraint-based approach to static analysis where suitable constraint systems conceptually divide the analysis task into a front-end that generates constraints from program code and a back-end that solves the constraints to produce the analysis results.

The analyses that we cover are expressed using di?erent kinds of constraint systems, each with their own constraint solvers:

  • term uni?cation constraints, with an almost-linear union-?nd algorithm,
  • conditional subset constraints, with a cubic-time algorithm, and
  • monotone constraints over lattices, with variations of ?xed-point solvers.

1. Introduction

Dijkstra: “Program testing can be used to show the presence of bugs, but never to show their absence.

A program analyzer is such a program that takes other programs as input and decides whether or not they have a certain property.

Rice’s theorem is a general result from 1953 which informally states that all interesting questions about the behavior of programs (written in Turing-complete programming languages ) are undecidable.

At ?rst, this seems like a discouraging result, however, this theoretical result does not prevent approximative answers. While it is impossible to build an analysis that would correctly decide a property for any analyzed program, it is often possible to build analysis tools that give useful answers for most realistic programs. As the ideal analyzer does not exist, there is always room for building more precise approximations (which is colloquially called the full employment theorem for static program analysis designers).

  • We say that a program analyzer is sound if it never gives incorrect results (but it may answer maybe).
  • A veri?cation tool is typically called sound if it never misses any errors of the kinds it has been designed to detect, but it is allowed to produce spurious warnings (also called false positives).
  • An automated testing tool is called sound if all reported errors are genuine, but it may miss errors.

Program analyses that are used for optimizations typically require soundness. If given false information, the optimization may change the semantics of the program. Conversely, if given trivial information, then the optimization fails to do anything.

Although soundness is a laudable goal in analysis design, modern analyzers for real programming languages often cut corners by sacri?cing soundness to obtain better precision and performance, for example when modeling re?ection in Java.

It is impossible to build a static program analysis that can decide whether a given program may fail when executed. Moreover, this result holds even if the analysis is only required to work for programs that halt on all inputs. In other words, the halting problem is not the only obstacle; approximation is inevitably necessary.


2. A Tiny Imperative Programming Language

Abstract syntax trees (ASTs) as known from compiler construction provide a representation of programs that is suitable for ?ow-insensitive analyses, for example, type analysis, control ?ow analysis, and pointer analysis.

For ?ow-sensitive analysis, in particular data?ow analysis, where statement order matters it is more convenient to view the program as a control ?ow graph, which is a different representation of the program code.

A control ?ow graph (CFG) is a directed graph, in which nodes correspond to statements and edges represent possible ?ow of control.


3. Type Analysis

We resort to a conservative approximation: typability. A program is typable if it satis?es a collection of type constraints that is systematically derived, typically from the program AST.

The type constraints are constructed in such a way that the above requirements are guaranteed to hold during execution, but the converse is not true. Thus, our type checker will be conservative and reject some programs that in fact will not violate any requirements during execution.

For a given program we generate a constraint system and de?ne the program to be typable when the constraints are solvable.

This class of constraints can be e?ciently solved using a uni?cation algorithm.

If solutions exist, then they can be computed in almost linear time using a uni?cation algorithm for regular terms as explained below. Since the constraints may also be extracted in linear time, the whole type analysis is quite e?cient.

The uni?cation algorithm we use is based on the familiar union-?nd data structure (also called a disjoint-set data structure) from 1964 for representing and manipulating equivalence relations柠横。

The type analysis is of course only approximate, which means that certain programs will be unfairly rejected.


4. Lattice Theory

We circumvent undecidability by introducing approximation. That is, the analysis must be prepared to handle uncertain information, in this case situations where it does not know the sign of some expression, so we add a special abstract value (T) representing “don’t know”.

Due to undecidability, imperfect precision is inevitable.

  • partial order
  • upper bound
  • lower bound
  • least upper bound
  • greatest lower bound
  • lattice

The least upper bound operation plays an important role in program analysis.

As we shall see, we use least upper bound when combining abstract information from multiple sources, for example when control ?ow merges after the branches of if statements.

  • the height of a lattice
  • powerset lattice
  • reverse powerset lattice
  • ?at(A)
  • product
  • map lattice
  • homomorphism
  • isomorphism
  • lift(L)

Also notice that it is important for the analysis of this simple program that the order of statements is taken into account, which is called ?ow-sensitive analysis.

  • monotone

As the lattice order when used in program analysis represents precision of information, the intuition of monotonicity is that “more precise input does not result in less precise output”.

  • ?xed-point
  • least ?xed-point
  • equation system
  • constraint functions
  • solution

A solution to an equation system provides a value from L for each variable such that all equations are satis?ed.

This clearly shows that a solution to an equation system is the same as a ?xed-point of its functions. As we aim for the most precise solutions, we want least ?xed-points.

The ?xed-point theorem
In a lattice L with ?nite height, every monotone function f : L → L has a unique least ?xed-point denoted ?x(f) de?ned as

fix(f) = \bigvee_{i\geqslant 0} f^i(\perp)

The theorem is a powerful result: It tells us not only that equation systems over lattices always have solutions, provided that the lattices have ?nite height and the constraint functions are monotone, but also that uniquely most precise solutions always exist. Furthermore, the careful reader may have noticed that the theorem provides an algorithm for computing the least ?xed-point.

The least ?xed point is the most precise possible solution to the equation system, but the equation system is (for a sound analysis) merely a conservative approximation of the actual program behavior. This means that the semantically most precise possible (while still correct) answer is generally below the least ?xed point in the lattice.


Reference

Static Program Analysis

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市狮杨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖镐躲,帶你破解...
    沈念sama閱讀 207,248評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贾漏,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡歧寺,警方通過(guò)查閱死者的電腦和手機(jī)燥狰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)斜筐,“玉大人龙致,你說(shuō)我怎么就攤上這事∏炅矗” “怎么了目代?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,443評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)嗤练。 經(jīng)常有香客問(wèn)我榛了,道長(zhǎng),這世上最難降的妖魔是什么煞抬? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,475評(píng)論 1 279
  • 正文 為了忘掉前任霜大,我火速辦了婚禮,結(jié)果婚禮上革答,老公的妹妹穿的比我還像新娘战坤。我一直安慰自己,他們只是感情好残拐,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,458評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布途茫。 她就那樣靜靜地躺著,像睡著了一般溪食。 火紅的嫁衣襯著肌膚如雪囊卜。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,185評(píng)論 1 284
  • 那天眠菇,我揣著相機(jī)與錄音边败,去河邊找鬼。 笑死捎废,一個(gè)胖子當(dāng)著我的面吹牛笑窜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播登疗,決...
    沈念sama閱讀 38,451評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼排截,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了辐益?” 一聲冷哼從身側(cè)響起断傲,我...
    開(kāi)封第一講書(shū)人閱讀 37,112評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎智政,沒(méi)想到半個(gè)月后认罩,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,609評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡续捂,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,083評(píng)論 2 325
  • 正文 我和宋清朗相戀三年垦垂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了宦搬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,163評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡劫拗,死狀恐怖间校,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情页慷,我是刑警寧澤憔足,帶...
    沈念sama閱讀 33,803評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站酒繁,受9級(jí)特大地震影響滓彰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜欲逃,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,357評(píng)論 3 307
  • 文/蒙蒙 一找蜜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧稳析,春花似錦、人聲如沸弓叛。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,357評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)撰筷。三九已至陈惰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間毕籽,已是汗流浹背抬闯。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,590評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留关筒,地道東北人溶握。 一個(gè)月前我還...
    沈念sama閱讀 45,636評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像蒸播,于是被迫代替她去往敵國(guó)和親睡榆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,925評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容

  • 本文轉(zhuǎn)載自知乎 作者:季子烏 筆記版權(quán)歸筆記作者所有 其中英文語(yǔ)句取自:英語(yǔ)流利說(shuō)-懂你英語(yǔ) ——————————...
    Danny_Edward閱讀 43,855評(píng)論 4 38
  • 步入一個(gè)大家都覺(jué)得你該結(jié)婚的年紀(jì)袍榆,跟弟弟吵架的時(shí)候胀屿,把他氣走了會(huì)擔(dān)心是不是自己脾氣不好,惹得這個(gè)家有點(diǎn)不和諧包雀,他是...
    西瓜九千斤閱讀 40評(píng)論 0 0
  • 一宿崭、知識(shí)點(diǎn) 二、作業(yè)
    靜靜的頓河wj閱讀 149評(píng)論 0 1
  • 我并不是說(shuō)要摧毀頭腦才写,它是存在里面最進(jìn)化的現(xiàn)象葡兑。我是在說(shuō)要小心奴愉,不要讓仆人變成主人。記住铁孵,是你的靈魂第一锭硼,心第二,...
    紫涵的世界閱讀 167評(píng)論 0 0
  • 今天一天都在關(guān)注手機(jī)蜕劝,感覺(jué)好累檀头,不止工作累,心也累岖沛,我還是喜歡不天天關(guān)注手機(jī)上的信息好一些暑始。 今天運(yùn)動(dòng)了半個(gè)小時(shí),...
    噯寧閱讀 112評(píng)論 0 0