Static Analysis by Abstract Interpretation of Functional Properties - - PowerPoint PPT Presentation

static analysis by abstract interpretation of functional
SMART_READER_LITE
LIVE PREVIEW

Static Analysis by Abstract Interpretation of Functional Properties - - PowerPoint PPT Presentation

Static Analysis by Abstract Interpretation of Functional Properties of Device Drivers in TinyOS Abdelraouf Ouadjaout, Antoine Min, Noureddine Lasla, Nadjib Badache ENS & UPMC, Paris CERIST & USTHB, Algiers Workshop on Static


slide-1
SLIDE 1

Static Analysis by Abstract Interpretation of Functional Properties of Device Drivers in TinyOS

Abdelraouf Ouadjaout, Antoine Miné, Noureddine Lasla, Nadjib Badache

ENS & UPMC, Paris CERIST & USTHB, Algiers

Workshop on Static Analysis of Concurrent Software September 11th, 2016 Edinburgh, Scotland

1 / 20

slide-2
SLIDE 2

Part I Context

2 / 20

slide-3
SLIDE 3

Context

Application Domain: Wireless Sensor Networks

We target programs for wireless sensor networks (WSN). A distributed system of wirelessly connected embedded nodes for monitoring a physical phenomena. Ad hoc communications and collaborative routing. Many applications: irrigation, weather/pollution monitoring, fire detection, etc.

3 / 20

slide-4
SLIDE 4

Context

Problem Formulation: Device Driver Verification

We aim at verifying the correctness of device drivers in TinyOS programs. Motivation Drivers difficult to develop/debug, error-prone and critical. Summary of Our Approach We focus on functional properties specifying programming rules to access hardware correctly. We employ Abstract Interpretation to automatically verify that all possible executions obey such specifications.

For more details: Static Analysis by Abstract Interpretation of Functional Properties of Device Drivers in TinyOS In Journal of Systems and Software, 2016.

4 / 20

slide-5
SLIDE 5

Part II Expressing Specifications

5 / 20

slide-6
SLIDE 6

Specifications

Example: ATmega128 Timer/Counter0

ATmega128

slide-7
SLIDE 7

Specifications

Example: ATmega128 Timer/Counter0

ATmega128 Datasheet, pp. 107

slide-8
SLIDE 8

Specifications

Example: ATmega128 Timer/Counter0

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode.

slide-9
SLIDE 9

Specifications

Example: ATmega128 Timer/Counter0

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode. Formalism To formalize this rule, we use an automaton tai- lored to describe patterns of hardware interactions

6 / 20

slide-10
SLIDE 10

Specifications

Abstract Device Properties

An abstract device property is a special register automaton describing patterns of hardware interactions: A = (S,s0,sBUG,R,ξ,T ) where: S set of states s0 initial state sBUG bug state R set of hardware registers ξ = {X◇ ∣ X ∈ R,◇ ∈ {r,w}} ∪ {inti ∣ i ∈ I} ∪ {α,sleep} T ⊆ S × ξ × S × StmtC × StmtC

7 / 20

slide-11
SLIDE 11

Specifications

ADP Example

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode.

slide-12
SLIDE 12

Specifications

ADP Example

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode.

STABLE

slide-13
SLIDE 13

Specifications

ADP Example

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode.

STABLE SLEEP e:sleep UNSTBL e:int15∣16

slide-14
SLIDE 14

Specifications

ADP Example

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode.

STABLE SLEEP e:sleep UNSTBL e:int15∣16 BUSY e:TCCR0w a:ASSR|=(1<<TCR0UB)

slide-15
SLIDE 15

Specifications

ADP Example

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode.

STABLE SLEEP e:sleep UNSTBL e:int15∣16 BUSY e:TCCR0w a:ASSR|=(1<<TCR0UB) e:α a:ASSR&=~(1<<TCR0UB)

slide-16
SLIDE 16

Specifications

ADP Example

ATmega128 Datasheet, pp. 107

Rule If Timer/Counter0 is used to wake the device up [...], precautions must be taken [...] 1 Write a value to TCCR0, TCNT0, or OCR0. 2 Wait until the corresponding Update Busy flag in ASSR returns to zero. 3 Enter Power-save or Extended Standby mode.

STABLE SLEEP e:sleep UNSTBL e:int15∣16 BUSY e:TCCR0w a:ASSR|=(1<<TCR0UB) e:α a:ASSR&=~(1<<TCR0UB) BUG e:sleep e:sleep 8 / 20

slide-17
SLIDE 17

Part III Abstractions

9 / 20

slide-18
SLIDE 18

Concrete Semantics

TinyOS Kernel

TinyOS is an open source OS developped by Berkely. Mixture of preemptive and cooperative execution models.

1 Hardware interrupts can preempt execution at any time (if not

masked).

2 Tasks are functions that are posted for being executed when

system is idle.

10 / 20

slide-19
SLIDE 19

Concrete Semantics

TinyOS Kernel

TinyOS is an open source OS developped by Berkely. Mixture of preemptive and cooperative execution models.

1 Hardware interrupts can preempt execution at any time (if not

masked).

2 Tasks are functions that are posted for being executed when

system is idle. Concrete Environment E = M

Memory × HW state

S × Q

Tasks queue × Interrupts

I

10 / 20

slide-20
SLIDE 20

Hardware State Partitioning

Definition

Hardware state is the primary information in the analysis. We should keep precise information about it. Memory content should be in relation with the hardware state because program infer the state by accessing hardware registers and/or some program variables.

11 / 20

slide-21
SLIDE 21

Hardware State Partitioning

Definition

Hardware state is the primary information in the analysis. We should keep precise information about it. Memory content should be in relation with the hardware state because program infer the state by accessing hardware registers and/or some program variables. Hardware State Partitioning D♯

S 1 First, we partition the environments w.r.t. automaton states

S.

2 Then, we use a numerical abstract domain ⟨D♯ M,⊑M⟩ to

abstract the values of registers and variables. ⟨℘(E),⊆⟩ − − → ← − − ⟨S → D♯

M, ˙

⊑M⟩

11 / 20

slide-22
SLIDE 22

Hardware State Partitioning

Example

1 void main () { 2 // Config timer 3 ... 4 // Wait for interrupt 5 while (1) { 6 asm volatile("sleep"::); 7 ... 8 } 9 } 10 ISR( TIMER0_OVF_vect ) { 11 ... 12 // Stabilize the timer 13 TCCR0 = TCCR0; 14 while 15 (ASSR & 1 << TCR0UB ); 16 // Continue work 17 ... 18 }

STABLE SLEEP e:sleep UNSTBL e:int15∣16 BUG e:sleep BUSY e:TCCR0w a:ASSR|=(1<<TCR0UB) e:sleep e:α a:ASSR&=~(1<<TCR0UB) 12 / 20

slide-23
SLIDE 23

Hardware State Partitioning

Example

1 void main () { 2 // Config timer 3 ... 4 // Wait for interrupt 5 while (1) { 6 asm volatile("sleep"::); 7 ... 8 } 9 } 10 ISR( TIMER0_OVF_vect ) { 11 ... 12 // Stabilize the timer 13 TCCR0 = TCCR0; 14 while 15 (ASSR & 1 << TCR0UB ); 16 // Continue work 17 ... 18 }

STABLE SLEEP e:sleep UNSTBL e:int15∣16 BUG e:sleep BUSY e:TCCR0w a:ASSR|=(1<<TCR0UB) e:sleep e:α a:ASSR&=~(1<<TCR0UB)

X6 X11 X14 X17

12 / 20

slide-24
SLIDE 24

Hardware State Partitioning

Example

1 void main () { 2 // Config timer 3 ... 4 // Wait for interrupt 5 while (1) { 6 asm volatile("sleep"::); 7 ... 8 } 9 } 10 ISR( TIMER0_OVF_vect ) { 11 ... 12 // Stabilize the timer 13 TCCR0 = TCCR0; 14 while 15 (ASSR & 1 << TCR0UB ); 16 // Continue work 17 ... 18 }

STABLE SLEEP e:sleep UNSTBL e:int15∣16 BUG e:sleep BUSY e:TCCR0w a:ASSR|=(1<<TCR0UB) e:sleep e:α a:ASSR&=~(1<<TCR0UB)

X6 X11 X14 X17

STABLE ASSR = 0 UNSTBL ASSR = 0 BUSY ASSR = 1 STABLE ASSR = 0 STABLE ASSR = 0 12 / 20

slide-25
SLIDE 25

Tasks Queue Partitioning

Motivation

Example: SPI serial transfer with a task polling (instead of an active polling).

13 / 20

slide-26
SLIDE 26

Tasks Queue Partitioning

Motivation

Example: SPI serial transfer with a task polling (instead of an active polling).

task void tx() { if (i < m_len) { SPDR = m_data[i]; post check (); return; } post end (); } task void check () { if !( SPSR &(1<< SPIF )) post check (); else { m_answer[i] = SPDR; i++; post tx (); } } task void end () { SPCR &=~(1<< SPE ); ... }

13 / 20

slide-27
SLIDE 27

Tasks Queue Partitioning

Motivation

Example: SPI serial transfer with a task polling (instead of an active polling).

task void tx() { if (i < m_len) { SPDR = m_data[i]; post check (); return; } post end (); } task void check () { if !( SPSR &(1<< SPIF )) post check (); else { m_answer[i] = SPDR; i++; post tx (); } } task void end () { SPCR &=~(1<< SPE ); ... }

Rule Data register SPDR can not be used when the bus is not free.

OFF ON DONE BUG BUSY

e:SPDRw e:SPCRw g:(SPCR&(1<<SPE) e:SPCRw g:!(SPCR&(1<<SPE)) e:SPDRw e:SPDRw e:α a:SPSR|=(1<<SPIF) e:SPDRr a:SPSR &=~(1<<SPIF)

13 / 20

slide-28
SLIDE 28

Tasks Queue Partitioning

Definition

Tasks control flow problem We can not verify this ADP with D♯

S since we consider every

possible order of tasks. Solution We distinguish between the case when a task is posted or not. We count the number of occurances of posted tasks. ⟨℘(E),⊆⟩ − − → ← − − ⟨℘(T) → (D♯

S × N ♯ T→N),⊑Q⟩

{tx} ON i ∈ [0, m_len] SPSR = 0 tx = 1 {check} BUSY i ∈ [0, m_len[ SPSR = 0 check = 1 DONE i ∈ [0, m_len[ SPSR = 0x80 check = 1 {end} ON i = m_len SPSR = 0 end = 1 ∅ OFF i = m_len SPSR = 0

14 / 20

slide-29
SLIDE 29

Abstraction of Preemption

Overview

Modular Preemption Analysis Avoid re-analyzing interrupts each time they are enabled.

task void t() { SPCR |= (1<<SPE ); ... x = y * i; } ISR(ADC_vect) { ... } ISR( SPI_STC_vect ) { ... }

15 / 20

slide-30
SLIDE 30

Abstraction of Preemption

Overview

Modular Preemption Analysis Avoid re-analyzing interrupts each time they are enabled. Collect the preemption environments at possible fire sites and analyze interrupts independently.

task void t() { SPCR |= (1<<SPE ); ... x = y * i; } ISR(ADC_vect) { ... } ISR( SPI_STC_vect ) { ... }

15 / 20

slide-31
SLIDE 31

Abstraction of Preemption

Overview

Modular Preemption Analysis Avoid re-analyzing interrupts each time they are enabled. Collect the preemption environments at possible fire sites and analyze interrupts independently.

task void t() { SPCR |= (1<<SPE ); ... x = y * i; } ISR(ADC_vect) { ... } ISR( SPI_STC_vect ) { ... }

15 / 20

slide-32
SLIDE 32

Abstraction of Preemption

Overview

Modular Preemption Analysis Avoid re-analyzing interrupts each time they are enabled. Collect the preemption environments at possible fire sites and analyze interrupts independently. The result environments are injected at fire sites to approximate the effect of interrupts.

task void t() { SPCR |= (1<<SPE ); ... x = y * i; } ISR(ADC_vect) { ... } ISR( SPI_STC_vect ) { ... }

15 / 20

slide-33
SLIDE 33

Abstraction of Preemption

Overview

Modular Preemption Analysis Avoid re-analyzing interrupts each time they are enabled. Collect the preemption environments at possible fire sites and analyze interrupts independently. The result environments are injected at fire sites to approximate the effect of interrupts.

task void t() { SPCR |= (1<<SPE ); ... x = y * i; } ISR(ADC_vect) { ... } ISR( SPI_STC_vect ) { ... }

15 / 20

slide-34
SLIDE 34

Abstraction of Preemption

Definition

Abstraction of Interrupts Masks

D♯

K ≜ {01,0,1,⊺01}

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

K♯

G

×(℘(I) → D♯

Q)

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

K♯

P

K♯

G is an abstraction of the global interrupt flag. It is modified

by statements like asm volatile("sei":::"memory"). K♯

P is a partitioning of environments w.r.t. active interrupts.

It is affected by modification of specific hardware registers.

16 / 20

slide-35
SLIDE 35

Abstraction of Preemption

Definition

Abstraction of Interrupts Masks

D♯

K ≜ {01,0,1,⊺01}

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

K♯

G

×(℘(I) → D♯

Q)

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

K♯

P

K♯

G is an abstraction of the global interrupt flag. It is modified

by statements like asm volatile("sei":::"memory"). K♯

P is a partitioning of environments w.r.t. active interrupts.

It is affected by modification of specific hardware registers. Abstraction of Interruption Environments

D♯

I ≜ D♯ K × (I → D♯ K)

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

Preemption

×(I → D♯

K)

ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ

Return

16 / 20

slide-36
SLIDE 36

Experiments

Implementation

Prototype implementation called SADA (Static Analysis with Device Abstraction). Done in OCaml (∼ 4000 lines of code). Using CIL and Apron. Seven ADPs were analyzed related to three devices:

1 Wireless transceiver CC2420. 2 Analog switch ADG715. 3 ATmega128 timer.

Two driver implementations for each device: TinyOS 1.x and 2.x.

Very different design and algorithms. Same property can be used.

17 / 20

slide-37
SLIDE 37

Experiments

Results

Time (s) × Memory (MB) × {✔, ✘, ◯

✘ }

Driver LoC # ISR ∣T∣ ADP ∣S∣ D♯

I(D♯ S)

D♯

I(D♯ Q)

CC2420 1.x 2666 1 1 ASPI-SS 4 12 12 ✔ 850 42 ✔ ASPI-TX 10 21 13 ✔ 1600 74 ✔ CC2420 2.x 10133 2 10 ASPI-SS 4 ∞ 34 18 ✔ ASPI-TX 10 ∞ 39 18 ✔ ADG715 1.x 2038 1 1 APULL-UP 4 1 11 ✘ 1 11 ✘ ATWI-TX 6 4 11 ✔ 7 12 ✔ ADG715 2.x 4412 1 6 APULL-UP 4 23 14 ✔ 8 13 ✔ ATWI-TX 6 40 16 ✘ 6 14 ✘ Timer 1.x 1627 1 3 ASTBL 7 6 11

39 16

AOCR0 4 3 10

29 15

ATCCR0 4 3 10 ✔ 37 16 ✔ Timer 2.x 2384 2 2 ASTBL 4 7 12 ✔ 26 13 ✔ AOCR0 4 10 12 ✔ 38 13 ✔ ATCCR0 4 10 11

23 13

✘ 18 / 20

slide-38
SLIDE 38

Experiments

Results

Time (s) × Memory (MB) × {✔, ✘, ◯

✘ }

Driver LoC # ISR ∣T∣ ADP ∣S∣ D♯

I(D♯ S)

D♯

I(D♯ Q)

CC2420 1.x 2666 1 1 ASPI-SS 4 12 12 ✔ 850 42 ✔ ASPI-TX 10 21 13 ✔ 1600 74 ✔ CC2420 2.x 10133 2 10 ASPI-SS 4 ∞ 34 18 ✔ ASPI-TX 10 ∞ 39 18 ✔ ADG715 1.x 2038 1 1 APULL-UP 4 1 11 ✘ 1 11 ✘ ATWI-TX 6 4 11 ✔ 7 12 ✔ ADG715 2.x 4412 1 6 APULL-UP 4 23 14 ✔ 8 13 ✔ ATWI-TX 6 40 16 ✘ 6 14 ✘ Timer 1.x 1627 1 3 ASTBL 7 6 11

39 16

AOCR0 4 3 10

29 15

ATCCR0 4 3 10 ✔ 37 16 ✔ Timer 2.x 2384 2 2 ASTBL 4 7 12 ✔ 26 13 ✔ AOCR0 4 10 12 ✔ 38 13 ✔ ATCCR0 4 10 11

23 13

✘ 18 / 20

slide-39
SLIDE 39

Experiments

Results

Time (s) × Memory (MB) × {✔, ✘, ◯

✘ }

Driver LoC # ISR ∣T∣ ADP ∣S∣ D♯

I(D♯ S)

D♯

I(D♯ Q)

CC2420 1.x 2666 1 1 ASPI-SS 4 12 12 ✔ 850 42 ✔ ASPI-TX 10 21 13 ✔ 1600 74 ✔ CC2420 2.x 10133 2 10 ASPI-SS 4 ∞ 34 18 ✔ ASPI-TX 10 ∞ 39 18 ✔ ADG715 1.x 2038 1 1 APULL-UP 4 1 11 ✘ 1 11 ✘ ATWI-TX 6 4 11 ✔ 7 12 ✔ ADG715 2.x 4412 1 6 APULL-UP 4 23 14 ✔ 8 13 ✔ ATWI-TX 6 40 16 ✘ 6 14 ✘ Timer 1.x 1627 1 3 ASTBL 7 6 11

39 16

AOCR0 4 3 10

29 15

ATCCR0 4 3 10 ✔ 37 16 ✔ Timer 2.x 2384 2 2 ASTBL 4 7 12 ✔ 26 13 ✔ AOCR0 4 10 12 ✔ 38 13 ✔ ATCCR0 4 10 11

23 13

✘ 18 / 20

slide-40
SLIDE 40

Experiments

Results

Time (s) × Memory (MB) × {✔, ✘, ◯

✘ }

Driver LoC # ISR ∣T∣ ADP ∣S∣ D♯

I(D♯ S)

D♯

I(D♯ Q)

CC2420 1.x 2666 1 1 ASPI-SS 4 12 12 ✔ 850 42 ✔ ASPI-TX 10 21 13 ✔ 1600 74 ✔ CC2420 2.x 10133 2 10 ASPI-SS 4 ∞ 34 18 ✔ ASPI-TX 10 ∞ 39 18 ✔ ADG715 1.x 2038 1 1 APULL-UP 4 1 11 ✘ 1 11 ✘ ATWI-TX 6 4 11 ✔ 7 12 ✔ ADG715 2.x 4412 1 6 APULL-UP 4 23 14 ✔ 8 13 ✔ ATWI-TX 6 40 16 ✘ 6 14 ✘ Timer 1.x 1627 1 3 ASTBL 7 6 11

39 16

AOCR0 4 3 10

29 15

ATCCR0 4 3 10 ✔ 37 16 ✔ Timer 2.x 2384 2 2 ASTBL 4 7 12 ✔ 26 13 ✔ AOCR0 4 10 12 ✔ 38 13 ✔ ATCCR0 4 10 11

23 13

✘ 18 / 20

slide-41
SLIDE 41

Related Work

Approach Tool Properties Preemption SafeTinyOS Runtime Dynamic analysis TemporalMonitors Runtime/LTL Enumeration TOS2CProver Runtime Sequenlization Model checking i-CBMC Runtime Partial order encoding Deductive methods [Duan & Regeher] Hardware Not supported [Monniaux] Hardware MCSquare Runtime Abstract interpretation [Wu et al.] Runtime Sequenlization

19 / 20

slide-42
SLIDE 42

Conclusion

Summary An abstract interpreter for verifying hardware interactions in TinyOS device drivers was presented. It is based on different levels of partitioning for handling hardware state, tasks queue and interrupt masks. Preemption is formalized using an iterative and modular fixpoint analysis. Future Work Develop abstract domains for timing specifications of devices. Consider other cooperative models (e.g. Protothreads of Contiki).

20 / 20