The higher the sleep score, the more likely the fuzzer makes a thread sleep
[c11tester.git] / predicate.cc
1 #include "funcinst.h"
2 #include "predicate.h"
3 #include "concretepredicate.h"
4
5 Predicate::Predicate(FuncInst * func_inst, bool is_entry, bool is_exit) :
6         func_inst(func_inst),
7         entry_predicate(is_entry),
8         exit_predicate(is_exit),
9         does_write(false),
10         exploration_count(0),
11         failure_count(0),
12         sleep_score(100),
13         pred_expressions(16),
14         children(),
15         parent(NULL),
16         exit(NULL),
17         backedges(16)
18 {}
19
20 Predicate::~Predicate()
21 {
22         // parent and func_inst should not be deleted
23         pred_expressions.reset();
24         backedges.reset();
25         children.clear();
26 }
27
28 unsigned int pred_expr_hash(struct pred_expr * expr)
29 {
30         return (unsigned int)((uintptr_t)expr);
31 }
32
33 bool pred_expr_equal(struct pred_expr * p1, struct pred_expr * p2)
34 {
35         if (p1->token != p2->token)
36                 return false;
37         if (p1->token == EQUALITY && p1->func_inst != p2->func_inst)
38                 return false;
39         if (p1->value != p2->value)
40                 return false;
41         return true;
42 }
43
44 void Predicate::add_predicate_expr(token_t token, FuncInst * func_inst, bool value)
45 {
46         struct pred_expr *ptr = new pred_expr(token, func_inst, value);
47         pred_expressions.add(ptr);
48 }
49
50 void Predicate::add_child(Predicate * child)
51 {
52         /* check duplication? */
53         children.push_back(child);
54 }
55
56 void Predicate::copy_predicate_expr(Predicate * other)
57 {
58         PredExprSet * other_pred_expressions = other->get_pred_expressions();
59         PredExprSetIter * it = other_pred_expressions->iterator();
60
61         while (it->hasNext()) {
62                 struct pred_expr * ptr = it->next();
63                 struct pred_expr * copy = new pred_expr(ptr->token, ptr->func_inst, ptr->value);
64                 pred_expressions.add(copy);
65         }
66 }
67
68 /* Evaluate predicate expressions against the given inst_act_map */
69 ConcretePredicate * Predicate::evaluate(inst_act_map_t * inst_act_map, thread_id_t tid)
70 {
71         ConcretePredicate * concrete = new ConcretePredicate(tid);
72         PredExprSetIter * it = pred_expressions.iterator();
73
74         while (it->hasNext()) {
75                 struct pred_expr * ptr = it->next();
76                 uint64_t value = 0;
77
78                 switch(ptr->token) {
79                         case NOPREDICATE:
80                                 break;
81                         case EQUALITY:
82                                 FuncInst * to_be_compared;
83                                 ModelAction * last_act;
84
85                                 to_be_compared = ptr->func_inst;
86                                 last_act = inst_act_map->get(to_be_compared);
87                                 value = last_act->get_reads_from_value();
88                                 break;
89                         case NULLITY:
90                                 break;
91                         default:
92                                 break;
93                 }
94
95                 concrete->add_expression(ptr->token, value, ptr->value);
96         }
97
98         return concrete;
99 }
100
101 void Predicate::incr_expl_count()
102 {
103         exploration_count++;
104 }
105
106 void Predicate::incr_fail_count()
107 {
108         failure_count++;
109 }
110
111 void Predicate::incr_sleep_score(uint32_t amount)
112 {
113         if (sleep_score + amount > 100)
114                 sleep_score = 100;
115         else
116                 sleep_score += amount;
117 }
118
119 void Predicate::decr_sleep_score(uint32_t amount)
120 {
121         if (sleep_score > amount)
122                 sleep_score -= amount;
123         else
124                 sleep_score = 0;
125 }
126
127 void Predicate::print_predicate()
128 {
129         model_print("\"%p\" [shape=box, label=\"\n", this);
130         if (entry_predicate) {
131                 model_print("entry node\"];\n");
132                 return;
133         }
134
135         if (exit_predicate) {
136                 model_print("exit node\"];\n");
137                 return;
138         }
139
140         func_inst->print();
141
142         if (pred_expressions.getSize() == 0)
143                 model_print("predicate unset\n");
144
145         PredExprSetIter * it = pred_expressions.iterator();
146         while (it->hasNext()) {
147                 struct pred_expr * expr = it->next();
148                 FuncInst * inst = expr->func_inst;
149                 switch (expr->token) {
150                         case NOPREDICATE:
151                                 break;
152                         case EQUALITY:
153                                 model_print("predicate token: equality, position: %s, value: %d\n", inst->get_position(), expr->value);
154                                 break;
155                         case NULLITY:
156                                 model_print("predicate token: nullity, value: %d\n", expr->value);
157                                 break;
158                         default:
159                                 model_print("unknown predicate token\n");
160                                 break;
161                 }
162         }
163
164         if (does_write) {
165                 model_print("Does write\n");
166         }
167         model_print("Count: %d, failed count: %d\n", exploration_count, failure_count);
168         model_print("\"];\n");
169 }
170
171 void Predicate::print_pred_subtree()
172 {
173         print_predicate();
174         for (uint i = 0; i < children.size(); i++) {
175                 Predicate * child = children[i];
176                 child->print_pred_subtree();
177                 model_print("\"%p\" -> \"%p\"\n", this, child);
178         }
179
180         PredSetIter * it = backedges.iterator();
181         while (it->hasNext()) {
182                 Predicate * backedge = it->next();
183                 model_print("\"%p\" -> \"%p\"[style=dashed, color=grey]\n", this, backedge);
184         }
185
186         if (exit) {
187                 model_print("\"%p\" -> \"%p\"[style=dashed, color=red]\n", this, exit);
188         }
189 }