[Theory] Static Program Analysis 讀書筆記(三):function, pointer & abstract interpretation

9. Control Flow Analysis

If we introduce functions as values (and thereby higher-order functions), or objects with methods, then control flow and dataflow suddenly become intertwined.

The task of control flow analysis is to conservatively approximate the interprocedural control flow, also called the call graph, for such programs.

  • closures
  • closure analysis

The constraints for closure analysis are an instance of a general class that can be solved in cubic time.

A language with functions as values must use the kind of control flow analysis to obtain a reasonably precise CFG. For common object-oriented languages, such as Java or C#, it is also useful, but the added structure provided by the class hierarchy and the type system permits some simpler alternatives.

  • Class Hierarchy Analysis (CHA)
  • Rapid Type Analysis (RTA)
  • Variable Type Analysis (VTA)

10. Pointer Analysis

  • Interprocedural Points-To Analysis
  • Null Pointer Analysis
  • Flow-Sensitive Points-To Analysis
  • Escape Analysis

The final extension of the TIP language introduces pointers and dynamic memory allocation.

  • allocation-site abstraction
  • points-to analysis
  • address taken
  • Andersen’s algorithm
  • Steensgaard’s Algorithm

In languages with both function values and pointers, functions may be stored in the heap, which makes it difficult to perform control flow analysis before points-to analysis. But it is also difficult to perform interprocedural points-to analysis without the information from a control flow analysis.

  • points-to graphs
  • escape analysis

11. Abstract Interpretation

If an analysis is sound, the properties it infers for a given program hold in all actual executions of the program.

The theory of abstract interpretation provides a solid mathematical foundation for what it means for an analysis to be sound, by relating the analysis specification to the formal semantics of the programming language.

Another use of abstract interpretation is for understanding whether an analysis design, or a part of an analysis design, is as precise as possible relative to a choice of analysis lattice and where imprecision may arise.

What matters is that the semantics captures the meaning of programs in ordinary executions, without any approximations. The semantics specifies how a concrete program execution works, whereas our analyses can be thought of as abstract interpreters.

  • collecting semantics
  • continuous

With these definitions and observations, we can define the semantics of a given program as the least solution to the generated constraints. (A solution to a constraint system is, as usual, a valuation of the constraint variables that satisfies all the constraints – in other words, a fixed-point of cf .)

As readers familiar with theory of programming language semantics know, the fixed-point theorem also holds for infinite-height lattices provided that f is continuous. This tells us that a unique least solution always exists – even though the solution, of course, generally cannot be computed using the naive fixed-point algorithm.

To clarify the connection between concrete information and abstract information for the sign analysis example, let us consider three different abstraction functions that tell us how each element from the semantic lattices is most precisely described by an element in the analysis lattices. The functions map sets of concrete values, sets of concrete states, and n-tuples of sets of concrete states to their abstract counterparts.

  • concretization functions

Furthermore, abstraction functions and concretization functions that arise naturally when developing program analyses are closely connected.

  • extensive
  • reductive

The pair of monotone functions, α and γ, is called a Galois connection if it satisfies these two properties.

The intuition of the first property is that abstraction may lose precision but must be safe. One way to interpret the second property is that the abstraction function should always give the most precise possible abstract description for any element in the semantic lattice.

We have argued that the Galois connection property is natural for any reasonable pair of an abstraction function and a concretization function, including those that appear in our sign analysis example. The following exercise tells us that it always suffices to specify either α or γ, then the other is uniquely determined if requiring that they together form a Galois connection.

Once the analysis designer has specified the collecting semantics and the analysis lattice and constraint rules, then the relation between the semantic domain and the analysis domain may be specified using an abstraction function α (resp. a concretization function γ), and then the associated concretization function γ (resp. abstraction function α) is uniquely determined – provided that one exists such that the two functions form a Galois connection.

This raises an interesting question: Under what conditions does α (resp. γ) have a corresponding γ (resp. α) such that α and γ form a Galois connection?

The following exercise demonstrates that the Galois connection property can be used as a “sanity check” when designing analysis lattices.

Soundness means that the analysis result over-approximates the abstraction of the semantics of the program.

We often use the term soundness of analyses without mentioning specific programs. An analysis is sound if it is sound for every program.

soundness theorem
Let L_1 and L_2 be lattices where L_2 has finite height, assume \alpha :L_1\rightarrow L_2 and \gamma :L_2\rightarrow L_1 form a Galois connection, cf:L_1 \rightarrow L_1 is continuous, and af:L_2 \rightarrow L_2 is monotone. If af is a sound abstraction of cf , then

\alpha (fix(cf)) \sqsubseteq fix(af)

To summarize, a general recipe for specifying and proving soundness of an analysis consists of the following steps:

  • Specify the analysis, i.e. the analysis lattice and the constraint generation rules, and check that all the analysis constraint functions are monotone.
  • Specify the collecting semantics, and check that the semantic constraint functions are continuous.
  • Establish the connection between the semantic lattice and the analysis lattice, either by an abstraction function or by a concretization function. Then check, for example using the property from Exercise 11.20, that the function pairs form Galois connections.
  • Show that each constituent of the analysis constraints is a sound abstraction of the corresponding constituent of the semantic constraints, for all programs.

The requirements that the analysis constraint functions are monotone, the semantic constraint functions are continuous, and the abstraction and concretization functions form Galois connections are rarely restrictive in practice but can be considered as “sanity checks” that the design is meaningful.

As we have seen in Exercises 11.21 and 11.23, it is possible to design sound analyses that do not have all these nice properties, but the price is usually less precision or more complicated soundness proofs. Another restriction of the soundness theorem above is that it requires L_2 to have finite height, however, the theorem and proof can easily be extended to analyses with infinite-height lattices and widenings.

  • soundness testing
  • optimal abstraction

As usual in logics, the dual of soundness is completeness.
In Section 11.3 we de?ned soundness of an analysis for a program P as the property

\alpha(\{\[P\]\})\sqsubseteq \[ \[P\] \]

Consequently, it is natural to de?ne that an analysis is complete for P if

\[ \[ P\] \] \sqsubseteq \alpha(\{\[P\]\})

An analysis is complete if it complete for all programs.

If an analysis is both sound and complete for P we have

\alpha(\{\[P\]\}) = \[ \[P\] \]
  • complete abstraction

For abstractions that are sound, completeness implies optimality (but not vice versa).

  • soundness and completeness theorem

Abstractions that are incomplete may be complete in some situations; for example, abstract addition in sign analysis is not complete in general, but it is complete in situations where, for example, both arguments are positive values. For this reason, even though few analyses are sound and complete for all programs, many analyses are sound and complete for some programs or program fragments.

  • reachable states collecting semantics
  • trace semantics

Reference

Static Program Analysis

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市班利,隨后出現(xiàn)的幾起案子讯泣,更是在濱河造成了極大的恐慌斥杜,老刑警劉巖唠椭,帶你破解...
    沈念sama閱讀 216,692評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異益兄,居然都是意外死亡戒洼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門枢劝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來井联,“玉大人,你說我怎么就攤上這事您旁±映#” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵鹤盒,是天一觀的道長(zhǎng)蚕脏。 經(jīng)常有香客問我,道長(zhǎng)侦锯,這世上最難降的妖魔是什么驼鞭? 我笑而不...
    開封第一講書人閱讀 58,223評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮尺碰,結(jié)果婚禮上挣棕,老公的妹妹穿的比我還像新娘译隘。我一直安慰自己,他們只是感情好洛心,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評(píng)論 6 388
  • 文/花漫 我一把揭開白布固耘。 她就那樣靜靜地躺著,像睡著了一般词身。 火紅的嫁衣襯著肌膚如雪玻驻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,208評(píng)論 1 299
  • 那天偿枕,我揣著相機(jī)與錄音璧瞬,去河邊找鬼。 笑死渐夸,一個(gè)胖子當(dāng)著我的面吹牛嗤锉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播墓塌,決...
    沈念sama閱讀 40,091評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼瘟忱,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了苫幢?” 一聲冷哼從身側(cè)響起访诱,我...
    開封第一講書人閱讀 38,929評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎韩肝,沒想到半個(gè)月后触菜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,346評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡哀峻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評(píng)論 2 333
  • 正文 我和宋清朗相戀三年涡相,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片剩蟀。...
    茶點(diǎn)故事閱讀 39,739評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡催蝗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出育特,到底是詐尸還是另有隱情丙号,我是刑警寧澤,帶...
    沈念sama閱讀 35,437評(píng)論 5 344
  • 正文 年R本政府宣布缰冤,位于F島的核電站犬缨,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏锋谐。R本人自食惡果不足惜遍尺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望涮拗。 院中可真熱鬧乾戏,春花似錦迂苛、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽薛训。三九已至店印,卻和暖如春尚揣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背摆出。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工朗徊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人偎漫。 一個(gè)月前我還...
    沈念sama閱讀 47,760評(píng)論 2 369
  • 正文 我出身青樓爷恳,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親象踊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子温亲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評(píng)論 2 354