• No results found

Step 4: Pairing Algorithm

CHAPTER 6. L-SAP: EVIDENCE-ENABLED LINUX VERIFICATION

6.2 L-SAP Approach

6.2.4 Step 4: Pairing Algorithm

L-SAP iterates over the set of signatures to apply the pairing algorithm to each M P Go

to pair the locks and unlocks with signature o. For efficiency, the pairing algorithm computes context-sensitive function summaries using the EFGs computed in the previous step. Note that, if a function appears in two matching pair graphs with corresponding signatures o1and o2, then

the function would have two contexts as well as two summaries. L-SAP visits the matching pair graph in a bottom-up manner while: (1) computing compact function summaries for each visited function, plugging in the summaries of the callees at call-sites while analyzing the callers, and (2) keeping track of the lock/unlock pairs, unpaired locks, and deadlocks.

6.2.4.1 Compact Function Summaries

For each function, the function summary is computed by traversing its EFG in a depth-first manner while keeping track of all entry/exit locks/unlocks on all EFG paths. Let us illustrate our approach to computing compact function summaries. Figures6.2(a) and (b) show the EFG for functions f and g. In this example, f calls g at statement: Call g;. The function summary for g should retain the information relevant for analyzing f . The pairing analysis is concerned with what follows a given lock: (i) a lock followed by unlock implies pairing, (ii) a lock followed by another lock implies a deadlock, and (iii) a lock not followed by lock or unlock implies an unpaired lock.

The function summary consists of two sub-summaries: entry and exit summaries.

The entry summary for g summarizes: (i) the possible unlock(s) in g that can be paired with LP(o), (ii) the lock(s) in g that cause deadlocks with LP(o), and (iii) the possibility of

not pairing LP(o) with lock/unlock in g. Case (iii) is possible if there exists a path in g that

does not have lock/unlock events. The entry summary for g - denoted by entry summary in Figure 6.2(d)- includes: the entry locks (L1(o) and L3(o)), the entry unlock (U2(o)), and the

THROUGH state denoting the existence of paths in g that do not have any locks/unlocks.

The exit summary for g summarizes: (a) the possible lock(s) in g that can be paired with the lock (LE(o))/unlock (UE(o)) in f , and (b) the possibility that a lock before calling g can

Figure 6.2 Compact function summaries for caller (f ) and callee (g)

be paired with a lock/unlock after calling g. Case (b) is possible if there is a THROUGH state in g. The exit summary for g - denoted byexit summaryin Figure 6.2(d) - includes: the exit locks (L2(o) and L3(o)) and theTHROUGH state.

Since the pairing algorithm is traversing the M P Go in a bottom-up manner, the summary

for g is available to compute the summary for f . Figure 6.2(c) shows the entry and exit summaries for f . Note that: (1) the entry summary for g is part of the entry summary of f because the entry locks/unlocks in g can be the entry locks/unlocks for f too, and (2) the exit summary of g is part of the exit summary of f as the exit locks in g can be the exit locks in f .

6.2.4.2 Pairing Algorithm

Listing 6.1 describes the pairing algorithm. It iterates over the set of signatures and takes as input: the matching pair graph M P Go, the EFG for each function within M P Go. Then, it

1 main ( M P Go) 2 f u n c t i o n s ← r e v e r s e t o p o l o g i c a l s o r t ( M P Go) ; 3 f o r ( e a c h f u n c t i o n i n f u n c t i o n s ) 4 e f g ← g e t E F G ( f u n c t i o n ) ; 5 e n t r y n o d e ← g e t e n t r y n o d e ( e f g ) ; 6 e x i t n o d e ← g e t e x i t n o d e ( e f g ) ; 7 n o d e s u m m a r y ← { p r e s u m : { } , p o s t s u m : { } } ; 8 traverse efg ( e n t r y n o d e , n o d e s u m m a r y ) ; 9 summary . e n t r y s u m m a r y ← p r e s u m f o r e n t r y n o d e ; 10 summary . e x i t s u m m a r y ← p o s t s u m f o r e x i t n o d e ; 11 s u m m a r i e s . p u t ( f u n c t i o n , summary ) ; 12 i f ( f u n c t i o n ∈ r o o t s ( M P Go) ) AND ( summary . e x i t s u m m a r y c o n t a i n s a l o c k ) 13 r e p o r t t h e l o c k ( s ) i n summary . e x i t s u m m a r y a s u n p a i r e d l o c k ( s ) ; 14 end 15

16 traverse efg ( node , n s )

17 i f n o d e c o n t a i n s a l o c k f u n c t i o n c a l l 18 i f n s . p o s t s u m c o n t a i n s a l o c k 19 r e p o r t a p o t e n t i a l d e a d l o c k b e t w e e n t h e c u r r e n t l o c k t h e l o c k ( s ) i n n s . p o s t s u m . 20 u p d a t e t h e p r e s u m and p o s t s u m o f n s w i t h t h e c u r r e n t l o c k . 21 e l s e i f t h e n o d e i s a c a l l − s i t e f o r f u n c t i o n w i t h i n M P Go 22 sum ← s u m m a r i e s . g e t ( c a l l e d f u n c t i o n by n o d e ) ; 23 i f ( n s . p o s t s u m c o n t a i n s a l o c k ) AND ( sum . e n t r y s u m m a r y c o n t a i n s a l o c k ) 24 r e p o r t a p o t e n t i a l d e a d l o c k b e t w e e n t h e l o c k ( s ) i n n s . p o s t s u m and t h e l o c k ( s ) i n sum . e n t r y s u m m a r y ; 25 n s . p r e s u m ← sum . e n t r y s u m m a r y ; 26 n s . p o s t s u m ← sum . e x i t s u m m a r y ; 27 e l s e i f n o d e i s u n l o c k 28 u p d a t e t h e p r e s u m and p o s t s u m o f n s w i t h t h i s u n l o c k ; 29

30 i f n o d e i s v i s i t e d b e f o r e AND n s h o l d s t h e same summary when t h e n o d e p r e v i o u s l y

v i s i t e d 31 r e t u r n n s . p r e s u m ; 32 33 p r e s u m ← { } ; 34 f o r ( e a c h s i n s u c c e s s o r s ( n o d e ) ) 35 p r e s u m + = traverse efg ( s , n s ) ; 36 i f n s . p o s t s u m m a r y c o n t a i n s a l o c k AND p r e s u m m a r y c o n t a i n s an u n l o c k 37 r e p o r t l o c k / u n l o c k p a i r i n g b e t w e e n t h e l o c k ( s ) i n n s . p o s t s u m m a r y and t h e u n l o c k ( s ) i n p r e s u m ; 38 u p d a t e n s . p r e s u m w i t h p r e s u m ; 39 r e t u r n n s . p r e s u m ; 40 end

Listing 6.1 Pairing Algorithm

The pairing algorithm (Listing 6.1) starts by visiting the matching pair graph M P Go in a

bottom-up manner (lines 2-13). For each function in M P Go, the algorithm retrieves the EFG

(line 4), gets the entry/exit nodes (lines 5-6), and passes the entry node and its empty node summary to the function traverse efgto start the EFG traversal in a depth-first manner (line 8). Upon the return of traverse efg(lines 9-12), the function summary forfunctionis computed as follows: theentry summaryis the same as the entry summary (pre sum) for the EFG entry node (line 9), and theexit summaryis equivalent to the (post sum) of the EFG exit node (line 10). This function summary is then stored in a global structure (summaries) for later use (line 11). Lines (12-13) check whether the current function is one of the roots in M P Go and the exit summary of its summary contains a lock. If so, it reports the lock(s) inexit summaryas unpaired locks.

Lines (17-20) of function traverse efgcheck whether the currently visited node (node) con- tains a lock function call, if that is the case: the algorithm checks if there is a potential deadlock by checking if the post sum of the previously visited node contains a lock. If so, it reports a potential deadlock between the lock in ns.post sumand the current lock atnode. Finally, it up- dates the current node summary with the current lock. In lines (21-26), the algorithm checks if the current node (node) is a call-site for a function within M P Go, then if theentry summaryof the called function contains a lock and the post sum of previously visited node contains a lock (line 23): the algorithm reports a potential deadlock between the lock(s) inns.post sumand the lock(s) insum.entry summary(line 24). At lines 25-26, the current node summary is updated with the summary of the called function. In case of the current visited node is an unlock function call (lines 27-28), then the algorithm updates the current node summary with the current unlock.

In our pairing algorithm, traverse efg can visit an EFG node multiple times if new infor- mation that affects the locking/unlocking analysis is present. At lines (30-31), the algorithm checks whether the current node is visited before and it stops traversing through this node, if the current node summary is the same as the one when previously visited. In other words, if no new information is presented at this node, then no need to re-visit the node. Otherwise, the traversal continues to line 33. Lines (33-35) iterate through the successors of the current node and passes each successor totraverse efgto recursively visit subsequent nodes. Upon the return of traverse efg, thepre sumis updated with the entry summary for each of its successors. Once iterating through the successors is completed (lines 36-37), the algorithm checks whether

the post sum of the current node contains a lock and thepre sum of the successors contains an

unlock, if that is the case: the algorithm reports locks/unlocks pairing between the lock(s) in

ns.post sum and the unlock(s) inpre sum of successors. Then at line 38, the algorithm updates

thepre sumof the current node’s with thepre sum of successors. Finally, the updatedpre sumfor

the current node is returned (line 39).