Changeset 2087


Ignore:
Timestamp:
Jun 15, 2012, 11:40:25 AM (5 years ago)
Author:
mulligan
Message:

Tidied up the paper, added a few more things, tidied and expanded bibliography.

Location:
src/ASM/CPP2012-asm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/ASM/CPP2012-asm/cpp-2012-asm.bib

    r2083 r2087  
    2525  author = {Gerwin Klein and Tobias Nipkow},
    2626  title = {A machine-checked model for a {Java-like} language, virtual machine and compiler},
    27   journal = {{TOPLAS}},
     27  journal = {{ACM} Transactions on Programming Languages and Systems},
    2828  volume = {28},
    2929  number = {4},
     
    3636  author = {Gerwin Klein and June Andronick and Kevin Elphinstone and Gernot Heiser and David Cock and Philip Derrin and Dhammika Elkaduwe and Kai Engelhardt and Rafal Kolanski and Michael Norrish and Thomas Sewell, Harvey Tuch and Simon Winwood},
    3737  title = {{seL4}: Formal verification of an operating system kernel},
    38   journal = {{CACM}},
     38  journal = {Communications of the {ACM}},
    3939  issue = {6},
    4040  volume = {53},
     
    4747  author = {Xavier Leroy},
    4848  title = {Formal verification of a realistic compiler},
    49   journal = {{CACM}},
     49  journal = {Communications of the {ACM}},
    5050  volume = {52},
    5151  number = {7},
     
    8080  author = {Robert Atkey},
    8181  title = {{CoqJVM}: An executable specification of the {Java Virtual Machine} using dependent types},
    82   booktitle = {{TYPES}},
     82  booktitle = {Types for Proofs and Programs},
    8383  pages = {18--32},
    8484  year = {2007}
     
    9898  author = {Jaap Boender and Claudio {Sacerdoti Coen}},
    9999  title = {On the correctness of a branch displacement algorithm},
    100   booktitle = {{CPP}},
     100  booktitle = {Certified Proofs and Programs {(CPP)}},
    101101  year =  {2012},
    102102  note = {Submitted}
     
    116116  author = {Harvey Tuch and Gerwin Klein and Michael Norrish},
    117117  title = {Types, Bytes, and Separation Logic},
    118   booktitle = {{POPL}},
     118  booktitle = {Principles of Programming Languages {(POPL)}},
    119119  pages = {97--108},
    120120  year =  {2007}
     
    126126  author = {Gerwin Klein and June Andronick and Kevin Elphinstone and Gernot Heiser and David Cock and Philip Derrin and Dhammika Elkaduwe and Kai Engelhardt and Rafal Kolanski and Michael Norrish and Thomas Sewell, Harvey Tuch and Simon Winwood},
    127127  title = {{seL4}: Formal verification of an operating system kernel},
    128   booktitle = {{SOSP}},
     128  booktitle = {{ACM} Symposium on Operating Systems Principles {(SOSP)}},
    129129  year = {2009}
    130130}
     
    152152  author = {Matthieu Sozeau},
    153153  title = {Subset Coercions in {Coq}},
    154   booktitle = {{TYPES}},
     154  booktitle = {Types for proofs and programs},
    155155  pages = {237--252},
    156156  year = {2006}
  • src/ASM/CPP2012-asm/cpp-2012-asm.tex

    r2083 r2087  
    3737\title{On the correctness of an optimising assembler for the Intel MCS-51 microprocessor\thanks{The project CerCo acknowledges the financial support of the Future and Emerging Technologies (FET) programme within the Seventh Framework Programme for Research of the European Commission, under FET-Open grant number: 243881}}
    3838\author{Dominic P. Mulligan \and Claudio Sacerdoti Coen}
    39 \institute{Dipartimento di Scienze dell'Informazione, Universit\'a di Bologna}
     39\institute{Dipartimento di Scienze dell'Informazione,\\ Universit\'a di Bologna}
    4040
    4141\bibliographystyle{splncs03}
     
    5656Assembly language programs can manipulate concrete addresses in arbitrary ways.
    5757Our proof strategy contains a tracking facility for `good addresses' and only programs that use good addresses have their semantics preserved under assembly.
    58 Our strategy offers increased flexibility over the traditional approach of keeping addresses in assembly opaque.
     58Our strategy offers increased flexibility over the traditional approach to proving the correctness of assemblers, wherein addresses in assembly are kept opaque and immutable.
    5959In particular, we may experiment with allowing the benign manipulation of addresses.
    6060\end{abstract}
     
    7777As a result our C compiler, to have any sort of hope of successfully compiling realistic programs for embedded devices, ought to produce `tight' machine code.
    7878
    79 In order to do this, we must solve the `branch displacement' problem---deciding how best to expand jumps to labels in assembly language to machine code jumps.
    80 Clearly a correct but efficient strategy would be to expand all unconditional jumps to the MCS-51's \texttt{LJMP} instruction, and all conditional jumps to a set configuration of jumps using \texttt{LJMP} instructions; this is inefficient.
    81 Finding an efficient solution with this expansion process is not trivial, and is a well-known problem for those writing assemblers targetting RISC architectures.
    82 
    83 Branch displacement is not a simple problem to solve and requires the implementation of an optimising assembler.
     79In order to do this, we must solve the `branch displacement' problem---deciding how best to expand pseudojumps to labels in assembly language to machine code jumps.
     80Clearly a correct but efficient strategy would be to expand all unconditional pseudojumps to the MCS-51's \texttt{LJMP} instruction, and all conditional pseudojumps to a set configuration of jumps using \texttt{LJMP} instructions; this is inefficient and a waste of valuable code memory space.
     81Finding an efficient solution with this expansion process is not trivial, and is a well-known problem for those writing assemblers targetting RISC architectures (for instance, see~\cite{holmes:branch:2006}).
     82
     83To free the CerCo C compiler from having to consider complications relating to branch displacement, we have chosen to implement an optimising assembler, whose input language the compiler will target.
    8484Labels, conditional jumps to labels, a program preamble containing global data and a \texttt{MOV} instruction for moving this global data into the MCS-51's one 16-bit register all feature in our assembly language.
    85 We simplify the proof by assuming that all our assembly programs are pre-linked (i.e. we do not formalise a linker).
     85We simplify the proof by assuming that all our assembly programs are pre-linked (i.e. we do not formalise a linker---this is left for future work).
    8686
    8787Further, we must make sure that the assembly process does not change the timing characteristics of an assembly program for two reasons.
     
    105105This is achieved by means of dependent types: the assembly function is total over a program, a policy and the proof that the policy is correct for that program.
    106106
    107 Policies do not exist in only a limited number of circumstances: namely, if a pseudoinstruction attempts to jump to a label that does not exist, or the program is too large to fit in code memory, even after shrinking jumps according to the best policy.
     107Policies do not exist in only a limited number of circumstances: namely, if a pseudoinstruction attempts to jump to a label that does not exist, or the program is too large to fit in code memory, even after shrinking jumps according to the policy.
    108108The first circumstance is an example of a serious compiler error, as an ill-formed assembly program was generated, and does not (and should not) count as a mark against the completeness of the assembler.
    109109
     
    155155Our emulator centres around a \texttt{Status} record, describing the microprocessor's state.
    156156This record contains fields corresponding to the microprocessor's program counter, registers, and so on.
    157 At the machine code level, code memory is implemented as a compact trie of bytes, addressed by the program counter.
     157At the machine code level, code memory is implemented as a compact trie of bytes addressed by the program counter.
    158158We parameterise \texttt{Status} records by this representation as a few technical tasks manipulating statuses are made simpler using this approach, as well as permitting a modicum of abstraction.
    159159
     
    163163\end{lstlisting}
    164164%The function \texttt{execute} allows one to execute an arbitrary, but fixed (due to Matita's normalisation requirement) number of steps of a program.
    165 The function \texttt{execute\_1} closely matches the operation of the MCS-51 hardware, as described by a Siemen's manufacturer's data sheet.
     165The function \texttt{execute\_1} closely matches the operation of the MCS-51 hardware, as described by a Siemen's manufacturer's data sheet (specifically, this one~\cite{siemens:2011}).
    166166We first fetch, using the program counter, from code memory the first byte of the instruction to be executed, decoding the resulting opcode, fetching more bytes as is necessary.
    167 Decoded instructions are represented as an inductive type, where $\llbracket - \rrbracket$ denotes a vector:
     167Decoded instructions are represented as an inductive type, where $\llbracket - \rrbracket$ denotes a fixed-length vector:
    168168\begin{lstlisting}
    169169inductive preinstruction (A: Type[0]): Type[0] :=
     
    183183
    184184Once decoded, execution proceeds by a case analysis on the decoded instruction, following the operation of the hardware.
    185 For example:
     185For example, the \texttt{DEC} instruction (`decrement') is implemented as follows:
    186186
    187187\begin{lstlisting}
     
    202202\label{subsect.assembly.code.semantics}
    203203
    204 An assembly program is a list of potentially labelled pseudoinstructions, bundled with a preamble consisting of a list of symbolic names for locations in data memory.
     204An assembly program is a list of potentially labelled pseudoinstructions, bundled with a preamble consisting of a list of symbolic names for locations in data memory (i.e. global variables).
    205205Pseudoinstructions are implemented as an inductive type:
    206206\begin{lstlisting}
     
    219219Execution of pseudoinstructions is a function from \texttt{PseudoStatus} to \texttt{PseudoStatus}.
    220220Both \texttt{Status} and \texttt{PseudoStatus} are instances of a more general type parameterised over the representation of code memory.
    221 The more general type will be crucial to share most of the semantics of the
    222 two languages.
     221The more general type is crucial for sharing the majority of the semantics of the two languages.
    223222
    224223Emulation for pseudoinstructions is handled by \texttt{execute\_1\_pseudo\_instruction}:
     
    233232
    234233The costing returns \emph{pairs} of natural numbers because, in the case of expanding conditional jumps to labels, the expansion of the `true branch' and `false branch' may differ in execution time.
    235 The \texttt{ticks1} function we already seen used to increment the machine
    236 clock is determined, for the assembly language, from the costing function.
    237 It is instead fixed and precomputed for machine code.
     234The \texttt{add\_ticks1} function, which we have already seen used to increment the machine clock above, is determined for the assembly language from the costing function.
    238235
    239236Execution proceeds by first fetching from pseudo-code memory using the program counter---treated as an index into the pseudoinstruction list.
     
    267264Each of these three instructions expects arguments in different sizes and behaves in markedly different ways: \texttt{SJMP} may only perform a `local jump'; \texttt{LJMP} may jump to any address in the MCS-51's memory space and \texttt{AJMP} may jump to any address in the current memory page.
    268265Consequently, the size of each opcode is different, and to squeeze as much code as possible into the MCS-51's limited code memory, the smallest possible opcode that will suffice should be selected.
    269 This is a well known problem to assembler writers who target RISC architectures.
    270266
    271267Similarly, a conditional pseudojump must be translated potentially into a configuration of machine code instructions, depending on the distance to the jump's target.
     
    303299The input \texttt{pi} is the pseudoinstruction to be expanded and is found at address \texttt{ppc} in the assembly program.
    304300The policy is defined using the two functions \texttt{sigma} and \texttt{policy}, of which \texttt{sigma} is the most interesting.
    305 The function $\sigma$ maps pseudo program counters to program counters: the encoding of the expansion of the pseudoinstruction found at address \texttt{a} in the assembly code should be placed into code memory at address \texttt{sigma(a)}.
     301The function \texttt{sigma} maps pseudo program counters to program counters: the encoding of the expansion of the pseudoinstruction found at address \texttt{a} in the assembly code should be placed into code memory at address \texttt{sigma(a)}.
    306302Of course this is possible only if the policy is correct, which means that the encoding of consecutive assembly instructions must be consecutive in code memory.
    307303\begin{displaymath}
     
    311307Note that the entanglement we hinted at is only partially solved in this way: the assembler code can ignore the implementation details of the algorithm that finds a policy;
    312308however, the algorithm that finds a policy must know the exact behaviour of the assembly program because it needs to predict the way the assembly will expand and encode pseudoinstructions, once fed with a policy.
    313 A companion paper to this one~\cite{boender:correctness:2012} certifies an algorithm that finds branch displacement policies for the assembler described in this paper.
     309A companion submission to this one~\cite{boender:correctness:2012} certifies an algorithm that finds branch displacement policies for the assembler described in this paper.
    314310
    315311The \texttt{expand\_pseudo\_instruction} function uses the \texttt{sigma} map to determine the size of jump required when expanding pseudojumps, computing the jump size by examining the size of the differences between program counters.
     
    328324By `total correctness', we mean that the assembly process never fails when provided with a correct policy and that the process does not change the semantics of a certain class of well behaved assembly programs.
    329325
    330 The aim of this section is to prove the following informal statement: when we fetch an assembly pseudoinstruction \texttt{I} at address \texttt{ppc}, then we can fetch the expanded pseudoinstruction(s) \texttt{[J1, \ldots, Jn] = fetch\_pseudo\_instruction \ldots\ I\ ppc} from \texttt{sigma(ppc)} in the code memory obtained loading the assembled object code.
    331 
     326The aim of this section is to prove the following informal statement: when we fetch an assembly pseudoinstruction \texttt{I} at address \texttt{ppc}, then we can fetch the expanded pseudoinstruction(s) \texttt{[J1, \ldots, Jn] = fetch\_pseudo\_instruction \ldots\ I\ ppc} from \texttt{sigma(ppc)} in the code memory obtained by loading the assembled object code.
    332327This constitutes the first major step in the proof of correctness of the assembler, the next one being the simulation of \texttt{I} by \texttt{[J1, \ldots, Jn]} (see Subsection~\ref{subsect.total.correctness.for.well.behaved.assembly.programs}).
    333328
     
    355350Then, indexing into this list with any natural number \texttt{j} less than the length of \texttt{a} gives the same result as indexing into \texttt{assembled} with \texttt{sigma(ppc)} (the program counter pointing to the start of the expansion in \texttt{assembled}) plus \texttt{j}.
    356351
    357 Essentially the lemma above states that the \texttt{assembly} function correctly expands pseudoinstructions.
     352Essentially the lemma above states that the \texttt{assembly} function correctly expands pseudoinstructions, and that the expanded instruction reside consecutively in memory.
    358353This result is lifted from lists of bytes into a result on tries of bytes (i.e. code memories), using an additional lemma: \texttt{assembly\_ok}.
    359354
     
    397392\end{lstlisting}
    398393Here, \texttt{l} is the number of machine code instructions the pseudoinstruction at hand has been expanded into.
    399 We assemble a single pseudoinstruction with \texttt{assembly\_1\_pseudoinstruction}, which internally calls \texttt{jump\_expansion} and \texttt{expand\_pseudo\_instruction}.
     394We assemble a single pseudoinstruction with \texttt{assembly\_1\_pseudoinstruction}, which internally calls \texttt{expand\_pseudo\_instruction}.
    400395The function \texttt{fetch\_many} fetches multiple machine code instructions from code memory and performs some routine checks.
    401396
     
    427422We read \texttt{fetch\_assembly\_pseudo2} as follows.
    428423Suppose we are able to successfully assemble an assembly program using \texttt{assembly} and produce a code memory, \texttt{cmem}.
    429 Then, fetching a pseudoinstruction from the pseudo-code memory at address \texttt{ppc} corresponds to fetching a sequence of instructions from the real code memory using $\sigma$ to expand pseudoinstructions.
     424Then, fetching a pseudoinstruction from the pseudo-code memory at address \texttt{ppc} corresponds to fetching a sequence of instructions from the real code memory using \texttt{sigma} to expand pseudoinstructions.
    430425The fetched sequence corresponds to the expansion, according to \texttt{sigma}, of the pseudoinstruction.
    431426
     
    456451In contrast, in this paper we take a different approach.
    457452We trace memory locations (and, potentially, registers) that contain memory addresses.
    458 We then prove that only those assembly programs that use addresses in `safe' ways have their semantics preserved by the assembly process---a sort of type system sitting atop memory.
     453We then prove that only those assembly programs that use addresses in `safe' ways have their semantics preserved by the assembly process---a sort of dynamic type system sitting atop memory.
    459454
    460455We believe that this approach is more flexible when compared to the traditional approach, as in principle it allows us to introduce some permitted \emph{benign} manipulations of addresses that the traditional approach, using opaque addresses, cannot handle, therefore expanding the set of input programs that can be assembled correctly.
     
    462457
    463458Our analogue of the semantic function above is then merely a wrapper around the function that implements the semantics of machine code, paired with a function that keeps track of addresses.
    464 This permits a large amount of code reuse, as the semantics of pseudo- and machine code is essentially shared.
    465 The only thing that changes as the assembly level is the presence of the new tracking function.
     459This permits a large amount of code reuse, as the semantics of pseudo- and machine code are essentially shared.
     460The only thing that changes at the assembly level is the presence of the new tracking function.
    466461
    467462However, with this approach we must detect (at run time) programs that manipulate addresses in well behaved ways, according to some approximation of well-behavedness.
     
    479474
    480475The \texttt{low\_internal\_ram\_of\_pseudo\_low\_internal\_ram} function converts the lower internal RAM of a \texttt{PseudoStatus} into the lower internal RAM of a \texttt{Status}.
    481 A similar function exists for higher internal RAM.
     476A similar function exists for high internal RAM.
    482477Note that both RAM segments are indexed using addresses 7-bits long.
    483478The function is currently axiomatised, and an associated set of axioms prescribe the behaviour of the function:
     
    486481 internal_pseudo_address_map$\rightarrow$BitVectorTrie Byte 7$\rightarrow$BitVectorTrie Byte 7.
    487482\end{lstlisting}
    488 Another pair of axioms precisely describes the supposed behaviour of \texttt{low\_internal\_ram\_of\_pseudo\_low\_internal\_ram} and its high internal ram counterpart.
    489483
    490484Next, we are able to translate \texttt{PseudoStatus} records into \texttt{Status} records using \texttt{status\_of\_pseudo\_status}.
     
    535529\end{lstlisting}
    536530The statement is standard for forward simulation, but restricted to \texttt{PseudoStatuses} \texttt{ps} whose next instruction to be executed is well-behaved with respect to the \texttt{internal\_pseudo\_address\_map} \texttt{M}.
    537 Further, we explicitly requires proof that our policy is correct and the pseudo program counter lies within the bounds of the program.
     531Further, we explicitly require proof that our policy is correct and the pseudo program counter lies within the bounds of the program.
    538532Theorem \texttt{main\_thm} establishes the total correctness of the assembly process and can simply be lifted to the forward simulation of an arbitrary number of well behaved steps on the assembly program.
    539533
     
    545539
    546540We are proving the total correctness of an assembler for MCS-51 assembly language.
    547 In particular, our assembly language featured labels, arbitrary conditional and unconditional jumps to labels, global data and instructions for moving this data into the MCS-51's single 16-bit register.
     541In particular, our assembly language features labels, arbitrary conditional and unconditional jumps to labels, global data and instructions for moving this data into the MCS-51's single 16-bit register.
    548542Expanding these pseudoinstructions into machine code instructions is not trivial, and the proof that the assembly process is `correct', in that the semantics of a subset of assembly programs are not changed is complex.
    549543
    550544The formalisation is a key component of the CerCo project, which aims to produce a verified concrete complexity preserving compiler for a large subset of the C programming language.
    551 The verified assembler, complete with the underlying formalisation of the semantics of MCS-51 machine code (described fully in~\cite{mulligan:executable:2011}), will form the bedrock layer upon which the rest of the CerCo project will build its verified compiler platform.
     545The verified assembler, complete with the underlying formalisation of the semantics of MCS-51 machine code, will form the bedrock layer upon which the rest of the CerCo project will build its verified compiler platform.
    552546
    553547It is interesting to compare our work to an `industrial grade' assembler for the MCS-51: SDCC~\cite{sdcc:2011}.
     
    557551However, this comes at the expense of assembler completeness: the generated program may be too large to fit into code memory.
    558552In this respect, there is a trade-off between the completeness of the assembler and the efficiency of the assembled program.
    559 The definition and proof of a terminating, correct jump expansion policy is described in a companion publication to this one.
     553The definition and proof of a terminating, correct jump expansion policy is described in a companion publication to this one~\cite{boender:correctness:2012}.
    560554
    561555Aside from their application in verified compiler projects such as CerCo and CompCert, verified assemblers such as ours could also be applied to the verification of operating system kernels.
     
    575569The second purpose is to single out the sources of incompleteness.
    576570By abstracting our functions over the dependent type of correct policies, we were able to manifest the fact that the compiler never refuses to compile a program where a correct policy exists.
    577 This also allowed to simplify the initial proof by dropping lemmas establishing that one function fails if and only if some other one does so.
    578 
    579 Finally, dependent types, together with Matita's liberal system of coercions, allow to simulate almost entirely in user space the proof methodology ``Russell'' of Sozeau~\cite{sozeau:subset:2006}.
    580 However, not every proof has been done this way: we only used this style to prove that a function satisfies a specification that only involves that function in a significant way.
     571This also allowed to simplify the initial proof by dropping lemmas establishing that one function fails if and only if some previous function does so.
     572
     573Finally, dependent types, together with Matita's liberal system of coercions, allow us to simulate almost entirely---in user space---the proof methodology ``Russell'' of Sozeau~\cite{sozeau:subset:2006}.
     574However, not every proof has been carried out in this way: we only used this style to prove that a function satisfies a specification that only involves that function in a significant way.
    581575For example, it would be unnatural to see the proof that fetch and assembly commute as the specification of one of the two functions.
    582576
     
    613607The low ratio between the number of lines of code and the number of lines of proof is unusual.
    614608It is justified by the fact that the pseudo-assembly and the assembly language share most constructs and that large parts of the semantics are also shared.
    615 Thus many lines of code are required to describe the complex semantics of the processor, but, for the shared cases, the proof of preservation of the semantics is essentially trivial.
     609Therefore many lines of code are required to describe the complex semantics of the processor, but, for the shared cases, the proof of preservation of the semantics is essentially trivial.
    616610
    617611\bibliography{cpp-2012-asm.bib}
Note: See TracChangeset for help on using the changeset viewer.