Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / vm / serialize / AdaptiveSerializer.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.vm.serialize;
20
21 import gov.nasa.jpf.vm.ChoiceGenerator;
22 import gov.nasa.jpf.vm.ElementInfo;
23
24 /**
25  * a CG type adaptive, canonicalizing & filtering serializer that is an
26  * under-approximation mostly aimed at finding data races and deadlocks in programs
27  * with a large number of scheduling points (= thread choices)
28  *
29  * This came to bear by accidentally discovering that JPF often seems to finds
30  * concurrency defects by just serializing the thread states, their topmost stack
31  * frames and the objects directly referenced from there.
32  * For non-scheduling points, we just fall back to serializing statics, all thread
33  * stacks and all the data reachable from there
34  */
35 public class AdaptiveSerializer extends CFSerializer {
36
37   boolean traverseObjects;
38   boolean isSchedulingPoint;
39
40   @Override
41   protected void initReferenceQueue() {
42     super.initReferenceQueue();
43     traverseObjects = true;
44
45     ChoiceGenerator<?> nextCg = vm.getNextChoiceGenerator();
46     isSchedulingPoint = (nextCg != null) && nextCg.isSchedulingPoint();
47   }
48
49   @Override
50   protected void queueReference(ElementInfo ei){
51     if (traverseObjects){
52       refQueue.add(ei);
53     }
54   }
55
56   @Override
57   protected void processReferenceQueue() {
58     if (isSchedulingPoint){
59       traverseObjects = false;
60     }
61     refQueue.process(this);
62   }
63
64   //@Override
65   @Override
66   protected void serializeClassLoaders(){
67     // for thread CGs we skip this - assuming that this is only relevant if there is
68     // a class object lock, which is covered by the thread lock info
69     if (!isSchedulingPoint){
70       // <2do> this seems too conservative - we should only serialize what is
71       // used from this thread, which can be collected at class load time
72       // by looking at GET/PUTSTATIC targets (and their superclasses)
73       super.serializeClassLoaders();
74     }
75   }
76 }