File: java_symbolization.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (76 lines) | stat: -rw-r--r-- 2,703 bytes parent folder | download | duplicates (18)
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
69
70
71
72
73
74
75
76
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
#include "java.h"
#include <memory.h>
#include <sanitizer/tsan_interface.h>

#if (__APPLE__)
__attribute__((weak)) // Required for dyld macOS 12.0+
#endif
__attribute__((disable_sanitizer_instrumentation))
extern "C" void
__tsan_symbolize_external_ex(jptr pc,
                             void (*add_frame)(void *, const char *,
                                               const char *, int, int),
                             void *ctx) {
  if (pc == (1234 | kExternalPCBit)) {
    add_frame(ctx, "MyInnerFunc", "MyInnerFile.java", 1234, 56);
    add_frame(ctx, "MyOuterFunc", "MyOuterFile.java", 4321, 65);
  }
  if (pc == (2345 | kExternalPCBit)) {
    add_frame(ctx, "Caller1", "CallerFile.java", 111, 22);
    add_frame(ctx, "Caller2", "CallerFile.java", 333, 44);
  }
  if (pc == (3456 | kExternalPCBit)) {
    add_frame(ctx, "Allocer1", "Alloc.java", 11, 222);
    add_frame(ctx, "Allocer2", "Alloc.java", 33, 444);
  }
}

void *Thread(void *p) {
  barrier_wait(&barrier);
  __tsan_func_entry(2345 | kExternalPCBit);
  __tsan_write1_pc((jptr)p + 16, 1234 | kExternalPCBit);
  __tsan_func_exit();
  return 0;
}

jptr const kHeapSize = 64 * 1024;
jptr java_heap[kHeapSize];

int main() {
  barrier_init(&barrier, 2);
  jptr jheap = (jptr)java_heap;
  __tsan_java_init(jheap, kHeapSize);
  const int kBlockSize = 32;
  __tsan_func_entry(3456 | kExternalPCBit);
  __tsan_java_alloc(jheap, kBlockSize);
  print_address("addr:", 2, jheap, jheap + 16);
  __tsan_func_exit();
  pthread_t th;
  pthread_create(&th, 0, Thread, (void*)jheap);
  __tsan_func_entry(2345 | kExternalPCBit);
  __tsan_write1_pc(jheap + 16, 1234 | kExternalPCBit);
  __tsan_func_exit();
  barrier_wait(&barrier);
  pthread_join(th, 0);
  __tsan_java_free(jheap, kBlockSize);
  fprintf(stderr, "DONE\n");
  return __tsan_java_fini();
}

// CHECK: addr:[[BLOCK:0x[0-9,a-f]+]] [[ADDR:0x[0-9,a-f]+]]
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK:   Write of size 1 at [[ADDR]] by thread T1:
// CHECK:     #0 MyInnerFunc MyInnerFile.java:1234:56
// CHECK:     #1 MyOuterFunc MyOuterFile.java:4321:65
// CHECK:     #2 Caller1 CallerFile.java:111:22
// CHECK:     #3 Caller2 CallerFile.java:333:44
// CHECK:   Previous write of size 1 at [[ADDR]] by main thread:
// CHECK:     #0 MyInnerFunc MyInnerFile.java:1234:56
// CHECK:     #1 MyOuterFunc MyOuterFile.java:4321:65
// CHECK:     #2 Caller1 CallerFile.java:111:22
// CHECK:     #3 Caller2 CallerFile.java:333:44
// CHECK:   Location is heap block of size 32 at [[BLOCK]] allocated by main thread:
// CHECK:     #0 Allocer1 Alloc.java:11:222
// CHECK:     #1 Allocer2 Alloc.java:33:444
// CHECK: DONE