source: Papers/jar-cerco-2017/introduction.tex @ 3633

Last change on this file since 3633 was 3633, checked in by mulligan, 4 years ago

Fleshed out first part of introduction.

File size: 24.4 KB
4% Introduction
5%   Problem being solved, background, etc.
6%   Current state of the art (and problem with it)
7%   The `CerCo approach' (tm)
8%   Brief Matita overview
9%   Map of paper
14Programs are specified using both functional and non-functional constraints.
15Functional constraints dictate what tasks a program must do; non-functional constraints limit the resources the program may consume whilst completing those tasks.
16Depending on the application domain, non-functional constraints are as important as functional constraints when specifying a program.
17Real-time systems, with hard limits on application response times, and implementations of cryptographic primitives, which must be hardened to timing side-channel attacks, are two examples that fit this pattern.
18A cryptography library susceptible to a timing side-channel attack is not an annoyance---it's an implementation error that undermines the entire purpose of the library.
20Currently, a program's functional properties can be established by combining user annotations---preconditions, invariants, and so on---with various automated and semi-automated analyses---invariant generators, type systems, abstract interpretations, applications of theorem proving, and so on---on the high-level source code of the program.
21Functional properties of a program are therefore established by reasoning about the source code that the application programmer actually sees.
22Further, the results of any analysis can be communicated to the programmer in terms of abstractions, control flow, and an overall system design that they are familiar with.
24By contrast, a program's non-functional properties are established by reasoning on low-level object code produced not by a programmer, but by a compiler.
25Whilst analyses operating at this level can and do produce very accurate results, analysis at such a low-level of abstraction invariably has disadvantages:
29It can be hard to deduce the high-level structure of the program after compiler optimisations.
30The object code produced by an optimising compiler may have a radically different control flow to the original source code program.
32Object-code analysis is unstable.
33Modern compilers and linkers are highly non-compositional: they implement a variety of sophisticated optimisation passes, and use an abundance of procedural, intra-procedural, module-level, and link-time analyses to direct these optimisations.
34As a result, small changes in high-level source code can produce radically different object code outputs from a compiler, affecting any analysis effected on that object code.
36It is well understood by Software Engineers that problems with the design or implementation of a program are cheaper to resolve early in the development process. %cite
37Despite this, techniques that operate on object code are not useful early in the development process of a program, where programs may be incomplete, with missing functionality, or entire subcomponents missing.
39Parametric cost analysis is very hard: how can we translate a cost that depends on the execution state, for example the value of a register or a carry bit, to a cost that the user can understand whilst looking at the source code?
41Performing functional analysis 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 analysis.
42It is hard for the programmer to understand the results of the analysis, or direct its execution, as high-level abstractions, control flow constructs, and so on, introduced by the programmer are `translated away'.
46We want to reconcile functional and non-functional analysis: to share
47information between them and perform both at the same time on high-level source
50What has previously prevented this approach from being applied is the lack of a
51uniform and precise cost model for high-level code, since each statement
52occurrence is compiled differently, optimisations may change control flow, and
53the cost of an object code instruction may depend on the runtime state of
54hardware components like pipelines and caches, none of which are visible in the
55source code.
57We envision a new generation of compilers that track program structure through
58compilation and optimisation and exploit this information to define a precise,
59non-uniform cost model for source code that accounts for runtime state. With
60such a cost model we can perform non-functional verification in a similar way
61to functional verification and exploit the state of the art in automated
62high-level verification~\cite{survey}.
64The techniques currently used by the Worst Case Execution Time (WCET) community,
65which perform analysis on object code, are still applicable but can be coupled
66with additional source-level analysis. In cases where our approach produces
67overly complex cost models, safe approximations can be used to trade complexity
68for precision.
70Finally, source code analysis can be used early in the development process,
71when components have been specified but not implemented, as modularity means
72that it is enough to specify the non-functional behaviour of missing components.
75We have developed \emph{the labelling approach}~\cite{labelling}, a technique
76to implement compilers that induce cost models on source programs by very
77lightweight tracking of code changes through the compilation pipeline.
79We have implemented a compiler from C to object binaries for the 8051
80microcontroller which uses this technique. The compiler predicts
81execution time and stack space usage. We have also verified the compile using
82an interactive theorem prover. As we are targeting an embedded microcontroller
83we have not considered dynamic memory allocation.
85To demonstrate source-level verification of costs we have implemented a Frama-C
86plugin~\cite{framac} that invokes the compiler on a source program and uses it
87to generate invariants on the high-level source that correctly model low-level
88costs. The plugin certifies that the program respects these costs by calling
89automated theorem provers, a new and innovative technique in the field of cost
92Finally, we have conducted several case studies, including showing that the
93plugin can automatically compute and certify the exact reaction time of
94Lustre~\cite{lustre} data flow programs compiled into C.
96\subsection{Project context and approach}
98Formal methods for verifying functional properties of programs have now reached
99such a level of maturity and automation that their adoption is slowly
100increasing in production environments.
102For safety critical code, it is becoming commonplace to combine rigorous
103software engineering methodologies and testing with static analyses, taking the
104strengths of each and mitigating their weaknesses. Of particular interest are
105open frameworks for the combination of different formal methods, where the
106programs can be progressively specified and enriched with new safety
107guarantees: every method contributes knowledge (e.g. new invariants) that
108can be built upon by other analysis methods.
110The outlook for verification of non-functional properties of programs (time
111spent, memory used, energy consumed) is bleaker. In most cases, verifying that
112real-time systems meet their deadlines is done by simply performing many runs
113of the system and timing their execution, computing the maximum time and adding
114an empirical safety margin, claiming the result to be a bound for the WCET of
115the program.
117Formal methods and software to statically analyse the WCET of programs exist,
118but they often produce bounds that are too pessimistic to be useful. Recent
119advances in hardware architecture have focused on improving average case
120performance, not predictability of the worst case.
122Execution time is becoming increasingly dependent on execution history and the
123internal state of hardware components like pipelines and caches. Multi-core
124processors and non-uniform memory models are drastically reducing the
125possibility of performing static analysis in isolation, because programs are
126less and less time-composable. Clock-precise hardware models are necessary for
127static analysis, and obtaining them is becoming harder due to the increased
128sophistication of hardware design.
130The need for reliable real-time systems and programs is increasing, and there
131is pressure from the research community for the introduction of hardware with
132more predictable behaviour, which would be more suitable for static analysis.
133One example, being investigated by the Proartis project~\cite{proartis}, is to
134decouple execution time from execution history by introducing randomisation.
136In CerCo~\cite{cerco} we do not address this problem, optimistically assuming
137that improvements in low-level timing analysis or architecture will make
138verification feasible in the longer term.
140Instead, the main objective of our work is to bring together static analysis of
141functional and non-functional properties, which in the current state of the art
142are independent activities with limited exchange of information: while the
143functional properties are verified on the source code, the analysis of
144non-functional properties is performed on object code to exploit clock-precise
145hardware models.
147\subsection{Current object-code methods}
149Analysis currently takes place on object code for two main reasons.
151Firstly, there cannot be a uniform, precise cost model for source code
152instructions (or even basic blocks). During compilation, high level
153instructions are broken up and reassembled in context-specific ways so that
154identifying a fragment of object code and a single high level instruction is
157Additionally, the control flow of the object and source code can be very
158different as a result of optimisations. For example, aggressive loop
159optimisations can completely transform source level loops.
161Despite the lack of a uniform, compilation- and program-independent cost model
162on the source language, research on the analysis of non-asymptotic execution
163time on high level languages assuming such a model is growing and gaining
166Unless such cost models are developed, the future practical impact of this
167research looks to be minimal. One existing approach is the EmBounded
168project~\cite{embounded}, which compositionally compiles high-level code to a
169byte code that is executed by an interpreter with guarantees on the maximal
170execution time spent for each byte code instruction. This provides a model
171that is uniform, though at the expense of precision (each cost is a pessimistic
172upper bound) and the performance of the executed code (the byte code is
173interpreted compositionally).
175The second reason to perform analysis on the object code is that bounding
176the worst case execution time of small code fragments in isolation (e.g. loop
177bodies) and then adding up the bounds yields very poor estimates as no
178knowledge of the hardware state prior to executing the fragment can be assumed.
180By analysing longer runs the bound obtained becomes more precise because the
181lack of information about the initial state has a relatively small impact.
183To calculate the cost of an execution, value and control flow analyses are
184required to bound the number of times each basic block is executed. Currently,
185state of the art WCET analysis tools, such as AbsInt's aiT
186toolset~\cite{absint}, perform these analyses on object code, where the logic
187of the program is harder to reconstruct and most information available at the
188source code level has been lost; see~\cite{stateart} for a survey.
190Imprecision in the analysis can lead to useless bounds. To augment precision,
191currently tools ask the user to provide constraints on the object code control
192flow, usually in the form of bounds on the number of iterations of loops or
193linear inequalities on them. This requires the user to manually link the source and object code, translating their assumptions on the source code (which may be
194wrong) to object code constraints. This task is hard and error-prone,
195especially in the presence of complex compiler optimisations.
197Traditional techniques for WCET that work on object code are also affected by
198another problem: they cannot be applied before the generation of the object
199code. Functional properties can be analysed in early development stages, while
200analysis of non-functional properties may come too late to avoid expensive
201changes to the program architecture.
203\subsection{The CerCo approach}
205In CerCo we propose a radically new approach to the problem: we reject the idea
206of a uniform cost model and we propose that the compiler, which knows how the
207code is translated, must return the cost model for basic blocks of high level
208instructions. It must do so by keeping track of the control flow modifications
209to reverse them and by interfacing with processor timing analysis.
211By embracing compilation, instead of avoiding it like EmBounded did, a CerCo
212compiler can both produce efficient code and return costs that are as precise
213as the processor timing analysis can be. Moreover, our costs can be parametric:
214the cost of a block can depend on actual program data, on a summary of the
215execution history, or on an approximated representation of the hardware state.
217For example, loop optimisations may assign a cost to a loop body that is a
218function of the number of iterations performed. As another example, the cost of
219a block may be a function of the vector of stalled pipeline states, which can
220be exposed in the source code and updated at each basic block exit.
222It is parametricity that allows one to analyse small code fragments without
223losing precision. In the analysis of the code fragment we do not have to ignore
224the initial hardware state; rather, we can assume that we know exactly which
225state (or mode, as the WCET literature calls it) we are in.
227The CerCo approach has the potential to dramatically improve the state of the
228art. By performing control and data flow analyses on the source code, the
229error-prone translation of invariants is avoided entirely. Instead, this work
230is done at the source code level using tools of the user's choice.
232Any available technique for the verification of functional properties can be
233easily reused and multiple techniques can collaborate to infer and certify cost
234invariants for the program. There are no limitations on the types of loops or
235data structures involved.
237Parametric cost analysis becomes the default, with non-parametric bounds used
238only as a last resort when the user decides to trade the complexity of the
239analysis for more precision.
241\emph{A priori}, no technique previously used in traditional WCET is obsolete:
242processor timing analysis can be used by the compiler on the object code, and
243other techniques can be applied at the source code level.
245Our approach also works in the early stages of development by allowing the user
246to axiomatically attach costs to unimplemented components.
248Software used to verify properties of programs must be as bug-free as possible.
249The trusted code base for verification consists of the code that needs to be
250trusted to believe that the property holds.
252The trusted code base of state-of-the-art WCET tools is very large: one needs
253to trust the control flow analyser, the linear programming libraries used, and
254also the formal models of the hardware under analysis.
256In CerCo we move the control flow analysis to the source code level, and we
257introduce a non-standard compiler. To reduce the size of the trusted code base,
258we have implemented a prototype compiler and static analyser in an interactive
259theorem prover, which was used to certify that the costs added to the source
260code are indeed those incurred by the hardware. We have also implemented formal models of the hardware and of the high level source language in the interactive
261theorem prover.
263Control flow analysis on the source code has been obtained using invariant
264generators, tools to produce proof obligations from generated invariants and
265automatic theorem provers to verify the obligations. If these tools are able to
266generate proof traces that can be independently checked, the only remaining
267component that enters the trusted code base is an off-the-shelf invariant
268generator which, in turn, can be proved correct using an interactive theorem
271With these methods, we achieve the objective of allowing the use of more
272off-the-shelf components (e.g. provers and invariant generators) whilst
273reducing the trusted code base at the same time.
275\subsection{Introduction to Matita}
277Matita is a theorem prover based on a variant of the Calculus of Coinductive Constructions~\cite{asperti:user:2007}.
278The system features a full spectrum of dependent types and (co)inductive families, a system of coercions, a tactic-driven proof construction engine~\cite{sacerdoti-coen:tinycals:2007}, and paramodulation based automation~\cite{asperti:higher-order:2007}, all of which we exploit in the formalisation described herein.
280Matita's syntax is similar to the syntaxes of mainstream functional programming languages such as OCaml or Standard ML.
281The type theory that Matita implements is broadly akin to that of Coq~\cite{coq:2004} and Agda~\cite{bove:brief:2009}.
282Nevertheless, we provide a brief explanation of the main syntactic and type-theoretic features of Matita that will be needed to follow the body of the paper:
285Non-recursive functions and definitions are introduced via the \texttt{definition} keyword.
286Recursive functions are introduced with \texttt{let rec}.
287Mutually recursive functions are separated via the \texttt{and} keyword.
288Matita's termination checker ensures that all recursive functions are terminating before being admitted to maintain soundness of the system's logic.
290Matita has an infinite hierarchy of type universes.
291A single impredicative universe of types, \texttt{Prop}, exists at the base of this hierarchy.
292An infinite series of predicative universes, \texttt{Type[0]} : \texttt{Type[1]} : \texttt{Type[2]}, and so on and so forth, sits atop \texttt{Prop}.
293Matita, unlike Coq or Agda, implements no form of typical ambiguity or universe polymorphism, with explicit concrete universe levels being preferred instead.
295Matita's type theory plays host to a rich and expressive higher-order logic.
296Constants \texttt{True} and \texttt{False} represent truth and falsity in \texttt{Prop} respectively.
297Two inductive families in \texttt{Prop} encode conjunction and disjunction---$\mathtt{P \wedge Q}$ and $\mathtt{P \vee Q}$ respectively.
299As is usual, implication and universal quantification are identified with the dependent function space ($\Pi$ types), whereas (constructive) existential quantification is encoded as a dependent sum (a $\Sigma$-type).
300We write $\All{x : \phi}\psi$ for the dependent function space, and abbreviate this as $\phi \rightarrow \psi$ when $x \not\in fv(\psi)$ as usual.
301We use $\langle M,N \rangle$ for the pairing of $M$ and $N$.
303Inductive and coinductive families are introduced via the \texttt{inductive} and \texttt{coinductive} keywords respectively, with named constructor declarations separated by a bar.
304Mutually inductive data family declarations are separated via \texttt{with}.
305In the following declaration:
307inductive I ($P_1$ : $\tau_1$) $\ldots$ ($P_n$ : $\tau_n$) : $\phi_1 \rightarrow \ldots \rightarrow \phi_m \rightarrow \phi$ := $\ldots$
309We call $P_i$ for $0 \leq i \leq n$ the \textbf{parameters} of \texttt{I} and $\phi_j$ for $0 \leq j \leq m$ the \textbf{indices} of \texttt{I}.
310Matita's positivity checker ensures that constructors have strictly-positive types before admitting an inductive family to maintain soundness of the system's logic.
312Records are introduced with the \texttt{record} keyword.
313A Matita record
315record R : Type[0] := { F1 : nat }.
317may be thought of as syntactic sugar for a single-constructor inductive data type of the same name:
319inductive R : Type[0] :=
320  | mk_R : nat -> R.
322A record field's type may depend on fields declared earlier in the record.
324Records may be decomposed with projections.
325Projections, one for each of field of a record, are registered in the global context.
326In the example record above, \texttt{F1} of type $R \rightarrow nat$ is registered as a field projection and $mk\_R$ of type $nat \rightarrow R$ is registered as a constructor.
328Record fields may also be marked as coercions.
329In the following example
331record S : Type[1] :=
333  Carrier :> Type[0];
334  op : Carrier -> Carrier -> Carrier
337the field \texttt{Carrier} is declared to be a coercion with `\texttt{:>}'.with the operational effect being that the field projection \texttt{Carrier} may be omitted where it could be successfully inferred by Matita.
338Field coercions facilitate the informal but common mathematical practice of intentionally confusing a structure with its underlying carrier set.
340Terms may be freely omitted, allowing the user to write down partial types and terms.
341A question mark, \texttt{?}, denotes a single term that has been omitted by the user.
342Some omitted terms can be deduced by Matita's refinement system.
343Other, more complex goals arising from omitted terms may require user input to solve, in which case a proof obligation is opened for each term that cannot be deduced automatically.
344Three consecutive dots, \texttt{$\ldots$}, denote multiple terms or types that have been omitted.
346Data may be decomposed by pattern matching with a \texttt{match} expression.
347We may fully annotate a \texttt{match} expression with its return type.
348This is especially useful when working with indexed families of types or with invariants, expressed as types, on functions.
349In the following
351match t return $\lam{x}x = 0 \rightarrow bool$ with
352[ 0    $\Rightarrow$ $\lam{prf_1}P_1$
353| S m $\Rightarrow$ $\lam{prf_2}P_2$
354] (refl $\ldots$ t)
356the \texttt{0} branch of the \texttt{match} expression returns a function from $0 = 0$ to \texttt{bool}, whereas the \texttt{S m} branch of the \texttt{match} expression returns a function from \texttt{S m = 0} to \texttt{bool}.
357In both cases the annotated return type $\lam{x}x = 0 \rightarrow bool$ has been specialised given new information about \texttt{t} revealed by the act of pattern matching.
358The entire term, with \texttt{match} expression applied to \texttt{refl $\ldots$ t}, has type \texttt{bool}.
360Matita features a liberal system of coercions (distinct from the previously mentioned record field coercions).
361It is possible to define a uniform coercion $\lam{x}\langle x, ?\rangle$ from every type $T$ to the dependent product $\Sigma{x : T}. P x$.
362The coercion opens a proof obligation that asks the user to prove that $P$ holds for $x$.
363When a coercion is to be applied to a complex term (for example, a $\lambda$-abstraction, a local definition, or a case analysis), the system automatically propagates the coercion to the sub-terms.
364For instance, to apply a coercion to force $\lam{x}M : A \rightarrow B$ to
365have type $\All{x : A}\Sigma{y : B}. P x y$, the system looks for a coercion from $M : B$ to $\Sigma{y : B}. P x y$ in a context augmented with $x : A$.
366This is significant when the coercion opens a proof obligation, as the user will be presented with multiple, but simpler proof obligations in the correct context.
367In this way, Matita supports the `Russell' proof methodology developed by Sozeau in~\cite{sozeau:subset:2007}, in a lightweight but tightly-integrated manner.
369Throughout, for reasons of clarity, conciseness, and readability, we may choose to simplify or omit parts of Matita code.
370We will always ensure that these omissions do not mislead the reader.
372\subsection{Map of the paper}
374The rest of the paper is structured as follows.
376In section~\ref{sect.compiler.architecture}, we describe the architecture of the
377CerCo compiler, as well as the intermediate languages that it uses. We also
378describe the target hardware and its formal model.
380In section~\ref{sect.compiler.proof}, we describe the proof of correctness of
381the compiler in more detail. We explain our use of structured traces, the
382labelling approach, and discuss the assembler.
384In section~\ref{sect.formal.development}, we present data on the formal
387In section~\ref{sect.framac.plugin}, we discuss the Frama-C plugin, as well as
388some of the case studies we have performed to validate it.
390Finally, in section~\ref{sect.conclusions} we present conclusions, as well as
391related and future work.
Note: See TracBrowser for help on using the repository browser.