2004-08-15 12:55:22 +01:00
|
|
|
/**
|
|
|
|
* Handle bindings for Arache engine.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "list.h"
|
2004-08-18 20:43:58 +01:00
|
|
|
#include "role.h"
|
2004-08-27 20:06:15 +01:00
|
|
|
#include "label.h"
|
2004-08-15 12:55:22 +01:00
|
|
|
#include "system.h"
|
|
|
|
#include "binding.h"
|
2004-08-15 15:57:50 +01:00
|
|
|
#include "warshall.h"
|
2004-08-15 12:55:22 +01:00
|
|
|
#include "memory.h"
|
2004-08-15 17:08:53 +01:00
|
|
|
#include "debug.h"
|
2004-08-17 12:03:18 +01:00
|
|
|
#include "term.h"
|
2004-08-27 20:06:15 +01:00
|
|
|
#include "termmap.h"
|
2005-05-19 15:43:32 +01:00
|
|
|
#include "arachne.h"
|
2005-06-07 16:02:27 +01:00
|
|
|
#include "switches.h"
|
2006-02-26 15:00:58 +00:00
|
|
|
#include "depend.h"
|
2004-12-08 16:25:27 +00:00
|
|
|
#include <malloc.h>
|
2004-08-15 12:55:22 +01:00
|
|
|
|
2006-01-02 21:06:08 +00:00
|
|
|
static System sys; //!< local storage of system pointer
|
2004-08-15 12:55:22 +01:00
|
|
|
|
2006-01-02 21:06:08 +00:00
|
|
|
extern Protocol INTRUDER; //!< The intruder protocol
|
|
|
|
extern Role I_M; //!< special role; precedes all other events always
|
2004-08-18 20:43:58 +01:00
|
|
|
|
2004-08-15 12:55:22 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Assist stuff
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Create mem for binding
|
|
|
|
Binding
|
2004-08-17 16:52:52 +01:00
|
|
|
binding_create (Term term, int run_to, int ev_to)
|
2004-08-15 12:55:22 +01:00
|
|
|
{
|
|
|
|
Binding b;
|
|
|
|
|
|
|
|
b = memAlloc (sizeof (struct binding));
|
2006-02-26 15:00:58 +00:00
|
|
|
b->done = false;
|
|
|
|
b->blocked = false;
|
2004-08-17 16:52:52 +01:00
|
|
|
b->run_from = -1;
|
|
|
|
b->ev_from = -1;
|
2004-08-15 12:55:22 +01:00
|
|
|
b->run_to = run_to;
|
|
|
|
b->ev_to = ev_to;
|
2004-08-17 12:03:18 +01:00
|
|
|
b->term = term;
|
2004-08-20 11:52:40 +01:00
|
|
|
b->level = 0;
|
2004-08-15 12:55:22 +01:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Remove mem for binding
|
|
|
|
void
|
|
|
|
binding_destroy (Binding b)
|
|
|
|
{
|
2004-08-17 16:52:52 +01:00
|
|
|
if (b->done)
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2004-08-18 10:57:01 +01:00
|
|
|
goal_unbind (b);
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
2004-08-15 12:55:22 +01:00
|
|
|
memFree (b, sizeof (struct binding));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Main
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Init module
|
|
|
|
void
|
|
|
|
bindingInit (const System mysys)
|
|
|
|
{
|
|
|
|
sys = mysys;
|
|
|
|
sys->bindings = NULL;
|
2006-02-26 15:00:58 +00:00
|
|
|
|
|
|
|
dependInit (sys);
|
2004-08-15 12:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Close up
|
|
|
|
void
|
|
|
|
bindingDone ()
|
|
|
|
{
|
|
|
|
int delete (Binding b)
|
|
|
|
{
|
|
|
|
binding_destroy (b);
|
2006-02-26 15:00:58 +00:00
|
|
|
return true;
|
2004-08-15 12:55:22 +01:00
|
|
|
}
|
|
|
|
list_iterate (sys->bindings, delete);
|
|
|
|
list_destroy (sys->bindings);
|
2004-12-08 16:25:27 +00:00
|
|
|
|
2006-02-26 15:00:58 +00:00
|
|
|
dependDone (sys);
|
2004-08-17 16:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-15 12:55:22 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Externally available functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-02-26 15:00:58 +00:00
|
|
|
|
|
|
|
//! Print a binding (given a binding list pointer)
|
2004-08-15 15:07:34 +01:00
|
|
|
int
|
2006-02-26 15:00:58 +00:00
|
|
|
binding_print (const Binding b)
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2006-02-26 15:00:58 +00:00
|
|
|
if (b->done)
|
|
|
|
eprintf ("Binding (%i,%i) --( ", b->run_from, b->ev_from);
|
|
|
|
else
|
|
|
|
eprintf ("Unbound --( ");
|
|
|
|
termPrint (b->term);
|
|
|
|
eprintf (" )->> (%i,%i)", b->run_to, b->ev_to);
|
|
|
|
if (b->blocked)
|
|
|
|
eprintf ("[blocked]");
|
|
|
|
return true;
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
|
|
|
|
2006-02-26 15:00:58 +00:00
|
|
|
//! Bind a goal (true if success, false if it must be pruned)
|
|
|
|
int
|
|
|
|
goal_bind (const Binding b, const int run, const int ev)
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2006-02-26 15:00:58 +00:00
|
|
|
if (b->blocked)
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2006-02-26 15:00:58 +00:00
|
|
|
error ("Trying to bind a blocked goal.");
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
2006-02-26 15:00:58 +00:00
|
|
|
if (!b->done)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (run >= sys->maxruns || sys->runs[run].step <= ev)
|
2006-02-26 17:18:59 +00:00
|
|
|
{
|
|
|
|
globalError++;
|
|
|
|
eprintf ("For term: ");
|
|
|
|
termPrint (b->term);
|
|
|
|
eprintf (" needed for r%ii%i.\n", b->run_to, b->ev_to);
|
|
|
|
eprintf ("Current limits: %i runs, %i events for this run.\n",
|
|
|
|
sys->maxruns, sys->runs[run].step);
|
|
|
|
globalError--;
|
|
|
|
error
|
|
|
|
("trying to bind to something not yet in the semistate: r%ii%i.",
|
|
|
|
run, ev);
|
|
|
|
}
|
2006-02-26 15:00:58 +00:00
|
|
|
#endif
|
|
|
|
b->run_from = run;
|
|
|
|
b->ev_from = ev;
|
|
|
|
if (dependPushEvent (run, ev, b->run_to, b->ev_to))
|
|
|
|
{
|
|
|
|
b->done = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
globalError++;
|
|
|
|
binding_print (b);
|
|
|
|
error ("Trying to bind a bound goal again.");
|
|
|
|
}
|
|
|
|
return false;
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
|
|
|
|
2006-02-26 15:00:58 +00:00
|
|
|
//! Unbind a goal
|
|
|
|
void
|
|
|
|
goal_unbind (const Binding b)
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2006-02-26 15:00:58 +00:00
|
|
|
if (b->done)
|
|
|
|
{
|
|
|
|
dependPopEvent ();
|
|
|
|
b->done = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error ("Trying to unbind an unbound goal again.");
|
|
|
|
}
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
|
|
|
|
2006-02-26 15:00:58 +00:00
|
|
|
//! Bind a goal as a dummy (block)
|
|
|
|
/**
|
|
|
|
* Especially made for tuple expansion
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
binding_block (Binding b)
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2006-02-26 15:00:58 +00:00
|
|
|
if (!b->blocked)
|
|
|
|
{
|
|
|
|
b->blocked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error ("Trying to block a goal again.");
|
|
|
|
}
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
|
|
|
|
2006-02-26 15:00:58 +00:00
|
|
|
//! Unblock a binding
|
2004-08-15 15:07:34 +01:00
|
|
|
int
|
2006-02-26 15:00:58 +00:00
|
|
|
binding_unblock (Binding b)
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2005-01-14 18:18:40 +00:00
|
|
|
if (b->blocked)
|
2006-02-26 15:00:58 +00:00
|
|
|
{
|
|
|
|
b->blocked = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error ("Trying to unblock a non-blocked goal.");
|
|
|
|
}
|
2004-08-17 16:52:52 +01:00
|
|
|
}
|
2004-08-15 15:07:34 +01:00
|
|
|
|
2004-08-17 16:52:52 +01:00
|
|
|
|
|
|
|
//! Add a goal
|
2004-08-20 11:52:40 +01:00
|
|
|
/**
|
|
|
|
* The int parameter 'level' is just to store additional info. Here, it stores priorities for a goal.
|
|
|
|
* Higher level goals will be selected first. Typically, a normal goal is level 0, a key is 1.
|
2006-02-26 15:00:58 +00:00
|
|
|
*
|
|
|
|
* Returns the number of added goals (sometimes unfolding tuples)
|
2004-08-20 11:52:40 +01:00
|
|
|
*/
|
2004-10-18 14:04:34 +01:00
|
|
|
int
|
2004-08-20 11:52:40 +01:00
|
|
|
goal_add (Term term, const int run, const int ev, const int level)
|
2004-08-17 16:52:52 +01:00
|
|
|
{
|
|
|
|
term = deVar (term);
|
2004-08-18 16:46:33 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (term == NULL)
|
2004-08-18 20:43:58 +01:00
|
|
|
error ("Trying to add an emtpy goal term");
|
2004-08-18 16:46:33 +01:00
|
|
|
if (run >= sys->maxruns)
|
2004-08-18 20:43:58 +01:00
|
|
|
error ("Trying to add a goal for a run that does not exist.");
|
2004-08-18 16:46:33 +01:00
|
|
|
if (ev >= sys->runs[run].step)
|
2004-08-18 20:43:58 +01:00
|
|
|
error
|
|
|
|
("Trying to add a goal for an event that is not in the semistate yet.");
|
2004-08-18 16:46:33 +01:00
|
|
|
#endif
|
2004-08-17 16:52:52 +01:00
|
|
|
if (realTermTuple (term))
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2005-05-17 19:45:01 +01:00
|
|
|
return goal_add (TermOp1 (term), run, ev, level) +
|
|
|
|
goal_add (TermOp2 (term), run, ev, level);
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
2004-08-17 16:52:52 +01:00
|
|
|
else
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2004-10-18 14:04:34 +01:00
|
|
|
// Determine whether we already had it
|
|
|
|
int nope;
|
2004-08-15 15:07:34 +01:00
|
|
|
|
2004-10-18 14:04:34 +01:00
|
|
|
int testSame (void *data)
|
2004-11-16 12:07:55 +00:00
|
|
|
{
|
|
|
|
Binding b;
|
|
|
|
|
|
|
|
b = (Binding) data;
|
|
|
|
if (isTermEqual (b->term, term) && run == b->run_to && ev == b->ev_to)
|
|
|
|
{ // abort scan, report
|
2006-02-26 15:00:58 +00:00
|
|
|
return false;
|
2004-11-16 12:07:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // proceed with scan
|
2006-02-26 15:00:58 +00:00
|
|
|
return true;
|
2004-11-16 12:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-18 14:04:34 +01:00
|
|
|
|
|
|
|
nope = list_iterate (sys->bindings, testSame);
|
|
|
|
if (nope)
|
|
|
|
{
|
|
|
|
// Add a new binding
|
|
|
|
Binding b;
|
|
|
|
b = binding_create (term, run, ev);
|
|
|
|
b->level = level;
|
|
|
|
sys->bindings = list_insert (sys->bindings, b);
|
2005-05-17 19:45:01 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (DEBUGL (3))
|
|
|
|
{
|
|
|
|
eprintf ("Adding new binding for ");
|
|
|
|
termPrint (term);
|
|
|
|
eprintf (" to run %i, ev %i.\n", run, ev);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
2004-10-18 14:04:34 +01:00
|
|
|
}
|
2004-08-15 15:07:34 +01:00
|
|
|
}
|
2005-05-17 19:45:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-17 16:52:52 +01:00
|
|
|
//! Remove a goal
|
|
|
|
void
|
2004-10-18 14:04:34 +01:00
|
|
|
goal_remove_last (int n)
|
2004-08-15 18:50:41 +01:00
|
|
|
{
|
2005-05-17 19:45:01 +01:00
|
|
|
while (n > 0)
|
2004-08-17 16:52:52 +01:00
|
|
|
{
|
2005-05-17 19:45:01 +01:00
|
|
|
if (sys->bindings != NULL)
|
|
|
|
{
|
|
|
|
Binding b;
|
2004-10-18 14:04:34 +01:00
|
|
|
|
2005-05-17 19:45:01 +01:00
|
|
|
b = (Binding) sys->bindings->data;
|
|
|
|
binding_destroy (b);
|
|
|
|
sys->bindings = list_delete (sys->bindings);
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error
|
|
|
|
("goal_remove_last error: trying to remove %i too many bindings.",
|
|
|
|
n);
|
|
|
|
}
|
2004-08-17 16:52:52 +01:00
|
|
|
}
|
2004-08-15 18:50:41 +01:00
|
|
|
}
|
2004-08-15 15:07:34 +01:00
|
|
|
|
2004-08-27 20:06:15 +01:00
|
|
|
//! Determine whether some label set is ordered w.r.t. send/read order.
|
|
|
|
/**
|
|
|
|
* Assumes all these labels exist in the system, within length etc, and that the run mappings are valid.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
labels_ordered (Termmap runs, Termlist labels)
|
|
|
|
{
|
|
|
|
while (labels != NULL)
|
|
|
|
{
|
|
|
|
// Given this label, and the mapping of runs, we want to know if the order is okay. Thus, we need to know sendrole and readrole
|
|
|
|
Labelinfo linfo;
|
|
|
|
int send_run, send_ev, read_run, read_ev;
|
|
|
|
|
|
|
|
int get_index (const int run)
|
|
|
|
{
|
|
|
|
Roledef rd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
rd = sys->runs[run].start;
|
|
|
|
while (rd != NULL && !isTermEqual (rd->label, labels->term))
|
|
|
|
{
|
|
|
|
rd = rd->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (rd == NULL)
|
|
|
|
error
|
|
|
|
("Could not locate send or read for label, after niagree holds, to test for order.");
|
|
|
|
#endif
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
linfo = label_find (sys->labellist, labels->term);
|
|
|
|
send_run = termmapGet (runs, linfo->sendrole);
|
|
|
|
read_run = termmapGet (runs, linfo->readrole);
|
|
|
|
send_ev = get_index (send_run);
|
|
|
|
read_ev = get_index (read_run);
|
2006-02-26 15:00:58 +00:00
|
|
|
if (!isDependEvent (send_run, send_ev, read_run, read_ev))
|
2004-08-27 20:06:15 +01:00
|
|
|
{
|
|
|
|
// Not ordered; false
|
2006-02-26 15:00:58 +00:00
|
|
|
return false;
|
2004-08-27 20:06:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proceed
|
|
|
|
labels = labels->next;
|
|
|
|
}
|
2006-02-26 15:00:58 +00:00
|
|
|
return true;
|
2004-08-27 20:06:15 +01:00
|
|
|
}
|
|
|
|
|
2005-01-14 18:18:40 +00:00
|
|
|
//! Check whether the binding denotes a sensible thing such that we can use run_from and ev_from
|
2005-05-17 19:45:01 +01:00
|
|
|
int
|
|
|
|
valid_binding (Binding b)
|
2005-01-14 18:18:40 +00:00
|
|
|
{
|
|
|
|
if (b->done && !b->blocked)
|
2006-02-26 15:00:58 +00:00
|
|
|
return true;
|
2005-01-14 18:18:40 +00:00
|
|
|
else
|
2006-02-26 15:00:58 +00:00
|
|
|
return false;
|
2005-01-14 18:18:40 +00:00
|
|
|
}
|
|
|
|
|
2005-10-08 21:57:39 +01:00
|
|
|
//! Check for unique origination
|
|
|
|
/*
|
|
|
|
* Contrary to a previous version, we simply check for unique origination.
|
|
|
|
* This immediately takes care of any 'occurs before' things. Complexity is N
|
|
|
|
* log N.
|
|
|
|
*
|
|
|
|
* Each term should originate only at one point (thus in one binding)
|
|
|
|
*
|
|
|
|
*@returns True, if it's okay. If false, it needs to be pruned.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
unique_origination ()
|
|
|
|
{
|
|
|
|
List bl;
|
|
|
|
|
|
|
|
bl = sys->bindings;
|
|
|
|
|
|
|
|
while (bl != NULL)
|
|
|
|
{
|
|
|
|
Binding b;
|
|
|
|
|
|
|
|
b = (Binding) bl->data;
|
|
|
|
// Check for a valid binding; it has to be 'done' and sensibly bound (not as in tuple expanded stuff)
|
|
|
|
if (valid_binding (b))
|
|
|
|
{
|
|
|
|
Termlist terms;
|
|
|
|
|
|
|
|
terms = tuple_to_termlist (b->term);
|
|
|
|
if (terms != NULL)
|
|
|
|
{
|
|
|
|
/* Apparently this is a good term.
|
|
|
|
* Now we check whether it occurs in any previous bindings as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
List bl2;
|
|
|
|
|
|
|
|
bl2 = sys->bindings;
|
|
|
|
while (bl2 != bl)
|
|
|
|
{
|
|
|
|
Binding b2;
|
|
|
|
|
|
|
|
b2 = (Binding) bl2->data;
|
|
|
|
if (valid_binding (b2))
|
|
|
|
{
|
|
|
|
Termlist terms2, sharedterms;
|
|
|
|
|
|
|
|
terms2 = tuple_to_termlist (b2->term);
|
|
|
|
sharedterms = termlistConjunct (terms, terms2);
|
|
|
|
|
|
|
|
// Compare terms
|
|
|
|
if (sharedterms != NULL)
|
|
|
|
{
|
|
|
|
// Apparently, this binding shares a term.
|
|
|
|
// Equal terms should originate at the same point
|
|
|
|
if (b->run_from != b2->run_from ||
|
|
|
|
b->ev_from != b2->ev_from)
|
|
|
|
{
|
|
|
|
// Not equal: thus no unique origination.
|
2006-02-26 15:00:58 +00:00
|
|
|
return false;
|
2005-10-08 21:57:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
termlistDelete (terms2);
|
|
|
|
termlistDelete (sharedterms);
|
|
|
|
}
|
|
|
|
bl2 = bl2->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
termlistDelete (terms);
|
|
|
|
}
|
|
|
|
bl = bl->next;
|
|
|
|
}
|
2006-02-26 15:00:58 +00:00
|
|
|
return true;
|
2005-10-08 21:57:39 +01:00
|
|
|
}
|
|
|
|
|
2004-08-17 16:52:52 +01:00
|
|
|
//! Prune invalid state w.r.t. <=C minimal requirement
|
2004-08-15 12:55:22 +01:00
|
|
|
/**
|
2004-08-17 16:52:52 +01:00
|
|
|
* Intuition says this can be done a lot more efficient. Luckily this is the prototype.
|
|
|
|
*
|
|
|
|
*@returns True, if it's okay. If false, it needs to be pruned.
|
2004-08-15 12:55:22 +01:00
|
|
|
*/
|
2004-08-18 15:06:14 +01:00
|
|
|
int
|
|
|
|
bindings_c_minimal ()
|
2004-08-15 12:55:22 +01:00
|
|
|
{
|
2006-02-26 15:00:58 +00:00
|
|
|
return unique_origination ();
|
2004-08-15 12:55:22 +01:00
|
|
|
}
|