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

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

Small corrections to manual.

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

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