Separation logic explained

In computer science, separation logic[1] is an extension of Hoare logic, a way of reasoning about programs.It was developed by John C. Reynolds, Peter O'Hearn, Samin Ishtiaq and Hongseok Yang,[2] [3] [4] drawing upon early work by Rod Burstall.[5] The assertion language of separation logic is a special case of the logic of bunched implications (BI).[6] A CACM review article by O'Hearn charts developments in the subject to early 2019.[7]

Overview

Separation logic facilitates reasoning about:

Separation logic supports the developing field of research described by Peter O'Hearn and others as local reasoning, whereby specifications and proofs of a program component mention only the portion of memory used by the component, and not the entire global state of the system. Applications include automated program verification (where an algorithm checks the validity of another algorithm) and automated parallelization of software.

Assertions: operators and semantics

Separation logic assertions describe "states" consisting of a store and a heap, roughly corresponding to the state of local (or stack-allocated) variables and dynamically-allocated objects in common programming languages such as C and Java. A store

s

is a function mapping variables to values. A heap

h

is a partial function mapping memory addresses to values. Two heaps

h

and

h'

are disjoint (denoted

h\both'

) if their domains do not overlap (i.e., for every memory address

\ell

, at least one of

h(\ell)

and

h'(\ell)

is undefined).

The logic allows to prove judgements of the form

s,h\modelsP

, where

s

is a store,

h

is a heap, and

P

is an assertion over the given store and heap. Separation logic assertions (denoted as

P

,

Q

,

R

) contain the standard boolean connectives and, in addition,

emp

,

e\mapstoe'

,

P\astQ

, and

P{-\ast}Q

, where

e

and

e'

are expressions.

emp

asserts that the heap is empty, i.e.,

s,h\modelsemp

when

h

is undefined for all addresses.

\mapsto

takes an address and a value and asserts that the heap is defined at exactly one location, mapping the given address to the given value. I.e.,

s,h\modelse\mapstoe'

when

h([[e]]s)=[[e']]s

(where

[[e]]s

denotes the value of expression

e

evaluated in store

s

) and

h

is otherwise undefined.

\ast

(pronounced star or separating conjunction) asserts that the heap can be split into two disjoint parts where its two arguments hold, respectively. I.e.,

s,h\modelsP\astQ

when there exist

h1,h2

such that

h1\both2

and

h=h1\cuph2

and

s,h1\modelsP

and

s,h2\modelsQ

.

-\ast

(pronounced magic wand or separating implication) asserts that extending the heap with a disjoint part that satisfies its first argument results in a heap that satisfies its second argument. I.e,.

s,h\modelsP-\astQ

when for every heap

h'\both

such that

s,h'\modelsP

, also

s,h\cuph'\modelsQ

holds.

The operators

\ast

and

-\ast

share some properties with the classical conjunction and implication operators. They can be combined using an inference rule similar to modus ponens
s,h\modelsP\ast(P-\astQ)
s,h\modelsQ
and they form an adjunction, i.e.,

s,h\cuph'\modelsP\astQR

if and only if

s,h\modelsPQ-\astR

for

h\both'

; more precisely, the adjoint operators are

\\astQ

and

Q-\ast\

.

Reasoning about programs: triples and proof rules

In separation logic, Hoare triples have a slightly different meaning than in Hoare logic. The triple

\{P\}C\{Q\}

asserts that if the program

C

executes from an initial state satisfying the precondition

P

then the program will not go wrong (e.g., have undefined behaviour), and if it terminates, then the final state will satisfy the postcondition

Q

. In essence, during its execution,

C

may access only memory locations whose existence is asserted in the precondition or that have been allocated by

C

itself.

In addition to the standard rules from Hoare logic, separation logic supports the following very important rule:

\{P\
C\{Q\}

}{\{P\astR\}C\{Q\astR\}}~mod(C)\capfv(R)=\emptyset

This is known as the frame rule (named after the frame problem) and enables local reasoning. It says that a program that executes safely in a small state (satisfying

P

), can also execute in any bigger state (satisfying

P\astR

) and that its execution will not affect the additional part of the state (and so

R

will remain true in the postcondition). The side condition enforces this by specifying that none of the variables modified by

C

occur free in

R

, i.e. none of them are in the 'free variable' set

fv

of

R

.

Sharing

Separation logic leads to simple proofs of pointer manipulation for data structures that exhibit regular sharing patterns which can be described simply using separating conjunctions; examples include singly and doubly linked lists and varieties of trees. Graphs and DAGs and other data structures with more general sharing are more difficult for both formal and informal proof. Separation logic has, nonetheless, been applied successfully to reasoning aboutprograms with general sharing.

In their POPL'01 paper, O'Hearn and Ishtiaq explained how the magic wand connective

{-*}

could be used to reason in the presence of sharing, at least in principle.For example, in the triple

\{(x\mapsto-)\ast((x\mapsto42){-*}P)\}[x]=42 \{P\}

we obtain the weakest precondition for a statement that mutates the heap at location

x

, and this works for any postcondition, not only one that is laid out neatly using the separating conjunction. This idea was taken much further by Yang, who used

{-*}

to provide localized reasoning about mutations in the classic Schorr-Waite graph marking algorithm.[8] Finally, one of the most recent works in this direction is that of Hobor and Villard,[9] who employ not only

{-*}

but also a connective

\cup*

which has variously been called overlapping conjunction or sepish,[10] and which can be used to describe overlapping data structures:

P\cup*Q

holds of a heap

h

when

P

and

Q

hold for subheaps

hP

and

hQ

whose union is

h

, but which possibly have a nonempty portion

hP\caphQ

in common. Abstractly,

P\cup*Q

can be seen to be a version of the fusion connective of relevance logic.

Concurrent separation logic

A Concurrent Separation Logic (CSL),a version of separation logic for concurrent programs, was originally proposed by Peter O'Hearn,[11] using a proof rule

\{P1\
C

1\{Q1\}\{P2\}C2\{Q2\}}{\{P1*P2\}C1\parallelC2\{Q1*Q2\}}

which allows independent reasoning about threads that access separate storage. O'Hearn's proof rules adapted an early approach of Tony Hoare to reasoning about concurrency,[12] replacing the use of scoping constraints to ensure separation by reasoning in separation logic. In addition to extending Hoare's approach to apply in the presence of heap-allocated pointers, O'Hearn showed how reasoning in concurrent separation logic could track dynamic ownership transfer of heap portions between processes; examples in the paper include a pointer-transferring buffer, and a memory manager.

Commenting on the early classical work on interference freedom by Susan Owicki and David Gries, O'Hearn says that explicit checking for non-interference isn't necessary because his system rules out interference in an implicit way, by the nature of the way proofs are constructed.

A model for concurrent separation logic was first provided by Stephen Brookes in a companion paper to O'Hearn's.[13] The soundness of the logic had been a difficult problem, and in fact a counterexample of John Reynolds had shown the unsoundness of an earlier, unpublished version of the logic; the issue raised by Reynolds's example is described briefly in O'Hearn's paper, and more thoroughly in Brookes's.

At first it appeared that CSL was well suited to what Dijkstra had called loosely connected processes,[14] but perhaps not to fine-grained concurrent algorithms with significant interference. However, gradually it was realized that the basic approach of CSL was considerably more powerful than first envisaged, if one employed non-standard models of the logical connectives and even the Hoare triples.

An abstract version of separation logic was proposed that works for Hoare tripleswhere the preconditions and postconditions are formulae interpreted over an arbitrary partial commutative monoid instead of a particular heap model.[15] Later, by suitable choice of commutative monoid, it was surprisingly found that the proof rules of abstract versions of concurrent separation logic could be used to reason about interfering concurrent processes, for example by encoding the rely-guarantee technique which had been originally proposed to reason about interference;[16] in this work the elements of the model were considered not resources, but rather "views" of the program state, and a non-standard interpretation of Hoare triples accompanies the non-standard reading of pre and postconditions.Finally, CSL-style principles have been used to compose reasoning about program histories instead of program states, in order to provide modular techniques for reasoning about fine-grained concurrent algorithms.[17]

Versions of CSL have been included in many interactive and semi-automatic (or "in-between") verification tools as described in the next section. A particularly significant verification effort is that of the μC/OS-II kernel mentioned there. But, although steps have been made,[18] as of yet CSL-style reasoning has been included in comparatively fewtools in the automatic program analysis category (and none mentioned in the next section).

O'Hearn and Brookes are co-recipients of the 2016 Gödel Prize for their invention of Concurrent Separation Logic.[19]

Verification and program analysis tools

Tools for reasoning about programs fall on a spectrum from fully automatic program analysis tools, which do not require any user input, to interactive tools where the humanis intimately involved in the proof process. Many such tools have been developed; the following list includes a few representatives in each category.

The distinction between interactive and in-between verifiers is not a sharp one. For example,Bedrock strives for a high degree of automation, in what it terms mostly-automatic verification, whereVerifast sometimes requires annotations that resemble the tactics (little programs) used in interactive verifiers.

Decidability and complexity

The satisfiability problem for a quantifier-free, multi-sorted fragment of separation logic parameterized over the sorts of locations and data can be shown to be PSPACE-complete.[27] An algorithm for solving this fragment in DPLL(T)-based SMT solvers has been integrated into cvc5.[28] Extending this result, satisfiability for an analog of the Bernays–Schönfinkel class for separation logic with uninterpreted memory locations can also be shown to be PSPACE-complete, whereas the problem is undecidable with interpreted memory locations (e.g., integers) or further quantifier alternations[29]

Notes and References

  1. Separation Logic: A Logic for Shared Mutable Data Structures . John C. . Reynolds . John C. Reynolds . LICS . 2002 .
  2. Book: Reynolds, John C. . Intuitionistic Reasoning about Shared Mutable Data Structure . Millennial Perspectives in Computer Science, Proceedings of the 1999 Oxford–Microsoft Symposium in Honour of Sir Tony Hoare . 1999 . . Jim . Davies . Bill . Roscoe . Jim . Woodcock . Jim Davies (computer scientist) . Bill Roscoe . Jim Woodcock .
  3. Book: Samin . Ishtiaq . Peter . O'Hearn . Proceedings of the 28th ACM SIGPLAN-SIGACT symposium on Principles of programming languages . BI as an assertion language for mutable data structures . Peter O'Hearn . POPL . . 2001 . 14–26 . 10.1145/360204.375719 . 1581133367 . 2652274 .
  4. Local Reasoning about Programs that Alter Data Structures . Peter . O'Hearn . John C. . Reynolds . Hongseok . Yang . CSL . 2001 . 10.1.1.29.1331.
  5. Some techniques for proving programs which alter data structures . R. M. . Burstall . Rod Burstall . . 7 . 1972 .
  6. The Logic of Bunched Implications . P. W. . O'Hearn . D. J. . Pym . . 5 . 2 . June 1999 . 215–244 . 10.2307/421090 . 421090 . 10.1.1.27.4742 . 2948552 .
  7. O'Hearn. Peter. February 2019. Separation Logic. Commun. ACM. 62. 2. 86–95. 10.1145/3211968. 0001-0782. free.
  8. Yang. Hongseok. An Example of Local Reasoning in BI Pointer Logic: the Schorr−Waite Graph Marking Algorithm. Proceedings of the 1st Workshop on Semantics' Program Analysis' and Computing Environments for Memory Management. 2001.
  9. Hobor. Aquinas. Villard. Jules. The ramifications of sharing in data structures. ACM SIGPLAN Notices. 48. 2013. 523–536. 10.1145/2480359.2429131.
  10. Book: Gardner. Philippa. Maffeis. Sergio. Smith. Hareth. Proceedings of the 39th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '12. 2012. 31–44. 10.1145/2103656.2103663. http://www.doc.ic.ac.uk/~gds/papers/TowardsProgramLogicJavaScriptPOPL2012.pdf. 9781450310833. 10044/1/33265. Towards a program logic for Java Script. 9571576.
  11. O'Hearn. Peter. Resources, Concurrency and Local Reasoning. Theoretical Computer Science. 2007. 375. 1–3. 271–307. 10.1016/j.tcs.2006.12.035. free.
  12. Hoare. C.A.R.. Towards a theory of parallel programming. Operating System Techniques. Academic Press. 1972.
  13. Brookes. Stephen. A Semantics for Concurrent Separation Logic. Theoretical Computer Science. 2007. 375. 1–3. 227–270. 10.1016/j.tcs.2006.12.034.
  14. (September 1965)
  15. Book: 10.1109/LICS.2007.30. http://www.cs.ox.ac.uk/people/hongseok.yang/paper/asl-short.pdf. 978-0-7695-2908-0. 10.1.1.66.6337. Local Action and Abstract Separation Logic. 22nd Annual IEEE Symposium on Logic in Computer Science (LICS 2007). 366–378. 2007. Calcagno. Cristiano. O'Hearn. Peter W.. Yang. Hongseok. 1044254.
  16. Dinsdale-Young. Thomas. Birkedal. Lars. Gardner. Philippa. Parkinson. Matthew. Yang. Hongseok. Views. ACM SIGPLAN Notices. 48. 287–300. 2013. 10.1145/2480359.2429104.
  17. Sergey. Ilya. Nanevski. Aleksandar. Banerjee. Anindya. Specifying and Verifying Concurrent Algorithms with Histories and Subjectivity. 24th European Symposium on Programming. 2015. 2014arXiv1410.0306S. 1410.0306.
  18. Book: Gotsman . Alexey . Berdine . Josh . Cook . Byron . Sagiv . Mooly . Verification, Model Checking, and Abstract Interpretation . Thread-Modular Shape Analysis . PLDI . 5403 . 2007 . 266–277 . 10.1007/978-3-540-93900-9_3 . Lecture Notes in Computer Science . 978-3-540-93899-6 .
  19. Web site: 2016 Gödel Prize. European Association for Theoretical Computer Science. 2022-08-29.
  20. https://fbinfer.com/docs/separation-logic-and-bi-abduction Separation logic and bi-abduction, page
  21. https://code.facebook.com/posts/1648953042007882/open-sourcing-facebook-infer-identify-bugs-before-you-ship/ Open-sourcing Facebook Infer: Identify bugs before you ship.
  22. https://pdos.csail.mit.edu/papers/fscq:sosp15.pdf Using Crash Hoare Logic for Certifying the FSCQ File System
  23. http://www.cs.princeton.edu/~appel/papers/verified-hmac.pdf Verified correctness and security of OpenSSL HMAC
  24. http://staff.ustc.edu.cn/~fuming/research/certiucos/ A Practical Verification Framework for Preemptive OS Kernels
  25. http://ynot.cs.harvard.edu/ The Ynot Project homepage
  26. https://pm.inf.ethz.ch/publications/MuellerSchwerhoffSummers16.pdf Viper: A Verification Infrastructure for Permission-Based Reasoning
  27. Book: Reynolds . Andrew . Iosif . Radu . Serban . Cristina . King . Tim . 2016 . Artho . Cyrille . Legay . Axel . Peled . Doron . A Decision Procedure for Separation Logic in SMT . https://link.springer.com/chapter/10.1007/978-3-319-46520-3_16 . Automated Technology for Verification and Analysis . Lecture Notes in Computer Science . en . Cham . Springer International Publishing . 244–261 . 10.1007/978-3-319-46520-3_16 . 978-3-319-46520-3. 1603.06844 .
  28. Book: Barbosa . Haniel . Barrett . Clark . Brain . Martin . Kremer . Gereon . Lachnitt . Hanna . Mann . Makai . Mohamed . Abdalrhman . Mohamed . Mudathir . Niemetz . Aina . Nötzli . Andres . Ozdemir . Alex . Preiner . Mathias . Reynolds . Andrew . Sheng . Ying . Tinelli . Cesare . 2022 . Fisman . Dana . Rosu . Grigore . cvc5: A Versatile and Industrial-Strength SMT Solver . Tools and Algorithms for the Construction and Analysis of Systems . Lecture Notes in Computer Science . en . Cham . Springer International Publishing . 415–442 . 10.1007/978-3-030-99524-9_24 . 978-3-030-99524-9. free .
  29. Book: Reynolds . Andrew . Iosif . Radu . Serban . Cristina . 2017 . Bouajjani . Ahmed . Monniaux . David . Reasoning in the Bernays-Schönfinkel-Ramsey Fragment of Separation Logic . https://link.springer.com/chapter/10.1007/978-3-319-52234-0_25 . Verification, Model Checking, and Abstract Interpretation . Lecture Notes in Computer Science . en . Cham . Springer International Publishing . 462–482 . 10.1007/978-3-319-52234-0_25 . 978-3-319-52234-0.