-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandstruct.cpp
201 lines (165 loc) · 6.02 KB
/
randstruct.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
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
#include "clang/AST/AST.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <memory>
#include <random>
#include <stack>
#include <vector>
#include "bucket.h"
#include "randstruct.h"
std::vector<FieldDecl *>
Randstruct::randomize(std::vector<FieldDecl *> fields) {
std::shuffle(std::begin(fields), std::end(fields), rng);
return fields;
}
std::vector<FieldDecl *>
Randstruct::perfrandomize(std::vector<FieldDecl *> fields) {
// Required for finding out size of field.
auto &ctx = Instance.getASTContext();
// All of the buckets produced by best-effort cache-line algorithm.
std::vector<std::unique_ptr<Bucket>> buckets;
// The current bucket of fields that we are trying to fill to a cache-line.
std::unique_ptr<Bucket> currentBucket = nullptr;
// The current bucket containing the run of adjacent bitfields to ensure
// they remain adjacent.
std::unique_ptr<Bucket> currentBitfieldRun = nullptr;
// Tracks the number of fields that we failed to fit to the current bucket,
// and thus still need to be added later.
auto skipped = 0;
while (!fields.empty()) {
// If we've skipped more fields than we have remaining to place,
// that means that they can't fit in our current bucket, and we
// need to start a new one.
if (skipped >= fields.size()) {
skipped = 0;
buckets.push_back(std::move(currentBucket));
}
// Take the first field that needs to be put in a bucket.
auto field = fields.begin();
if ((*field)->isBitField()) {
// Start a bitfield run if this is the first bitfield
// we have found.
if (!currentBitfieldRun) {
currentBitfieldRun = std::make_unique<BitfieldRun>();
}
// We've placed the field, and can remove it from the
// "awaiting buckets" vector called "fields"
currentBitfieldRun->add(*field, 1);
fields.erase(field);
} else {
// Else, current field is not a bitfield
// If we were previously in a bitfield run, end it.
if (currentBitfieldRun) {
buckets.push_back(std::move(currentBitfieldRun));
}
// If we don't have a bucket, make one.
if (!currentBucket) {
currentBucket = std::make_unique<Bucket>();
}
auto width = ctx.getTypeInfo((*field)->getType()).Width;
// If we can fit, add it.
if (currentBucket->canFit(width)) {
currentBucket->add(*field, width);
fields.erase(field);
// If it's now full, tie off the bucket.
if (currentBucket->full()) {
skipped = 0;
buckets.push_back(std::move(currentBucket));
}
} else {
// We can't fit it in our current bucket.
// Move to the end for processing later.
++skipped; // Mark it skipped.
fields.push_back(*field);
fields.erase(field);
}
}
}
// Done processing the fields awaiting a bucket.
// If we were filling a bucket, tie it off.
if (currentBucket) {
buckets.push_back(std::move(currentBucket));
}
// If we were processing a bitfield run bucket, tie it off.
if (currentBitfieldRun) {
buckets.push_back(std::move(currentBitfieldRun));
}
std::shuffle(std::begin(buckets), std::end(buckets), rng);
// Produce the new ordering of the elements from our buckets.
std::vector<FieldDecl *> finalOrder;
for (auto &bucket : buckets) {
auto randomized = bucket->randomize();
finalOrder.insert(finalOrder.end(), randomized.begin(), randomized.end());
}
return finalOrder;
}
std::vector<FieldDecl *>
Randstruct::rearrange(std::vector<FieldDecl *> fields) {
return perfrandomize(fields);
}
bool Randstruct::layout(
const RecordDecl *Record, std::vector<FieldDecl *> &fields, uint64_t &Size,
uint64_t &Alignment,
llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
ASTContext &ctx) {
Alignment = 0;
Size = 0;
#ifndef NDEBUG
llvm::errs() << "Name\tType\tSize\tAlign\tOffset\tAligned?\n"
<< "----\t----\t-----\t------\t--------\n";
#endif
for (auto f : fields) {
auto width = ctx.getTypeInfo(f->getType()).Width;
auto align = ctx.getTypeInfo(f->getType()).Align;
Alignment = Alignment > align ? Alignment : align;
// TODO: pad bitfields?
// https://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding
auto padding = (-Size & (align - 1));
FieldOffsets[f] = Size + padding;
#ifndef NDEBUG
llvm::errs() << f->getNameAsString() << "\t" << f->getType().getAsString()
<< "\t" << width << "\t" << align << "\t" << Size + padding
<< "\t"
<< ((Size + width + padding) % align == 0 ? "Yes" : "No")
<< "\n";
#endif
Size += width + padding;
}
// https://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding
auto tailpadded = (Size + (Alignment - 1)) & -Alignment;
Size = tailpadded;
// Respect the programmer's requested alignment from the structure
// by overriding what we've calculated so far.
if (auto alignAttr = Record->getAttr<AlignedAttr>()) {
Alignment = alignAttr->getAlignment(ctx);
}
#ifndef NDEBUG
llvm::errs() << "\n"
<< "Structure Size: " << Size << " bits ("
<< "Should be "
<< Size + (Alignment - (Size % Alignment)) % Alignment
<< " bits)\n"
<< "Alignment: " << Alignment << " bits\n"
<< "\n";
#endif
return true;
}
bool Randstruct::layoutRecordType(
const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets) {
auto &ctx = Instance.getASTContext();
Alignment = 0;
Size = 0;
if (Record->isUnion()) {
return false;
}
std::vector<FieldDecl *> fields;
for (auto field : Record->fields()) {
fields.push_back(field);
}
fields = rearrange(fields);
return layout(Record, fields, Size, Alignment, FieldOffsets,
Instance.getASTContext());
}