Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK...

27
Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer Stellenbosch University, South Africa Salvatore La Torre Università di Salerno, Italy Gennaro Parlato

Transcript of Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK...

Page 1: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Verifying Concurrent Programsby

Memory Unwinding

Ermenegildo Tomasco University of Southampton, UK

Omar Inverso University of Southampton, UK

Bernd Fischer Stellenbosch University, South Africa

Salvatore La Torre Università di Salerno, Italy

Gennaro Parlato University of Southampton, UK

Page 2: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Concurrent programs• multiple independent threads • communication via reads / writes

from / to shared memory• SC memory model

Shared-memory concurrency

SHARED MEMORY

T1

THREADS

Read(x) Write(x,val)

T2 Tn ...

Page 3: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Concurrent programs• multiple independent threads• communication via reads / writes

from / to shared memory• SC memory model

Reachability problems• error label (cf. SV-COMP)• assertion failure• out-of-bound array, division-by-zero, ...

Bounded analysis (bug hunting)

Shared-memory concurrency verification

SHARED MEMORY

T1

THREADS

Read(x) Write(x,val)

T2 Tn ...

Page 4: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Memory Unwinding

Page 5: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

T1 T2 T3

Interleaving semantics

rxwx rx wy rxwxrywy ryrx wywxrun

Page 6: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

T1 T2 T3

From interleavings to memory unwinding

rxwx rx wy rxwxrywyrun ryrx wy

accesses to shared memory

writes

rxwx rx wy rxwxrywy ryrx wy

wx

⇒ Memory unwinding models thread interaction history by data

MEMORYUNWINDINGwx wy wxwy wywx wy wxwy wy

Page 7: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Memory unwindings as thread interfaces

T1MEMO

RY UNWINDING

T2

Assume-guarantee reasoning style decomposition of verification

TASK 1 TASK 2

T1

Page 8: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Assume-guarantee reasoning

T1MEMOR

Y UNWINDING

T2

TASK 1 TASK 2

Thread T2

assumes: MU writes of other threads

guarantees: its own MU writes

Task 1: Task 2:

Thread T1

assumes: MU writes of other threads

guarantees: its own MU writes

Page 9: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

x=2y=1y=1

x=1

x=3y=8

z=2

pcmc

Global variable write – check against current MU entry

void write(uint t, uint v, int val) { mc[t] = th_nxt_wr[t][mc[t]]; assume(var[mc[t]] == v); assume(val[mc[t]] == val);}

pc

void write(uint t, uint v, int val) { mc[t] = th_nxt_wr[t][mc[t]];

}mc

Page 10: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

x=2y=1y=1

x=1

x=3y=8

z=2

pc

mc

Local statement (no global variables) – update pc, keep mc

pc

Page 11: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

x=2y=1y=1

x=1

x=3y=8

z=2

pcmc

Global variable read – “pick write position in range”

int read(uint t, uint v) { if (thr_terminated()) return 0; if (var_fst_wr[v]==0) return 0; uint r_from = *; assume((r_from <= last_wr_pos) && (r_from < th_nxt_wr[t][mc[t]])); assume(var[r_from] == v); if (r_from < mc[t]) assume(var_nxt_wr[r_from] > mc[t]); else { if (r_from < var_fst_wr[v]) return 0; mc[t] = r_from; }; return value[r_from];}

non-deterministic assignment

Page 12: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

x=2y=1y=1

x=1

x=3y=8

z=2

pcmc

Global variable read – “pick write position in range”

int read(uint t, uint v) { if (thr_terminated()) return 0; if (var_fst_wr[v]==0) return 0; uint r_from = *; assume((r_from <= last_wr_pos) && (r_from < th_nxt_wr[t][mc[t]])); assume(var[r_from] == v); if (r_from < mc[t]) assume(var_nxt_wr[r_from] > mc[t]); else { if (r_from < var_fst_wr[v]) return 0; mc[t] = r_from; }; return value[r_from];}

...before next write of thread...

Check in unwinding...

Page 13: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

x=2y=1y=1

x=1

x=3y=8

z=2

pcmc

Global variable read – “pick write position in range”

int read(uint t, uint v) { if (thr_terminated()) return 0; if (var_fst_wr[v]==0) return 0; uint r_from = *; assume((r_from <= last_wr_pos) && (r_from < th_nxt_wr[t][mc[t]])); assume(var[r_from] == v); if (r_from< mc[t]) assume(var_nxt_wr[nxt_mc] > mc[t]); else { if (r_from< var_fst_wr[v]) return 0; mc[t] = r_from; }; return value[r_from];}

...before next write of thread...

...right variable...

Page 14: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

x=2y=1y=1

x=1

x=3y=8

z=2

pcmc

Global variable read – “pick write position in range”

int read(uint t, uint v) { if (thr_terminated()) return 0; if (var_fst_wr[v]==0) return 0; uint r_from = *; assume((r_from <= last_wr_pos) && (r_from < th_nxt_wr[t][mc[t]])); assume(var[r_from] == v); if (r_from < mc[t]) assume(var_nxt_wr[r_from] > mc[t]); else { if (r_from < var_fst_wr[v]) return 0; mc[t] = r_from; }; return value[r_from];}

...right variable......next write of

variable in future...

Page 15: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

x=2y=1y=1

x=1

x=3y=8

z=2

pcmc

Global variable read – “pick write position in range”

int read(uint t, uint v) { if (thr_terminated()) return 0; if (var_fst_wr[v]==0) return 0; uint r_from = *; assume((r_from <= last_wr_pos) && (r_from < th_nxt_wr[t][mc[t]])); assume(var[r_from] == v); if (r_from < mc[t]) assume(var_nxt_wr[r_from] > mc[t]); else { if (r_from < var_fst_wr[v]) return 0; mc[t] = r_from; }; return value[r_from];}

...before next write of variable...

...else update mc.

mc

Return value.

pc

Page 16: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Simulating a thread against an MU

y=1 …. a=x+5 ..... x=1 ….

assert(b);

x=2y=1y=1

x=1

x=3y=8

z=2

Handling errors – “flag that an error occurs”

pc

translates to if (! b) _error = 1;

(computation might not be feasible)

Page 17: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

MU based sequentialization

Page 18: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Our apporach: sequentialization by MU

…T0 T1

TN

…F0 F1

FN main()

Concurrent program

Sequential program

Sequentialization(code-to-code translation)

Simulation functions

translates

translates

translates

MEMORY UNWINDING

Page 19: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Main Sequential Program

void main(void){

memory_init(W);

ct := mem_thread_create();

F0();

thread_terminate(ct);

all_thread_simulated();

assert(_error == 0) }

main

instantiate memory unwinding

error check

all writes of the thread are executed

simulation starts from main thread

all writes of the MU are executed

register main thread

Thread creation

Page 20: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Implementation and evaluation

Page 21: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

MU-based sequentialization implemented − for C programs with the Pthread library− optimized for sequential CBMC back-end− within the CSeq framework

MU-CSeq

sequentialnon-deterministic

C program

P'

PthreadC program

P

true/falseMU-CSeqsequential

CBMC

# writes

Page 22: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Evaluation: bug-huntingSVCOMP’15, Concurrency (UNSAFE instances)

- competitive with other tools

- Mu-CSeq won the silver medal in the Concurrency category of SVCOMP’15/14

≤ 24

# writes

90

Page 23: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

• MU scheme discussed so far:− all the writes of the shared variables are exposed

• The size of the MU determines the amount of nondeterminism− impacts the performance

MU compact representations

We give also two more compact schemes: − only relevant writes of the shared variables are exposed − can handle larger number of writes

x=2y=1y=1

x=1

x=3y=8

z=2

Page 24: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Conclusions

Page 25: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

Bounded analysis for shared-memory concurrency

Bounded search is supported by empirical evaluations:• most concurrency bugs exposed by few interactions [S. Lu, S. Park, E. Seo, Y. Zhou, SIGOPS 2008]• many concurrency errors manifest themselves already after only a few

context switches [S. Qadeer, D. Wu, PLDI 2007]

sequentializations with bounded context-switches

• up to 2 context switches [ Qadeer, Wu – PLDI’04 ]• eager, bounded context-switch, finite # threads [ Lal, Reps – CAV’08 ]• lazy, finite # threads, parameterized [La Torre, Madhusudan, Parlato – CAV’09, CAV’10]• thread creation [Bouajjani, Emmi, Parlato – SAS’11] [Emmi, Qadeer, Rakamaric – POPL’11]• Lazy, finite #threads

[Inverso, Tomasco, La Torre, Fisher, Parlato – CAV’14]

We also combine : bounded writes & bounded context-switches

Page 26: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

• We have presented− new bounded exploration of the computations of concurrent programs − new sequentialization

o bounded number of shared-memory write operationso not bounding the number of context switches

− tool optimized for Bounded Model Checking

• Mu-CSeq is competitive with state-of-the-art tools− silver medal at SVCOMP’14 and '15 (concurrency)

Conclusions

Page 27: Verifying Concurrent Programs by Memory Unwinding Ermenegildo Tomasco University of Southampton, UK Omar Inverso University of Southampton, UK Bernd Fischer.

• Weak Memory Models (WMM)− TSO

• Message Passing Interface (MPI)− Write → Send, Read → Receive

• Bug detection combining CSeq with − abstract interpretation− testing

Ongoing Work