source: CIVL/doc/manual/part-language.tex@ a82987f

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since a82987f was f4bf184, checked in by Stephen Siegel <siegel@…>, 12 years ago

Fixed svn:ignore in doc section, small updates to manual.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@658 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 47.9 KB
Line 
1\part{Language}
2\label{part:lang}
3
4\chapter{Overview of CIVL-C}
5
6CIVL-C is an extension of a subset of the C11 dialect of C. It
7includes the most commonly-used elements of C, including most of the
8syntax, types, expressions, and statements. Missing are some of the
9more esoteric type qualifiers and much of the standard library. None
10of the C11 language elements dealing with concurrency are included, as
11CIVL-C has its own concurrency primitives.
12
13The keywords in CIVL-C not already in C begin with symbol \cckey.
14This makes them readily identifiable and also prevents any naming
15conflicts with identifiers in C programs. This means that most
16legal C programs will also be legal CIVL-C programs.
17
18One of the most important features of CIVL-C not found in standard C
19is the ability to define functions in any scope. (Standard C allows
20function definitions only in the file scope.) This feature is also
21found in GNU C, the GNU extension of C.
22
23Another central CIVL-C feature is the ability to \emph{spawn}
24functions, i.e., run the function in a new process (thread).
25
26
27
28
29Key concepts: static scope tree, nested functions, dynamic scope tree,
30nested functions, processes, spawning, waiting, types, ...
31
32
33\chapter{Structure of a CIVL-C program}
34
35CIVL-C program may use preprocessor directives as specified in the C
36Standard. A source program is preprocessed, then parsed, resulting
37in a translation unit.
38
39A CIVL-C program begins with the line
40\begin{verbatim}
41#include <civlc.h>
42\end{verbatim}
43which includes the main CIVL-C header file, which declares all the
44types and other CIVL primitives.
45
46A translation unit consists of a sequence of variable declarations,
47function prototypes, function definitions, and \emph{assume}
48statements.
49
50% lexical scopes and the lexical scope tree
51
52% naming scopes (\$scope)
53
54% translation of source to model
55
56% root function, root scope and the main function
57
58% formal parameters to main, return value
59
60
61% write a grammar. Leave out type qualifiers, etc. Keep pointers.
62% Keep simple types? Why not keep all the standard types.
63% How about "symbolic types"? Make all the casts explicit.
64% Look at CIL?
65
66% Describe as subset of C, but leave out:... and add...
67
68% add Set<proc> and use it.
69
70\chapter{Sequential Elements}
71
72In this chapter we describe the main sequential elements of the
73language. For the most part these are the same as in C.
74Primitives dealing with concurrency are introduced in Chapter
75\ref{chap:concurrency}.
76
77\section{Types}
78
79\subsection{Standard types inherited from C}
80
81The boolean type is denoted \verb!_Bool!, as in C. Its values are $0$
82and $1$, which are also denoted by $\ctrue$ and $\cfalse$,
83respectively.
84
85There is one integer type, corresponding to the mathematical integers.
86Currently, all of the C integer types \texttt{int}, \texttt{long},
87\texttt{unsigned\ int}, \texttt{short}, etc., are mapped to the CIVL
88integer type.
89
90There is one real type, corresponding to the mathematical real
91numbers. Currently, all of the C real types \texttt{double},
92\texttt{float}, etc., are mapped to the CIVL real type.
93
94Array types, \texttt{struct} and \texttt{union} types, \texttt{char},
95and pointer types are all exactly as in C.
96
97% \subsection{The heap type $\cheap$ and handles}
98
99% Unlike C, a CIVL-C program does not necessarily have access to a
100% single, global heap. Instead, there is a $\cheap$ type, and heaps may
101% be declared explicitly wherever they are needed. Hence a CIVL-C
102% program may have several heaps, and these may exist in different
103% scopes.
104
105% A heap is declared and created as follows:
106% \begin{verbatim}
107% $heap h = $heap_create();
108% \end{verbatim}
109% The function \verb!$heap_create()! creates a new empty heap in the
110% current scope and returns a \emph{handle} to that heap. A handle is
111% like a pointer: it is a reference to another object. However, a handle
112% is much more restricted than a general pointer. In particular, it
113% cannot be dereferenced (by the \ct{*} operator). The underlying heap
114% object can only be accessed by using a handle to it as an argument to
115% a system function.
116
117% Handles can be used in assignments and passed as arguments to functions.
118% For example, this declaration could follow the one above:
119% \begin{verbatim}
120% $heap h2=h;
121% \end{verbatim}
122% After executing this code, \ct{h2} and \ct{h} will be aliased, i.e., the two
123% handles will refer to the same heap object.
124
125% The heap object exists in the scope in which it is created. In
126% particular, it will disappear when that scope disappears, i.e., when
127% control reaches the right curly brace that defines the end of the
128% scope. At that point, any references into the heap become invalid.
129
130% The following system functions deal with heaps:
131% \begin{verbatim}
132% void* $malloc($heap h, int size);
133% void free(void *p)
134% \end{verbatim}
135% The first function is like C's \texttt{malloc}, except that you
136% specify the heap in which the allocation takes place.
137% This modifies the specified heap and returns a pointer to the new object.
138% The function can only occur in a context in which the type of the object is
139% specified, as in:
140% \begin{verbatim}
141% $heap h;
142% int n = 10;
143% double *p = (double*)$malloc(h, n*sizeof(double));
144% \end{verbatim}
145% The function \ct{free} is exactly the same as in C. Note that
146% \texttt{free} modifies the heap which was used to allocate \texttt{p}.
147
148
149\subsection{The bundle type: $\cbundle$}
150
151CIVL-C includes a type named \cbundle. A bundle is basically a
152sequence of data, wrapped into an atomic package. A bundle is created
153using a function that specifies a region of memory. One can create a
154bundle from an array of integers, and another bundle from an array of
155reals. Both bundles have the same type, \cbundle. They can therfore be
156entered into an array of \cbundle, for example. Hence bundles are
157useful for mixing objects of different (even statically unknown) types
158into a single data structure. Later, the contents of a bundle can be
159extracted with another function that specifies a region of memory into
160which to unpack the bundle; if that memory does not have the right
161type to receive the contents of the bundle, a runtime error is
162generated.
163
164\begin{figure}
165\begin{verbatim}
166/* Creates a bundle from the memory region specified by ptr and size,
167 * copying the data into the new bundle */
168$bundle $bundle_pack(void *ptr, int size);
169
170/* Returns the size (number of bytes) of the bundle */
171int $bundle_size($bundle b);
172
173/* Copies the data out of the bundle into the region specified */
174void $bundle_unpack($bundle bundle, void *ptr);
175\end{verbatim}
176 \caption{The \emph{bundle} abstract data type}
177 \label{fig:bundle}
178\end{figure}
179
180The relevant functions for creating and manipulating bundles
181are given in Figure \ref{fig:bundle}.
182
183\subsection{The \cscope{} type}
184\label{sec:scopetype}
185
186An object of type $\cscope$ is a reference to a dynamic scope. It may
187be thought of as a ``dynamic scope ID, '' but it is not an integer and
188cannot be converted to an integer. Operations defined on scopes are
189discussed in Section \ref{sec:scopeexpr}.
190
191\section{Expressions}
192
193\subsection{Expressions inherited from C}
194
195The following C expressions are included in CIVL:
196\begin{itemize}
197\item \emph{identifier} expressions (\texttt{x})
198\item parenthetical expressions (\verb!(e)!)
199\item numerical \emph{addition} (\verb!a+b!), \emph{subtraction} (\verb!a-b!),
200 \emph{multiplication} (\verb!a*b!), \emph{division} (\verb!a/b!),
201 \emph{unary minus} (\verb!a-b!),
202 \emph{integer division} (\verb!a/b!) and \emph{modulus} (\verb!a%b!),
203 all with their ideal mathematical interpretations
204\item array \emph{index} expressions (\verb!a[e]!) and struct or union
205 \emph{navigation} expressions (\verb!x.f!)
206\item \emph{address-of} (\verb!&e!), pointer \emph{dereference} (\verb!*p!),
207 pointer \emph{addition} (\verb!p+i!) and \emph{subtraction} (\verb!p-q!)
208 expressions
209\item relational expressions (\verb!a==b!, \verb~a!=b~, \verb!a>=b!,
210 \verb!a<=b!, \verb!a<b!, \verb!a>b!)
211\item logical \emph{not} (\verb~!p~), \emph{and} (\verb!p&&q!), and
212 \emph{or} (\verb!p||q!)
213\item \emph{sizeof} a type (\verb!sizeof(t)!) or expression (\verb!sizeof(e)!)
214\item \emph{assignment} expressions (\verb!a=b!, \verb!a+=b!, \verb!a-=b!,
215 \verb!a*=b!, \verb!a/=b!)
216\item function \emph{calls} \verb!f(e1,...,en)!
217\item \emph{conditional} expressions (\verb!b ? e : f!).
218\end{itemize}
219
220Bit-wise operations are not yet supported.
221
222\subsection{Scope expressions}
223\label{sec:scopeexpr}
224
225As mentioned in Section \ref{sec:scopetype}, CIVL-C provides a type
226\cscope. An object of this type is a reference to a dynamic scope.
227Several constants, expressions, and functions dealing with the
228\cscope{} type are also provided.
229
230The $\cscope$ type is like any other object type. It may be used as
231the element type of an array, a field in a structure or union, and so
232on. Expressions of type $\cscope$ may occur on the left or right-hand
233sides of assignments and as arguments in function calls just like any
234other expression. Two different variables of type $\cscope$ may be
235aliased, i.e., they may refer to the same dynamic scope. When a
236dynamic scope leaves the state (because control reached the right
237curly brace which marks the end of the scope, or the process exits),
238any reference to that scope becomes ``undefined,'' and an attempt to
239use that scope will result in a runtime error.
240
241\subsubsection{The constant \chere}
242
243A constant \chere{} exists in every scope. This constant has
244type \cscope{} and refers to the dynamic scope in which it is
245contained. For example,
246\begin{verbatim}
247 { // scope s
248 int *p = (int*)$malloc($here, n*sizeof(int);
249 }
250\end{verbatim}
251allocates an object consisting of $n$ ints in the scope $s$.
252
253\subsubsection{Scope relational operators}
254
255Let $s_1$ and $s_2$ be expressions of type \cscope. The following
256are expressions:
257\begin{itemize}
258\item $s_1$ \ct{==} $s_2$. This is \emph{true} iff $s_1$ and $s_2$
259 refer to the same dynamic scope.
260\item $s_1$ \ct{!=} $s_2$. This is \emph{true} iff $s_1$ and $s_2$
261 refer to different dynamic scopes.
262\item $s_1$ \ct{<=} $s_2$. This is \emph{true} iff $s_1$ is equal to
263 or a descendant of $s_2$, i.e., $s_1$ is equal to or contained in $s_2$.
264\item $s_1$ \ct{<} $s_2$. This is \emph{true} iff $s_1$ is a strict
265 descendant of $s_2$, i.e., $s_1$ is contained in $s_2$ and is not
266 equal to $s_2$.
267\item $s_1$ \ct{>} $s_2$. This is equivalent to $s_2$ \ct{<} $s_1$.
268\item $s_1$ \ct{>=} $s_2$. This is equivalent to $s_2$ \ct{<=} $s_1$.
269\end{itemize}
270
271\subsubsection{Scope parent function $\cscopeparent$}
272
273The system function
274\begin{verbatim}
275 $scope $scope_parent($scope s);
276\end{verbatim}
277returns the parent dynamic scope of the dynamic scope referenced by
278\ct{s}. If \ct{s} is the root dynamic scope, it returns the undefined
279value of type $\cscope$.
280
281\subsubsection{Lowest Common Ancestor: \ct{+}}
282
283The expression $s_1$ \ct{+} $s_2$, where $s_1$ and $s_2$ are
284expressions of type \cscope, evaluates to the lowest common ancestor
285of $s_1$ and $s_2$ in the dynamic scope tree. This is the smallest
286dynamic scope containing both $s_1$ and $s_2$.
287
288\subsubsection{The \cscopeof{} expression}
289
290Given any left-hand-side expression \ct{expr}, the expression
291\begin{verbatim}
292 $scopeof(expr)
293\end{verbatim}
294evaluates to the dynamic scope containing the object specified by
295\ct{expr}.
296
297The following examples illustrates the meaning of the \cscopeof{}
298operator. All of the assertions hold:
299\begin{verbatim}
300{
301 $scope s1 = $here;
302 int x;
303 double a[10];
304
305 {
306 $scope s2 = $here;
307 int *p = &x;
308 double *q = &a[4];
309
310 assert($scopeof(x)==s1);
311 assert($scopeof(p)==s2);
312 assert($scopeof(*p)==s1);
313 assert($scopeof(a)==s1);
314 assert($scopeof(a[5])==s1);
315 assert($scopeof(q)==s2);
316 assert($scopeof(*q)==s1);
317 }
318}
319\end{verbatim}
320
321\section{Statements}
322
323The usual C statements are supported:
324\begin{itemize}
325\item \emph{no-op} (\ct{;})
326\item expression statements (\ct{e;})
327\item labeled statements, including \ct{case} and \ct{default} labels
328 (\ct{l: s})
329\item \emph{for} (\ct{for (init; cond; inc) s}), \emph{while}
330 (\ct{while (cond) s}) and \emph{do} (\ct{do s while (cond)})
331 loops
332\item compound statements (\lb \ct{s1;s2;} \ldots \rb)
333\item \texttt{if} and \verb!if! \ldots \verb!else!
334\item \verb!goto!
335\item \verb!switch!
336\item \verb!break!
337\item \verb!continue!
338\end{itemize}
339
340\section{Guards and nondeterminism}
341
342\subsection{Guarded commands: \cwhen}
343
344A guarded command is encoded in CIVL-C using a $\cwhen$ statement:
345\begin{verbatim}
346 $when (expr) stmt;
347\end{verbatim}
348All statements have a guard, either implicit or explicit. For most
349statements, the guard is \ctrue. The \cwhen{} statement allows one to
350attach an explicit guard to a statement.
351
352When \texttt{expr} is \emph{true}, the statement is enabled, otherwise
353it is disabled. A disabled statement is \emph{blocked}---it will not
354be scheduled for execution. When it is enabled, it may execute by
355moving control to the \texttt{stmt} and executing the first atomic
356action in the \texttt{stmt}.
357
358If \texttt{stmt} itself has a non-trivial guard, the guard of the
359\cwhen{} statement is effectively the conjunction of the \texttt{expr}
360and the guard of \texttt{stmt}.
361
362The evaluation of \texttt{expr} and the first atomic action of
363\texttt{stmt} effectively occur as a single atomic action. There is
364no guarantee that execution of \texttt{stmt} will continue atomically
365if it contains more than one atomic action, i.e., other processes may
366be scheduled.
367
368Examples:
369\begin{verbatim}
370 $when (s>0) s--;
371\end{verbatim}
372This will block until \texttt{s} is positive and then decrement
373\texttt{s}. The execution of \texttt{s--} is guaranteed to take place
374in an environment in which \texttt{s} is positive.
375
376\begin{verbatim}
377 $when (s>0) {s--; t++}
378\end{verbatim}
379The execution of \texttt{s--} must happen when \texttt{s>0}, but
380between \texttt{s--} and \texttt{t++}, other processes may execute.
381
382\begin{verbatim}
383 $when (s>0) $when (t>0) x=y*t;
384\end{verbatim}
385This blocks until both \texttt{x} and \texttt{t} are positive then
386executes the assignment in that state. It is equivalent to
387\begin{verbatim}
388 $when (s>0 && t>0) x=y*t;
389\end{verbatim}
390
391\subsection{Nondeterministic selection statement: \cchoose}
392
393A \cchoose{} statement has the form
394\begin{verbatim}
395 $choose {
396 stmt1;
397 stmt2;
398 ...
399 default: stmt
400 }
401\end{verbatim}
402The \texttt{default} clause is optional.
403
404The guards of the statements are evaluated and among those that are
405\emph{true}, one is chosen nondeterministically and executed. If none
406are \emph{true} and the \texttt{default} clause is present, it is
407chosen. The \texttt{default} clause will only be selected if all
408guards are \emph{false}. If no \texttt{default} clause is present and
409all guards are \emph{false}, the statement blocks. Hence the implicit
410guard of the \cchoose{} statement without a \texttt{default} clause is
411the disjunction of the guards of its sub-statements. The implicit
412guard of the \cchoose{} statement with a default clause is
413\emph{true}.
414
415Example: this shows how to encode a ``low-level'' CIVL guarded
416transition system:
417
418\begin{verbatim}
419 l1: $choose {
420 $when (x>0) {x--; goto l2;}
421 $when (x==0) {y=1; goto l3;}
422 default: {z=1; goto l4;}
423 }
424 l2: $choose {
425 ...
426 }
427 l3: $choose {
428 ...
429 }
430\end{verbatim}
431
432
433\subsection{Nondeterministic choice of integer: \cchooseint}
434
435The system function \cchooseint{} has the following signature:
436\begin{verbatim}
437 int $choose_int(int n);
438\end{verbatim}
439This function takes as input a positive integer \texttt{n} and
440nondeterministicaly returns an integer in the range
441$[0,\texttt{n}-1]$.
442
443
444\chapter{Concurrency}
445\label{chap:concurrency}
446
447\section{Process creation and management}
448
449\subsection{The process type: \cproc}
450
451This is a primitive object type and functions like any other primitive
452C type (e.g., \texttt{int}). An object of this type refers to a
453process. It can be thought of as a process ID, but it is not an
454integer and cannot be cast to one. It is analogous to the $\cscope$
455type for dynamic scopes.
456
457Certain expressions take an argument of \cproc{} type and some return
458something of \cproc{} type.
459
460\subsection{The \emph{self} process constant: \cself}
461
462This is a constant of type \cproc. It can be used wherever an argument
463of type \cproc{} is called for. It refers to the process that is
464evaluating the expression containing \cself.
465
466\subsection{Spawning a new process: \cspawn}
467
468A \emph{spawn} expression is an expression with side-effects. It
469spawns a new process and returns a reference to the new process, i.e.,
470an object of type \cproc. The syntax is the same as a procedure
471invocation with the keyword \cspawn{} inserted in front:
472\begin{verbatim}
473 $spawn f(expr1, ..., exprn)
474\end{verbatim}
475Typically the returned value is assigned to a variable, e.g.,
476\begin{verbatim}
477 $proc p = $spawn f(i);
478\end{verbatim}
479If the invoked function \texttt{f} returns a value, that value is
480simply ignored.
481
482\subsection{Waiting for another process to terminate: \cwait}
483
484The system function $\cwait$ has signature
485\begin{verbatim}
486 void $wait($proc p);
487\end{verbatim}
488When invoked, this function will not return until the process
489referenced by \ct{p} has terminated. Note that $p$ can be any
490expression of type \cproc{}, not just a variable.
491
492\subsection{Terminating a process immediately: \cexit}
493
494This function takes no arguments. It causes the
495calling process to terminate immediately, regardless of the state of
496its call stack:
497\begin{verbatim}
498 void $exit(void);
499\end{verbatim}
500
501\section{Dynamic scopes, revisited}
502
503\section{Atomicity}
504
505\subsection{Atom blocks: \catom} This defines a number of statements to be executed
506as a single atomic transition. An \catom~block has the following
507form:
508\begin{verbatim}
509 $atom {
510 stmt1;
511 stmt2;
512 ...
513 }
514\end{verbatim}
515
516The statements inside an \catom\ block are to be executed as one
517transition. It is required that the execution of the statements in an
518\catom\ block satisfy all of the following properties:
519\begin{enumerate}
520\item \emph{deterministic}: at each step in the execution of the atom
521 block, there must be at most one enabled statement;
522\item \emph{nonblocking}: at each step in the execution, there must be
523 at least one enabled statement, hence, together with (1), there must
524 be exactly one enabled statement;
525\item \emph{finite}: the execution of the atom block must terminate
526 after a finite number of steps; and
527\item \emph{isolated}: there are no jumps from outside the atom block
528 to inside the atom block, or from inside the atomc block to outside
529 of it.
530\end{enumerate}
531
532Violations of the \emph{deterministic}, \emph{nonblocking}, or
533\emph{isolated} properties will be reported either statically or
534dynamically. If the \emph{finite} property is violated, the
535verification may just run forever.
536
537Once the process enters an \catom\ block is said to be \emph{executing
538 atomly}. The process remains executing atomly until it reaches the
539terminating right brace of the block. Hence \emph{executing atomly}
540is a dynamic, not static condition. For example, the block might
541contain a function call which takes the process to a point in code
542which is not statically contained in an atom block; that process is
543nevertheless still executing atomly and is subject to the rules above.
544The process only stops executing atomly when that function call
545returns and control finally reaches the right curly brace at the end
546of the atom block (assuming the block is not contained in another atom
547block).
548
549\emph{Note:} \cwait\ statements are not allowed in \catom\ blocks.
550The rationale for this is that there is never a way to know for
551certain that another process has terminated (until \cwait\ has
552returned) so there is never a way to be certain the \cwait\ statement
553will not block. If one does occur in an \catom\ block, an error will
554be reported statically (if it can be detected statically) or
555dynamically (otherwise). Note that it is not always possible to
556detect this statically because the \catom\ block may contain a
557function call, and the function may contain the \cwait\ statement.
558
559\subsection{Atomic blocks: \catomic} The statements in an \emph{atomic} block
560will be executed without other processes interleaving, to
561the extent possible. It has the form:
562\begin{verbatim}
563 $atomic {
564 stmt1;
565 stmt2;
566 ...
567 }
568\end{verbatim}
569It is essentially a weaker form of \catom. Unlike \catom, there are
570no restrictions on the statements that can go inside an \catomic\
571block. A process executing an \catomic~block will try to execute the
572statements without interleaving with other processes, unless it
573becomes blocked. Unlike an \catom, the statements in an atomic block
574do not necessarily execute as a single transition; they may be spread
575out over multiple transitions.
576
577When no statement is enabled, the execution of the \catomic\ block
578will be interrupted. At this point, other processes are allowed to
579execute. Eventually, if the original process becomes enabled due to
580the actions of other processes, it may be scheduled again, in which
581case it regains atomicity and continues where it left off. For
582example, after executing the first loop, the process executing the
583following code will become blocked at the first \cwait\ statement:
584 \begin{verbatim}
585$atomic{
586 for(int i = 0; i < 5; i++) p[i] = $spawn foo(i);
587 for(int i = 0; i < 5; i++) $wait p[i];
588}
589\end{verbatim}
590Other processes will then execute. Eventually, if the process being
591waited on terminates, the original process becomes enabled and may be
592scheduled, in which case it regain atomicity, increments \texttt{i}
593and proceeds to the next $\cwait$ statement. This is in fact a common
594idiom for spawning and waiting on a set of processes.
595
596A process that enters an $\catomic$ block is said to be
597\emph{executing atomically}; it remains executing atomically until it
598reaches the closing curly brace.
599
600Both $\catom$ and $\catomic$ blocks can be nested arbitrarily, but
601$\catom$ overrides $\catomic$: a process that is executing atomly will
602continue executing atomly if it encounters an $\catomic$ statement;
603but a process executing atomically that encounters an $\catom$ will
604begin executing atomly.
605
606The atomic semantics are defined more precisely as follows: there is a
607single global variable called the \emph{atomic lock}. This variable
608can either be null (meaning the atomic lock is ``free''), or it can
609hold the PID of a process; that process is said to ``hold'' the atomic
610lock. Moreover, each process contains a special integer variable, its
611\emph{atomic counter}, which is initially 0. Every time a process
612enters an atomic block, it increments its atomic counter; every time
613it exits an atomic block, it decrements its counter. In order to
614increment its counter from $0$ to $1$, it must first wait for the
615atomic lock to become free, and then take the lock. When it
616decrements its counter from $1$ to $0$, it releases the atomic lock.
617When a process executing atomically becomes blocked, it releases the
618lock (without changing the value of its atomic counter).
619
620
621\section{Message Passing}
622
623CIVL-C provides a number of additional primitives that can be used to
624model message-passing systems. This part of the language is built in
625two layers: the lower layer defines an abstract data type for
626representing messages; the higher layer defines an abstract data type
627of \emph{communicators} for managing sets of messages being
628transferred among some set of processes.
629
630\subsection{Messages: \cmessage}
631
632Messages are similar to bundles, but with some additional
633``meta-data''. The \emph{data} component of the message is the
634``contents'' of the message and is formed and extracted much like a
635bundle. The meta-data consists of an integer identifier for the
636\emph{source} (sender) of the message, an integer identifier for the
637message \emph{destination} (receiver), and integer \emph{tag} which
638can be used by the received to discriminate among messages for
639reception. This is very similar to MPI.
640
641\begin{figure}
642 \begin{small}
643\begin{verbatim}
644/* creates a new message, copying data from the specified buffer */
645$message $message_pack(int source, int dest, int tag, void *data, int size);
646
647/* returns the message source */
648int $message_source($message message);
649
650/* returns the message tag */
651int $message_tag($message message);
652
653/* returns the message destination */
654int $message_dest($message message);
655
656/* returns the message size */
657int $message_size($message message);
658
659/* transfers message data to buf, throwing exception if message
660 * size exceeds specified size */
661void $message_unpack($message message, void *buf, int size);
662\end{verbatim}
663 \end{small}
664 \caption{The \emph{message} abstract data type}
665 \label{fig:message}
666\end{figure}
667
668The functions for creating, and extracting information from, messages
669are given in Figure \ref{fig:message}.
670
671\subsection{Communicators: \cgcomm{} and \ccomm}
672\label{sec:communicators}
673
674CIVL-C defines a \emph{global communicator} type $\cgcomm$ and a
675\emph{local communicator} type $\ccomm$. As in MPI, a communicator is
676an abstraction for a ``communication universe'' that comprises a
677finite, fixed sequence of distinct processes and a collection of
678buffered messages that are being transmitted from one of those
679processes to another.
680
681The global communicator is the shared object that must be declared in
682a scope containing all scopes in which communication in that universe
683will take place. It is created by specifying the number of
684\emph{places} that will comprise the communicator. A place is an
685address to which messages may be sent or where they may be received.
686There is not necessarily a one-to-one correspondence between places and
687processes: many processes can occupy the same place.
688
689Local communicators are created (typically in some child scope of the
690scope in which the global communicator is declared) by specifying the
691gobal communicator to which the local one will be associated and the
692place ID. The local communicator will be used in most of the
693message-passing functions; it may be thought of as an ordered pair
694consisting of a reference to the global communicator and the integer
695place.
696
697Both types ($\cgcomm$ and $\ccomm$) are handle types. When declared
698with a call the corresponding creation function, they create an object
699in the specified scope and return a handle to that object. The object
700can only be accessed through the specified system functions that take
701this handle as an argument.
702
703\begin{figure}
704 \begin{small}
705\begin{verbatim}
706/* Creates a new global communicator object and returns a handle to it.
707 * The global communicator will have size communication places. The
708 * global communicator defines a communication "universe" and encompasses
709 * message buffers and all other components of the state associated to
710 * message-passing. The new object will be allocated in the given scope. */
711$gcomm $gcomm_create($scope s, int size);
712
713/* Creates a new local communicator object and returns a handle to it.
714 * The new communicator will be affiliated with the specified global
715 * communicator. This local communicator handle will be used as an
716 * argument in most message-passing functions. The place must be in
717 * [0,size-1] and specifies the place in the global communication universe
718 * that will be occupied by the local communicator. The local communicator
719 * handle may be used by more than one process, but all of those
720 * processes will be viewed as occupying the same place.
721 * Only one call to $comm_create may occur for each gcomm-place pair.
722 * The new object will be allocated in the given scope. */
723$comm $comm_create($scope s, $gcomm gcomm, int place);
724
725/* Returns the size (number of places) in the global communicator associated
726 * to the given comm. */
727int $comm_size($comm comm);
728
729/* Returns the place of the local communicator. This is the same as the
730 * place argument used to create the local communicator. */
731int $comm_place($comm comm);
732
733/* Adds the message to the appropriate message queue in the communication
734 * universe specified by the comm. The source of the message must equal
735 * the place of the comm. */
736void $comm_enqueue($comm comm, $message message);
737
738/* Returns true iff a matching message exists in the communication universe
739 * specified by the comm. A message matches the arguments if the destination
740 * of the message is the place of the comm, and the sources and tags match. */
741_Bool $comm_probe($comm comm, int source, int tag);
742
743/* Finds the first matching message and returns it without modifying
744 * the communication universe. If no matching message exists, returns a message
745 * with source, dest, and tag all negative. */
746$message $comm_seek($comm comm, int source, int tag);
747
748/* Finds the first matching message, removes it from the communicator,
749 * and returns the message */
750$message $comm_dequeue($comm comm, int source, int tag);
751\end{verbatim}
752 \end{small}
753 \caption{The \emph{communicator} interface specifies handle
754 types $\cgcomm$ and $\ccomm$ and the functions above}
755 \label{fig:comm}
756\end{figure}
757
758The communicator interface is given in Figure \ref{fig:comm}.
759
760Certain restrictions are enforced on some relations between the
761objects involved in a communication universe.
762
763Fix a \cgcomm{} object. This object corresponds to a single
764communication universe with, say, $n$ places. At any time, there can
765be \emph{at most one} \ccomm{} object associated to a given place. If
766a program attempts to create a \ccomm{} object with the same \cgcomm{}
767and place as an earlier created \ccomm{} object, a runtime error will
768occur. In particular, there can be at most $n$ \ccomm{} objects
769associated to the \cgcomm.
770
771The relation between processes and \ccomm{} objects is unconstrained.
772One process may use any number of \ccomm{} objects. (Of course, those
773objects must be visible to the process.) Dually, a single \ccomm{}
774object may be used by any number of processes; this actually comes up
775when modeling a multi-threaded MPI program.
776
777\begin{figure}
778 \begin{small}
779\begin{verbatim}
780$gcomm gcomm = $gcomm_create($here, nprocs);
781void Process(int rank) {
782 $comm comm = $comm_create($here, gcomm, rank);
783
784 void Thread(int tid) {
785 ...$comm_enqueue(comm, msg)...
786 ...$comm_dequeue(comm, source, tag)...
787 }
788
789 for (int i=0; i<nthreads; i++) $spawn Thread(i);
790 ...
791}
792for (int i=0; i<nprocs; i++) $spawn Process(i);
793...
794\end{verbatim}
795 \end{small}
796 \caption{Code skeleton for model of multithreaded MPI program
797 showing placement of global and local communicator objects}
798 \label{fig:mpi-threads-comm}
799\end{figure}
800
801There is no special status given to the process which creates the
802\ccomm{} object of a given place. Any process which can reach that
803\ccomm{} object can use it to send or receive messages, regardless of
804whether that process was the one that created the \ccomm{} object.
805However, users should be aware that verification is likely to be most
806efficient when variables are declared as locally as possible, so it is
807best to declare the \ccomm{} object in the innermost scope possible.
808Figure \ref{fig:mpi-threads-comm} illustrates an effective way
809to do this in the context of modeling a multithreaded MPI program.
810In the code skeleton, each thread can access the local communicator
811object of its process, but not that of any other process.
812
813
814
815% nonblocking interface: instead of message_pack, it is a request
816% form a new request and Isend it
817% can it be re-used?
818% buffer_create,
819% request create,
820% match, complete,
821
822\chapter{Specification}
823
824\section{Overview}
825
826Specification is the means by which one expresses what a program is
827supposed to do, i.e., what it means for it to be correct.
828
829There are several specification mechanisms in CIVL-C. First, there are
830the default properties: these are generic properties which are checked
831by default in any program, and require no additional specification
832effort. These properties include absence of deadlocks, division by 0,
833illegal pointer dereferences, and out of bounds array indexes.
834
835Many more program-specific properties can be specified using
836assertions. CIVL-C has a rich assertion language which extends the
837language of boolean-valued C expressions. Assumptions are a
838specification dual to assertions in that they restrict the set
839of executions on which the assertions are checked.
840
841Functional equivalence is a power specification mechanism. In this
842approach, two programs are provided, one playing the role of the
843specification, the other the role of the implementation. The
844implementation is correct if, for all inputs $x$, it produces the same
845output as that produced by the specification on input $x$. In other
846words, the two programs define the same function; this is sometimes
847known as \emph{input-output equivalence}. In order to take this
848approach, one must first have a way to specify what the inputs and
849outputs of a programs are; CIVL-C provides special keywords for this.
850
851Procedure contracts are another powerful specification mechanisms.
852These typically involve specifying preconditions and postconditions
853for a function. The function is correct if, whenever it is called in a
854state satisfying the precondition, when it returns the state will
855satsify the postcondition. A program is correct if all its functions
856satsify their contract.
857
858\section{Input-output signature}
859
860\subsection{Input type qualifier: \cinput}
861
862The declaration of a variable in the root scope may
863include the type qualifier \cinput, e.g.,
864\begin{verbatim}
865 $input int n;
866\end{verbatim}
867This declares the variable to be an input variable, i.e., one which is
868considered to be an input to the program. Such a variable is
869initialized with an arbitrary (unconstrained) value of its type. When
870using symbolic execution to verify a program, such a variable will be
871assigned a unique symbolic constant of its type.
872
873In contrast, variables in the root scope which are not input variables
874will instead be initialized with the ``undefined'' value. If an
875undefined value is used in some way (such as in an argument to an
876operator), an error occurs.
877
878In addition, input variables may only be read, never written to.
879
880Alternatively, it is also possible to specify a particular concrete
881initial value for an input variable. This is done using a command
882line argument when verifying or running the program.
883
884Input (and output) variables also play a key role when determining
885whether two programs are functionally equivalent. Two programs are
886considered functionally equivalent if, whenever they are given the
887same inputs (i.e., corresponding \cinput{} variables are initialized
888with the same values) they will produce the same outputs (i.e.,
889corresponding \coutput{} variables will end up with the same values at
890termination).
891
892\subsection{Output type qualifier: \coutput}
893
894A variable in the root scope may be declared with this type qualifier
895to declare it to be an output variable. Output variables are ``dual''
896to input variables. They may only be written to, never read. They
897are used primarily in functional equivalence checking.
898
899\section{Assertions and assumptions}
900
901\subsection{Assertions: \cassert}
902
903The system function \cassert{} takes an argument of boolean type:
904\begin{verbatim}
905 void $assert (_Bool expr);
906\end{verbatim}
907During verification, the assertion is checked. If it cannot be proved
908that it must hold, a violation is reported.
909
910Note that CIVL-C boolean expressions have a richer syntax than C
911expressions, and may include universal or existential quantifiers
912(see below), and the boolean values \ctrue{} and \cfalse{}.
913
914The assertion function may take additional optional arguments used to
915print a specific message if the assertion is violated. These
916additional arguments are similar in form to those used in C's
917\texttt{printf} statement: a format string, followed by some number of
918arguments which are evaluated and substituted for successive codes in
919the format string. For example,
920\begin{verbatim}
921 $assert(x<=B, "x-coordinate %f exceeds bound %f", x, B);
922\end{verbatim}
923
924The C function \texttt{assert}, included in the standard library
925\texttt{assert.h}, is identical to \cassert. Programmers are
926free to use either one.
927
928
929\subsection{Assume statements: \cassume}
930
931As \emph{assume statement} has the form
932\begin{verbatim}
933 $assume expr;
934\end{verbatim}
935During verification, the assumed expression is assumed to hold. If
936this leads to a contradiction on some execution, that execution is
937simply ignored. It never reports a violation, it only restricts the
938set of possible executions that will be explored by the verification
939algorithm.
940
941Like as assertion statement, as assume statement can be used any place
942a statement is expected. In addition, as assume statement can be used
943in file scope to place restrictions on the global variables of the
944programs. For example,
945\begin{verbatim}
946$input int B;
947$input int N;
948$assume 0<=N && N<=B;
949\end{verbatim}
950declares \texttt{N} and \texttt{B} to be integer inputs and restricts
951consideration to inputs satisfying $0\leq\texttt{N}\leq\texttt{B}$.
952
953
954\section{Formulas}
955
956A formula is a boolean expression that can be used in an assert
957statement, assume statement, procedure contract (below), or invariant.
958Any ordinary C boolean expression is a formula. CIVL-C provides some
959additional kinds of formulas, described below.
960
961\subsection{Implication: \cimplies}
962
963The binary operation \cimplies{} represents logical implication.
964The expression \verb!p=>q! is equivalent to \verb~(!p)||q~.
965
966\subsection{Universal quantifier: \cforall}
967
968The universally qunatified formula has the form
969\begin{verbatim}
970 $forall { type identifier | restriction} expr
971\end{verbatim}
972where \verb!type! is a type name (e.g., \texttt{int} or
973\texttt{double}), \verb!identifier! is the name of the bound variable,
974\verb!restriction! is a boolean expression which expresses some
975restriction on the values that the bound variable can take, and
976\verb!expr! is a formula. The universally quantified formula
977holds iff for all values assignable to the bound variable
978for which the restriction holds, the formula \ct{expr} holds.
979
980A variation on the construct above can be used in the special case
981where the bound variable is to range over a finite interval
982of integers. In this case the quantified formula may be written:
983\begin{verbatim}
984 $forall { type identifier=lower .. upper } expr
985\end{verbatim}
986where \ct{lower} and \ct{upper} are integer expressions.
987
988\subsection{Existential quantifier: \cexists}
989
990The syntax for existentially quantified expressions is exactly the
991same as for universally quantified expressions, with \cexists{} in
992place of \cforall{}.
993
994\section{Contracts}
995
996\subsection{Procedure contracts: \crequires{} and \censures{}}
997The \crequires{} and \censures{} primitives are used to encode
998procedure contracts. There are optional
999elements that may occur in a procedure declaration or definition,
1000as follows. For a function prototype:
1001\begin{verbatim}
1002 T f(...)
1003 $requires expr;
1004 $ensures expr;
1005 ;
1006\end{verbatim}
1007For a function definition:
1008\begin{verbatim}
1009 T f(...)
1010 $requires expr;
1011 $ensures expr;
1012 {
1013 ...
1014 }
1015\end{verbatim}
1016The value \cresult{} may be used in post-conditions to refer
1017to the result returned by a procedure.
1018
1019\emph{Status}: parsed, but nothing is currently done with this
1020information.
1021
1022\subsection{Loop invariants: \cinvariant}
1023
1024This indicates a loop invariant. Each C loop
1025construct has an optional invariant clause as follows:
1026\begin{verbatim}
1027 while (expr) $invariant (expr) stmt
1028 for (e1; e2; e3) $invariant (expr) stmt
1029 do stmt while (expr) $invariant (expr) ;
1030\end{verbatim}
1031The invariant encodes the claim that if \texttt{expr} holds upon
1032entering the loop and the loop condition holds, then it will hold
1033after completion of execution of the loop body. The invariant is used
1034by certain verification techniques.
1035
1036\emph{Status:} parsed, but nothing is currently done with this
1037information.
1038
1039\section{Concurrency specification}
1040
1041\subsection{Remote expressions: \texttt{e@x}}.
1042
1043These have the form \verb!expr@x! and refer to a variable in another
1044process, e.g., \verb!procs[i]@x!. This special kind of expression is
1045used in collective expressions, which are used to formulate collective
1046assertions and invariants.
1047
1048The expression \verb!expr! must have \cproc{} type. The variable
1049\texttt{x} must be a statically visible variable in the context in
1050which it is occurs. When this expression is evaluated, the evaluation
1051context will be shifted to the process referred to by \texttt{expr}.
1052
1053\emph{Status}: not implemented.
1054
1055\subsection{Collective expressions: \ccollective}. These have the form
1056\begin{verbatim}
1057 $collective(proc_expr, int_expr) expr
1058\end{verbatim}
1059This is a collective expression over a set of processes. The
1060expression \texttt{proc{\U}expr} yields a pointer to the first element
1061of an array of \cproc. The expression \texttt{int{\U}expr} gives the
1062length of that array, i.e., the number of processes. Expression
1063\texttt{expr} is a boolean-valued expression; it may use remote
1064expressions to refer to variables in the processes specified in the
1065array. Example:
1066\begin{verbatim}
1067 $proc procs[N];
1068 ...
1069 $assert $collective(procs, N) i==procs[(pid+1)%N]@i ;
1070\end{verbatim}
1071
1072\emph{Status}: not implemented.
1073
1074\chapter{Pointers and Heaps}
1075\label{chap:pointers}
1076
1077CIVL-C supports pointers, using the same operators with the same
1078meanings as C (\texttt{\&}, \texttt{*}, pointer arithmetic). There is
1079also a heap in every scope, and system functions to allocate and
1080deallocate objects in the specified scope.
1081
1082\section{Memory functions: \texttt{memcpy}}
1083
1084The function \texttt{memcpy} is defined in the standard C library
1085\texttt{string.h} and works exactly the same in CIVL-C: it copies
1086data from the region pointed to by \ct{q} to that pointed to by
1087\ct{p}. The signature is
1088
1089\begin{verbatim}
1090 void memcpy(void *p, void *q, size_t size);
1091\end{verbatim}
1092
1093\section{Heaps, \cmalloc{} and \texttt{free}}
1094
1095As mentioned above, each dynamic scope has an implicit heap on which
1096objects can be allocated and deallocated dynamically. To allocate an
1097object, one first needs a reference to the dynamic scope to be used.
1098The system function $\cmalloc$ is like C's \texttt{malloc}, but takes
1099this extra scope argument:
1100\begin{verbatim}
1101 void * $malloc($scope scope, int size);
1102\end{verbatim}
1103The standard C function
1104\begin{verbatim}
1105 void * malloc(int size);
1106\end{verbatim}
1107is equivalent to \verb!$malloc($root, size)!.
1108
1109The system function \ct{free} is used to deallocate a heap object;
1110it is just like C's \texttt{free}:
1111\begin{verbatim}
1112 void free(void *p);
1113\end{verbatim}
1114
1115% \section{Pointer types}
1116
1117% Given any object type $T$ and a static scope $s$ in a CIVL-C program,
1118% there is a type \emph{pointer-to-$T$-in-$s$}. The type is used to
1119% represent a pointer to a memory location of type $T$ in scope $s$ or a
1120% descendant of $s$ (i.e., some scope contained in $s$).
1121
1122% If scope $s_1$ is a descendant of $s_2$ (i.e., $s_1$ is lexically
1123% contained in $s_2$), the type \emph{pointer-to-$T$-in-$s_1$} is a
1124% subtype of \emph{pointer-to-$T$-in-$s_2$}. This means that any
1125% expression of the first type can be used wherever an object of the
1126% second type is expected. In particular, any expression $e$ of the
1127% subtype can be assigned to a left-hand-side expression of the
1128% supertype without explicit casts; also $e$ can be used as an argument
1129% to a function for which the corresponding parameter has the supertype.
1130
1131% The syntax for denoting this type adheres to the usual C syntax for
1132% denoting the type \emph{pointer-to-$T$} with the addition of a scope
1133% parameter within angular brackets immediately following the \texttt{*}
1134% token. For example, to declare a variable \texttt{p} of type
1135% \emph{pointer-to-$T$-in-$s$}, one writes
1136% \begin{verbatim}
1137% int *<s> p;
1138% \end{verbatim}
1139% If the scope modifier \texttt{<...>} is absent, the scope is taken to
1140% be the root scope $s_0$. The object has type
1141% \emph{pointer-to-$T$-in-$s_0$}, which is abreviated as
1142% \emph{pointer-to-$T$}. In this way, stanard C programs can be
1143% interpreted as CIVL-C programs.
1144
1145% \section{Address-of operator}
1146
1147% The address-of operator \texttt{\&} returns a pointer of the
1148% appropriate subtype using the innermost scope in which its left-hand-side
1149% argument is declared. For example
1150
1151% \begin{verbatim}
1152% {
1153% $scope s1 = $here();
1154% int x;
1155% double a[N];
1156% int *<s1> p = &x;
1157% double *<s1> q = &a[2];
1158% }
1159% \end{verbatim}
1160% is correct (in particular, it is type-correct) because \texttt{\&x}
1161% has type \emph{pointer-to-\texttt{int}-in-\texttt{s1}}, since
1162% \texttt{s1} is the scope in which \texttt{x} is declared.
1163
1164% Another pointer example:
1165% \begin{small}
1166% \begin{verbatim}
1167% { $scope s0 = $here();
1168% { $scope s1 = $here();
1169% double x;
1170% { $scope s2 = $here();
1171% double y;
1172% double *<s1> p;
1173% /* p can only point to something in s1 or descendant, for example, s2 */
1174% p = &x; // fine
1175% p = &y; // fine
1176% p = (double*)$malloc(s0, 10*sizeof(double)); // static type error
1177% }
1178% }
1179% }
1180% \end{verbatim}
1181% \end{small}
1182
1183% \section{Pointer addition and subtractions}
1184
1185% If \texttt{e} is an expression of type \emph{pointer-to-$T$-in-$s$}
1186% and \texttt{i} is an expression of integer type then \texttt{e+i} also
1187% has type \emph{pointer-to-$T$-in-$s$}. In other words, pointer
1188% addition cannot leave the scope of the original pointer. This
1189% reflects the fact that every object is contained in one scope, and
1190% pointer addition cannot leave the object.
1191
1192% Pointer subtraction is defined on two pointers of the same type, where
1193% ``same'' includes the scope. That is checked statically. As in C, it
1194% is only defined if the two pointers point to the same object. In
1195% CIVL-C, a runtime error will be thrown if they do not point to the
1196% same object.
1197
1198% \section{Semantics of scopes and pointer types}
1199
1200% A variable of type \cscope{} is treated like any other variable.
1201% It becomes part of the state when the scope in which it is declared
1202% is instantiated to form a dynamic scope. The variable is
1203% initialized at that time and its value cannot change.
1204
1205% Each time a dynamic scope is instantiated, it is assigned a unique ID
1206% number. The exactly value of the ID number is not relevant, it just
1207% has to be distince from any other scope ID number that currently
1208% exists in the state. This is the value that is assigned to the scope
1209% variable. Therefore, if a static scope contains a scope variable, and
1210% that scope is instantiated twice to form two distinct dynamic scopes,
1211% the values assigned to the two variables will be distinct.
1212
1213% A pointer value is an ordered pair $\langle \delta,r \rangle$, where
1214% $\delta$ is a dynamic scope ID and $r$ is a reference to a memory
1215% location in the static scope associated to $\delta$. (We will define
1216% the exact form of a reference later.)
1217
1218% When a dynamic scope is instantiated, each new variable created is
1219% assigned a \emph{dynamic type}. This is a refinement of the static
1220% type associated to the static variable. Every dynamic type
1221% is an instance of exactly one static type. The dynamic
1222% type of the newly instantiated variable is an instance of the
1223% static type of the static variable.
1224
1225% The dynamic pointer types have the form
1226% \emph{pointer-to-$t$-in-$\delta$}, where $t$ is a dynamic type and
1227% $\delta$ is a dynamic scope ID. For a program to be dynamically type
1228% safe, such a variable should hold only values of the form $\langle
1229% \delta, r\rangle$. In particular, the variable should never be
1230% assigned a value where the dynamic scope component is a different
1231% instance of the static scope $s$ associated to $\delta$.
1232
1233% \section{Pointer casts}
1234
1235% If scope $s_1$ is contained in scope $s_2$, an expression of type
1236% \emph{pointer-to-$T$-in-$s_1$} can always be cast to
1237% \emph{pointer-to-$T$-in-$s_2$},
1238% because the first is a subtype of the second. (As described above,
1239% the cast is unnecessary.)
1240
1241% The cast in the other direction is also allowed, but the dynamic type
1242% safety of that cast will only be checked at runtime. In particular, a
1243% runtime error will result if the cast attempts to cast the pointer
1244% value to a dynamic scope which does not contain (is an ancestor of)
1245% the dynamic scope component of the pointer value.
1246
1247% A type \emph{pointer-to-$T_1$-in-$s$} can be cast to a type
1248% \emph{pointer-to-$T_2$-in-$s$} according to the usual rules of C. In
1249% other words, usual casting rules apply as long as you don't change the
1250% scope.
1251
1252% \section{Scope-Parameterized Functions}
1253
1254% Coming soon. (Parsed, type checked, not currently used otherwise.)
1255
1256% \section{Scope-Parameterized Type Definitions}
1257
1258% Coming soon. (Ditto.)
1259
1260\chapter{Libraries}
1261
1262Each of the following libraries is at least partially implemented and can
1263be included in a CIVL-C program:
1264\begin{itemize}
1265\item \ct{stdlib}
1266\item \ct{stdbool}
1267\item \ct{stdio}
1268\item \ct{assert}
1269\end{itemize}
Note: See TracBrowser for help on using the repository browser.