Catholic University in Ružomberok
Scientific Issues, Mathematica II, Ružomberok 2009
Verification of Java programs using networks of
finite automata with discrete data.
Bożena Woźna, Andrzej Zbrzezny
Institute of Mathematics and Computer Science
Jan Dlugosz University of Czstochowa
al. Armii Krajowej 13/15, 42-200 Czstochowa, Poland
e-mail: {b.wozna, a.zbrzezny}@ajd.czest.pl
Abstract. In the paper we show that automatic verification of Java
programs is feasible. In particular, we show that for a given Java program it is possible to build a network of Finite Automata with Discrete
Data (FADDs) that models the behaviour of the program under consideration. Such a model enables to verify some interesting properties of
the considered program by means of tools like Uppaal and VerICS.
1
Introduction
The model checking tools like VerICS [4] or Uppaal [1] accept a description
of a network of finite automata with discrete data (FADDs) [7] as input. This
FADD formalism is accurate enough to detect concurrency errors and yet abstract enough to make model checking tractable. So, in order to automatically
verify Java programs one has to bridge the semantic gap between a non-finitestate system expressed as a Java source code, and those tools input languages.
This requires the application of sophisticated transformation techniques, and
one of such methods is shown in this paper; it has been implemented as a
J2FADD module of the tool VerICS.
We illustrate our approach by presenting the automata models of the two
well known solutions (written in Java) of the dining philosophers problem
(DPP) - an illustrative example of a common computing problem in concurrency. We also provide some experimental results that help to evaluate our
method.
The subset of Java that can be translated to FADDs contains: definitions of integer variables, standard programming language constructs like
assignments, expressions with most operators, conditional statements and
loops (for, while, do while), instructions break and continue without labels, definitions of classes, (static) objects, constructors and methods, static
and non–static methods, and synchronisation methods and blocks. There
are also standard thread creation constructs recognised and special methods:
Thread.wait(), Thread.notify(), and Random.nextInt(int), the methods
Thread.join(Thread) and Thread.notifyAll(). For more details we refer
to [8].
To the best of our knowledge, there are only two other model checkers for
verification of Java programs: JavaPathFinder [5] and Bandera [2]. However,
both of them operate on the Java bytecode, and do not produce a low-level
models like finite automata with discrete data.
2
Dining Philosophers Problem (DPP)
In this section we show FADD models of the two well known solutions of DPP,
and we model check it by means of the tools: Uppaal, VerICS. In particular,
we search for deadlocks, which can be defined formally as follows [6]: A set of
processes is deadlocked if each process in the set is waiting for an event that
only another process in the set can cause.
FADDs are standard automata augmented to include integer variables over
which standard arithmetic and Boolean expressions can be defined. These
automata take as an input a set of initialised integer variables and a set of
propositional variables, true at particular states.
2.1
Problem description
The description of the dining philosophers problem (DPP) we provide below
is based on that in [3]. Consider n (n ≥ 2) philosophers. Each philosopher
has a room in which he engages in his professional activity of thinking. There
is also a common dining room, furnished with a circular table, surrounded by
n chairs, each labelled by the name of the philosopher who is to sit in it. On
the left of each philosopher there is a fork, and in the centre stands a large
bowl of spaghetti, which is constantly replenished. Whenever a philosopher
eats he has to use both forks, the one on the left and the other on the right
of his plate. A philosopher is expected to spend most of his time thinking,
2
but when he feels hungry, he goes to the dining room, sits down on his own
chair, and picks up the fork on his left provided it is not used by the other
philosopher. If the other philosopher uses it, he just has to wait until the fork
is available. Then the philosopher tries pick up the fork on his right. When a
philosopher has finished he puts down both his forks, exits dining-room and
continues thinking.
2.2
Possible solutions
We have implemented a possible solution of the DPP problem that could lead
to a deadlock (see Listing 1). The deadlock can happen, if every philosopher
sits down on his own chair at the same time and picks up his left fork. Then
all forks are locked and none of the philosophers can successfully pick up
his right fork. As a result, every philosopher waits for his right fork that is
currently being locked by his right neighbour, and hence a deadlock occurs.
The results for the deadlock property are in Table 1.
Tools
No. Ph
sec.
MB
J2FADD + VerICS
5
12 217 279.2
J2FADD + Uppaal
60
1.16
41.9
Table 1: Dining Philosophers. Deadlock.
Assume now another solution for DPP (see Listing 2), where there is
a lackey who ensures that at most n − 1 philosophers can be present in the
dining room at the same time. This lackey ensures that no deadlock is possible
(see Table 2 for the results).
Tools
No. Ph
sec.
MB
J2TADD + Uppaal
5
52.22 418.7
J2TADD + Uppaal
6
Table 2: Dining Philosophers. Absence of deadlocks.
All of the experiments have been performed on a computer equipped with
the processor Intel Core 2 Duo (2 GHz), 2 GB main memory and the operating
system Linux.
2.3
Automata models for the solution without lackey
In this section we present an automata model for DPP with three philosophers
(see Listing 1). The model consists of a network of the following six FADDs:
one FADD for each Philosopher, and one FADD for each Fork. On Figure 2
we show an automaton for Philosopher p0, and on Figure 1 an automaton
for the Fork f ork0. The automata for philosophers p1 and p2 are analogous
3
public c l a s s C o l l e g e 3 {
public s t a t i c void main ( S t r i n g a r g s [ ] ) {
Fork f o r k 0 = new Fork ( f a l s e ) ;
Fork f o r k 1 = new Fork ( f a l s e ) ;
Fork f o r k 2 = new Fork ( f a l s e ) ;
P h i l o s o p h e r p0 = new P h i l o s o p h e r ( 0 , f o r k 0 , f o r k 1 ) ;
P h i l o s o p h e r p1 = new P h i l o s o p h e r ( 1 , f o r k 1 , f o r k 2 ) ;
P h i l o s o p h e r p2 = new P h i l o s o p h e r ( 2 , f o r k 2 , f o r k 0 ) ;
(new Thread ( p0 ) ) . s t a r t ( ) ; (new Thread ( p1 ) ) . s t a r t ( ) ;
(new Thread ( p2 ) ) . s t a r t ( ) ;
}
}
c l a s s Fork {
private boolean u n a v a i l a b l e ;
public Fork ( boolean u n a v a i l a b l e ) { t h i s . u n a v a i l a b l e = u n a v a i l a b l e ; }
public synchronized void a c q u i r e ( ) {
while ( u n a v a i l a b l e ) {
try { w a i t ( ) ; } catch ( I n t e r r u p t e d E x c e p t i o n e ) { }
}
u n a v a i l a b l e = true ;
}
public synchronized void r e l e a s e ( ) { u n a v a i l a b l e = f a l s e ; n o t i f y ( ) ; }
}
c l a s s P h i l o s o p h e r implements Runnable {
private i n t nr ;
private Fork l e f t , r i g h t ;
public P h i l o s o p h e r ( i n t nr , Fork l e f t , Fork r i g h t ) {
t h i s . nr = nr ;
this . l e f t = l e f t ;
this . r i ght = r i ght ;
}
public void run ( ) {
while ( true ) {
l e f t . acquire ( ) ; right . acquire ( ) ;
right . release ( ); l e f t . release ( ) ;
}
}
}
Listing 1: Java source code of DPP (3 Philosophers). The main class.
to that of Philosopher p0, and the automata for forks f ork1 and f ork2 are
analogous to that of Fork f ork0.
The automaton for a philosopher reflects the method run of the class
Philosopher. Namely, the fragment consisting of the locations: v11, v12,
v13, v10, v9, v14, v15, v16 reflects the instruction left.acquire(); the fragment consisting of the locations: v16, v17, v18, v8, v7, v19, v20, v21 reflects
the instruction right.acquire(); the fragment consisting of the locations:
v21, v22, v23, v24, v25 reflects the instruction right.release(); and the
fragment consisting of the locations: v25, v26, v27, v28, v11 reflects the instruction left.release().
The automaton for a fork consists of two locations, operates as a binary
semaphore, and implements critical sections.
4
public c l a s s C o l l e g e 3 L {
public s t a t i c void main ( S t r i n g a r g s [ ] ) {
Fork f 0 = new Fork ( f a l s e ) ; Fork f 1 = new Fork ( f a l s e ) ;
Fork f 2 = new Fork ( f a l s e ) ; Lackey s = new Lackey ( 2 ) ;
P h i l o s o p h e r p0 = new P h i l o s o p h e r ( 0 , f 0 , f 1 , s ) ;
P h i l o s o p h e r p1 = new P h i l o s o p h e r ( 1 , f 1 , f 2 , s ) ;
P h i l o s o p h e r p2 = new P h i l o s o p h e r ( 2 , f 2 , f 0 , s ) ;
(new Thread ( p0 ) ) . s t a r t ( ) ; (new Thread ( p1 ) ) . s t a r t ( ) ;
(new Thread ( p2 ) ) . s t a r t ( ) ;
}
}
c l a s s Fork {
private boolean u n a v a i l a b l e ;
public Fork ( boolean u n a v a i l a b l e ) { t h i s . u n a v a i l a b l e = u n a v a i l a b l e ; }
public synchronized void a c q u i r e ( ) {
while ( u n a v a i l a b l e ) { try { w a i t ( ) ; } catch ( E x cep ti o n e ) { } }
u n a v a i l a b l e = true ;
}
public synchronized void r e l e a s e ( ) { u n a v a i l a b l e = f a l s e ; n o t i f y ( ) ; }
}
c l a s s Lackey {
private i n t m; private i n t max ;
public Lackey ( i n t max ) { t h i s . max = max ; }
public synchronized void a c q u i r e ( ) {
while (m >= max) { try { w a i t ( ) ; } catch ( E x cep ti o n e ) {} } ++m;
}
public synchronized void r e l e a s e ( ) { −−m; n o t i f y ( ) ; }
}
c l a s s P h i l o s o p h e r implements Runnable {
private i n t nr ; private Lackey s ;
private Fork l e f t , r i g h t ;
public P h i l o s o p h e r ( i n t nr , Fork l e f t , Fork r i g h t , Lackey s ) {
t h i s . nr = nr ; t h i s . s = s ;
this . l e f t = l e f t ; this . r i ght = r i ght ;
}
public void run ( ) {
while ( true ) {
s . acquire ( ) ; l e f t . acquire ( ) ; right . acquire ( ) ;
right . release ( ); l e f t . release ( ) ; s . release () ;
}
}
}
Listing 2: Java source code of DPP (3 Philosophers). The main class.
2.4
Automata models for the solution with lackey
In this section we present an automata model for DPP with lackey and three
philosophers (see Listing 2). The model consists of a network of the following
seven FADDs: one FADD for each Philosopher, one FADD for each Fork,
and one FADD for the Lackey. On Figure 4 we show an automaton for
Philosopher p0, and on Figure 3 automata for the Fork f ork0 and for the
Lackey s. The automata for philosophers p1 and p2 are analogous to that of
Philosopher p0, and the automata for forks f ork1 and f ork2 are analogous
5
e1!
e3!
e2!
e0!
v2
e6!
v1
z3 = z3 + 1
e5!
e4!
e7! z3 = z3 + 1
Figure 1: The automaton for Fork f ork0.
v18
v8
v22
v23
e14?
z1 = 0
z1 == 1
e24?
e10?
v16
e10?
v24
e8?
v21
v17
e4?
e33!
z4 = z4 - 1
z4 == 0
v7
e12?
z1 == 0
e12?
v25
v15
z1 = 1
z0 = 1
v19
e2?
v20
v14
v10
v26
v13
e6?
z0 == 0
e25?
z3 == 0
v11
e0?
v9
z0 = 0
z0 == 1
e2?
e4?
v12
v28
z3 = z3 - 1
e36!
v27
Figure 2: The automaton for Philosopher p0.
to that of Fork f ork0.
The automaton for a philosopher reflects the method run of the class
Philosopher. Namely, the fragment consisting of the locations: v15, v16,
v17, v14, v13, v18, v19, v20 reflects the instruction s.acquire(); the fragment consisting of the locations: v20, v21, v22, v23, v11, v12, v24, v25 reflects
the instruction left.acquire(); the fragment consisting of the locations:
v25, v26, v27, v28, v10, v9, v29, v30 reflects the instruction right.acquire();
the fragment consisting of the locations: v30, v31, v32, v33, v34 reflects the
instruction right.release(); the fragment consisting of the locations: v34,
v35, v36, v37, v38 reflects the instruction left.release(); and the fragment
consisting of the locations: v38, v39, v40, v41, v15 reflects the instruction
6
s.release().
The automaton for a fork consists of two locations, operates as a binary
semaphore, and implements locking a fork. The automaton for a lackey consists of two locations, operates as a bounded semaphore, and implements
locking the dining room.
e33! z8 = z8 + 1
e34! z8 = z8 + 1
e30!
e32!
e31!
v8
z8 = z8 + 1
e35!
v7
e24!
v2
e25!
z5 = z5 + 1 e6!
e2!
e0!
v1
e3!
e27!
e28!
e26!
e29!
e1!
e4!
e5!
e7! z5 = z5 + 1
The automaton for Lackey s;
The automaton for Fork f ork0.
Figure 3:
3
Conclusions
In the paper we have shown that model checking of Java programs by means
of tools Uppaal and VerICS that accept a description of a network of FADDs
is feasible. We have presented the method using the dining philosophers
problem, a classic multi-process synchronisation problem
The experiments confirm that our approach provides a valuable aid for
Java software verification.
References
[1] J. Bengtsson, K. Larsen, F. Larsson, P. Pettersson, W. Yi, and C. Weise.
New generation of Uppaal. In Proceedings of the International Workshop
on Software Tools for Technology Transfer, 1998.
[2] J. Corbett, M. Dwyer, J. Hatcliff, Robby C. Pasareanu, S. Laubach, and
H. Zheng. Bandera: Extracting finite-state models from java source code.
In Proceedings of the 22nd International Conference on Software Engineering(ICSE ’00), pages 439–448, New York, NY, USA, 2000. ACM Press.
[3] C.A.R. Hoare. Communicating sequential processes. Prentice Hall, 1985.
[4] A. Niewiadomski W. Penczek A. Pólrola M. Szreter B. Woźna
M. Kacprzak, W. Nabialek and A. Zbrzezny. VerICS 2007 - a model
7
v30
z6 = z6 - 1
e49!
v32
v31
e10?
v33
z1 = 0
z6 == 0
e12?
e12?
v29
v9
v10
e36?
v34
z1 = 1
e2?
e14?
e8?
v28
z1 == 0
z1 == 1
v35
v27
z0 = 0
v26
v25
e10?
v14
e33?
e39?
z0 = 1
v37
e0?
z0 == 1
e4?
z0 == 0
e30?
e2?
v38
v21
z3 < z4
e56! z8 = z8 - 1
e27?
e30?
v16
z5 = z5 - 1
e52!
v20
v19
z3 = z3 + 1
z5 == 0
v11
v23
v18
e24?
v22
e37?
e38?
v17
v36
e4?
v13
z3 >= z4
v12
e6?
v24
v41
e55! z8 = z8 - 1
e27?
v40
z3 = z3 - 1
v15
z8 == 0
v39
Figure 4: The automaton for Philosopher p0.
checker for knowledge and real-time. Fundamenta Informaticae, 85(14):313–328, 2008.
[5] C. Pasareanu and W. Visser. Verification of Java Programs Using Symbolic Execution and Invariant Generation. In Proceedings of SPIN’04,
volume 2989 of LNCS, pages 164–181. Springer-Verlag, 2004.
[6] Andrew S. Tanenbaum. Modern Operating Systems. Prentice Hall, Vrije
University, Amsterdam, The Netherlands, 2/e edition, 2001.
[7] A. Zbrzezny and A. Pólrola. Sat-based reachability checking for timed
automata with discrete data. Fundamenta Informaticae, 79(3–4):579–593,
2007.
[8] A. Zbrzezny and B. Woźna. Towards verification of Java programs in
VerICS. Fundamenta Informaticae, 85(1-4):533–548, 2008.
8
© Copyright 2026 Paperzz