atomic methods and invariants
#983
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR improves Gobra's situation in dealing with atomicity, so that we no longer need informal arguments to justify that opening invariants around certain parts of the code is safe. In particular, it brings the following changes:
atomicmodifier for abstract methods and functions (non-abstract atomic members are disallowed, as we cannot prove atomicity - at least for now). Atomic methods should be those whose effects occur, logically, at a single linearization point. Interface methods may be marked asatomictoo, in which case they may only be implemented byatomicmethods.Pof typepred()is an invariant, written asInvariant(p)if it has been shown to hold using theEstablishInvariantbuiltin ghost function. Once established, invariants must be preserved by all atomic operations, and thus, by all operations.P(), wherePis a built-in FPredicate likePredTrue. We were forced to writePredTrue!<!>()at all times before.This statement opens invariant
P!<!>(), which is assumed at the start of the critical region, and must be shown at its end. Critical regions check that there is no re-entrance, i.e., no invariant is opened twice. Statements inSmay contain, at most, a single call to an atomic method (from an interface or otherwise, more on this later) and arbitrary ghost code (which must be shown to terminate). Like theoultinestatement, thecriticalstatement does not introduce a block (i.e., it does not introduce its own namespace).There are two critical decisions I took to simplify the logics here:
PopensPagain. One way of doing this would be to have some way of tracking the currently open invariants, and specify in the method specifications which invariants are required to not be open. This requires a more complex encoding. Instead, I opted for the following split of concerns, which I think is not very limiting:ghostmethods cannot open invariants. The invariants must be opened in the actual code before calling ghost methods that depend on them. Thus, ghost methods may all be called safely from critical regions. This makes checking for reentrance very easy.Scontains a calli.M(), whereiis of interface type, I believe that reasoning about this program is similar to reasoning about the following:Step (1) is "transparent", i.e., its effects cannot be observed, if the value stored in
icannot change between (1) and (2), which guarantees that the method that is called still matches the dynamic type of the value stored ini. I believe that is always the case:iis either in an exclusive or shared memory location.i.M, there must be at least read permissions toiin the current thread. The permissions may either come from from the surrounding environment or fromP!<!>().a. If they come the former, then no other thread may ever obtain full permission to
iand modify it while the call is being performed.b. If they come from the latter, another thread could in principle try to open
P!<!>()in parallel and modifyiin an atomic step. However, there is no way to do so as far as I can tell. Regular assignments toiare not atomic (and thus, disallowed in critical regions) and the packageatomicdoes not offer a way (as far as I can see) to atomically mutate a variable of interface type.EDIT: maybe disallowing opening invariants in ghost methods is too restrictive after all. An example where this is very limiting is in the implementation of Iris's ghost locations in gobra-libs, where the only way to implement a model for this requires an invariant. At first sight, a solution to this may be to require an annotation on methods that may open invariants and disallow calling those methods from methods without that annotation or inside critical regions. More experience is required to see if this is permissive enough.