-
Notifications
You must be signed in to change notification settings - Fork 2
/
LockSetChecker.cpp
168 lines (127 loc) · 4.09 KB
/
LockSetChecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* LockSetChecker.cpp
*
* Created on: Aug 28, 2014
* Author: wilhelma
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include "LockSetChecker.h"
#include "Event.h"
#include "ShadowThread.h"
#include "ShadowVar.h"
#include "ShadowLock.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
LockSetChecker::LockSetChecker(const char *outFile) : outFile_(outFile) {
}
LockSetChecker::~LockSetChecker() {
dumpRaceEntries(outFile_);
}
void LockSetChecker::create(const Event* e) {
ShadowThread* childThread =
dynamic_cast<const NewThreadEvent*>(e)->getNewThreadInfo()->childThread;
// LockSet_u = set of all possible locks
lockSet_[childThread] = lockSet_[e->getThread()];
}
void LockSetChecker::join(const Event* e) {
}
void LockSetChecker::acquire(const Event* e) {
// LockSet_t = LockSet_t + {lock}
lockSet_[e->getThread()].insert(((AcquireEvent*)e)->getAcquireInfo()->lock);
}
void LockSetChecker::release(const Event* e) {
// LockSet_t = LockSet_t - {lock}
auto lock = ((AcquireEvent*)e)->getAcquireInfo()->lock;
lockSet_[e->getThread()].erase(lock);
}
void LockSetChecker::access(const Event* e) {
const AccessEvent *event = dynamic_cast<const AccessEvent*>(e);
const RefId ref = event->getAccessInfo()->var->id;
const ThreadId threadId = event->getThread()->threadId;
if (event->getAccessInfo()->var->type == ShadowVar::STACK)
return;
switch(event->getAccessInfo()->type) {
case Access::READ:
{
readVarSet_[ref][threadId].instruction =
event->getAccessInfo()->instructionID;
// R_x[t].lockset = LockSet_t
readVarSet_[ref][threadId].lockset = lockSet_[e->getThread()];
if (lsIsEmptySet( readVarSet_[ref][threadId].lockset,
writeVarSet_[ref].lockset) ) {
raceEntries_.push_back(
std::unique_ptr<RaceEntry_>(new RaceEntry_(
WRITE_READ,
writeVarSet_[ref].instruction,
event->getAccessInfo()->instructionID,
ref)
));
std::cout << "race detected..1" << std::endl;
}
}
break;
case Access::WRITE:
{
// W_x.lockset = W_x.lockset intersect Lockset_t
lsIntersect(writeVarSet_[ref].lockset, lockSet_[e->getThread()]);
// check W_x.lockset = empty
if (writeVarSet_[ref].lockset.empty()) {
raceEntries_.push_back(
std::unique_ptr<RaceEntry_>(new RaceEntry_(
WRITE_WRITE,
writeVarSet_[ref].instruction,
event->getAccessInfo()->instructionID,
ref)
));
std::cout << "race detected..2" << std::endl;
}
}
default:
break;
}
}
void LockSetChecker::dumpRaceEntries(const char* fileName) const {
rapidjson::Document doc;
doc.SetObject();
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
rapidjson::Value array(rapidjson::kArrayType);
for (const auto& race : raceEntries_) {
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("type", race->type, allocator);
object.AddMember("1st", race->firstInstruction, allocator);
object.AddMember("2nd", race->secondInstruction, allocator);
object.AddMember("id", race->id, allocator);
array.PushBack(object, allocator);
}
doc.AddMember("races", array, allocator);
// 3. Stringify the DOM
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
// 4. Write to the file.
std::ofstream file;
file.open (fileName);
file << buffer.GetString();
file.close();
}
void LockSetChecker::call(const Event* e) { }
bool LockSetChecker::lsIsEmptySet(const LockSet_& lhs,
const LockSet_& rhs) const {
LockSet_ result;
std::set_intersection(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end(),
std::inserter(result, result.begin()));
return result.empty();
}
void LockSetChecker::lsIntersect(LockSet_& lhs, const LockSet_& rhs) const {
LockSet_ result;
std::set_intersection(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end(),
std::inserter(result, result.begin()));
lhs = result;
}