--- /dev/null
+#include "datarace.h"
+#include "threads.h"
+#include <stdio.h>
+#include <cstring>
+
+struct ShadowTable *root;
+
+void initRaceDetector() {
+ root=(struct ShadowTable *) calloc(sizeof(struct ShadowTable),1);
+}
+
+static uint64_t * lookupAddressEntry(void * address) {
+ struct ShadowTable *currtable=root;
+#ifdef BIT48
+ currtable=(struct ShadowTable *) currtable->array[(((uintptr_t)address)>>32)&0xffff];
+ if (currtable==NULL) {
+ currtable=(struct ShadowTable *) (root->array[(((uintptr_t)address)>>32)&MASK16BIT]=calloc(sizeof(struct ShadowTable),1));
+ }
+#endif
+
+ struct ShadowBaseTable * basetable=(struct ShadowBaseTable *) currtable->array[(((uintptr_t)address)>>16)&MASK16BIT];
+ if (basetable==NULL) {
+ basetable=(struct ShadowBaseTable *) (currtable->array[(((uintptr_t)address)>>16)&MASK16BIT]=calloc(sizeof(struct ShadowBaseTable),1));
+ }
+ return &basetable->array[((uintptr_t)address)&MASK16BIT];
+}
+
+static void expandRecord(uint64_t * shadow) {
+ uint64_t shadowval=*shadow;
+
+ int readClock = READVECTOR(shadowval);
+ thread_id_t readThread = int_to_id(RDTHREADID(shadowval));
+ int writeClock = WRITEVECTOR(shadowval);
+ thread_id_t writeThread = int_to_id(WRTHREADID(shadowval));
+
+ struct RaceRecord * record=(struct RaceRecord *)calloc(1,sizeof(struct RaceRecord));
+ record->writeThread=writeThread;
+ record->writeClock=writeClock;
+
+ if (readClock!=0) {
+ record->capacity=INITCAPACITY;
+ record->thread=(thread_id_t *) malloc(sizeof(thread_id_t)*record->capacity);
+ record->readClock=(int *) malloc(sizeof(int)*record->capacity);
+ record->numReads=1;
+ record->thread[0]=readThread;
+ record->readClock[0]=readClock;
+ }
+ *shadow=(uint64_t) record;
+}
+
+static void reportDataRace() {
+ printf("The reportDataRace method should report useful things about this datarace!\n");
+}
+
+void fullRaceCheckWrite(thread_id_t thread, uint64_t * shadow, ClockVector *currClock) {
+ struct RaceRecord * record=(struct RaceRecord *) (*shadow);
+
+ /* Check for datarace against last read. */
+
+ for(int i=0;i<record->numReads;i++) {
+ int readClock = record->readClock[i];
+ thread_id_t readThread = record->thread[i];
+
+ if (readThread != thread && readClock != 0 && currClock->getClock(readThread) <= readClock) {
+ /* We have a datarace */
+ reportDataRace();
+ }
+ }
+
+ /* Check for datarace against last write. */
+
+ int writeClock = record->writeClock;
+ thread_id_t writeThread = record->writeThread;
+
+ if (writeThread != thread && writeClock != 0 && currClock->getClock(writeThread) <= writeClock) {
+ /* We have a datarace */
+ reportDataRace();
+ }
+
+ record->numReads=0;
+ record->writeThread=thread;
+ int ourClock = currClock->getClock(thread);
+ record->writeClock=ourClock;
+}
+
+void raceCheckWrite(thread_id_t thread, void *location, ClockVector *currClock) {
+ uint64_t * shadow=lookupAddressEntry(location);
+ uint64_t shadowval=*shadow;
+
+ /* Do full record */
+ if (shadowval!=0&&!ISSHORTRECORD(shadowval)) {
+ fullRaceCheckWrite(thread, shadow, currClock);
+ return;
+ }
+
+ int threadid = id_to_int(thread);
+ int ourClock = currClock->getClock(thread);
+
+ /* Thread ID is too large or clock is too large. */
+ if (threadid > MAXTHREADID || ourClock > MAXWRITEVECTOR) {
+ expandRecord(shadow);
+ fullRaceCheckWrite(thread, shadow, currClock);
+ return;
+ }
+
+ /* Check for datarace against last read. */
+
+ int readClock = READVECTOR(shadowval);
+ thread_id_t readThread = int_to_id(RDTHREADID(shadowval));
+
+ if (readThread != thread && readClock != 0 && currClock->getClock(readThread) <= readClock) {
+ /* We have a datarace */
+ reportDataRace();
+ }
+
+ /* Check for datarace against last write. */
+
+ int writeClock = WRITEVECTOR(shadowval);
+ thread_id_t writeThread = int_to_id(WRTHREADID(shadowval));
+
+ if (writeThread != thread && writeClock != 0 && currClock->getClock(writeThread) <= writeClock) {
+ /* We have a datarace */
+ reportDataRace();
+ }
+ *shadow = ENCODEOP(0, 0, threadid, ourClock);
+}
+
+void fullRaceCheckRead(thread_id_t thread, uint64_t * shadow, ClockVector *currClock) {
+ struct RaceRecord * record=(struct RaceRecord *) (*shadow);
+
+ /* Check for datarace against last write. */
+
+ int writeClock = record->writeClock;
+ thread_id_t writeThread = record->writeThread;
+
+ if (writeThread != thread && writeClock != 0 && currClock->getClock(writeThread) <= writeClock) {
+ /* We have a datarace */
+ reportDataRace();
+ }
+
+ /* Shorten vector when possible */
+
+ int copytoindex=0;
+
+ for(int i=0;i<record->numReads;i++) {
+ int readClock = record->readClock[i];
+ thread_id_t readThread = record->thread[i];
+
+ if (readThread != thread && currClock->getClock(readThread) <= readClock) {
+ /* Still need this read in vector */
+ if (copytoindex!=i) {
+ record->readClock[copytoindex]=record->readClock[i];
+ record->thread[copytoindex]=record->thread[i];
+ }
+ copytoindex++;
+ }
+ }
+
+ if (copytoindex>=record->capacity) {
+ int newCapacity=record->capacity*2;
+ thread_id_t *newthread=(thread_id_t *) malloc(sizeof(thread_id_t)*newCapacity);
+ int * newreadClock=(int *) malloc(sizeof(int)*newCapacity);
+ std::memcpy(newthread, record->thread, record->capacity*sizeof(thread_id_t));
+ std::memcpy(newreadClock, record->readClock, record->capacity*sizeof(int));
+ free(record->readClock);
+ free(record->thread);
+ record->readClock=newreadClock;
+ record->thread=newthread;
+ record->capacity=newCapacity;
+ }
+
+ int ourClock = currClock->getClock(thread);
+
+ record->thread[copytoindex]=thread;
+ record->readClock[copytoindex]=ourClock;
+ record->numReads=copytoindex+1;
+}
+
+void raceCheckRead(thread_id_t thread, void *location, ClockVector *currClock) {
+ uint64_t * shadow=lookupAddressEntry(location);
+ uint64_t shadowval=*shadow;
+
+ /* Do full record */
+ if (shadowval!=0&&!ISSHORTRECORD(shadowval)) {
+ fullRaceCheckRead(thread, shadow, currClock);
+ return;
+ }
+
+ int threadid = id_to_int(thread);
+ int ourClock = currClock->getClock(thread);
+
+ /* Thread ID is too large or clock is too large. */
+ if (threadid > MAXTHREADID || ourClock > MAXWRITEVECTOR) {
+ expandRecord(shadow);
+ fullRaceCheckRead(thread, shadow, currClock);
+ return;
+ }
+
+ /* Check for datarace against last write. */
+
+ int writeClock = WRITEVECTOR(shadowval);
+ thread_id_t writeThread = int_to_id(WRTHREADID(shadowval));
+
+ if (writeThread != thread && writeClock != 0 && currClock->getClock(writeThread) <= writeClock) {
+ /* We have a datarace */
+ reportDataRace();
+ }
+
+ int readClock = READVECTOR(shadowval);
+ thread_id_t readThread = int_to_id(RDTHREADID(shadowval));
+
+ if (readThread != thread && readClock != 0 && currClock->getClock(readThread) <= readClock) {
+ /* We don't subsume this read... Have to expand record. */
+ expandRecord(shadow);
+ fullRaceCheckRead(thread, shadow, currClock);
+ return;
+ }
+
+ *shadow = ENCODEOP(writeThread, writeClock, threadid, ourClock);
+}
--- /dev/null
+#ifndef DATARACE_H
+#include "config.h"
+#include <stdint.h>
+#include "clockvector.h"
+
+struct ShadowTable {
+ void * array[65536];
+};
+
+struct ShadowBaseTable {
+ uint64_t array[65536];
+};
+
+#define MASK16BIT 0xffff
+
+void initRaceDetector();
+void raceCheckWrite(thread_id_t thread, void *location, ClockVector *currClock);
+void raceCheckRead(thread_id_t thread, void *location, ClockVector *currClock);
+
+
+
+/** Encoding idea:
+ * (void *) Either:
+ * (1) points to a full record or
+ *
+ * (2) encodes the information in a 64 bit word. Encoding is as
+ * follows: lowest bit set to 1, next 8 bits are read thread id, next
+ * 23 bits are read clock vector, next 8 bites are write thread id,
+ * next 23 bits are write clock vector. */
+
+struct RaceRecord {
+ int *readClock;
+ thread_id_t *thread;
+ int capacity;
+ int numReads;
+ thread_id_t writeThread;
+ int writeClock;
+};
+
+#define INITCAPACITY 4
+
+#define ISSHORTRECORD(x) ((x)&0x1)
+
+#define THREADMASK 0xff
+#define RDTHREADID(x) (((x)>>1)&THREADMASK)
+#define READMASK 0x07fffff
+#define READVECTOR(x) (((x)>>9)&READMASK)
+
+#define WRTHREADID(x) (((x)>>32)&THREADMASK)
+
+#define WRITEMASK READMASK
+#define WRITEVECTOR(x) (((x)>>40)&WRITEMASK)
+
+#define ENCODEOP(rdthread, rdtime, wrthread, wrtime) (0x1ULL | ((rdthread)<<1) | ((rdtime) << 9) | (((uint64_t)wrthread)<<32) | (((uint64_t)wrtime)<<40))
+
+#define MAXTHREADID (THREADMASK-1)
+#define MAXREADVECTOR (READMASK-1)
+#define MAXWRITEVECTOR (WRITEMASK-1)
+#endif