Software Verification with ACL2
Total Page:16
File Type:pdf, Size:1020Kb
Software Verification with ACL2 Francisco Palomo Lozano [email protected] Software Verification and Validation Department of Computer Science Introduction Summary 1 Introduction 2 First Steps 3 Atoms and Lists 4 Sorted Lists 5 Sorting by Insertion 6 Sorting by Merging 7 Parallelism Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 3 / 39 Introduction What is ACL2? 1 AC omputational Logic for an Applicative Common Lisp 2 ACL2 is three things under the same name A pure functional programming language A computational logic formalizing this language An automated reasoning system for formal verification 3 ACL2 is regarded as an incarnation of McCarthy’s dream A dream came true Reasoning about Lisp functions 4 Landmarks Successor of NQTHM, the Boyer-Moore theorem prover Developed by Moore and Kaufmann for more than 20 years ACM Software System Award 2005 Annual conferences Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 4 / 39 Introduction Where has ACL2 been used?I 1996 Motorola CAP DSP 20-bit, used in radio, 1024-point complex FFT in 131 µs Verification of microcode DSP programs 1998 IBM 4758 PCI Cryptographic Coprocessor Used in ATMs Security model and formal analysis of bootstrapping code 2000 AMD Athlon Microprocessor Floating-point addition, subtraction, multiplication, and division Floating-point square root 2002 IBM Power4 Microprocessor Floating-point division and square-root algorithms 2005 Rockwell Collins AAMP7G Cryptoprocessor NSA MILS certified, used by the DoD in military avionics Verification of security policies implemented by microcode Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 5 / 39 Introduction Where has ACL2 been used?II 2008 Freescale Semiconductor Inc. Flash memory verification 2010 Centaur VIA Nano Microprocessor Low-power, used in netbooks, a direct competitor of Intel Atom Verification of instructions in the Media Unit Floating-point addition, subtraction and comparison Integer and floating-point conversions Integer multiplication 2011 AMD Llano Microprocessor Next-generation AMD mobile processor Integer division Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 6 / 39 Introduction Why so much interest in hardware verification? 1994 Intel Pentium FDIV error 4195835 1.33382044914 1.33373906890 buggy 3145727 ¼ 6Æ Î Dismissed as «not serious» when uncovered Defective chips had to be eventually recalled Intel recognised 475 000 000 USD in losses Moreover, the company’s prestige was damaged Intel Pentium jokes became famous Q: Know how the Republicans can cut taxes and pay the deficit at the same time? A: Their spreadsheet runs on a Pentium computer. Intel shares fell 5% in a week in December 1994 1997 Intel Pentium F00F error F00F C7C8 [lock cmpxchg8b eax] Processor hang! ¡! Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 7 / 39 Introduction Sure they learnt the lesson, didn’t they? 2007 Intel Core 2 Specification Update http://download.intel.com/design/processor/specupdt/313279.pdf Intel lists 129 design errors OpenBSD identified more than 20 of them as unfixable A few of them, for which no workaround exist, seem exploitable Reflections 1 These problems are not exclusive to Intel 2 It is likely that any complex hardware is buggy 3 As hardware gets more complex it resembles software 4 It has to do with the sheer complexity of new products 5 It has to do with processes and time-to-market pressure 6 It has to do with the status of our current tools and technology Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 8 / 39 Introduction What about software verification? 1 Nowadays the line between hardware and software is thinning Algorithm Microcode ! Algorithm HDL/RTL NDL Silicon ! ! ! 2 Real Lisp code can be verified and executed with ACL2 ACL2 code is directly executable in its host Lisp system ACL2 is a pure subset of Common Lisp with some extensions 3 Custom languages can be embedded or translated into ACL2 Java Bytecode ACL2 ! ! Several models of the JVM are available for ACL2 In general, this approach requires considerable effort The first silicon JVM was produced in 1997 by Rockwell Collins 4 Algorithms can be verified with ACL2 Complex algorithms are usually hard to test or validate Being able to execute algorithms modelled in ACL2 is a plus Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 9 / 39 First Steps Summary 1 Introduction 2 First Steps 3 Atoms and Lists 4 Sorted Lists 5 Sorting by Insertion 6 Sorting by Merging 7 Parallelism Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 10 / 39 First Steps ACL2 features 1 ACL2 is a pure functional language Reasoning is easier on pure functional languages Function execution only depends on its arguments 2 ACL2 is a logic of total recursive functions Function termination must be proven Even on inputs outside the intended domain 3 ACL2 functions can be annotated with guards 4 ACL2 functions may abort when executed like a program Guard violation Resource exhaustion, as stack overflow or memory full 5 ACL2 functions are Lisp functions 6 The reciprocal is not always true Lisp functions may not terminate Lisp functions can depend on state and even modify it Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 11 / 39 First Steps Your first steps in ACL2 1 Installing ACL2 Debian and Ubuntu sudo apt-get install acl2 Detailed instructions available for other systems http://www.cs.utexas.edu/users/moore/acl2 2 Executing ACL2 and running your first program acl2 ... ACL2 !> (defun f (n) (if (zp n) 1 (* n (f (- n 1))))) ... ACL2 !> (f 32) 263130836933693530167218012160000000 3 Finishing the ACL2 session ACL2 !> (exit) Also C-d, (quit) or (good-bye) Î Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 12 / 39 First Steps Development in ACL2 1 You do not usually work at the ACL2 prompt Typically you use an editor and save to .lisp files However the ACL2 command line is useful for other tasks 2 ACL2 .lisp files can be Fed into ACL2 acl2 < file.lisp Î Loaded inside ACL2 (ld "file.lisp") Î Converted in books and included (include-book "file") Î 3 Development environments Emacs with a shell buffer ACL2 Sedan http://acl2s.ccs.neu.edu Î DrACuLa http://www.ccs.neu.edu/home/cce/acl2 Î 4 Dual mode of operation It can be used as an interpreter for rapid development Code can be compiled for efficient execution when desired Compiled and interpreted code can be used together at will Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 13 / 39 First Steps The Lisp host system 1 ACL2 runs on top of a Lisp host system 2 CCL, SBCL, GCL, Allegro CL, CLISP, and CMUCL supported 3 The ACL2 command loop is a typical Lisp read-eval-print loop Exiting to the host Lisp system ACL2 !> :q Exiting the ACL2 read-eval-print loop. To re-enter, execute (LP). ACL2> Resuming from a correctable error or interruption ACL2 !> ^C User C-c interruption Î Correctable error: Console interrupt. ... ACL2[RAW LISP]>> :q ACL2 !> Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 14 / 39 First Steps Dissecting your first program 1 The mathematical factorial function f (0) 1 Æ f (n) nf (n 1) if n 0 Æ ¡ È 2 The ACL2 factorial function (defun f (n) function name and arguments Î (if (zp n) conditional expression Î 1 then branch value Î (* n (f (- n 1))))) else branch value Î A fundamental difference The mathematical function is implicitly defined on N The ACL2 function is defined on the universe of ACL2 objects Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 15 / 39 First Steps Expression evaluation 1 Lisp syntax ACL2!> (+ (* 5 (- 4 1)) 1) prefix notation 16 Î 2 Guards ACL2 !> (/ 1 0) ACL2 Error in TOP-LEVEL: The guard for the function call (UNARY-/ X), which is (AND (ACL2-NUMBERP X) (NOT (EQUAL X 0))), is violated by the arguments in the call (/ 0) ... ACL2 !> (set-guard-checking :none) ... ACL2 > (/ 1 0) all functions are total 0 Î Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 16 / 39 First Steps Guarded and unguarded evaluation 1 Some functions have attached a guard Guards warns about evaluations outside the intended domain Guards are checked during evaluation by default Guards are not used when proving properties 2 No guard was explicitly attached to f but . ACL2 !> (f -1) ACL2 Error in TOP-LEVEL: The guard for the function call (ZP X), which is (AND (INTEGERP X) (<= 0 X)), is violated by the arguments in the call (ZP -1) ... ACL2 !> (set-guard-checking :none) ... ACL2 > (f -1) (zp -1) is t 1 Î Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 17 / 39 First Steps Your first formal verification 1 Another function for computing factorials (defun g (n p) (if (zp n) p (g (- n 1) (* p n)))) (defun f* (n) (g n 1)) 2 Function g is just a certain generalisation of f (defthm g-generalises-f (implies (and (integerp n) (integerp p)) (equal (g n p) (* p (f n))))) 3 Functions f* and f are simply equivalent (defthm equivalence-of-f*-and-f (equal (f* n) (f n))) Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 18 / 39 Atoms and Lists Summary 1 Introduction 2 First Steps 3 Atoms and Lists 4 Sorted Lists 5 Sorting by Insertion 6 Sorting by Merging 7 Parallelism Francisco Palomo (UCA) Software Verification with ACL2 Version 1.1 19 / 39 Atoms and Lists Common data types 1 Atoms predicate atom Î Form the basic components of pairs and lists Include booleans, characters, strings, numbers and symbols 2 Pairs or conses predicate consp ACL2 !> (consÎ 1 2) (1 .