-
Notifications
You must be signed in to change notification settings - Fork 0
An example showing how to read from an array of structs #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikswamy
wants to merge
1
commit into
main
Choose a base branch
from
_array_example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,101 @@ | ||||||||||||||
| #include "pal.h" | ||||||||||||||
| #include <stdint.h> | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
| * Regression test for preserving the logical snapshot fact produced by | ||||||||||||||
| * Pulse.Lib.C.Array.array_read. | ||||||||||||||
| * | ||||||||||||||
| * PAL models an _array value with the ghost snapshot | ||||||||||||||
| * | ||||||||||||||
| * array_value_of entries : Seq.seq (option entry_t) | ||||||||||||||
| * | ||||||||||||||
| * and array_read proves that the concrete value read at index i rewrites to | ||||||||||||||
| * | ||||||||||||||
| * Some?.v (Seq.index (array_value_of entries) i) | ||||||||||||||
| * | ||||||||||||||
| * provided the array element exists. This regression records that PAL's | ||||||||||||||
| * ordinary translation of a C local initialization such as | ||||||||||||||
| * | ||||||||||||||
| * entry_t entry = entries[i]; | ||||||||||||||
| * | ||||||||||||||
| * as a direct read into the mutable local is strong enough: the array_read | ||||||||||||||
| * postcondition remains available through the assignment, so a ghost assertion | ||||||||||||||
| * about entry can use a precondition quantified over array_value_of entries. | ||||||||||||||
| * | ||||||||||||||
| * The key point is that no special emitter machinery is needed here: PAL should | ||||||||||||||
| * neither introduce a second read temporary nor emit an extra pure equality | ||||||||||||||
| * assertion. These tests exercise both whole-struct reads and field reads, and | ||||||||||||||
| * fail if the direct array_read assignment loses the snapshot refinement. | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| typedef struct { | ||||||||||||||
| uint32_t x; | ||||||||||||||
| } entry_t; | ||||||||||||||
|
|
||||||||||||||
| _include_pulse( | ||||||||||||||
| $declare(entry_t e) | ||||||||||||||
|
|
||||||||||||||
| let entry_ok ($(e): $type(entry_t)) : prop = | ||||||||||||||
| UInt32.v $(e.x) < 10 | ||||||||||||||
|
|
||||||||||||||
| // Specification supplied by the caller: every initialized entry in the first | ||||||||||||||
| // n array slots satisfies entry_ok. The proof obligations below are about | ||||||||||||||
| // concrete locals read from entries[i], so they require PAL to expose the | ||||||||||||||
| // connection between those locals and this ghost snapshot. | ||||||||||||||
| let entries_ok (s: Seq.seq (option $type(entry_t))) (n: nat) : prop = | ||||||||||||||
| forall i. i < n ==> Some? (Seq.index s i) ==> | ||||||||||||||
| entry_ok (Some?.v (Seq.index s i)) | ||||||||||||||
|
Comment on lines
+45
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
| * Whole-struct read case. | ||||||||||||||
| * | ||||||||||||||
| * The assertion after the read intentionally mentions only the concrete local | ||||||||||||||
| * entry. It does not restate the Seq.index expression; the proof relies on the | ||||||||||||||
| * ordinary array_read assignment preserving enough information to connect entry | ||||||||||||||
| * back to array_value_of entries. | ||||||||||||||
| */ | ||||||||||||||
| void read_struct_from_snapshot( | ||||||||||||||
| _refine(this._length == 64) _array entry_t *entries, | ||||||||||||||
| uint32_t count) | ||||||||||||||
| _requires(count <= entries._length) | ||||||||||||||
| _requires((bool) _inline_pulse( | ||||||||||||||
| entries_ok (array_value_of $(entries)) (UInt32.v $(count)))) | ||||||||||||||
| { | ||||||||||||||
| for (uint32_t i = 0; i < count; i = i + 1) | ||||||||||||||
| _invariant(_live(i) && _live(count)) | ||||||||||||||
| _invariant(i <= count) | ||||||||||||||
| _invariant(count <= entries._length) | ||||||||||||||
| _invariant((bool) _inline_pulse( | ||||||||||||||
| entries_ok (array_value_of $(entries)) (UInt32.v $(count)))) | ||||||||||||||
| { | ||||||||||||||
| entry_t entry = entries[i]; | ||||||||||||||
| _ghost_stmt(assert pure (entry_ok $(entry))); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
| * Field-read case. | ||||||||||||||
| * | ||||||||||||||
| * C code often reads only a field of an array element, e.g. entries[i].x. The | ||||||||||||||
| * same snapshot fact must be available after projection: the local x should be | ||||||||||||||
| * known equal to the x field of the snapshot entry at i. | ||||||||||||||
| */ | ||||||||||||||
| void read_field_from_snapshot( | ||||||||||||||
| _refine(this._length == 64) _array entry_t *entries, | ||||||||||||||
| uint32_t count) | ||||||||||||||
| _requires(count <= entries._length) | ||||||||||||||
| _requires((bool) _inline_pulse( | ||||||||||||||
| entries_ok (array_value_of $(entries)) (UInt32.v $(count)))) | ||||||||||||||
| { | ||||||||||||||
| for (uint32_t i = 0; i < count; i = i + 1) | ||||||||||||||
| _invariant(_live(i) && _live(count)) | ||||||||||||||
| _invariant(i <= count) | ||||||||||||||
| _invariant(count <= entries._length) | ||||||||||||||
| _invariant((bool) _inline_pulse( | ||||||||||||||
| entries_ok (array_value_of $(entries)) (UInt32.v $(count)))) | ||||||||||||||
| { | ||||||||||||||
| uint32_t x = entries[i].x; | ||||||||||||||
| _ghost_stmt(assert pure (UInt32.v $(x) < 10)); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.