Java PathFinder (JPF) cs498dm Software Testing January 19, 2012.
Java PathFinder - D3S
Transcript of Java PathFinder - D3S
![Page 1: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/1.jpg)
CHARLES UNIVERSITY IN PRAGUE
http://d3s.mff.cuni.cz
faculty of mathematics and physics
Java PathFinder
Pavel Parízek
![Page 2: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/2.jpg)
Java PathFinder (JPF)
Pavel Parízek Java Pathfinder 2
Verification framework for Java programs
Explicit state space traversal (with POR)
Highly customizable and extensible (API)
Open source since April 2005
Maintainers: NASA Ames Research Center
Available on GitHub since 2018
WWW: https://github.com/javapathfinder/jpf-core
![Page 3: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/3.jpg)
What JPF really is ...
Pavel Parízek Java Pathfinder 3
Special JVM
Execution choices
Backtracking
State matching
State space exploration
assertions, deadlocks, races, ...
![Page 4: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/4.jpg)
General usage pattern
Pavel Parízek Java Pathfinder 4
Picture taken from JPF wiki (https://github.com/javapathfinder/jpf-core/wiki)
![Page 5: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/5.jpg)
Architecture
Pavel Parízek Java Pathfinder 5
Picture taken from JPF wiki (https://github.com/javapathfinder/jpf-core/wiki)
![Page 6: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/6.jpg)
Program state space in JPF
Pavel Parízek Java Pathfinder 6
States
Full snapshot of JVM
Dynamic heap
Thread stacks
Program counters
Static data (classes)
Locks and monitors
![Page 7: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/7.jpg)
Program state space in JPF
Pavel Parízek Java Pathfinder 7
Transitions
Non-empty sequences of bytecode instructions
Terminates when JPF makes a new choice
![Page 8: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/8.jpg)
Program state space in JPF
Pavel Parízek Java Pathfinder 8
Choices
Thread scheduling
Data (boolean, int)
![Page 9: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/9.jpg)
On-the-fly state space construction
Pavel Parízek Java Pathfinder 9
public Producer extends Thread {
void run() {
while (true) {
d.buf = i;
i++;
d.count++;
}
}
}
public Consumer extends Thread {
void run() {
while (true) {
k = d.buf;
print(k);
}
}
}
public static void main(...) {
Data d = new Data();
new Producer(d).start();
new Consumer(d).start();
}
<start threads>
P: buf = iP: i++
C: k = bufC: print(k)
P
C
![Page 10: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/10.jpg)
On-the-fly state space construction
Pavel Parízek Java Pathfinder 10
public Producer extends Thread {
void run() {
while (true) {
d.buf = i;
i++;
d.count++;
}
}
}
public Consumer extends Thread {
void run() {
while (true) {
k = d.buf;
print(k);
}
}
}
public static void main(...) {
Data d = new Data();
new Producer(d).start();
new Consumer(d).start();
}
<start threads>
P: buf = iP: i++
C: k = bufC: print(k)
P: count ++
C: k = bufC: print(k)
![Page 11: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/11.jpg)
On-the-fly state space construction
Pavel Parízek Java Pathfinder 11
public Producer extends Thread {
void run() {
while (true) {
d.buf = i;
i++;
d.count++;
}
}
}
public Consumer extends Thread {
void run() {
while (true) {
k = d.buf;
print(k);
}
}
}
public static void main(...) {
Data d = new Data();
new Producer(d).start();
new Consumer(d).start();
}
<start threads>
P: buf = iP: i++
C: k = bufC: print(k)
P: count ++
C: k = bufC: print(k)
P: buf = iP: i++
C: k = bufC: print(k)P: buf = i
P: i++
![Page 12: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/12.jpg)
Properties
Pavel Parízek Java Pathfinder 12
Built-in
Deadlock freedom
Race conditions
Uncaught exceptions
Assertions
![Page 13: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/13.jpg)
Features
Pavel Parízek Java Pathfinder 13
Partial order reduction
Class loading symmetry
Heap symmetry
Selected heuristics
![Page 14: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/14.jpg)
Running JPF
Pavel Parízek Java Pathfinder 14
![Page 15: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/15.jpg)
Running JPF
Pavel Parízek Java Pathfinder 15
Download JPF and unpack somewhere
http://d3s.mff.cuni.cz/teaching/program_analysis_verification/files/JPF.zip
Example: Dining Philosophers
Command: java -jar build\RunJPF.jar src\examples\DiningPhil.jpf
Output: application, error info, statistics
![Page 16: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/16.jpg)
Error info
Pavel Parízek Java Pathfinder 16
Full error trace (counterexample)
Snapshot of the error state
Message from the property checker
Command:java -jar build\RunJPF.jar
+report.console.property_violation
=trace,error,snapshot
src\examples\DiningPhil.jpf
![Page 17: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/17.jpg)
Running JPF
Pavel Parízek Java Pathfinder 17
Examples
BoundedBuffer
Crossing
oldclassic
Racer
![Page 18: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/18.jpg)
JPF API
Pavel Parízek Java Pathfinder 18
![Page 19: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/19.jpg)
JPF API
Pavel Parízek Java Pathfinder 19
ListenersInspecting current program state
Custom properties
Search driver
AdvancedInstruction factory (bytecode interpreter)
Scheduler (sync policy, sharedness policy)
![Page 20: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/20.jpg)
Listeners
Pavel Parízek Java Pathfinder 20
Observer design pattern
Notified about specific events
JVM: bytecode instruction executed, new heap object allocated, start of a new thread
State space traversal: new state, backtrack, finish
Inspecting current program state
heap objects, local variables, thread call stacks, ...
![Page 21: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/21.jpg)
Listeners
Pavel Parízek Java Pathfinder 21
SearchListener
VMListener
ListenerAdapter
Examples (source code)JPF/src/main/gov/nasa/jpf/listener
![Page 22: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/22.jpg)
Custom properties
Pavel Parízek Java Pathfinder 22
Property
GenericProperty
PropertyListenerAdapter
Common practice: decide property status based on listener notifications (and program state)
Examples (source code)JPF/src/main/gov/nasa/jpf/vm
![Page 23: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/23.jpg)
Registering listeners and properties
Pavel Parízek Java Pathfinder 23
listener=<class name 1>,...,<class N>
search.listener=...
search.properties=...
![Page 24: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/24.jpg)
Listeners: tracking bytecode instructions
Pavel Parízek Java Pathfinder 24
ExecTracker
ObjectTracker
![Page 25: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/25.jpg)
Listeners: inspecting program state
Pavel Parízek Java Pathfinder 25
CallMonitor
ObjectTracker
![Page 26: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/26.jpg)
Task 1
Pavel Parízek Java Pathfinder 26
Write your own listenerAfter every field write instruction, print the field name and new valueBefore every method call (invoke), print values of all parameters supplied by the caller
Use existing classes as a basic templateListenerAdapter, VMListener, CallMonitor, ObjectTrackersrc/main/gov/nasa/jpf/listener/*src/main/gov/nasa/jpf/jvm/bytecode/*
Ask questions !!
![Page 27: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/27.jpg)
Configuration properties
Pavel Parízek Java Pathfinder 27
File jpf.properties
![Page 28: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/28.jpg)
JPF wiki
Pavel Parízek Java Pathfinder 28
Main page
https://github.com/javapathfinder/jpf-core/wiki
User guidehttps://github.com/javapathfinder/jpf-core/wiki/How-to-use-JPF
Internals (developer guide)https://github.com/javapathfinder/jpf-core/wiki/Developer-guide
![Page 29: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/29.jpg)
JPF source code tree
Pavel Parízek Java Pathfinder 29
src/main/gov/nasa/jpf
the “main” class (JPF), interfaces
vm: virtual machine, choices, built-in properties
jvm: Java bytecode specific, instructions, class file
search: search driver, heuristics
util: custom data structures, utility classes
report: reporting system (console, XML)
listener: various listeners
![Page 30: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/30.jpg)
JPF and native methods
Pavel Parízek Java Pathfinder 30
![Page 31: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/31.jpg)
JPF and native methods
Pavel Parízek Java Pathfinder 31
Support for all Java bytecode instructions
but some library methods are native
file I/O, GUI, networking, ...
Problem
JPF cannot determine how execution of a native method changes the program state
Solution: Model-Java Interface (MJI)
![Page 32: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/32.jpg)
Model-Java Interface (MJI)
Pavel Parízek Java Pathfinder 32
Executing native methods in the underlying JVMSimilar mechanism to Java-Native Interface (JNI)Custom versions of some Java library classes
Object, Thread, Class, java.util.concurrent.*, ...
![Page 33: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/33.jpg)
Environment construction
Pavel Parízek Java Pathfinder 33
![Page 34: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/34.jpg)
Environment construction
Pavel Parízek Java Pathfinder 34
Why: some programs do not contain “main”libraries, components, plug-ins
Problem: JPF accepts only complete programs
Solution: create artificial environment
Program with multiple threads and data choices
Also called “test driver”
![Page 35: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/35.jpg)
Example
Pavel Parízek Java Pathfinder 35
Program: java.util.HashMap
public class PutTh
extends Thread {
Map m;
public void run() {
m.put("1", "abc");
m.put("2", "def");
}
}
public class GetTh
extends Thread {
Map m;
public void run() {
m.get("1");
m.get("0");
}
}
public static void main(...) {
Map m = new HashMap();
Thread th1 = new PutTh(m);
Thread th2 = new GetTh(m);
th1.start();
th2.start();
th1.join();
th2.join();
}
![Page 36: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/36.jpg)
Environment construction – challenges
Pavel Parízek Java Pathfinder 36
Coverage
Should trigger all (most) execution paths, thread interleavings, and error states
Approach
Different method call sequences
Many combinations of parameter values
Several concurrent threads
State explosion
Use the least possible number of concurrent threads (2)
Reasonable number of parameter values (domain size)
![Page 37: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/37.jpg)
Using the Verify class
Pavel Parízek Java Pathfinder 37
JPF-aware test drivers (environments)Checking program behavior for different inputs
Data choiceimport gov.nasa.jpf.vm.Verify
if (Verify.getBoolean())
int x = Verify.getInt(0,10)
Search pruningVerify.ignoreIf(cond)
![Page 38: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/38.jpg)
Task 2
Pavel Parízek Java Pathfinder 38
Write reasonable environment for
java.util.LinkedList
java.util.concurrent.Semaphore
Run JPF on the complete program
Enable search for data race conditions
Use: gov.nasa.jpf.listener.PreciseRaceDetector
Try different workloads (threads, input data)
![Page 39: Java PathFinder - D3S](https://reader031.fdocuments.in/reader031/viewer/2022013001/61ca86f9e06f751b1d5b0d78/html5/thumbnails/39.jpg)
Time for questions about JPF
Pavel Parízek Java Pathfinder 39
ArchitectureImplementationHow something worksPublic APIOutput
Play with JPFlook into source code & try examples
Explore wikihttps://github.com/javapathfinder/jpf-core/wiki
Ask questions