-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMRobustness.h
More file actions
760 lines (624 loc) · 18.2 KB
/
PMRobustness.h
File metadata and controls
760 lines (624 loc) · 18.2 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#include "llvm/IR/IRBuilder.h"
#include "llvm/ADT/BitVector.h"
#include "FunctionSummary.h"
#include <cassert>
using namespace llvm;
#define UNKNOWNOFFSET 0xffffffff
#define VARIABLEOFFSET 0xfffffffe
//#define FUNC_PARAM_USE 100
struct ob_state_t;
struct ArrayInfo;
typedef DenseMap<const Value *, ob_state_t *> state_t;
typedef DenseMap<const Instruction *, state_t> state_map_t;
typedef DenseMap<const BasicBlock *, state_t> b_state_map_t;
typedef DenseMap<const Value *, ArrayInfo> addr_set_t;
typedef DenseSet<Value *> value_set_t;
typedef DenseMap<const Value *, value_set_t *> alias_set_t;
enum NVMOP {
NVM_CLWB,
NVM_CLFLUSH,
NVM_FENCE,
NVM_UNKNOWN
};
struct VariableGEPIndex {
// An opaque Value - we can't decompose this further.
const Value *V;
// We need to track what extensions we've done as we consider the same Value
// with different extensions as different variables in a GEP's linear
// expression;
// e.g.: if V == -1, then sext(x) != zext(x).
unsigned ZExtBits;
unsigned SExtBits;
APInt Scale;
bool operator==(const VariableGEPIndex &Other) const {
return V == Other.V && ZExtBits == Other.ZExtBits &&
SExtBits == Other.SExtBits && Scale == Other.Scale;
}
bool operator!=(const VariableGEPIndex &Other) const {
return !operator==(Other);
}
};
struct DecomposedGEP {
// Base pointer of the GEP
const Value *Base;
// Total constant offset w.r.t the base from indexing into structs
APInt StructOffset;
// Total constant offset w.r.t the base from indexing through
// pointers/arrays/vectors
APInt OtherOffset;
// Scaled variable (non-constant) indices.
SmallVector<VariableGEPIndex, 4> VarIndices;
bool isArray;
uint64_t getOffsets() {
if (VarIndices.size() == 0) {
assert(StructOffset.getSExtValue() >= 0);
//assert(OtherOffset.getSExtValue() >= 0);
if (OtherOffset.getSExtValue() < 0) {
#ifdef PMROBUST_DEBUG
errs() << "strange offset: " << StructOffset.getSExtValue() << ", " << OtherOffset.getSExtValue() << "\n";
#endif
return UNKNOWNOFFSET;
}
uint64_t offset = StructOffset.getZExtValue() + OtherOffset.getZExtValue();
return offset;
} else
return VARIABLEOFFSET;
}
uint64_t getStructOffset() {
assert(StructOffset.getSExtValue() >= 0);
return StructOffset.getZExtValue();
}
};
struct ArrayInfo {
// `state` should only record dirty/clwb states, with possible values be
// DIRTY_ESCAPED or CLWB_ESCAPED
// Escapedness is recored in other places
ParamStateType state;
const Value *Base;
ArrayInfo() {
Base = nullptr;
}
ParamStateType getState() {
return state;
}
void set(ParamStateType s, const Value *B) {
if (Base != nullptr)
assert(Base == B);
state = s;
Base = B;
}
void setState(ParamStateType s) {
state = s;
}
void copyFrom(ArrayInfo &other) {
Base = other.Base;
state = other.state;
}
void mergeFrom(ArrayInfo &other) {
// TODO
assert(Base == other.Base);
if (state == ParamStateType::DIRTY_ESCAPED || other.state == ParamStateType::DIRTY_ESCAPED)
state = ParamStateType::DIRTY_ESCAPED;
else if(state == ParamStateType::CLWB_ESCAPED || other.state == ParamStateType::CLWB_ESCAPED)
state = ParamStateType::CLWB_ESCAPED;
}
};
static inline std::string getPosition(const Instruction * I, bool print = false)
{
const DebugLoc & debug_location = I->getDebugLoc ();
std::string position_string;
{
llvm::raw_string_ostream position_stream (position_string);
debug_location.print (position_stream);
}
// Phi instructions do not have positions
// TODO: some instructions have position:0
if (print) {
errs() << position_string << "\n";
}
return position_string;
}
bool checkPosition(Instruction * I, IRBuilder <> IRB, std::string sub)
{
const DebugLoc & debug_location = I->getDebugLoc ();
std::string position_string;
{
llvm::raw_string_ostream position_stream (position_string);
debug_location . print (position_stream);
}
std::size_t found = position_string.find(sub);
if (found!=std::string::npos)
return true;
return false;
}
static void printBitVectorAsIntervals(BitVector BV) {
int IntervalStart = -1; //-1 if not in a strip of ones
for (unsigned i = 0; i < BV.size(); i++) {
if (BV[i] && IntervalStart == -1)
IntervalStart = i;
if ((!BV[i] || i+1 == BV.size()) && IntervalStart != -1) {
errs() << IntervalStart << " - " << i << ", ";
IntervalStart = -1;
}
}
}
static const unsigned max_size = 8192;
/**
* TODO: may need to track the size of fields
**/
// <dirty_byte, clwb_byte> is either <0, 0>, <1, 0>, or <1, 1>
class ob_state_t {
private:
unsigned size;
BitVector dirty_bytes;
BitVector clwb_bytes;
bool escaped;
bool nonpmem;
bool is_array;
// the position where the state most recently
// changes to dirty/escaped
// empty from not dirty/escaped
Instruction* dirty_pos = nullptr;
Instruction* escaped_pos = nullptr;
bool mark_delete;
void resize(unsigned s) {
if (size >= s)
return;
assert(s <= max_size && "oversize s");
size = s;
dirty_bytes.resize(s);
clwb_bytes.resize(s);
}
public:
ob_state_t() :
size(0),
dirty_bytes(),
clwb_bytes(),
escaped(false),
nonpmem(false),
is_array(false),
mark_delete(false)
{}
ob_state_t(unsigned s) :
escaped(false),
nonpmem(false),
is_array(false),
mark_delete(false)
{
s = std::min(s, max_size);
size = s;
dirty_bytes.resize(s);
clwb_bytes.resize(s);
}
ob_state_t(ob_state_t * other) :
size(other->size),
dirty_bytes(other->dirty_bytes),
clwb_bytes(other->clwb_bytes),
escaped(other->escaped),
nonpmem(other->nonpmem),
is_array(other->is_array),
dirty_pos(other->dirty_pos),
escaped_pos(other->escaped_pos),
mark_delete(false)
{}
void mergeFrom(ob_state_t * other) {
//assert(size == other->size);
// <dirty_byte, clwb_byte> is either <0, 0>, <1, 0>, or <1, 1>
// clwb_bytes = (clwb_bytes | other->clwb_bytes) &
// ~((dirty_bytes ^ clwb_bytes) | (other->dirty_bytes ^ other->clwb_bytes));
// dirty_bytes = dirty_bytes | other->dirty_bytes
if (other->size > size)
size = other->size;
BitVector tmp1(dirty_bytes);
BitVector tmp2(other->dirty_bytes);
tmp1 ^= clwb_bytes;
tmp2 ^= other->clwb_bytes;
tmp1 |= tmp2;
tmp1.flip();
clwb_bytes |= other->clwb_bytes;
clwb_bytes &= tmp1;
dirty_bytes |= other->dirty_bytes;
escaped |= other->escaped;
nonpmem &= other->nonpmem;
is_array |= other->is_array;
}
void copyFrom(ob_state_t * src) {
//assert(size == src->size);
size = src->size;
dirty_bytes = src->dirty_bytes;
clwb_bytes = src->clwb_bytes;
escaped = src->escaped;
// if (nonpmem != src->nonpmem) {
// assert(false);
// }
// nonpmem comes from Calling Contexts, which can be different
// Since only the initial state is reset each time analyzing functions,
// we propagate nonpmem by copying
nonpmem = src->nonpmem;
is_array = src->is_array;
escaped_pos = src->escaped_pos;
dirty_pos = src->dirty_pos;
}
void resetDirtyEscapedPos(Instruction *pos) {
escaped_pos = pos;
dirty_pos = pos;
}
bool copyFromCheckDiff(ob_state_t * src, bool update_pos = true) {
bool updated = false;
updated |= (size != src->size);
updated |= (dirty_bytes != src->dirty_bytes);
updated |= (clwb_bytes != src->clwb_bytes);
updated |= (escaped != src->escaped);
updated |- (is_array != src->is_array);
size = src->size;
dirty_bytes = src->dirty_bytes;
clwb_bytes = src->clwb_bytes;
escaped = src->escaped;
// nonpmem comes from Calling Contexts, and is not considered as a change in states
nonpmem = src->nonpmem;
is_array = src->is_array;
if(update_pos) {
// should change in pos be counted in update?
escaped_pos = src->escaped_pos;
dirty_pos = src->dirty_pos;
}
return updated;
}
void setToArray() {
is_array = true;
bool anyDirty = dirty_bytes.any();
bool anyClwb = clwb_bytes.any();
dirty_bytes.resize(1);
clwb_bytes.resize(1);
if (anyDirty)
dirty_bytes.set();
if (anyClwb)
clwb_bytes.set();
}
// return true: modified; return else: un)changed
bool setDirty(unsigned start, unsigned len, Instruction *new_dirty_pos) {
if (nonpmem)
return false;
if (is_array) {
bool ret = dirty_bytes.none() || clwb_bytes.any();
dirty_bytes.set();
clwb_bytes.reset();
return ret;
}
unsigned end = start + len;
start = std::min(start, max_size);
end = std::min(end, max_size);
if (end > size) {
resize(end);
}
//errs() << "start: " << start << "; len: " << len << "; end:" << end << "\n";
//errs() << "actual size: " << size << "\n";
int index1 = dirty_bytes.find_first_unset_in(start, end);
int index2 = clwb_bytes.find_first_in(start, end);
// dirty_byte are all 1 and clwb_bytes are all 0, then no change
if (index1 == -1 && index2 == -1)
return false;
dirty_pos = new_dirty_pos;
dirty_bytes.set(start, end);
clwb_bytes.reset(start, end);
return true;
}
// Check if there are other nonclean (dirty or clwb) bytes other than the ones in [start, end)
bool hasNoncleanBytesNotIn(unsigned start, unsigned len) {
if (nonpmem)
return false;
//conservatively true for arrays
if (is_array)
return true;
unsigned end = start + len;
start = std::min(start, max_size);
end = std::min(end, max_size);
if (end > size) {
resize(end);
}
int setBitBeforeStart = dirty_bytes.find_first_in(0, start);
int setBitAfterEnd = dirty_bytes.find_first_in(end, dirty_bytes.size());
if (setBitBeforeStart == -1 && setBitAfterEnd == -1)
return false;
return true;
}
bool MultipleNoncleanFieldsBadApproximation() {
if (nonpmem)
return false;
unsigned j = dirty_bytes.size() / 8;
if (dirty_bytes.size() % 8 != 0)
j++;
int dirty_count = 0;
for (unsigned i = 0; i < j; i++) {
unsigned start = i * 8;
unsigned end = (i + 1) * 8;
if (end >= dirty_bytes.size())
end = dirty_bytes.size();
/*
if (dirty_bytes.find_first_in(start, end) != -1) {
dirty_count++;
if (dirty_count >= 2) {
return true;
}
}
*/
}
return false;
}
// TODO: start + len and size?
// Flush wrapper function may flush cache lines exceeding the size of this object
bool setFlush(unsigned start, unsigned len, bool onlyFlushWrittenBytes = false) {
if (nonpmem)
return false;
if (is_array) {
bool ret = dirty_bytes.any() || clwb_bytes.any();
dirty_bytes.reset();
clwb_bytes.reset();
return ret;
}
if (start > size && onlyFlushWrittenBytes) {
errs() << "FIXME: Flush unknown bytes\n";
return false;
//assert(false && "Flush unknown bytes");
}
unsigned end = start + len;
start = std::min(start, max_size);
end = std::min(end, max_size);
if (len == (unsigned)-1 && onlyFlushWrittenBytes) {
// start + len may overflow
end = size;
} else if (end > size && onlyFlushWrittenBytes) {
end = size;
} else if (end > size)
resize(end);
int index1 = dirty_bytes.find_first_in(start, end);
int index2 = clwb_bytes.find_first_in(start, end);
// dirty_byte and clwb_bytes are all 0, then no change
if (index1 == -1 && index2 == -1)
return false;
dirty_bytes.reset(start, end);
clwb_bytes.reset(start, end);
return true;
}
// TODO: start + len and size?
// Flush wrapper function may flush cache lines exceeding the size of this object
bool setClwb(unsigned start, unsigned len, bool onlyFlushWrittenBytes = false) {
if (nonpmem)
return false;
if (is_array) {
bool old = clwb_bytes.any();
if (dirty_bytes.any())
clwb_bytes.set();
return !old && clwb_bytes.any();
}
if (start > size && onlyFlushWrittenBytes) {
errs() << "FIXME: Clwb unknown bytes\n";
return false;
//assert(false && "Clwb unknown bytes");
}
unsigned end = start + len;
start = std::min(start, max_size);
end = std::min(end, max_size);
if (end > size && onlyFlushWrittenBytes) {
end = size;
} else if (end > size)
resize(end);
// set clwb_bytes for bytes in dirty_bytes
BitVector tmp(dirty_bytes);
tmp.reset(0, start);
tmp.reset(end, tmp.size());
BitVector old_clwb_bytes(clwb_bytes);
clwb_bytes |= tmp;
if (old_clwb_bytes == clwb_bytes)
return false; // No change
return true;
}
// apply Fence on clwb bytes
bool applyFence() {
if (clwb_bytes.any()) {
dirty_bytes ^= clwb_bytes;
clwb_bytes.reset();
return true;
}
return false;
}
// return true: modified; return else: unchanged
bool setEscape(Instruction *new_escaped_pos) {
escaped_pos = new_escaped_pos;
if (escaped == false) {
escaped = true;
return true;
}
return false;
}
bool setCaptured() {
if (escaped == true) {
escaped = false;
return true;
}
return false;
}
bool isEscaped() {
return escaped;
}
void setNonPmem() {
nonpmem = true;
}
bool isNonPmem() {
return nonpmem;
}
void markDelete() {
mark_delete = true;
}
void unmarkDelete() {
mark_delete = false;
}
bool shouldDelete() {
return mark_delete;
}
unsigned getSize() {
return size;
}
void setSize(unsigned s) {
size = s;
}
ParamStateType checkState() {
return checkState(0, size);
}
ParamStateType checkState(unsigned startByte, unsigned len) {
if (is_array) {
startByte = 0;
len = 1;
}
unsigned endByte = startByte + len;
//errs() << "range: " << startByte << " - " << startByte + len << "; size: " << size << "\n";
if (size == 0) {
if (escaped)
return ParamStateType::CLEAN_ESCAPED;
else
return ParamStateType::TOP;
}
startByte = std::min(startByte, max_size);
endByte = std::min(endByte, max_size);
if (startByte >= size) {
errs() << "Checking state out of range\n";
errs() << "range: " << startByte << " - " << startByte + len << "; size: " << size << "\n";
return ParamStateType::TOP;
//assert(false);
}
if (endByte > size)
endByte = size;
BitVector tmp(dirty_bytes);
tmp &= clwb_bytes;
if (escaped) {
if (dirty_bytes.find_first_in(startByte, endByte) == -1) {
// dirty_bytes are all 0
return ParamStateType::CLEAN_ESCAPED;
} else if (dirty_bytes == tmp) {
// all set dirty_bytes are clwbed;
return ParamStateType::CLWB_ESCAPED;
} else {
// Some set dirty_bytes are not clwbed
return ParamStateType::DIRTY_ESCAPED;
}
} else {
if (dirty_bytes.find_first_in(startByte, endByte) == -1) {
// dirty_bytes are all 0
return ParamStateType::CLEAN_CAPTURED;
} else if (dirty_bytes == tmp) {
// all set dirty_bytes are clwbed;
return ParamStateType::CLWB_CAPTURED;
} else {
// Some set dirty_bytes are not clwbed
return ParamStateType::DIRTY_CAPTURED;
}
}
}
bool isDirty() {
for (auto Itr = dirty_bytes.set_bits_begin(); Itr != dirty_bytes.set_bits_end(); Itr++)
if(!clwb_bytes.test(*Itr))
return true;
return false;
}
bool isClwb() {
return clwb_bytes.any();
}
void computeDirtyBytes(DirtyBytesInfo &info) {
//dump();
BitVector only_dirty_bytes(dirty_bytes);
only_dirty_bytes ^= clwb_bytes;
int i = only_dirty_bytes.find_first();
while (i != -1) {
// Store [i, j)
int j = only_dirty_bytes.find_next_unset(i);
if (j == -1) {
j = only_dirty_bytes.size();
info.push(i, j);
break;
}
info.push(i, j);
assert(j >= 1);
i = only_dirty_bytes.find_next(j - 1);
}
}
Instruction * getDirtyPos() {
return dirty_pos;
}
Instruction * getEscapedPos() {
return escaped_pos;
}
inline std::string getDirtyPosStr() {
if (!dirty_pos)
return "unknown position";
auto ret = getPosition(dirty_pos);
if (ret.empty()) {
llvm::raw_string_ostream position_stream (ret);
position_stream << *dirty_pos;
}
return ret;
}
inline std::string getEscapedPosStr() {
if (!escaped_pos)
return "unknown position";
auto ret = getPosition(escaped_pos);
if (ret.empty()) {
llvm::raw_string_ostream position_stream (ret);
position_stream << *escaped_pos;
}
return ret;
}
std::string getDirtyEscapedPosStr() {
return "dirty at " + getDirtyPosStr() + ", escaped at " + getEscapedPosStr();
}
void dump() {
errs() << "bit vector size: " << size << "\n";
if (size != 0) {
if (dirty_bytes.any()) {
errs() << "dirty bytes: ";
printBitVectorAsIntervals(dirty_bytes);
errs() << "\nfirst dirty at " << getDirtyPosStr() << "\n";
errs() << "clwb bytes: ";
printBitVectorAsIntervals(clwb_bytes);
errs() << "\n";
}
}
if (escaped)
errs() << "escaped at " << getEscapedPosStr();
else
errs() << "captured";
if (nonpmem)
errs() << "; nonpmem";
else
errs() << "; pmem";
errs() << "\n";
}
};
void printDecomposedGEP(DecomposedGEP &Decom) {
errs() << "Store Base: " << *Decom.Base << "\t";
errs() << "Struct Offset: " << Decom.StructOffset << "\t";
errs() << "Other Offset: " << Decom.OtherOffset << "\t";
errs() << "Has VarIndices: " << Decom.VarIndices.size() << "\n";
/*
for (unsigned i = 0 ; i < Decom.VarIndices.size(); i++) {
VariableGEPIndex &VI = Decom.VarIndices[i];
errs() << *VI.V << "\n";
errs() << "(" << VI.ZExtBits << ", " << VI.SExtBits << ")\t";
errs() << "Scale: " << VI.Scale << "\n";
}*/
}
/// To ensure a pointer offset fits in an integer of size PointerSize
/// (in bits) when that size is smaller than the maximum pointer size. This is
/// an issue, for example, in particular for 32b pointers with negative indices
/// that rely on two's complement wrap-arounds for precise alias information
/// where the maximum pointer size is 64b.
static APInt adjustToPointerSize(APInt Offset, unsigned PointerSize) {
assert(PointerSize <= Offset.getBitWidth() && "Invalid PointerSize!");
unsigned ShiftBits = Offset.getBitWidth() - PointerSize;
return (Offset << ShiftBits).ashr(ShiftBits);
}
static unsigned getMaxPointerSize(const DataLayout &DL) {
unsigned MaxPointerSize = DL.getMaxPointerSizeInBits();
//if (MaxPointerSize < 64 && ForceAtLeast64Bits) MaxPointerSize = 64;
//if (DoubleCalcBits) MaxPointerSize *= 2;
return MaxPointerSize;
}