This repository has been archived by the owner on Feb 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pattern.cc
258 lines (206 loc) · 6.81 KB
/
pattern.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
#include "pattern.h"
#include <sys/mman.h>
#include <iostream>
void Next::print() {
std::cout << "op: " << operation << ", addr: 0x" << std::hex <<
address << std::dec << ", size: " << size << ", permissions: " <<
permissions << std::endl;
}
PatternGenerator::PatternGenerator (std::vector<PatternPart> pattern, void *start) :
pattern(pattern), index(0) {
previous.address = start;
}
Next PatternGenerator::next() {
Next next;
next.size = pattern[index].size * PAGE_SIZE;
next.operation = pattern[index].operation;
next.address = get_address();
next.permissions = get_prot();
previous = next;
index = (index + 1) % pattern.size();
return next;
}
void *PatternGenerator::get_address() {
unsigned long prev = (unsigned long)previous.address;
unsigned long next = prev + pattern[index].location * PAGE_SIZE;
return (void*)next;
}
int PatternGenerator::get_prot() {
char prot = pattern[index].permissions;
switch(pattern[index].operation) {
case OP_MMAP:
case OP_MPROT:
return prot ?
(prot & 1 ? PROT_EXEC : 0) |
(prot & 2 ? PROT_WRITE : 0) |
(prot & 4 ? PROT_READ : 0) :
PROT_NONE;
default:
return 0;
}
}
// mmap a bunch of pages that are contiguous
PatternGenerator get_mmap_cont(int size, char permissions) {
PatternPart only;
only.size = size; // size * 4KB
only.location = size;
only.permissions = permissions;
only.operation = OP_MMAP;
std::vector<PatternPart> pattern;
pattern.push_back(only);
return PatternGenerator(pattern);
}
// mmap a bunch of pages sequentially with a given stride
PatternGenerator get_mmap_stride(int size, char permissions, int stride) {
PatternPart only;
only.size = size; // size * 4KB
only.location = stride;
only.permissions = permissions;
only.operation = OP_MMAP;
std::vector<PatternPart> pattern;
pattern.push_back(only);
return PatternGenerator(pattern);
}
// mmap a bunch of pages contiguously and change their permissions
PatternGenerator get_mmap_cont_change_prot(int size, char permissions, char new_permissions) {
PatternPart first;
first.size = size; // size * 4KB
first.location = size;
first.permissions = permissions;
first.operation = OP_MMAP;
PatternPart second;
second.size = size; // size * 4KB
second.location = 0;
second.permissions = new_permissions;
second.operation = OP_MPROT;
std::vector<PatternPart> pattern;
pattern.push_back(first);
pattern.push_back(second);
return PatternGenerator(pattern);
}
// mmap a bunch of pages contiguosly and change permissions of subset of these pages
PatternGenerator get_mmap_cont_change_prot_partial(int size, char permissions,\
int sub_size, char new_permissions) {
PatternPart first;
first.size = size; // size * 4KB
first.location = size;
first.permissions = permissions;
first.operation = OP_MMAP;
PatternPart second;
second.size = sub_size; // sub_size * 4KB
second.location = 0;
second.permissions = new_permissions;
second.operation = OP_MPROT;
std::vector<PatternPart> pattern;
pattern.push_back(first);
pattern.push_back(second);
return PatternGenerator(pattern);
}
// mmap a bunch of pages
// Resize them to smaller pages
PatternGenerator get_mmap_large_remap_small(int large_size, char permissions, int small_size) {
PatternPart first;
first.size = large_size; // large_size * 4KB
first.location = small_size;
first.permissions = permissions;
first.operation = OP_MMAP;
PatternPart second;
second.size = small_size; // small_size * 4KB
second.location = 0;
second.permissions = permissions;
second.operation = OP_MREMAP;
std::vector<PatternPart> pattern;
pattern.push_back(first);
pattern.push_back(second);
return PatternGenerator(pattern);
}
// mmap a bunch of small pages
// Resize them to large pages
PatternGenerator get_mmap_small_remap_large(int small_size, char permissions, int large_size) {
PatternPart first;
first.size = small_size; // small_size * 4KB
first.location = large_size;
first.permissions = permissions;
first.operation = OP_MMAP;
PatternPart second;
second.size = large_size; // large_size * 4KB
second.location = 0;
second.permissions = permissions;
second.operation = OP_MREMAP;
std::vector<PatternPart> pattern;
pattern.push_back(first);
pattern.push_back(second);
return PatternGenerator(pattern);
}
// Resize pages to larger size
PatternGenerator get_mremap_frag(unsigned long offset, int size, char permissions) {
PatternPart first;
first.size = size; // 4KB * size
first.location = offset;
first.permissions = permissions;
first.operation = OP_MREMAP;
std::vector<PatternPart> pattern;
pattern.push_back(first);
return PatternGenerator(pattern);
}
// mmap a bunch of pages with a certain stride
// Remap the pages to the unmapped spaces
PatternGenerator get_mmap_stride_reloc(int size, char permissions) {
PatternPart first;
first.size = size; // size * 4KB
first.location = size;
first.permissions = permissions;
first.operation = OP_MMAP;
PatternPart second;
second.size = size; // size * 4KB
second.location = size;
second.permissions = permissions;
second.operation = OP_MREMAP;
std::vector<PatternPart> pattern;
pattern.push_back(first);
pattern.push_back(second);
return PatternGenerator(pattern);
}
// mmap a bunch of 16KB pages contiguosly
// Unmap 8KB pages in the middle of these 16KB pages
PatternGenerator get_mmap_16k_cont_munmap_8k() {
PatternPart first;
first.size = 4; // 16KB
first.location = 3;
first.permissions = 7;
first.operation = OP_MMAP;
PatternPart second;
second.size = 2; // 4KB
second.location = 1;
second.permissions = 7;
second.operation = OP_MUNMAP;
std::vector<PatternPart> pattern;
pattern.push_back(first);
pattern.push_back(second);
return PatternGenerator(pattern);
}
// mmap a bunch of 4KB pages contiguously
// Resize them to 8KB pages
// Unmap the initial 4KB
PatternGenerator get_mmap_4k_remap_8k_unmap_4k() {
PatternPart first;
first.size = 1; // 4KB
first.location = 2;
first.permissions = 7;
first.operation = OP_MMAP;
PatternPart second;
second.size = 2; // 8KB
second.location = 0;
second.permissions = 7;
second.operation = OP_MREMAP;
PatternPart third;
third.size = 1; // 4KB
third.location = 0;
third.permissions = 7;
third.operation = OP_MUNMAP;
std::vector<PatternPart> pattern;
pattern.push_back(first);
pattern.push_back(second);
pattern.push_back(third);
return PatternGenerator(pattern);
}