eed0e1711fbcadb5bc9a2858f4138f23b31c95e4
[c11tester.git] / datarace.h
1 /** @file datarace.h
2  *  @brief Data race detection code.
3  */
4
5 #ifndef __DATARACE_H__
6 #define __DATARACE_H__
7
8 #include "config.h"
9 #include <stdint.h>
10 #include "modeltypes.h"
11 #include "classlist.h"
12 #include "hashset.h"
13
14 struct ShadowTable {
15         void * array[65536];
16 };
17
18 struct ShadowBaseTable {
19         uint64_t array[65536];
20 };
21
22 struct DataRace {
23         /* Clock and thread associated with first action.  This won't change in
24                  response to synchronization. */
25
26         thread_id_t oldthread;
27         modelclock_t oldclock;
28         /* Record whether this is a write, so we can tell the user. */
29         bool isoldwrite;
30
31         /* Model action associated with second action.  This could change as
32                  a result of synchronization. */
33         ModelAction *newaction;
34         /* Record whether this is a write, so we can tell the user. */
35         bool isnewwrite;
36
37         /* Address of data race. */
38         const void *address;
39         void * backtrace[64];
40         int numframes;
41 };
42
43 #define MASK16BIT 0xffff
44
45 void initRaceDetector();
46 void raceCheckWrite(thread_id_t thread, void *location);
47 void atomraceCheckWrite(thread_id_t thread, void *location);
48 void raceCheckRead(thread_id_t thread, const void *location);
49
50 void atomraceCheckRead(thread_id_t thread, const void *location);
51 void recordWrite(thread_id_t thread, void *location);
52 void recordCalloc(void *location, size_t size);
53 void assert_race(struct DataRace *race);
54 bool hasNonAtomicStore(const void *location);
55 void setAtomicStoreFlag(const void *location);
56 void getStoreThreadAndClock(const void *address, thread_id_t * thread, modelclock_t * clock);
57
58 void raceCheckRead8(thread_id_t thread, const void *location);
59 void raceCheckRead16(thread_id_t thread, const void *location);
60 void raceCheckRead32(thread_id_t thread, const void *location);
61 void raceCheckRead64(thread_id_t thread, const void *location);
62
63 void raceCheckWrite8(thread_id_t thread, void *location);
64 void raceCheckWrite16(thread_id_t thread, void *location);
65 void raceCheckWrite32(thread_id_t thread, void *location);
66 void raceCheckWrite64(thread_id_t thread, void *location);
67
68 /**
69  * @brief A record of information for detecting data races
70  */
71 struct RaceRecord {
72         modelclock_t *readClock;
73         thread_id_t *thread;
74         int numReads : 31;
75         int isAtomic : 1;
76         thread_id_t writeThread;
77         modelclock_t writeClock;
78 };
79
80 unsigned int race_hash(struct DataRace *race);
81 bool race_equals(struct DataRace *r1, struct DataRace *r2);
82
83 #define INITCAPACITY 4
84
85 #define ISSHORTRECORD(x) ((x)&0x1)
86
87 #define THREADMASK 0x3f
88 #define RDTHREADID(x) (((x)>>1)&THREADMASK)
89 #define READMASK 0x1ffffff
90 #define READVECTOR(x) (((x)>>7)&READMASK)
91
92 #define WRTHREADID(x) (((x)>>32)&THREADMASK)
93
94 #define WRITEMASK READMASK
95 #define WRITEVECTOR(x) (((x)>>38)&WRITEMASK)
96
97 #define ATOMICMASK (0x1ULL << 63)
98 #define NONATOMICMASK ~(0x1ULL << 63)
99
100 /**
101  * The basic encoding idea is that (void *) either:
102  *  -# points to a full record (RaceRecord) or
103  *  -# encodes the information in a 64 bit word. Encoding is as
104  *     follows:
105  *     - lowest bit set to 1
106  *     - next 6 bits are read thread id
107  *     - next 25 bits are read clock vector
108  *     - next 6 bits are write thread id
109  *     - next 25 bits are write clock vector
110  *     - highest bit is 1 if the write is from an atomic
111  */
112 #define ENCODEOP(rdthread, rdtime, wrthread, wrtime) (0x1ULL | ((rdthread)<<1) | ((rdtime) << 7) | (((uint64_t)wrthread)<<32) | (((uint64_t)wrtime)<<38))
113
114 #define MAXTHREADID (THREADMASK-1)
115 #define MAXREADVECTOR (READMASK-1)
116 #define MAXWRITEVECTOR (WRITEMASK-1)
117
118 #define INVALIDSHADOWVAL 0x2ULL
119 #define CHECKBOUNDARY(location, bits) ( (((uintptr_t)location & MASK16BIT) + bits < MASK16BIT) ? true : false );
120
121 typedef HashSet<struct DataRace *, uintptr_t, 0, model_malloc, model_calloc, model_free, race_hash, race_equals> RaceSet;
122
123 #endif  /* __DATARACE_H__ */