2007-06-11 13:01:04 +01:00
|
|
|
/*
|
|
|
|
* Scyther : An automatic verifier for security protocols.
|
2013-10-05 23:56:12 +01:00
|
|
|
* Copyright (C) 2007-2013 Cas Cremers
|
2007-06-11 13:01:04 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
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 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"
|
2007-01-06 14:45:29 +00:00
|
|
|
#include "error.h"
|
2007-05-18 13:06:29 +01:00
|
|
|
#include "mymalloc.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;
|
|
|
|
|
2006-03-08 13:58:46 +00:00
|
|
|
b = malloc (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
|
|
|
}
|
2006-03-08 13:58:46 +00:00
|
|
|
free (b);
|
2004-08-15 12:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* 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;
|
2006-03-05 15:18:39 +00:00
|
|
|
if (switches.output == PROOF)
|
|
|
|
{
|
|
|
|
indentPrint ();
|
|
|
|
binding_print (b);
|
|
|
|
eprintf ("\n");
|
|
|
|
}
|
2006-02-26 15:00:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
2006-03-15 08:51:08 +00:00
|
|
|
{
|
|
|
|
globalError++;
|
|
|
|
roledefPrint (eventRoledef (sys, run, ev));
|
|
|
|
eprintf ("\n");
|
|
|
|
globalError--;
|
|
|
|
error ("Trying to add an emtpy goal term to r%ii%i, with level %i.",
|
|
|
|
run, ev, level);
|
|
|
|
}
|
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
|
2006-03-19 12:59:26 +00:00
|
|
|
if (switches.intruder && realTermTuple (term))
|
2004-08-15 15:07:34 +01:00
|
|
|
{
|
2006-03-19 12:59:26 +00:00
|
|
|
// Only split if there is an intruder
|
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
|
2006-02-27 22:27:09 +00:00
|
|
|
int createnew;
|
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;
|
2006-02-27 22:27:09 +00:00
|
|
|
if (isTermEqual (b->term, term))
|
|
|
|
{
|
|
|
|
// binding of same term
|
|
|
|
if (run == b->run_to && ev == b->ev_to)
|
|
|
|
{
|
|
|
|
// identical binding
|
|
|
|
createnew = false;
|
|
|
|
}
|
2004-11-16 12:07:55 +00:00
|
|
|
}
|
2006-02-27 22:27:09 +00:00
|
|
|
return true;
|
2004-11-16 12:07:55 +00:00
|
|
|
}
|
2004-10-18 14:04:34 +01:00
|
|
|
|
2006-02-27 22:27:09 +00:00
|
|
|
createnew = true;
|
|
|
|
list_iterate (sys->bindings, testSame);
|
|
|
|
if (createnew)
|
2004-10-18 14:04:34 +01:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
2012-04-25 08:53:07 +01:00
|
|
|
//! Determine whether some label set is ordered w.r.t. send/recv order.
|
2004-08-27 20:06:15 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2012-04-25 08:53:07 +01:00
|
|
|
// Given this label, and the mapping of runs, we want to know if the order is okay. Thus, we need to know sendrole and recvrole
|
2004-08-27 20:06:15 +01:00
|
|
|
Labelinfo linfo;
|
2012-04-25 08:53:07 +01:00
|
|
|
int send_run, send_ev, recv_run, recv_ev;
|
2004-08-27 20:06:15 +01:00
|
|
|
|
|
|
|
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
|
2012-04-25 08:53:07 +01:00
|
|
|
("Could not locate send or recv for label, after niagree holds, to test for order.");
|
2004-08-27 20:06:15 +01:00
|
|
|
#endif
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
linfo = label_find (sys->labellist, labels->term);
|
2006-04-25 14:58:14 +01:00
|
|
|
if (!linfo->ignore)
|
2004-08-27 20:06:15 +01:00
|
|
|
{
|
2006-04-25 14:58:14 +01:00
|
|
|
send_run = termmapGet (runs, linfo->sendrole);
|
2012-04-25 08:53:07 +01:00
|
|
|
recv_run = termmapGet (runs, linfo->recvrole);
|
2006-04-25 14:58:14 +01:00
|
|
|
send_ev = get_index (send_run);
|
2012-04-25 08:53:07 +01:00
|
|
|
recv_ev = get_index (recv_run);
|
|
|
|
if (!isDependEvent (send_run, send_ev, recv_run, recv_ev))
|
2006-04-25 14:58:14 +01:00
|
|
|
{
|
|
|
|
// Not ordered; false
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-27 20:06:15 +01:00
|
|
|
|
2006-04-25 14:58:14 +01:00
|
|
|
}
|
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
|
|
|
{
|
2006-03-05 15:18:39 +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
|
|
|
}
|
|
|
|
|
2006-03-14 11:37:28 +00:00
|
|
|
//! Iterate over all bindings
|
2006-03-10 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* Iterator should return true to proceed
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iterate_bindings (int (*func) (Binding b))
|
|
|
|
{
|
|
|
|
List bl;
|
|
|
|
|
|
|
|
for (bl = sys->bindings; bl != NULL; bl = bl->next)
|
|
|
|
{
|
|
|
|
Binding b;
|
|
|
|
|
|
|
|
b = (Binding) bl->data;
|
2006-03-14 11:37:28 +00:00
|
|
|
if (!func (b))
|
2006-03-10 14:48:40 +00:00
|
|
|
{
|
2006-03-14 11:37:28 +00:00
|
|
|
return false;
|
2006-03-10 14:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-16 10:55:15 +00:00
|
|
|
//! Determine whether two bindings have the same source and destination
|
|
|
|
int
|
|
|
|
same_binding (const Binding b1, const Binding b2)
|
|
|
|
{
|
|
|
|
if (b1 == b2)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((b1 != NULL) && (b2 != NULL))
|
|
|
|
{
|
|
|
|
if ((b1->done) && (b2->done))
|
|
|
|
{
|
|
|
|
if ((b1->run_to == b2->run_to) && (b1->ev_to == b2->ev_to))
|
|
|
|
{
|
|
|
|
if ((b1->run_from == b2->run_from)
|
|
|
|
&& (b1->ev_from == b2->ev_from))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-10 14:48:40 +00:00
|
|
|
|
|
|
|
//! Iterate over preceding bindings (this does not include stuff bound to the same destination)
|
|
|
|
/**
|
|
|
|
* Iterator should return true to proceed
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iterate_preceding_bindings (const int run, const int ev,
|
|
|
|
int (*func) (Binding b))
|
|
|
|
{
|
|
|
|
int precs (Binding b)
|
|
|
|
{
|
|
|
|
if (isDependEvent (b->run_to, b->ev_to, run, ev))
|
|
|
|
{
|
|
|
|
return func (b);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iterate_bindings (precs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2006-03-19 12:59:26 +00:00
|
|
|
if (switches.intruder)
|
|
|
|
{
|
|
|
|
// For intruder we work with sets here
|
|
|
|
terms2 = tuple_to_termlist (b2->term);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For regular agents we use terms
|
|
|
|
terms2 = termlistAdd (NULL, b2->term);
|
|
|
|
}
|
2005-10-08 21:57:39 +01:00
|
|
|
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.
|
2012-05-01 13:27:38 +01:00
|
|
|
|
|
|
|
// Cleanup of garbage
|
|
|
|
termlistDelete (terms2);
|
|
|
|
termlistDelete (sharedterms);
|
|
|
|
termlistDelete (terms);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-03-10 14:48:40 +00:00
|
|
|
|
2012-05-01 13:46:01 +01:00
|
|
|
//! Check for first-origination points
|
2004-08-15 12:55:22 +01:00
|
|
|
/**
|
2004-08-17 16:52:52 +01:00
|
|
|
*@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
|
2012-05-01 13:46:01 +01:00
|
|
|
first_origination ()
|
2004-08-15 12:55:22 +01:00
|
|
|
{
|
2012-05-01 13:46:01 +01:00
|
|
|
List bl;
|
|
|
|
|
|
|
|
// For all goals
|
|
|
|
bl = sys->bindings;
|
|
|
|
while (bl != NULL)
|
2006-02-27 22:27:09 +00:00
|
|
|
{
|
2012-05-01 13:46:01 +01:00
|
|
|
Binding b;
|
2006-02-27 22:27:09 +00:00
|
|
|
|
2012-05-01 13:46:01 +01:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
int run;
|
2006-02-27 22:27:09 +00:00
|
|
|
|
2012-05-01 13:46:01 +01:00
|
|
|
// Find all preceding events
|
|
|
|
for (run = 0; run < sys->maxruns; run++)
|
|
|
|
{
|
|
|
|
int ev;
|
|
|
|
Roledef rd;
|
2006-02-27 22:27:09 +00:00
|
|
|
|
2012-05-01 13:46:01 +01:00
|
|
|
rd = sys->runs[run].start;
|
2006-02-27 22:27:09 +00:00
|
|
|
|
2012-05-01 13:46:01 +01:00
|
|
|
//!@todo hardcoded reference to step, should be length
|
|
|
|
for (ev = 0; ev < sys->runs[run].step; ev++)
|
|
|
|
{
|
|
|
|
if (rd->type == SEND || rd->type == RECV)
|
|
|
|
{
|
|
|
|
if (isDependEvent (run, ev, b->run_from, b->ev_from))
|
|
|
|
{
|
|
|
|
// this node is *before* the from node
|
|
|
|
|
|
|
|
int occursthere;
|
|
|
|
|
|
|
|
if (switches.intruder)
|
|
|
|
{
|
|
|
|
// intruder: interm bindings should cater for the first occurrence
|
|
|
|
occursthere = termInTerm (rd->message, b->term);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no intruder, then simple test
|
|
|
|
occursthere =
|
|
|
|
isTermEqual (rd->message, b->term);
|
|
|
|
}
|
|
|
|
if (occursthere)
|
|
|
|
{
|
|
|
|
// This term already occurs in a previous node!
|
2008-11-09 14:11:43 +00:00
|
|
|
#ifdef DEBUG
|
2012-05-01 13:46:01 +01:00
|
|
|
if (DEBUGL (4))
|
|
|
|
{
|
|
|
|
// Report this
|
|
|
|
indentPrint ();
|
|
|
|
eprintf ("Binding for ");
|
|
|
|
termPrint (b->term);
|
|
|
|
eprintf
|
2012-05-02 09:15:45 +01:00
|
|
|
(" at r%i i%i is not redundant because it occurred before at r%i i%i in ",
|
2012-05-01 13:46:01 +01:00
|
|
|
b->run_from, b->ev_from, run, ev);
|
|
|
|
termPrint (rd->message);
|
|
|
|
eprintf ("\n");
|
|
|
|
}
|
2006-02-27 22:27:09 +00:00
|
|
|
#endif
|
2012-05-01 13:46:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If this event is not before the target, then the
|
|
|
|
// next in the run certainly is not either (because
|
|
|
|
// that would imply that this one is before it)
|
|
|
|
// Thus, we effectively exit the loop.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rd = rd->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bl = bl->next;
|
|
|
|
}
|
2006-02-27 22:27:09 +00:00
|
|
|
return true;
|
2004-08-15 12:55:22 +01:00
|
|
|
}
|
2006-04-02 13:29:02 +01:00
|
|
|
|
2012-05-01 13:46:01 +01:00
|
|
|
//! Determine if pattern is redundant or not
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
non_redundant ()
|
|
|
|
{
|
|
|
|
return (unique_origination () && first_origination ());
|
|
|
|
}
|
|
|
|
|
2006-04-02 13:29:02 +01:00
|
|
|
//! Count the number of bindings that are done.
|
|
|
|
int
|
|
|
|
countBindingsDone ()
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
|
|
|
|
int countDone (Binding b)
|
|
|
|
{
|
2006-04-03 09:21:52 +01:00
|
|
|
if ((!b->blocked) && b->done)
|
2006-04-02 13:29:02 +01:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
iterate_bindings (countDone);
|
|
|
|
return count;
|
|
|
|
}
|