Skip to content

Commit 909de29

Browse files
committed
fixing sync issues with dynamorio
1 parent dcdc171 commit 909de29

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

include/instrument.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stddef.h> /* for offsetof */
33
#include <string.h>
44
#include <pthread.h>
5+
#include<sys/wait.h>
56
#include <sys/syscall.h>
67
#include "dr_api.h"
78
#include "drmgr.h"

race_detector.c

+17-7
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,15 @@ void wrap_pre_unlock(void *wrapcxt, OUT void **user_data) {
192192
if (program_locks[i].unlock_count >= program_locks[i].lock_count) {
193193
program_locks[i].state = ReadHeld;
194194
}
195-
pthread_mutex_unlock(&mutex_program_locks);
196195
thread_accessed->last_locked_mutex_addr = -1;
196+
pthread_mutex_unlock(&mutex_program_locks);
197197
}
198198
// todo => handle post lock/unlock and check wether it was successfull!.
199199
void wrap_pre_lock(void *wrapcxt, OUT void **user_data) {
200+
// wait(10000000000000);
201+
202+
int i;
203+
for(i = 0; i <= 100000000; i++) {}
200204
// printf("pre LOCK\n");
201205
void *addr = drwrap_get_arg(wrapcxt, 0);
202206
// printf("locking: %ld \n", addr);
@@ -208,10 +212,11 @@ void wrap_pre_lock(void *wrapcxt, OUT void **user_data) {
208212
return;
209213
}
210214
ThreadState *thread_accessed = &program_threads[t_index];
215+
// printf("--: %d %d \n ", (usize)addr, thread_id);
211216
pthread_mutex_lock(&mutex_program_threads);
212217
thread_accessed->last_locked_mutex_addr = (usize)addr;
213218
pthread_mutex_unlock(&mutex_program_threads);
214-
int i;
219+
// int i;
215220
for (i = 0; i <= n_program_locks; i++) {
216221
if (program_locks[i].addr == (usize)addr) break;
217222
if (i >= n_program_locks) {
@@ -263,6 +268,10 @@ void wrap_pre_malloc(void *wrapcxt, OUT void **user_data) {
263268
}
264269

265270
void check_for_race(ThreadState *thread_state) {
271+
// int i;
272+
// for (i = 0; i <= thread_state->mem_write_set_len; i++) {
273+
// printf("state %d \n", thread_state->mem_write_set[i].lock_access.state);
274+
// }
266275
int thread_i, write_set_i, write_set_i_plus1, read_set_i;
267276
for (write_set_i = 0; write_set_i < thread_state->mem_write_set_len; write_set_i++) {
268277
for (thread_i = 0; thread_i < n_program_threads; thread_i++) {
@@ -282,14 +291,12 @@ void check_for_race(ThreadState *thread_state) {
282291
}
283292
// write write-read pairs
284293
for (write_set_i_plus1 = 0; write_set_i_plus1 < iterated_thread->mem_write_set_len; write_set_i_plus1++) {
285-
if (thread_state->mem_write_set[write_set_i].address_accessed == iterated_thread->mem_write_set[write_set_i_plus1].address_accessed) {
286-
if (thread_state->mem_write_set[write_set_i].callee_thread_id != iterated_thread->mem_write_set[write_set_i_plus1].callee_thread_id) {
294+
if (thread_state->mem_write_set[write_set_i].address_accessed == iterated_thread->mem_write_set[write_set_i_plus1+1].address_accessed) {
295+
if (thread_state->mem_write_set[write_set_i].callee_thread_id != iterated_thread->mem_write_set[write_set_i_plus1+1].callee_thread_id) {
287296
// printf("addr match(not smae thread id) %ld, %ld \n", thread_state->mem_write_set[write_set_i].address_accessed, iterated_thread->mem_write_set[write_set_i_plus1].address_accessed);
288-
if(thread_state->mem_write_set[write_set_i].lock_access.state != WriteHeld && iterated_thread->mem_write_set[write_set_i_plus1].lock_access.state != WriteHeld) {
297+
if(thread_state->mem_write_set[write_set_i].lock_access.state != WriteHeld && iterated_thread->mem_write_set[write_set_i_plus1+1].lock_access.state != WriteHeld) {
289298
detected_races_counter += 1;
290299
printf("race on: %ld \n", thread_state->mem_write_set[write_set_i].address_accessed);
291-
// printf("lol\n");
292-
// printf("state match %d, %d \n", thread_state->mem_write_set[write_set_i].lock_access.state, iterated_thread->mem_write_set[write_set_i_plus1].lock_access.state);
293300
break;
294301
}
295302
}
@@ -374,6 +381,7 @@ void memtrace(void *drcontext, u64 thread_id) {
374381
// todo => should not be invoked
375382
printf("no lock for addr %ld \n", mem_ref->addr);
376383
}
384+
printf("held: %d \n", program_locks[lock_state_i].state);
377385
thread_accessed->mem_write_set_len += 1;
378386
} else if(mem_ref->type == 0 || mem_ref->type == 227 || mem_ref->type == 225 || mem_ref->type == 197 || mem_ref->type == 228 || mem_ref->type == 229 || mem_ref->type == 299 || mem_ref->type == 173) {
379387
// mem read
@@ -391,8 +399,10 @@ void memtrace(void *drcontext, u64 thread_id) {
391399
} else {
392400
printf("no lock for addr %ld \n", mem_ref->addr);
393401
}
402+
printf("held: %d \n", program_locks[lock_state_i].state);
394403
thread_accessed->mem_read_set_len += 1;
395404
}
405+
396406
pthread_mutex_unlock(&mutex_program_threads);
397407
check_for_race(thread_accessed);
398408
continue_outer_loop:;

0 commit comments

Comments
 (0)