Merge branch 'norris'
[model-checker.git] / datarace.h
1 /** @file datarace.h
2  *  @brief Data race detection code.
3  */
4
5 #ifndef DATARACE_H
6 #include "config.h"
7 #include <stdint.h>
8 #include "clockvector.h"
9 #include <vector>
10
11 struct ShadowTable {
12         void * array[65536];
13 };
14
15 struct ShadowBaseTable {
16         uint64_t array[65536];
17 };
18
19 struct DataRace {
20         /* Clock and thread associated with first action.  This won't change in
21                  response to synchronization. */
22
23         thread_id_t oldthread;
24         modelclock_t oldclock;
25         /* Record whether this is a write, so we can tell the user. */
26         bool isoldwrite;
27
28         /* Model action associated with second action.  This could change as
29                  a result of synchronization. */
30         ModelAction *newaction;
31         /* Record whether this is a write, so we can tell the user. */
32         bool isnewwrite;
33
34         /* Address of data race. */
35         void *address;
36 };
37
38 #define MASK16BIT 0xffff
39
40 void initRaceDetector();
41 void raceCheckWrite(thread_id_t thread, void *location, ClockVector *currClock);
42 void raceCheckRead(thread_id_t thread, void *location, ClockVector *currClock);
43 void checkDataRaces();
44 void printRace(struct DataRace *race);
45
46 extern std::vector<struct DataRace *> unrealizedraces;
47
48 /** Basic encoding idea:
49  *       (void *) Either:
50  *       (1) points to a full record or
51  *
52  * (2) encodes the information in a 64 bit word.  Encoding is as
53  * follows: lowest bit set to 1, next 8 bits are read thread id, next
54  * 23 bits are read clock vector, next 8 bites are write thread id,
55  * next 23 bits are write clock vector.  */
56
57 struct RaceRecord {
58         modelclock_t *readClock;
59         thread_id_t *thread;
60         int capacity;
61         int numReads;
62         thread_id_t writeThread;
63         modelclock_t writeClock;
64 };
65
66 #define INITCAPACITY 4
67
68 #define ISSHORTRECORD(x) ((x)&0x1)
69
70 #define THREADMASK 0xff
71 #define RDTHREADID(x) (((x)>>1)&THREADMASK)
72 #define READMASK 0x07fffff
73 #define READVECTOR(x) (((x)>>9)&READMASK)
74
75 #define WRTHREADID(x) (((x)>>32)&THREADMASK)
76
77 #define WRITEMASK READMASK
78 #define WRITEVECTOR(x) (((x)>>40)&WRITEMASK)
79
80 #define ENCODEOP(rdthread, rdtime, wrthread, wrtime) (0x1ULL | ((rdthread)<<1) | ((rdtime) << 9) | (((uint64_t)wrthread)<<32) | (((uint64_t)wrtime)<<40))
81
82 #define MAXTHREADID (THREADMASK-1)
83 #define MAXREADVECTOR (READMASK-1)
84 #define MAXWRITEVECTOR (WRITEMASK-1)
85 #endif