/
FdTable.cc
286 lines (249 loc) · 7.38 KB
/
FdTable.cc
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* -*- Mode: C++; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
#include "FdTable.h"
#include <limits.h>
#include <unordered_set>
#include <utility>
#include "rr/rr.h"
#include "AddressSpace.h"
#include "RecordTask.h"
#include "ReplayTask.h"
#include "Session.h"
#include "core.h"
#include "log.h"
using namespace std;
namespace rr {
FdTable::shr_ptr FdTable::create(Task* t) {
shr_ptr fds(new FdTable(t->session().syscallbuf_fds_disabled_size()));
fds->insert_task(t);
return fds;
}
void FdTable::insert_task(Task* t) {
HasTaskSet::insert_task(t);
++vms[t->vm().get()];
}
void FdTable::erase_task(Task* t) {
if (task_set().find(t) == task_set().end()) {
return;
}
HasTaskSet::erase_task(t);
auto it = vms.find(t->vm().get());
if (it == vms.end()) {
FATAL() << "Lost track of VM already?";
}
--it->second;
if (!it->second) {
vms.erase(it);
}
}
void FdTable::add_monitor(Task* t, int fd, FileMonitor* monitor) {
// In the future we could support multiple monitors on an fd, but we don't
// need to yet.
FileMonitor* current = get_monitor(fd);
if (current) {
ASSERT(t, false) << "Task " << t->rec_tid << " already monitoring fd "
<< fd << " " << file_monitor_type_name(current->type());
}
if (fd >= syscallbuf_fds_disabled_size && fds.count(fd) == 0) {
fd_count_beyond_limit++;
}
fds[fd] = FileMonitor::shr_ptr(monitor);
update_syscallbuf_fds_disabled(fd);
}
void FdTable::replace_monitor(Task* t, int fd, FileMonitor* monitor) {
if (!is_monitoring(fd)) {
add_monitor(t, fd, monitor);
} else {
fds[fd] = FileMonitor::shr_ptr(monitor);
}
}
bool FdTable::is_rr_fd(int fd) {
auto it = fds.find(fd);
if (it == fds.end()) {
return false;
}
return it->second->is_rr_fd();
}
bool FdTable::emulate_ioctl(int fd, RecordTask* t, uint64_t* result) {
auto it = fds.find(fd);
if (it == fds.end()) {
return false;
}
return it->second->emulate_ioctl(t, result);
}
bool FdTable::emulate_fcntl(int fd, RecordTask* t, uint64_t* result) {
auto it = fds.find(fd);
if (it == fds.end()) {
return false;
}
return it->second->emulate_fcntl(t, result);
}
bool FdTable::emulate_read(int fd, RecordTask* t,
const std::vector<FileMonitor::Range>& ranges,
FileMonitor::LazyOffset& offset, uint64_t* result) {
auto it = fds.find(fd);
if (it == fds.end()) {
return false;
}
return it->second->emulate_read(t, ranges, offset, result);
}
void FdTable::filter_getdents(int fd, RecordTask* t) {
auto it = fds.find(fd);
if (it == fds.end()) {
return;
}
it->second->filter_getdents(t);
}
Switchable FdTable::will_write(Task* t, int fd) {
auto it = fds.find(fd);
if (it == fds.end()) {
return ALLOW_SWITCH;
}
return it->second->will_write(t);
}
void FdTable::did_write(Task* t, int fd,
const std::vector<FileMonitor::Range>& ranges,
FileMonitor::LazyOffset& offset) {
auto it = fds.find(fd);
if (it != fds.end()) {
it->second->did_write(t, ranges, offset);
}
}
void FdTable::did_dup(FdTable* table, int from, int to) {
if (table->fds.count(from)) {
if (to >= syscallbuf_fds_disabled_size && fds.count(to) == 0) {
fd_count_beyond_limit++;
}
fds[to] = table->fds[from];
} else {
if (to >= syscallbuf_fds_disabled_size && fds.count(to) > 0) {
fd_count_beyond_limit--;
}
fds.erase(to);
}
update_syscallbuf_fds_disabled(to);
}
void FdTable::did_close(int fd) {
LOG(debug) << "Close fd " << fd;
if (fd >= syscallbuf_fds_disabled_size && fds.count(fd) > 0) {
fd_count_beyond_limit--;
}
fds.erase(fd);
update_syscallbuf_fds_disabled(fd);
}
FileMonitor* FdTable::get_monitor(int fd) {
auto it = fds.find(fd);
if (it == fds.end()) {
return nullptr;
}
return it->second.get();
}
static syscallbuf_fd_classes join_fd_classes_over_tasks(AddressSpace* vm, int fd,
int syscallbuf_fds_disabled_size) {
syscallbuf_fd_classes cls = FD_CLASS_UNTRACED;
for (Task* t : vm->task_set()) {
auto table = t->fd_table();
if (table->is_monitoring(fd)) {
if (cls != FD_CLASS_UNTRACED) {
return FD_CLASS_TRACED;
}
cls = table->get_monitor(fd)->get_syscallbuf_class();
} else if (fd >= syscallbuf_fds_disabled_size - 1 &&
table->count_beyond_limit() > 0) {
return FD_CLASS_TRACED;
}
}
return cls;
}
void FdTable::update_syscallbuf_fds_disabled(int fd) {
DEBUG_ASSERT(fd >= 0);
DEBUG_ASSERT(task_set().size() > 0);
// It's possible for tasks with different VMs to share this fd table.
// But tasks with the same VM might have different fd tables...
for (auto address_space : vms) {
RecordTask* rt = nullptr;
if (address_space.first->task_set().empty()) {
FATAL() << "Address space must have at least one task";
}
for (Task* t : address_space.first->task_set()) {
if (!t->session().is_recording()) {
// We could return but we want to check that all our
// AddressSpaces have tasks (i.e. aren't dead/dangling)
break;
}
rt = static_cast<RecordTask*>(t);
if (!rt->already_exited()) {
break;
}
rt = nullptr;
}
if (rt && !rt->preload_globals.is_null()) {
if (fd >= syscallbuf_fds_disabled_size) {
fd = syscallbuf_fds_disabled_size - 1;
}
char disable = (char)join_fd_classes_over_tasks(address_space.first, fd,
syscallbuf_fds_disabled_size);
auto addr =
REMOTE_PTR_FIELD(rt->preload_globals, syscallbuf_fd_class[0]) + fd;
rt->write_mem(addr, disable);
rt->record_local(addr, &disable);
}
}
}
void FdTable::init_syscallbuf_fds_disabled(Task* t) {
if (!t->session().is_recording()) {
return;
}
RecordTask* rt = static_cast<RecordTask*>(t);
ASSERT(rt, has_task(rt));
if (rt->preload_globals.is_null()) {
return;
}
char disabled[syscallbuf_fds_disabled_size];
memset(disabled, 0, sizeof(disabled));
// It's possible that some tasks in this address space have a different
// FdTable. We need to disable syscallbuf for an fd if any tasks for this
// address space are monitoring the fd.
for (Task* vm_t : rt->vm()->task_set()) {
for (auto& it : vm_t->fd_table()->fds) {
int fd = it.first;
DEBUG_ASSERT(fd >= 0);
if (fd >= syscallbuf_fds_disabled_size) {
fd = syscallbuf_fds_disabled_size - 1;
}
if (disabled[fd] == FD_CLASS_UNTRACED) {
disabled[fd] = it.second->get_syscallbuf_class();
} else {
disabled[fd] = FD_CLASS_TRACED;
}
}
}
auto addr = REMOTE_PTR_FIELD(t->preload_globals, syscallbuf_fd_class[0]);
rt->write_mem(addr, disabled, syscallbuf_fds_disabled_size);
rt->record_local(addr, disabled, syscallbuf_fds_disabled_size);
}
void FdTable::close_after_exec(ReplayTask* t, const vector<int>& fds_to_close) {
ASSERT(t, has_task(t));
for (auto fd : fds_to_close) {
did_close(fd);
}
}
static bool is_fd_open(Task* t, int fd) {
char path[PATH_MAX];
sprintf(path, "/proc/%d/fd/%d", t->tid, fd);
struct stat st;
return 0 == lstat(path, &st);
}
vector<int> FdTable::fds_to_close_after_exec(RecordTask* t) {
ASSERT(t, has_task(t));
vector<int> fds_to_close;
for (auto& it : fds) {
if (!is_fd_open(t, it.first)) {
fds_to_close.push_back(it.first);
}
}
for (auto fd : fds_to_close) {
did_close(fd);
}
return fds_to_close;
}
} // namespace rr