summaryrefslogtreecommitdiff
path: root/test/tsan/Darwin/external-lib.cc
blob: f0afdf1dc06033f99052540521d94bdea5770896 (plain) (blame)
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
// This file is used from other tests.
// RUN: true

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

struct MyObject;
typedef MyObject *MyObjectRef;
extern "C" {
  void InitializeLibrary();
  MyObject *ObjectCreate();
  long ObjectRead(MyObject *);
  void ObjectWrite(MyObject *, long);
  void ObjectWriteAnother(MyObject *, long);
}

struct MyObject {
  long _val;
  long _another;
};

#if defined(USE_TSAN_CALLBACKS)
static void *tag;
void *(*callback_register_tag)(const char *object_type);
void *(*callback_assign_tag)(void *addr, void *tag);
void (*callback_read)(void *addr, void *caller_pc, void *tag);
void (*callback_write)(void *addr, void *caller_pc, void *tag);
#endif

void InitializeLibrary() {
#if defined(USE_TSAN_CALLBACKS)
  callback_register_tag = (decltype(callback_register_tag))dlsym(RTLD_DEFAULT, "__tsan_external_register_tag");
  callback_assign_tag = (decltype(callback_assign_tag))dlsym(RTLD_DEFAULT, "__tsan_external_assign_tag");
  callback_read = (decltype(callback_read))dlsym(RTLD_DEFAULT, "__tsan_external_read");
  callback_write = (decltype(callback_write))dlsym(RTLD_DEFAULT, "__tsan_external_write");
  tag = callback_register_tag("MyLibrary::MyObject");
#endif
}

MyObject *ObjectCreate() {
  MyObject *ref = (MyObject *)malloc(sizeof(MyObject));
#if defined(USE_TSAN_CALLBACKS)
  callback_assign_tag(ref, tag);
#endif
  return ref;
}

long ObjectRead(MyObject *ref) {
#if defined(USE_TSAN_CALLBACKS)
  callback_read(ref, __builtin_return_address(0), tag);
#endif
  return ref->_val;
}

void ObjectWrite(MyObject *ref, long val) {
#if defined(USE_TSAN_CALLBACKS)
  callback_write(ref, __builtin_return_address(0), tag);
#endif
  ref->_val = val;
}

void ObjectWriteAnother(MyObject *ref, long val) {
#if defined(USE_TSAN_CALLBACKS)
  callback_write(ref, __builtin_return_address(0), tag);
#endif
  ref->_another = val;
}