Construction of a Semantic Model Construction of a Semantic Model - - PowerPoint PPT Presentation

construction of a semantic model construction of a
SMART_READER_LITE
LIVE PREVIEW

Construction of a Semantic Model Construction of a Semantic Model - - PowerPoint PPT Presentation

Construction of a Semantic Model Construction of a Semantic Model for a Typed Assembly Language Gang Tan, Andrew Appel, Kedar Swadi and Dinghao Wu Princeton University Jan 11, 2004 Extensible systems Extensible systems code extensions host


slide-1
SLIDE 1

Construction of a Semantic Model Construction of a Semantic Model for a Typed Assembly Language

Gang Tan, Andrew Appel, Kedar Swadi and Dinghao Wu Princeton University Jan 11, 2004

slide-2
SLIDE 2

Extensible systems Extensible systems

code host

extensions

Applet, plug-in Web browser Device drivers Operating system Packet filters Routers

VMCAI 04

2

VB Add-ins PowerPoint

slide-3
SLIDE 3

Security concerns Security concerns

code host

How to give this untrusted code direct access without violating host’s safety policy? g f y p y We consider the case of machine/assembly code

VMCAI 04

3

slide-4
SLIDE 4

Typed Assembly Languages (TAL) yp y g g ( )

[Morrisett, Walker, Crary and Glew 1999]

L0: φ0 B0 B L1: φ1 B1 M

h

Type Cheker Bn Ln: φn

host

  • Loop invariants

– in terms of types – generated by a compiler

  • Type checks the assembly code

VMCAI 04

4

yp y

slide-5
SLIDE 5

TAL type checking TAL type checking

B0 L0: φ0 L : φ B1 L1: φ1 M Γ C Bn Ln: φn M

n

  • Hoare-logic style checking: pre- and postconditions
  • a e og c sty e c ec

g: p e a d postco d t o s

VMCAI 04

5

slide-6
SLIDE 6

Checking instructions Checking instructions

L0: φ0 L1: φ1 Ln: φn M M

n

φn

Can we trust these rules!

VMCAI 04

6

slide-7
SLIDE 7

Can we trust these typing rules? Can we trust these typing rules?

  • For small systems, maybe yes.
  • Production-scale low-level type systems
  • Production-scale low-level type systems

– Huge: LTAL by Chen et al. has 1200 operators & rules! Complex: because of intricate machine semantics – Complex: because of intricate machine semantics

  • Think about condition code types

– We routinely find and fix bugs in its early versions We routinely find and fix bugs in its early versions

VMCAI 04

7

slide-8
SLIDE 8

The type safety theorem The type-safety theorem

Type

host

Type Cheker

host

VMCAI 04

8

slide-9
SLIDE 9

Semantic model approach Semantic model approach

  • A classic idea: give a model in some logic so
  • A classic idea: give a model in some logic so

that the rule can be proved as a lemma

VMCAI 04

9

slide-10
SLIDE 10

What we need to model

Previous work

What we need to model

This talk

  • Models for safety of code, instructions, types, …

– [Appel & Felty 2000, Michael & Appel 2000] pp y pp

  • We also need models for typing judgments
  • Goal: give models to typing judgments

P ll h i l d i d l – Prove all the typing rules as derived lemmas – Verify the type-safety theorem

VMCAI 04

10

slide-11
SLIDE 11

A i ti ti f S hi Axiomatization of Sparc machine

O l i i d lib l i l

  • Our step relation is deliberately partial

– Omit any steps that would violate the safety policy

  • Mixing of machine semantics and safety policy is to

follow standard practice in type theory

VMCAI 04

11

slide-12
SLIDE 12

Safety definition Safety definition

  • A state is safe for k steps
  • Safe code

r m a a a L

VMCAI 04

12

slide-13
SLIDE 13

codeptr types codeptr types

  • Address l has type codeptr(φ) if it is safe to pass the

yp p (φ) p control to l, provided that φ is satisfied

– Safety within k steps

  • (m,l ) : codeptr(φ) ≡ ∀k. (m,l ) :k codeptr(φ)

– Safe for any number of steps

VMCAI 04

13

slide-14
SLIDE 14

Constructing a safety proof Constructing a safety proof

L0: φ0 L1: φ1 Ln: φn

M

  • The goal: safe_state(k,r,m) for any natural number k

– ∀ k. (m,L0) :k codeptr(φ0)

  • Do it by induction

– safe_state(0,r,m) is vacuously true – safe_state(k,r,m) ⇒ safe_state(k+1,r,m) (need a stronger induction hypothesis!)

?

VMCAI 04

14

slide-15
SLIDE 15

Simultaneous induction over all labels Simultaneous induction over all labels

L0: φ0 L1: φ1 Ln: φn

M

  • The goal ∀k. (m,L0) : codeptr(φ0)

g ( ,

0)

p (φ0)

  • Induction hypothesis: ∀l ,k. (m,l ) :k codeptr(φl)

simultaneously prove that all labels are safe for k steps – simultaneously prove that all labels are safe for k steps

VMCAI 04

15

slide-16
SLIDE 16

An example of the inductive case An example of the inductive case

  • Prove (m,4) :k+1 codeptr(φ4)
  • Induction hypothesis has

yp

– (m,0) :k codeptr(φ0)

r

int

m r’

int

m’

M

pc

int . . 4 M

pc

int .

a ak

. . . .

VMCAI 04

16

slide-17
SLIDE 17

The model of instruction judgment

  • For any state (r,m) and k such that

– (r,m) : φ – Instruction i is at location l – (m,l +4) :k codeptr(φ 0) and m :k Γ

  • Prove that (m,l) :k+1 codeptr(φ)

VMCAI 04

17

slide-18
SLIDE 18

The model of The model of

  • m : Δ(C) is to describe the program in the memory

m : Δ(C) is to describe the program in the memory

  • m : Γ means that the program respects all the loop invariants
  • The model can be written as

– If we define

Δ(C) Γ

VMCAI 04

18

( ) Γ

slide-19
SLIDE 19

The type safety theorem The type-safety theorem

VMCAI 04

19

slide-20
SLIDE 20

Implementations Implementations

  • Successfully defined the models of typing

judgments in LTAL

  • Proved the type safety theorem and the typing rules
  • f instructions
  • All the proofs are implemented in Twelf and

machine checkable machine checkable

VMCAI 04

20

slide-21
SLIDE 21

FPCC system FPCC system

Compiler 170,000 lines in ML

subtyping model of judgments; f f

TYPED ASSEMBLY LANGUAGE

subtyping theorems

1 l lf

proof of typing rules

TYPED MACHINE LANGUAGE

model of types

120,000 lines in Twelf

abstract instruction instruction decoder

machine states

nstruct on s

VMCAI 04

21

2,000 lines in Twelf Sparc spec. Logic

slide-22
SLIDE 22

Related work Related work

  • Proof of the type safety theorem

– Necula had 12 pages in his thesis – The TAL paper by Morrisett et al. had 8 pages – Paper proofs; not machine checked Paper proofs; not machine checked – Not proofs about their implemented systems

  • A syntactic approach to prove type soundness

[Hamid et al. 2002, Crary 2003]

– Type soundness theorem based on an abstract machine A simulation relation between the abstract machine and the real – A simulation relation between the abstract machine and the real machine

  • Models for unstructured programs with goto statements and

l b l labels [de Bruin 1981]

– Domain-theoretic models – k-th approximations of code behavior respects invariants

VMCAI 04

22

k th approximations of code behavior respects invariants