# source:Papers/jar-cerco-2017/cerco.tex@3609

Last change on this file since 3609 was 3609, checked in by boender, 3 years ago

Prettified author list

File size: 29.0 KB
RevLine
[3599]1\begin{filecontents*}{example.eps}
3%%BoundingBox: 19 19 221 221
4%%CreationDate: Mon Sep 29 1997
5%%Creator: programmed by hand (JK)
7gsave
8newpath
9  20 20 moveto
10  20 220 lineto
11  220 220 lineto
12  220 20 lineto
13closepath
142 setlinewidth
15gsave
16  .4 setgray fill
17grestore
18stroke
19grestore
20\end{filecontents*}
[3600]21
[3599]22\RequirePackage{fix-cm}
23
[3600]24\documentclass[smallextended]{svjour3}
[3599]25
[3600]26\usepackage{amsfonts}
27\usepackage{amsmath}
[3603]28\usepackage{amssymb}
[3600]29\usepackage[british]{babel}
[3603]30\usepackage{color}
31\usepackage{fancybox}
32\usepackage{fancyvrb}
[3600]33\usepackage{graphicx}
35            bookmarks,bookmarksopen,bookmarksdepth=2]{hyperref}
36\usepackage{hyphenat}
37\usepackage[utf8x]{inputenc}
38\usepackage{listings}
39\usepackage{mdwlist}
[3600]40\usepackage{microtype}
[3603]41\usepackage{stmaryrd}
42\usepackage{url}
[3599]43
[3603]44\usepackage{tikz}
45\usetikzlibrary{positioning,calc,patterns,chains,shapes.geometric,scopes}
46
[3604]47\lstset{language=C,basicstyle=\tt,basewidth=.5em,lineskip=-1.5pt}
48
49\newlength{\mylength}
50\newenvironment{frametxt}%
51        {\setlength{\fboxsep}{5pt}
52                \setlength{\mylength}{\linewidth}%
55                \Sbox
56                \minipage{\mylength}%
57                        \setlength{\abovedisplayskip}{0pt}%
58                        \setlength{\belowdisplayskip}{0pt}%
59                }%
60                {\endminipage\endSbox
61                        $\fbox{\TheSbox}$}
62
[3600]63\smartqed
[3599]64
[3608]65% PROPOSED PAPER STRUCTURE
66
67% Introduction
68%   Problem being solved, background, etc.
69%   Current state of the art (and problem with it)
70%   The CerCo approach' (tm)
71%   Brief Matita overview
72%   Map of paper
73
74% CerCo compiler architecture
75%   Description of languages
76%   Target hardware description
77
78% Compiler proof
79%   Structure of proof, and high-level discussion
80%   Technical devices: structured traces, labelling, etc.
81%   Assembler proof
82%   Technical issues in front end (Brian?)
83%   Main theorem statement
84
85% FramaC plugin
86%   Purpose
87%   Description of architecture
88%   Case study/worked example (crypto example?)
89
90% Formal development
92%   Statistics (number of lines, etc.)
93%   Description of remaining axioms --- try and explain them away/make them sound reasonable
94
95% Conclusions
96%   Summary
97%   Related work
98%   Future work
99
100% Bibliography
101
[3601]102\title{CerCo: Certified Complexity\thanks{The project CerCo acknowledges the
103financial support of the Future and Emerging Technologies (FET) programme within
104the Seventh Framework Programme for Research of the European Commission, under
105FET-Open grant number: 243881}}
[3603]106\subtitle{Verified lifting of concrete complexity annotations through a realistic C compiler}
[3600]107\journalname{Journal of Automated Reasoning}
108\titlerunning{Certified Complexity}
[3609]110\author{Jaap Boender \and Brian Campbell \and Dominic P. Mulligan \and Claudio Sacerdoti~Coen} % who else?
111\authorrunning{Boender, Campbell, Mulligan, and Sacerdoti~Coen}
[3600]112\institute{Jaap Boender \at
[3602]113              Faculty of Science and Technology,\\
114                                                        Middlesex University London,\\
115                                                        United Kingdom.\\
[3601]116              \email{J.Boender@mdx.ac.uk}
[3599]117           \and
[3600]118           Brian Campbell \at
[3602]119              Department of Informatics,\\
120              University of Edinburgh,\\
121              United Kingdom.\\
122              \email{Brian.Campbell@ed.ac.uk}
[3600]123           \and
124           Dominic P. Mulligan \at
[3602]125             Computer Laboratory,\\
126             University of Cambridge, \\
127             United Kingdom.\\
[3609]128             \email{Dominic.Mulligan@cl.cam.ac.uk}
[3600]129           \and
[3609]130           Claudio Sacerdoti~Coen \at
[3602]131              Dipartimento di Informatica---Scienza e Ingegneria (DISI),\\
132              University of Bologna,\\
133              Italy.\\
134              \email{Claudio.SacerdotiCoen@unibo.it}}
[3599]135
[3600]136\begin{document}
[3599]137
138\maketitle
139
140\begin{abstract}
[3603]141We provide an overview of the FET-Open Project CerCo (Certified Complexity').
142Our main achievement is the development of a technique for analysing non-functional properties of programs (time, space) at the source level with little or no loss of accuracy and a small trusted code base.
143The core component is a C compiler, verified in the Matita theorem prover, that produces an instrumented copy of the source code in addition to generating object code.
144This instrumentation exposes, and tracks precisely, the actual (non-asymptotic) computational cost of the input program at the source level.
145Untrusted invariant generators and trusted theorem provers may then be used to compute and certify the parametric execution time of the code.
[3600]146\keywords{Verified compilation \and Complexity analysis \and CerCo (`Certified Complexity')}
[3599]147\end{abstract}
148
[3604]149% ---------------------------------------------------------------------------- %
150% SECTION                                                                      %
151% ---------------------------------------------------------------------------- %
[3607]152
[3604]153\section{Introduction}
[3607]154\label{sect.introduction}
[3604]155
156%\paragraph{Problem statement.}
157Programs can be specified with both
158functional constraints (what the program must do) and non-functional constraints (what time, space or other resources the program may use).  In the current
159state of the art, functional properties are verified
160by combining user annotations---preconditions, invariants, and so on---with a
161multitude of automated analyses---invariant generators, type systems, abstract
162interpretation, theorem proving, and so on---on the program's high-level source code.
163By contrast, many non-functional properties
164are verified using analyses on low-level object code,
165%\footnote{A notable
166%  exception is the explicit allocation of heap space in languages like C, which
167%  can be handled at the source level.}
168but these analyses may then need information
169about the high-level functional behaviour of the program that must then be reconstructed.
170This analysis on low-level object code has several problems:
171\begin{itemize*}
172\item
173It can be hard to deduce the high-level structure of the program after compiler optimisations.
174The object code produced by an optimising compiler may have radically different control flow to the original source code program.
175\item
176Techniques that operate on object code are not useful early in the development process of a program, yet problems with a program's design or implementation are cheaper to resolve earlier in the process, rather than later.
177\item
178Parametric cost analysis is very hard: how can we reflect a cost that depends on the execution state, for example the
179value of a register or a carry bit, to a cost that the user can understand
180looking at the source code?
181\item
182Performing functional analyses on object code makes it hard for the programmer to provide information about the program and its expected execution, leading to a loss of precision in the resulting analyses.
183\end{itemize*}
184\paragraph{Vision and approach.}
185We want to reconcile functional and
186non-functional analyses: to share information and perform both at the same time
187on high-level source code.
188%
189What has previously prevented this approach is the lack of a uniform and precise
190cost model for high-level code as each statement occurrence is compiled
191differently, optimisations may change control flow, and the cost of an object
192code instruction may depend on the runtime state of hardware components like
193pipelines and caches, all of which are not visible in the source code.
194
195We envision a new generation of compilers that track program structure through compilation and optimisation and exploit this
196information to define a precise, non-uniform cost model for source code that accounts for runtime state. With such a cost model we can
197reduce non-functional verification to the functional case and exploit the state
198of the art in automated high-level verification~\cite{survey}. The techniques
199currently used by the Worst Case Execution Time (WCET) community, who perform analyses on object code,
200are still available but can be coupled with additional source-level
201analyses. Where our approach produces overly complex cost models, safe approximations can be used to trade complexity with precision.
202Finally, source code analysis can be used early in the development process, when
203components have been specified but not implemented, as modularity means
204that it is enough to specify the non-functional behaviour of missing
205components.
206\paragraph{Contributions.}
207We have developed \emph{the labelling approach}~\cite{labelling}, a
208technique to implement compilers that induce cost models on source programs by
209very lightweight tracking of code changes through compilation. We have studied
210how to formally prove the correctness of compilers implementing this technique, and
211have implemented such a compiler from C to object binaries for the 8051
212microcontroller for predicting execution time and stack space usage,
213verifying it in an interactive theorem prover.  As we are targeting
214an embedded microcontroller we do not consider dynamic memory allocation.
215
216To demonstrate source-level verification of costs we have implemented
217a Frama-C plugin~\cite{framac} that invokes the compiler on a source
218program and uses it to generate invariants on the high-level source
219that correctly model low-level costs. The plugin certifies that the
220program respects these costs by calling automated theorem provers, a
221new and innovative technique in the field of cost analysis. Finally,
222we have conducted several case studies, including showing that the
223plugin can automatically compute and certify the exact reaction time
224of Lustre~\cite{lustre} data flow programs compiled into C.
225
[3607]226% ---------------------------------------------------------------------------- %
227% SECTION                                                                      %
228% ---------------------------------------------------------------------------- %
229
[3604]230\section{Project context and approach}
[3607]231\label{sect.project.context.and.approach}
232
[3604]233Formal methods for verifying functional properties of programs have
234now reached a level of maturity and automation that their adoption is slowly
235increasing in production environments. For safety critical code, it is
236becoming commonplace to combine rigorous software engineering methodologies and testing
237with static analyses, taking the strengths of each and mitigating
238their weaknesses. Of particular interest are open frameworks
239for the combination of different formal methods, where the programs can be
240progressively specified and enriched with new safety
241guarantees: every method contributes knowledge (e.g. new invariants) that
242becomes an assumption for later analysis.
243
244The outlook for verifying non-functional properties of programs (time spent,
245memory used, energy consumed) is bleaker.
246% and the future seems to be getting even worse.
247Most industries verify that real time systems meet their deadlines
248by simply performing many runs of the system and timing their execution,  computing the
249maximum time and adding an empirical safety margin, claiming the result to be a
250bound for the WCET of the program. Formal methods and software to statically
251analyse the WCET of programs exist, but they often produce bounds that are too
252pessimistic to be useful. Recent advancements in hardware architecture
253have been
254focused on the improvement of the average case performance, not the
255predictability of the worst case. Execution time is becoming increasingly
256dependent on execution history and the internal state of
257hardware components like pipelines and caches. Multi-core processors and non-uniform
258memory models are drastically reducing the possibility of performing
259static analysis in isolation, because programs are less and less time
260composable. Clock-precise hardware models are necessary for static analysis, and
261obtaining them is becoming harder due to the increased sophistication
262of hardware design.
263
264Despite these problems, the need for reliable real time
265systems and programs is increasing, and there is pressure
266from the research community for the introduction of
267hardware with more predictable behaviour, which would be more suitable
268for static analysis.  One example, being investigated by the Proartis
269project~\cite{proartis}, is to decouple execution time from execution
270history by introducing randomisation.
271
272In CerCo~\cite{cerco} we do not address this problem, optimistically
273assuming that improvements in low-level timing analysis or architecture will make
274verification feasible in the longer term. Instead, the main objective of our work is
275to bring together the static analysis of functional and non-functional
276properties, which in the current state of the art are
277independent activities with limited exchange of information: while the
278functional properties are verified on the source code, the analysis of
279non-functional properties is performed on object code to exploit
280clock-precise hardware models.
281
282\subsection{Current object-code methods}
283
284Analysis currently takes place on object code for two main reasons.
285First, there cannot be a uniform, precise cost model for source
286code instructions (or even basic blocks). During compilation, high level
287instructions are broken up and reassembled in context-specific ways so that
288identifying a fragment of object code and a single high level instruction is
289infeasible. Even the control flow of the object and source code can be very
290different as a result of optimisations, for example aggressive loop
291optimisations may completely transform source level loops. Despite the lack of a uniform, compilation- and
292program-independent cost model on the source language, the literature on the
293analysis of non-asymptotic execution time on high level languages assuming
294such a model is growing and gaining momentum. However, unless we provide a
295replacement for such cost models, this literature's future practical impact looks
296to be minimal. Some hope has been provided by the EmBounded project \cite{embounded}, which
297compositionally compiles high-level code to a byte code that is executed by an
298interpreter with guarantees on the maximal execution time spent for each byte code
299instruction. This provides a uniform model at the expense of the model's
300precision (each cost is a pessimistic upper bound) and the performance of the
301executed code (because the byte code is interpreted compositionally instead of
302performing a fully non-compositional compilation).
303
304The second reason to perform the analysis on the object code is that bounding
305the worst case execution time of small code fragments in isolation (e.g. loop
306bodies) and then adding up the bounds yields very poor estimates as no
307knowledge of the hardware state prior to executing the fragment can be assumed. By
308analysing longer runs the bound obtained becomes more precise because the lack
309of information about the initial state has a relatively small impact.
310
311To calculate the cost of an execution, value and control flow analyses
312are required to bound the number of times each basic block is
313executed.  Currently, state
314of the art WCET analysis tools, such as AbsInt's aiT toolset~\cite{absint}, perform these analyses on
315object code, where the logic of the program is harder to reconstruct
316and most information available at the source code level has been lost;
317see~\cite{stateart} for a survey. Imprecision in the analysis can lead to useless bounds. To
318augment precision, the tools ask the user to provide constraints on
319the object code control flow, usually in the form of bounds on the
320number of iterations of loops or linear inequalities on them. This
321requires the user to manually link the source and object code,
322translating his assumptions on the source code (which may be wrong) to
323object code constraints. The task is error prone and hard, especially
324in the presence of complex compiler optimisations.
325
326Traditional techniques for WCET that work on object code are also affected by
327another problem: they cannot be applied before the generation of the object
328code. Functional properties can be analysed in early development stages, while
329analysis of non-functional properties may come too late to avoid expensive
330changes to the program architecture.
331
332\subsection{CerCo's approach}
333
334In CerCo we propose a radically new approach to the problem: we reject the idea
335of a uniform cost model and we propose that the compiler, which knows how the
336code is translated, must return the cost model for basic blocks of high level
337instructions. It must do so by keeping track of the control flow modifications
338to reverse them and by interfacing with processor timing analysis.
339
340By embracing compilation, instead of avoiding it like EmBounded did, a CerCo
341compiler can both produce efficient code and return costs that are
342as precise as the processor timing analysis can be. Moreover, our costs can be
343parametric: the cost of a block can depend on actual program data, on a
344summary of the execution history, or on an approximated representation of the
345hardware state. For example, loop optimisations may assign a cost to a loop body
346that is a function of the number of iterations performed. As another example,
347the cost of a block may be a function of the vector of stalled pipeline states,
348which can be exposed in the source code and updated at each basic block exit. It
349is parametricity that allows one to analyse small code fragments without losing
350precision. In the analysis of the code fragment we do not have to ignore the
351initial hardware state, rather, we may assume that we know exactly which
352state (or mode, as the WCET literature calls it) we are in.
353
354The CerCo approach has the potential to dramatically improve the state of the
355art. By performing control and data flow analyses on the source code, the error
356prone translation of invariants is completely avoided. Instead, this
357work is done at the source level using tools of the user's choice.
358Any available technique for the verification of functional properties
359can be immediately reused and multiple techniques can collaborate together to
360infer and certify cost invariants for the program.  There are no
361limitations on the types of loops or data structures involved. Parametric cost analysis
362becomes the default one, with non-parametric bounds used as a last resort when the user
363decides to trade the complexity of the analysis with its precision. \emph{A priori}, no
364technique previously used in traditional WCET is lost: processor
365timing analyses can be used by the compiler on the object code, and the rest can be applied
366at the source code level.
367 Our approach can also work in the early
368stages of development by axiomatically attaching costs to unimplemented components.
369
370
371Software used to verify properties of programs must be as bug free as
372possible. The trusted code base for verification consists of the code that needs
373to be trusted to believe that the property holds. The trusted code base of
374state-of-the-art WCET tools is very large: one needs to trust the control flow
375analyser, the linear programming libraries used, and also the formal models
376of the hardware under analysis, for example. In CerCo we are moving the control flow analysis to the source
377code and we are introducing a non-standard compiler too. To reduce the trusted
378code base, we implemented a prototype and a static analyser in an interactive
379theorem prover, which was used to certify that the costs added to the source
380code are indeed those incurred by the hardware. Formal models of the
381hardware and of the high level source languages were also implemented in the
382interactive theorem prover. Control flow analysis on the source code has been
383obtained using invariant generators, tools to produce proof obligations from
384generated invariants and automatic theorem provers to verify the obligations. If
385these tools are able to generate proof traces that can be
386independently checked, the only remaining component that enters the trusted code
387base is an off-the-shelf invariant generator which, in turn, can be proved
388correct using an interactive theorem prover. Therefore we achieve the double
389objective of allowing the use of more off-the-shelf components (e.g. provers and
390invariant generators) whilst reducing the trusted code base at the same time.
391
392%\paragraph{Summary of the CerCo objectives.} To summarize, the goal of CerCo is
393% to reconcile functional with non-functional analysis by performing them together
394% on the source code, sharing common knowledge about execution invariants. We want
395% to achieve the goal implementing a new generation of compilers that induce a
396% parametric, precise cost model for basic blocks on the source code. The compiler
397% should be certified using an interactive theorem prover to minimize the trusted
398% code base of the analysis. Once the cost model is induced, off-the-shelf tools
399% and techniques can be combined together to infer and prove parametric cost
400% bounds.
401%The long term benefits of the CerCo vision are expected to be:
402%1. the possibility to perform static analysis during early development stages
404%3.  the application of off-the-shelf techniques currently unused for the
405% analysis of non-functional properties, like automated proving and type systems
406%4. simpler and safer interaction with the user, that is still asked for
407% knowledge, but on the source code, with the additional possibility of actually
408% verifying the provided knowledge
409%5. a reduced trusted code base
410%6. the increased accuracy of the bounds themselves.
411%
412%The long term success of the project is hindered by the increased complexity of
413% the static prediction of the non-functional behaviour of modern hardware. In the
414% time frame of the European contribution we have focused on the general
415% methodology and on the difficulties related to the development and certification
416% of a cost model inducing compiler.
417
[3607]418% ---------------------------------------------------------------------------- %
419% SECTION                                                                      %
420% ---------------------------------------------------------------------------- %
421
[3604]422\section{The typical CerCo workflow}
423\label{sec:workflow}
[3607]424
[3604]425\begin{figure}[!t]
426\begin{tabular}{l@{\hspace{0.2cm}}|@{\hspace{0.2cm}}l}
427\begin{lstlisting}
428char a[] = {3, 2, 7, 14};
429char threshold = 4;
430
431int count(char *p, int len) {
432  char j;
433  int found = 0;
434  for (j=0; j < len; j++) {
435    if (*p <= threshold)
436      found++;
437    p++;
438    }
439  return found;
440}
441
442int main() {
443  return count(a,4);
444}
445\end{lstlisting}
446&
447%  $\vcenter{\includegraphics[width=7.5cm]{interaction_diagram.pdf}}$
448\begin{tikzpicture}[
449    baseline={([yshift=-.5ex]current bounding box)},
450    element/.style={draw, text width=1.6cm, on chain, text badly centered},
451    >=stealth
452    ]
453{[start chain=going below, node distance=.5cm]
454\node [element] (cerco) {CerCo\\compiler};
455\node [element] (cost)  {CerCo\\cost plugin};
456{[densely dashed]
457\node [element] (ded)   {Deductive\\platform};
458\node [element] (check) {Proof\\checker};
459}
460}
461\coordinate [left=3.5cm of cerco] (left);
462{[every node/.style={above, text width=3.5cm, text badly centered,
463                     font=\scriptsize}]
464\draw [<-] ([yshift=-1ex]cerco.north west) coordinate (t) --
465    node {C source}
466    (t-|left);
467\draw [->] (cerco) -- (cost);
468\draw [->] ([yshift=1ex]cerco.south west) coordinate (t) --
469    node {C source+\color{red}{cost annotations}}
470    (t-|left) coordinate (cerco out);
471\draw [->] ([yshift=1ex]cost.south west) coordinate (t) --
472    node {C source+\color{red}{cost annotations}\\+\color{blue}{synthesized assertions}}
473    (t-|left) coordinate (out);
474{[densely dashed]
475\draw [<-] ([yshift=-1ex]ded.north west) coordinate (t) --
476    node {C source+\color{red}{cost annotations}\\+\color{blue}{complexity assertions}}
477    (t-|left) coordinate (ded in) .. controls +(-.5, 0) and +(-.5, 0) .. (out);
478\draw [->] ([yshift=1ex]ded.south west) coordinate (t) --
479    node {complexity obligations}
480    (t-|left) coordinate (out);
481\draw [<-] ([yshift=-1ex]check.north west) coordinate (t) --
482    node {complexity proof}
483    (t-|left) .. controls +(-.5, 0) and +(-.5, 0) .. (out);
484\draw [dash phase=2.5pt] (cerco out) .. controls +(-1, 0) and +(-1, 0) ..
485    (ded in);
486}}
487%% user:
489\draw (current bounding box.west) ++(-.2,.5)
490    circle (.2) ++(0,-.2) -- ++(0,-.1) coordinate (t);
491% arms
492\draw (t) -- +(-.3,-.2);
493\draw (t) -- +(.3,-.2);
494% body
495\draw (t) -- ++(0,-.4) coordinate (t);
496% legs
497\draw (t) -- +(-.2,-.6);
498\draw (t) -- +(.2,-.6);
499\end{tikzpicture}
500\end{tabular}
501\caption{On the left: C code to count the number of elements in an array
502 that are less than or equal to a given threshold. On the right: CerCo's interaction
503 diagram. Components provided by CerCo are drawn with a solid border.}
504\label{test1}
505\end{figure}
506We illustrate the workflow we envisage (on the right
507of~\autoref{test1}) on an example program (on the left
508of~\autoref{test1}).  The user writes the program and feeds it to the
509CerCo compiler, which outputs an instrumented version of the same
510program that updates global variables that record the elapsed
511execution time and the stack space usage.  The red lines in
512\autoref{itest1} introducing variables, functions and function calls
513starting with \lstinline'__cost' and \lstinline'__stack' are the instrumentation introduced by the
514compiler.  For example, the two calls at the start of
515\lstinline'count' say that 4 bytes of stack are required, and that it
516takes 111 cycles to reach the next cost annotation (in the loop body).
517The compiler measures these on the labelled object code that it generates.
518
519 The annotated program can then be enriched with complexity
520assertions in the style of Hoare logic, that are passed to a deductive
521platform (in our case Frama-C). We provide as a Frama-C cost plugin a
522simple automatic synthesiser for complexity assertions which can
523be overridden by the user to increase or decrease accuracy.  These are the blue
524comments starting with \lstinline'/*@' in \autoref{itest1}, written in
525Frama-C's specification language, ACSL.  From the
526assertions, a general purpose deductive platform produces proof
527obligations which in turn can be closed by automatic or interactive
528provers, ending in a proof certificate.
529
530% NB: if you try this example with the live CD you should increase the timeout
531
532Twelve proof obligations are generated from~\autoref{itest1} (to prove
533that the loop invariant holds after one execution if it holds before,
534to prove that the whole program execution takes at most 1358 cycles, and so on).  Note that the synthesised time bound for \lstinline'count',
535$178+214*(1+\text{\lstinline'len'})$ cycles, is parametric in the length of
536the array. The CVC3 prover
537closes all obligations within half a minute on routine commodity
538hardware.  A simpler non-parametric version can be solved in a few
539seconds.
540%
541\fvset{commandchars=\\\{\}}
542\lstset{morecomment=[s][\color{blue}]{/*@}{*/},
543        moredelim=[is][\color{blue}]{$}{$},
544        moredelim=[is][\color{red}]{|}{|},
545        lineskip=-2pt}
546\begin{figure}[!p]
547\begin{lstlisting}
548|int __cost = 33, __stack = 5, __stack_max = 5;|
549|void __cost_incr(int incr) { __cost += incr; }|
550|void __stack_incr(int incr) {
551  __stack += incr;
552  __stack_max = __stack_max < __stack ? __stack : __stack_max;
553}|
554
555char a[4] = {3, 2, 7, 14};  char threshold = 4;
556
557/*@ behavior stack_cost:
558      ensures __stack_max <= __max(\old(__stack_max), 4+\old(__stack));
559      ensures __stack == \old(__stack);
560    behavior time_cost:
561      ensures __cost <= \old(__cost)+(178+214*__max(1+\at(len,Pre), 0));
562*/
563int count(char *p, int len) {
564  char j;  int found = 0;
565  |__stack_incr(4);  __cost_incr(111);|
566  $__l: /* internal */$
567  /*@ for time_cost: loop invariant
568        __cost <= \at(__cost,__l)+
569                  214*(__max(\at((len-j)+1,__l), 0)-__max(1+(len-j), 0));
570      for stack_cost: loop invariant
571        __stack_max == \at(__stack_max,__l);
572      for stack_cost: loop invariant
573        __stack == \at(__stack,__l);
574      loop variant len-j;
575  */
576  for (j = 0; j < len; j++) {
577    |__cost_incr(78);|
578    if (*p <= threshold) { |__cost_incr(136);| found ++; }
579    else { |__cost_incr(114);| }
580    p ++;
581  }
582  |__cost_incr(67);  __stack_incr(-4);|
583  return found;
584}
585
586/*@ behavior stack_cost:
587      ensures __stack_max <= __max(\old(__stack_max), 6+\old(__stack));
588      ensures __stack == \old(__stack);
589    behavior time_cost:
590      ensures __cost <= \old(__cost)+1358;
591*/
592int main(void) {
593  int t;
594  |__stack_incr(2);  __cost_incr(110);|
595  t = count(a,4);
596  |__stack_incr(-2);|
597  return t;
598}
599\end{lstlisting}
600\caption{The instrumented version of the program in \autoref{test1},
601 with instrumentation added by the CerCo compiler in red and cost invariants
602 added by the CerCo Frama-C plugin in blue. The \lstinline'__cost',
603 \lstinline'__stack' and \lstinline'__stack_max' variables hold the elapsed time
604in clock cycles and the current and maximum stack usage. Their initial values
605hold the clock cycles spent in initialising the global data before calling
606\lstinline'main' and the space required by global data (and thus unavailable for
607the stack).
608}
609\label{itest1}
610\end{figure}
611
[3607]612% ---------------------------------------------------------------------------- %
613% SECTION                                                                      %
614% ---------------------------------------------------------------------------- %
615
616\section{Conclusions}
617\label{sect.conclusions}
618
[3600]619\begin{acknowledgements}
620\end{acknowledgements}
[3599]621
[3606]622\bibliographystyle{spmpsci}
[3605]623\bibliography{cerco}
[3599]624
625\end{document}
Note: See TracBrowser for help on using the repository browser.