Search space complexity -> Re: ARC model specified in spinroot/promela
Hi Again,
This email is a very long rabbit-hole into formal verification itself -
If you don't really care about this - just about QA and CI integration,
then you can skip this mail entirely - I will send an update on that in
a few days.
If you would like to help out with the verification run, then please try
the attached patch on a reasonably powerful machine (previous reports on
list seem to have made progress with about 64GB of RAM etc.), and please
send me the output offlist. TIA!
As usual, with $PATH suitably set for spin and/or modex, the commands
are:
# For hand written model verification
$ make clean spin-gen spin-run spin-trace
# For modex extracted model verification
$ make clean modex-gen spin-run spin-trace
If you care about the details and some minimal theory of how spin can be
best made to explore statespace, and how it is different from
hand-written test cases, then please read on, and also try the updated
patch below.
contd. - inline below please read on...
> Mathew, Cherry G * writes:
[...]
> A few things remain WIP. Obviously, this is a "toy" model - it has
> stack based "memory" management, and a directory buffer size of
> 64. So that needs to be hammered on a bit more. Further, I'm keen
> to now dip my toes into re-entrancy. If you noticed in the earlier
> patches, there were model routines for
> "mutex_enter()/mutex_exit()". I'll re-look these and play around a
> little bit with concurrent entry of ARC() (ARC() is now strictly
> entered sequentially in a loop - see arc.drv). Once things settle
> down, I will look at making an actual block disk driver with an
> ARC(9) managed cache using this code.
I will work on this next. But see below for FV details.
> Another area of further work would be the *.inv files, where the
> invariant specifications are made.
I bring your attention to the last expression in arc/arc.inv
Here, there is a block marked as follows:
#if 1 /* Disable for easy demo. Enable to search harder */
This is a knob you can turn off, to have a quick understanding of the
description below. When I refer to the spin "trace", I am referring to
what the console barfs out when you run the above commands with the
#if negated (ie; s/#if 1/#if 0/)
> I also noticed that the arc input trace makes the extracted and
> hand-coded models exlclude slightly different looking codepaths. I
> need to review these to understand how/what is different. Finally,
> need to update the input trace itself to exercise all possible
> codepaths in both.
This problem went away on its own, because the current state space
exploration code works all code paths (confirmed on both handcoded model
and modex extracted model).
Right, so let's start with what we're trying to do, and how spin (and
modern Finite Automaton based verification) differs from predicate logic
based testing (eg: assert()s ) we currently rely on in our codebases.
I'm obviously not an FV expert, so please refer to all the very
excellent documentation out there - in particular, this spin Tutorial
helped me quite a bit [1]. If my description below is inconsistent or
has errors, please feel free to point them out, on list.
Chapter 1: Specification vs. Code
Given a "program" - in this case our promela model in arc/arc.pml, spin
views each statement as a potentially reachable "state" in a None
Deterministic Finite Automaton (NDFA). The first line will be the
"Start" state, and the end of the file is usually considered the "End"
state (unless one modifies that using "end:" labels - see spin manual
for details). We will call a single example of the
{ "Start" -> "Execute subset of given statements" -> "End" }, a "Run".
Why "Non Deterministic" ? Because there are more than one possible
branches a next statement can take, to land on the next state, during a
"Run". Which branch (formally called "Edge", "Arrow" or "transition",
depending on which book you read) you take, can depend on the current
state of select variables one can specify. Spin provides the if/fi and
do/od constructs to facilitate this.
Aside:
Note that a single run is deterministic and can thus be viewed as a
constituent subset DFA of the equivalent DFA to the specified
NDFA. (Recall that any NDFA can be "broken out" into an equivalent
DFA).
This is where things start to get interesting. When we normally program
in C, for eg:, the programming language semantics do not force us to
consider all possible execution paths and possibilities.
As programmers, we thus usually mentally model only the functional case
at hand (eg: Add two numbers, add()) and relegate the alternative cases
(eg: the legal range of values that can be provided to the add()
function) to a "later" problem to be investigated during "testing".
For eg: if I were to write the following C code block:
state0: /* Start */
{
state1:
int i = 0;
state2:
if (i == 0) {
state3:
printf("Expr
Re: ARC model specified in spinroot/promela
> Mathew, Cherry G * writes:
[...]
> With that thought, I take leave for the weekend, and hope to put
> my money where my mouth is, by debugging the current C
> implementation using ideas described above.
> $ make prog $ ./arc
> should give you a crash dump for gdb, which, I'm getting a SEG
> fault at delLRU() well into the caching input trace (T1 has 59 out
> of 64 entries filled!)
Hello again,
Reporting back after a really interesting weekend. Highlights are:
- The "D3" methodology seems to work as advertised - the system found a
little sneaky "ordering" bug related to "list"->qcount inc/dec and
TAILQ() ops.
- the segfault turned out to be namespace collision related
complications unrelated to the ARC algo.
I wrote up an "Equivalence Verification" subdirectory (see arc_queue/
below) - and moved around a few files. Other than these, everything
shoud be self explanatory.
A few things remain WIP. Obviously, this is a "toy" model - it has stack
based "memory" management, and a directory buffer size of 64. So that
needs to be hammered on a bit more. Further, I'm keen to now dip my toes
into re-entrancy. If you noticed in the earlier patches, there were
model routines for "mutex_enter()/mutex_exit()". I'll re-look these and
play around a little bit with concurrent entry of ARC() (ARC() is now
strictly entered sequentially in a loop - see arc.drv). Once things
settle down, I will look at making an actual block disk driver with an
ARC(9) managed cache using this code.
Another area of further work would be the *.inv files, where the
invariant specifications are made.
I also noticed that the arc input trace makes the extracted and
hand-coded models exlclude slightly different looking codepaths. I need
to review these to understand how/what is different. Finally, need to
update the input trace itself to exercise all possible codepaths in
both.
TLDR, "I want to try this":
Pre-requisites: installed spin and modex in $PATH
$ make spin-gen spin-run;make clean;make modex-gen spin-run;make clean;make
prog;./arc;make clean
Ditto above after:
$ cd arc_queue
If you want to inspect the generated / hand coded spinmodel.pml do so
after the respective '*-gen' targets run above - they create and
overwrite the pre-existing spinmodel.pml in $PWD
As always, looking forward to bricks and bats!
Many Thanks.
--
~cherry
diff -urN arc.null/arc.c arc/arc.c
--- arc.null/arc.c 1970-01-01 00:00:00.0 +
+++ arc/arc.c 2023-09-14 13:59:20.607107308 +
@@ -0,0 +1,173 @@
+/* C Implementation of the Adaptive Replacement Cache algorithm. Written by
cherry */
+
+/*
+ * We implement the following algorithm from page 10, Figure 4.
+ *
https://www.usenix.org/legacy/events/fast03/tech/full_papers/megiddo/megiddo.pdf
+ *
+ *
+ * ARC(c)
+ *
+ * INPUT: The request stream x1,x2,,xt,
+ * INITIALIZATION: Set p = 0 and set the LRU lists T1, B1, T2, and B2 to
empty.
+ *
+ * For every t>=1 and any xt, one and only one of the following four cases
must occur.
+ * Case I: xt is in T1 or T2. A cache hit has occurred in ARC(c) and DBL(2c).
+ * Move xt to MRU position in T2.
+ *
+ * Case II: xt is in B1. A cache miss (resp. hit) has occurred in ARC(c)
(resp. DBL(2c)).
+ * ADAPTATION: Update p = min { p + d1,c }
+ * where d1 = { 1 if |B1| >= |B2|, |B2|/|B1| otherwise
+ *
+ * REPLACE(xt, p). Move xt from B1 to the MRU position in T2 (also fetch
xt to the cache).
+ *
+ * Case III: xt is in B2. A cache miss (resp. hit) has occurred in ARC(c)
(resp. DBL(2c)).
+ * ADAPTATION: Update p = max { p - d2,0 }
+ * where d2 = { 1 if |B2| >= |B1|, |B1|/|B2| otherwise
+ *
+ * REPLACE(xt, p). Move xt from B2 to the MRU position in T2 (also fetch
xt to the cache).
+ *
+ * Case IV: xt is not in T1 U B1 U T2 U B2. A cache miss has occurred in
ARC(c) and DBL(2c).
+ * Case A: L1 = T1 U B1 has exactly c pages.
+ * If (|T1| < c)
+ * Delete LRU page in B1. REPLACE(xt,p).
+ * else
+ * Here B1 is empty. Delete LRU page in T1 (also remove it
from the cache).
+ * endif
+ * Case B: L1 = T1 U B1 has less than c pages.
+ * If (|T1| + |T2| + |B1| + |B2| >= c)
+ * Delete LRU page in B2, if (|T1| + |T2| + |B1| + |B2| = 2c).
+ * REPLACE(xt, p).
+ * endif
+ *
+ * Finally, fetch xt to the cache and move it to MRU position in T1.
+ *
+ * Subroutine REPLACE(xt,p)
+ * If ( (|T1| is not empty) and ((|T1| exceeds the target p) or (xt is
in B2 and |T1| = p)) )
+ * Delete the LRU page in T1 (also remove it from the cache),
and move it to MRU position in B1.
+ * else
+ * Delete the LRU page in T2 (also remove it from the cache),
and move it to MRU position in B2.
+ * endif
+ */
+
+#include
Re: ARC model specified in spinroot/promela
> Mathew, Cherry G * writes: > Mathew, Cherry G * writes: > [...] >> The next step is to write the equivalent C code, compile it with >> a simple test "driver" (ideally ATF, but since this is standalone >> for the moment, I'll use something simpler, just to illustrate >> the concept), and then hook it into spin's "modex" tool, to >> extract the implicit model. Once this is done, we'll have a full >> cycle of design-> implement->verify and this will demonstrate the DDD (Design >> Driven Development) methodology I'd love to hear your thoughts >> about. > I'm attaching below patch (with the usual instructions) to give a > sense of direction. At this point, the C code is purely manually > crafted. The next step (modex extraction) ties the original model > (arc.pml and friends) to the extracted code (arc.c and friends). I > am working on that next, and will post later in the week when > that's done. Follow up as promised. Please see below patch with the usual instructions. There's a slight complication though, in that the modex tool isn't packaged for most OSs. It's fairly straightforward to compile it on a posix-y build environment though. You can grab the sources here: https://github.com/nimble-code/Modex I think at this stage a few comments on the model extraction are important: 1) The usual pattern for spin modex is to convert the source code into C code "snippets" that sit within the extracted promela source. What then happens is that, subsets of the code's data state space are exported to the model, which can then be subject to the usual LTL invariance checks. I've taken a different approach here - slightly abusing the extractor framework, I have basically "converted" the C source code back as close to the original model as possible. In order to see this in action, do the following: $ make clean modex-gen $ cp spinmodel.pml /tmp/spinmodel-modex.pml $ make clean spin-gen $ diff -u spinmodel /tmp/spinmodel-modex.pml When you make the comparison, you will see that the extraction attempts a source model level retrieval. I ran this by the author of spin (Dr. Gerard Holzmann) and he said the approach looked interesting. The problem is that the "harness language" used to drive the extraction is very brittle and has terrible syntax IMHO (whoever thought up as token separator ?!!!). See arc.prx for this in action. The advantage of this approach is that you can virtually assume that if the model passes verification, then any logical bugs lie elsewhere. Interestingly, I now have a good challenge, as the queue.h manipulation I have done via arc.h does crash (at delLRU() ). So this will be a good test of my theory. At the very least, the model extraction process is a far more rigourous process of code review - where questions about logic can be asked formally via the LTL constraints. 2) The modex data object export support is terrible - it only supports basic data types, and the source parser is unable to automatically export expressions. As you probably noticed in the arc.prx file, I've kludged around this by doing effectively a sed s// over all expressions in the source code. This shortcoming can be addressed by improving the modex parser code. I have applied for a research grant in this regard, and if it comes through, then I am committed to fixing it (in consultation with the primary author, of course). 3) The gap between extracted code, and the "glue" that allows it to be driven both as a final compiled program, and as a module under test can be addressed by setting up infrastructure that I'm calling "Equivalence Verification/Testing". For eg: the semantics of readLRU()/delLRU() are implemented using spin "channel"s in the model, and a simple queue(3) mechanism in the C implementation. Equivalence will guarantee that they implement the intended semantics accurately. I believe that the *BSD OSs have a firm USP, if we can implement modular verification as part of the CI infrastructure - having a mirror "Equivalence" infrastructure will make this process much easier for new modules, as well as ingesting existing ones. With that thought, I take leave for the weekend, and hope to put my money where my mouth is, by debugging the current C implementation using ideas described above. $ make prog $ ./arc should give you a crash dump for gdb, which, I'm getting a SEG fault at delLRU() well into the caching input trace (T1 has 59 out of 64 entries filled!) Finally, if you know anyone contracting or hiring software Engineers or Leads, where an intersect between concurret programming and modelling is interesting, please signpost me (offlist of course). Have a great weekend! -- ~cherry diff -urN arc.null/arc.c arc/arc.c --- arc.null/arc.c 1970-01-01 00:00:00.0 + +++ arc/arc.c 2023-09-07 07:56:40.245824950 + @@ -0,0 +1,174 @@ +/* C Implementation of the Adaptive Replacement Cache algorithm. Written by
Re: ARC model specified in spinroot/promela
> Mathew, Cherry G * writes:
[...]
> The next step is to write the equivalent C code, compile it with a
> simple test "driver" (ideally ATF, but since this is standalone
> for the moment, I'll use something simpler, just to illustrate the
> concept), and then hook it into spin's "modex" tool, to extract
> the implicit model. Once this is done, we'll have a full cycle of
design-> implement->verify and this will demonstrate the DDD (Design
> Driven Development) methodology I'd love to hear your thoughts
> about.
I'm attaching below patch (with the usual instructions) to give a sense
of direction. At this point, the C code is purely manually crafted. The
next step (modex extraction) ties the original model (arc.pml and
friends) to the extracted code (arc.c and friends). I am working on that
next, and will post later in the week when that's done.
Meanwhile, please feel free to comment on this approach at Design Driven
Development (D3) if you have any.
--
~cherry
diff -urN arc.null/arc.c arc/arc.c
--- arc.null/arc.c 1970-01-01 00:00:00.0 +
+++ arc/arc.c 2023-09-06 11:18:57.502125698 +
@@ -0,0 +1,173 @@
+/* C Implementation of the Adaptive Replacement Cache algorithm. Written by
cherry */
+
+/*
+ * We implement the following algorithm from page 10, Figure 4.
+ *
https://www.usenix.org/legacy/events/fast03/tech/full_papers/megiddo/megiddo.pdf
+ *
+ *
+ * ARC(c)
+ *
+ * INPUT: The request stream x1,x2,,xt,
+ * INITIALIZATION: Set p = 0 and set the LRU lists T1, B1, T2, and B2 to
empty.
+ *
+ * For every t>=1 and any xt, one and only one of the following four cases
must occur.
+ * Case I: xt is in T1 or T2. A cache hit has occurred in ARC(c) and DBL(2c).
+ * Move xt to MRU position in T2.
+ *
+ * Case II: xt is in B1. A cache miss (resp. hit) has occurred in ARC(c)
(resp. DBL(2c)).
+ * ADAPTATION: Update p = min { p + d1,c }
+ * where d1 = { 1 if |B1| >= |B2|, |B2|/|B1| otherwise
+ *
+ * REPLACE(xt, p). Move xt from B1 to the MRU position in T2 (also fetch
xt to the cache).
+ *
+ * Case III: xt is in B2. A cache miss (resp. hit) has occurred in ARC(c)
(resp. DBL(2c)).
+ * ADAPTATION: Update p = max { p - d2,0 }
+ * where d2 = { 1 if |B2| >= |B1|, |B1|/|B2| otherwise
+ *
+ * REPLACE(xt, p). Move xt from B2 to the MRU position in T2 (also fetch
xt to the cache).
+ *
+ * Case IV: xt is not in T1 U B1 U T2 U B2. A cache miss has occurred in
ARC(c) and DBL(2c).
+ * Case A: L1 = T1 U B1 has exactly c pages.
+ * If (|T1| < c)
+ * Delete LRU page in B1. REPLACE(xt,p).
+ * else
+ * Here B1 is empty. Delete LRU page in T1 (also remove it
from the cache).
+ * endif
+ * Case B: L1 = T1 U B1 has less than c pages.
+ * If (|T1| + |T2| + |B1| + |B2| >= c)
+ * Delete LRU page in B2, if (|T1| + |T2| + |B1| + |B2| = 2c).
+ * REPLACE(xt, p).
+ * endif
+ *
+ * Finally, fetch xt to the cache and move it to MRU position in T1.
+ *
+ * Subroutine REPLACE(xt,p)
+ * If ( (|T1| is not empty) and ((|T1| exceeds the target p) or (xt is
in B2 and |T1| = p)) )
+ * Delete the LRU page in T1 (also remove it from the cache),
and move it to MRU position in B1.
+ * else
+ * Delete the LRU page in T2 (also remove it from the cache),
and move it to MRU position in B2.
+ * endif
+ */
+
+#include "arc.h"
+
+void arc_list_init(struct arc_list *_arc_list)
+{
+ TAILQ_INIT(&_arc_list->qhead);
+ _arc_list->qcount = 0;
+
+ for(int i = 0; i < ARCLEN; i++) {
+ init_arc_item(&_arc_list->item_list[i], IID_INVAL, false);
+ };
+}
+
+int p, d1, d2;
+struct arc_list _B1, *B1 = &_B1, _B2, *B2 = &_B2, _T1, *T1 = &_T1, _T2, *T2 =
&_T2;
+
+void arc_init(void)
+{
+ p = d1 = d2 = 0;
+
+ arc_list_init(B1);
+ arc_list_init(B2);
+ arc_list_init(T1);
+ arc_list_init(T2);
+}
+
+struct arc_item _LRUitem, *LRUitem = &_LRUitem;
+
+static void
+REPLACE(struct arc_item *x_t, int p)
+{
+
+
+ init_arc_item(LRUitem, IID_INVAL, false);
+
+ if ((lengthof(T1) != 0) &&
+ ((lengthof(T1) > p) ||
+(memberof(B2, x_t) && (lengthof(T1) == p {
+ /* XXX: d_step { ? */
+ readLRU(T1, LRUitem);
+ delLRU(T1);
+ cacheremove(LRUitem);
+ addMRU(B1, LRUitem);
+ } else {
+ readLRU(T2, LRUitem);
+ delLRU(T2);
+ cacheremove(LRUitem);
+ addMRU(B2, LRUitem);
+ }
+}
+
+void
+ARC(struct arc_item *x_t)
+{
+ if (memberof(T1, x_t)) { /* Case I */
+ delitem(T1, x_t);
+ addMRU(T2, x_t);
+ }
+
+ if (memberof(T2, x_t)) { /*
Re: ARC model specified in spinroot/promela
> Mathew, Cherry G * writes:
[...]
> So there's room for improvement in my Makefile to:
> 1) Explore multithreaded/CPU
> 2) Understand state space explosion scale - it is exponential to
> the number of processes, I've fixed it to run in a loop - will
> report back once I have good progress also with the model
> extraction part.
> 3) My model has errors - I'm glad to see that the error reporting
> is the same in my new optimised loop, as in the RAM hungry version
> you ran.
> Thanks once again - this is very helpful. I will report back once
> I have more progress.
Hi Again,
So just wanted to report back with a patch (absolute patch, not relative,
so please nuke your existing arc/ if you tried the old one and apply in
a clean new subdirectory) on the following:
- implemented a loop method to drive the ARC() function runs in a second
at most.
- the model verification caught a logical error in my model! It was
causing an unbounded growth in the T2 directory list.
- expanded the invariants to include several ones described in the
paper.
- cleaned up the "UI" a bit - now you can directly run the verifier
using: "make clean spin-run"
Since the objective here is to demonstrate the dev methodology, I've
left the verifier with a couple of unexplored code/state-paths. See
comments in arc.drv for more on how to fix that. (I will look into this
later).
The next step is to write the equivalent C code, compile it with a
simple test "driver" (ideally ATF, but since this is standalone for the
moment, I'll use something simpler, just to illustrate the concept), and
then hook it into spin's "modex" tool, to extract the implicit
model. Once this is done, we'll have a full cycle of
design->implement->verify and this will demonstrate the DDD (Design
Driven Development) methodology I'd love to hear your thoughts about.
Many Thanks in advance for any ideas, thoughts, critiques, bugs, etc.
--
~cherry
diff -urN arc.null/arc.drv arc/arc.drv
--- arc.null/arc.drv1970-01-01 00:00:00.0 +
+++ arc/arc.drv 2023-09-04 10:32:34.042373574 +
@@ -0,0 +1,52 @@
+/* Spin process model for Adaptive Replacement Cache algorithm. Written by
cherry */
+
+/* Note: What we're attempting in this driver file, is to generate an
+ * input trace that would exercise all code-paths of the model specified
+ * in arc.pml
+ *
+ * Feeding a static trace to the algorithm in array _x[N_ITEMS] is a
+ * acceptable alternative.
+ */
+#include "arc.pmh"
+
+#define N_ITEMS (2 * C) /* Number of distinct cache items to test with */
+#define ITEM_REPS (C / 4) /* Max repeat item requests */
+#define N_ITERATIONS 3
+
+hidden arc_item _x[N_ITEMS]; /* Input state is irrelevant from a verification
PoV */
+hidden int _x_iid = 0;
+hidden int _item_rep = 0;
+hidden byte _iterations = 0;
+
+/* Drive the procs */
+init {
+
+ atomic {
+ do
+ ::
+ _iterations < N_ITERATIONS ->
+
+ _x_iid = 0;
+ do
+ :: _x_iid < N_ITEMS ->
+ init_arc_item(_x[_x_iid], _x_iid, false);
+ _item_rep = 0;
+ do
+ :: _item_rep < (_x_iid % ITEM_REPS) ->
+ ARC(_x[_x_iid]);
+ _item_rep++;
+ :: _item_rep >= (_x_iid % ITEM_REPS) ->
+ break;
+ od
+ _x_iid++;
+ :: _x_iid >= N_ITEMS ->
+ break;
+ od
+ _iterations++;
+ ::
+ _iterations >= N_ITERATIONS ->
+ break;
+ od
+ }
+
+}
diff -urN arc.null/arc.inv arc/arc.inv
--- arc.null/arc.inv1970-01-01 00:00:00.0 +
+++ arc/arc.inv 2023-09-04 09:20:20.224107390 +
@@ -0,0 +1,59 @@
+/* $NetBSD$ */
+
+/* These are Linear Temporal Logic invariants (and constraints)
+ * applied over the statespace created by the promela
+ * specification. Correctness is implied by Logical consistency.
+ */
+ltl
+{
+ /* Liveness - all threads, except control must finally exit */
+ eventually always (_nr_pr == 1) &&
+ /* c.f Section I. B, on page 3 of paper */
+ always ((lengthof(T1) +
+lengthof(B1) +
+lengthof(T2) +
+lengthof(B2)) <= (2 * C)) &&
+
+ /* Reading together Section III. A., on page 7, and
+* Section III. B., on pages 7,8
+*/
+ always ((lengthof(T1) + lengthof(B1)) <= C) &&
+ always ((lengthof(T2) + lengthof(B2)) < (2 * C)) &&
+
+ /* Section III. B, Remark III.1 */
+ always ((lengthof(T1) + lengthof(T2)) <=

