Alex Vlasov
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# A model to prove absence of aliasing In the context of formal reasoning about imperative programs, one often faces [frame problem](https://en.wikipedia.org/wiki/Frame_problem). However, literature, addressing the problem that we have read, lacks simple and concise intuition of how the problem is solved. In particular, while comparing two papers, [Dynamic Frames](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.185.2957&rep=rep1&type=pdf) and [Separation Logic](https://www.cs.cmu.edu/~jcr/seplogic.pdf), it becomes clear the papers use varying terminology. While their approaches look rather similar, it was not evident how their actually can be compared. We address the problems by developing a simple unified model, which solves the framing problem (in the context of formal reasoning about imperative programs). We intend to use the model to compare approaches of Dynamic Frames and of Separation Logic. Hopefully, it will also serve as a relatively simple description of how the frame problem can be tackled. The write-up is also a follow up to [Why pure functional form matters](https://hackmd.io/8UosBtxfQc6kzj94mWUSpg), which discusses an approach to avoid "dangerous" aliasing (less expressive obviously). # Frame problem We are starting from the following understanding of the frame problem (from [Frame problem's wiki page](https://en.wikipedia.org/wiki/Frame_problem)): > The frame problem is that specifying only which conditions are changed by the actions does not entail that all other conditions are not changed. This problem can be solved by adding the so-called “frame axioms”, which explicitly specify that all conditions not affected by actions are not changed while executing that action. In the case of imperative programs, one typically uses different terminology. We assume that there is a `memory`, which can be affected by actions. An action corresponds to a modification of the `memory`. Finally, a condition corresponds to an expression, which value depends on the `memory` contents. ## Aliasing Depending on programing language semantics, it sometimes can be quite easy to determine, which expressions are affected by a particular memory update. For example, it can be the case that two different syntactic names imply that they reference disjoint memory locations. So, if a variable `a` is modified, only expressions depending on `a` are affected. However, practical languages are more expressive and two syntactically different names may reference two overlapping locations. The situation is called [aliasing](https://en.wikipedia.org/wiki/Aliasing_(computing)). For example, a reference to a location can be copied to another variable: ``` a = new A b = a ``` `a` and `b` refer to the same location after the program is executed. Another typical case is arrays: - `a[i]` is an alias to `a[j]`, if `i == j` - `a[i]` and `b[j]` can share memory locations, if there is an aliasing between `a` and `b` ## Aliasing and formal reasoning In presence of aliasing, reasoning about imperative programs can become complicated or even impossible without additional assumptions (so called "frame axioms"). For example, there are two variables: `a` and `b`, and there is some fact about `a`. After some statement modifies `b`, can we still assume that the fact is sill valid? In presence of aliasing, we cannot, as `a` and `b` can share memory locations. This was a "write-after-read" example: a write following a read can invalidate the value just read (and stored somewhere, e.g. in a register or in a fact store). One can imagine a "write-after-write" example. However, memory semantics typically mean that once we wrote to a location, a consequent read of it will return the value written. So, in semantic sense, there is an implicit read. Thus, it's typically enough to consider the "write-after-read" case only. ### Split case reasoning One way to deal with the problem is to consider two cases: there is aliasing between `a` and `b`, and there is no aliasing. In the latter case, one can re-use the fact about `a`, while in the former case, one has to prove a new version of the fact (based on the effect that the update of `b` caused on `a`). Obviously, when there are many variables, facts and updates, such split case reasoning can become very complicated. ### Absence of aliasing A more promising way is to make sure there is no aliasing. One typically needs to introduce frame axioms. Then one can prove absence of aliasing between variables of interest. There are two important cases: - restricting language semantics, to prevent aliasing (e.g. in certain cases) - introducing necessary framing pre-conditions Of course, a caller of such preconditioned code need to prove that the precondition holds before invocation (this is also true in the case of restricting language semantics: e.g. one may need to reject programs violating restrictions). To be able to do that, one may also need necessary framing post-conditions. Restricted language semantics is a quite popular way, as it's typically much easier to implement (especially, when dealing with low level languages, like C, where there are plenty of ways to introduce aliasing), and easier to use (less annotation and prove burden). However, it can rule out many interesting implementations. In the write up, we follow the harder route of specifying and proving absence of dependencies between memory accesses. ### Proving absence of aliasing Here is a rough idea of how such proof can be preformed. First, an effect of a memory mutator is specified. Second, a memory footprint of an expression is specified. If the effect and the footprint are disjoint, i.e. consist of different memory locations, one can conclude that the mutator has no effect on the expression. And thus, the expression can be re-used in the new memory state, which occurs after the mutation is performed. There can be other specification and proving activity, which is discussed further. # Model to prove absence of aliasing Without loss of generality, we assume that one need to prove absence of aliasing between two references (to memory), and at least one of them is used to modify the memory. **NB** We use `reference` instead of `variable`, as the latter notion differ between the DF and SL papers. ## Memory model There is an infinite set of memory `location`s. A `state` is a finite mapping from `location`s to `value`s. A `location` in the domain of the `state` is called allocated (in the `state`). Typically, a `location` is a `value` and can be dereferenced (and stored in memory), then we refer to it as an `address`. `Value`s can be composite (e.g. tuples, records, etc), however, we assume that they are immutable. **NB** A memory can be non-addressable, i.e. there is no dereference operation on address-valued expressions. One can still access locations of such memory, though aliasing is not possible as any access to the same location looks the same way. Such memory can still be useful to describe parts of a mixed memory model, like store + heap in the SL paper. Another example is stack variables in a language like Java or registers in an assembly language. Apart from operations to read from and write to a `location` (either directly or via dereferencing), there can be allocation and (optional) de-allocation operations. An allocation is performed from a set of non-allocated locations, and de-allocation is a reverse procedure. ## Framing specifications A `framing specification` describes how a mutator or an expression is related to memory locations. There are two aspects od such specifications: - a memory `effect` of a mutator (allocation, modification or de-allocation) - a memory `footprint` of a (pure) expression - memory locations that its value depends on The specifications need not be exact, i.e. an upper bound can be specified in practice. Both `effect` and `footprint` refer to an allocated memory region (sets of allocated locations). **NB** While we use term `set` here, in practice one need a way to reason about regions and construct them. This is straightforward, when a logic has built-in sets. For example, it's the case for the DF theory. However, the SL paper uses inductively defined predicates (over algebraic data structure), which is an alternative to sets. An informal meaning of the `effect` is that if an allocated location doesn't belong to the `effect` than its content is not affected by the mutator. An informal meaning of the `footprint` is that if no location from the `footprint` has been modified, then the expression value has not changed. In simple cases, one doesn't need to specify a `footprint` or an `effect` explicitly. However, a variable's value can be an object, which consists of many memory locations, some of them can be hidden. For example, it's the case in the DF theory, which supports object oriented and refinement features. Actual `framing specification`s can combine `footprint` and `effect` in one statement. For example, "m **reads** a" may mean that method `m` can read `a` (footprint) and it doesn't modify the heap (effect). Similarly, "m **modifies** a" may mean that method `m` can read/modify `a`. However, we ignore them in our model. ## Programming language assumptions For convenience, computational state can be split in an addressable and a non-addressable parts. The former is usually called `heap`, while latter can be called `store` (e.g. in the SL paper) or `stack` or `registers`. Though, stack in C *is* addressable, so it's an unsafe term. Without loss of generality, we assume that each imperative (i.e. impure) statement consists of several pure expressions (which can read memory) and one mutator. So, it has both `footprint` and `effect`. It's likely that a logic used to prove correctness of a program, follows the [Hoare-style](https://en.wikipedia.org/wiki/Hoare_logic), like the SL. Or inspired from it, like [Refinement calculus](https://en.wikipedia.org/wiki/Refinement_(computing)#Refinement_calculus). A `footprint` of a statement can be seen as a part of its precondition. Indeed, if a statement reads a memory location, which is not allocated, then such program is not "well-specified" and should be either rejected or aborted (though returning some default value is an option too). Similarly, an `effect` can be a part of its post-condition. ## Disjointness One may also need a `disjointness` statement, to prove that an `effect` and a `footprint` regions are disjoint. There can be an explicit `disjoint` predicate, as in the DF theory, while SL employs `separating conjunction` $P * Q$. The SL's frame rule also uses a precondition that a computation's `effect` on `store` variables is disjoint with the set of free `store` variables of the frame. ## Proof activity We now describe sketches of proof activity. Concrete details can vary depending on programming language semantics and underlying logic/theories. ### Absence of aliasing Now, there are all ingredients (to attempt) to construct a proof of absence of aliasing. Let's assume there is an imperative statement and a (pure) expression about some memory region. We also assume that both the statement and the expression reference syntactically different memory regions, which can alias though. After execution of the statement, the memory `state` will become different, so if one re-evaluate the expression, its value may change. Therefore, one cannot use it as a fact in the new state. Our goal is to be able to prove absence of aliasing, so that the expression can be re-used, when it's required. Else one has to derive the value of the expression in the new state, which can be costly (especially, if there are many such expressions). In our model, it's sufficient to prove that the `effect` of the statement is disjoint with the `footprint` of the expression. Indeed, if the `footprint` is disjoint with the `effect` then content of every `footprint`'s location is not changed by the statement. Thus, the expression value has not changed in the new state. ### Framing specification proofs One may also need to prove that a program or an expression obeys their `framing specification`s, i.e. that a statement doesn't modify already allocated locations beyond its `effect` (it may modify freshly allocated locations). And that an expression doesn't read memory beyond its `footprint`. #### Permission logics Programming language semantics may require aborting a program execution, when it dereferences or de-allocates an unallocated memory location. This is the case for the SL. In such case, an assertion about heap in a precondition can certify that memory locations are allocated. So, such assertion acts as a permission to access the locations. Such logics are sometimes called permission logics. In order to avoid implementing the aborting semantics in runtime, one may need to prove that a statement or an expression access only locations, for which there exist permissions in their pre-conditions. ### Auxiliary proofs There, of course, may be required more or less traditional proofs, like proving that a pre-condition of a statement is fulfilled, proving that sets/regions are disjoint or proving that two address-valued expressions are equivalent. # Composite memory models One can combine several kinds of addressable and non-addressable memories, which can have different location/address and value types. Memory states can be stored in memory too, as states are values. We assume that all memory kinds are disjoint, i.e. pairwise intersections of their sets of locations are all empty. Combined with dereferencing operations that becomes a quite expressive framework to express various memory models and control aliasing. Here are some simple examples. ## Dynamic Frames' memory model In the [Dynamic Frames](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.185.2957&rep=rep1&type=pdf), there is only one kind of memory. Locations can be dereferenced. Values are not specified precisely, however, they at least contain integers, locations and sets. De-allocation is not mentioned. ## Separation Logic In Separation Logic (as described in the [paper](https://www.cs.cmu.edu/~jcr/seplogic.pdf)), there are two kinds of memory: Store and Heap. Store is non-addressable, it's a mapping from variables to values. Heap is addressable, it's a mapping from addresses to values. There are several flavors of SL logic and its memory model. In a simple variant, values are infinite integers and addresses are a subset of integers. Of course, one can introduce more expressible type system (which is also discussed in the paper). Integer addresses together with dereference operations allow pointer arithmetics. There is also a de-allocation operation. ## Strict aliasing rule A simple approach to restrict aliasing is to forbid aliasing between references of fundamentally different types (e.g. [strict aliasing rule in C/C++](https://en.wikipedia.org/wiki/Pointer_aliasing#Aliasing_and_re-ordering)). In Java, references to objects of incompatible types cannot alias too. Such memory model is employed in formal verification too (e.g. see [Frama-C/WP, section 3.6](http://frama-c.com/download/frama-c-wp-manual.pdf)). ## Modeling mutable data structures Values are immutable in our model. However, memory states (i.e. mappings from locations to values) are immutable and can be stored in memory. We thus can model mutability by introducing a dedicated memory kind for a data structure and store its states in memory, since a memory state is an immutable value. A location of a (mutable) data structure and a location of its state are thus distinct notions and we can keep them in different memory kinds. In order to associate the data structure's location with its state's location, we can introduce handles, which is a value, containing a pointer to a data state (and perhaps some additional information like tags). For example, a data structure update `a.f = b` can look something like this: ``` handle = *addr_a currState = *handle // handle is a pointer to state in the example newState = currState.copy(f = b) *handle = newState ``` This is a somewhat verbose example, to illustrate using of dereferencing (i.e. `*` operation) and handles. The `o.copy(f = b)` creates a new copy of `o` value, where field `f` is replaced with value `b`. A more concise version can look like (for a variable in an addressable heap): ``` *a = (*a).copy(f = b) ``` Or just (for a variable in a non-addressable store): ``` a = a.copy(f = b) ``` ## Java-like memory model Since we can model mutable data structures, we can express a Java-like memory model too. E.g. one can introduce two kinds of stores for each class to hold states of static fields and object fields. And stores to hold states of arrays. There also should be stores to keep states of stack frames of methods.

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully