1 | \documentclass[11pt,epsf,a4wide]{article} |
---|
2 | \usepackage[mathletters]{ucs} |
---|
3 | \usepackage[utf8x]{inputenc} |
---|
4 | \usepackage{stmaryrd} |
---|
5 | \usepackage{listings} |
---|
6 | \usepackage{../../style/cerco} |
---|
7 | \newcommand{\ocaml}{OCaml} |
---|
8 | \newcommand{\clight}{Clight} |
---|
9 | \newcommand{\matita}{Matita} |
---|
10 | \newcommand{\sdcc}{\texttt{sdcc}} |
---|
11 | |
---|
12 | \newcommand{\textSigma}{\ensuremath{\Sigma}} |
---|
13 | |
---|
14 | % LaTeX Companion, p 74 |
---|
15 | \newcommand{\todo}[1]{\marginpar{\raggedright - #1}} |
---|
16 | |
---|
17 | \lstdefinelanguage{coq} |
---|
18 | {keywords={Definition,Lemma,Theorem,Remark,Qed,Save,Inductive,Record}, |
---|
19 | morekeywords={[2]if,then,else}, |
---|
20 | } |
---|
21 | |
---|
22 | \lstdefinelanguage{matita} |
---|
23 | {keywords={definition,lemma,theorem,remark,inductive,record,qed,let,rec,match,with,Type,and,on}, |
---|
24 | morekeywords={[2]whd,normalize,elim,cases,destruct}, |
---|
25 | mathescape=true, |
---|
26 | morecomment=[n]{(*}{*)}, |
---|
27 | } |
---|
28 | |
---|
29 | \lstset{language=matita,basicstyle=\small\tt,columns=flexible,breaklines=false, |
---|
30 | keywordstyle=\color{red}\bfseries, |
---|
31 | keywordstyle=[2]\color{blue}, |
---|
32 | commentstyle=\color{green}, |
---|
33 | stringstyle=\color{blue}, |
---|
34 | showspaces=false,showstringspaces=false} |
---|
35 | |
---|
36 | \lstset{extendedchars=false} |
---|
37 | \lstset{inputencoding=utf8x} |
---|
38 | \DeclareUnicodeCharacter{8797}{:=} |
---|
39 | \DeclareUnicodeCharacter{10746}{++} |
---|
40 | \DeclareUnicodeCharacter{9001}{\ensuremath{\langle}} |
---|
41 | \DeclareUnicodeCharacter{9002}{\ensuremath{\rangle}} |
---|
42 | |
---|
43 | |
---|
44 | \title{ |
---|
45 | INFORMATION AND COMMUNICATION TECHNOLOGIES\\ |
---|
46 | (ICT)\\ |
---|
47 | PROGRAMME\\ |
---|
48 | \vspace*{1cm}Project FP7-ICT-2009-C-243881 \cerco{}} |
---|
49 | |
---|
50 | \date{ } |
---|
51 | \author{} |
---|
52 | |
---|
53 | \begin{document} |
---|
54 | \thispagestyle{empty} |
---|
55 | |
---|
56 | \vspace*{-1cm} |
---|
57 | \begin{center} |
---|
58 | \includegraphics[width=0.6\textwidth]{../../style/cerco_logo.png} |
---|
59 | \end{center} |
---|
60 | |
---|
61 | \begin{minipage}{\textwidth} |
---|
62 | \maketitle |
---|
63 | \end{minipage} |
---|
64 | |
---|
65 | |
---|
66 | \vspace*{0.5cm} |
---|
67 | \begin{center} |
---|
68 | \begin{LARGE} |
---|
69 | \bf |
---|
70 | Report n. D3.4\\ |
---|
71 | Front-end Correctness Proofs\\ |
---|
72 | \end{LARGE} |
---|
73 | \end{center} |
---|
74 | |
---|
75 | \vspace*{2cm} |
---|
76 | \begin{center} |
---|
77 | \begin{large} |
---|
78 | Version 1.0 |
---|
79 | \end{large} |
---|
80 | \end{center} |
---|
81 | |
---|
82 | \vspace*{0.5cm} |
---|
83 | \begin{center} |
---|
84 | \begin{large} |
---|
85 | Authors:\\ |
---|
86 | Brian~Campbell, Ilias~Garnier, James~McKinna, Ian~Stark |
---|
87 | \end{large} |
---|
88 | \end{center} |
---|
89 | |
---|
90 | \vspace*{\fill} |
---|
91 | \noindent |
---|
92 | Project Acronym: \cerco{}\\ |
---|
93 | Project full title: Certified Complexity\\ |
---|
94 | Proposal/Contract no.: FP7-ICT-2009-C-243881 \cerco{}\\ |
---|
95 | |
---|
96 | \clearpage \pagestyle{myheadings} \markright{\cerco{}, FP7-ICT-2009-C-243881} |
---|
97 | |
---|
98 | \newpage |
---|
99 | |
---|
100 | \section*{Executive Summary} |
---|
101 | \addcontentsline{toc}{section}{Executive Summary} |
---|
102 | |
---|
103 | |
---|
104 | This document reports on \textbf{D3.4:~Front-end Correctness Proofs}, the |
---|
105 | final deliverable of \cerco{} work package~3 \emph{Verified Compiler - front |
---|
106 | end}. That work package was devoted to the formalisation and verification of |
---|
107 | the front-end of the \cerco{} cost lifting compiler, and Deliverable~3.4 is a |
---|
108 | machine-checked formal correctness proof of the compiler front-end, written in |
---|
109 | the \matita{} proof assistant. |
---|
110 | |
---|
111 | The proof itself is contained in a series of Matita files, listed in |
---|
112 | Appendix~A and culminating in the lemma \lstinline'front_end_correct' and the |
---|
113 | theorem \lstinline'correct' in the file \lstinline'correctness.ma'. This |
---|
114 | document accompanies the deliverable and briefly reports on the work carried |
---|
115 | out in the development of that proof. |
---|
116 | |
---|
117 | \vspace*{1cm} |
---|
118 | |
---|
119 | \paragraph{Abstract} |
---|
120 | |
---|
121 | We report on the correctness proofs for the front-end of the \cerco{} |
---|
122 | cost lifting compiler. First, we identify the core result we wish to |
---|
123 | prove, which says that the we correctly predict the precise execution |
---|
124 | time for particular parts of the execution called \emph{measurable} |
---|
125 | subtraces. Then we consider the three distinct parts of the task: |
---|
126 | showing that the \emph{annotated source code} output by the compiler |
---|
127 | has equivalent behaviour to the original input (up to the |
---|
128 | annotations); showing that a measurable subtrace of the |
---|
129 | annotated source code corresponds to an equivalent measurable subtrace |
---|
130 | in the code produced by the front-end, including costs; and finally |
---|
131 | showing that the enriched \emph{structured} execution traces required |
---|
132 | for cost correctness in the back-end can be constructed from the |
---|
133 | properties of the code produced by the front-end. |
---|
134 | |
---|
135 | A key part of our work is that the intensional correctness results which show |
---|
136 | that we get consistent cost measurements throughout the intermediate languages |
---|
137 | of the compiler can be layered on top of normal forward simulation results, |
---|
138 | if we split those results into local call-structure preserving simulations. |
---|
139 | This split allowed us to concentrate on the \textbf{intensional} proofs by |
---|
140 | axiomatising some of the extensional simulation results that are very similar to |
---|
141 | existing compiler correctness results, such as CompCert. |
---|
142 | |
---|
143 | This report is about the correctness results that are deliverable |
---|
144 | D3.4, which are about the formalised compiler described in D3.2, using |
---|
145 | the source language semantics from D3.1 and intermediate language |
---|
146 | semantics from D3.3. It builds on earlier work on the correctness of |
---|
147 | a toy compiler built to test the labelling approach in D2.1. Together |
---|
148 | with the companion deliverable about the correctness of the back-end, |
---|
149 | D4.4, we obtain results about the whole formalised compiler. |
---|
150 | |
---|
151 | \newpage |
---|
152 | |
---|
153 | \tableofcontents |
---|
154 | |
---|
155 | % CHECK: clear up any -ize vs -ise |
---|
156 | % CHECK: clear up any "front end" vs "front-end" |
---|
157 | % CHECK: clear up any mentions of languages that aren't textsf'd. |
---|
158 | % CHECK: fix unicode in listings |
---|
159 | |
---|
160 | \section{Introduction} |
---|
161 | |
---|
162 | The \cerco{} compiler produces a version of the source code containing |
---|
163 | annotations describing the timing behaviour of the object code, as |
---|
164 | well as the object code itself. It compiles C code, targeting |
---|
165 | microcontrollers implementing the Intel 8051 architecture. There are |
---|
166 | two versions: first, an initial prototype was implemented in |
---|
167 | \ocaml{}~\cite{d2.2}, then a version was formalised in the \matita{} |
---|
168 | proof assistant~\cite{d3.2,d4.2} and extracted to \ocaml{} code to |
---|
169 | produce an executable compiler. In this document we present results |
---|
170 | from Deliverable 3.4, the formalised proofs in \matita{} about the |
---|
171 | front-end of the latter version of the compiler (culminating in the |
---|
172 | \lstinline'front_end_correct' lemma), and describe how that fits |
---|
173 | into the verification of the whole compiler. |
---|
174 | |
---|
175 | A key part of this work was to layer the \emph{intensional} correctness |
---|
176 | results that show that the costs produced are correct on top of the |
---|
177 | proofs about the compiled code's \emph{extensional} behaviour (that is, the |
---|
178 | functional correctness of the compiler). Unfortunately, the ambitious |
---|
179 | goal of completely verifying the entire compiler was not feasible |
---|
180 | within the time available, but thanks to this separation of |
---|
181 | extensional and intensional proofs we are able to axiomatise some extensional |
---|
182 | simulation results which are similar to those in other compiler verification |
---|
183 | projects and concentrate on the novel intensional proofs. We were |
---|
184 | also able to add stack space costs to obtain a stronger result. The |
---|
185 | proofs were made more tractable by introducing compile-time checks for |
---|
186 | the `sound and precise' cost labelling properties rather than proving |
---|
187 | that they are preserved throughout. |
---|
188 | |
---|
189 | The overall statement of correctness says that the annotated program has the |
---|
190 | same behaviour as the input, and that for any suitably well-structured part of |
---|
191 | the execution (which we call \emph{measurable}), the object code will execute |
---|
192 | the same behaviour taking precisely the time given by the cost annotations in |
---|
193 | the annotated source program. |
---|
194 | |
---|
195 | In the next section we recall the structure of the compiler and make the overall |
---|
196 | statement more precise. Following that, in Section~\ref{sec:fegoals} we |
---|
197 | describe the statements we need to prove about the intermediate \textsf{RTLabs} |
---|
198 | programs for the back-end proofs. |
---|
199 | Section~\ref{sec:inputtolabelling} covers the compiler passes which produce the |
---|
200 | annotated source program and Section~\ref{sec:measurablelifting} the rest |
---|
201 | of the transformations in the front-end. Then the compile-time checks |
---|
202 | for good cost labelling are detailed in Section~\ref{sec:costchecks} |
---|
203 | and the proofs that the structured traces required by the back-end |
---|
204 | exist are discussed in Section~\ref{sec:structuredtrace}. |
---|
205 | |
---|
206 | \section{The compiler and its correctness statement} |
---|
207 | |
---|
208 | The uncertified prototype \ocaml{} \cerco{} compiler was originally described |
---|
209 | in Deliverables 2.1 and 2.2. Its design was replicated in the formal |
---|
210 | \matita{} code, which was presented in Deliverables 3.2 and 4.2, for |
---|
211 | the front-end and back-end respectively. |
---|
212 | |
---|
213 | \begin{figure} |
---|
214 | \begin{center} |
---|
215 | \includegraphics[width=0.5\linewidth]{compiler-plain.pdf} |
---|
216 | \end{center} |
---|
217 | \caption{Languages in the \cerco{} compiler} |
---|
218 | \label{fig:compilerlangs} |
---|
219 | \end{figure} |
---|
220 | |
---|
221 | The compiler uses a number of intermediate languages, as outlined the |
---|
222 | middle two lines of Figure~\ref{fig:compilerlangs}. The upper line |
---|
223 | represents the front-end of the compiler, and the lower the back-end, |
---|
224 | finishing with Intel 8051 binary code. Not all of the front-end compiler passes |
---|
225 | introduce a new language, and Figure~\ref{fig:summary} presents a |
---|
226 | list of every pass involved. |
---|
227 | |
---|
228 | \begin{figure} |
---|
229 | \begin{center} |
---|
230 | \begin{minipage}{.8\linewidth} |
---|
231 | \begin{tabbing} |
---|
232 | \quad \= $\downarrow$ \quad \= \kill |
---|
233 | \textsf{C} (unformalised)\\ |
---|
234 | \> $\downarrow$ \> CIL parser (unformalised \ocaml)\\ |
---|
235 | \textsf{Clight}\\ |
---|
236 | %\> $\downarrow$ \> add runtime functions\\ |
---|
237 | \> $\downarrow$ \> \lstinline[language=C]'switch' removal\\ |
---|
238 | \> $\downarrow$ \> labelling\\ |
---|
239 | \> $\downarrow$ \> cast removal\\ |
---|
240 | \> $\downarrow$ \> stack variable allocation and control structure |
---|
241 | simplification\\ |
---|
242 | \textsf{Cminor}\\ |
---|
243 | %\> $\downarrow$ \> generate global variable initialization code\\ |
---|
244 | \> $\downarrow$ \> transform to RTL graph\\ |
---|
245 | \textsf{RTLabs}\\ |
---|
246 | \> $\downarrow$ \> check cost labelled properties of RTL graph\\ |
---|
247 | \> $\downarrow$ \> start of target specific back-end\\ |
---|
248 | \>\quad \vdots |
---|
249 | \end{tabbing} |
---|
250 | \end{minipage} |
---|
251 | \end{center} |
---|
252 | \caption{Front-end languages and compiler passes} |
---|
253 | \label{fig:summary} |
---|
254 | \end{figure} |
---|
255 | |
---|
256 | \label{page:switchintro} |
---|
257 | The annotated source code is produced by the cost labelling phase. |
---|
258 | Note that there is a pass to replace C \lstinline[language=C]'switch' |
---|
259 | statements before labelling --- we need to remove them because the |
---|
260 | simple form of labelling used in the formalised compiler is not quite |
---|
261 | capable of capturing their execution time costs, largely due to C's |
---|
262 | `fall-through' behaviour where execution from one branch continues in |
---|
263 | the next unless there is an explicit \lstinline[language=C]'break'. |
---|
264 | |
---|
265 | The cast removal phase which follows cost labelling simplifies |
---|
266 | expressions to prevent unnecessary arithmetic promotion, which is |
---|
267 | specified by the C standard but costly for an 8-bit target. The |
---|
268 | transformation to \textsf{Cminor} and subsequently \textsf{RTLabs} |
---|
269 | bear considerable resemblance to some passes of the CompCert |
---|
270 | compiler~\cite{Blazy-Leroy-Clight-09,Leroy-backend}, although we use a simpler \textsf{Cminor} where |
---|
271 | all loops use \lstinline[language=C]'goto' statements, and the |
---|
272 | \textsf{RTLabs} language retains a target-independent flavour. The |
---|
273 | back-end takes \textsf{RTLabs} code as input. |
---|
274 | |
---|
275 | The whole compilation function returns the following information on success: |
---|
276 | \begin{lstlisting}[language=matita] |
---|
277 | record compiler_output : Type[0] := |
---|
278 | { c_labelled_object_code: labelled_object_code |
---|
279 | ; c_stack_cost: stack_cost_model |
---|
280 | ; c_max_stack: nat |
---|
281 | ; c_init_costlabel: costlabel |
---|
282 | ; c_labelled_clight: clight_program |
---|
283 | ; c_clight_cost_map: clight_cost_map |
---|
284 | }. |
---|
285 | \end{lstlisting} |
---|
286 | It consists of annotated 8051 object code, a mapping from function |
---|
287 | identifiers to the function's stack space usage, the space available for the |
---|
288 | stack after global variable allocation, a cost label covering the |
---|
289 | execution time for the initialisation of global variables and the call |
---|
290 | to the \lstinline[language=C]'main' function, the annotated source |
---|
291 | code, and finally a mapping from cost labels to actual execution time |
---|
292 | costs. |
---|
293 | |
---|
294 | An \ocaml{} pretty printer is used to provide a concrete version of |
---|
295 | the output code and annotated source code. In the case of the |
---|
296 | annotated source code, it also inserts the actual costs alongside the |
---|
297 | cost labels, and optionally adds a global cost variable and |
---|
298 | instrumentation to support further reasoning in external tools such as |
---|
299 | Frama-C. |
---|
300 | |
---|
301 | \subsection{Revisions to the prototype compiler} |
---|
302 | |
---|
303 | Our focus on intensional properties prompted us to consider whether we |
---|
304 | could incorporate stack space into the costs presented to the user. |
---|
305 | We only allocate one fixed-size frame per function, so modelling this |
---|
306 | was relatively simple. It is the only form of dynamic memory |
---|
307 | allocation provided by the compiler, so we were able to strengthen the |
---|
308 | statement of the goal to guarantee successful execution whenever the |
---|
309 | stack space obeys the \lstinline'c_max_stack' bound calculated by |
---|
310 | subtracting the global variable requirements from the total memory |
---|
311 | available. |
---|
312 | |
---|
313 | The cost labelling checks at the end of Figure~\ref{fig:summary} have been |
---|
314 | introduced to reduce the proof burden, and are described in |
---|
315 | Section~\ref{sec:costchecks}. |
---|
316 | |
---|
317 | The use of dependent types to capture simple intermediate language |
---|
318 | invariants makes every front-end pass a total function, except |
---|
319 | \textsf{Clight} to \textsf{Cminor} and the cost checks. Hence various |
---|
320 | well-formedness and type safety checks are performed only once between |
---|
321 | \textsf{Clight} and \textsf{Cminor}, and the invariants rule out any |
---|
322 | difficulties in the later stages. With the benefit of hindsight we |
---|
323 | would have included an initial checking phase to produce a |
---|
324 | `well-formed' variant of \textsf{Clight}, conjecturing that this would |
---|
325 | simplify various parts of the proofs for the \textsf{Clight} stages |
---|
326 | which deal with potentially ill-formed code. |
---|
327 | |
---|
328 | Following D2.2, we previously generated code for global variable |
---|
329 | initialisation in \textsf{Cminor}, for which we reserved a cost label |
---|
330 | to represent the execution time for initialisation. However, the |
---|
331 | back-end must also add an initial call to the main function, whose |
---|
332 | cost must also be accounted for, so we decided to move the |
---|
333 | initialisation code to the back-end and merge the costs. |
---|
334 | |
---|
335 | \subsection{Main correctness statement} |
---|
336 | |
---|
337 | Informally, our main intensional result links the time difference in a source |
---|
338 | code execution to the time difference in the object code, expressing the time |
---|
339 | for the source by summing the values for the cost labels in the trace, and the |
---|
340 | time for the target by a clock built in to the 8051 executable semantics. |
---|
341 | |
---|
342 | The availability of precise timing information for 8501 |
---|
343 | implementations and the design of the compiler allow it to give exact |
---|
344 | time costs in terms of processor cycles, not just upper bounds. |
---|
345 | However, these exact results are only available if the subtrace we |
---|
346 | measure starts and ends at suitable points. In particular, pure |
---|
347 | computation with no observable effects may be reordered and moved past |
---|
348 | cost labels, so we cannot measure time between arbitrary statements in |
---|
349 | the program. |
---|
350 | |
---|
351 | There is also a constraint on the subtraces that we |
---|
352 | measure due to the requirements of the correctness proof for the |
---|
353 | object code timing analysis. To be sure that the timings are assigned |
---|
354 | to the correct cost label, we need to know that each return from a |
---|
355 | function call must go to the correct return address. It is difficult |
---|
356 | to observe this property locally in the object code because it relies |
---|
357 | on much earlier stages in the compiler. To convey this information to |
---|
358 | the timing analysis extra structure is imposed on the subtraces, which |
---|
359 | is described in Section~\ref{sec:fegoals}. |
---|
360 | |
---|
361 | % Regarding the footnote, would there even be much point? |
---|
362 | % TODO: this might be quite easy to add ('just' subtract the |
---|
363 | % measurable subtrace from the second label to the end). Could also |
---|
364 | % measure other traces in this manner. |
---|
365 | These restrictions are reflected in the subtraces that we give timing |
---|
366 | guarantees on; they must start at a cost label and end at the return |
---|
367 | of the enclosing function of the cost label\footnote{We expect that |
---|
368 | this would generalise to more general subtraces by subtracting costs |
---|
369 | for unwanted measurable suffixes of a measurable subtrace.}. A |
---|
370 | typical example of such a subtrace is the execution of an entire |
---|
371 | function from the cost label at the start of the function until it |
---|
372 | returns. We call such any such subtrace \emph{measurable} if it (and |
---|
373 | the prefix of the trace from the start to the subtrace) can also be |
---|
374 | executed within the available stack space. |
---|
375 | |
---|
376 | Now we can give the main intensional statement for the compiler. |
---|
377 | Given a \emph{measurable} subtrace for a labelled \textsf{Clight} |
---|
378 | program, there is a subtrace of the 8051 object code program where the |
---|
379 | time differences match. Moreover, \emph{observable} parts of the |
---|
380 | trace also match --- these are the appearance of cost labels and |
---|
381 | function calls and returns. |
---|
382 | |
---|
383 | |
---|
384 | |
---|
385 | More formally, the definition of this statement in \matita{} is |
---|
386 | \begin{lstlisting}[language=matita] |
---|
387 | definition simulates := |
---|
388 | $\lambda$p: compiler_output. |
---|
389 | let initial_status := initialise_status $...$ (cm (c_labelled_object_code $...$ p)) in |
---|
390 | $\forall$m1,m2. |
---|
391 | measurable Clight_pcs (c_labelled_clight $...$ p) m1 m2 |
---|
392 | (stack_sizes (c_stack_cost $...$ p)) (c_max_stack $...$ p) $\rightarrow$ |
---|
393 | $\forall$c1,c2. |
---|
394 | clock_after Clight_pcs (c_labelled_clight $...$ p) m1 (c_clight_cost_map $...$ p) = OK $...$ c1 $\rightarrow$ |
---|
395 | clock_after Clight_pcs (c_labelled_clight $...$ p) (m1+m2) (c_clight_cost_map $...$ p) = OK $...$ c2 $\rightarrow$ |
---|
396 | $\exists$n1,n2. |
---|
397 | observables Clight_pcs (c_labelled_clight $...$ p) m1 m2 = |
---|
398 | observables (OC_preclassified_system (c_labelled_object_code $...$ p)) |
---|
399 | (c_labelled_object_code $...$ p) n1 n2 |
---|
400 | $\wedge$ |
---|
401 | clock ?? (execute (n1+n2) ? initial_status) = |
---|
402 | clock ?? (execute n1 ? initial_status) + (c2-c1). |
---|
403 | \end{lstlisting} |
---|
404 | where the \lstinline'measurable', \lstinline'clock_after' and |
---|
405 | \lstinline'observables' definitions are generic definitions for multiple |
---|
406 | languages; in this case the \lstinline'Clight_pcs' record applies them |
---|
407 | to \textsf{Clight} programs. |
---|
408 | |
---|
409 | There is a second part to the statement, which says that the initial |
---|
410 | processing of the input program to produce the cost labelled version |
---|
411 | does not affect the semantics of the program: |
---|
412 | % Yes, I'm paraphrasing the result a tiny bit to remove the observe non-function |
---|
413 | \begin{lstlisting}[language=matita] |
---|
414 | $\forall$input_program,output. |
---|
415 | compile input_program = return output $\rightarrow$ |
---|
416 | not_wrong $...$ (exec_inf $...$ clight_fullexec input_program) $\rightarrow$ |
---|
417 | sim_with_labels |
---|
418 | (exec_inf $...$ clight_fullexec input_program) |
---|
419 | (exec_inf $...$ clight_fullexec (c_labelled_clight $...$ output)) |
---|
420 | \end{lstlisting} |
---|
421 | That is, any successful compilation produces a labelled program that |
---|
422 | has identical behaviour to the original, so long as there is no |
---|
423 | `undefined behaviour'. |
---|
424 | |
---|
425 | Note that this statement provides full functional correctness, including |
---|
426 | preservation of (non-)termination. The intensional result above does |
---|
427 | not do this directly --- it does not guarantee the same result or same |
---|
428 | termination. There are two mitigating factors, however: first, to |
---|
429 | prove the intensional property you need local simulation results --- these |
---|
430 | can be pieced together to form full behavioural equivalence, only time |
---|
431 | constraints have prevented us from doing so. Second, if we wish to |
---|
432 | confirm a result, termination, or non-termination we could add an |
---|
433 | observable witness, such as a function that is only called if the |
---|
434 | correct result is given. The intensional result guarantees that the |
---|
435 | observable witness is preserved, so the program must behave correctly. |
---|
436 | |
---|
437 | These two results are combined in the the \lstinline'correct' |
---|
438 | theorem in the file \lstinline'correctness.ma'. |
---|
439 | |
---|
440 | \section{Correctness statement for the front-end} |
---|
441 | \label{sec:fegoals} |
---|
442 | |
---|
443 | The essential parts of the intensional proof were outlined during work |
---|
444 | on a toy compiler in Task |
---|
445 | 2.1~\cite{d2.1,springerlink:10.1007/978-3-642-32469-7_3}. These are |
---|
446 | \begin{enumerate} |
---|
447 | \item functional correctness, in particular preserving the trace of |
---|
448 | cost labels, |
---|
449 | \item the \emph{soundness} and \emph{precision} of the cost labelling |
---|
450 | on the object code, and |
---|
451 | \item the timing analysis on the object code produces a correct |
---|
452 | mapping from cost labels to time. |
---|
453 | \end{enumerate} |
---|
454 | |
---|
455 | However, that toy development did not include function calls. For the |
---|
456 | full \cerco{} compiler we also need to maintain the invariant that |
---|
457 | functions return to the correct program location in the caller, as we |
---|
458 | mentioned in the previous section. During work on the back-end timing |
---|
459 | analysis (describe in more detail in the companion deliverable, D4.4) |
---|
460 | the notion of a \emph{structured trace} was developed to enforce this |
---|
461 | return property, and also most of the cost labelling properties too. |
---|
462 | |
---|
463 | \begin{figure} |
---|
464 | \begin{center} |
---|
465 | \includegraphics[width=0.5\linewidth]{compiler.pdf} |
---|
466 | \end{center} |
---|
467 | \caption{The compiler and proof outline} |
---|
468 | \label{fig:compiler} |
---|
469 | \end{figure} |
---|
470 | |
---|
471 | Jointly, we generalised the structured traces to apply to any of the |
---|
472 | intermediate languages which have some idea of program counter. This means |
---|
473 | that they are introduced part way through the compiler, see |
---|
474 | Figure~\ref{fig:compiler}. Proving that a structured trace can be |
---|
475 | constructed at \textsf{RTLabs} has several virtues: |
---|
476 | \begin{itemize} |
---|
477 | \item This is the first language where every operation has its own |
---|
478 | unique, easily addressable, statement. |
---|
479 | \item Function calls and returns are still handled implicitly in the |
---|
480 | language and so the structural properties are ensured by the |
---|
481 | semantics. |
---|
482 | \item Many of the back-end languages from \textsf{RTL} onwards share a common |
---|
483 | core set of definitions, and using structured traces throughout |
---|
484 | increases this uniformity. |
---|
485 | \end{itemize} |
---|
486 | |
---|
487 | \begin{figure} |
---|
488 | \begin{center} |
---|
489 | \includegraphics[width=0.6\linewidth]{strtraces.pdf} |
---|
490 | \end{center} |
---|
491 | \caption{Nesting of functions in structured traces} |
---|
492 | \label{fig:strtrace} |
---|
493 | \end{figure} |
---|
494 | A structured trace is a mutually inductive data type which |
---|
495 | contains the steps from a normal program trace, but arranged into a |
---|
496 | nested structure which groups entire function calls together and |
---|
497 | aggregates individual steps between cost labels (or between the final |
---|
498 | cost label and the return from the function), see |
---|
499 | Figure~\ref{fig:strtrace}. This captures the idea that the cost labels |
---|
500 | only represent costs \emph{within} a function --- calls to other |
---|
501 | functions are accounted for in the nested trace for their execution, and we |
---|
502 | can locally regard function calls as a single step. |
---|
503 | |
---|
504 | These structured traces form the core part of the intermediate results |
---|
505 | that we must prove so that the back-end can complete the main |
---|
506 | intensional result stated above. In full, we provide the back-end |
---|
507 | with |
---|
508 | \begin{enumerate} |
---|
509 | \item A normal trace of the \textbf{prefix} of the program's execution |
---|
510 | before reaching the measurable subtrace. (This needs to be |
---|
511 | preserved so that we know that the stack space consumed is correct, |
---|
512 | and to set up the simulation results.) |
---|
513 | \item The \textbf{structured trace} corresponding to the measurable |
---|
514 | subtrace. |
---|
515 | \item An additional property about the structured trace that no |
---|
516 | `program counter' is \textbf{repeated} between cost labels. Together with |
---|
517 | the structure in the trace, this takes over from showing that |
---|
518 | cost labelling is sound and precise. |
---|
519 | \item A proof that the \textbf{observables} have been preserved. |
---|
520 | \item A proof that the \textbf{stack limit} is still observed by the prefix and |
---|
521 | the structure trace. (This is largely a consequence of the |
---|
522 | preservation of observables.) |
---|
523 | \end{enumerate} |
---|
524 | The \lstinline'front_end_correct' lemma in the |
---|
525 | \lstinline'correctness.ma' file provides a record containing these. |
---|
526 | |
---|
527 | Following the outline in Figure~\ref{fig:compiler}, we will first deal |
---|
528 | with the transformations in \textsf{Clight} that produce the source |
---|
529 | program with cost labels, then show that measurable traces can be |
---|
530 | lifted to \textsf{RTLabs}, and finally show that we can construct the |
---|
531 | properties listed above ready for the back-end proofs. |
---|
532 | |
---|
533 | \section{Input code to cost labelled program} |
---|
534 | \label{sec:inputtolabelling} |
---|
535 | |
---|
536 | As explained on page~\pageref{page:switchintro}, the costs of complex |
---|
537 | C \lstinline[language=C]'switch' statements cannot be represented with |
---|
538 | the simple labelling used in the formalised compiler. Our first pass |
---|
539 | replaces these statements with simpler C code, allowing our second |
---|
540 | pass to perform the cost labelling. We show that the behaviour of |
---|
541 | programs is unchanged by these passes using forward |
---|
542 | simulations\footnote{All of our languages are deterministic, which can |
---|
543 | be seen directly from their executable definitions. Thus we know that |
---|
544 | forward simulations are sufficient because the target cannot have any |
---|
545 | other behaviour.}. |
---|
546 | |
---|
547 | \subsection{Switch removal} |
---|
548 | |
---|
549 | We compile away \lstinline[language=C]'switch' statements into more |
---|
550 | basic \textsf{Clight} code. |
---|
551 | Note that this transformation does not necessarily deteriorate the |
---|
552 | efficiency of the generated code. For instance, compilers such as GCC |
---|
553 | introduce balanced trees of ``if-then-else'' constructs for small |
---|
554 | switches. However, our implementation strategy is much simpler. Let |
---|
555 | us consider the following input statement. |
---|
556 | |
---|
557 | \begin{lstlisting}[language=C] |
---|
558 | switch(e) { |
---|
559 | case v1: |
---|
560 | stmt1; |
---|
561 | case v2: |
---|
562 | stmt2; |
---|
563 | default: |
---|
564 | stmt_default; |
---|
565 | } |
---|
566 | \end{lstlisting} |
---|
567 | |
---|
568 | Note that \textsf{stmt1}, \textsf{stmt2}, \ldots \textsf{stmt\_default} |
---|
569 | may contain \lstinline[language=C]'break' statements, which have the |
---|
570 | effect of exiting the switch statement. In the absence of break, the |
---|
571 | execution falls through each case sequentially. In our implementation, |
---|
572 | we produce an equivalent sequence of ``if-then'' chained by gotos: |
---|
573 | \begin{lstlisting}[language=C] |
---|
574 | fresh = e; |
---|
575 | if(fresh == v1) { |
---|
576 | $\llbracket$stmt1$\rrbracket$; |
---|
577 | goto lbl_case2; |
---|
578 | }; |
---|
579 | if(fresh == v2) { |
---|
580 | lbl_case2: |
---|
581 | $\llbracket$stmt2$\rrbracket$; |
---|
582 | goto lbl_case2; |
---|
583 | }; |
---|
584 | $\llbracket$stmt_default$\rrbracket$; |
---|
585 | exit_label: |
---|
586 | \end{lstlisting} |
---|
587 | |
---|
588 | The proof had to tackle the following points: |
---|
589 | \begin{itemize} |
---|
590 | \item the source and target memories are not the same (due to the fresh variable), |
---|
591 | \item the flow of control is changed in a non-local way (e.g. \textbf{goto} |
---|
592 | instead of \textbf{break}). |
---|
593 | \end{itemize} |
---|
594 | In order to tackle the first point, we implemented a version of memory |
---|
595 | extensions similar to those of CompCert. |
---|
596 | |
---|
597 | For the simulation we decided to prove a sufficient amount to give us |
---|
598 | confidence in the definitions and approach, but to curtail the proof |
---|
599 | because this pass does not contribute to the intensional correctness |
---|
600 | result. We tackled several simple cases, that do not interact with |
---|
601 | the switch removal per se, to show that the definitions were usable, |
---|
602 | and part of the switch case to check that the approach is |
---|
603 | reasonable. This comprises propagating the memory extension through |
---|
604 | each statement (except switch), as well as various invariants that are |
---|
605 | needed for the switch case (in particular, freshness hypotheses). The |
---|
606 | details of the evaluation process for the source switch statement and |
---|
607 | its target counterpart can be found in the file |
---|
608 | \lstinline'switchRemoval.ma', along more details on the transformation |
---|
609 | itself. |
---|
610 | |
---|
611 | Proving the correctness of the second point would require reasoning on the |
---|
612 | semantics of \lstinline[language=C]'goto' statements. In the \textsf{Clight} |
---|
613 | semantics, this is implemented as a function-wide lookup of the target label. |
---|
614 | The invariant we would need is the fact that a global label lookup on a freshly |
---|
615 | created goto is equivalent to a local lookup. This would in turn require the |
---|
616 | propagation of some freshness hypotheses on labels. As discussed, |
---|
617 | we decided to omit this part of the correctness proof. |
---|
618 | |
---|
619 | \subsection{Cost labelling} |
---|
620 | |
---|
621 | The simulation for the cost labelling pass is the simplest in the |
---|
622 | front-end. The main argument is that any step of the source program |
---|
623 | is simulated by the same step of the labelled one, plus any extra |
---|
624 | steps for the added cost labels. The extra instructions do not change |
---|
625 | the memory or local environments, although we have to keep track of |
---|
626 | the extra instructions that appear in continuations, for example |
---|
627 | during the execution of a \lstinline[language=C]'while' loop. |
---|
628 | |
---|
629 | We do not attempt to capture any cost properties of the labelling\footnote{We describe how the cost properties are |
---|
630 | established in Section~\ref{sec:costchecks}.} in |
---|
631 | the simulation proof, which allows the proof to be oblivious to the choice |
---|
632 | of cost labels. Hence we do not have to reason about the threading of |
---|
633 | name generation through the labelling function, greatly reducing the |
---|
634 | amount of effort required. |
---|
635 | |
---|
636 | %TODO: both give one-step-sim-by-many forward sim results; switch |
---|
637 | %removal tricky, uses aux var to keep result of expr, not central to |
---|
638 | %intensional correctness so curtailed proof effort once reasonable |
---|
639 | %level of confidence in code gained; labelling much simpler; don't care |
---|
640 | %what the labels are at this stage, just need to know when to go |
---|
641 | %through extra steps. Rolled up into a single result with a cofixpoint |
---|
642 | %to obtain coinductive statement of equivalence (show). |
---|
643 | |
---|
644 | \section{Finding corresponding measurable subtraces} |
---|
645 | \label{sec:measurablelifting} |
---|
646 | |
---|
647 | There follow the three main passes of the front-end: |
---|
648 | \begin{enumerate} |
---|
649 | \item simplification of casts in \textsf{Clight} code |
---|
650 | \item \textsf{Clight} to \textsf{Cminor} translation, performing stack |
---|
651 | variable allocation and simplifying control structures |
---|
652 | \item transformation to \textsf{RTLabs} control flow graph |
---|
653 | \end{enumerate} |
---|
654 | We have taken a common approach to |
---|
655 | each pass: first we build (or axiomatise) forward simulation results |
---|
656 | that are similar to normal compiler proofs, but which are slightly more |
---|
657 | fine-grained so that we can see that the call structure and relative |
---|
658 | placement of cost labels is preserved. |
---|
659 | |
---|
660 | Then we instantiate a general result which shows that we can find a |
---|
661 | \emph{measurable} subtrace in the target of the pass that corresponds |
---|
662 | to the measurable subtrace in the source. By repeated application of |
---|
663 | this result we can find a measurable subtrace of the execution of the |
---|
664 | \textsf{RTLabs} code, suitable for the construction of a structured |
---|
665 | trace (see Section~\ref{sec:structuredtrace}). This is essentially an |
---|
666 | extra layer on top of the simulation proofs that provides us with the |
---|
667 | additional information required for our intensional correctness proof. |
---|
668 | |
---|
669 | \subsection{Generic measurable subtrace lifting proof} |
---|
670 | |
---|
671 | Our generic proof is parametrised on a record containing small-step |
---|
672 | semantics for the source and target language, a classification of |
---|
673 | states (the same form of classification is used when defining |
---|
674 | structured traces), a simulation relation which respects the |
---|
675 | classification and cost labelling and |
---|
676 | four simulation results. The simulations are split by the starting state's |
---|
677 | classification and whether it is a cost label, which will allow us to |
---|
678 | observe that the call structure is preserved. They are: |
---|
679 | \begin{enumerate} |
---|
680 | \item a step from a `normal' state (which is not classified as a call |
---|
681 | or return) which is not a cost label is simulated by zero or more |
---|
682 | `normal' steps; |
---|
683 | \item a step from a `call' state followed by a cost label step is |
---|
684 | simulated by a step from a `call' state, a corresponding label step, |
---|
685 | then zero or more `normal' steps; |
---|
686 | \item a step from a `call' state not followed by a cost label |
---|
687 | similarly (note that this case cannot occur in a well-labelled |
---|
688 | program, but we do not have enough information locally to exploit |
---|
689 | this); and |
---|
690 | \item a cost label step is simulated by a cost label step. |
---|
691 | \end{enumerate} |
---|
692 | Finally, we need to know that a successfully translated program will |
---|
693 | have an initial state in the simulation relation with the original |
---|
694 | program's initial state. |
---|
695 | |
---|
696 | The back-end has similar requirements for lifting simulations to |
---|
697 | structured traces. Fortunately, our treatment of calls and returns |
---|
698 | can be slightly simpler because we have special call and return states |
---|
699 | that correspond to function entry and return that are separate from |
---|
700 | the actual instructions. This was originally inherited from our port |
---|
701 | of CompCert's \textsf{Clight} semantics, but proves useful here |
---|
702 | because we only need to consider adding extra steps \emph{after} a |
---|
703 | call or return state, because the instruction step deals with extra |
---|
704 | steps that occur before. The back-end makes all of the call and |
---|
705 | return machinery explicit, and thus needs more complex statements |
---|
706 | about extra steps before and after each call and return. |
---|
707 | |
---|
708 | \begin{figure} |
---|
709 | \begin{center} |
---|
710 | \includegraphics[width=0.5\linewidth]{meassim.pdf} |
---|
711 | \end{center} |
---|
712 | \caption{Tiling of simulation for a measurable subtrace} |
---|
713 | \label{fig:tiling} |
---|
714 | \end{figure} |
---|
715 | |
---|
716 | To find the measurable subtrace in the target program's execution we |
---|
717 | walk along the original program's execution trace applying the |
---|
718 | appropriate simulation result by induction on the number of steps. |
---|
719 | While the number of steps taken varies, the overall structure is |
---|
720 | preserved, as illustrated in Figure~\ref{fig:tiling}. By preserving |
---|
721 | the structure we also maintain the same intensional observables. One |
---|
722 | delicate point is that the cost label following a call must remain |
---|
723 | directly afterwards\footnote{The prototype compiler allowed some |
---|
724 | straight-line code to appear before the cost label until a later |
---|
725 | stage of the compiler, but we must move the requirement forward to |
---|
726 | fit with the structured traces.} |
---|
727 | % Damn it, I should have just moved the cost label forwards in RTLabs, |
---|
728 | % like the prototype does in RTL to ERTL; the result would have been |
---|
729 | % simpler. Or was there some reason not to do that? |
---|
730 | (both in the program code and in the execution trace), even if we |
---|
731 | introduce extra steps, for example to store parameters in memory in |
---|
732 | \textsf{Cminor}. Thus we have a version of the call simulation |
---|
733 | that deals with both the call and the cost label in one result. |
---|
734 | |
---|
735 | In addition to the subtrace we are interested in measuring, we must |
---|
736 | prove that the earlier part of the trace is also preserved in |
---|
737 | order to use the simulation from the initial state. This proof also |
---|
738 | guarantees that we do not run out of stack space before the subtrace |
---|
739 | we are interested in. The lemmas for this prefix and the measurable |
---|
740 | subtrace are similar, following the pattern above. However, the |
---|
741 | measurable subtrace also requires us to rebuild the termination |
---|
742 | proof. This is defined recursively: |
---|
743 | \label{prog:terminationproof} |
---|
744 | \begin{lstlisting}[language=matita] |
---|
745 | let rec will_return_aux C (depth:nat) |
---|
746 | (trace:list (cs_state $...$ C $\times$ trace)) on trace : bool := |
---|
747 | match trace with |
---|
748 | [ nil $\Rightarrow$ false |
---|
749 | | cons h tl $\Rightarrow$ |
---|
750 | let $\langle$s,tr$\rangle$ := h in |
---|
751 | match cs_classify C s with |
---|
752 | [ cl_call $\Rightarrow$ will_return_aux C (S depth) tl |
---|
753 | | cl_return $\Rightarrow$ |
---|
754 | match depth with |
---|
755 | [ O $\Rightarrow$ match tl with [ nil $\Rightarrow$ true | _ $\Rightarrow$ false ] |
---|
756 | | S d $\Rightarrow$ will_return_aux C d tl |
---|
757 | ] |
---|
758 | | _ $\Rightarrow$ will_return_aux C depth tl |
---|
759 | ] |
---|
760 | ]. |
---|
761 | \end{lstlisting} |
---|
762 | The \lstinline'depth' is the number of return states we need to see |
---|
763 | before we have returned to the original function (initially zero) and |
---|
764 | \lstinline'trace' the measurable subtrace obtained from the running |
---|
765 | the semantics for the correct number of steps. This definition |
---|
766 | unfolds tail recursively for each step, and once the corresponding |
---|
767 | simulation result has been applied a new one for the target can be |
---|
768 | asserted by unfolding and applying the induction hypothesis on the |
---|
769 | shorter trace. |
---|
770 | |
---|
771 | Combining the lemmas about the prefix and the measurable subtrace |
---|
772 | requires a little care because the states joining the two might not be |
---|
773 | related in the simulation. In particular, if the measurable subtrace |
---|
774 | starts from the cost label at the beginning of the function there may |
---|
775 | be some extra instructions in the target code to execute to complete |
---|
776 | function entry before the states are back in the relation. Hence we |
---|
777 | carefully phrased the lemmas to allow for such extra steps. |
---|
778 | |
---|
779 | Together, these then gives us an overall result for any simulation fitting the |
---|
780 | requirements above (contained in the \lstinline'meas_sim' record): |
---|
781 | \begin{lstlisting}[language=matita] |
---|
782 | theorem measured_subtrace_preserved : |
---|
783 | $\forall$MS:meas_sim. |
---|
784 | $\forall$p1,p2,m,n,stack_cost,max. |
---|
785 | ms_compiled MS p1 p2 $\rightarrow$ |
---|
786 | measurable (ms_C1 MS) p1 m n stack_cost max $\rightarrow$ |
---|
787 | $\exists$m',n'. |
---|
788 | measurable (ms_C2 MS) p2 m' n' stack_cost max $\wedge$ |
---|
789 | observables (ms_C1 MS) p1 m n = observables (ms_C2 MS) p2 m' n'. |
---|
790 | \end{lstlisting} |
---|
791 | The stack space requirement that is embedded in \lstinline'measurable' |
---|
792 | is a consequence of the preservation of observables, because it is |
---|
793 | determined by the functions called and returned from, which are observable. |
---|
794 | |
---|
795 | \subsection{Simulation results for each pass} |
---|
796 | |
---|
797 | We now consider the simulation results for the passes, each of which |
---|
798 | is used to instantiate the |
---|
799 | \lstinline[language=matita]'measured_subtrace_preserved' theorem to |
---|
800 | construct the measurable subtrace for the next language. |
---|
801 | |
---|
802 | \subsubsection{Cast simplification} |
---|
803 | |
---|
804 | The parser used in \cerco{} introduces a lot of explicit type casts. |
---|
805 | If left as they are, these constructs can greatly hamper the |
---|
806 | quality of the generated code -- especially as the architecture |
---|
807 | we consider is an $8$-bit one. In \textsf{Clight}, casts are |
---|
808 | expressions. Hence, most of the work of this transformation |
---|
809 | proceeds on expressions. The transformation proceeds by recursively |
---|
810 | trying to coerce an expression to a particular integer type, which |
---|
811 | is in practice smaller than the original one. This functionality |
---|
812 | is implemented by two mutually recursive functions whose signature |
---|
813 | is the following. |
---|
814 | |
---|
815 | \begin{lstlisting}[language=matita] |
---|
816 | let rec simplify_expr (e:expr) (target_sz:intsize) (target_sg:signedness) |
---|
817 | : $\Sigma$result:bool$\times$expr. |
---|
818 | $\forall$ge,en,m. simplify_inv ge en m e (\snd result) target_sz target_sg (\fst result) := $\ldots$ |
---|
819 | |
---|
820 | and simplify_inside (e:expr) : $\Sigma$result:expr. conservation e result := $\ldots$ |
---|
821 | \end{lstlisting} |
---|
822 | |
---|
823 | The \textsf{simplify\_inside} acts as a wrapper for |
---|
824 | \textsf{simplify\_expr}. Whenever \textsf{simplify\_inside} encounters |
---|
825 | a \textsf{Ecast} expression, it tries to coerce the sub-expression |
---|
826 | to the desired type using \textsf{simplify\_expr}, which tries to |
---|
827 | perform the actual coercion. In return, \textsf{simplify\_expr} calls |
---|
828 | back \textsf{simplify\_inside} in some particular positions, where we |
---|
829 | decided to be conservative in order to simplify the proofs. However, |
---|
830 | the current design allows to incrementally revert to a more aggressive |
---|
831 | version, by replacing recursive calls to \textsf{simplify\_inside} by |
---|
832 | calls to \textsf{simplify\_expr} \emph{and} proving the corresponding |
---|
833 | invariants -- where possible. |
---|
834 | |
---|
835 | The \textsf{simplify\_inv} invariant encodes either the conservation |
---|
836 | of the semantics during the transformation corresponding to the failure |
---|
837 | of the coercion (\textsf{Inv\_eq} constructor), or the successful |
---|
838 | downcast of the considered expression to the target type |
---|
839 | (\textsf{Inv\_coerce\_ok}). |
---|
840 | |
---|
841 | \begin{lstlisting}[language=matita] |
---|
842 | inductive simplify_inv |
---|
843 | (ge : genv) (en : env) (m : mem) |
---|
844 | (e1 : expr) (e2 : expr) (target_sz : intsize) (target_sg : signedness) : bool $\rightarrow$ Prop := |
---|
845 | | Inv_eq : $\forall$result_flag. $\ldots$ |
---|
846 | simplify_inv ge en m e1 e2 target_sz target_sg result_flag |
---|
847 | | Inv_coerce_ok : $\forall$src_sz,src_sg. |
---|
848 | typeof e1 = Tint src_sz src_sg $\rightarrow$ |
---|
849 | typeof e2 = Tint target_sz target_sg $\rightarrow$ |
---|
850 | smaller_integer_val src_sz target_sz src_sg (exec_expr ge en m e1) (exec_expr ge en m e2) $\rightarrow$ |
---|
851 | simplify_inv ge en m e1 e2 target_sz target_sg true. |
---|
852 | \end{lstlisting} |
---|
853 | |
---|
854 | The \textsf{conservation} invariant for \textsf{simplify\_inside} simply states the conservation |
---|
855 | of the semantics, as in the \textsf{Inv\_eq} constructor of the previous |
---|
856 | invariant. |
---|
857 | |
---|
858 | \begin{lstlisting}[language=matita] |
---|
859 | definition conservation := $\lambda$e,result. $\forall$ge,en,m. |
---|
860 | res_sim ? (exec_expr ge en m e) (exec_expr ge en m result) |
---|
861 | $\wedge$ res_sim ? (exec_lvalue ge en m e) (exec_lvalue ge en m result) |
---|
862 | $\wedge$ typeof e = typeof result. |
---|
863 | \end{lstlisting} |
---|
864 | |
---|
865 | This invariant is then easily lifted to statement evaluations. |
---|
866 | The main problem encountered with this particular pass was dealing with |
---|
867 | inconsistently typed programs, a canonical case being a particular |
---|
868 | integer constant of a certain size typed with another size. This |
---|
869 | prompted the need to introduce numerous type checks, making |
---|
870 | both the implementation and the proof more complex, even though more |
---|
871 | comprehensive checks are made in the next stage. |
---|
872 | %\todo{Make this a particular case of the more general statement on baking more invariants in the Clight language} |
---|
873 | |
---|
874 | \subsubsection{Clight to Cminor} |
---|
875 | |
---|
876 | This pass is the last one operating on the \textsf{Clight} language. |
---|
877 | Its input is a full \textsf{Clight} program, and its output is a |
---|
878 | \textsf{Cminor} program. Note that we do not use an equivalent of |
---|
879 | CompCert's \textsf{C\#minor} language: we translate directly to a |
---|
880 | variant of \textsf{Cminor}. This presents the advantage of not |
---|
881 | requiring the special loop constructs, nor the explicit block |
---|
882 | structure. Another salient point of our approach is that a significant |
---|
883 | number of the properties needed for the simulation proof were directly |
---|
884 | encoded in dependently typed translation functions. In particular, |
---|
885 | freshness conditions and well-typedness conditions are included. The |
---|
886 | main effects of the transformation from \textsf{Clight} to |
---|
887 | \textsf{Cminor} are listed below. |
---|
888 | |
---|
889 | \begin{itemize} |
---|
890 | \item Variables are classified as being either globals, stack-allocated |
---|
891 | locals or potentially register-allocated locals. The value of register-allocated |
---|
892 | local variables is moved out of the modelled memory and stored in a |
---|
893 | dedicated environment. |
---|
894 | \item In \textsf{Clight}, each local variable has a dedicated memory block, whereas |
---|
895 | stack-allocated locals are bundled together on a function-by-function basis. |
---|
896 | \item Loops are converted to jumps. |
---|
897 | \end{itemize} |
---|
898 | |
---|
899 | The first two points require memory injections which are more flexible that those |
---|
900 | needed in the switch removal case. In the remainder of this section, we briefly |
---|
901 | discuss our implementation of memory injections, and then the simulation proof. |
---|
902 | |
---|
903 | \paragraph{Memory injections.} |
---|
904 | |
---|
905 | Our memory injections are modelled after the work of Blazy \& Leroy. |
---|
906 | However, the corresponding paper is based on the first version of the |
---|
907 | CompCert memory model~\cite{2008-Leroy-Blazy-memory-model}, whereas we use a much more concrete model, allowing byte-level |
---|
908 | manipulations (as in the later version of CompCert's memory model). We proved |
---|
909 | roughly 80 \% of the required lemmas. Notably, some of the difficulties encountered were |
---|
910 | due to overly relaxed conditions on pointer validity (fixed during development). |
---|
911 | Some more side conditions had to be added to take care of possible overflows when converting |
---|
912 | from \textbf{Z} block bounds to $16$ bit pointer offsets (in practice, such overflows |
---|
913 | only occur in edge cases that are easily ruled out -- but this fact is not visible |
---|
914 | in memory injections). Concretely, some of the lemmas on the preservation of simulation of |
---|
915 | loads after writes were axiomatised, due to a lack of time. |
---|
916 | |
---|
917 | \paragraph{Simulation proof.} |
---|
918 | |
---|
919 | We proved the simulation result for expressions and a representative |
---|
920 | selection of statements. In particular we tackled |
---|
921 | \lstinline[language=C]'while' statements to ensure that we correctly |
---|
922 | translate loops because our approach differs from CompCert by |
---|
923 | converting directly to \textsf{Cminor} \lstinline[language=C]'goto's |
---|
924 | rather than maintaining a notion of loop in \textsf{Cminor}. We also have a partial |
---|
925 | proof for function entry, covering the setup of the memory injection, |
---|
926 | but not function exit. Exits, and the remaining statements, have been |
---|
927 | axiomatised. |
---|
928 | |
---|
929 | Careful management of the proof state was required because proof terms |
---|
930 | are embedded in \textsf{Cminor} code to show that invariants are |
---|
931 | respected. These proof terms appear in the proof state when inverting |
---|
932 | the translation functions, and they can be large and awkward. While |
---|
933 | generalising them away is usually sufficient, it can be difficult when |
---|
934 | they appear under a binder. |
---|
935 | |
---|
936 | %The correctness proof for this transformation was not completed. We proved the |
---|
937 | %simulation result for expressions and for some subset of the critical statement cases. |
---|
938 | %Notably lacking are the function entry and exit, where the memory injection is |
---|
939 | %properly set up. As would be expected, a significant amount of work has to be performed |
---|
940 | %to show the conservation of all invariants at each simulation step. |
---|
941 | |
---|
942 | %\todo{list cases, explain while loop, explain labeling problem} |
---|
943 | |
---|
944 | \subsubsection{Cminor to RTLabs} |
---|
945 | |
---|
946 | The translation from \textsf{Cminor} to \textsf{RTLabs} is a fairly |
---|
947 | routine control flow graph (CFG) construction. As such, we chose to |
---|
948 | axiomatise the associated extensional simulation results. However, we did prove several |
---|
949 | properties of the generated programs: |
---|
950 | \begin{itemize} |
---|
951 | \item All statements are type correct with respect to the declared |
---|
952 | pseudo-register type environment. |
---|
953 | \item The CFG is closed, and has a distinguished entry node and a |
---|
954 | unique exit node. |
---|
955 | \end{itemize} |
---|
956 | |
---|
957 | These properties rely on similar properties about type safety and the |
---|
958 | presence of \lstinline[language=C]'goto'-labels for \textsf{Cminor} programs |
---|
959 | which are checked at the preceding stage. As a result, this |
---|
960 | transformation is total and any compilation failures must occur when |
---|
961 | the corresponding \textsf{Clight} source is available and a better |
---|
962 | error message can be generated. |
---|
963 | |
---|
964 | The proof obligations for these properties include many instances of |
---|
965 | graph inclusion. We automated these proofs using a small amount of |
---|
966 | reflection, making the obligations much easier to handle. One |
---|
967 | drawback to enforcing invariants throughout is that temporarily |
---|
968 | breaking them can be awkward. For example, \lstinline'return' |
---|
969 | statements were originally used as placeholders for |
---|
970 | \lstinline[language=C]'goto' destinations that had not yet been |
---|
971 | translated. However, this made establishing the single exit node |
---|
972 | property rather difficult, and a different placeholder was chosen |
---|
973 | instead. In other circumstances it is possible to prove a more |
---|
974 | complex invariant then simplify it at the end of the transformation. |
---|
975 | |
---|
976 | \section{Checking cost labelling properties} |
---|
977 | \label{sec:costchecks} |
---|
978 | |
---|
979 | Ideally, we would provide proofs that the cost labelling pass always |
---|
980 | produces programs that are soundly and precisely labelled and that |
---|
981 | each subsequent pass preserves these properties. This would match our |
---|
982 | use of dependent types to eliminate impossible sources of errors |
---|
983 | during compilation, in particular retaining intermediate language type |
---|
984 | information. |
---|
985 | |
---|
986 | However, given the limited amount of time available we realised that |
---|
987 | implementing a compile-time check for a sound and precise labelling of |
---|
988 | the \textsf{RTLabs} intermediate code would reduce the proof burden |
---|
989 | considerably. This is similar in spirit to the use of translation |
---|
990 | validation in certified compilation, which makes a similar trade-off |
---|
991 | between the potential for compile-time failure and the volume of proof |
---|
992 | required. |
---|
993 | |
---|
994 | The check cannot be pushed into a later stage of the compiler because |
---|
995 | much of the information is embedded into the structured traces. |
---|
996 | However, if an alternative method was used to show that function |
---|
997 | returns in the compiled code are sufficiently well-behaved, then we |
---|
998 | could consider pushing the cost property checks into the timing |
---|
999 | analysis itself. We leave this as a possible area for future work. |
---|
1000 | |
---|
1001 | \subsection{Implementation and correctness} |
---|
1002 | \label{sec:costchecksimpl} |
---|
1003 | |
---|
1004 | For a cost labelling to be sound and precise we need a cost label at |
---|
1005 | the start of each function, after each branch and at least one in |
---|
1006 | every loop. The first two parts are trivial to check by examining the |
---|
1007 | code. In \textsf{RTLabs} the last part is specified by saying |
---|
1008 | that there is a bound on the number of successive instruction nodes in |
---|
1009 | the CFG that you can follow before you encounter a cost label, and |
---|
1010 | checking this is more difficult. |
---|
1011 | |
---|
1012 | The implementation progresses through the set of nodes in the graph, |
---|
1013 | following successors until a cost label is found or a label-free cycle |
---|
1014 | is discovered (in which case the property does not hold and we return |
---|
1015 | an error). This is made easier by the prior knowledge that every |
---|
1016 | successor of a branch instruction is a cost label, so we do not need |
---|
1017 | to search each branch. When a label is found, we remove the chain of |
---|
1018 | program counters from the set and continue from another node in the |
---|
1019 | set until it is empty, at which point we know that there is a bound |
---|
1020 | for every node in the graph. |
---|
1021 | |
---|
1022 | Directly reasoning about the function that implements this procedure would be |
---|
1023 | rather awkward, so an inductive specification of a single step of its |
---|
1024 | behaviour was written and proved to match the implementation. This |
---|
1025 | was then used to prove the implementation sound and complete. |
---|
1026 | |
---|
1027 | While we have not attempted to prove that the cost labelled properties |
---|
1028 | are established and preserved earlier in the compiler, we expect that |
---|
1029 | the effort for the \textsf{Cminor} to \textsf{RTLabs} stage alone |
---|
1030 | would be similar to the work outlined above, because it involves the |
---|
1031 | change from requiring a cost label at particular positions to |
---|
1032 | requiring cost labels to break loops in the CFG. As there are another |
---|
1033 | three passes to consider (including the labelling itself), we believe |
---|
1034 | that using the check above is much simpler overall. |
---|
1035 | |
---|
1036 | % TODO? Found some Clight to Cminor bugs quite quickly |
---|
1037 | |
---|
1038 | \section{Existence of a structured trace} |
---|
1039 | \label{sec:structuredtrace} |
---|
1040 | |
---|
1041 | The \emph{structured trace} idea introduced in |
---|
1042 | Section~\ref{sec:fegoals} enriches the execution trace of a program by |
---|
1043 | nesting function calls in a mixed-step style and embedding the cost |
---|
1044 | labelling properties of the program. See Figure~\ref{fig:strtrace} on |
---|
1045 | page~\pageref{fig:strtrace} for an illustration of a structured trace. |
---|
1046 | It was originally designed to support the proof of correctness for the |
---|
1047 | timing analysis of the object code in the back-end, then generalised |
---|
1048 | to provide a common structure to use from the end of the front-end to |
---|
1049 | the object code. |
---|
1050 | |
---|
1051 | To make the definition generic we abstract over the semantics of the |
---|
1052 | language, |
---|
1053 | \begin{lstlisting}[language=matita] |
---|
1054 | record abstract_status : Type[1] := |
---|
1055 | { as_status :> Type[0] |
---|
1056 | ; as_execute : relation as_status |
---|
1057 | ; as_pc : DeqSet |
---|
1058 | ; as_pc_of : as_status $\rightarrow$ as_pc |
---|
1059 | ; as_classify : as_status $\rightarrow$ status_class |
---|
1060 | ; as_label_of_pc : as_pc $\rightarrow$ option costlabel |
---|
1061 | ; as_after_return : ($\Sigma$s:as_status. as_classify s = cl_call) $\rightarrow$ as_status $\rightarrow$ Prop |
---|
1062 | ; as_result: as_status $\rightarrow$ option int |
---|
1063 | ; as_call_ident : ($\Sigma$s:as_status.as_classify s = cl_call) $\rightarrow$ ident |
---|
1064 | ; as_tailcall_ident : ($\Sigma$s:as_status.as_classify s = cl_tailcall) $\rightarrow$ ident |
---|
1065 | }. |
---|
1066 | \end{lstlisting} |
---|
1067 | which requires a type of states, an execution relation\footnote{All of |
---|
1068 | our semantics are executable, but using a relation was simpler in |
---|
1069 | the abstraction.}, some notion of abstract |
---|
1070 | program counter with decidable equality, the classification of states, |
---|
1071 | and functions to extract the observable intensional information (cost |
---|
1072 | labels and the identity of functions that are called). The |
---|
1073 | \lstinline'as_after_return' property links the state before a function |
---|
1074 | call with the state after return, providing the evidence that |
---|
1075 | execution returns to the correct place. The precise form varies |
---|
1076 | between stages; in \textsf{RTLabs} it insists the CFG, the pointer to |
---|
1077 | the CFG node to execute next, and some call stack information is |
---|
1078 | preserved. |
---|
1079 | |
---|
1080 | The structured traces are defined using three mutually inductive |
---|
1081 | types. The core data structure is \lstinline'trace_any_label', which |
---|
1082 | captures some straight-line execution until the next cost label or the |
---|
1083 | return from the enclosing function. Any function calls are embedded as |
---|
1084 | a single step, with its own trace nested inside and the before and |
---|
1085 | after states linked by \lstinline'as_after_return'; and states |
---|
1086 | classified as a `jump' (in particular branches) must be followed by a |
---|
1087 | cost label. |
---|
1088 | |
---|
1089 | The second type, \lstinline'trace_label_label', is a |
---|
1090 | \lstinline'trace_any_label' where the initial state is cost labelled. |
---|
1091 | Thus a trace in this type identifies a series of steps whose cost is |
---|
1092 | entirely accounted for by the label at the start. |
---|
1093 | |
---|
1094 | Finally, \lstinline'trace_label_return' is a sequence of |
---|
1095 | \lstinline'trace_label_label' values which end in the return from the |
---|
1096 | function. These correspond to a measurable subtrace, and in |
---|
1097 | particular include executions of an entire function call (and so are |
---|
1098 | used for the nested calls in \lstinline'trace_any_label'). |
---|
1099 | |
---|
1100 | \subsection{Construction} |
---|
1101 | |
---|
1102 | The construction of the structured trace replaces syntactic cost |
---|
1103 | labelling properties, which place requirements on where labels appear |
---|
1104 | in the program, with semantic properties that constrain the execution |
---|
1105 | traces of the program. The construction begins by defining versions |
---|
1106 | of the sound and precise labelling properties on states and global |
---|
1107 | environments (for the code that appears in each of them) rather than |
---|
1108 | whole programs, and showing that these are preserved by steps of the |
---|
1109 | \textsf{RTLabs} semantics. |
---|
1110 | |
---|
1111 | Then we show that each cost labelling property required by the |
---|
1112 | definition of structured traces is locally satisfied. These proofs are |
---|
1113 | broken up by the classification of states. Similarly, we prove a |
---|
1114 | step-by-step stack preservation result, which states that the |
---|
1115 | \textsf{RTLabs} semantics never changes the lower parts of the stack. |
---|
1116 | |
---|
1117 | The core part of the construction of a structured trace is to use the |
---|
1118 | proof of termination from the measurable trace (defined on |
---|
1119 | page~\pageref{prog:terminationproof}) to `fold up' the execution into |
---|
1120 | the nested form. The results outlined above fill in the proof |
---|
1121 | obligations for the cost labelling properties and the stack |
---|
1122 | preservation result shows that calls return to the correct location. |
---|
1123 | |
---|
1124 | The structured trace alone is not sufficient to capture the property |
---|
1125 | that the program is soundly labelled. While the structured trace |
---|
1126 | guarantees termination, it still permits a loop to be executed a |
---|
1127 | finite number of times without encountering a cost label. We |
---|
1128 | eliminate this by proving that no `program counter' repeats within any |
---|
1129 | \lstinline'trace_any_label' section by showing that it is incompatible |
---|
1130 | with the property that there is a bound on the number of successor |
---|
1131 | instructions you can follow in the CFG before you encounter a cost |
---|
1132 | label (from Section~\ref{sec:costchecksimpl}). |
---|
1133 | |
---|
1134 | \subsubsection{Complete execution structured traces} |
---|
1135 | |
---|
1136 | The development of the construction above started relatively early, |
---|
1137 | before the measurable subtrace preservation proofs. To be confident |
---|
1138 | that the traces were well-formed at that time, we also developed a |
---|
1139 | complete execution form that embeds the traces above. This includes |
---|
1140 | non-terminating program executions, where an infinite number of the terminating |
---|
1141 | structured traces are embedded. This construction confirmed that our |
---|
1142 | definition of structured traces was consistent, although we later |
---|
1143 | found that we did not require the whole execution version for the |
---|
1144 | compiler correctness results. |
---|
1145 | |
---|
1146 | To construct these we need to know whether each function call will |
---|
1147 | eventually terminate, requiring the use of the excluded middle. This |
---|
1148 | classical reasoning is local to the construction of whole program |
---|
1149 | traces and is not necessary for our main results. |
---|
1150 | |
---|
1151 | \section{Conclusion} |
---|
1152 | |
---|
1153 | In combination with the work on the CerCo back-end and by |
---|
1154 | concentrating on the novel intensional parts of the proof, we have |
---|
1155 | shown that it is possible to construct certifying compilers that |
---|
1156 | correctly report execution time and stack space costs. The layering |
---|
1157 | of intensional correctness proofs on top of normal simulation results |
---|
1158 | provides a useful separation of concerns, and could permit the reuse |
---|
1159 | of existing results. |
---|
1160 | |
---|
1161 | \appendix |
---|
1162 | |
---|
1163 | \section{Files} |
---|
1164 | |
---|
1165 | The following table gives a high-level overview of the \matita{} |
---|
1166 | source files in Deliverable 3.4: |
---|
1167 | |
---|
1168 | \bigskip |
---|
1169 | |
---|
1170 | \begin{tabular}{rp{.7\linewidth}} |
---|
1171 | \lstinline'compiler.ma' & Top-level compiler definitions, in particular |
---|
1172 | \lstinline'front_end', and the whole compiler definition |
---|
1173 | \lstinline'compile'. \\ |
---|
1174 | \lstinline'correctness.ma' & Correctness results: \lstinline'front_end_correct' |
---|
1175 | and \lstinline'correct', respectively. \\ |
---|
1176 | \lstinline'Clight/*' & \textsf{Clight}: proofs for switch |
---|
1177 | removal, cost labelling, cast simplification and conversion to |
---|
1178 | \textsf{Cminor}. \\ |
---|
1179 | \lstinline'Cminor/*' & \textsf{Cminor}: axioms of conversion to |
---|
1180 | \textsf{RTLabs}. \\ |
---|
1181 | \lstinline'RTLabs/*' & \textsf{RTLabs}: definitions and proofs for |
---|
1182 | compile-time cost labelling checks, construction of structured traces. |
---|
1183 | \\ |
---|
1184 | \lstinline'common/Measurable.ma' & Definitions for measurable |
---|
1185 | subtraces. \\ |
---|
1186 | \lstinline'common/FEMeasurable.ma' & Generic measurable subtrace |
---|
1187 | lifting proof. \\ |
---|
1188 | \lstinline'common/*' & Other common definitions relevant to many parts |
---|
1189 | of the compiler and proof. \\ |
---|
1190 | \lstinline'utilities/*' & General purpose definitions used throughout, |
---|
1191 | including extensions to the standard \matita{} library. |
---|
1192 | \end{tabular} |
---|
1193 | |
---|
1194 | \bibliographystyle{plain} |
---|
1195 | \bibliography{report} |
---|
1196 | |
---|
1197 | \end{document} |
---|